| 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" | |
| 6 #include "chrome/app/chrome_dll_resource.h" | 5 #include "chrome/app/chrome_dll_resource.h" |
| 7 #include "chrome/browser/browser.h" | 6 #include "chrome/browser/browser.h" |
| 8 #include "chrome/browser/extensions/extension_browsertest.h" | 7 #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" | |
| 11 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 8 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 12 #include "chrome/browser/tab_contents/tab_contents.h" | 9 #include "chrome/browser/tab_contents/tab_contents.h" |
| 13 #include "chrome/common/chrome_switches.h" | 10 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/test/ui_test_utils.h" | 11 #include "chrome/test/ui_test_utils.h" |
| 15 #include "net/base/mock_host_resolver.h" | |
| 16 #include "third_party/WebKit/WebKit/chromium/public/WebContextMenuData.h" | 12 #include "third_party/WebKit/WebKit/chromium/public/WebContextMenuData.h" |
| 17 #include "webkit/glue/context_menu.h" | 13 #include "webkit/glue/context_menu.h" |
| 18 | 14 |
| 19 using menus::MenuModel; | |
| 20 using WebKit::WebContextMenuData; | 15 using WebKit::WebContextMenuData; |
| 21 | 16 |
| 22 // This test class helps us sidestep platform-specific issues with popping up a | 17 // This test class helps us sidestep platform-specific issues with popping up a |
| 23 // real context menu, while still running through the actual code in | 18 // real context menu, while still running through the actual code in |
| 24 // RenderViewContextMenu where extension items get added and executed. | 19 // RenderViewContextMenu where extension items get added and executed. |
| 25 class TestRenderViewContextMenu : public RenderViewContextMenu { | 20 class TestRenderViewContextMenu : public RenderViewContextMenu { |
| 26 public: | 21 public: |
| 27 TestRenderViewContextMenu(TabContents* tab_contents, | 22 TestRenderViewContextMenu(TabContents* tab_contents, |
| 28 const ContextMenuParams& params) | 23 const ContextMenuParams& params) |
| 29 : RenderViewContextMenu(tab_contents, params) {} | 24 : RenderViewContextMenu(tab_contents, params) {} |
| 30 | 25 |
| 31 virtual ~TestRenderViewContextMenu() {} | 26 virtual ~TestRenderViewContextMenu() {} |
| 32 | 27 |
| 33 bool HasExtensionItemWithLabel(const std::string& label) { | 28 bool HasExtensionItemWithTitle(std::string title) { |
| 34 string16 label16 = UTF8ToUTF16(label); | |
| 35 std::map<int, ExtensionMenuItem::Id>::iterator i; | 29 std::map<int, ExtensionMenuItem::Id>::iterator i; |
| 36 for (i = extension_item_map_.begin(); i != extension_item_map_.end(); ++i) { | 30 for (i = extension_item_map_.begin(); i != extension_item_map_.end(); ++i) { |
| 37 const ExtensionMenuItem::Id& id = i->second; | 31 int id = i->first; |
| 38 string16 tmp_label; | 32 ExtensionMenuItem* item = GetExtensionMenuItem(id); |
| 39 EXPECT_TRUE(GetItemLabel(id, &tmp_label)); | 33 if (item && item->title() == title) { |
| 40 if (tmp_label == label16) | |
| 41 return true; | 34 return true; |
| 35 } |
| 42 } | 36 } |
| 43 return false; | 37 return false; |
| 44 } | 38 } |
| 45 | 39 |
| 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; | |
| 61 } | |
| 62 | |
| 63 protected: | 40 protected: |
| 64 // These two functions implement pure virtual methods of | |
| 65 // RenderViewContextMenu. | |
| 66 virtual bool GetAcceleratorForCommandId(int command_id, | 41 virtual bool GetAcceleratorForCommandId(int command_id, |
| 67 menus::Accelerator* accelerator) { | 42 menus::Accelerator* accelerator) { |
| 68 // None of our commands have accelerators, so always return false. | 43 // None of our commands have accelerators, so always return false. |
| 69 return false; | 44 return false; |
| 70 } | 45 } |
| 71 virtual void PlatformInit() {} | 46 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 } | |
| 112 }; | 47 }; |
| 113 | 48 |
| 114 class ExtensionContextMenuBrowserTest : public ExtensionBrowserTest { | 49 class ExtensionContextMenuBrowserTest : public ExtensionBrowserTest { |
| 115 public: | 50 public: |
| 116 // Helper to load an extension from context_menus/|subdirectory| in the | 51 // Helper to load an extension from context_menus/|subdirectory| in the |
| 117 // extensions test data dir. | 52 // extensions test data dir. |
| 118 bool LoadContextMenuExtension(std::string subdirectory) { | 53 void LoadContextMenuExtension(std::string subdirectory) { |
| 119 FilePath extension_dir = | 54 FilePath extension_dir = |
| 120 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory); | 55 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory); |
| 121 return LoadExtension(extension_dir); | 56 ASSERT_TRUE(LoadExtension(extension_dir)); |
| 122 } | 57 } |
| 123 | 58 |
| 124 // This creates and returns a test menu for a page with |url|. | 59 // This creates a test menu using |params|, looks for an extension item with |
| 125 TestRenderViewContextMenu* CreateMenuForURL(const GURL& url) { | 60 // the given |title|, and returns true if the item was found. |
| 61 bool MenuHasItemWithTitle(const ContextMenuParams& params, |
| 62 std::string title) { |
| 126 TabContents* tab_contents = browser()->GetSelectedTabContents(); | 63 TabContents* tab_contents = browser()->GetSelectedTabContents(); |
| 127 WebContextMenuData data; | 64 TestRenderViewContextMenu menu(tab_contents, params); |
| 128 ContextMenuParams params(data); | 65 menu.Init(); |
| 129 params.page_url = url; | 66 return menu.HasExtensionItemWithTitle(title); |
| 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); | |
| 159 } | 67 } |
| 160 }; | 68 }; |
| 161 | 69 |
| 162 // Tests adding a simple context menu item. | 70 // Returns a new ContextMenuParams initialized with reasonable default values. |
| 71 ContextMenuParams* CreateParams() { |
| 72 WebContextMenuData data; |
| 73 ContextMenuParams* params = new ContextMenuParams(data); |
| 74 return params; |
| 75 } |
| 76 |
| 163 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Simple) { | 77 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Simple) { |
| 164 ExtensionTestMessageListener listener1("created item"); | 78 LoadContextMenuExtension("simple"); |
| 165 ExtensionTestMessageListener listener2("onclick fired"); | |
| 166 ASSERT_TRUE(LoadContextMenuExtension("simple")); | |
| 167 | 79 |
| 168 // Wait for the extension to tell us it's created an item. | 80 // The extension's background page will create a context menu item and then |
| 169 ASSERT_TRUE(listener1.WaitUntilSatisfied()); | 81 // cause a navigation on success - we wait for that here. |
| 82 ASSERT_TRUE(ui_test_utils::WaitForNavigationsInCurrentTab(browser(), 1)); |
| 170 | 83 |
| 171 GURL page_url("http://www.google.com"); | 84 // Initialize the data we need to create a context menu. |
| 85 TabContents* tab_contents = browser()->GetSelectedTabContents(); |
| 86 scoped_ptr<ContextMenuParams> params(CreateParams()); |
| 87 params->page_url = GURL("http://www.google.com"); |
| 172 | 88 |
| 173 // Create and build our test context menu. | 89 // Create and build our test context menu. |
| 174 scoped_ptr<TestRenderViewContextMenu> menu(CreateMenuForURL(page_url)); | 90 TestRenderViewContextMenu menu(tab_contents, *params); |
| 91 menu.Init(); |
| 175 | 92 |
| 176 // Look for the extension item in the menu, and execute it. | 93 // Look for the extension item in the menu, and execute it. |
| 177 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; | 94 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
| 178 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); | 95 ASSERT_TRUE(menu.IsCommandIdEnabled(command_id)); |
| 179 menu->ExecuteCommand(command_id); | 96 menu.ExecuteCommand(command_id); |
| 180 | 97 |
| 181 // Wait for the extension's script to tell us its onclick fired. | 98 // The onclick handler for the extension item will cause a navigation - we |
| 182 ASSERT_TRUE(listener2.WaitUntilSatisfied()); | 99 // wait for that here. |
| 100 ASSERT_TRUE(ui_test_utils::WaitForNavigationsInCurrentTab(browser(), 1)); |
| 183 } | 101 } |
| 184 | 102 |
| 185 // Tests that setting "documentUrlPatterns" for an item properly restricts | |
| 186 // those items to matching pages. | |
| 187 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Patterns) { | 103 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Patterns) { |
| 188 ExtensionTestMessageListener listener("created items"); | 104 // The js test code will create two items with patterns and then navigate a |
| 105 // tab to tell us to proceed. |
| 106 LoadContextMenuExtension("patterns"); |
| 107 ASSERT_TRUE(ui_test_utils::WaitForNavigationsInCurrentTab(browser(), 1)); |
| 189 | 108 |
| 190 ASSERT_TRUE(LoadContextMenuExtension("patterns")); | 109 scoped_ptr<ContextMenuParams> params(CreateParams()); |
| 191 | |
| 192 // Wait for the js test code to create its two items with patterns. | |
| 193 ASSERT_TRUE(listener.WaitUntilSatisfied()); | |
| 194 | 110 |
| 195 // Check that a document url that should match the items' patterns appears. | 111 // Check that a document url that should match the items' patterns appears. |
| 196 GURL google_url("http://www.google.com"); | 112 params->frame_url = GURL("http://www.google.com"); |
| 197 ASSERT_TRUE(MenuHasItemWithLabel(google_url, std::string("test_item1"))); | 113 ASSERT_TRUE(MenuHasItemWithTitle(*params, std::string("test_item1"))); |
| 198 ASSERT_TRUE(MenuHasItemWithLabel(google_url, std::string("test_item2"))); | 114 ASSERT_TRUE(MenuHasItemWithTitle(*params, std::string("test_item2"))); |
| 199 | 115 |
| 200 // Now check with a non-matching url. | 116 // Now check for a non-matching url. |
| 201 GURL test_url("http://www.test.com"); | 117 params->frame_url = GURL("http://www.test.com"); |
| 202 ASSERT_FALSE(MenuHasItemWithLabel(test_url, std::string("test_item1"))); | 118 ASSERT_FALSE(MenuHasItemWithTitle(*params, std::string("test_item1"))); |
| 203 ASSERT_FALSE(MenuHasItemWithLabel(test_url, std::string("test_item2"))); | 119 ASSERT_FALSE(MenuHasItemWithTitle(*params, std::string("test_item2"))); |
| 204 } | 120 } |
| 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 |