| 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 "base/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
| 6 #include "chrome/app/chrome_command_ids.h" | 6 #include "chrome/app/chrome_command_ids.h" |
| 7 #include "chrome/browser/extensions/extension_browsertest.h" | 7 #include "chrome/browser/extensions/extension_browsertest.h" |
| 8 #include "chrome/browser/extensions/extension_context_menu_model.h" | 8 #include "chrome/browser/extensions/extension_context_menu_model.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_system.h" | 10 #include "chrome/browser/extensions/extension_system.h" |
| 11 #include "chrome/browser/extensions/extension_test_message_listener.h" | 11 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 12 #include "chrome/browser/extensions/lazy_background_page_test_util.h" | 12 #include "chrome/browser/extensions/lazy_background_page_test_util.h" |
| 13 #include "chrome/browser/extensions/test_management_policy.h" | 13 #include "chrome/browser/extensions/test_management_policy.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 15 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 16 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_finder.h" | 17 #include "chrome/browser/ui/browser_finder.h" |
| 18 #include "chrome/browser/ui/browser_tabstrip.h" | 18 #include "chrome/browser/ui/browser_tabstrip.h" |
| 19 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/test/base/ui_test_utils.h" | 20 #include "chrome/test/base/ui_test_utils.h" |
| 21 #include "content/public/common/context_menu_params.h" | 21 #include "content/public/common/context_menu_params.h" |
| 22 #include "net/base/mock_host_resolver.h" | 22 #include "net/base/mock_host_resolver.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" |
| 24 #include "ui/base/models/menu_model.h" | 24 #include "ui/base/models/menu_model.h" |
| 25 | 25 |
| 26 using WebKit::WebContextMenuData; | 26 using WebKit::WebContextMenuData; |
| 27 using content::WebContents; | 27 using content::WebContents; |
| 28 using extensions::MenuItem; |
| 28 using ui::MenuModel; | 29 using ui::MenuModel; |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 // This test class helps us sidestep platform-specific issues with popping up a | 32 // This test class helps us sidestep platform-specific issues with popping up a |
| 32 // real context menu, while still running through the actual code in | 33 // real context menu, while still running through the actual code in |
| 33 // RenderViewContextMenu where extension items get added and executed. | 34 // RenderViewContextMenu where extension items get added and executed. |
| 34 class TestRenderViewContextMenu : public RenderViewContextMenu { | 35 class TestRenderViewContextMenu : public RenderViewContextMenu { |
| 35 public: | 36 public: |
| 36 TestRenderViewContextMenu(WebContents* web_contents, | 37 TestRenderViewContextMenu(WebContents* web_contents, |
| 37 const content::ContextMenuParams& params) | 38 const content::ContextMenuParams& params) |
| 38 : RenderViewContextMenu(web_contents, params) {} | 39 : RenderViewContextMenu(web_contents, params) {} |
| 39 | 40 |
| 40 virtual ~TestRenderViewContextMenu() {} | 41 virtual ~TestRenderViewContextMenu() {} |
| 41 | 42 |
| 42 bool HasExtensionItemWithLabel(const std::string& label) { | 43 bool HasExtensionItemWithLabel(const std::string& label) { |
| 43 string16 label16 = UTF8ToUTF16(label); | 44 string16 label16 = UTF8ToUTF16(label); |
| 44 std::map<int, ExtensionMenuItem::Id>::iterator i; | 45 std::map<int, MenuItem::Id>::iterator i; |
| 45 for (i = extension_item_map_.begin(); i != extension_item_map_.end(); ++i) { | 46 for (i = extension_item_map_.begin(); i != extension_item_map_.end(); ++i) { |
| 46 const ExtensionMenuItem::Id& id = i->second; | 47 const MenuItem::Id& id = i->second; |
| 47 string16 tmp_label; | 48 string16 tmp_label; |
| 48 EXPECT_TRUE(GetItemLabel(id, &tmp_label)); | 49 EXPECT_TRUE(GetItemLabel(id, &tmp_label)); |
| 49 if (tmp_label == label16) | 50 if (tmp_label == label16) |
| 50 return true; | 51 return true; |
| 51 } | 52 } |
| 52 return false; | 53 return false; |
| 53 } | 54 } |
| 54 | 55 |
| 55 // Looks in the menu for an extension item with |id|, and if it is found and | 56 // Looks in the menu for an extension item with |id|, and if it is found and |
| 56 // has a label, that is put in |result| and we return true. Otherwise returns | 57 // has a label, that is put in |result| and we return true. Otherwise returns |
| 57 // false. | 58 // false. |
| 58 bool GetItemLabel(const ExtensionMenuItem::Id& id, string16* result) { | 59 bool GetItemLabel(const MenuItem::Id& id, string16* result) { |
| 59 int command_id = 0; | 60 int command_id = 0; |
| 60 if (!FindCommandId(id, &command_id)) | 61 if (!FindCommandId(id, &command_id)) |
| 61 return false; | 62 return false; |
| 62 | 63 |
| 63 MenuModel* model = NULL; | 64 MenuModel* model = NULL; |
| 64 int index = -1; | 65 int index = -1; |
| 65 if (!GetMenuModelAndItemIndex(command_id, &model, &index)) { | 66 if (!GetMenuModelAndItemIndex(command_id, &model, &index)) { |
| 66 return false; | 67 return false; |
| 67 } | 68 } |
| 68 *result = model->GetLabelAt(index); | 69 *result = model->GetLabelAt(index); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 90 models_to_search.push_back(model->GetSubmenuModelAt(i)); | 91 models_to_search.push_back(model->GetSubmenuModelAt(i)); |
| 91 } | 92 } |
| 92 } | 93 } |
| 93 } | 94 } |
| 94 | 95 |
| 95 return false; | 96 return false; |
| 96 } | 97 } |
| 97 | 98 |
| 98 // Given an extension menu item id, tries to find the corresponding command id | 99 // Given an extension menu item id, tries to find the corresponding command id |
| 99 // in the menu. | 100 // in the menu. |
| 100 bool FindCommandId(const ExtensionMenuItem::Id& id, int* command_id) { | 101 bool FindCommandId(const MenuItem::Id& id, int* command_id) { |
| 101 std::map<int, ExtensionMenuItem::Id>::const_iterator i; | 102 std::map<int, MenuItem::Id>::const_iterator i; |
| 102 for (i = extension_item_map_.begin(); i != extension_item_map_.end(); ++i) { | 103 for (i = extension_item_map_.begin(); i != extension_item_map_.end(); ++i) { |
| 103 if (i->second == id) { | 104 if (i->second == id) { |
| 104 *command_id = i->first; | 105 *command_id = i->first; |
| 105 return true; | 106 return true; |
| 106 } | 107 } |
| 107 } | 108 } |
| 108 return false; | 109 return false; |
| 109 } | 110 } |
| 110 | 111 |
| 111 protected: | 112 protected: |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 content::ContextMenuParams params(data); | 150 content::ContextMenuParams params(data); |
| 150 params.page_url = page_url; | 151 params.page_url = page_url; |
| 151 params.link_url = link_url; | 152 params.link_url = link_url; |
| 152 params.frame_url = frame_url; | 153 params.frame_url = frame_url; |
| 153 TestRenderViewContextMenu* menu = | 154 TestRenderViewContextMenu* menu = |
| 154 new TestRenderViewContextMenu(web_contents, params); | 155 new TestRenderViewContextMenu(web_contents, params); |
| 155 menu->Init(); | 156 menu->Init(); |
| 156 return menu; | 157 return menu; |
| 157 } | 158 } |
| 158 | 159 |
| 159 // Shortcut to return the current ExtensionMenuManager. | 160 // Shortcut to return the current MenuManager. |
| 160 ExtensionMenuManager* menu_manager() { | 161 extensions::MenuManager* menu_manager() { |
| 161 return browser()->profile()->GetExtensionService()->menu_manager(); | 162 return browser()->profile()->GetExtensionService()->menu_manager(); |
| 162 } | 163 } |
| 163 | 164 |
| 164 // Returns a pointer to the currently loaded extension with |name|, or null | 165 // Returns a pointer to the currently loaded extension with |name|, or null |
| 165 // if not found. | 166 // if not found. |
| 166 const extensions::Extension* GetExtensionNamed(std::string name) { | 167 const extensions::Extension* GetExtensionNamed(std::string name) { |
| 167 const ExtensionSet* extensions = | 168 const ExtensionSet* extensions = |
| 168 browser()->profile()->GetExtensionService()->extensions(); | 169 browser()->profile()->GetExtensionService()->extensions(); |
| 169 ExtensionSet::const_iterator i; | 170 ExtensionSet::const_iterator i; |
| 170 for (i = extensions->begin(); i != extensions->end(); ++i) { | 171 for (i = extensions->begin(); i != extensions->end(); ++i) { |
| 171 if ((*i)->name() == name) { | 172 if ((*i)->name() == name) { |
| 172 return *i; | 173 return *i; |
| 173 } | 174 } |
| 174 } | 175 } |
| 175 return NULL; | 176 return NULL; |
| 176 } | 177 } |
| 177 | 178 |
| 178 // This gets all the items that any extension has registered for possible | 179 // This gets all the items that any extension has registered for possible |
| 179 // inclusion in context menus. | 180 // inclusion in context menus. |
| 180 ExtensionMenuItem::List GetItems() { | 181 MenuItem::List GetItems() { |
| 181 ExtensionMenuItem::List result; | 182 MenuItem::List result; |
| 182 std::set<std::string> extension_ids = menu_manager()->ExtensionIds(); | 183 std::set<std::string> extension_ids = menu_manager()->ExtensionIds(); |
| 183 std::set<std::string>::iterator i; | 184 std::set<std::string>::iterator i; |
| 184 for (i = extension_ids.begin(); i != extension_ids.end(); ++i) { | 185 for (i = extension_ids.begin(); i != extension_ids.end(); ++i) { |
| 185 const ExtensionMenuItem::List* list = menu_manager()->MenuItems(*i); | 186 const MenuItem::List* list = menu_manager()->MenuItems(*i); |
| 186 result.insert(result.end(), list->begin(), list->end()); | 187 result.insert(result.end(), list->begin(), list->end()); |
| 187 } | 188 } |
| 188 return result; | 189 return result; |
| 189 } | 190 } |
| 190 | 191 |
| 191 // This creates a test menu for a page with |page_url| and |link_url|, looks | 192 // This creates a test menu for a page with |page_url| and |link_url|, looks |
| 192 // for an extension item with the given |label|, and returns true if the item | 193 // for an extension item with the given |label|, and returns true if the item |
| 193 // was found. | 194 // was found. |
| 194 bool MenuHasItemWithLabel(const GURL& page_url, | 195 bool MenuHasItemWithLabel(const GURL& page_url, |
| 195 const GURL& link_url, | 196 const GURL& link_url, |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 // the actual menu displayed. | 297 // the actual menu displayed. |
| 297 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, LongTitle) { | 298 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, LongTitle) { |
| 298 ExtensionTestMessageListener listener("created", false); | 299 ExtensionTestMessageListener listener("created", false); |
| 299 | 300 |
| 300 // Load the extension and wait until it's created a menu item. | 301 // Load the extension and wait until it's created a menu item. |
| 301 ASSERT_TRUE(LoadContextMenuExtension("long_title")); | 302 ASSERT_TRUE(LoadContextMenuExtension("long_title")); |
| 302 ASSERT_TRUE(listener.WaitUntilSatisfied()); | 303 ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 303 | 304 |
| 304 // Make sure we have an item registered with a long title. | 305 // Make sure we have an item registered with a long title. |
| 305 size_t limit = RenderViewContextMenu::kMaxExtensionItemTitleLength; | 306 size_t limit = RenderViewContextMenu::kMaxExtensionItemTitleLength; |
| 306 ExtensionMenuItem::List items = GetItems(); | 307 MenuItem::List items = GetItems(); |
| 307 ASSERT_EQ(1u, items.size()); | 308 ASSERT_EQ(1u, items.size()); |
| 308 ExtensionMenuItem* item = items.at(0); | 309 MenuItem* item = items.at(0); |
| 309 ASSERT_GT(item->title().size(), limit); | 310 ASSERT_GT(item->title().size(), limit); |
| 310 | 311 |
| 311 // Create a context menu, then find the item's label. It should be properly | 312 // Create a context menu, then find the item's label. It should be properly |
| 312 // truncated. | 313 // truncated. |
| 313 GURL url("http://foo.com/"); | 314 GURL url("http://foo.com/"); |
| 314 scoped_ptr<TestRenderViewContextMenu> menu( | 315 scoped_ptr<TestRenderViewContextMenu> menu( |
| 315 CreateMenu(browser(), url, GURL(), GURL())); | 316 CreateMenu(browser(), url, GURL(), GURL())); |
| 316 | 317 |
| 317 string16 label; | 318 string16 label; |
| 318 ASSERT_TRUE(menu->GetItemLabel(item->id(), &label)); | 319 ASSERT_TRUE(menu->GetItemLabel(item->id(), &label)); |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 // Test that menu items appear while the page is unloaded. | 573 // Test that menu items appear while the page is unloaded. |
| 573 ASSERT_TRUE(MenuHasItemWithLabel( | 574 ASSERT_TRUE(MenuHasItemWithLabel( |
| 574 about_blank, GURL(), GURL(), std::string("Item 1"))); | 575 about_blank, GURL(), GURL(), std::string("Item 1"))); |
| 575 ASSERT_TRUE(MenuHasItemWithLabel( | 576 ASSERT_TRUE(MenuHasItemWithLabel( |
| 576 about_blank, GURL(), GURL(), std::string("Checkbox 1"))); | 577 about_blank, GURL(), GURL(), std::string("Checkbox 1"))); |
| 577 | 578 |
| 578 // Test that checked menu items retain their checkedness. | 579 // Test that checked menu items retain their checkedness. |
| 579 LazyBackgroundObserver checkbox_checked; | 580 LazyBackgroundObserver checkbox_checked; |
| 580 scoped_ptr<TestRenderViewContextMenu> menu( | 581 scoped_ptr<TestRenderViewContextMenu> menu( |
| 581 CreateMenu(browser(), about_blank, GURL(), GURL())); | 582 CreateMenu(browser(), about_blank, GURL(), GURL())); |
| 582 ExtensionMenuItem::Id id(false, extension->id()); | 583 MenuItem::Id id(false, extension->id()); |
| 583 id.string_uid = "checkbox1"; | 584 id.string_uid = "checkbox1"; |
| 584 int command_id = -1; | 585 int command_id = -1; |
| 585 ASSERT_TRUE(menu->FindCommandId(id, &command_id)); | 586 ASSERT_TRUE(menu->FindCommandId(id, &command_id)); |
| 586 EXPECT_FALSE(menu->IsCommandIdChecked(command_id)); | 587 EXPECT_FALSE(menu->IsCommandIdChecked(command_id)); |
| 587 | 588 |
| 588 // Executing the checkbox also fires the onClicked event. | 589 // Executing the checkbox also fires the onClicked event. |
| 589 ExtensionTestMessageListener listener("onClicked fired for checkbox1", false); | 590 ExtensionTestMessageListener listener("onClicked fired for checkbox1", false); |
| 590 menu->ExecuteCommand(command_id); | 591 menu->ExecuteCommand(command_id); |
| 591 checkbox_checked.WaitUntilClosed(); | 592 checkbox_checked.WaitUntilClosed(); |
| 592 | 593 |
| 593 EXPECT_TRUE(menu->IsCommandIdChecked(command_id)); | 594 EXPECT_TRUE(menu->IsCommandIdChecked(command_id)); |
| 594 ASSERT_TRUE(listener.WaitUntilSatisfied()); | 595 ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 595 } | 596 } |
| OLD | NEW |