1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
// 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.
#include "base/command_line.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/renderer_context_menu/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"
#include "chrome/browser/ui/panels/panel_constants.h"
#include "chrome/browser/ui/panels/panel_manager.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_utils.h"
#include "extensions/common/extension.h"
#include "extensions/test/extension_test_message_listener.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_MACOSX)
#include "base/mac/scoped_nsautorelease_pool.h"
#endif
using extensions::Extension;
class PanelExtensionBrowserTest : public ExtensionBrowserTest {
protected:
void SetUpCommandLine(base::CommandLine* command_line) override {
ExtensionBrowserTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kEnablePanels);
PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
test_data_dir_ = test_data_dir_.AppendASCII("panels");
}
Panel* CreatePanelFromExtension(const Extension* extension) const {
#if defined(OS_MACOSX)
// Opening panels on a Mac causes NSWindowController of the Panel window
// to be autoreleased. We need a pool drained after it's done so the test
// can close correctly. The NSWindowController of the Panel window controls
// lifetime of the Panel object so we want to release it as soon as
// possible. In real Chrome, this is done by message pump.
// On non-Mac platform, this is an empty class.
base::mac::ScopedNSAutoreleasePool autorelease_pool;
#endif
Panel* panel = PanelManager::GetInstance()->CreatePanel(
web_app::GenerateApplicationNameFromExtensionId(extension->id()),
browser()->profile(),
GURL(),
gfx::Rect(),
PanelManager::CREATE_AS_DETACHED);
panel->ShowInactive();
return panel;
}
void WaitForAppIconAvailable(Panel* panel) const {
content::WindowedNotificationObserver signal(
chrome::NOTIFICATION_PANEL_APP_ICON_LOADED,
content::Source<Panel>(panel));
if (!panel->app_icon().IsEmpty())
return;
signal.Wait();
EXPECT_FALSE(panel->app_icon().IsEmpty());
}
static NativePanelTesting* CreateNativePanelTesting(Panel* panel) {
return panel->native_panel()->CreateNativePanelTesting();
}
};
// TODO(jschuh): Hanging plugin tests. crbug.com/244653
#if !defined(OS_WIN) && !defined(ARCH_CPU_X86_64)
IN_PROC_BROWSER_TEST_F(PanelExtensionBrowserTest, PanelAppIcon) {
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("test_extension"));
Panel* panel = CreatePanelFromExtension(extension);
// Wait for the app icon gets fully loaded.
WaitForAppIconAvailable(panel);
// First verify on the panel level.
gfx::ImageSkia app_icon = panel->app_icon().AsImageSkia();
EXPECT_EQ(panel::kPanelAppIconSize, app_icon.width());
EXPECT_EQ(panel::kPanelAppIconSize, app_icon.height());
// Then verify on the native panel level.
#if !defined(OS_WIN) || !defined(USE_AURA)
scoped_ptr<NativePanelTesting> native_panel_testing(
CreateNativePanelTesting(panel));
EXPECT_TRUE(native_panel_testing->VerifyAppIcon());
#endif
panel->Close();
}
#endif
IN_PROC_BROWSER_TEST_F(PanelExtensionBrowserTest,
ClosePanelBeforeIconLoadingCompleted) {
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("test_extension"));
Panel* panel = CreatePanelFromExtension(extension);
// Close tha panel without waiting for the app icon loaded.
panel->Close();
}
// Non-abstract RenderViewContextMenu class for testing context menus in Panels.
class PanelContextMenu : public RenderViewContextMenu {
public:
PanelContextMenu(content::RenderFrameHost* render_frame_host,
const content::ContextMenuParams& params)
: RenderViewContextMenu(render_frame_host, params) {}
bool HasCommandWithId(int command_id) {
return menu_model_.GetIndexOfCommandId(command_id) != -1;
}
void Show() override {}
protected:
// RenderViewContextMenu implementation.
bool GetAcceleratorForCommandId(int command_id,
ui::Accelerator* accelerator) override {
return false;
}
};
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);
// 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.
{
content::ContextMenuParams params;
params.page_url = web_contents->GetURL();
// Ensure context menu isn't swallowed by WebContentsDelegate (the panel).
EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params));
scoped_ptr<PanelContextMenu> menu(
new PanelContextMenu(web_contents->GetMainFrame(), 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));
EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
}
// Verify expected menu contents for editable item.
{
content::ContextMenuParams params;
params.is_editable = true;
params.page_url = web_contents->GetURL();
// Ensure context menu isn't swallowed by WebContentsDelegate (the panel).
EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params));
scoped_ptr<PanelContextMenu> menu(
new PanelContextMenu(web_contents->GetMainFrame(), 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));
EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
}
// Verify expected menu contents for text selection.
{
content::ContextMenuParams params;
params.page_url = web_contents->GetURL();
params.selection_text = base::ASCIIToUTF16("Select me");
// Ensure context menu isn't swallowed by WebContentsDelegate (the panel).
EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params));
scoped_ptr<PanelContextMenu> menu(
new PanelContextMenu(web_contents->GetMainFrame(), 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));
EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
}
// Verify expected menu contexts for a link.
{
content::ContextMenuParams params;
params.page_url = web_contents->GetURL();
params.unfiltered_link_url = GURL("http://google.com/");
// Ensure context menu isn't swallowed by WebContentsDelegate (the panel).
EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params));
scoped_ptr<PanelContextMenu> menu(
new PanelContextMenu(web_contents->GetMainFrame(), 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));
EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
}
}
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);
content::ContextMenuParams params;
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->GetMainFrame(), 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));
EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
// 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, 0);
EXPECT_TRUE(onclick_listener.WaitUntilSatisfied());
}
|