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