blob: 0e5dddbcaab24068af7895534ca63185b800e07d [file] [log] [blame]
[email protected]a44657202012-01-09 05:48:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]cfc6cca2011-12-01 02:30:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]ad687a72012-06-08 06:03:115#include "chrome/app/chrome_command_ids.h"
[email protected]605fb8102012-05-04 01:36:556#include "chrome/browser/automation/automation_util.h"
[email protected]375003a2011-12-13 02:53:217#include "chrome/browser/tab_contents/render_view_context_menu.h"
[email protected]259771102012-05-31 16:52:208#include "chrome/browser/extensions/extension_test_message_listener.h"
9#include "chrome/browser/extensions/platform_app_browsertest_util.h"
10#include "chrome/browser/extensions/shell_window_registry.h"
[email protected]cfc6cca2011-12-01 02:30:0611#include "chrome/browser/ui/browser.h"
[email protected]52877dbc62012-06-29 22:22:0312#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]a5a0be02012-07-18 05:51:5413#include "chrome/browser/ui/extensions/application_launch.h"
[email protected]d72d3a62012-05-10 03:45:0814#include "chrome/browser/ui/extensions/shell_window.h"
[email protected]5b1a04b42012-06-15 00:41:4415#include "chrome/common/chrome_notification_types.h"
[email protected]cfc6cca2011-12-01 02:30:0616#include "chrome/test/base/ui_test_utils.h"
[email protected]fb29e6cf2012-07-12 21:27:2017#include "content/public/browser/render_process_host.h"
[email protected]cfc6cca2011-12-01 02:30:0618
[email protected]bb81f382012-01-03 22:45:4419using content::WebContents;
[email protected]d9ede582012-08-14 19:21:3820
21namespace extensions {
[email protected]31bdbfef2012-05-22 19:59:1522
[email protected]375003a2011-12-13 02:53:2123namespace {
24// Non-abstract RenderViewContextMenu class.
25class PlatformAppContextMenu : public RenderViewContextMenu {
26 public:
[email protected]bb81f382012-01-03 22:45:4427 PlatformAppContextMenu(WebContents* web_contents,
[email protected]35be7ec2012-02-12 20:42:5128 const content::ContextMenuParams& params)
[email protected]bb81f382012-01-03 22:45:4429 : RenderViewContextMenu(web_contents, params) {}
[email protected]375003a2011-12-13 02:53:2130
[email protected]ad687a72012-06-08 06:03:1131 bool HasCommandWithId(int command_id) {
32 return menu_model_.GetIndexOfCommandId(command_id) != -1;
33 }
34
[email protected]375003a2011-12-13 02:53:2135 protected:
36 // These two functions implement pure virtual methods of
37 // RenderViewContextMenu.
38 virtual bool GetAcceleratorForCommandId(int command_id,
39 ui::Accelerator* accelerator) {
40 return false;
41 }
42 virtual void PlatformInit() {}
[email protected]f5d5cd02012-05-04 13:59:3043 virtual void PlatformCancel() {}
[email protected]375003a2011-12-13 02:53:2144};
45
46} // namespace
47
[email protected]eb4bcac2012-06-27 01:32:0848// Tests that CreateShellWindow doesn't crash if you close it straight away.
49// LauncherPlatformAppBrowserTest relies on this behaviour, but is only run for
50// ash, so we test that it works here.
51IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, CreateAndCloseShellWindow) {
52 const Extension* extension = LoadAndLaunchPlatformApp("minimal");
53 ShellWindow* window = CreateShellWindow(extension);
54 CloseShellWindow(window);
55}
56
[email protected]dc37b002012-04-23 23:02:2657// Tests that platform apps received the "launch" event when launched.
58IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
59 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
[email protected]cfc6cca2011-12-01 02:30:0660}
[email protected]375003a2011-12-13 02:53:2161
[email protected]567230b2012-08-21 21:11:4462// Tests that platform apps cannot use certain disabled window properties, but
63// can override them and then use them.
64IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisabledWindowProperties) {
65 ASSERT_TRUE(RunPlatformAppTest("platform_apps/disabled_window_properties"))
66 << message_;
67}
68
[email protected]edd7ed692012-02-08 02:50:0369IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
[email protected]dc37b002012-04-23 23:02:2670 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]31bdbfef2012-05-22 19:59:1571 LoadAndLaunchPlatformApp("minimal");
[email protected]dc37b002012-04-23 23:02:2672
73 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:2174
[email protected]375003a2011-12-13 02:53:2175 // The empty app doesn't add any context menu items, so its menu should
[email protected]0cfacd82012-02-09 01:55:3376 // only include the developer tools.
[email protected]d72d3a62012-05-10 03:45:0877 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:3178 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:2179 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:5180 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:4481 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:2182 params);
83 menu->Init();
[email protected]ad687a72012-06-08 06:03:1184 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
85 ASSERT_TRUE(menu->HasCommandWithId(IDC_RELOAD));
86 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
87 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]375003a2011-12-13 02:53:2188}
89
[email protected]edd7ed692012-02-08 02:50:0390IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) {
[email protected]dc37b002012-04-23 23:02:2691 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]375003a2011-12-13 02:53:2192 LoadAndLaunchPlatformApp("context_menu");
93
[email protected]dc37b002012-04-23 23:02:2694 // Wait for the extension to tell us it's initialized its context menus and
95 // launched a window.
96 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:2197
[email protected]0cfacd82012-02-09 01:55:3398 // The context_menu app has one context menu item. This, along with a
99 // separator and the developer tools, is all that should be in the menu.
[email protected]d72d3a62012-05-10 03:45:08100 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:31101 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:21102 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:51103 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:44104 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:21105 params);
106 menu->Init();
[email protected]ad687a72012-06-08 06:03:11107 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
108 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
109 ASSERT_TRUE(menu->HasCommandWithId(IDC_RELOAD));
110 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
111 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]375003a2011-12-13 02:53:21112}
[email protected]5f000f272012-01-19 05:25:08113
[email protected]dc83c582012-08-17 02:18:14114IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuClicked) {
115 ExtensionTestMessageListener launched_listener("Launched", false);
116 LoadAndLaunchPlatformApp("context_menu_click");
117
118 // Wait for the extension to tell us it's initialized its context menus and
119 // launched a window.
120 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
121
122 // Test that the menu item shows up
123 WebContents* web_contents = GetFirstShellWindowWebContents();
124 ASSERT_TRUE(web_contents);
125 WebKit::WebContextMenuData data;
126 content::ContextMenuParams params(data);
127 params.page_url = GURL("http://foo.bar");
128 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
129 params);
130 menu->Init();
131 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
132
133 // Execute the menu item
134 ExtensionTestMessageListener onclicked_listener("onClicked fired for id1",
135 false);
136 menu->ExecuteCommand(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST);
137
138 ASSERT_TRUE(onclicked_listener.WaitUntilSatisfied());
139}
140
[email protected]edd7ed692012-02-08 02:50:03141IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) {
[email protected]dc37b002012-04-23 23:02:26142 ASSERT_TRUE(StartTestServer());
143 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_;
[email protected]5f000f272012-01-19 05:25:08144}
[email protected]863e6472012-01-24 19:33:58145
[email protected]fd3238af2012-05-22 18:55:30146IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Iframes) {
147 ASSERT_TRUE(StartTestServer());
148 ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_;
149}
150
[email protected]2aac7ff2012-01-25 18:05:11151// Tests that localStorage and WebSQL are disabled for platform apps.
[email protected]edd7ed692012-02-08 02:50:03152IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) {
[email protected]2aac7ff2012-01-25 18:05:11153 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
154}
[email protected]6a5a2e52012-03-22 03:21:12155
[email protected]c0cecd1f2012-04-05 16:50:12156IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) {
157 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_;
158}
159
[email protected]f0233ff2012-07-20 20:14:50160// Tests that platform apps can use the chrome.app.window.* API.
[email protected]dc37b002012-04-23 23:02:26161IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) {
[email protected]6a5a2e52012-03-22 03:21:12162 ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_;
163}
[email protected]605fb8102012-05-04 01:36:55164
165// Tests that platform apps have isolated storage by default.
166IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Isolation) {
167 ASSERT_TRUE(StartTestServer());
168
169 // Load a (non-app) page under the "localhost" origin that sets a cookie.
170 GURL set_cookie_url = test_server()->GetURL(
171 "files/extensions/platform_apps/isolation/set_cookie.html");
172 GURL::Replacements replace_host;
173 std::string host_str("localhost"); // Must stay in scope with replace_host.
174 replace_host.SetHostStr(host_str);
175 set_cookie_url = set_cookie_url.ReplaceComponents(replace_host);
176
177 ui_test_utils::NavigateToURLWithDisposition(
178 browser(), set_cookie_url,
179 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
180
181 // Make sure the cookie is set.
182 int cookie_size;
183 std::string cookie_value;
184 automation_util::GetCookies(
185 set_cookie_url,
[email protected]52877dbc62012-06-29 22:22:03186 chrome::GetWebContentsAt(browser(), 0),
[email protected]605fb8102012-05-04 01:36:55187 &cookie_size,
188 &cookie_value);
189 ASSERT_EQ("testCookie=1", cookie_value);
190
191 // Let the platform app request the same URL, and make sure that it doesn't
192 // see the cookie.
193 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_;
194}
[email protected]31bdbfef2012-05-22 19:59:15195
196IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ExtensionWindowingApis) {
197 // Initially there should be just the one browser window visible to the
198 // extensions API.
199 const Extension* extension = LoadExtension(
200 test_data_dir_.AppendASCII("common/background_page"));
201 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
202
203 // And no shell windows.
204 ASSERT_EQ(0U, GetShellWindowCount());
205
206 // Launch a platform app that shows a window.
207 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]ad5bb8a92012-06-07 03:55:58208 LoadAndLaunchPlatformApp("minimal");
[email protected]31bdbfef2012-05-22 19:59:15209 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
210 ASSERT_EQ(1U, GetShellWindowCount());
211 ShellWindowRegistry::ShellWindowSet shell_windows =
212 ShellWindowRegistry::Get(browser()->profile())->shell_windows();
213 int shell_window_id = (*shell_windows.begin())->session_id().id();
214
215 // But it's not visible to the extensions API, it still thinks there's just
216 // one browser window.
217 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
218 // It can't look it up by ID either
219 ASSERT_FALSE(RunGetWindowFunctionForExtension(shell_window_id, extension));
220
221 // The app can also only see one window (its own).
[email protected]ad5bb8a92012-06-07 03:55:58222 // TODO(jeremya): add an extension function to get a shell window by ID, and
223 // to get a list of all the shell windows, so we can test this.
[email protected]31bdbfef2012-05-22 19:59:15224
225 // Launch another platform app that also shows a window.
226 ExtensionTestMessageListener launched_listener2("Launched", false);
[email protected]ad5bb8a92012-06-07 03:55:58227 LoadAndLaunchPlatformApp("context_menu");
[email protected]31bdbfef2012-05-22 19:59:15228 ASSERT_TRUE(launched_listener2.WaitUntilSatisfied());
229
230 // There are two total shell windows, but each app can only see its own.
231 ASSERT_EQ(2U, GetShellWindowCount());
[email protected]ad5bb8a92012-06-07 03:55:58232 // TODO(jeremya): as above, this requires more extension functions.
[email protected]31bdbfef2012-05-22 19:59:15233}
[email protected]12e540452012-05-26 07:09:36234
235// TODO(benwells): fix these tests for ChromeOS.
236#if !defined(OS_CHROMEOS)
237// Tests that command line parameters get passed through to platform apps
238// via launchData correctly when launching with a file.
239IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFile) {
240 SetCommandLineArg( "platform_apps/launch_files/test.txt");
241 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file"))
242 << message_;
243}
244
[email protected]a5a0be02012-07-18 05:51:54245// Tests that relative paths can be passed through to the platform app.
246// This test doesn't use the normal test infrastructure as it needs to open
247// the application differently to all other platform app tests, by setting
248// the application_launch::LaunchParams.current_directory field.
249IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithRelativeFile) {
250 // Setup the command line
251 ClearCommandLineArgs();
252 CommandLine* command_line = CommandLine::ForCurrentProcess();
253 FilePath relative_test_doc = FilePath::FromUTF8Unsafe(
254 "platform_apps/launch_files/test.txt");
255 relative_test_doc = relative_test_doc.NormalizePathSeparators();
256 command_line->AppendArgPath(relative_test_doc);
257
258 // Load the extension
259 ResultCatcher catcher;
[email protected]d9ede582012-08-14 19:21:38260 const Extension* extension = LoadExtension(
[email protected]a5a0be02012-07-18 05:51:54261 test_data_dir_.AppendASCII("platform_apps/launch_file"));
262 ASSERT_TRUE(extension);
263
264 // Run the test
265 application_launch::LaunchParams params(browser()->profile(), extension,
266 extension_misc::LAUNCH_NONE,
267 NEW_WINDOW);
268 params.command_line = CommandLine::ForCurrentProcess();
269 params.current_directory = test_data_dir_;
270 application_launch::OpenApplication(params);
271
272 if (!catcher.GetNextResult()) {
273 message_ = catcher.message();
274 ASSERT_TRUE(0);
275 }
276}
277
[email protected]12e540452012-05-26 07:09:36278// Tests that no launch data is sent through if the platform app provides
279// an intent with the wrong action.
280IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongIntent) {
281 SetCommandLineArg("platform_apps/launch_files/test.txt");
282 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_intent"))
283 << message_;
284}
285
286// Tests that no launch data is sent through if the file is of the wrong MIME
287// type.
288IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongType) {
289 SetCommandLineArg("platform_apps/launch_files/test.txt");
290 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_type"))
291 << message_;
292}
293
294// Tests that no launch data is sent through if the platform app does not
295// provide an intent.
296IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNoIntent) {
297 SetCommandLineArg("platform_apps/launch_files/test.txt");
298 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_no_intent"))
299 << message_;
300}
301
302// Tests that no launch data is sent through if the file MIME type cannot
303// be read.
304IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoType) {
305 SetCommandLineArg("platform_apps/launch_files/test.unknownextension");
306 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
307 << message_;
308}
309
310// Tests that no launch data is sent through if the file does not exist.
311IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoFile) {
312 SetCommandLineArg("platform_apps/launch_files/doesnotexist.txt");
313 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
314 << message_;
315}
316
317// Tests that no launch data is sent through if the argument is a directory.
318IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithDirectory) {
319 SetCommandLineArg("platform_apps/launch_files");
320 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
321 << message_;
322}
323
324// Tests that no launch data is sent through if there are no arguments passed
325// on the command line
326IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNothing) {
327 ClearCommandLineArgs();
328 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_nothing"))
329 << message_;
330}
[email protected]ffc7b4d2012-06-08 00:05:32331
332// Test that platform apps can use the chrome.fileSystem.getDisplayPath
333// function to get the native file system path of a file they are launched with.
334IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, GetDisplayPath) {
335 SetCommandLineArg("platform_apps/launch_files/test.txt");
336 ASSERT_TRUE(RunPlatformAppTest("platform_apps/get_display_path"))
337 << message_;
338}
339
[email protected]12e540452012-05-26 07:09:36340#endif // defined(OS_CHROMEOS)
[email protected]5b1a04b42012-06-15 00:41:44341
342IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OpenLink) {
343 ASSERT_TRUE(StartTestServer());
[email protected]a7fe9112012-07-20 02:34:45344 content::WindowedNotificationObserver observer(
[email protected]5b1a04b42012-06-15 00:41:44345 chrome::NOTIFICATION_TAB_ADDED,
346 content::Source<content::WebContentsDelegate>(browser()));
347 LoadAndLaunchPlatformApp("open_link");
348 observer.Wait();
349 ASSERT_EQ(2, browser()->tab_count());
350}
[email protected]d9ede582012-08-14 19:21:38351
352} // namespace extensions