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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "chrome/browser/extensions/extension_browsertest.h" | 10 #include "chrome/browser/extensions/extension_browsertest.h" |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 return result; | 99 return result; |
100 } | 100 } |
101 | 101 |
102 // This creates a test menu for a page with |page_url| and |link_url|, looks | 102 // This creates a test menu for a page with |page_url| and |link_url|, looks |
103 // for an extension item with the given |label|, and returns true if the item | 103 // for an extension item with the given |label|, and returns true if the item |
104 // was found. | 104 // was found. |
105 bool MenuHasItemWithLabel(const GURL& page_url, | 105 bool MenuHasItemWithLabel(const GURL& page_url, |
106 const GURL& link_url, | 106 const GURL& link_url, |
107 const GURL& frame_url, | 107 const GURL& frame_url, |
108 const std::string& label) { | 108 const std::string& label) { |
109 scoped_ptr<TestRenderViewContextMenu> menu( | 109 std::unique_ptr<TestRenderViewContextMenu> menu( |
110 TestRenderViewContextMenu::Create( | 110 TestRenderViewContextMenu::Create(GetWebContents(), page_url, link_url, |
111 GetWebContents(), page_url, link_url, frame_url)); | 111 frame_url)); |
112 return MenuHasExtensionItemWithLabel(menu.get(), label); | 112 return MenuHasExtensionItemWithLabel(menu.get(), label); |
113 } | 113 } |
114 | 114 |
115 // Click on a context menu identified by |target_menu_item_id|, and returns | 115 // Click on a context menu identified by |target_menu_item_id|, and returns |
116 // the result of chrome.test.sendMessage. The .js test file that sets up the | 116 // the result of chrome.test.sendMessage. The .js test file that sets up the |
117 // context menu should call chrome.test.sendMessage in its onclick event. | 117 // context menu should call chrome.test.sendMessage in its onclick event. |
118 std::string ClickMenuInFrame(content::RenderFrameHost* frame, | 118 std::string ClickMenuInFrame(content::RenderFrameHost* frame, |
119 const std::string& target_menu_item_id) { | 119 const std::string& target_menu_item_id) { |
120 content::ContextMenuParams params; | 120 content::ContextMenuParams params; |
121 if (frame->GetParent()) | 121 if (frame->GetParent()) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 else | 155 else |
156 begin.Reply("start disabled"); | 156 begin.Reply("start disabled"); |
157 | 157 |
158 // Wait for the extension to tell us it's created an item. | 158 // Wait for the extension to tell us it's created an item. |
159 ASSERT_TRUE(create.WaitUntilSatisfied()); | 159 ASSERT_TRUE(create.WaitUntilSatisfied()); |
160 create.Reply("go"); | 160 create.Reply("go"); |
161 | 161 |
162 GURL page_url("http://www.google.com"); | 162 GURL page_url("http://www.google.com"); |
163 | 163 |
164 // Create and build our test context menu. | 164 // Create and build our test context menu. |
165 scoped_ptr<TestRenderViewContextMenu> menu( | 165 std::unique_ptr<TestRenderViewContextMenu> menu( |
166 TestRenderViewContextMenu::Create( | 166 TestRenderViewContextMenu::Create(GetWebContents(), page_url, GURL(), |
167 GetWebContents(), page_url, GURL(), GURL())); | 167 GURL())); |
168 | 168 |
169 // Look for the extension item in the menu, and make sure it's |enabled|. | 169 // Look for the extension item in the menu, and make sure it's |enabled|. |
170 int command_id = ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0); | 170 int command_id = ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0); |
171 ASSERT_EQ(enabled, menu->IsCommandIdEnabled(command_id)); | 171 ASSERT_EQ(enabled, menu->IsCommandIdEnabled(command_id)); |
172 | 172 |
173 // Update the item and make sure it is now |!enabled|. | 173 // Update the item and make sure it is now |!enabled|. |
174 ASSERT_TRUE(update.WaitUntilSatisfied()); | 174 ASSERT_TRUE(update.WaitUntilSatisfied()); |
175 ASSERT_EQ(!enabled, menu->IsCommandIdEnabled(command_id)); | 175 ASSERT_EQ(!enabled, menu->IsCommandIdEnabled(command_id)); |
176 } | 176 } |
177 | 177 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 ExtensionTestMessageListener listener1("created item", false); | 231 ExtensionTestMessageListener listener1("created item", false); |
232 ExtensionTestMessageListener listener2("onclick fired", false); | 232 ExtensionTestMessageListener listener2("onclick fired", false); |
233 ASSERT_TRUE(LoadContextMenuExtension("simple")); | 233 ASSERT_TRUE(LoadContextMenuExtension("simple")); |
234 | 234 |
235 // Wait for the extension to tell us it's created an item. | 235 // Wait for the extension to tell us it's created an item. |
236 ASSERT_TRUE(listener1.WaitUntilSatisfied()); | 236 ASSERT_TRUE(listener1.WaitUntilSatisfied()); |
237 | 237 |
238 GURL page_url("http://www.google.com"); | 238 GURL page_url("http://www.google.com"); |
239 | 239 |
240 // Create and build our test context menu. | 240 // Create and build our test context menu. |
241 scoped_ptr<TestRenderViewContextMenu> menu(TestRenderViewContextMenu::Create( | 241 std::unique_ptr<TestRenderViewContextMenu> menu( |
242 GetWebContents(), page_url, GURL(), GURL())); | 242 TestRenderViewContextMenu::Create(GetWebContents(), page_url, GURL(), |
| 243 GURL())); |
243 | 244 |
244 // Look for the extension item in the menu, and execute it. | 245 // Look for the extension item in the menu, and execute it. |
245 int command_id = ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0); | 246 int command_id = ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0); |
246 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); | 247 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); |
247 menu->ExecuteCommand(command_id, 0); | 248 menu->ExecuteCommand(command_id, 0); |
248 | 249 |
249 // Wait for the extension's script to tell us its onclick fired. | 250 // Wait for the extension's script to tell us its onclick fired. |
250 ASSERT_TRUE(listener2.WaitUntilSatisfied()); | 251 ASSERT_TRUE(listener2.WaitUntilSatisfied()); |
251 } | 252 } |
252 | 253 |
253 // Tests that previous onclick is not fired after updating the menu's onclick, | 254 // Tests that previous onclick is not fired after updating the menu's onclick, |
254 // and whether setting onclick to null removes the handler. | 255 // and whether setting onclick to null removes the handler. |
255 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, UpdateOnclick) { | 256 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, UpdateOnclick) { |
256 ExtensionTestMessageListener listener_error1("onclick1-unexpected", false); | 257 ExtensionTestMessageListener listener_error1("onclick1-unexpected", false); |
257 ExtensionTestMessageListener listener_error2("onclick2-unexpected", false); | 258 ExtensionTestMessageListener listener_error2("onclick2-unexpected", false); |
258 ExtensionTestMessageListener listener_update1("update1", true); | 259 ExtensionTestMessageListener listener_update1("update1", true); |
259 ExtensionTestMessageListener listener_update2("update2", false); | 260 ExtensionTestMessageListener listener_update2("update2", false); |
260 ExtensionTestMessageListener listener_done("onclick2", false); | 261 ExtensionTestMessageListener listener_done("onclick2", false); |
261 | 262 |
262 const extensions::Extension* extension = | 263 const extensions::Extension* extension = |
263 LoadContextMenuExtension("onclick_null"); | 264 LoadContextMenuExtension("onclick_null"); |
264 ASSERT_TRUE(extension); | 265 ASSERT_TRUE(extension); |
265 | 266 |
266 // Wait till item has been created and updated. | 267 // Wait till item has been created and updated. |
267 ASSERT_TRUE(listener_update1.WaitUntilSatisfied()); | 268 ASSERT_TRUE(listener_update1.WaitUntilSatisfied()); |
268 | 269 |
269 GURL page_url("http://www.google.com"); | 270 GURL page_url("http://www.google.com"); |
270 | 271 |
271 // Create and build our test context menu. | 272 // Create and build our test context menu. |
272 scoped_ptr<TestRenderViewContextMenu> menu(TestRenderViewContextMenu::Create( | 273 std::unique_ptr<TestRenderViewContextMenu> menu( |
273 GetWebContents(), page_url, GURL(), GURL())); | 274 TestRenderViewContextMenu::Create(GetWebContents(), page_url, GURL(), |
| 275 GURL())); |
274 | 276 |
275 // Look for the extension item in the menu, and execute it. | 277 // Look for the extension item in the menu, and execute it. |
276 MenuItem::Id id(false, MenuItem::ExtensionKey(extension->id())); | 278 MenuItem::Id id(false, MenuItem::ExtensionKey(extension->id())); |
277 id.string_uid = "id1"; | 279 id.string_uid = "id1"; |
278 int command_id = -1; | 280 int command_id = -1; |
279 ASSERT_TRUE(FindCommandId(menu.get(), id, &command_id)); | 281 ASSERT_TRUE(FindCommandId(menu.get(), id, &command_id)); |
280 menu->ExecuteCommand(command_id, 0); | 282 menu->ExecuteCommand(command_id, 0); |
281 | 283 |
282 // Let the test proceed. | 284 // Let the test proceed. |
283 listener_update1.Reply(""); | 285 listener_update1.Reply(""); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 // Make sure we have an item registered with a long title. | 345 // Make sure we have an item registered with a long title. |
344 size_t limit = extensions::ContextMenuMatcher::kMaxExtensionItemTitleLength; | 346 size_t limit = extensions::ContextMenuMatcher::kMaxExtensionItemTitleLength; |
345 MenuItem::List items = GetItems(); | 347 MenuItem::List items = GetItems(); |
346 ASSERT_EQ(1u, items.size()); | 348 ASSERT_EQ(1u, items.size()); |
347 MenuItem* item = items.at(0); | 349 MenuItem* item = items.at(0); |
348 ASSERT_GT(item->title().size(), limit); | 350 ASSERT_GT(item->title().size(), limit); |
349 | 351 |
350 // Create a context menu, then find the item's label. It should be properly | 352 // Create a context menu, then find the item's label. It should be properly |
351 // truncated. | 353 // truncated. |
352 GURL url("http://foo.com/"); | 354 GURL url("http://foo.com/"); |
353 scoped_ptr<TestRenderViewContextMenu> menu( | 355 std::unique_ptr<TestRenderViewContextMenu> menu( |
354 TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL())); | 356 TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL())); |
355 | 357 |
356 base::string16 label; | 358 base::string16 label; |
357 ASSERT_TRUE(GetItemLabel(menu.get(), item->id(), &label)); | 359 ASSERT_TRUE(GetItemLabel(menu.get(), item->id(), &label)); |
358 ASSERT_TRUE(label.size() <= limit); | 360 ASSERT_TRUE(label.size() <= limit); |
359 } | 361 } |
360 | 362 |
361 // Flaky on Windows debug bots. http://crbug.com/251590 | 363 // Flaky on Windows debug bots. http://crbug.com/251590 |
362 #if defined(OS_WIN) | 364 #if defined(OS_WIN) |
363 #define MAYBE_TopLevel DISABLED_TopLevel | 365 #define MAYBE_TopLevel DISABLED_TopLevel |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 // Load extensions and wait until it's created two menu items. | 397 // Load extensions and wait until it's created two menu items. |
396 ExtensionTestMessageListener listener4("created items", false); | 398 ExtensionTestMessageListener listener4("created items", false); |
397 ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi4")); | 399 ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi4")); |
398 ASSERT_TRUE(listener4.WaitUntilSatisfied()); | 400 ASSERT_TRUE(listener4.WaitUntilSatisfied()); |
399 | 401 |
400 ExtensionTestMessageListener listener5("created items", false); | 402 ExtensionTestMessageListener listener5("created items", false); |
401 ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi5")); | 403 ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi5")); |
402 ASSERT_TRUE(listener5.WaitUntilSatisfied()); | 404 ASSERT_TRUE(listener5.WaitUntilSatisfied()); |
403 | 405 |
404 GURL url("http://foo.com/"); | 406 GURL url("http://foo.com/"); |
405 scoped_ptr<TestRenderViewContextMenu> menu( | 407 std::unique_ptr<TestRenderViewContextMenu> menu( |
406 TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL())); | 408 TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL())); |
407 | 409 |
408 int index = 0; | 410 int index = 0; |
409 MenuModel* model = NULL; | 411 MenuModel* model = NULL; |
410 | 412 |
411 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( | 413 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( |
412 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0), | 414 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0), |
413 &model, | 415 &model, |
414 &index)); | 416 &index)); |
415 EXPECT_EQ(base::UTF8ToUTF16("An Extension with multiple Context Menus"), | 417 EXPECT_EQ(base::UTF8ToUTF16("An Extension with multiple Context Menus"), |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 | 482 |
481 // Navigate to test1.html inside the extension, which should create a bunch | 483 // Navigate to test1.html inside the extension, which should create a bunch |
482 // of items at the top-level (but they'll get pushed into an auto-generated | 484 // of items at the top-level (but they'll get pushed into an auto-generated |
483 // parent). | 485 // parent). |
484 ExtensionTestMessageListener listener1("test1 create finished", false); | 486 ExtensionTestMessageListener listener1("test1 create finished", false); |
485 ui_test_utils::NavigateToURL(browser(), | 487 ui_test_utils::NavigateToURL(browser(), |
486 GURL(extension->GetResourceURL("test1.html"))); | 488 GURL(extension->GetResourceURL("test1.html"))); |
487 listener1.WaitUntilSatisfied(); | 489 listener1.WaitUntilSatisfied(); |
488 | 490 |
489 GURL url("http://www.google.com/"); | 491 GURL url("http://www.google.com/"); |
490 scoped_ptr<TestRenderViewContextMenu> menu( | 492 std::unique_ptr<TestRenderViewContextMenu> menu( |
491 TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL())); | 493 TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL())); |
492 | 494 |
493 // The top-level item should be an "automagic parent" with the extension's | 495 // The top-level item should be an "automagic parent" with the extension's |
494 // name. | 496 // name. |
495 MenuModel* model = NULL; | 497 MenuModel* model = NULL; |
496 int index = 0; | 498 int index = 0; |
497 base::string16 label; | 499 base::string16 label; |
498 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( | 500 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( |
499 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0), | 501 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0), |
500 &model, | 502 &model, |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 | 576 |
575 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito")); | 577 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito")); |
576 | 578 |
577 // Wait for the extension's processes to tell us they've created an item. | 579 // Wait for the extension's processes to tell us they've created an item. |
578 ASSERT_TRUE(created.WaitUntilSatisfied()); | 580 ASSERT_TRUE(created.WaitUntilSatisfied()); |
579 ASSERT_TRUE(created_incognito.WaitUntilSatisfied()); | 581 ASSERT_TRUE(created_incognito.WaitUntilSatisfied()); |
580 | 582 |
581 GURL page_url("http://www.google.com"); | 583 GURL page_url("http://www.google.com"); |
582 | 584 |
583 // Create and build our test context menu. | 585 // Create and build our test context menu. |
584 scoped_ptr<TestRenderViewContextMenu> menu(TestRenderViewContextMenu::Create( | 586 std::unique_ptr<TestRenderViewContextMenu> menu( |
585 GetWebContents(), page_url, GURL(), GURL())); | 587 TestRenderViewContextMenu::Create(GetWebContents(), page_url, GURL(), |
| 588 GURL())); |
586 WebContents* incognito_web_contents = | 589 WebContents* incognito_web_contents = |
587 browser_incognito->tab_strip_model()->GetActiveWebContents(); | 590 browser_incognito->tab_strip_model()->GetActiveWebContents(); |
588 scoped_ptr<TestRenderViewContextMenu> menu_incognito( | 591 std::unique_ptr<TestRenderViewContextMenu> menu_incognito( |
589 TestRenderViewContextMenu::Create( | 592 TestRenderViewContextMenu::Create(incognito_web_contents, page_url, |
590 incognito_web_contents, page_url, GURL(), GURL())); | 593 GURL(), GURL())); |
591 | 594 |
592 // Look for the extension item in the menu, and execute it. | 595 // Look for the extension item in the menu, and execute it. |
593 int command_id = ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0); | 596 int command_id = ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0); |
594 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); | 597 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); |
595 menu->ExecuteCommand(command_id, 0); | 598 menu->ExecuteCommand(command_id, 0); |
596 | 599 |
597 // Wait for the extension's script to tell us its onclick fired. Ensure | 600 // Wait for the extension's script to tell us its onclick fired. Ensure |
598 // that the incognito version doesn't fire until we explicitly click the | 601 // that the incognito version doesn't fire until we explicitly click the |
599 // incognito menu item. | 602 // incognito menu item. |
600 ASSERT_TRUE(onclick.WaitUntilSatisfied()); | 603 ASSERT_TRUE(onclick.WaitUntilSatisfied()); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
677 page_complete.Wait(); | 680 page_complete.Wait(); |
678 | 681 |
679 // Test that menu items appear while the page is unloaded. | 682 // Test that menu items appear while the page is unloaded. |
680 ASSERT_TRUE(MenuHasItemWithLabel( | 683 ASSERT_TRUE(MenuHasItemWithLabel( |
681 about_blank, GURL(), GURL(), std::string("Item 1"))); | 684 about_blank, GURL(), GURL(), std::string("Item 1"))); |
682 ASSERT_TRUE(MenuHasItemWithLabel( | 685 ASSERT_TRUE(MenuHasItemWithLabel( |
683 about_blank, GURL(), GURL(), std::string("Checkbox 1"))); | 686 about_blank, GURL(), GURL(), std::string("Checkbox 1"))); |
684 | 687 |
685 // Test that checked menu items retain their checkedness. | 688 // Test that checked menu items retain their checkedness. |
686 LazyBackgroundObserver checkbox_checked; | 689 LazyBackgroundObserver checkbox_checked; |
687 scoped_ptr<TestRenderViewContextMenu> menu(TestRenderViewContextMenu::Create( | 690 std::unique_ptr<TestRenderViewContextMenu> menu( |
688 GetWebContents(), about_blank, GURL(), GURL())); | 691 TestRenderViewContextMenu::Create(GetWebContents(), about_blank, GURL(), |
| 692 GURL())); |
689 | 693 |
690 MenuItem::Id id(false, MenuItem::ExtensionKey(extension->id())); | 694 MenuItem::Id id(false, MenuItem::ExtensionKey(extension->id())); |
691 id.string_uid = "checkbox1"; | 695 id.string_uid = "checkbox1"; |
692 int command_id = -1; | 696 int command_id = -1; |
693 ASSERT_TRUE(FindCommandId(menu.get(), id, &command_id)); | 697 ASSERT_TRUE(FindCommandId(menu.get(), id, &command_id)); |
694 EXPECT_FALSE(menu->IsCommandIdChecked(command_id)); | 698 EXPECT_FALSE(menu->IsCommandIdChecked(command_id)); |
695 | 699 |
696 // Executing the checkbox also fires the onClicked event. | 700 // Executing the checkbox also fires the onClicked event. |
697 ExtensionTestMessageListener listener("onClicked fired for checkbox1", false); | 701 ExtensionTestMessageListener listener("onClicked fired for checkbox1", false); |
698 menu->ExecuteCommand(command_id, 0); | 702 menu->ExecuteCommand(command_id, 0); |
(...skipping 14 matching lines...) Expand all Loading... |
713 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito")); | 717 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito")); |
714 | 718 |
715 // Wait for the extension's processes to tell us they've created an item. | 719 // Wait for the extension's processes to tell us they've created an item. |
716 ASSERT_TRUE(created.WaitUntilSatisfied()); | 720 ASSERT_TRUE(created.WaitUntilSatisfied()); |
717 ASSERT_TRUE(created_incognito.WaitUntilSatisfied()); | 721 ASSERT_TRUE(created_incognito.WaitUntilSatisfied()); |
718 ASSERT_EQ(2u, GetItems().size()); | 722 ASSERT_EQ(2u, GetItems().size()); |
719 | 723 |
720 browser()->profile()->DestroyOffTheRecordProfile(); | 724 browser()->profile()->DestroyOffTheRecordProfile(); |
721 ASSERT_EQ(1u, GetItems().size()); | 725 ASSERT_EQ(1u, GetItems().size()); |
722 } | 726 } |
OLD | NEW |