| 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/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 8 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 9 #include "chrome/browser/extensions/extension_browsertest.h" | 10 #include "chrome/browser/extensions/extension_browsertest.h" |
| 10 #include "chrome/browser/extensions/lazy_background_page_test_util.h" | 11 #include "chrome/browser/extensions/lazy_background_page_test_util.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 13 #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" | 14 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti
l.h" |
| 14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 16 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "content/public/browser/render_frame_host.h" |
| 17 #include "content/public/common/context_menu_params.h" | 19 #include "content/public/common/context_menu_params.h" |
| 20 #include "content/public/test/browser_test_utils.h" |
| 21 #include "extensions/browser/extension_api_frame_id_map.h" |
| 18 #include "extensions/browser/extension_registry.h" | 22 #include "extensions/browser/extension_registry.h" |
| 19 #include "extensions/browser/process_manager.h" | 23 #include "extensions/browser/process_manager.h" |
| 20 #include "extensions/browser/test_management_policy.h" | 24 #include "extensions/browser/test_management_policy.h" |
| 21 #include "extensions/common/extension_set.h" | 25 #include "extensions/common/extension_set.h" |
| 22 #include "extensions/test/extension_test_message_listener.h" | 26 #include "extensions/test/extension_test_message_listener.h" |
| 23 #include "net/dns/mock_host_resolver.h" | 27 #include "net/dns/mock_host_resolver.h" |
| 24 #include "ui/base/models/menu_model.h" | 28 #include "ui/base/models/menu_model.h" |
| 25 | 29 |
| 26 using content::WebContents; | 30 using content::WebContents; |
| 27 using extensions::ContextMenuMatcher; | 31 using extensions::ContextMenuMatcher; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 bool MenuHasItemWithLabel(const GURL& page_url, | 105 bool MenuHasItemWithLabel(const GURL& page_url, |
| 102 const GURL& link_url, | 106 const GURL& link_url, |
| 103 const GURL& frame_url, | 107 const GURL& frame_url, |
| 104 const std::string& label) { | 108 const std::string& label) { |
| 105 scoped_ptr<TestRenderViewContextMenu> menu( | 109 scoped_ptr<TestRenderViewContextMenu> menu( |
| 106 TestRenderViewContextMenu::Create( | 110 TestRenderViewContextMenu::Create( |
| 107 GetWebContents(), page_url, link_url, frame_url)); | 111 GetWebContents(), page_url, link_url, frame_url)); |
| 108 return MenuHasExtensionItemWithLabel(menu.get(), label); | 112 return MenuHasExtensionItemWithLabel(menu.get(), label); |
| 109 } | 113 } |
| 110 | 114 |
| 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 |
| 117 // context menu should call chrome.test.sendMessage in its onclick event. |
| 118 std::string ClickMenuInFrame(content::RenderFrameHost* frame, |
| 119 const std::string& target_menu_item_id) { |
| 120 content::ContextMenuParams params; |
| 121 if (frame->GetParent()) |
| 122 params.frame_url = frame->GetLastCommittedURL(); |
| 123 else |
| 124 params.page_url = frame->GetLastCommittedURL(); |
| 125 |
| 126 TestRenderViewContextMenu menu(frame, params); |
| 127 menu.Init(); |
| 128 |
| 129 MenuItem::Id menu_item_id; |
| 130 menu_item_id.string_uid = target_menu_item_id; |
| 131 int command_id = -1; |
| 132 if (!FindCommandId(&menu, menu_item_id, &command_id)) |
| 133 return "Menu item not found: " + target_menu_item_id; |
| 134 |
| 135 ExtensionTestMessageListener listener(false); |
| 136 menu.ExecuteCommand(command_id, 0); |
| 137 if (!listener.WaitUntilSatisfied()) |
| 138 return "Onclick never fired for menu item: " + target_menu_item_id; |
| 139 |
| 140 return listener.message(); |
| 141 } |
| 142 |
| 111 // This creates an extension that starts |enabled| and then switches to | 143 // This creates an extension that starts |enabled| and then switches to |
| 112 // |!enabled|. | 144 // |!enabled|. |
| 113 void TestEnabledContextMenu(bool enabled) { | 145 void TestEnabledContextMenu(bool enabled) { |
| 114 ExtensionTestMessageListener begin("begin", true); | 146 ExtensionTestMessageListener begin("begin", true); |
| 115 ExtensionTestMessageListener create("create", true); | 147 ExtensionTestMessageListener create("create", true); |
| 116 ExtensionTestMessageListener update("update", false); | 148 ExtensionTestMessageListener update("update", false); |
| 117 ASSERT_TRUE(LoadContextMenuExtension("enabled")); | 149 ASSERT_TRUE(LoadContextMenuExtension("enabled")); |
| 118 | 150 |
| 119 ASSERT_TRUE(begin.WaitUntilSatisfied()); | 151 ASSERT_TRUE(begin.WaitUntilSatisfied()); |
| 120 | 152 |
| (...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 588 page_url, GURL(), no_frame_url, std::string("Page item"))); | 620 page_url, GURL(), no_frame_url, std::string("Page item"))); |
| 589 ASSERT_FALSE(MenuHasItemWithLabel( | 621 ASSERT_FALSE(MenuHasItemWithLabel( |
| 590 page_url, GURL(), no_frame_url, std::string("Frame item"))); | 622 page_url, GURL(), no_frame_url, std::string("Frame item"))); |
| 591 | 623 |
| 592 ASSERT_TRUE(MenuHasItemWithLabel( | 624 ASSERT_TRUE(MenuHasItemWithLabel( |
| 593 page_url, GURL(), frame_url, std::string("Page item"))); | 625 page_url, GURL(), frame_url, std::string("Page item"))); |
| 594 ASSERT_TRUE(MenuHasItemWithLabel( | 626 ASSERT_TRUE(MenuHasItemWithLabel( |
| 595 page_url, GURL(), frame_url, std::string("Frame item"))); | 627 page_url, GURL(), frame_url, std::string("Frame item"))); |
| 596 } | 628 } |
| 597 | 629 |
| 630 // Tests that info.frameId is correctly set when the context menu is invoked. |
| 631 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, ClickInFrame) { |
| 632 ExtensionTestMessageListener listener("created items", false); |
| 633 ASSERT_TRUE(LoadContextMenuExtension("frames")); |
| 634 GURL url_with_frame("data:text/html,<iframe name='child'>"); |
| 635 ui_test_utils::NavigateToURL(browser(), url_with_frame); |
| 636 ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 637 |
| 638 // Click on a menu item in the main frame. |
| 639 EXPECT_EQ( |
| 640 "pageUrl=" + url_with_frame.spec() + ", frameUrl=undefined, frameId=0", |
| 641 ClickMenuInFrame(GetWebContents()->GetMainFrame(), "item1")); |
| 642 |
| 643 // Click on a menu item in the child frame. |
| 644 content::RenderFrameHost* child_frame = content::FrameMatchingPredicate( |
| 645 GetWebContents(), base::Bind(&content::FrameMatchesName, "child")); |
| 646 ASSERT_TRUE(child_frame); |
| 647 int extension_api_frame_id = |
| 648 extensions::ExtensionApiFrameIdMap::GetFrameId(child_frame); |
| 649 EXPECT_EQ( |
| 650 base::StringPrintf("pageUrl=undefined, frameUrl=about:blank, frameId=%d", |
| 651 extension_api_frame_id), |
| 652 ClickMenuInFrame(child_frame, "item1")); |
| 653 } |
| 654 |
| 598 // Tests enabling and disabling a context menu item. | 655 // Tests enabling and disabling a context menu item. |
| 599 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Enabled) { | 656 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Enabled) { |
| 600 TestEnabledContextMenu(true); | 657 TestEnabledContextMenu(true); |
| 601 TestEnabledContextMenu(false); | 658 TestEnabledContextMenu(false); |
| 602 } | 659 } |
| 603 | 660 |
| 604 class ExtensionContextMenuBrowserLazyTest : | 661 class ExtensionContextMenuBrowserLazyTest : |
| 605 public ExtensionContextMenuBrowserTest { | 662 public ExtensionContextMenuBrowserTest { |
| 606 void SetUpOnMainThread() override { | 663 void SetUpOnMainThread() override { |
| 607 ExtensionContextMenuBrowserTest::SetUpOnMainThread(); | 664 ExtensionContextMenuBrowserTest::SetUpOnMainThread(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 656 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito")); | 713 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito")); |
| 657 | 714 |
| 658 // Wait for the extension's processes to tell us they've created an item. | 715 // Wait for the extension's processes to tell us they've created an item. |
| 659 ASSERT_TRUE(created.WaitUntilSatisfied()); | 716 ASSERT_TRUE(created.WaitUntilSatisfied()); |
| 660 ASSERT_TRUE(created_incognito.WaitUntilSatisfied()); | 717 ASSERT_TRUE(created_incognito.WaitUntilSatisfied()); |
| 661 ASSERT_EQ(2u, GetItems().size()); | 718 ASSERT_EQ(2u, GetItems().size()); |
| 662 | 719 |
| 663 browser()->profile()->DestroyOffTheRecordProfile(); | 720 browser()->profile()->DestroyOffTheRecordProfile(); |
| 664 ASSERT_EQ(1u, GetItems().size()); | 721 ASSERT_EQ(1u, GetItems().size()); |
| 665 } | 722 } |
| OLD | NEW |