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 "chrome/app/chrome_dll_resource.h" | 6 #include "chrome/app/chrome_dll_resource.h" |
6 #include "chrome/browser/browser.h" | 7 #include "chrome/browser/browser.h" |
7 #include "chrome/browser/extensions/extension_browsertest.h" | 8 #include "chrome/browser/extensions/extension_browsertest.h" |
| 9 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 10 #include "chrome/browser/extensions/extensions_service.h" |
8 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 11 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
9 #include "chrome/browser/tab_contents/tab_contents.h" | 12 #include "chrome/browser/tab_contents/tab_contents.h" |
10 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
11 #include "chrome/test/ui_test_utils.h" | 14 #include "chrome/test/ui_test_utils.h" |
| 15 #include "net/base/mock_host_resolver.h" |
12 #include "third_party/WebKit/WebKit/chromium/public/WebContextMenuData.h" | 16 #include "third_party/WebKit/WebKit/chromium/public/WebContextMenuData.h" |
13 #include "webkit/glue/context_menu.h" | 17 #include "webkit/glue/context_menu.h" |
14 | 18 |
| 19 using menus::MenuModel; |
15 using WebKit::WebContextMenuData; | 20 using WebKit::WebContextMenuData; |
16 | 21 |
17 // This test class helps us sidestep platform-specific issues with popping up a | 22 // This test class helps us sidestep platform-specific issues with popping up a |
18 // real context menu, while still running through the actual code in | 23 // real context menu, while still running through the actual code in |
19 // RenderViewContextMenu where extension items get added and executed. | 24 // RenderViewContextMenu where extension items get added and executed. |
20 class TestRenderViewContextMenu : public RenderViewContextMenu { | 25 class TestRenderViewContextMenu : public RenderViewContextMenu { |
21 public: | 26 public: |
22 TestRenderViewContextMenu(TabContents* tab_contents, | 27 TestRenderViewContextMenu(TabContents* tab_contents, |
23 const ContextMenuParams& params) | 28 const ContextMenuParams& params) |
24 : RenderViewContextMenu(tab_contents, params) {} | 29 : RenderViewContextMenu(tab_contents, params) {} |
25 | 30 |
26 virtual ~TestRenderViewContextMenu() {} | 31 virtual ~TestRenderViewContextMenu() {} |
27 | 32 |
28 bool HasExtensionItemWithTitle(std::string title) { | 33 bool HasExtensionItemWithLabel(const std::string& label) { |
| 34 string16 label16 = UTF8ToUTF16(label); |
29 std::map<int, ExtensionMenuItem::Id>::iterator i; | 35 std::map<int, ExtensionMenuItem::Id>::iterator i; |
30 for (i = extension_item_map_.begin(); i != extension_item_map_.end(); ++i) { | 36 for (i = extension_item_map_.begin(); i != extension_item_map_.end(); ++i) { |
31 int id = i->first; | 37 const ExtensionMenuItem::Id& id = i->second; |
32 ExtensionMenuItem* item = GetExtensionMenuItem(id); | 38 string16 tmp_label; |
33 if (item && item->title() == title) { | 39 EXPECT_TRUE(GetItemLabel(id, &tmp_label)); |
| 40 if (tmp_label == label16) |
34 return true; | 41 return true; |
35 } | 42 } |
36 } | 43 return false; |
37 return false; | 44 } |
| 45 |
| 46 // Looks in the menu for an extension item with |id|, and if it is found and |
| 47 // has a label, that is put in |result| and we return true. Otherwise returns |
| 48 // false. |
| 49 bool GetItemLabel(const ExtensionMenuItem::Id& id, string16* result) { |
| 50 int command_id = 0; |
| 51 if (!FindCommandId(id, &command_id)) |
| 52 return false; |
| 53 |
| 54 MenuModel* model = NULL; |
| 55 int index = -1; |
| 56 if (!GetMenuModelAndItemIndex(command_id, &model, &index)) { |
| 57 return false; |
| 58 } |
| 59 *result = model->GetLabelAt(index); |
| 60 return true; |
38 } | 61 } |
39 | 62 |
40 protected: | 63 protected: |
| 64 // These two functions implement pure virtual methods of |
| 65 // RenderViewContextMenu. |
41 virtual bool GetAcceleratorForCommandId(int command_id, | 66 virtual bool GetAcceleratorForCommandId(int command_id, |
42 menus::Accelerator* accelerator) { | 67 menus::Accelerator* accelerator) { |
43 // None of our commands have accelerators, so always return false. | 68 // None of our commands have accelerators, so always return false. |
44 return false; | 69 return false; |
45 } | 70 } |
46 virtual void PlatformInit() {} | 71 virtual void PlatformInit() {} |
| 72 |
| 73 |
| 74 // Given an extension menu item id, tries to find the corresponding command id |
| 75 // in the menu. |
| 76 bool FindCommandId(const ExtensionMenuItem::Id& id, int* command_id) { |
| 77 std::map<int, ExtensionMenuItem::Id>::const_iterator i; |
| 78 for (i = extension_item_map_.begin(); i != extension_item_map_.end(); ++i) { |
| 79 if (i->second == id) { |
| 80 *command_id = i->first; |
| 81 return true; |
| 82 } |
| 83 } |
| 84 return false; |
| 85 } |
| 86 |
| 87 // Searches for an menu item with |command_id|. If it's found, the return |
| 88 // value is true and the model and index where it appears in that model are |
| 89 // returned in |found_model| and |found_index|. Otherwise returns false. |
| 90 bool GetMenuModelAndItemIndex(int command_id, |
| 91 MenuModel** found_model, |
| 92 int *found_index) { |
| 93 std::vector<MenuModel*> models_to_search; |
| 94 models_to_search.push_back(&menu_model_); |
| 95 |
| 96 while (!models_to_search.empty()) { |
| 97 MenuModel* model = models_to_search.back(); |
| 98 models_to_search.pop_back(); |
| 99 for (int i = 0; i < model->GetItemCount(); i++) { |
| 100 if (model->GetCommandIdAt(i) == command_id) { |
| 101 *found_model = model; |
| 102 *found_index = i; |
| 103 return true; |
| 104 } else if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU) { |
| 105 models_to_search.push_back(model->GetSubmenuModelAt(i)); |
| 106 } |
| 107 } |
| 108 } |
| 109 |
| 110 return false; |
| 111 } |
47 }; | 112 }; |
48 | 113 |
49 class ExtensionContextMenuBrowserTest : public ExtensionBrowserTest { | 114 class ExtensionContextMenuBrowserTest : public ExtensionBrowserTest { |
50 public: | 115 public: |
51 // Helper to load an extension from context_menus/|subdirectory| in the | 116 // Helper to load an extension from context_menus/|subdirectory| in the |
52 // extensions test data dir. | 117 // extensions test data dir. |
53 void LoadContextMenuExtension(std::string subdirectory) { | 118 bool LoadContextMenuExtension(std::string subdirectory) { |
54 FilePath extension_dir = | 119 FilePath extension_dir = |
55 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory); | 120 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory); |
56 ASSERT_TRUE(LoadExtension(extension_dir)); | 121 return LoadExtension(extension_dir); |
57 } | 122 } |
58 | 123 |
59 // This creates a test menu using |params|, looks for an extension item with | 124 // This creates and returns a test menu for a page with |url|. |
60 // the given |title|, and returns true if the item was found. | 125 TestRenderViewContextMenu* CreateMenuForURL(const GURL& url) { |
61 bool MenuHasItemWithTitle(const ContextMenuParams& params, | |
62 std::string title) { | |
63 TabContents* tab_contents = browser()->GetSelectedTabContents(); | 126 TabContents* tab_contents = browser()->GetSelectedTabContents(); |
64 TestRenderViewContextMenu menu(tab_contents, params); | 127 WebContextMenuData data; |
65 menu.Init(); | 128 ContextMenuParams params(data); |
66 return menu.HasExtensionItemWithTitle(title); | 129 params.page_url = url; |
| 130 TestRenderViewContextMenu* menu = |
| 131 new TestRenderViewContextMenu(tab_contents, params); |
| 132 menu->Init(); |
| 133 return menu; |
| 134 } |
| 135 |
| 136 // Shortcut to return the current ExtensionMenuManager. |
| 137 ExtensionMenuManager* menu_manager() { |
| 138 return browser()->profile()->GetExtensionsService()->menu_manager(); |
| 139 } |
| 140 |
| 141 // This gets all the items that any extension has registered for possible |
| 142 // inclusion in context menus. |
| 143 ExtensionMenuItem::List GetItems() { |
| 144 ExtensionMenuItem::List result; |
| 145 std::set<std::string> extension_ids = menu_manager()->ExtensionIds(); |
| 146 std::set<std::string>::iterator i; |
| 147 for (i = extension_ids.begin(); i != extension_ids.end(); ++i) { |
| 148 const ExtensionMenuItem::List* list = menu_manager()->MenuItems(*i); |
| 149 result.insert(result.end(), list->begin(), list->end()); |
| 150 } |
| 151 return result; |
| 152 } |
| 153 |
| 154 // This creates a test menu for a page with |url|, looks for an extension item |
| 155 // with the given |label|, and returns true if the item was found. |
| 156 bool MenuHasItemWithLabel(const GURL& url, const std::string& label) { |
| 157 scoped_ptr<TestRenderViewContextMenu> menu(CreateMenuForURL(url)); |
| 158 return menu->HasExtensionItemWithLabel(label); |
67 } | 159 } |
68 }; | 160 }; |
69 | 161 |
70 // Returns a new ContextMenuParams initialized with reasonable default values. | 162 // Tests adding a simple context menu item. |
71 ContextMenuParams* CreateParams() { | |
72 WebContextMenuData data; | |
73 ContextMenuParams* params = new ContextMenuParams(data); | |
74 return params; | |
75 } | |
76 | |
77 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Simple) { | 163 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Simple) { |
78 LoadContextMenuExtension("simple"); | 164 ExtensionTestMessageListener listener1("created item"); |
79 | 165 ExtensionTestMessageListener listener2("onclick fired"); |
80 // The extension's background page will create a context menu item and then | 166 ASSERT_TRUE(LoadContextMenuExtension("simple")); |
81 // cause a navigation on success - we wait for that here. | 167 |
82 ASSERT_TRUE(ui_test_utils::WaitForNavigationsInCurrentTab(browser(), 1)); | 168 // Wait for the extension to tell us it's created an item. |
83 | 169 ASSERT_TRUE(listener1.WaitUntilSatisfied()); |
84 // Initialize the data we need to create a context menu. | 170 |
85 TabContents* tab_contents = browser()->GetSelectedTabContents(); | 171 GURL page_url("http://www.google.com"); |
86 scoped_ptr<ContextMenuParams> params(CreateParams()); | |
87 params->page_url = GURL("http://www.google.com"); | |
88 | 172 |
89 // Create and build our test context menu. | 173 // Create and build our test context menu. |
90 TestRenderViewContextMenu menu(tab_contents, *params); | 174 scoped_ptr<TestRenderViewContextMenu> menu(CreateMenuForURL(page_url)); |
91 menu.Init(); | |
92 | 175 |
93 // Look for the extension item in the menu, and execute it. | 176 // Look for the extension item in the menu, and execute it. |
94 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; | 177 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
95 ASSERT_TRUE(menu.IsCommandIdEnabled(command_id)); | 178 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); |
96 menu.ExecuteCommand(command_id); | 179 menu->ExecuteCommand(command_id); |
97 | 180 |
98 // The onclick handler for the extension item will cause a navigation - we | 181 // Wait for the extension's script to tell us its onclick fired. |
99 // wait for that here. | 182 ASSERT_TRUE(listener2.WaitUntilSatisfied()); |
100 ASSERT_TRUE(ui_test_utils::WaitForNavigationsInCurrentTab(browser(), 1)); | |
101 } | 183 } |
102 | 184 |
| 185 // Tests that setting "documentUrlPatterns" for an item properly restricts |
| 186 // those items to matching pages. |
103 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Patterns) { | 187 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Patterns) { |
104 // The js test code will create two items with patterns and then navigate a | 188 ExtensionTestMessageListener listener("created items"); |
105 // tab to tell us to proceed. | 189 |
106 LoadContextMenuExtension("patterns"); | 190 ASSERT_TRUE(LoadContextMenuExtension("patterns")); |
107 ASSERT_TRUE(ui_test_utils::WaitForNavigationsInCurrentTab(browser(), 1)); | 191 |
108 | 192 // Wait for the js test code to create its two items with patterns. |
109 scoped_ptr<ContextMenuParams> params(CreateParams()); | 193 ASSERT_TRUE(listener.WaitUntilSatisfied()); |
110 | 194 |
111 // Check that a document url that should match the items' patterns appears. | 195 // Check that a document url that should match the items' patterns appears. |
112 params->frame_url = GURL("http://www.google.com"); | 196 GURL google_url("http://www.google.com"); |
113 ASSERT_TRUE(MenuHasItemWithTitle(*params, std::string("test_item1"))); | 197 ASSERT_TRUE(MenuHasItemWithLabel(google_url, std::string("test_item1"))); |
114 ASSERT_TRUE(MenuHasItemWithTitle(*params, std::string("test_item2"))); | 198 ASSERT_TRUE(MenuHasItemWithLabel(google_url, std::string("test_item2"))); |
115 | 199 |
116 // Now check for a non-matching url. | 200 // Now check with a non-matching url. |
117 params->frame_url = GURL("http://www.test.com"); | 201 GURL test_url("http://www.test.com"); |
118 ASSERT_FALSE(MenuHasItemWithTitle(*params, std::string("test_item1"))); | 202 ASSERT_FALSE(MenuHasItemWithLabel(test_url, std::string("test_item1"))); |
119 ASSERT_FALSE(MenuHasItemWithTitle(*params, std::string("test_item2"))); | 203 ASSERT_FALSE(MenuHasItemWithLabel(test_url, std::string("test_item2"))); |
120 } | 204 } |
| 205 |
| 206 // Tests registering an item with a very long title that should get truncated in |
| 207 // the actual menu displayed. |
| 208 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, LongTitle) { |
| 209 ExtensionTestMessageListener listener("created"); |
| 210 |
| 211 // Load the extension and wait until it's created a menu item. |
| 212 ASSERT_TRUE(LoadContextMenuExtension("long_title")); |
| 213 ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 214 |
| 215 // Make sure we have an item registered with a long title. |
| 216 size_t limit = RenderViewContextMenu::kMaxExtensionItemTitleLength; |
| 217 ExtensionMenuItem::List items = GetItems(); |
| 218 ASSERT_EQ(1u, items.size()); |
| 219 ExtensionMenuItem* item = items.at(0); |
| 220 ASSERT_GT(item->title().size(), limit); |
| 221 |
| 222 // Create a context menu, then find the item's label. It should be properly |
| 223 // truncated. |
| 224 GURL url("http://foo.com/"); |
| 225 scoped_ptr<TestRenderViewContextMenu> menu(CreateMenuForURL(url)); |
| 226 |
| 227 string16 label; |
| 228 ASSERT_TRUE(menu->GetItemLabel(item->id(), &label)); |
| 229 ASSERT_TRUE(label.size() <= limit); |
| 230 } |
OLD | NEW |