diff options
author | asargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-07 22:25:23 +0000 |
---|---|---|
committer | asargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-07 22:25:23 +0000 |
commit | 71cf6aa188f65ef51073cb3482c7f2995f73cbe3 (patch) | |
tree | d1ff114f2b1f87044561675341641796a748d698 /chrome | |
parent | 35b1eb2a2e8bca7732421539e9094813fc7e77e2 (diff) | |
download | chromium_src-71cf6aa188f65ef51073cb3482c7f2995f73cbe3.zip chromium_src-71cf6aa188f65ef51073cb3482c7f2995f73cbe3.tar.gz chromium_src-71cf6aa188f65ef51073cb3482c7f2995f73cbe3.tar.bz2 |
First cut at an end-to-end browser test for extension context menu API.
BUG=45811
TEST=(This is adding a test to browser_tests)
Review URL: http://codereview.chromium.org/2841037
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51788 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
7 files changed, 126 insertions, 0 deletions
diff --git a/chrome/browser/extensions/extension_context_menu_browsertest.cc b/chrome/browser/extensions/extension_context_menu_browsertest.cc new file mode 100644 index 0000000..a5c0397 --- /dev/null +++ b/chrome/browser/extensions/extension_context_menu_browsertest.cc @@ -0,0 +1,73 @@ +// Copyright (c) 2010 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. + +#include "app/menus/menu_model.h" +#include "chrome/app/chrome_dll_resource.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/extensions/extension_browsertest.h" +#include "chrome/browser/tab_contents/render_view_context_menu.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/test/ui_test_utils.h" +#include "third_party/WebKit/WebKit/chromium/public/WebContextMenuData.h" +#include "webkit/glue/context_menu.h" + +using menus::MenuModel; +using WebKit::WebContextMenuData; + +// This test class helps us sidestep platform-specific issues with popping up a +// real context menu, while still running through the actual code in +// RenderViewContextMenu where extension items get added and executed. +class TestRenderViewContextMenu : public RenderViewContextMenu { + public: + TestRenderViewContextMenu(TabContents* tab_contents, + const ContextMenuParams& params) + : RenderViewContextMenu(tab_contents, params) {} + + virtual ~TestRenderViewContextMenu() {} + + protected: + virtual bool GetAcceleratorForCommandId(int command_id, + menus::Accelerator* accelerator) { + // None of our commands have accelerators, so always return false. + return false; + } + virtual void PlatformInit() {} +}; + +IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, ContextMenusSimple) { + CommandLine::ForCurrentProcess()->AppendSwitch( + switches::kEnableExperimentalExtensionApis); + FilePath extension_dir = test_data_dir_.AppendASCII("context_menus"); + ASSERT_TRUE(LoadExtension(extension_dir)); + + // The extension's background page will create a context menu item and then + // cause a navigation on success - we wait for that here. + ASSERT_TRUE(ui_test_utils::WaitForNavigationsInCurrentTab(browser(), 1)); + + // Initialize the data we need to create a context menu. + TabContents* tab_contents = browser()->GetSelectedTabContents(); + ContextMenuParams params; + params.media_type = WebContextMenuData::MediaTypeNone; + params.x = 0; + params.y = 0; + params.is_image_blocked = false; + params.frame_url = tab_contents->GetURL(); + params.media_flags = 0; + params.spellcheck_enabled = false; + params.is_editable = false; + + // Create and build our test context menu. + TestRenderViewContextMenu menu(tab_contents, params); + menu.Init(); + + // Look for the extension item in the menu, and execute it. + int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; + ASSERT_TRUE(menu.IsCommandIdEnabled(command_id)); + menu.ExecuteCommand(command_id); + + // The onclick handler for the extension item will cause a navigation - we + // wait for that here. + ASSERT_TRUE(ui_test_utils::WaitForNavigationsInCurrentTab(browser(), 1)); +} diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index cd9101e..c76e00c 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1395,6 +1395,7 @@ 'browser/extensions/extension_browsertests_misc.cc', 'browser/extensions/extension_clipboard_apitest.cc', 'browser/extensions/extension_context_menu_apitest.cc', + 'browser/extensions/extension_context_menu_browsertest.cc', 'browser/extensions/extension_cookies_apitest.cc', 'browser/extensions/extension_cookies_unittest.cc', 'browser/extensions/extension_crash_recovery_browsertest.cc', diff --git a/chrome/test/data/extensions/context_menus/background.html b/chrome/test/data/extensions/context_menus/background.html new file mode 100644 index 0000000..46f4d74 --- /dev/null +++ b/chrome/test/data/extensions/context_menus/background.html @@ -0,0 +1 @@ +<script src="test.js"></script> diff --git a/chrome/test/data/extensions/context_menus/manifest.json b/chrome/test/data/extensions/context_menus/manifest.json new file mode 100644 index 0000000..5efbbfe --- /dev/null +++ b/chrome/test/data/extensions/context_menus/manifest.json @@ -0,0 +1,6 @@ +{ + "name" : "Context Menus Test Extension", + "version" : "0.1", + "permissions": [ "experimental", "tabs" ], + "background_page": "background.html" +} diff --git a/chrome/test/data/extensions/context_menus/test.html b/chrome/test/data/extensions/context_menus/test.html new file mode 100644 index 0000000..8267927 --- /dev/null +++ b/chrome/test/data/extensions/context_menus/test.html @@ -0,0 +1,5 @@ +<html> +<body> +This is test page 1 +</body> +</html> diff --git a/chrome/test/data/extensions/context_menus/test.js b/chrome/test/data/extensions/context_menus/test.js new file mode 100644 index 0000000..c6a1049 --- /dev/null +++ b/chrome/test/data/extensions/context_menus/test.js @@ -0,0 +1,34 @@ +// Copyright (c) 2010 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. + +// Checks that there is only one window and one tab, and calls back |callback| +// with its id (or -1 if there is more than 1 window or more than 1 tab). +function getCurrentSingleTabId(callback) { + chrome.windows.getAll({"populate":true}, function(windows) { + if (windows.length != 1 || windows[0].tabs.length != 1) { + callback(-1); + } else { + callback(windows[0].tabs[0].id); + } + }); +} + +function navigateCurrentTab(url) { + getCurrentSingleTabId(function(tabid) { + chrome.tabs.update(tabid, {"url": url}); + }); +} + +function onclick(info) { + navigateCurrentTab(chrome.extension.getURL("test2.html")); +} + +window.onload = function() { + chrome.experimental.contextMenu.create({"title":"Extension Item 1", + "onclick": onclick}, function(id) { + if (!chrome.extension.lastError) { + navigateCurrentTab(chrome.extension.getURL("test.html")); + } + }); +}; diff --git a/chrome/test/data/extensions/context_menus/test2.html b/chrome/test/data/extensions/context_menus/test2.html new file mode 100644 index 0000000..7a82e48 --- /dev/null +++ b/chrome/test/data/extensions/context_menus/test2.html @@ -0,0 +1,6 @@ +<html> +<head><title>test2</title></head> +<body> +test2 +</body> +</html> |