blob: 636ee305a463e099b79fa2c63f17ca94aa99fc41 [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]a6db6122012-09-03 06:00:235#include "base/bind.h"
[email protected]939532d02012-08-31 23:37:336#include "base/test/test_timeouts.h"
7#include "base/threading/platform_thread.h"
[email protected]b3c019b2012-08-28 02:43:488#include "base/utf_string_conversions.h"
[email protected]ad687a72012-06-08 06:03:119#include "chrome/app/chrome_command_ids.h"
[email protected]605fb8102012-05-04 01:36:5510#include "chrome/browser/automation/automation_util.h"
[email protected]375003a2011-12-13 02:53:2111#include "chrome/browser/tab_contents/render_view_context_menu.h"
[email protected]a6db6122012-09-03 06:00:2312#include "chrome/browser/extensions/extension_browsertest.h"
[email protected]259771102012-05-31 16:52:2013#include "chrome/browser/extensions/extension_test_message_listener.h"
14#include "chrome/browser/extensions/platform_app_browsertest_util.h"
[email protected]a6db6122012-09-03 06:00:2315#include "chrome/browser/extensions/platform_app_launcher.h"
[email protected]259771102012-05-31 16:52:2016#include "chrome/browser/extensions/shell_window_registry.h"
[email protected]cfc6cca2011-12-01 02:30:0617#include "chrome/browser/ui/browser.h"
[email protected]52877dbc62012-06-29 22:22:0318#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]a5a0be02012-07-18 05:51:5419#include "chrome/browser/ui/extensions/application_launch.h"
[email protected]d72d3a62012-05-10 03:45:0820#include "chrome/browser/ui/extensions/shell_window.h"
[email protected]5b1a04b42012-06-15 00:41:4421#include "chrome/common/chrome_notification_types.h"
[email protected]cfc6cca2011-12-01 02:30:0622#include "chrome/test/base/ui_test_utils.h"
[email protected]fb29e6cf2012-07-12 21:27:2023#include "content/public/browser/render_process_host.h"
[email protected]a6db6122012-09-03 06:00:2324#include "content/public/browser/web_intents_dispatcher.h"
25#include "googleurl/src/gurl.h"
26#include "webkit/glue/web_intent_data.h"
[email protected]cfc6cca2011-12-01 02:30:0627
[email protected]bb81f382012-01-03 22:45:4428using content::WebContents;
[email protected]d9ede582012-08-14 19:21:3829
30namespace extensions {
[email protected]31bdbfef2012-05-22 19:59:1531
[email protected]375003a2011-12-13 02:53:2132namespace {
[email protected]85d3d5e2012-08-31 21:19:1733
[email protected]375003a2011-12-13 02:53:2134// Non-abstract RenderViewContextMenu class.
35class PlatformAppContextMenu : public RenderViewContextMenu {
36 public:
[email protected]bb81f382012-01-03 22:45:4437 PlatformAppContextMenu(WebContents* web_contents,
[email protected]35be7ec2012-02-12 20:42:5138 const content::ContextMenuParams& params)
[email protected]bb81f382012-01-03 22:45:4439 : RenderViewContextMenu(web_contents, params) {}
[email protected]375003a2011-12-13 02:53:2140
[email protected]ad687a72012-06-08 06:03:1141 bool HasCommandWithId(int command_id) {
42 return menu_model_.GetIndexOfCommandId(command_id) != -1;
43 }
44
[email protected]375003a2011-12-13 02:53:2145 protected:
[email protected]85d3d5e2012-08-31 21:19:1746 // RenderViewContextMenu implementation.
47 virtual bool GetAcceleratorForCommandId(
48 int command_id,
49 ui::Accelerator* accelerator) OVERRIDE {
[email protected]375003a2011-12-13 02:53:2150 return false;
51 }
[email protected]85d3d5e2012-08-31 21:19:1752 virtual void PlatformInit() OVERRIDE {}
53 virtual void PlatformCancel() OVERRIDE {}
[email protected]375003a2011-12-13 02:53:2154};
55
[email protected]a6db6122012-09-03 06:00:2356// State holder for the LaunchReply test. This provides an WebIntentsDispatcher
57// that will, when used to launch a Web Intent, will return its reply via this
58// class. The result may then be waited on via WaitUntilReply().
59class LaunchReplyHandler {
60 public:
61 explicit LaunchReplyHandler(webkit_glue::WebIntentData& data)
62 : data_(data),
63 replied_(false),
64 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
65 intents_dispatcher_ = content::WebIntentsDispatcher::Create(data);
66 intents_dispatcher_->RegisterReplyNotification(base::Bind(
67 &LaunchReplyHandler::OnReply, weak_ptr_factory_.GetWeakPtr()));
68 }
69
70 content::WebIntentsDispatcher* intents_dispatcher() {
71 return intents_dispatcher_;
72 }
73
74 // Waits until a reply to this Web Intent is provided via the
75 // WebIntentsDispatcher.
76 bool WaitUntilReply() {
77 if (replied_)
78 return true;
79 waiting_ = true;
80 content::RunMessageLoop();
81 waiting_ = false;
82 return replied_;
83 }
84
85 private:
86 void OnReply(webkit_glue::WebIntentReplyType reply) {
87 // Note that the ReplyNotification registered on WebIntentsDispatcher does
88 // not include the result data: this is reserved for the source page (which
89 // we don't care about).
90 replied_ = true;
91 if (waiting_)
92 MessageLoopForUI::current()->Quit();
93 }
94
95 webkit_glue::WebIntentData data_;
96 bool replied_;
97 bool waiting_;
98 content::WebIntentsDispatcher* intents_dispatcher_;
99 base::WeakPtrFactory<LaunchReplyHandler> weak_ptr_factory_;
100};
101
[email protected]85d3d5e2012-08-31 21:19:17102const char kTestFilePath[] = "platform_apps/launch_files/test.txt";
103
[email protected]375003a2011-12-13 02:53:21104} // namespace
105
[email protected]eb4bcac2012-06-27 01:32:08106// Tests that CreateShellWindow doesn't crash if you close it straight away.
107// LauncherPlatformAppBrowserTest relies on this behaviour, but is only run for
108// ash, so we test that it works here.
109IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, CreateAndCloseShellWindow) {
110 const Extension* extension = LoadAndLaunchPlatformApp("minimal");
111 ShellWindow* window = CreateShellWindow(extension);
112 CloseShellWindow(window);
113}
114
[email protected]dc37b002012-04-23 23:02:26115// Tests that platform apps received the "launch" event when launched.
116IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
117 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
[email protected]cfc6cca2011-12-01 02:30:06118}
[email protected]375003a2011-12-13 02:53:21119
[email protected]a6db6122012-09-03 06:00:23120// Tests that platform apps can reply to "launch" events that contain a Web
121// Intent. This test does not test the mechanics of invoking a Web Intent
122// from a source page, and short-circuits to LaunchPlatformAppWithWebIntent.
123IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchReply) {
124 FilePath path = test_data_dir_.AppendASCII("platform_apps/launch_reply");
125 const extensions::Extension* extension = LoadExtension(path);
126 ASSERT_TRUE(extension) << "Failed to load extension.";
127
128 webkit_glue::WebIntentData data(
129 UTF8ToUTF16("http://webintents.org/view"),
130 UTF8ToUTF16("text/plain"),
131 UTF8ToUTF16("irrelevant unserialized string data"));
132 LaunchReplyHandler handler(data);
133
134 // Navigate to a boring page: we don't care what it is, but we require some
135 // source WebContents to launch the Web Intent "from".
136 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
137 WebContents* web_contents = chrome::GetActiveWebContents(browser());
138 ASSERT_TRUE(web_contents);
139
140 extensions::LaunchPlatformAppWithWebIntent(
141 browser()->profile(),
142 extension,
143 handler.intents_dispatcher(),
144 web_contents);
145
146 ASSERT_TRUE(handler.WaitUntilReply());
147}
148
[email protected]567230b2012-08-21 21:11:44149// Tests that platform apps cannot use certain disabled window properties, but
150// can override them and then use them.
151IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisabledWindowProperties) {
152 ASSERT_TRUE(RunPlatformAppTest("platform_apps/disabled_window_properties"))
153 << message_;
154}
155
[email protected]edd7ed692012-02-08 02:50:03156IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
[email protected]dc37b002012-04-23 23:02:26157 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]31bdbfef2012-05-22 19:59:15158 LoadAndLaunchPlatformApp("minimal");
[email protected]dc37b002012-04-23 23:02:26159
160 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:21161
[email protected]375003a2011-12-13 02:53:21162 // The empty app doesn't add any context menu items, so its menu should
[email protected]0cfacd82012-02-09 01:55:33163 // only include the developer tools.
[email protected]d72d3a62012-05-10 03:45:08164 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:31165 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:21166 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:51167 content::ContextMenuParams params(data);
[email protected]85d3d5e2012-08-31 21:19:17168 scoped_ptr<PlatformAppContextMenu> menu;
169 menu.reset(new PlatformAppContextMenu(web_contents, params));
[email protected]375003a2011-12-13 02:53:21170 menu->Init();
[email protected]ad687a72012-06-08 06:03:11171 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
[email protected]65992d52012-09-19 23:05:31172 ASSERT_TRUE(
173 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
[email protected]ad687a72012-06-08 06:03:11174 ASSERT_TRUE(menu->HasCommandWithId(IDC_RELOAD));
175 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
176 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]375003a2011-12-13 02:53:21177}
178
[email protected]edd7ed692012-02-08 02:50:03179IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) {
[email protected]dc37b002012-04-23 23:02:26180 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]375003a2011-12-13 02:53:21181 LoadAndLaunchPlatformApp("context_menu");
182
[email protected]dc37b002012-04-23 23:02:26183 // Wait for the extension to tell us it's initialized its context menus and
184 // launched a window.
185 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:21186
[email protected]271d01c2012-08-27 23:48:05187 // The context_menu app has two context menu items. These, along with a
[email protected]0cfacd82012-02-09 01:55:33188 // separator and the developer tools, is all that should be in the menu.
[email protected]d72d3a62012-05-10 03:45:08189 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:31190 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:21191 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:51192 content::ContextMenuParams params(data);
[email protected]85d3d5e2012-08-31 21:19:17193 scoped_ptr<PlatformAppContextMenu> menu;
194 menu.reset(new PlatformAppContextMenu(web_contents, params));
[email protected]375003a2011-12-13 02:53:21195 menu->Init();
[email protected]ad687a72012-06-08 06:03:11196 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
[email protected]271d01c2012-08-27 23:48:05197 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + 1));
[email protected]ad687a72012-06-08 06:03:11198 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
[email protected]65992d52012-09-19 23:05:31199 ASSERT_TRUE(
200 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
[email protected]ad687a72012-06-08 06:03:11201 ASSERT_TRUE(menu->HasCommandWithId(IDC_RELOAD));
202 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
203 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]b3c019b2012-08-28 02:43:48204 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
205}
206
[email protected]65992d52012-09-19 23:05:31207IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, InstalledAppWithContextMenu) {
208 ExtensionTestMessageListener launched_listener("Launched", false);
209 InstallAndLaunchPlatformApp("context_menu");
210
211 // Wait for the extension to tell us it's initialized its context menus and
212 // launched a window.
213 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
214
215 // The context_menu app has two context menu items. For an installed app
216 // these are all that should be in the menu.
217 WebContents* web_contents = GetFirstShellWindowWebContents();
218 ASSERT_TRUE(web_contents);
219 WebKit::WebContextMenuData data;
220 content::ContextMenuParams params(data);
221 scoped_ptr<PlatformAppContextMenu> menu;
222 menu.reset(new PlatformAppContextMenu(web_contents, params));
223 menu->Init();
224 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
225 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + 1));
226 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
227 ASSERT_FALSE(
228 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
229 ASSERT_FALSE(menu->HasCommandWithId(IDC_RELOAD));
230 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
231 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
232 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
233}
234
[email protected]b3c019b2012-08-28 02:43:48235IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuTextField) {
236 ExtensionTestMessageListener launched_listener("Launched", false);
237 LoadAndLaunchPlatformApp("context_menu");
238
239 // Wait for the extension to tell us it's initialized its context menus and
240 // launched a window.
241 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
242
243 // The context_menu app has one context menu item. This, along with a
244 // separator and the developer tools, is all that should be in the menu.
245 WebContents* web_contents = GetFirstShellWindowWebContents();
246 ASSERT_TRUE(web_contents);
247 WebKit::WebContextMenuData data;
248 content::ContextMenuParams params(data);
249 params.is_editable = true;
[email protected]85d3d5e2012-08-31 21:19:17250 scoped_ptr<PlatformAppContextMenu> menu;
251 menu.reset(new PlatformAppContextMenu(web_contents, params));
[email protected]b3c019b2012-08-28 02:43:48252 menu->Init();
253 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
254 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
[email protected]65992d52012-09-19 23:05:31255 ASSERT_TRUE(
256 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
[email protected]b3c019b2012-08-28 02:43:48257 ASSERT_TRUE(menu->HasCommandWithId(IDC_RELOAD));
258 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
259 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
260 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
261 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
262}
263
264IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuSelection) {
265 ExtensionTestMessageListener launched_listener("Launched", false);
266 LoadAndLaunchPlatformApp("context_menu");
267
268 // Wait for the extension to tell us it's initialized its context menus and
269 // launched a window.
270 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
271
272 // The context_menu app has one context menu item. This, along with a
273 // separator and the developer tools, is all that should be in the menu.
274 WebContents* web_contents = GetFirstShellWindowWebContents();
275 ASSERT_TRUE(web_contents);
276 WebKit::WebContextMenuData data;
277 content::ContextMenuParams params(data);
278 params.selection_text = ASCIIToUTF16("Hello World");
[email protected]85d3d5e2012-08-31 21:19:17279 scoped_ptr<PlatformAppContextMenu> menu;
280 menu.reset(new PlatformAppContextMenu(web_contents, params));
[email protected]b3c019b2012-08-28 02:43:48281 menu->Init();
282 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
283 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
[email protected]65992d52012-09-19 23:05:31284 ASSERT_TRUE(
285 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
[email protected]b3c019b2012-08-28 02:43:48286 ASSERT_TRUE(menu->HasCommandWithId(IDC_RELOAD));
287 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
288 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
289 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
290 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]375003a2011-12-13 02:53:21291}
[email protected]5f000f272012-01-19 05:25:08292
[email protected]dc83c582012-08-17 02:18:14293IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuClicked) {
294 ExtensionTestMessageListener launched_listener("Launched", false);
295 LoadAndLaunchPlatformApp("context_menu_click");
296
297 // Wait for the extension to tell us it's initialized its context menus and
298 // launched a window.
299 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
300
301 // Test that the menu item shows up
302 WebContents* web_contents = GetFirstShellWindowWebContents();
303 ASSERT_TRUE(web_contents);
304 WebKit::WebContextMenuData data;
305 content::ContextMenuParams params(data);
306 params.page_url = GURL("http://foo.bar");
[email protected]85d3d5e2012-08-31 21:19:17307 scoped_ptr<PlatformAppContextMenu> menu;
308 menu.reset(new PlatformAppContextMenu(web_contents, params));
[email protected]dc83c582012-08-17 02:18:14309 menu->Init();
310 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
311
312 // Execute the menu item
313 ExtensionTestMessageListener onclicked_listener("onClicked fired for id1",
314 false);
315 menu->ExecuteCommand(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST);
316
317 ASSERT_TRUE(onclicked_listener.WaitUntilSatisfied());
318}
319
[email protected]edd7ed692012-02-08 02:50:03320IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) {
[email protected]dc37b002012-04-23 23:02:26321 ASSERT_TRUE(StartTestServer());
322 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_;
[email protected]5f000f272012-01-19 05:25:08323}
[email protected]863e6472012-01-24 19:33:58324
[email protected]fd3238af2012-05-22 18:55:30325IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Iframes) {
326 ASSERT_TRUE(StartTestServer());
327 ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_;
328}
329
[email protected]2aac7ff2012-01-25 18:05:11330// Tests that localStorage and WebSQL are disabled for platform apps.
[email protected]edd7ed692012-02-08 02:50:03331IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) {
[email protected]2aac7ff2012-01-25 18:05:11332 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
333}
[email protected]6a5a2e52012-03-22 03:21:12334
[email protected]c0cecd1f2012-04-05 16:50:12335IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) {
336 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_;
337}
338
[email protected]f0233ff2012-07-20 20:14:50339// Tests that platform apps can use the chrome.app.window.* API.
[email protected]dc37b002012-04-23 23:02:26340IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) {
[email protected]6a5a2e52012-03-22 03:21:12341 ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_;
342}
[email protected]605fb8102012-05-04 01:36:55343
[email protected]1df22042012-08-30 19:48:55344// Tests that extensions can't use platform-app-only APIs.
345IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, PlatformAppsOnly) {
346 ASSERT_TRUE(RunExtensionTestIgnoreManifestWarnings(
347 "platform_apps/apps_only")) << message_;
348}
349
[email protected]605fb8102012-05-04 01:36:55350// Tests that platform apps have isolated storage by default.
351IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Isolation) {
352 ASSERT_TRUE(StartTestServer());
353
354 // Load a (non-app) page under the "localhost" origin that sets a cookie.
355 GURL set_cookie_url = test_server()->GetURL(
356 "files/extensions/platform_apps/isolation/set_cookie.html");
357 GURL::Replacements replace_host;
358 std::string host_str("localhost"); // Must stay in scope with replace_host.
359 replace_host.SetHostStr(host_str);
360 set_cookie_url = set_cookie_url.ReplaceComponents(replace_host);
361
362 ui_test_utils::NavigateToURLWithDisposition(
363 browser(), set_cookie_url,
364 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
365
366 // Make sure the cookie is set.
367 int cookie_size;
368 std::string cookie_value;
369 automation_util::GetCookies(
370 set_cookie_url,
[email protected]52877dbc62012-06-29 22:22:03371 chrome::GetWebContentsAt(browser(), 0),
[email protected]605fb8102012-05-04 01:36:55372 &cookie_size,
373 &cookie_value);
374 ASSERT_EQ("testCookie=1", cookie_value);
375
376 // Let the platform app request the same URL, and make sure that it doesn't
377 // see the cookie.
378 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_;
379}
[email protected]31bdbfef2012-05-22 19:59:15380
381IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ExtensionWindowingApis) {
382 // Initially there should be just the one browser window visible to the
383 // extensions API.
384 const Extension* extension = LoadExtension(
385 test_data_dir_.AppendASCII("common/background_page"));
386 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
387
388 // And no shell windows.
389 ASSERT_EQ(0U, GetShellWindowCount());
390
391 // Launch a platform app that shows a window.
392 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]ad5bb8a92012-06-07 03:55:58393 LoadAndLaunchPlatformApp("minimal");
[email protected]31bdbfef2012-05-22 19:59:15394 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
395 ASSERT_EQ(1U, GetShellWindowCount());
396 ShellWindowRegistry::ShellWindowSet shell_windows =
397 ShellWindowRegistry::Get(browser()->profile())->shell_windows();
398 int shell_window_id = (*shell_windows.begin())->session_id().id();
399
400 // But it's not visible to the extensions API, it still thinks there's just
401 // one browser window.
402 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
403 // It can't look it up by ID either
404 ASSERT_FALSE(RunGetWindowFunctionForExtension(shell_window_id, extension));
405
406 // The app can also only see one window (its own).
[email protected]ad5bb8a92012-06-07 03:55:58407 // TODO(jeremya): add an extension function to get a shell window by ID, and
408 // to get a list of all the shell windows, so we can test this.
[email protected]31bdbfef2012-05-22 19:59:15409
410 // Launch another platform app that also shows a window.
411 ExtensionTestMessageListener launched_listener2("Launched", false);
[email protected]ad5bb8a92012-06-07 03:55:58412 LoadAndLaunchPlatformApp("context_menu");
[email protected]31bdbfef2012-05-22 19:59:15413 ASSERT_TRUE(launched_listener2.WaitUntilSatisfied());
414
415 // There are two total shell windows, but each app can only see its own.
416 ASSERT_EQ(2U, GetShellWindowCount());
[email protected]ad5bb8a92012-06-07 03:55:58417 // TODO(jeremya): as above, this requires more extension functions.
[email protected]31bdbfef2012-05-22 19:59:15418}
[email protected]12e540452012-05-26 07:09:36419
420// TODO(benwells): fix these tests for ChromeOS.
421#if !defined(OS_CHROMEOS)
422// Tests that command line parameters get passed through to platform apps
423// via launchData correctly when launching with a file.
424IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFile) {
[email protected]85d3d5e2012-08-31 21:19:17425 SetCommandLineArg(kTestFilePath);
[email protected]12e540452012-05-26 07:09:36426 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file"))
427 << message_;
428}
429
[email protected]a5a0be02012-07-18 05:51:54430// Tests that relative paths can be passed through to the platform app.
431// This test doesn't use the normal test infrastructure as it needs to open
432// the application differently to all other platform app tests, by setting
433// the application_launch::LaunchParams.current_directory field.
434IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithRelativeFile) {
435 // Setup the command line
436 ClearCommandLineArgs();
437 CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]85d3d5e2012-08-31 21:19:17438 FilePath relative_test_doc = FilePath::FromUTF8Unsafe(kTestFilePath);
[email protected]a5a0be02012-07-18 05:51:54439 relative_test_doc = relative_test_doc.NormalizePathSeparators();
440 command_line->AppendArgPath(relative_test_doc);
441
442 // Load the extension
443 ResultCatcher catcher;
[email protected]d9ede582012-08-14 19:21:38444 const Extension* extension = LoadExtension(
[email protected]a5a0be02012-07-18 05:51:54445 test_data_dir_.AppendASCII("platform_apps/launch_file"));
446 ASSERT_TRUE(extension);
447
448 // Run the test
449 application_launch::LaunchParams params(browser()->profile(), extension,
450 extension_misc::LAUNCH_NONE,
451 NEW_WINDOW);
452 params.command_line = CommandLine::ForCurrentProcess();
453 params.current_directory = test_data_dir_;
454 application_launch::OpenApplication(params);
455
456 if (!catcher.GetNextResult()) {
457 message_ = catcher.message();
458 ASSERT_TRUE(0);
459 }
460}
461
[email protected]12e540452012-05-26 07:09:36462// Tests that no launch data is sent through if the platform app provides
463// an intent with the wrong action.
464IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongIntent) {
[email protected]85d3d5e2012-08-31 21:19:17465 SetCommandLineArg(kTestFilePath);
[email protected]12e540452012-05-26 07:09:36466 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_intent"))
467 << message_;
468}
469
470// Tests that no launch data is sent through if the file is of the wrong MIME
471// type.
472IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongType) {
[email protected]85d3d5e2012-08-31 21:19:17473 SetCommandLineArg(kTestFilePath);
[email protected]12e540452012-05-26 07:09:36474 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_type"))
475 << message_;
476}
477
478// Tests that no launch data is sent through if the platform app does not
479// provide an intent.
480IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNoIntent) {
[email protected]85d3d5e2012-08-31 21:19:17481 SetCommandLineArg(kTestFilePath);
[email protected]12e540452012-05-26 07:09:36482 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_no_intent"))
483 << message_;
484}
485
486// Tests that no launch data is sent through if the file MIME type cannot
487// be read.
488IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoType) {
489 SetCommandLineArg("platform_apps/launch_files/test.unknownextension");
490 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
491 << message_;
492}
493
494// Tests that no launch data is sent through if the file does not exist.
495IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoFile) {
496 SetCommandLineArg("platform_apps/launch_files/doesnotexist.txt");
497 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
498 << message_;
499}
500
501// Tests that no launch data is sent through if the argument is a directory.
502IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithDirectory) {
503 SetCommandLineArg("platform_apps/launch_files");
504 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
505 << message_;
506}
507
508// Tests that no launch data is sent through if there are no arguments passed
509// on the command line
510IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNothing) {
511 ClearCommandLineArgs();
512 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_nothing"))
513 << message_;
514}
[email protected]ffc7b4d2012-06-08 00:05:32515
516// Test that platform apps can use the chrome.fileSystem.getDisplayPath
517// function to get the native file system path of a file they are launched with.
518IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, GetDisplayPath) {
[email protected]85d3d5e2012-08-31 21:19:17519 SetCommandLineArg(kTestFilePath);
[email protected]ffc7b4d2012-06-08 00:05:32520 ASSERT_TRUE(RunPlatformAppTest("platform_apps/get_display_path"))
521 << message_;
522}
523
[email protected]12e540452012-05-26 07:09:36524#endif // defined(OS_CHROMEOS)
[email protected]5b1a04b42012-06-15 00:41:44525
526IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OpenLink) {
527 ASSERT_TRUE(StartTestServer());
[email protected]a7fe9112012-07-20 02:34:45528 content::WindowedNotificationObserver observer(
[email protected]5b1a04b42012-06-15 00:41:44529 chrome::NOTIFICATION_TAB_ADDED,
530 content::Source<content::WebContentsDelegate>(browser()));
531 LoadAndLaunchPlatformApp("open_link");
532 observer.Wait();
533 ASSERT_EQ(2, browser()->tab_count());
534}
[email protected]d9ede582012-08-14 19:21:38535
[email protected]3a8e61942012-08-23 01:46:45536IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MutationEventsDisabled) {
537 ASSERT_TRUE(RunPlatformAppTest("platform_apps/mutation_events")) << message_;
538}
539
[email protected]939532d02012-08-31 23:37:33540// Test that windows created with an id will remember and restore their
541// geometry when opening new windows.
542IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ShellWindowRestorePosition) {
543 ExtensionTestMessageListener page2_listener("WaitForPage2", true);
544 ExtensionTestMessageListener page3_listener("WaitForPage3", true);
545 ExtensionTestMessageListener done_listener("Done1", false);
546 ExtensionTestMessageListener done2_listener("Done2", false);
547 ExtensionTestMessageListener done3_listener("Done3", false);
548
549 ASSERT_TRUE(LoadAndLaunchPlatformApp("geometry"));
550
551 // Wait for the app to be launched (although this is mostly to have a
552 // message to reply to to let the script know it should create its second
553 // window.
554 ASSERT_TRUE(page2_listener.WaitUntilSatisfied());
555
556 // Wait for the first window to verify its geometry was correctly set
557 // from the default* attributes passed to the create function.
558 ASSERT_TRUE(done_listener.WaitUntilSatisfied());
559
560 // Programatically move and resize the window.
561 ShellWindow* window = GetFirstShellWindow();
562 ASSERT_TRUE(window);
563 gfx::Rect bounds(137, 143, 203, 187);
564 window->GetBaseWindow()->SetBounds(bounds);
565
566#if defined(TOOLKIT_GTK)
567 // TODO(mek): On GTK we have to wait for a roundtrip to the X server before
568 // a resize actually happens:
569 // "if you call gtk_window_resize() then immediately call
570 // gtk_window_get_size(), the size won't have taken effect yet. After the
571 // window manager processes the resize request, GTK+ receives notification
572 // that the size has changed via a configure event, and the size of the
573 // window gets updated."
574 // Because of this we have to wait for an unknown time for the resize to
575 // actually take effect. So wait some time or until the resize got
576 // handled.
577 base::TimeTicks end_time = base::TimeTicks::Now() +
578 TestTimeouts::action_timeout();
579 while (base::TimeTicks::Now() < end_time &&
580 bounds != window->GetBaseWindow()->GetBounds()) {
581 content::RunAllPendingInMessageLoop();
582 }
583
584 // In the GTK ShellWindow implementation there also is a delay between
585 // getting the correct bounds and it calling SaveWindowPosition, so call that
586 // method explicitly to make sure the value was stored.
587 window->SaveWindowPosition();
588#endif // defined(TOOLKIT_GTK)
589
590 // Make sure the window was properly moved&resized.
591 ASSERT_EQ(bounds, window->GetBaseWindow()->GetBounds());
592
593 // Tell javascript to open a second window.
594 page2_listener.Reply("continue");
595
596 // Wait for javascript to verify that the second window got the updated
597 // coordinates, ignoring the default coordinates passed to the create method.
598 ASSERT_TRUE(done2_listener.WaitUntilSatisfied());
599
600 // Tell javascript to open a third window.
601 page3_listener.Reply("continue");
602
603 // Wait for javascript to verify that the third window got the restored size
604 // and explicitly specified coordinates.
605 ASSERT_TRUE(done3_listener.WaitUntilSatisfied());
606}
[email protected]939532d02012-08-31 23:37:33607
[email protected]d9ede582012-08-14 19:21:38608} // namespace extensions