Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / panels / panel_extension_browsertest.cc
blobc5a88a1149a33017d5881d716a25a8fd9daf1fde
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
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/path_service.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/extension_browsertest.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/panels/native_panel.h"
15 #include "chrome/browser/ui/panels/panel.h"
16 #include "chrome/browser/ui/panels/panel_constants.h"
17 #include "chrome/browser/ui/panels/panel_manager.h"
18 #include "chrome/browser/web_applications/web_app.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/test/test_utils.h"
23 #include "extensions/common/extension.h"
24 #include "extensions/test/extension_test_message_listener.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 #if defined(OS_MACOSX)
28 #include "base/mac/scoped_nsautorelease_pool.h"
29 #endif
31 using extensions::Extension;
33 class PanelExtensionBrowserTest : public ExtensionBrowserTest {
34 protected:
35 void SetUpCommandLine(base::CommandLine* command_line) override {
36 ExtensionBrowserTest::SetUpCommandLine(command_line);
37 command_line->AppendSwitch(switches::kEnablePanels);
38 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
39 test_data_dir_ = test_data_dir_.AppendASCII("panels");
42 Panel* CreatePanelFromExtension(const Extension* extension) const {
43 #if defined(OS_MACOSX)
44 // Opening panels on a Mac causes NSWindowController of the Panel window
45 // to be autoreleased. We need a pool drained after it's done so the test
46 // can close correctly. The NSWindowController of the Panel window controls
47 // lifetime of the Panel object so we want to release it as soon as
48 // possible. In real Chrome, this is done by message pump.
49 // On non-Mac platform, this is an empty class.
50 base::mac::ScopedNSAutoreleasePool autorelease_pool;
51 #endif
53 Panel* panel = PanelManager::GetInstance()->CreatePanel(
54 web_app::GenerateApplicationNameFromExtensionId(extension->id()),
55 browser()->profile(),
56 GURL(),
57 gfx::Rect(),
58 PanelManager::CREATE_AS_DETACHED);
59 panel->ShowInactive();
60 return panel;
63 void WaitForAppIconAvailable(Panel* panel) const {
64 content::WindowedNotificationObserver signal(
65 chrome::NOTIFICATION_PANEL_APP_ICON_LOADED,
66 content::Source<Panel>(panel));
67 if (!panel->app_icon().IsEmpty())
68 return;
69 signal.Wait();
70 EXPECT_FALSE(panel->app_icon().IsEmpty());
73 static NativePanelTesting* CreateNativePanelTesting(Panel* panel) {
74 return panel->native_panel()->CreateNativePanelTesting();
78 // TODO(jschuh): Hanging plugin tests. crbug.com/244653
79 #if !defined(OS_WIN) && !defined(ARCH_CPU_X86_64)
80 IN_PROC_BROWSER_TEST_F(PanelExtensionBrowserTest, PanelAppIcon) {
81 const Extension* extension =
82 LoadExtension(test_data_dir_.AppendASCII("test_extension"));
83 Panel* panel = CreatePanelFromExtension(extension);
85 // Wait for the app icon gets fully loaded.
86 WaitForAppIconAvailable(panel);
88 // First verify on the panel level.
89 gfx::ImageSkia app_icon = panel->app_icon().AsImageSkia();
90 EXPECT_EQ(panel::kPanelAppIconSize, app_icon.width());
91 EXPECT_EQ(panel::kPanelAppIconSize, app_icon.height());
93 // Then verify on the native panel level.
94 #if !defined(OS_WIN) || !defined(USE_AURA)
95 scoped_ptr<NativePanelTesting> native_panel_testing(
96 CreateNativePanelTesting(panel));
97 EXPECT_TRUE(native_panel_testing->VerifyAppIcon());
98 #endif
100 panel->Close();
102 #endif
104 IN_PROC_BROWSER_TEST_F(PanelExtensionBrowserTest,
105 ClosePanelBeforeIconLoadingCompleted) {
106 const Extension* extension =
107 LoadExtension(test_data_dir_.AppendASCII("test_extension"));
108 Panel* panel = CreatePanelFromExtension(extension);
110 // Close tha panel without waiting for the app icon loaded.
111 panel->Close();
114 // Non-abstract RenderViewContextMenu class for testing context menus in Panels.
115 class PanelContextMenu : public RenderViewContextMenu {
116 public:
117 PanelContextMenu(content::RenderFrameHost* render_frame_host,
118 const content::ContextMenuParams& params)
119 : RenderViewContextMenu(render_frame_host, params) {}
121 bool HasCommandWithId(int command_id) {
122 return menu_model_.GetIndexOfCommandId(command_id) != -1;
125 void Show() override {}
127 protected:
128 // RenderViewContextMenu implementation.
129 bool GetAcceleratorForCommandId(int command_id,
130 ui::Accelerator* accelerator) override {
131 return false;
135 IN_PROC_BROWSER_TEST_F(PanelExtensionBrowserTest, BasicContextMenu) {
136 ExtensionTestMessageListener listener("panel loaded", false);
137 LoadExtension(test_data_dir_.AppendASCII("basic"));
138 ASSERT_TRUE(listener.WaitUntilSatisfied());
140 // There should only be one panel.
141 PanelManager* panel_manager = PanelManager::GetInstance();
142 EXPECT_EQ(1, panel_manager->num_panels());
143 Panel* panel = panel_manager->panels().front();
144 content::WebContents* web_contents = panel->GetWebContents();
145 ASSERT_TRUE(web_contents);
147 // Verify basic menu contents. The basic extension does not add any
148 // context menu items so the panel's menu should include only the
149 // developer tools.
151 content::ContextMenuParams params;
152 params.page_url = web_contents->GetURL();
153 // Ensure context menu isn't swallowed by WebContentsDelegate (the panel).
154 EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params));
156 scoped_ptr<PanelContextMenu> menu(
157 new PanelContextMenu(web_contents->GetMainFrame(), params));
158 menu->Init();
160 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
161 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
162 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_PASTE));
163 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
164 EXPECT_FALSE(menu->HasCommandWithId(IDC_BACK));
165 EXPECT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
166 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
169 // Verify expected menu contents for editable item.
171 content::ContextMenuParams params;
172 params.is_editable = true;
173 params.page_url = web_contents->GetURL();
174 // Ensure context menu isn't swallowed by WebContentsDelegate (the panel).
175 EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params));
177 scoped_ptr<PanelContextMenu> menu(
178 new PanelContextMenu(web_contents->GetMainFrame(), params));
179 menu->Init();
181 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
182 EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
183 EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_PASTE));
184 EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
185 EXPECT_FALSE(menu->HasCommandWithId(IDC_BACK));
186 EXPECT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
187 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
190 // Verify expected menu contents for text selection.
192 content::ContextMenuParams params;
193 params.page_url = web_contents->GetURL();
194 params.selection_text = base::ASCIIToUTF16("Select me");
195 // Ensure context menu isn't swallowed by WebContentsDelegate (the panel).
196 EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params));
198 scoped_ptr<PanelContextMenu> menu(
199 new PanelContextMenu(web_contents->GetMainFrame(), params));
200 menu->Init();
202 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
203 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
204 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_PASTE));
205 EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
206 EXPECT_FALSE(menu->HasCommandWithId(IDC_BACK));
207 EXPECT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
208 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
211 // Verify expected menu contexts for a link.
213 content::ContextMenuParams params;
214 params.page_url = web_contents->GetURL();
215 params.unfiltered_link_url = GURL("http://google.com/");
216 // Ensure context menu isn't swallowed by WebContentsDelegate (the panel).
217 EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params));
219 scoped_ptr<PanelContextMenu> menu(
220 new PanelContextMenu(web_contents->GetMainFrame(), params));
221 menu->Init();
223 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
224 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
225 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_PASTE));
226 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
227 EXPECT_FALSE(menu->HasCommandWithId(IDC_BACK));
228 EXPECT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
229 EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
233 IN_PROC_BROWSER_TEST_F(PanelExtensionBrowserTest, CustomContextMenu) {
234 ExtensionTestMessageListener listener("created item", false);
235 LoadExtension(test_data_dir_.AppendASCII("context_menu"));
236 ASSERT_TRUE(listener.WaitUntilSatisfied());
238 // Load a second extension that also creates a custom context menu item.
239 ExtensionTestMessageListener bogey_listener("created bogey item", false);
240 LoadExtension(test_data_dir_.AppendASCII("context_menu2"));
241 ASSERT_TRUE(bogey_listener.WaitUntilSatisfied());
243 // There should only be one panel.
244 PanelManager* panel_manager = PanelManager::GetInstance();
245 EXPECT_EQ(1, panel_manager->num_panels());
246 Panel* panel = panel_manager->panels().front();
247 content::WebContents* web_contents = panel->GetWebContents();
248 ASSERT_TRUE(web_contents);
250 content::ContextMenuParams params;
251 params.page_url = web_contents->GetURL();
253 // Ensure context menu isn't swallowed by WebContentsDelegate (the panel).
254 EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params));
256 // Verify menu contents contains the custom item added by their own extension.
257 scoped_ptr<PanelContextMenu> menu;
258 menu.reset(new PanelContextMenu(web_contents->GetMainFrame(), params));
259 menu->Init();
260 EXPECT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
261 EXPECT_FALSE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + 1));
262 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
263 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
264 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_PASTE));
265 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
266 EXPECT_FALSE(menu->HasCommandWithId(IDC_BACK));
267 EXPECT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
268 EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
270 // Execute the extension's custom menu item and wait for the extension's
271 // script to tell us its onclick fired.
272 ExtensionTestMessageListener onclick_listener("clicked", false);
273 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST;
274 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id));
275 menu->ExecuteCommand(command_id, 0);
276 EXPECT_TRUE(onclick_listener.WaitUntilSatisfied());