blob: 6ce75dd541edf28b0f9be38e16c064d7771b2c25 [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]31bdbfef2012-05-22 19:59:1520using extensions::Extension;
21
[email protected]375003a2011-12-13 02:53:2122namespace {
23// Non-abstract RenderViewContextMenu class.
24class PlatformAppContextMenu : public RenderViewContextMenu {
25 public:
[email protected]bb81f382012-01-03 22:45:4426 PlatformAppContextMenu(WebContents* web_contents,
[email protected]35be7ec2012-02-12 20:42:5127 const content::ContextMenuParams& params)
[email protected]bb81f382012-01-03 22:45:4428 : RenderViewContextMenu(web_contents, params) {}
[email protected]375003a2011-12-13 02:53:2129
[email protected]ad687a72012-06-08 06:03:1130 bool HasCommandWithId(int command_id) {
31 return menu_model_.GetIndexOfCommandId(command_id) != -1;
32 }
33
[email protected]375003a2011-12-13 02:53:2134 protected:
35 // These two functions implement pure virtual methods of
36 // RenderViewContextMenu.
37 virtual bool GetAcceleratorForCommandId(int command_id,
38 ui::Accelerator* accelerator) {
39 return false;
40 }
41 virtual void PlatformInit() {}
[email protected]f5d5cd02012-05-04 13:59:3042 virtual void PlatformCancel() {}
[email protected]375003a2011-12-13 02:53:2143};
44
45} // namespace
46
[email protected]eb4bcac2012-06-27 01:32:0847// Tests that CreateShellWindow doesn't crash if you close it straight away.
48// LauncherPlatformAppBrowserTest relies on this behaviour, but is only run for
49// ash, so we test that it works here.
50IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, CreateAndCloseShellWindow) {
51 const Extension* extension = LoadAndLaunchPlatformApp("minimal");
52 ShellWindow* window = CreateShellWindow(extension);
53 CloseShellWindow(window);
54}
55
[email protected]dc37b002012-04-23 23:02:2656// Tests that platform apps received the "launch" event when launched.
57IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
58 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
[email protected]cfc6cca2011-12-01 02:30:0659}
[email protected]375003a2011-12-13 02:53:2160
[email protected]edd7ed692012-02-08 02:50:0361IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
[email protected]dc37b002012-04-23 23:02:2662 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]31bdbfef2012-05-22 19:59:1563 LoadAndLaunchPlatformApp("minimal");
[email protected]dc37b002012-04-23 23:02:2664
65 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:2166
[email protected]375003a2011-12-13 02:53:2167 // The empty app doesn't add any context menu items, so its menu should
[email protected]0cfacd82012-02-09 01:55:3368 // only include the developer tools.
[email protected]d72d3a62012-05-10 03:45:0869 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:3170 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:2171 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:5172 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:4473 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:2174 params);
75 menu->Init();
[email protected]ad687a72012-06-08 06:03:1176 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
77 ASSERT_TRUE(menu->HasCommandWithId(IDC_RELOAD));
78 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
79 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]375003a2011-12-13 02:53:2180}
81
[email protected]edd7ed692012-02-08 02:50:0382IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) {
[email protected]dc37b002012-04-23 23:02:2683 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]375003a2011-12-13 02:53:2184 LoadAndLaunchPlatformApp("context_menu");
85
[email protected]dc37b002012-04-23 23:02:2686 // Wait for the extension to tell us it's initialized its context menus and
87 // launched a window.
88 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:2189
[email protected]0cfacd82012-02-09 01:55:3390 // The context_menu app has one context menu item. This, along with a
91 // separator and the developer tools, is all that should be in the menu.
[email protected]d72d3a62012-05-10 03:45:0892 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:3193 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:2194 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:5195 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:4496 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:2197 params);
98 menu->Init();
[email protected]ad687a72012-06-08 06:03:1199 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
100 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
101 ASSERT_TRUE(menu->HasCommandWithId(IDC_RELOAD));
102 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
103 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]375003a2011-12-13 02:53:21104}
[email protected]5f000f272012-01-19 05:25:08105
[email protected]edd7ed692012-02-08 02:50:03106IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) {
[email protected]dc37b002012-04-23 23:02:26107 ASSERT_TRUE(StartTestServer());
108 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_;
[email protected]5f000f272012-01-19 05:25:08109}
[email protected]863e6472012-01-24 19:33:58110
[email protected]fd3238af2012-05-22 18:55:30111IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Iframes) {
112 ASSERT_TRUE(StartTestServer());
113 ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_;
114}
115
[email protected]2aac7ff2012-01-25 18:05:11116// Tests that localStorage and WebSQL are disabled for platform apps.
[email protected]edd7ed692012-02-08 02:50:03117IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) {
[email protected]2aac7ff2012-01-25 18:05:11118 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
119}
[email protected]6a5a2e52012-03-22 03:21:12120
[email protected]c0cecd1f2012-04-05 16:50:12121IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) {
122 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_;
123}
124
[email protected]eb4bcac2012-06-27 01:32:08125// Tests that platform apps can use the chrome.appWindow.* API.
[email protected]dc37b002012-04-23 23:02:26126IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) {
[email protected]6a5a2e52012-03-22 03:21:12127 ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_;
128}
[email protected]605fb8102012-05-04 01:36:55129
130// Tests that platform apps have isolated storage by default.
131IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Isolation) {
132 ASSERT_TRUE(StartTestServer());
133
134 // Load a (non-app) page under the "localhost" origin that sets a cookie.
135 GURL set_cookie_url = test_server()->GetURL(
136 "files/extensions/platform_apps/isolation/set_cookie.html");
137 GURL::Replacements replace_host;
138 std::string host_str("localhost"); // Must stay in scope with replace_host.
139 replace_host.SetHostStr(host_str);
140 set_cookie_url = set_cookie_url.ReplaceComponents(replace_host);
141
142 ui_test_utils::NavigateToURLWithDisposition(
143 browser(), set_cookie_url,
144 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
145
146 // Make sure the cookie is set.
147 int cookie_size;
148 std::string cookie_value;
149 automation_util::GetCookies(
150 set_cookie_url,
[email protected]52877dbc62012-06-29 22:22:03151 chrome::GetWebContentsAt(browser(), 0),
[email protected]605fb8102012-05-04 01:36:55152 &cookie_size,
153 &cookie_value);
154 ASSERT_EQ("testCookie=1", cookie_value);
155
156 // Let the platform app request the same URL, and make sure that it doesn't
157 // see the cookie.
158 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_;
159}
[email protected]31bdbfef2012-05-22 19:59:15160
161IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ExtensionWindowingApis) {
162 // Initially there should be just the one browser window visible to the
163 // extensions API.
164 const Extension* extension = LoadExtension(
165 test_data_dir_.AppendASCII("common/background_page"));
166 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
167
168 // And no shell windows.
169 ASSERT_EQ(0U, GetShellWindowCount());
170
171 // Launch a platform app that shows a window.
172 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]ad5bb8a92012-06-07 03:55:58173 LoadAndLaunchPlatformApp("minimal");
[email protected]31bdbfef2012-05-22 19:59:15174 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
175 ASSERT_EQ(1U, GetShellWindowCount());
176 ShellWindowRegistry::ShellWindowSet shell_windows =
177 ShellWindowRegistry::Get(browser()->profile())->shell_windows();
178 int shell_window_id = (*shell_windows.begin())->session_id().id();
179
180 // But it's not visible to the extensions API, it still thinks there's just
181 // one browser window.
182 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
183 // It can't look it up by ID either
184 ASSERT_FALSE(RunGetWindowFunctionForExtension(shell_window_id, extension));
185
186 // The app can also only see one window (its own).
[email protected]ad5bb8a92012-06-07 03:55:58187 // TODO(jeremya): add an extension function to get a shell window by ID, and
188 // to get a list of all the shell windows, so we can test this.
[email protected]31bdbfef2012-05-22 19:59:15189
190 // Launch another platform app that also shows a window.
191 ExtensionTestMessageListener launched_listener2("Launched", false);
[email protected]ad5bb8a92012-06-07 03:55:58192 LoadAndLaunchPlatformApp("context_menu");
[email protected]31bdbfef2012-05-22 19:59:15193 ASSERT_TRUE(launched_listener2.WaitUntilSatisfied());
194
195 // There are two total shell windows, but each app can only see its own.
196 ASSERT_EQ(2U, GetShellWindowCount());
[email protected]ad5bb8a92012-06-07 03:55:58197 // TODO(jeremya): as above, this requires more extension functions.
[email protected]31bdbfef2012-05-22 19:59:15198}
[email protected]12e540452012-05-26 07:09:36199
200// TODO(benwells): fix these tests for ChromeOS.
201#if !defined(OS_CHROMEOS)
202// Tests that command line parameters get passed through to platform apps
203// via launchData correctly when launching with a file.
204IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFile) {
205 SetCommandLineArg( "platform_apps/launch_files/test.txt");
206 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file"))
207 << message_;
208}
209
[email protected]a5a0be02012-07-18 05:51:54210// Tests that relative paths can be passed through to the platform app.
211// This test doesn't use the normal test infrastructure as it needs to open
212// the application differently to all other platform app tests, by setting
213// the application_launch::LaunchParams.current_directory field.
214IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithRelativeFile) {
215 // Setup the command line
216 ClearCommandLineArgs();
217 CommandLine* command_line = CommandLine::ForCurrentProcess();
218 FilePath relative_test_doc = FilePath::FromUTF8Unsafe(
219 "platform_apps/launch_files/test.txt");
220 relative_test_doc = relative_test_doc.NormalizePathSeparators();
221 command_line->AppendArgPath(relative_test_doc);
222
223 // Load the extension
224 ResultCatcher catcher;
225 const extensions::Extension* extension = LoadExtension(
226 test_data_dir_.AppendASCII("platform_apps/launch_file"));
227 ASSERT_TRUE(extension);
228
229 // Run the test
230 application_launch::LaunchParams params(browser()->profile(), extension,
231 extension_misc::LAUNCH_NONE,
232 NEW_WINDOW);
233 params.command_line = CommandLine::ForCurrentProcess();
234 params.current_directory = test_data_dir_;
235 application_launch::OpenApplication(params);
236
237 if (!catcher.GetNextResult()) {
238 message_ = catcher.message();
239 ASSERT_TRUE(0);
240 }
241}
242
[email protected]12e540452012-05-26 07:09:36243// Tests that no launch data is sent through if the platform app provides
244// an intent with the wrong action.
245IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongIntent) {
246 SetCommandLineArg("platform_apps/launch_files/test.txt");
247 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_intent"))
248 << message_;
249}
250
251// Tests that no launch data is sent through if the file is of the wrong MIME
252// type.
253IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongType) {
254 SetCommandLineArg("platform_apps/launch_files/test.txt");
255 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_type"))
256 << message_;
257}
258
259// Tests that no launch data is sent through if the platform app does not
260// provide an intent.
261IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNoIntent) {
262 SetCommandLineArg("platform_apps/launch_files/test.txt");
263 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_no_intent"))
264 << message_;
265}
266
267// Tests that no launch data is sent through if the file MIME type cannot
268// be read.
269IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoType) {
270 SetCommandLineArg("platform_apps/launch_files/test.unknownextension");
271 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
272 << message_;
273}
274
275// Tests that no launch data is sent through if the file does not exist.
276IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoFile) {
277 SetCommandLineArg("platform_apps/launch_files/doesnotexist.txt");
278 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
279 << message_;
280}
281
282// Tests that no launch data is sent through if the argument is a directory.
283IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithDirectory) {
284 SetCommandLineArg("platform_apps/launch_files");
285 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
286 << message_;
287}
288
289// Tests that no launch data is sent through if there are no arguments passed
290// on the command line
291IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNothing) {
292 ClearCommandLineArgs();
293 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_nothing"))
294 << message_;
295}
[email protected]ffc7b4d2012-06-08 00:05:32296
297// Test that platform apps can use the chrome.fileSystem.getDisplayPath
298// function to get the native file system path of a file they are launched with.
299IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, GetDisplayPath) {
300 SetCommandLineArg("platform_apps/launch_files/test.txt");
301 ASSERT_TRUE(RunPlatformAppTest("platform_apps/get_display_path"))
302 << message_;
303}
304
[email protected]12e540452012-05-26 07:09:36305#endif // defined(OS_CHROMEOS)
[email protected]5b1a04b42012-06-15 00:41:44306
307IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OpenLink) {
308 ASSERT_TRUE(StartTestServer());
309 ui_test_utils::WindowedNotificationObserver observer(
310 chrome::NOTIFICATION_TAB_ADDED,
311 content::Source<content::WebContentsDelegate>(browser()));
312 LoadAndLaunchPlatformApp("open_link");
313 observer.Wait();
314 ASSERT_EQ(2, browser()->tab_count());
315}
[email protected]8df666b2012-07-03 00:43:33316
317IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, BrowserTag) {
318 ASSERT_TRUE(RunPlatformAppTest("platform_apps/browser_tag")) << message_;
319}
[email protected]fb29e6cf2012-07-12 21:27:20320
[email protected]435108842012-07-13 17:28:13321// This test is disabled. nasko is working on a fix and it should be committed
322// within the hour. The test crashed on linux chromiumos.
323IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DISABLED_BrowserTagIsolation) {
[email protected]fb29e6cf2012-07-12 21:27:20324 ASSERT_TRUE(StartTestServer());
325
326 // Load a (non-app) page under the "localhost" origin that sets a cookie.
327 GURL set_cookie_url = test_server()->GetURL(
328 "files/extensions/platform_apps/isolation/set_cookie.html");
329 GURL::Replacements replace_host;
330 std::string host_str("localhost"); // Must stay in scope with replace_host.
331 replace_host.SetHostStr(host_str);
332 set_cookie_url = set_cookie_url.ReplaceComponents(replace_host);
333
334 ui_test_utils::NavigateToURLWithDisposition(
335 browser(), set_cookie_url,
336 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
337
338 // Make sure the cookie is set.
339 int cookie_size;
340 std::string cookie_value;
341 automation_util::GetCookies(set_cookie_url,
342 chrome::GetWebContentsAt(browser(), 0),
343 &cookie_size, &cookie_value);
344 ASSERT_EQ("testCookie=1", cookie_value);
345
346 // Create a listener to JS message to know when the app has launched.
347 ExtensionTestMessageListener launched_listener("Launched", false);
348 LoadAndLaunchPlatformApp("browser_tag_isolation");
349 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
350
351 ui_test_utils::WindowedNotificationObserver load_stop_observer(
352 content::NOTIFICATION_LOAD_STOP,
353 content::NotificationService::AllSources());
354 load_stop_observer.Wait();
355 content::Source<content::NavigationController> source =
356 load_stop_observer.source();
357
358 // Test the regular browser context to ensure we still have only one cookie.
359 automation_util::GetCookies(GURL("http://localhost"),
360 chrome::GetWebContentsAt(browser(), 0),
361 &cookie_size, &cookie_value);
362 ASSERT_EQ("testCookie=1", cookie_value);
363
364 ASSERT_TRUE(source->GetWebContents()->GetRenderProcessHost()->IsGuest());
365
366 // Now, test the browser tag to ensure we have properly set the cookie and
367 // we have only one.
368 automation_util::GetCookies(GURL("http://localhost"),
369 source->GetWebContents(),
370 &cookie_size, &cookie_value);
371 ASSERT_EQ("isolation_guest=true", cookie_value);
372}