1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/renderer_context_menu/context_menu_content_type.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
10 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 using extensions::MenuItem;
17 class ContextMenuContentTypeTest : public ChromeRenderViewHostTestHarness {
19 static ContextMenuContentType* Create(
20 content::WebContents* web_contents,
21 content::ContextMenuParams& params) {
22 return new ContextMenuContentType(web_contents, params, true);
26 // Generates a ContextMenuParams that matches the specified contexts.
27 content::ContextMenuParams CreateParams(int contexts) {
28 content::ContextMenuParams rv;
29 rv.is_editable = false;
30 rv.media_type = blink::WebContextMenuData::MediaTypeNone;
31 rv.page_url = GURL("http://test.page/");
33 static const base::string16 selected_text = base::ASCIIToUTF16("sel");
34 if (contexts & MenuItem::SELECTION)
35 rv.selection_text = selected_text;
37 if (contexts & MenuItem::LINK) {
38 rv.link_url = GURL("http://test.link/");
39 rv.unfiltered_link_url = GURL("http://test.link/");
42 if (contexts & MenuItem::EDITABLE)
43 rv.is_editable = true;
45 if (contexts & MenuItem::IMAGE) {
46 rv.src_url = GURL("http://test.image/");
47 rv.media_type = blink::WebContextMenuData::MediaTypeImage;
50 if (contexts & MenuItem::VIDEO) {
51 rv.src_url = GURL("http://test.video/");
52 rv.media_type = blink::WebContextMenuData::MediaTypeVideo;
55 if (contexts & MenuItem::AUDIO) {
56 rv.src_url = GURL("http://test.audio/");
57 rv.media_type = blink::WebContextMenuData::MediaTypeAudio;
60 if (contexts & MenuItem::FRAME)
61 rv.frame_url = GURL("http://test.frame/");
66 TEST_F(ContextMenuContentTypeTest, CheckTypes) {
68 content::ContextMenuParams params = CreateParams(MenuItem::LINK);
69 scoped_ptr<ContextMenuContentType> content_type(Create(web_contents(),
71 EXPECT_TRUE(content_type->SupportsGroup(
72 ContextMenuContentType::ITEM_GROUP_LINK));
73 EXPECT_TRUE(content_type->SupportsGroup(
74 ContextMenuContentType::ITEM_GROUP_ALL_EXTENSION));
75 EXPECT_FALSE(content_type->SupportsGroup(
76 ContextMenuContentType::ITEM_GROUP_CURRENT_EXTENSION));
80 content::ContextMenuParams params = CreateParams(MenuItem::SELECTION);
81 scoped_ptr<ContextMenuContentType> content_type(Create(web_contents(),
83 EXPECT_FALSE(content_type->SupportsGroup(
84 ContextMenuContentType::ITEM_GROUP_LINK));
85 EXPECT_TRUE(content_type->SupportsGroup(
86 ContextMenuContentType::ITEM_GROUP_COPY));
87 EXPECT_FALSE(content_type->SupportsGroup(
88 ContextMenuContentType::ITEM_GROUP_EDITABLE));
89 EXPECT_TRUE(content_type->SupportsGroup(
90 ContextMenuContentType::ITEM_GROUP_SEARCH_PROVIDER));
94 content::ContextMenuParams params = CreateParams(MenuItem::EDITABLE);
95 scoped_ptr<ContextMenuContentType> content_type(Create(web_contents(),
97 EXPECT_FALSE(content_type->SupportsGroup(
98 ContextMenuContentType::ITEM_GROUP_LINK));
99 EXPECT_FALSE(content_type->SupportsGroup(
100 ContextMenuContentType::ITEM_GROUP_COPY));
101 EXPECT_TRUE(content_type->SupportsGroup(
102 ContextMenuContentType::ITEM_GROUP_EDITABLE));
106 content::ContextMenuParams params = CreateParams(MenuItem::IMAGE);
107 scoped_ptr<ContextMenuContentType> content_type(Create(web_contents(),
109 EXPECT_TRUE(content_type->SupportsGroup(
110 ContextMenuContentType::ITEM_GROUP_MEDIA_IMAGE));
111 EXPECT_TRUE(content_type->SupportsGroup(
112 ContextMenuContentType::ITEM_GROUP_SEARCHWEBFORIMAGE));
113 EXPECT_TRUE(content_type->SupportsGroup(
114 ContextMenuContentType::ITEM_GROUP_PRINT));
116 EXPECT_FALSE(content_type->SupportsGroup(
117 ContextMenuContentType::ITEM_GROUP_MEDIA_VIDEO));
118 EXPECT_FALSE(content_type->SupportsGroup(
119 ContextMenuContentType::ITEM_GROUP_MEDIA_AUDIO));
120 EXPECT_FALSE(content_type->SupportsGroup(
121 ContextMenuContentType::ITEM_GROUP_MEDIA_PLUGIN));
125 content::ContextMenuParams params = CreateParams(MenuItem::VIDEO);
126 scoped_ptr<ContextMenuContentType> content_type(Create(web_contents(),
128 EXPECT_TRUE(content_type->SupportsGroup(
129 ContextMenuContentType::ITEM_GROUP_MEDIA_VIDEO));
131 EXPECT_FALSE(content_type->SupportsGroup(
132 ContextMenuContentType::ITEM_GROUP_MEDIA_IMAGE));
133 EXPECT_FALSE(content_type->SupportsGroup(
134 ContextMenuContentType::ITEM_GROUP_MEDIA_AUDIO));
135 EXPECT_FALSE(content_type->SupportsGroup(
136 ContextMenuContentType::ITEM_GROUP_MEDIA_PLUGIN));
140 content::ContextMenuParams params = CreateParams(MenuItem::AUDIO);
141 scoped_ptr<ContextMenuContentType> content_type(Create(web_contents(),
143 EXPECT_TRUE(content_type->SupportsGroup(
144 ContextMenuContentType::ITEM_GROUP_MEDIA_AUDIO));
146 EXPECT_FALSE(content_type->SupportsGroup(
147 ContextMenuContentType::ITEM_GROUP_MEDIA_IMAGE));
148 EXPECT_FALSE(content_type->SupportsGroup(
149 ContextMenuContentType::ITEM_GROUP_MEDIA_VIDEO));
150 EXPECT_FALSE(content_type->SupportsGroup(
151 ContextMenuContentType::ITEM_GROUP_MEDIA_PLUGIN));
155 content::ContextMenuParams params = CreateParams(MenuItem::FRAME);
156 scoped_ptr<ContextMenuContentType> content_type(Create(web_contents(),
158 EXPECT_TRUE(content_type->SupportsGroup(
159 ContextMenuContentType::ITEM_GROUP_FRAME));
160 EXPECT_TRUE(content_type->SupportsGroup(
161 ContextMenuContentType::ITEM_GROUP_PAGE));