| 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/strings/utf_string_conversions.h" | 5 #include "base/strings/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_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_test_message_listener.h" | 9 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 10 #include "chrome/browser/extensions/lazy_background_page_test_util.h" | 10 #include "chrome/browser/extensions/lazy_background_page_test_util.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 12 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
| 13 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti
l.h" |
| 13 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
| 16 #include "content/public/common/context_menu_params.h" | 17 #include "content/public/common/context_menu_params.h" |
| 17 #include "extensions/browser/extension_system.h" | 18 #include "extensions/browser/extension_system.h" |
| 18 #include "extensions/browser/test_management_policy.h" | 19 #include "extensions/browser/test_management_policy.h" |
| 19 #include "extensions/common/extension_set.h" | 20 #include "extensions/common/extension_set.h" |
| 20 #include "extensions/common/switches.h" | 21 #include "extensions/common/switches.h" |
| 21 #include "net/dns/mock_host_resolver.h" | 22 #include "net/dns/mock_host_resolver.h" |
| 22 #include "ui/base/models/menu_model.h" | 23 #include "ui/base/models/menu_model.h" |
| 23 | 24 |
| 24 using content::WebContents; | 25 using content::WebContents; |
| 25 using extensions::MenuItem; | 26 using extensions::MenuItem; |
| 26 using ui::MenuModel; | 27 using ui::MenuModel; |
| 27 | 28 |
| 28 namespace { | |
| 29 // This test class helps us sidestep platform-specific issues with popping up a | |
| 30 // real context menu, while still running through the actual code in | |
| 31 // RenderViewContextMenu where extension items get added and executed. | |
| 32 class TestRenderViewContextMenu : public RenderViewContextMenu { | |
| 33 public: | |
| 34 TestRenderViewContextMenu(content::RenderFrameHost* render_frame_host, | |
| 35 const content::ContextMenuParams& params) | |
| 36 : RenderViewContextMenu(render_frame_host, params) {} | |
| 37 | |
| 38 virtual ~TestRenderViewContextMenu() {} | |
| 39 | |
| 40 // Searches for an menu item with |command_id|. If it's found, the return | |
| 41 // value is true and the model and index where it appears in that model are | |
| 42 // returned in |found_model| and |found_index|. Otherwise returns false. | |
| 43 bool GetMenuModelAndItemIndex(int command_id, | |
| 44 MenuModel** found_model, | |
| 45 int *found_index) { | |
| 46 std::vector<MenuModel*> models_to_search; | |
| 47 models_to_search.push_back(&menu_model_); | |
| 48 | |
| 49 while (!models_to_search.empty()) { | |
| 50 MenuModel* model = models_to_search.back(); | |
| 51 models_to_search.pop_back(); | |
| 52 for (int i = 0; i < model->GetItemCount(); i++) { | |
| 53 if (model->GetCommandIdAt(i) == command_id) { | |
| 54 *found_model = model; | |
| 55 *found_index = i; | |
| 56 return true; | |
| 57 } else if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU) { | |
| 58 models_to_search.push_back(model->GetSubmenuModelAt(i)); | |
| 59 } | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 return false; | |
| 64 } | |
| 65 | |
| 66 extensions::ContextMenuMatcher& extension_items() { | |
| 67 return extension_items_; | |
| 68 } | |
| 69 | |
| 70 protected: | |
| 71 // These two functions implement pure virtual methods of | |
| 72 // RenderViewContextMenu. | |
| 73 virtual bool GetAcceleratorForCommandId( | |
| 74 int command_id, | |
| 75 ui::Accelerator* accelerator) OVERRIDE { | |
| 76 // None of our commands have accelerators, so always return false. | |
| 77 return false; | |
| 78 } | |
| 79 virtual void PlatformInit() OVERRIDE {} | |
| 80 virtual void PlatformCancel() OVERRIDE {} | |
| 81 }; | |
| 82 | |
| 83 } // namespace | |
| 84 | |
| 85 class ExtensionContextMenuBrowserTest : public ExtensionBrowserTest { | 29 class ExtensionContextMenuBrowserTest : public ExtensionBrowserTest { |
| 86 public: | 30 public: |
| 87 // Helper to load an extension from context_menus/|subdirectory| in the | 31 // Helper to load an extension from context_menus/|subdirectory| in the |
| 88 // extensions test data dir. | 32 // extensions test data dir. |
| 89 const extensions::Extension* LoadContextMenuExtension( | 33 const extensions::Extension* LoadContextMenuExtension( |
| 90 std::string subdirectory) { | 34 std::string subdirectory) { |
| 91 base::FilePath extension_dir = | 35 base::FilePath extension_dir = |
| 92 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory); | 36 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory); |
| 93 return LoadExtension(extension_dir); | 37 return LoadExtension(extension_dir); |
| 94 } | 38 } |
| 95 | 39 |
| 96 // Helper to load an extension from context_menus/top_level/|subdirectory| in | 40 // Helper to load an extension from context_menus/top_level/|subdirectory| in |
| 97 // the extensions test data dir. | 41 // the extensions test data dir. |
| 98 const extensions::Extension* LoadTopLevelContextMenuExtension( | 42 const extensions::Extension* LoadTopLevelContextMenuExtension( |
| 99 std::string subdirectory) { | 43 std::string subdirectory) { |
| 100 base::FilePath extension_dir = | 44 base::FilePath extension_dir = |
| 101 test_data_dir_.AppendASCII("context_menus").AppendASCII("top_level"); | 45 test_data_dir_.AppendASCII("context_menus").AppendASCII("top_level"); |
| 102 extension_dir = extension_dir.AppendASCII(subdirectory); | 46 extension_dir = extension_dir.AppendASCII(subdirectory); |
| 103 return LoadExtension(extension_dir); | 47 return LoadExtension(extension_dir); |
| 104 } | 48 } |
| 105 | 49 |
| 106 const extensions::Extension* LoadContextMenuExtensionIncognito( | 50 const extensions::Extension* LoadContextMenuExtensionIncognito( |
| 107 std::string subdirectory) { | 51 std::string subdirectory) { |
| 108 base::FilePath extension_dir = | 52 base::FilePath extension_dir = |
| 109 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory); | 53 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory); |
| 110 return LoadExtensionIncognito(extension_dir); | 54 return LoadExtensionIncognito(extension_dir); |
| 111 } | 55 } |
| 112 | 56 |
| 113 TestRenderViewContextMenu* CreateMenu(Browser* browser, | 57 // Returns the active WebContents. |
| 114 const GURL& page_url, | 58 WebContents* GetWebContents() { |
| 115 const GURL& link_url, | 59 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 116 const GURL& frame_url) { | |
| 117 WebContents* web_contents = | |
| 118 browser->tab_strip_model()->GetActiveWebContents(); | |
| 119 content::ContextMenuParams params; | |
| 120 params.page_url = page_url; | |
| 121 params.link_url = link_url; | |
| 122 params.frame_url = frame_url; | |
| 123 TestRenderViewContextMenu* menu = | |
| 124 new TestRenderViewContextMenu(web_contents->GetMainFrame(), params); | |
| 125 menu->Init(); | |
| 126 return menu; | |
| 127 } | 60 } |
| 128 | 61 |
| 129 // Shortcut to return the current MenuManager. | 62 // Shortcut to return the current MenuManager. |
| 130 extensions::MenuManager* menu_manager() { | 63 extensions::MenuManager* menu_manager() { |
| 131 return extensions::MenuManager::Get(browser()->profile()); | 64 return extensions::MenuManager::Get(browser()->profile()); |
| 132 } | 65 } |
| 133 | 66 |
| 134 // Returns a pointer to the currently loaded extension with |name|, or null | 67 // Returns a pointer to the currently loaded extension with |name|, or null |
| 135 // if not found. | 68 // if not found. |
| 136 const extensions::Extension* GetExtensionNamed(std::string name) { | 69 const extensions::Extension* GetExtensionNamed(std::string name) { |
| 137 const extensions::ExtensionSet* extensions = | 70 const extensions::ExtensionSet* extensions = |
| 138 browser()->profile()->GetExtensionService()->extensions(); | 71 browser()->profile()->GetExtensionService()->extensions(); |
| 139 for (extensions::ExtensionSet::const_iterator i = extensions->begin(); | 72 for (extensions::ExtensionSet::const_iterator i = extensions->begin(); |
| 140 i != extensions->end(); ++i) { | 73 i != extensions->end(); ++i) { |
| 141 if ((*i)->name() == name) { | 74 if ((*i)->name() == name) { |
| 142 return i->get(); | 75 return i->get(); |
| 143 } | 76 } |
| 144 } | 77 } |
| 145 return NULL; | 78 return NULL; |
| 146 } | 79 } |
| 147 | 80 |
| 148 // This gets all the items that any extension has registered for possible | 81 // This gets all the items that any extension has registered for possible |
| 149 // inclusion in context menus. | 82 // inclusion in context menus. |
| 150 MenuItem::List GetItems() { | 83 MenuItem::List GetItems() { |
| 151 MenuItem::List result; | 84 MenuItem::List result; |
| 152 std::set<std::string> extension_ids = menu_manager()->ExtensionIds(); | 85 std::set<MenuItem::ExtensionKey> extension_ids = |
| 153 std::set<std::string>::iterator i; | 86 menu_manager()->ExtensionIds(); |
| 87 std::set<MenuItem::ExtensionKey>::iterator i; |
| 154 for (i = extension_ids.begin(); i != extension_ids.end(); ++i) { | 88 for (i = extension_ids.begin(); i != extension_ids.end(); ++i) { |
| 155 const MenuItem::List* list = menu_manager()->MenuItems(*i); | 89 const MenuItem::List* list = menu_manager()->MenuItems(*i); |
| 156 result.insert(result.end(), list->begin(), list->end()); | 90 result.insert(result.end(), list->begin(), list->end()); |
| 157 } | 91 } |
| 158 return result; | 92 return result; |
| 159 } | 93 } |
| 160 | 94 |
| 161 // This creates a test menu for a page with |page_url| and |link_url|, looks | 95 // This creates a test menu for a page with |page_url| and |link_url|, looks |
| 162 // for an extension item with the given |label|, and returns true if the item | 96 // for an extension item with the given |label|, and returns true if the item |
| 163 // was found. | 97 // was found. |
| 164 bool MenuHasItemWithLabel(const GURL& page_url, | 98 bool MenuHasItemWithLabel(const GURL& page_url, |
| 165 const GURL& link_url, | 99 const GURL& link_url, |
| 166 const GURL& frame_url, | 100 const GURL& frame_url, |
| 167 const std::string& label) { | 101 const std::string& label) { |
| 168 scoped_ptr<TestRenderViewContextMenu> menu( | 102 scoped_ptr<TestRenderViewContextMenu> menu( |
| 169 CreateMenu(browser(), page_url, link_url, frame_url)); | 103 TestRenderViewContextMenu::Create( |
| 104 GetWebContents(), page_url, link_url, frame_url)); |
| 170 return MenuHasExtensionItemWithLabel(menu.get(), label); | 105 return MenuHasExtensionItemWithLabel(menu.get(), label); |
| 171 } | 106 } |
| 172 | 107 |
| 173 // This creates an extension that starts |enabled| and then switches to | 108 // This creates an extension that starts |enabled| and then switches to |
| 174 // |!enabled|. | 109 // |!enabled|. |
| 175 void TestEnabledContextMenu(bool enabled) { | 110 void TestEnabledContextMenu(bool enabled) { |
| 176 ExtensionTestMessageListener begin("begin", true); | 111 ExtensionTestMessageListener begin("begin", true); |
| 177 ExtensionTestMessageListener create("create", true); | 112 ExtensionTestMessageListener create("create", true); |
| 178 ExtensionTestMessageListener update("update", false); | 113 ExtensionTestMessageListener update("update", false); |
| 179 ASSERT_TRUE(LoadContextMenuExtension("enabled")); | 114 ASSERT_TRUE(LoadContextMenuExtension("enabled")); |
| 180 | 115 |
| 181 ASSERT_TRUE(begin.WaitUntilSatisfied()); | 116 ASSERT_TRUE(begin.WaitUntilSatisfied()); |
| 182 | 117 |
| 183 if (enabled) | 118 if (enabled) |
| 184 begin.Reply("start enabled"); | 119 begin.Reply("start enabled"); |
| 185 else | 120 else |
| 186 begin.Reply("start disabled"); | 121 begin.Reply("start disabled"); |
| 187 | 122 |
| 188 // Wait for the extension to tell us it's created an item. | 123 // Wait for the extension to tell us it's created an item. |
| 189 ASSERT_TRUE(create.WaitUntilSatisfied()); | 124 ASSERT_TRUE(create.WaitUntilSatisfied()); |
| 190 create.Reply("go"); | 125 create.Reply("go"); |
| 191 | 126 |
| 192 GURL page_url("http://www.google.com"); | 127 GURL page_url("http://www.google.com"); |
| 193 | 128 |
| 194 // Create and build our test context menu. | 129 // Create and build our test context menu. |
| 195 scoped_ptr<TestRenderViewContextMenu> menu( | 130 scoped_ptr<TestRenderViewContextMenu> menu( |
| 196 CreateMenu(browser(), page_url, GURL(), GURL())); | 131 TestRenderViewContextMenu::Create( |
| 132 GetWebContents(), page_url, GURL(), GURL())); |
| 197 | 133 |
| 198 // Look for the extension item in the menu, and make sure it's |enabled|. | 134 // Look for the extension item in the menu, and make sure it's |enabled|. |
| 199 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; | 135 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
| 200 ASSERT_EQ(enabled, menu->IsCommandIdEnabled(command_id)); | 136 ASSERT_EQ(enabled, menu->IsCommandIdEnabled(command_id)); |
| 201 | 137 |
| 202 // Update the item and make sure it is now |!enabled|. | 138 // Update the item and make sure it is now |!enabled|. |
| 203 ASSERT_TRUE(update.WaitUntilSatisfied()); | 139 ASSERT_TRUE(update.WaitUntilSatisfied()); |
| 204 ASSERT_EQ(!enabled, menu->IsCommandIdEnabled(command_id)); | 140 ASSERT_EQ(!enabled, menu->IsCommandIdEnabled(command_id)); |
| 205 } | 141 } |
| 206 | 142 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 ExtensionTestMessageListener listener1("created item", false); | 196 ExtensionTestMessageListener listener1("created item", false); |
| 261 ExtensionTestMessageListener listener2("onclick fired", false); | 197 ExtensionTestMessageListener listener2("onclick fired", false); |
| 262 ASSERT_TRUE(LoadContextMenuExtension("simple")); | 198 ASSERT_TRUE(LoadContextMenuExtension("simple")); |
| 263 | 199 |
| 264 // Wait for the extension to tell us it's created an item. | 200 // Wait for the extension to tell us it's created an item. |
| 265 ASSERT_TRUE(listener1.WaitUntilSatisfied()); | 201 ASSERT_TRUE(listener1.WaitUntilSatisfied()); |
| 266 | 202 |
| 267 GURL page_url("http://www.google.com"); | 203 GURL page_url("http://www.google.com"); |
| 268 | 204 |
| 269 // Create and build our test context menu. | 205 // Create and build our test context menu. |
| 270 scoped_ptr<TestRenderViewContextMenu> menu( | 206 scoped_ptr<TestRenderViewContextMenu> menu(TestRenderViewContextMenu::Create( |
| 271 CreateMenu(browser(), page_url, GURL(), GURL())); | 207 GetWebContents(), page_url, GURL(), GURL())); |
| 272 | 208 |
| 273 // Look for the extension item in the menu, and execute it. | 209 // Look for the extension item in the menu, and execute it. |
| 274 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; | 210 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
| 275 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); | 211 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); |
| 276 menu->ExecuteCommand(command_id, 0); | 212 menu->ExecuteCommand(command_id, 0); |
| 277 | 213 |
| 278 // Wait for the extension's script to tell us its onclick fired. | 214 // Wait for the extension's script to tell us its onclick fired. |
| 279 ASSERT_TRUE(listener2.WaitUntilSatisfied()); | 215 ASSERT_TRUE(listener2.WaitUntilSatisfied()); |
| 280 } | 216 } |
| 281 | 217 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 size_t limit = extensions::ContextMenuMatcher::kMaxExtensionItemTitleLength; | 261 size_t limit = extensions::ContextMenuMatcher::kMaxExtensionItemTitleLength; |
| 326 MenuItem::List items = GetItems(); | 262 MenuItem::List items = GetItems(); |
| 327 ASSERT_EQ(1u, items.size()); | 263 ASSERT_EQ(1u, items.size()); |
| 328 MenuItem* item = items.at(0); | 264 MenuItem* item = items.at(0); |
| 329 ASSERT_GT(item->title().size(), limit); | 265 ASSERT_GT(item->title().size(), limit); |
| 330 | 266 |
| 331 // Create a context menu, then find the item's label. It should be properly | 267 // Create a context menu, then find the item's label. It should be properly |
| 332 // truncated. | 268 // truncated. |
| 333 GURL url("http://foo.com/"); | 269 GURL url("http://foo.com/"); |
| 334 scoped_ptr<TestRenderViewContextMenu> menu( | 270 scoped_ptr<TestRenderViewContextMenu> menu( |
| 335 CreateMenu(browser(), url, GURL(), GURL())); | 271 TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL())); |
| 336 | 272 |
| 337 base::string16 label; | 273 base::string16 label; |
| 338 ASSERT_TRUE(GetItemLabel(menu.get(), item->id(), &label)); | 274 ASSERT_TRUE(GetItemLabel(menu.get(), item->id(), &label)); |
| 339 ASSERT_TRUE(label.size() <= limit); | 275 ASSERT_TRUE(label.size() <= limit); |
| 340 } | 276 } |
| 341 | 277 |
| 342 // Flaky on Windows debug bots. http://crbug.com/251590 | 278 // Flaky on Windows debug bots. http://crbug.com/251590 |
| 343 #if defined(OS_WIN) | 279 #if defined(OS_WIN) |
| 344 #define MAYBE_TopLevel DISABLED_TopLevel | 280 #define MAYBE_TopLevel DISABLED_TopLevel |
| 345 #else | 281 #else |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 ExtensionTestMessageListener listener4("created items", false); | 313 ExtensionTestMessageListener listener4("created items", false); |
| 378 ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi4")); | 314 ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi4")); |
| 379 ASSERT_TRUE(listener4.WaitUntilSatisfied()); | 315 ASSERT_TRUE(listener4.WaitUntilSatisfied()); |
| 380 | 316 |
| 381 ExtensionTestMessageListener listener5("created items", false); | 317 ExtensionTestMessageListener listener5("created items", false); |
| 382 ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi5")); | 318 ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi5")); |
| 383 ASSERT_TRUE(listener5.WaitUntilSatisfied()); | 319 ASSERT_TRUE(listener5.WaitUntilSatisfied()); |
| 384 | 320 |
| 385 GURL url("http://foo.com/"); | 321 GURL url("http://foo.com/"); |
| 386 scoped_ptr<TestRenderViewContextMenu> menu( | 322 scoped_ptr<TestRenderViewContextMenu> menu( |
| 387 CreateMenu(browser(), url, GURL(), GURL())); | 323 TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL())); |
| 388 | 324 |
| 389 int index = 0; | 325 int index = 0; |
| 390 MenuModel* model = NULL; | 326 MenuModel* model = NULL; |
| 391 | 327 |
| 392 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( | 328 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( |
| 393 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index)); | 329 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index)); |
| 394 EXPECT_EQ(base::UTF8ToUTF16("An Extension with multiple Context Menus"), | 330 EXPECT_EQ(base::UTF8ToUTF16("An Extension with multiple Context Menus"), |
| 395 model->GetLabelAt(index++)); | 331 model->GetLabelAt(index++)); |
| 396 EXPECT_EQ(base::UTF8ToUTF16("Context Menu #1 - Extension #2"), | 332 EXPECT_EQ(base::UTF8ToUTF16("Context Menu #1 - Extension #2"), |
| 397 model->GetLabelAt(index++)); | 333 model->GetLabelAt(index++)); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 // Navigate to test1.html inside the extension, which should create a bunch | 396 // Navigate to test1.html inside the extension, which should create a bunch |
| 461 // of items at the top-level (but they'll get pushed into an auto-generated | 397 // of items at the top-level (but they'll get pushed into an auto-generated |
| 462 // parent). | 398 // parent). |
| 463 ExtensionTestMessageListener listener1("test1 create finished", false); | 399 ExtensionTestMessageListener listener1("test1 create finished", false); |
| 464 ui_test_utils::NavigateToURL(browser(), | 400 ui_test_utils::NavigateToURL(browser(), |
| 465 GURL(extension->GetResourceURL("test1.html"))); | 401 GURL(extension->GetResourceURL("test1.html"))); |
| 466 listener1.WaitUntilSatisfied(); | 402 listener1.WaitUntilSatisfied(); |
| 467 | 403 |
| 468 GURL url("http://www.google.com/"); | 404 GURL url("http://www.google.com/"); |
| 469 scoped_ptr<TestRenderViewContextMenu> menu( | 405 scoped_ptr<TestRenderViewContextMenu> menu( |
| 470 CreateMenu(browser(), url, GURL(), GURL())); | 406 TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL())); |
| 471 | 407 |
| 472 // The top-level item should be an "automagic parent" with the extension's | 408 // The top-level item should be an "automagic parent" with the extension's |
| 473 // name. | 409 // name. |
| 474 MenuModel* model = NULL; | 410 MenuModel* model = NULL; |
| 475 int index = 0; | 411 int index = 0; |
| 476 base::string16 label; | 412 base::string16 label; |
| 477 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( | 413 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( |
| 478 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index)); | 414 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index)); |
| 479 EXPECT_EQ(base::UTF8ToUTF16(extension->name()), model->GetLabelAt(index)); | 415 EXPECT_EQ(base::UTF8ToUTF16(extension->name()), model->GetLabelAt(index)); |
| 480 ASSERT_EQ(MenuModel::TYPE_SUBMENU, model->GetTypeAt(index)); | 416 ASSERT_EQ(MenuModel::TYPE_SUBMENU, model->GetTypeAt(index)); |
| 481 | 417 |
| 482 // Get the submenu and verify the items there. | 418 // Get the submenu and verify the items there. |
| 483 MenuModel* submenu = model->GetSubmenuModelAt(index); | 419 MenuModel* submenu = model->GetSubmenuModelAt(index); |
| 484 ASSERT_TRUE(submenu != NULL); | 420 ASSERT_TRUE(submenu != NULL); |
| 485 VerifyMenuForSeparatorsTest(*submenu); | 421 VerifyMenuForSeparatorsTest(*submenu); |
| 486 | 422 |
| 487 // Now run our second test - navigate to test2.html which creates an explicit | 423 // Now run our second test - navigate to test2.html which creates an explicit |
| 488 // parent node and populates that with the same items as in test1. | 424 // parent node and populates that with the same items as in test1. |
| 489 ExtensionTestMessageListener listener2("test2 create finished", false); | 425 ExtensionTestMessageListener listener2("test2 create finished", false); |
| 490 ui_test_utils::NavigateToURL(browser(), | 426 ui_test_utils::NavigateToURL(browser(), |
| 491 GURL(extension->GetResourceURL("test2.html"))); | 427 GURL(extension->GetResourceURL("test2.html"))); |
| 492 listener2.WaitUntilSatisfied(); | 428 listener2.WaitUntilSatisfied(); |
| 493 menu.reset(CreateMenu(browser(), url, GURL(), GURL())); | 429 menu.reset( |
| 430 TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL())); |
| 494 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( | 431 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( |
| 495 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index)); | 432 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index)); |
| 496 EXPECT_EQ(base::UTF8ToUTF16("parent"), model->GetLabelAt(index)); | 433 EXPECT_EQ(base::UTF8ToUTF16("parent"), model->GetLabelAt(index)); |
| 497 submenu = model->GetSubmenuModelAt(index); | 434 submenu = model->GetSubmenuModelAt(index); |
| 498 ASSERT_TRUE(submenu != NULL); | 435 ASSERT_TRUE(submenu != NULL); |
| 499 VerifyMenuForSeparatorsTest(*submenu); | 436 VerifyMenuForSeparatorsTest(*submenu); |
| 500 } | 437 } |
| 501 | 438 |
| 502 // Tests that targetUrlPattern keeps items from appearing when there is no | 439 // Tests that targetUrlPattern keeps items from appearing when there is no |
| 503 // target url. | 440 // target url. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 | 485 |
| 549 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito")); | 486 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito")); |
| 550 | 487 |
| 551 // Wait for the extension's processes to tell us they've created an item. | 488 // Wait for the extension's processes to tell us they've created an item. |
| 552 ASSERT_TRUE(created.WaitUntilSatisfied()); | 489 ASSERT_TRUE(created.WaitUntilSatisfied()); |
| 553 ASSERT_TRUE(created_incognito.WaitUntilSatisfied()); | 490 ASSERT_TRUE(created_incognito.WaitUntilSatisfied()); |
| 554 | 491 |
| 555 GURL page_url("http://www.google.com"); | 492 GURL page_url("http://www.google.com"); |
| 556 | 493 |
| 557 // Create and build our test context menu. | 494 // Create and build our test context menu. |
| 558 scoped_ptr<TestRenderViewContextMenu> menu( | 495 scoped_ptr<TestRenderViewContextMenu> menu(TestRenderViewContextMenu::Create( |
| 559 CreateMenu(browser(), page_url, GURL(), GURL())); | 496 GetWebContents(), page_url, GURL(), GURL())); |
| 497 WebContents* incognito_web_contents = |
| 498 browser_incognito->tab_strip_model()->GetActiveWebContents(); |
| 560 scoped_ptr<TestRenderViewContextMenu> menu_incognito( | 499 scoped_ptr<TestRenderViewContextMenu> menu_incognito( |
| 561 CreateMenu(browser_incognito, page_url, GURL(), GURL())); | 500 TestRenderViewContextMenu::Create( |
| 501 incognito_web_contents, page_url, GURL(), GURL())); |
| 562 | 502 |
| 563 // Look for the extension item in the menu, and execute it. | 503 // Look for the extension item in the menu, and execute it. |
| 564 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; | 504 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
| 565 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); | 505 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); |
| 566 menu->ExecuteCommand(command_id, 0); | 506 menu->ExecuteCommand(command_id, 0); |
| 567 | 507 |
| 568 // Wait for the extension's script to tell us its onclick fired. Ensure | 508 // Wait for the extension's script to tell us its onclick fired. Ensure |
| 569 // that the incognito version doesn't fire until we explicitly click the | 509 // that the incognito version doesn't fire until we explicitly click the |
| 570 // incognito menu item. | 510 // incognito menu item. |
| 571 ASSERT_TRUE(onclick.WaitUntilSatisfied()); | 511 ASSERT_TRUE(onclick.WaitUntilSatisfied()); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 625 page_complete.Wait(); | 565 page_complete.Wait(); |
| 626 | 566 |
| 627 // Test that menu items appear while the page is unloaded. | 567 // Test that menu items appear while the page is unloaded. |
| 628 ASSERT_TRUE(MenuHasItemWithLabel( | 568 ASSERT_TRUE(MenuHasItemWithLabel( |
| 629 about_blank, GURL(), GURL(), std::string("Item 1"))); | 569 about_blank, GURL(), GURL(), std::string("Item 1"))); |
| 630 ASSERT_TRUE(MenuHasItemWithLabel( | 570 ASSERT_TRUE(MenuHasItemWithLabel( |
| 631 about_blank, GURL(), GURL(), std::string("Checkbox 1"))); | 571 about_blank, GURL(), GURL(), std::string("Checkbox 1"))); |
| 632 | 572 |
| 633 // Test that checked menu items retain their checkedness. | 573 // Test that checked menu items retain their checkedness. |
| 634 LazyBackgroundObserver checkbox_checked; | 574 LazyBackgroundObserver checkbox_checked; |
| 635 scoped_ptr<TestRenderViewContextMenu> menu( | 575 scoped_ptr<TestRenderViewContextMenu> menu(TestRenderViewContextMenu::Create( |
| 636 CreateMenu(browser(), about_blank, GURL(), GURL())); | 576 GetWebContents(), about_blank, GURL(), GURL())); |
| 637 MenuItem::Id id(false, extension->id()); | 577 |
| 578 MenuItem::Id id(false, MenuItem::ExtensionKey(extension->id())); |
| 638 id.string_uid = "checkbox1"; | 579 id.string_uid = "checkbox1"; |
| 639 int command_id = -1; | 580 int command_id = -1; |
| 640 ASSERT_TRUE(FindCommandId(menu.get(), id, &command_id)); | 581 ASSERT_TRUE(FindCommandId(menu.get(), id, &command_id)); |
| 641 EXPECT_FALSE(menu->IsCommandIdChecked(command_id)); | 582 EXPECT_FALSE(menu->IsCommandIdChecked(command_id)); |
| 642 | 583 |
| 643 // Executing the checkbox also fires the onClicked event. | 584 // Executing the checkbox also fires the onClicked event. |
| 644 ExtensionTestMessageListener listener("onClicked fired for checkbox1", false); | 585 ExtensionTestMessageListener listener("onClicked fired for checkbox1", false); |
| 645 menu->ExecuteCommand(command_id, 0); | 586 menu->ExecuteCommand(command_id, 0); |
| 646 checkbox_checked.WaitUntilClosed(); | 587 checkbox_checked.WaitUntilClosed(); |
| 647 | 588 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 660 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito")); | 601 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito")); |
| 661 | 602 |
| 662 // Wait for the extension's processes to tell us they've created an item. | 603 // Wait for the extension's processes to tell us they've created an item. |
| 663 ASSERT_TRUE(created.WaitUntilSatisfied()); | 604 ASSERT_TRUE(created.WaitUntilSatisfied()); |
| 664 ASSERT_TRUE(created_incognito.WaitUntilSatisfied()); | 605 ASSERT_TRUE(created_incognito.WaitUntilSatisfied()); |
| 665 ASSERT_EQ(2u, GetItems().size()); | 606 ASSERT_EQ(2u, GetItems().size()); |
| 666 | 607 |
| 667 browser()->profile()->DestroyOffTheRecordProfile(); | 608 browser()->profile()->DestroyOffTheRecordProfile(); |
| 668 ASSERT_EQ(1u, GetItems().size()); | 609 ASSERT_EQ(1u, GetItems().size()); |
| 669 } | 610 } |
| OLD | NEW |