OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/menus/menu_model.h" | 5 #include "app/menus/menu_model.h" |
6 #include "chrome/app/chrome_dll_resource.h" | 6 #include "chrome/app/chrome_dll_resource.h" |
7 #include "chrome/browser/browser.h" | 7 #include "chrome/browser/browser.h" |
8 #include "chrome/browser/extensions/extension_browsertest.h" | 8 #include "chrome/browser/extensions/extension_browsertest.h" |
9 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 9 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
10 #include "chrome/browser/tab_contents/tab_contents.h" | 10 #include "chrome/browser/tab_contents/tab_contents.h" |
11 #include "chrome/common/chrome_switches.h" | 11 #include "chrome/common/chrome_switches.h" |
12 #include "chrome/test/ui_test_utils.h" | 12 #include "chrome/test/ui_test_utils.h" |
13 #include "third_party/WebKit/WebKit/chromium/public/WebContextMenuData.h" | 13 #include "third_party/WebKit/WebKit/chromium/public/WebContextMenuData.h" |
14 #include "webkit/glue/context_menu.h" | 14 #include "webkit/glue/context_menu.h" |
15 | 15 |
16 using menus::MenuModel; | 16 using menus::MenuModel; |
17 using WebKit::WebContextMenuData; | 17 using WebKit::WebContextMenuData; |
18 | 18 |
19 // This test class helps us sidestep platform-specific issues with popping up a | 19 // This test class helps us sidestep platform-specific issues with popping up a |
20 // real context menu, while still running through the actual code in | 20 // real context menu, while still running through the actual code in |
21 // RenderViewContextMenu where extension items get added and executed. | 21 // RenderViewContextMenu where extension items get added and executed. |
22 class TestRenderViewContextMenu : public RenderViewContextMenu { | 22 class TestRenderViewContextMenu : public RenderViewContextMenu { |
23 public: | 23 public: |
24 TestRenderViewContextMenu(TabContents* tab_contents, | 24 TestRenderViewContextMenu(TabContents* tab_contents, |
25 const ContextMenuParams& params) | 25 const ContextMenuParams& params) |
26 : RenderViewContextMenu(tab_contents, params) {} | 26 : RenderViewContextMenu(tab_contents, params) {} |
27 | 27 |
28 virtual ~TestRenderViewContextMenu() {} | 28 virtual ~TestRenderViewContextMenu() {} |
29 | 29 |
| 30 bool HasExtensionItemWithTitle(std::string title) { |
| 31 std::map<int, ExtensionMenuItem::Id>::iterator i; |
| 32 for (i = extension_item_map_.begin(); i != extension_item_map_.end(); ++i) { |
| 33 int id = i->first; |
| 34 ExtensionMenuItem* item = GetExtensionMenuItem(id); |
| 35 if (item && item->title() == title) { |
| 36 return true; |
| 37 } |
| 38 } |
| 39 return false; |
| 40 } |
| 41 |
30 protected: | 42 protected: |
31 virtual bool GetAcceleratorForCommandId(int command_id, | 43 virtual bool GetAcceleratorForCommandId(int command_id, |
32 menus::Accelerator* accelerator) { | 44 menus::Accelerator* accelerator) { |
33 // None of our commands have accelerators, so always return false. | 45 // None of our commands have accelerators, so always return false. |
34 return false; | 46 return false; |
35 } | 47 } |
36 virtual void PlatformInit() {} | 48 virtual void PlatformInit() {} |
37 }; | 49 }; |
38 | 50 |
39 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, ContextMenusSimple) { | 51 class ExtensionContextMenuBrowserTest : public ExtensionBrowserTest { |
40 CommandLine::ForCurrentProcess()->AppendSwitch( | 52 public: |
41 switches::kEnableExperimentalExtensionApis); | 53 // Helper to load an extension from context_menus/|subdirectory| in the |
42 FilePath extension_dir = test_data_dir_.AppendASCII("context_menus"); | 54 // extensions test data dir. |
43 ASSERT_TRUE(LoadExtension(extension_dir)); | 55 void LoadContextMenuExtension(std::string subdirectory) { |
| 56 FilePath extension_dir = |
| 57 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory); |
| 58 ASSERT_TRUE(LoadExtension(extension_dir)); |
| 59 } |
| 60 |
| 61 // This creates a test menu using |params|, looks for an extension item with |
| 62 // the given |title|, and returns true if the item was found. |
| 63 bool MenuHasItemWithTitle(const ContextMenuParams& params, |
| 64 std::string title) { |
| 65 TabContents* tab_contents = browser()->GetSelectedTabContents(); |
| 66 TestRenderViewContextMenu menu(tab_contents, params); |
| 67 menu.Init(); |
| 68 return menu.HasExtensionItemWithTitle(title); |
| 69 } |
| 70 }; |
| 71 |
| 72 // Returns a new ContextMenuParams initialized with reasonable default values. |
| 73 ContextMenuParams* CreateParams() { |
| 74 WebContextMenuData data; |
| 75 ContextMenuParams* params = new ContextMenuParams(data); |
| 76 return params; |
| 77 } |
| 78 |
| 79 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Simple) { |
| 80 LoadContextMenuExtension("simple"); |
44 | 81 |
45 // The extension's background page will create a context menu item and then | 82 // The extension's background page will create a context menu item and then |
46 // cause a navigation on success - we wait for that here. | 83 // cause a navigation on success - we wait for that here. |
47 ASSERT_TRUE(ui_test_utils::WaitForNavigationsInCurrentTab(browser(), 1)); | 84 ASSERT_TRUE(ui_test_utils::WaitForNavigationsInCurrentTab(browser(), 1)); |
48 | 85 |
49 // Initialize the data we need to create a context menu. | 86 // Initialize the data we need to create a context menu. |
50 TabContents* tab_contents = browser()->GetSelectedTabContents(); | 87 TabContents* tab_contents = browser()->GetSelectedTabContents(); |
51 ContextMenuParams params; | 88 scoped_ptr<ContextMenuParams> params(CreateParams()); |
52 params.media_type = WebContextMenuData::MediaTypeNone; | 89 params->page_url = GURL("http://www.google.com"); |
53 params.x = 0; | |
54 params.y = 0; | |
55 params.is_image_blocked = false; | |
56 params.frame_url = tab_contents->GetURL(); | |
57 params.media_flags = 0; | |
58 params.spellcheck_enabled = false; | |
59 params.is_editable = false; | |
60 | 90 |
61 // Create and build our test context menu. | 91 // Create and build our test context menu. |
62 TestRenderViewContextMenu menu(tab_contents, params); | 92 TestRenderViewContextMenu menu(tab_contents, *params); |
63 menu.Init(); | 93 menu.Init(); |
64 | 94 |
65 // Look for the extension item in the menu, and execute it. | 95 // Look for the extension item in the menu, and execute it. |
66 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; | 96 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
67 ASSERT_TRUE(menu.IsCommandIdEnabled(command_id)); | 97 ASSERT_TRUE(menu.IsCommandIdEnabled(command_id)); |
68 menu.ExecuteCommand(command_id); | 98 menu.ExecuteCommand(command_id); |
69 | 99 |
70 // The onclick handler for the extension item will cause a navigation - we | 100 // The onclick handler for the extension item will cause a navigation - we |
71 // wait for that here. | 101 // wait for that here. |
72 ASSERT_TRUE(ui_test_utils::WaitForNavigationsInCurrentTab(browser(), 1)); | 102 ASSERT_TRUE(ui_test_utils::WaitForNavigationsInCurrentTab(browser(), 1)); |
73 } | 103 } |
| 104 |
| 105 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Patterns) { |
| 106 // The js test code will create two items with patterns and then navigate a |
| 107 // tab to tell us to proceed. |
| 108 LoadContextMenuExtension("patterns"); |
| 109 ASSERT_TRUE(ui_test_utils::WaitForNavigationsInCurrentTab(browser(), 1)); |
| 110 |
| 111 scoped_ptr<ContextMenuParams> params(CreateParams()); |
| 112 |
| 113 // Check that a document url that should match the items' patterns appears. |
| 114 params->frame_url = GURL("http://www.google.com"); |
| 115 ASSERT_TRUE(MenuHasItemWithTitle(*params, std::string("test_item1"))); |
| 116 ASSERT_TRUE(MenuHasItemWithTitle(*params, std::string("test_item2"))); |
| 117 |
| 118 // Now check for a non-matching url. |
| 119 params->frame_url = GURL("http://www.test.com"); |
| 120 ASSERT_FALSE(MenuHasItemWithTitle(*params, std::string("test_item1"))); |
| 121 ASSERT_FALSE(MenuHasItemWithTitle(*params, std::string("test_item2"))); |
| 122 } |
OLD | NEW |