OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 5 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
6 | 6 |
7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "chrome/app/chrome_command_ids.h" | 8 #include "chrome/app/chrome_command_ids.h" |
9 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" | 9 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" |
10 #include "chrome/browser/extensions/extension_prefs.h" | 10 #include "chrome/browser/extensions/extension_prefs.h" |
11 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 11 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
12 #include "chrome/browser/tab_contents/render_view_context_menu_test_util.h" | 12 #include "chrome/browser/tab_contents/render_view_context_menu_test_util.h" |
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
14 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
17 #include "content/public/test/test_browser_thread.h" | |
18 #include "extensions/common/url_pattern.h" | 16 #include "extensions/common/url_pattern.h" |
19 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" |
22 | 20 |
23 using extensions::MenuItem; | 21 using extensions::MenuItem; |
24 using extensions::URLPatternSet; | 22 using extensions::URLPatternSet; |
25 | 23 |
26 class RenderViewContextMenuTest : public testing::Test { | 24 class RenderViewContextMenuTest : public testing::Test { |
27 public: | |
28 RenderViewContextMenuTest() { } | |
29 | |
30 protected: | 25 protected: |
31 // Proxy defined here to minimize friend classes in RenderViewContextMenu | 26 // Proxy defined here to minimize friend classes in RenderViewContextMenu |
32 static bool ExtensionContextAndPatternMatch( | 27 static bool ExtensionContextAndPatternMatch( |
33 const content::ContextMenuParams& params, | 28 const content::ContextMenuParams& params, |
34 MenuItem::ContextList contexts, | 29 MenuItem::ContextList contexts, |
35 const URLPatternSet& patterns) { | 30 const URLPatternSet& patterns) { |
36 return RenderViewContextMenu::ExtensionContextAndPatternMatch(params, | 31 return RenderViewContextMenu::ExtensionContextAndPatternMatch(params, |
37 contexts, patterns); | 32 contexts, patterns); |
38 } | 33 } |
39 | |
40 private: | |
41 DISALLOW_COPY_AND_ASSIGN(RenderViewContextMenuTest); | |
42 }; | 34 }; |
43 | 35 |
44 // Generates a ContextMenuParams that matches the specified contexts. | 36 // Generates a ContextMenuParams that matches the specified contexts. |
45 static content::ContextMenuParams CreateParams(int contexts) { | 37 static content::ContextMenuParams CreateParams(int contexts) { |
46 content::ContextMenuParams rv; | 38 content::ContextMenuParams rv; |
47 rv.is_editable = false; | 39 rv.is_editable = false; |
48 rv.media_type = WebKit::WebContextMenuData::MediaTypeNone; | 40 rv.media_type = WebKit::WebContextMenuData::MediaTypeNone; |
49 rv.page_url = GURL("http://test.page/"); | 41 rv.page_url = GURL("http://test.page/"); |
50 | 42 |
51 static const char16 selected_text[] = { 's', 'e', 'l', 0 }; | 43 static const char16 selected_text[] = { 's', 'e', 'l', 0 }; |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 contexts.Add(MenuItem::SELECTION); | 235 contexts.Add(MenuItem::SELECTION); |
244 contexts.Add(MenuItem::IMAGE); | 236 contexts.Add(MenuItem::IMAGE); |
245 | 237 |
246 URLPatternSet patterns = CreatePatternSet("*://test.none/*"); | 238 URLPatternSet patterns = CreatePatternSet("*://test.none/*"); |
247 | 239 |
248 EXPECT_TRUE(ExtensionContextAndPatternMatch(params, contexts, patterns)); | 240 EXPECT_TRUE(ExtensionContextAndPatternMatch(params, contexts, patterns)); |
249 } | 241 } |
250 | 242 |
251 class RenderViewContextMenuPrefsTest : public ChromeRenderViewHostTestHarness { | 243 class RenderViewContextMenuPrefsTest : public ChromeRenderViewHostTestHarness { |
252 public: | 244 public: |
253 RenderViewContextMenuPrefsTest() | 245 virtual void SetUp() OVERRIDE { |
254 : browser_thread_(content::BrowserThread::UI, &message_loop_), | 246 ChromeRenderViewHostTestHarness::SetUp(); |
255 registry_(profile(), NULL) {} | 247 registry_.reset(new ProtocolHandlerRegistry(profile(), NULL)); |
| 248 } |
| 249 |
| 250 virtual void TearDown() OVERRIDE { |
| 251 registry_.reset(); |
| 252 ChromeRenderViewHostTestHarness::TearDown(); |
| 253 } |
256 | 254 |
257 TestRenderViewContextMenu* CreateContextMenu() { | 255 TestRenderViewContextMenu* CreateContextMenu() { |
258 content::ContextMenuParams params = CreateParams(MenuItem::LINK); | 256 content::ContextMenuParams params = CreateParams(MenuItem::LINK); |
259 params.unfiltered_link_url = params.link_url; | 257 params.unfiltered_link_url = params.link_url; |
260 content::WebContents* wc = web_contents(); | 258 content::WebContents* wc = web_contents(); |
261 TestRenderViewContextMenu* menu = new TestRenderViewContextMenu( | 259 TestRenderViewContextMenu* menu = new TestRenderViewContextMenu( |
262 wc, params); | 260 wc, params); |
263 // TestingProfile (returned by profile()) does not provide a protocol | 261 // TestingProfile (returned by profile()) does not provide a protocol |
264 // registry. | 262 // registry. |
265 menu->protocol_handler_registry_ = ®istry_; | 263 menu->protocol_handler_registry_ = registry_.get(); |
266 menu->Init(); | 264 menu->Init(); |
267 return menu; | 265 return menu; |
268 } | 266 } |
269 | 267 |
270 private: | 268 private: |
271 content::TestBrowserThread browser_thread_; | 269 scoped_ptr<ProtocolHandlerRegistry> registry_; |
272 ProtocolHandlerRegistry registry_; | |
273 | |
274 DISALLOW_COPY_AND_ASSIGN(RenderViewContextMenuPrefsTest); | |
275 }; | 270 }; |
276 | 271 |
277 // Verifies when Incognito Mode is not available (disabled by policy), | 272 // Verifies when Incognito Mode is not available (disabled by policy), |
278 // Open Link in Incognito Window link in the context menu is disabled. | 273 // Open Link in Incognito Window link in the context menu is disabled. |
279 TEST_F(RenderViewContextMenuPrefsTest, | 274 TEST_F(RenderViewContextMenuPrefsTest, |
280 DisableOpenInIncognitoWindowWhenIncognitoIsDisabled) { | 275 DisableOpenInIncognitoWindowWhenIncognitoIsDisabled) { |
281 scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenu()); | 276 scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenu()); |
282 | 277 |
283 // Initially the Incognito mode is be enabled. So is the Open Link in | 278 // Initially the Incognito mode is be enabled. So is the Open Link in |
284 // Incognito Window link. | 279 // Incognito Window link. |
285 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD)); | 280 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD)); |
286 EXPECT_TRUE( | 281 EXPECT_TRUE( |
287 menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD)); | 282 menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD)); |
288 | 283 |
289 // Disable Incognito mode. | 284 // Disable Incognito mode. |
290 IncognitoModePrefs::SetAvailability(profile()->GetPrefs(), | 285 IncognitoModePrefs::SetAvailability(profile()->GetPrefs(), |
291 IncognitoModePrefs::DISABLED); | 286 IncognitoModePrefs::DISABLED); |
292 menu.reset(CreateContextMenu()); | 287 menu.reset(CreateContextMenu()); |
293 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD)); | 288 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD)); |
294 EXPECT_FALSE( | 289 EXPECT_FALSE( |
295 menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD)); | 290 menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD)); |
296 } | 291 } |
OLD | NEW |