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::RenderFrameHost* render_frame_host,
21 content::ContextMenuParams& params) {
22 return new ContextMenuContentType(render_frame_host, 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(main_rfh(), params));
70 EXPECT_TRUE(content_type->SupportsGroup(
71 ContextMenuContentType::ITEM_GROUP_LINK));
72 EXPECT_TRUE(content_type->SupportsGroup(
73 ContextMenuContentType::ITEM_GROUP_ALL_EXTENSION));
74 EXPECT_FALSE(content_type->SupportsGroup(
75 ContextMenuContentType::ITEM_GROUP_CURRENT_EXTENSION));
79 content::ContextMenuParams params = CreateParams(MenuItem::SELECTION);
80 scoped_ptr<ContextMenuContentType> content_type(Create(main_rfh(), params));
81 EXPECT_FALSE(content_type->SupportsGroup(
82 ContextMenuContentType::ITEM_GROUP_LINK));
83 EXPECT_TRUE(content_type->SupportsGroup(
84 ContextMenuContentType::ITEM_GROUP_COPY));
85 EXPECT_FALSE(content_type->SupportsGroup(
86 ContextMenuContentType::ITEM_GROUP_EDITABLE));
87 EXPECT_TRUE(content_type->SupportsGroup(
88 ContextMenuContentType::ITEM_GROUP_SEARCH_PROVIDER));
92 content::ContextMenuParams params = CreateParams(MenuItem::EDITABLE);
93 scoped_ptr<ContextMenuContentType> content_type(Create(main_rfh(), params));
94 EXPECT_FALSE(content_type->SupportsGroup(
95 ContextMenuContentType::ITEM_GROUP_LINK));
96 EXPECT_FALSE(content_type->SupportsGroup(
97 ContextMenuContentType::ITEM_GROUP_COPY));
98 EXPECT_TRUE(content_type->SupportsGroup(
99 ContextMenuContentType::ITEM_GROUP_EDITABLE));
103 content::ContextMenuParams params = CreateParams(MenuItem::IMAGE);
104 scoped_ptr<ContextMenuContentType> content_type(Create(main_rfh(), params));
105 EXPECT_TRUE(content_type->SupportsGroup(
106 ContextMenuContentType::ITEM_GROUP_MEDIA_IMAGE));
107 EXPECT_TRUE(content_type->SupportsGroup(
108 ContextMenuContentType::ITEM_GROUP_SEARCHWEBFORIMAGE));
109 EXPECT_TRUE(content_type->SupportsGroup(
110 ContextMenuContentType::ITEM_GROUP_PRINT));
112 EXPECT_FALSE(content_type->SupportsGroup(
113 ContextMenuContentType::ITEM_GROUP_MEDIA_VIDEO));
114 EXPECT_FALSE(content_type->SupportsGroup(
115 ContextMenuContentType::ITEM_GROUP_MEDIA_AUDIO));
116 EXPECT_FALSE(content_type->SupportsGroup(
117 ContextMenuContentType::ITEM_GROUP_MEDIA_PLUGIN));
121 content::ContextMenuParams params = CreateParams(MenuItem::VIDEO);
122 scoped_ptr<ContextMenuContentType> content_type(Create(main_rfh(), params));
123 EXPECT_TRUE(content_type->SupportsGroup(
124 ContextMenuContentType::ITEM_GROUP_MEDIA_VIDEO));
126 EXPECT_FALSE(content_type->SupportsGroup(
127 ContextMenuContentType::ITEM_GROUP_MEDIA_IMAGE));
128 EXPECT_FALSE(content_type->SupportsGroup(
129 ContextMenuContentType::ITEM_GROUP_MEDIA_AUDIO));
130 EXPECT_FALSE(content_type->SupportsGroup(
131 ContextMenuContentType::ITEM_GROUP_MEDIA_PLUGIN));
135 content::ContextMenuParams params = CreateParams(MenuItem::AUDIO);
136 scoped_ptr<ContextMenuContentType> content_type(Create(main_rfh(), params));
137 EXPECT_TRUE(content_type->SupportsGroup(
138 ContextMenuContentType::ITEM_GROUP_MEDIA_AUDIO));
140 EXPECT_FALSE(content_type->SupportsGroup(
141 ContextMenuContentType::ITEM_GROUP_MEDIA_IMAGE));
142 EXPECT_FALSE(content_type->SupportsGroup(
143 ContextMenuContentType::ITEM_GROUP_MEDIA_VIDEO));
144 EXPECT_FALSE(content_type->SupportsGroup(
145 ContextMenuContentType::ITEM_GROUP_MEDIA_PLUGIN));
149 content::ContextMenuParams params = CreateParams(MenuItem::FRAME);
150 scoped_ptr<ContextMenuContentType> content_type(Create(main_rfh(), params));
151 EXPECT_TRUE(content_type->SupportsGroup(
152 ContextMenuContentType::ITEM_GROUP_FRAME));
153 EXPECT_TRUE(content_type->SupportsGroup(
154 ContextMenuContentType::ITEM_GROUP_PAGE));