1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/renderer_context_menu/context_menu_content_type_factory.h"
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/test/base/chrome_render_view_host_test_harness.h"
105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/renderer_context_menu/context_menu_content_type.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "url/gurl.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using extensions::MenuItem;
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class ContextMenuContentTypeTest : public ChromeRenderViewHostTestHarness {
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static ContextMenuContentType* Create(
19c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      content::WebContents* web_contents,
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      content::ContextMenuParams& params) {
215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return ContextMenuContentTypeFactory::SetInternalResourcesURLChecker(
225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        new ContextMenuContentType(web_contents, params, true));
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Generates a ContextMenuParams that matches the specified contexts.
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)content::ContextMenuParams CreateParams(int contexts) {
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  content::ContextMenuParams rv;
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  rv.is_editable = false;
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  rv.media_type = blink::WebContextMenuData::MediaTypeNone;
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  rv.page_url = GURL("http://test.page/");
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static const base::string16 selected_text = base::ASCIIToUTF16("sel");
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (contexts & MenuItem::SELECTION)
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    rv.selection_text = selected_text;
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (contexts & MenuItem::LINK) {
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    rv.link_url = GURL("http://test.link/");
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    rv.unfiltered_link_url = GURL("http://test.link/");
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (contexts & MenuItem::EDITABLE)
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    rv.is_editable = true;
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (contexts & MenuItem::IMAGE) {
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    rv.src_url = GURL("http://test.image/");
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    rv.media_type = blink::WebContextMenuData::MediaTypeImage;
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (contexts & MenuItem::VIDEO) {
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    rv.src_url = GURL("http://test.video/");
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    rv.media_type = blink::WebContextMenuData::MediaTypeVideo;
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (contexts & MenuItem::AUDIO) {
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    rv.src_url = GURL("http://test.audio/");
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    rv.media_type = blink::WebContextMenuData::MediaTypeAudio;
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (contexts & MenuItem::FRAME)
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    rv.frame_url = GURL("http://test.frame/");
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return rv;
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(ContextMenuContentTypeTest, CheckTypes) {
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  {
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::ContextMenuParams params = CreateParams(MenuItem::LINK);
69c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    scoped_ptr<ContextMenuContentType> content_type(Create(web_contents(),
70c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                                           params));
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_TRUE(content_type->SupportsGroup(
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_LINK));
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_TRUE(content_type->SupportsGroup(
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_ALL_EXTENSION));
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_FALSE(content_type->SupportsGroup(
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_CURRENT_EXTENSION));
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  {
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::ContextMenuParams params = CreateParams(MenuItem::SELECTION);
81c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    scoped_ptr<ContextMenuContentType> content_type(Create(web_contents(),
82c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                                           params));
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_FALSE(content_type->SupportsGroup(
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_LINK));
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_TRUE(content_type->SupportsGroup(
86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_COPY));
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_FALSE(content_type->SupportsGroup(
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_EDITABLE));
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_TRUE(content_type->SupportsGroup(
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_SEARCH_PROVIDER));
91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  {
94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::ContextMenuParams params = CreateParams(MenuItem::EDITABLE);
95c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    scoped_ptr<ContextMenuContentType> content_type(Create(web_contents(),
96c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                                           params));
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_FALSE(content_type->SupportsGroup(
98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_LINK));
99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_FALSE(content_type->SupportsGroup(
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_COPY));
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_TRUE(content_type->SupportsGroup(
102a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_EDITABLE));
103a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
104a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
105a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  {
106a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::ContextMenuParams params = CreateParams(MenuItem::IMAGE);
107c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    scoped_ptr<ContextMenuContentType> content_type(Create(web_contents(),
108c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                                           params));
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_TRUE(content_type->SupportsGroup(
110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_MEDIA_IMAGE));
111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_TRUE(content_type->SupportsGroup(
112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_SEARCHWEBFORIMAGE));
113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_TRUE(content_type->SupportsGroup(
114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_PRINT));
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
116a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_FALSE(content_type->SupportsGroup(
117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_MEDIA_VIDEO));
118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_FALSE(content_type->SupportsGroup(
119a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_MEDIA_AUDIO));
120a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_FALSE(content_type->SupportsGroup(
121a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_MEDIA_PLUGIN));
122a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
123a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
124a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  {
125a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::ContextMenuParams params = CreateParams(MenuItem::VIDEO);
126c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    scoped_ptr<ContextMenuContentType> content_type(Create(web_contents(),
127c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                                           params));
128a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_TRUE(content_type->SupportsGroup(
129a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_MEDIA_VIDEO));
130a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
131a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_FALSE(content_type->SupportsGroup(
132a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_MEDIA_IMAGE));
133a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_FALSE(content_type->SupportsGroup(
134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_MEDIA_AUDIO));
135a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_FALSE(content_type->SupportsGroup(
136a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_MEDIA_PLUGIN));
137a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
138a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
139a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  {
140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::ContextMenuParams params = CreateParams(MenuItem::AUDIO);
141c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    scoped_ptr<ContextMenuContentType> content_type(Create(web_contents(),
142c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                                           params));
143a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_TRUE(content_type->SupportsGroup(
144a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_MEDIA_AUDIO));
145a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_FALSE(content_type->SupportsGroup(
147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_MEDIA_IMAGE));
148a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_FALSE(content_type->SupportsGroup(
149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_MEDIA_VIDEO));
150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_FALSE(content_type->SupportsGroup(
151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_MEDIA_PLUGIN));
152a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
154a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  {
155a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::ContextMenuParams params = CreateParams(MenuItem::FRAME);
156c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    scoped_ptr<ContextMenuContentType> content_type(Create(web_contents(),
157c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                                           params));
158a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_TRUE(content_type->SupportsGroup(
159a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_FRAME));
160a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_TRUE(content_type->SupportsGroup(
161a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    ContextMenuContentType::ITEM_GROUP_PAGE));
162a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
163a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
164