diff options
author | jennb@chromium.org <jennb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-27 13:01:49 +0000 |
---|---|---|
committer | jennb@chromium.org <jennb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-27 13:01:49 +0000 |
commit | 95c98b680a6a686bc4a2a3d74c3e01e5a8143daf (patch) | |
tree | 71dd13a45a36abea6d6f2e6538820d31b1ca0bf0 | |
parent | 88a61bf4abf1483f6d8ade487559a0c10f17cc1b (diff) | |
download | chromium_src-95c98b680a6a686bc4a2a3d74c3e01e5a8143daf.zip chromium_src-95c98b680a6a686bc4a2a3d74c3e01e5a8143daf.tar.gz chromium_src-95c98b680a6a686bc4a2a3d74c3e01e5a8143daf.tar.bz2 |
[Panels] Allow edit-related context menus.
BUG=155610
TEST=PanelExtensionBrowserTest.*ContextMenu
Review URL: https://chromiumcodereview.appspot.com/11175047
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@164541 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/tab_contents/render_view_context_menu.cc | 19 | ||||
-rw-r--r-- | chrome/browser/tab_contents/render_view_context_menu.h | 1 | ||||
-rw-r--r-- | chrome/browser/ui/panels/panel_extension_browsertest.cc | 129 | ||||
-rw-r--r-- | chrome/browser/ui/panels/panel_host.cc | 4 | ||||
-rw-r--r-- | chrome/browser/ui/panels/panel_host.h | 2 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 | ||||
-rw-r--r-- | chrome/test/data/panels/basic/manifest.json | 10 | ||||
-rw-r--r-- | chrome/test/data/panels/basic/panel.html | 10 | ||||
-rw-r--r-- | chrome/test/data/panels/basic/panel.js | 5 | ||||
-rw-r--r-- | chrome/test/data/panels/basic/test.js | 6 | ||||
-rw-r--r-- | chrome/test/data/panels/context_menu/manifest.json | 10 | ||||
-rw-r--r-- | chrome/test/data/panels/context_menu/panel.html | 10 | ||||
-rw-r--r-- | chrome/test/data/panels/context_menu/panel.js | 7 | ||||
-rw-r--r-- | chrome/test/data/panels/context_menu/test.js | 20 | ||||
-rw-r--r-- | chrome/test/data/panels/context_menu2/manifest.json | 10 | ||||
-rw-r--r-- | chrome/test/data/panels/context_menu2/test.js | 14 |
16 files changed, 252 insertions, 6 deletions
diff --git a/chrome/browser/tab_contents/render_view_context_menu.cc b/chrome/browser/tab_contents/render_view_context_menu.cc index 1bb5296..73f6c00 100644 --- a/chrome/browser/tab_contents/render_view_context_menu.cc +++ b/chrome/browser/tab_contents/render_view_context_menu.cc @@ -404,6 +404,9 @@ void RenderViewContextMenu::InitMenu() { } else if (view_type == chrome::VIEW_TYPE_EXTENSION_POPUP) { AppendPopupExtensionItems(); return; + } else if (view_type == chrome::VIEW_TYPE_PANEL) { + AppendPanelItems(); + return; } bool has_link = !params_.unfiltered_link_url.is_empty(); @@ -564,6 +567,22 @@ void RenderViewContextMenu::AppendPopupExtensionItems() { AppendDeveloperItems(); } +void RenderViewContextMenu::AppendPanelItems() { + const Extension* extension = GetExtension(); + + bool has_selection = !params_.selection_text.empty(); + + if (params_.is_editable) + AppendEditableItems(); + else if (has_selection) + AppendCopyItem(); + + // Only add extension items from this extension. + int index = 0; + extension_items_.AppendExtensionItems(extension->id(), + PrintableSelectionText(), &index); +} + void RenderViewContextMenu::AddMenuItem(int command_id, const string16& title) { menu_model_.AddItem(command_id, title); diff --git a/chrome/browser/tab_contents/render_view_context_menu.h b/chrome/browser/tab_contents/render_view_context_menu.h index b50b292..d79d9ab 100644 --- a/chrome/browser/tab_contents/render_view_context_menu.h +++ b/chrome/browser/tab_contents/render_view_context_menu.h @@ -205,6 +205,7 @@ class RenderViewContextMenu : public ui::SimpleMenuModel::Delegate, const extensions::Extension* GetExtension() const; void AppendPlatformAppItems(); void AppendPopupExtensionItems(); + void AppendPanelItems(); bool AppendCustomItems(); void AppendDeveloperItems(); void AppendLinkItems(); diff --git a/chrome/browser/ui/panels/panel_extension_browsertest.cc b/chrome/browser/ui/panels/panel_extension_browsertest.cc index eb7fad4..a642e06 100644 --- a/chrome/browser/ui/panels/panel_extension_browsertest.cc +++ b/chrome/browser/ui/panels/panel_extension_browsertest.cc @@ -4,8 +4,12 @@ #include "base/command_line.h" #include "base/path_service.h" +#include "base/utf_string_conversions.h" +#include "chrome/app/chrome_command_ids.h" #include "chrome/browser/extensions/extension_apitest.h" +#include "chrome/browser/extensions/extension_test_message_listener.h" #include "chrome/browser/profiles/profile.h" +#include "chrome/browser/tab_contents/render_view_context_menu.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/panels/native_panel.h" #include "chrome/browser/ui/panels/panel.h" @@ -17,6 +21,7 @@ #include "chrome/common/chrome_switches.h" #include "chrome/common/extensions/extension.h" #include "chrome/test/base/ui_test_utils.h" +#include "content/public/browser/web_contents.h" #include "content/public/test/test_utils.h" #include "testing/gtest/include/gtest/gtest.h" @@ -104,6 +109,130 @@ IN_PROC_BROWSER_TEST_F(PanelExtensionBrowserTest, panel->Close(); } +// Non-abstract RenderViewContextMenu class for testing context menus in Panels. +class PanelContextMenu : public RenderViewContextMenu { + public: + PanelContextMenu(content::WebContents* web_contents, + const content::ContextMenuParams& params) + : RenderViewContextMenu(web_contents, params) {} + + bool HasCommandWithId(int command_id) { + return menu_model_.GetIndexOfCommandId(command_id) != -1; + } + + protected: + // RenderViewContextMenu implementation. + virtual bool GetAcceleratorForCommandId( + int command_id, + ui::Accelerator* accelerator) OVERRIDE { + return false; + } + virtual void PlatformInit() OVERRIDE {} + virtual void PlatformCancel() OVERRIDE {} +}; + +IN_PROC_BROWSER_TEST_F(PanelExtensionBrowserTest, BasicContextMenu) { + ExtensionTestMessageListener listener("panel loaded", false); + LoadExtension(test_data_dir_.AppendASCII("basic")); + ASSERT_TRUE(listener.WaitUntilSatisfied()); + + // There should only be one panel. + PanelManager* panel_manager = PanelManager::GetInstance(); + EXPECT_EQ(1, panel_manager->num_panels()); + Panel* panel = panel_manager->panels().front(); + content::WebContents* web_contents = panel->GetWebContents(); + ASSERT_TRUE(web_contents); + + WebKit::WebContextMenuData data; + content::ContextMenuParams params(data); + params.page_url = web_contents->GetURL(); + + // Ensure context menu isn't swallowed by WebContentsDelegate (the panel). + EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params)); + + // Verify basic menu contents. The basic extension does not add any + // context menu items so the panel's menu should include only the + // developer tools. + scoped_ptr<PanelContextMenu> menu; + menu.reset(new PanelContextMenu(web_contents, params)); + menu->Init(); + EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_PASTE)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_BACK)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE)); + + // Verify expected menu contents for editable item. + params.is_editable = true; + menu.reset(new PanelContextMenu(web_contents, params)); + menu->Init(); + EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT)); + EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO)); + EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_PASTE)); + EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_BACK)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE)); + + // Verify expected menu contents for text selection. + params.is_editable = false; + params.selection_text = ASCIIToUTF16("Select me"); + menu.reset(new PanelContextMenu(web_contents, params)); + menu->Init(); + EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_PASTE)); + EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_BACK)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE)); +} + +IN_PROC_BROWSER_TEST_F(PanelExtensionBrowserTest, CustomContextMenu) { + ExtensionTestMessageListener listener("created item", false); + LoadExtension(test_data_dir_.AppendASCII("context_menu")); + ASSERT_TRUE(listener.WaitUntilSatisfied()); + + // Load a second extension that also creates a custom context menu item. + ExtensionTestMessageListener bogey_listener("created bogey item", false); + LoadExtension(test_data_dir_.AppendASCII("context_menu2")); + ASSERT_TRUE(bogey_listener.WaitUntilSatisfied()); + + // There should only be one panel. + PanelManager* panel_manager = PanelManager::GetInstance(); + EXPECT_EQ(1, panel_manager->num_panels()); + Panel* panel = panel_manager->panels().front(); + content::WebContents* web_contents = panel->GetWebContents(); + ASSERT_TRUE(web_contents); + + WebKit::WebContextMenuData data; + content::ContextMenuParams params(data); + params.page_url = web_contents->GetURL(); + + // Ensure context menu isn't swallowed by WebContentsDelegate (the panel). + EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params)); + + // Verify menu contents contains the custom item added by their own extension. + scoped_ptr<PanelContextMenu> menu; + menu.reset(new PanelContextMenu(web_contents, params)); + menu->Init(); + EXPECT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + 1)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_PASTE)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_BACK)); + EXPECT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE)); + + // Execute the extension's custom menu item and wait for the extension's + // script to tell us its onclick fired. + ExtensionTestMessageListener onclick_listener("clicked", false); + int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; + ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); + menu->ExecuteCommand(command_id); + EXPECT_TRUE(onclick_listener.WaitUntilSatisfied()); +} + class PanelExtensionApiTest : public ExtensionApiTest { protected: virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { diff --git a/chrome/browser/ui/panels/panel_host.cc b/chrome/browser/ui/panels/panel_host.cc index 1dcef9c..5949b47 100644 --- a/chrome/browser/ui/panels/panel_host.cc +++ b/chrome/browser/ui/panels/panel_host.cc @@ -179,10 +179,6 @@ void PanelHost::ContentsZoomChange(bool zoom_in) { Zoom(zoom_in ? content::PAGE_ZOOM_IN : content::PAGE_ZOOM_OUT); } -bool PanelHost::HandleContextMenu(const content::ContextMenuParams& params) { - return true; // Disallow context menu. -} - void PanelHost::HandleKeyboardEvent( content::WebContents* source, const content::NativeWebKeyboardEvent& event) { diff --git a/chrome/browser/ui/panels/panel_host.h b/chrome/browser/ui/panels/panel_host.h index 9049fbc..e7344cf 100644 --- a/chrome/browser/ui/panels/panel_host.h +++ b/chrome/browser/ui/panels/panel_host.h @@ -68,8 +68,6 @@ class PanelHost : public content::WebContentsDelegate, virtual bool IsPopupOrPanel( const content::WebContents* source) const OVERRIDE; virtual void ContentsZoomChange(bool zoom_in) OVERRIDE; - virtual bool HandleContextMenu( - const content::ContextMenuParams& params) OVERRIDE; virtual void HandleKeyboardEvent( content::WebContents* source, const content::NativeWebKeyboardEvent& event) OVERRIDE; diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 1a2df29..1a38c79 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -3370,6 +3370,7 @@ 'browser/service/service_process_control_browsertest.cc', # chromeos does not use cross-platform panels 'browser/ui/views/panels/panel_view_browsertest.cc', + 'browser/ui/panels/panel_extension_browsertest.cc', ], 'dependencies': [ '../dbus/dbus.gyp:dbus_test_support', diff --git a/chrome/test/data/panels/basic/manifest.json b/chrome/test/data/panels/basic/manifest.json new file mode 100644 index 0000000..ea9d236 --- /dev/null +++ b/chrome/test/data/panels/basic/manifest.json @@ -0,0 +1,10 @@ +{ + "name": "Simple extension that creates a panel.", + "version": "1", + "manifest_version": 2, + "description": "Used by tests that simply need a panel created.", + "background": { + "scripts": ["test.js"] + }, + "permissions": ["tabs"] +} diff --git a/chrome/test/data/panels/basic/panel.html b/chrome/test/data/panels/basic/panel.html new file mode 100644 index 0000000..1d035d2 --- /dev/null +++ b/chrome/test/data/panels/basic/panel.html @@ -0,0 +1,10 @@ +<!-- + * Copyright (c) 2012 The Chromium Authors. All rights reserved. Use of this + * source code is governed by a BSD-style license that can be found in the + * LICENSE file. +--> +<html> +<body> +<script src="panel.js"></script> +</body> +</html> diff --git a/chrome/test/data/panels/basic/panel.js b/chrome/test/data/panels/basic/panel.js new file mode 100644 index 0000000..4a5ed5b --- /dev/null +++ b/chrome/test/data/panels/basic/panel.js @@ -0,0 +1,5 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +chrome.test.sendMessage('panel loaded'); diff --git a/chrome/test/data/panels/basic/test.js b/chrome/test/data/panels/basic/test.js new file mode 100644 index 0000000..f30fcbd --- /dev/null +++ b/chrome/test/data/panels/basic/test.js @@ -0,0 +1,6 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +chrome.windows.create({'url': 'panel.html', 'type': 'panel'}, + function() {}); diff --git a/chrome/test/data/panels/context_menu/manifest.json b/chrome/test/data/panels/context_menu/manifest.json new file mode 100644 index 0000000..e44c8e3 --- /dev/null +++ b/chrome/test/data/panels/context_menu/manifest.json @@ -0,0 +1,10 @@ +{ + "name": "Extension that creates a panel and custom context menu item.", + "version": "1", + "manifest_version": 2, + "description": "Used to verify panels can create custom context menu items.", + "background": { + "scripts": ["test.js"] + }, + "permissions": ["contextMenus", "tabs"] +} diff --git a/chrome/test/data/panels/context_menu/panel.html b/chrome/test/data/panels/context_menu/panel.html new file mode 100644 index 0000000..1d035d2 --- /dev/null +++ b/chrome/test/data/panels/context_menu/panel.html @@ -0,0 +1,10 @@ +<!-- + * Copyright (c) 2012 The Chromium Authors. All rights reserved. Use of this + * source code is governed by a BSD-style license that can be found in the + * LICENSE file. +--> +<html> +<body> +<script src="panel.js"></script> +</body> +</html> diff --git a/chrome/test/data/panels/context_menu/panel.js b/chrome/test/data/panels/context_menu/panel.js new file mode 100644 index 0000000..58cb34e --- /dev/null +++ b/chrome/test/data/panels/context_menu/panel.js @@ -0,0 +1,7 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +var background_page = chrome.extension.getBackgroundPage(); +background_page.panelCallback(); + diff --git a/chrome/test/data/panels/context_menu/test.js b/chrome/test/data/panels/context_menu/test.js new file mode 100644 index 0000000..17e7902 --- /dev/null +++ b/chrome/test/data/panels/context_menu/test.js @@ -0,0 +1,20 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +function onclick(info) { + chrome.test.sendMessage("clicked"); +} + +// Invoked by panel when it has loaded. +function panelCallback() { + chrome.contextMenus.create({"title":"Extension Item 1", + "onclick": onclick}, function() { + if (!chrome.extension.lastError) { + chrome.test.sendMessage("created item"); + } + }); +} + +chrome.windows.create({'url': 'panel.html', 'type': 'panel'}, + function() {}); diff --git a/chrome/test/data/panels/context_menu2/manifest.json b/chrome/test/data/panels/context_menu2/manifest.json new file mode 100644 index 0000000..95b00f6 --- /dev/null +++ b/chrome/test/data/panels/context_menu2/manifest.json @@ -0,0 +1,10 @@ +{ + "name": "Extension that creates a custom context menu item.", + "version": "1", + "manifest_version": 2, + "description": "Used to verify panels only see custom context menu items from their own extension.", + "background": { + "scripts": ["test.js"] + }, + "permissions": ["contextMenus", "tabs"] +} diff --git a/chrome/test/data/panels/context_menu2/test.js b/chrome/test/data/panels/context_menu2/test.js new file mode 100644 index 0000000..4f27d9e --- /dev/null +++ b/chrome/test/data/panels/context_menu2/test.js @@ -0,0 +1,14 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +function onclick(info) { + chrome.test.sendMessage("ow, don't do that!"); +} + +chrome.contextMenus.create({"title":"Extension Item Bogey", + "onclick": onclick}, function() { + if (!chrome.extension.lastError) { + chrome.test.sendMessage("created bogey item"); + } +}); |