blob: 164b8d77f8ba8d76a57b1bd7a8d52bcd89437528 [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]28c3eeb2012-10-15 05:47:5312#include "chrome/browser/extensions/app_restore_service_factory.h"
13#include "chrome/browser/extensions/app_restore_service.h"
[email protected]a6db6122012-09-03 06:00:2314#include "chrome/browser/extensions/extension_browsertest.h"
[email protected]28c3eeb2012-10-15 05:47:5315#include "chrome/browser/extensions/extension_prefs.h"
16#include "chrome/browser/extensions/extension_service.h"
17#include "chrome/browser/extensions/extension_system.h"
[email protected]259771102012-05-31 16:52:2018#include "chrome/browser/extensions/extension_test_message_listener.h"
19#include "chrome/browser/extensions/platform_app_browsertest_util.h"
[email protected]a6db6122012-09-03 06:00:2320#include "chrome/browser/extensions/platform_app_launcher.h"
[email protected]259771102012-05-31 16:52:2021#include "chrome/browser/extensions/shell_window_registry.h"
[email protected]cfc6cca2011-12-01 02:30:0622#include "chrome/browser/ui/browser.h"
[email protected]52877dbc62012-06-29 22:22:0323#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]a5a0be02012-07-18 05:51:5424#include "chrome/browser/ui/extensions/application_launch.h"
[email protected]d72d3a62012-05-10 03:45:0825#include "chrome/browser/ui/extensions/shell_window.h"
[email protected]5b1a04b42012-06-15 00:41:4426#include "chrome/common/chrome_notification_types.h"
[email protected]4d007b312012-10-17 03:00:4827#include "chrome/common/url_constants.h"
[email protected]cfc6cca2011-12-01 02:30:0628#include "chrome/test/base/ui_test_utils.h"
[email protected]fb29e6cf2012-07-12 21:27:2029#include "content/public/browser/render_process_host.h"
[email protected]a6db6122012-09-03 06:00:2330#include "content/public/browser/web_intents_dispatcher.h"
[email protected]28c3eeb2012-10-15 05:47:5331#include "content/public/test/test_utils.h"
[email protected]a6db6122012-09-03 06:00:2332#include "googleurl/src/gurl.h"
33#include "webkit/glue/web_intent_data.h"
[email protected]cfc6cca2011-12-01 02:30:0634
[email protected]bb81f382012-01-03 22:45:4435using content::WebContents;
[email protected]d9ede582012-08-14 19:21:3836
37namespace extensions {
[email protected]31bdbfef2012-05-22 19:59:1538
[email protected]375003a2011-12-13 02:53:2139namespace {
[email protected]85d3d5e2012-08-31 21:19:1740
[email protected]375003a2011-12-13 02:53:2141// Non-abstract RenderViewContextMenu class.
42class PlatformAppContextMenu : public RenderViewContextMenu {
43 public:
[email protected]bb81f382012-01-03 22:45:4444 PlatformAppContextMenu(WebContents* web_contents,
[email protected]35be7ec2012-02-12 20:42:5145 const content::ContextMenuParams& params)
[email protected]bb81f382012-01-03 22:45:4446 : RenderViewContextMenu(web_contents, params) {}
[email protected]375003a2011-12-13 02:53:2147
[email protected]ad687a72012-06-08 06:03:1148 bool HasCommandWithId(int command_id) {
49 return menu_model_.GetIndexOfCommandId(command_id) != -1;
50 }
51
[email protected]375003a2011-12-13 02:53:2152 protected:
[email protected]85d3d5e2012-08-31 21:19:1753 // RenderViewContextMenu implementation.
54 virtual bool GetAcceleratorForCommandId(
55 int command_id,
56 ui::Accelerator* accelerator) OVERRIDE {
[email protected]375003a2011-12-13 02:53:2157 return false;
58 }
[email protected]85d3d5e2012-08-31 21:19:1759 virtual void PlatformInit() OVERRIDE {}
60 virtual void PlatformCancel() OVERRIDE {}
[email protected]375003a2011-12-13 02:53:2161};
62
[email protected]a6db6122012-09-03 06:00:2363// State holder for the LaunchReply test. This provides an WebIntentsDispatcher
64// that will, when used to launch a Web Intent, will return its reply via this
65// class. The result may then be waited on via WaitUntilReply().
66class LaunchReplyHandler {
67 public:
68 explicit LaunchReplyHandler(webkit_glue::WebIntentData& data)
69 : data_(data),
70 replied_(false),
71 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
72 intents_dispatcher_ = content::WebIntentsDispatcher::Create(data);
73 intents_dispatcher_->RegisterReplyNotification(base::Bind(
74 &LaunchReplyHandler::OnReply, weak_ptr_factory_.GetWeakPtr()));
75 }
76
77 content::WebIntentsDispatcher* intents_dispatcher() {
78 return intents_dispatcher_;
79 }
80
81 // Waits until a reply to this Web Intent is provided via the
82 // WebIntentsDispatcher.
83 bool WaitUntilReply() {
84 if (replied_)
85 return true;
86 waiting_ = true;
87 content::RunMessageLoop();
88 waiting_ = false;
89 return replied_;
90 }
91
92 private:
93 void OnReply(webkit_glue::WebIntentReplyType reply) {
94 // Note that the ReplyNotification registered on WebIntentsDispatcher does
95 // not include the result data: this is reserved for the source page (which
96 // we don't care about).
97 replied_ = true;
98 if (waiting_)
99 MessageLoopForUI::current()->Quit();
100 }
101
102 webkit_glue::WebIntentData data_;
103 bool replied_;
104 bool waiting_;
105 content::WebIntentsDispatcher* intents_dispatcher_;
106 base::WeakPtrFactory<LaunchReplyHandler> weak_ptr_factory_;
107};
108
[email protected]4d007b312012-10-17 03:00:48109// This class keeps track of tabs as they are added to the browser. It will be
110// "done" (i.e. won't block on Wait()) once |observations| tabs have been added.
111class TabsAddedNotificationObserver
112 : public content::WindowedNotificationObserver {
113 public:
114 explicit TabsAddedNotificationObserver(size_t observations)
115 : content::WindowedNotificationObserver(
116 chrome::NOTIFICATION_TAB_ADDED,
117 content::NotificationService::AllSources()),
118 observations_(observations) {
119 }
120
121 virtual void Observe(int type,
122 const content::NotificationSource& source,
123 const content::NotificationDetails& details) OVERRIDE {
124 observed_tabs_.push_back(
125 content::Details<WebContents>(details).ptr());
126 if (observed_tabs_.size() == observations_)
127 content::WindowedNotificationObserver::Observe(type, source, details);
128 }
129
130 const std::vector<content::WebContents*>& tabs() { return observed_tabs_; }
131
132 private:
133 size_t observations_;
134 std::vector<content::WebContents*> observed_tabs_;
135
136 DISALLOW_COPY_AND_ASSIGN(TabsAddedNotificationObserver);
137};
138
[email protected]85d3d5e2012-08-31 21:19:17139const char kTestFilePath[] = "platform_apps/launch_files/test.txt";
140
[email protected]375003a2011-12-13 02:53:21141} // namespace
142
[email protected]eb4bcac2012-06-27 01:32:08143// Tests that CreateShellWindow doesn't crash if you close it straight away.
144// LauncherPlatformAppBrowserTest relies on this behaviour, but is only run for
145// ash, so we test that it works here.
146IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, CreateAndCloseShellWindow) {
147 const Extension* extension = LoadAndLaunchPlatformApp("minimal");
148 ShellWindow* window = CreateShellWindow(extension);
149 CloseShellWindow(window);
150}
151
[email protected]dc37b002012-04-23 23:02:26152// Tests that platform apps received the "launch" event when launched.
153IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
154 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
[email protected]cfc6cca2011-12-01 02:30:06155}
[email protected]375003a2011-12-13 02:53:21156
[email protected]a6db6122012-09-03 06:00:23157// Tests that platform apps can reply to "launch" events that contain a Web
158// Intent. This test does not test the mechanics of invoking a Web Intent
159// from a source page, and short-circuits to LaunchPlatformAppWithWebIntent.
160IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchReply) {
161 FilePath path = test_data_dir_.AppendASCII("platform_apps/launch_reply");
162 const extensions::Extension* extension = LoadExtension(path);
163 ASSERT_TRUE(extension) << "Failed to load extension.";
164
165 webkit_glue::WebIntentData data(
166 UTF8ToUTF16("http://webintents.org/view"),
167 UTF8ToUTF16("text/plain"),
168 UTF8ToUTF16("irrelevant unserialized string data"));
169 LaunchReplyHandler handler(data);
170
171 // Navigate to a boring page: we don't care what it is, but we require some
172 // source WebContents to launch the Web Intent "from".
173 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
174 WebContents* web_contents = chrome::GetActiveWebContents(browser());
175 ASSERT_TRUE(web_contents);
176
177 extensions::LaunchPlatformAppWithWebIntent(
178 browser()->profile(),
179 extension,
180 handler.intents_dispatcher(),
181 web_contents);
182
183 ASSERT_TRUE(handler.WaitUntilReply());
184}
185
[email protected]567230b2012-08-21 21:11:44186// Tests that platform apps cannot use certain disabled window properties, but
187// can override them and then use them.
188IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisabledWindowProperties) {
189 ASSERT_TRUE(RunPlatformAppTest("platform_apps/disabled_window_properties"))
190 << message_;
191}
192
[email protected]edd7ed692012-02-08 02:50:03193IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
[email protected]dc37b002012-04-23 23:02:26194 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]31bdbfef2012-05-22 19:59:15195 LoadAndLaunchPlatformApp("minimal");
[email protected]dc37b002012-04-23 23:02:26196
197 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:21198
[email protected]375003a2011-12-13 02:53:21199 // The empty app doesn't add any context menu items, so its menu should
[email protected]0cfacd82012-02-09 01:55:33200 // only include the developer tools.
[email protected]d72d3a62012-05-10 03:45:08201 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:31202 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:21203 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:51204 content::ContextMenuParams params(data);
[email protected]85d3d5e2012-08-31 21:19:17205 scoped_ptr<PlatformAppContextMenu> menu;
206 menu.reset(new PlatformAppContextMenu(web_contents, params));
[email protected]375003a2011-12-13 02:53:21207 menu->Init();
[email protected]ad687a72012-06-08 06:03:11208 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
[email protected]65992d52012-09-19 23:05:31209 ASSERT_TRUE(
210 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
[email protected]671c9702012-09-27 07:51:19211 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
[email protected]ad687a72012-06-08 06:03:11212 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
213 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]375003a2011-12-13 02:53:21214}
215
[email protected]edd7ed692012-02-08 02:50:03216IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) {
[email protected]dc37b002012-04-23 23:02:26217 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]375003a2011-12-13 02:53:21218 LoadAndLaunchPlatformApp("context_menu");
219
[email protected]dc37b002012-04-23 23:02:26220 // Wait for the extension to tell us it's initialized its context menus and
221 // launched a window.
222 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:21223
[email protected]271d01c2012-08-27 23:48:05224 // The context_menu app has two context menu items. These, along with a
[email protected]0cfacd82012-02-09 01:55:33225 // separator and the developer tools, is all that should be in the menu.
[email protected]d72d3a62012-05-10 03:45:08226 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:31227 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:21228 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:51229 content::ContextMenuParams params(data);
[email protected]85d3d5e2012-08-31 21:19:17230 scoped_ptr<PlatformAppContextMenu> menu;
231 menu.reset(new PlatformAppContextMenu(web_contents, params));
[email protected]375003a2011-12-13 02:53:21232 menu->Init();
[email protected]ad687a72012-06-08 06:03:11233 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
[email protected]271d01c2012-08-27 23:48:05234 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + 1));
[email protected]ad687a72012-06-08 06:03:11235 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
[email protected]65992d52012-09-19 23:05:31236 ASSERT_TRUE(
237 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
[email protected]671c9702012-09-27 07:51:19238 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
[email protected]ad687a72012-06-08 06:03:11239 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
240 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]b3c019b2012-08-28 02:43:48241 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
242}
243
[email protected]65992d52012-09-19 23:05:31244IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, InstalledAppWithContextMenu) {
245 ExtensionTestMessageListener launched_listener("Launched", false);
246 InstallAndLaunchPlatformApp("context_menu");
247
248 // Wait for the extension to tell us it's initialized its context menus and
249 // launched a window.
250 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
251
252 // The context_menu app has two context menu items. For an installed app
253 // these are all that should be in the menu.
254 WebContents* web_contents = GetFirstShellWindowWebContents();
255 ASSERT_TRUE(web_contents);
256 WebKit::WebContextMenuData data;
257 content::ContextMenuParams params(data);
258 scoped_ptr<PlatformAppContextMenu> menu;
259 menu.reset(new PlatformAppContextMenu(web_contents, params));
260 menu->Init();
261 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
262 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + 1));
263 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
264 ASSERT_FALSE(
265 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
[email protected]671c9702012-09-27 07:51:19266 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
[email protected]65992d52012-09-19 23:05:31267 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
268 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
269 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
270}
271
[email protected]b3c019b2012-08-28 02:43:48272IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuTextField) {
273 ExtensionTestMessageListener launched_listener("Launched", false);
274 LoadAndLaunchPlatformApp("context_menu");
275
276 // Wait for the extension to tell us it's initialized its context menus and
277 // launched a window.
278 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
279
280 // The context_menu app has one context menu item. This, along with a
281 // separator and the developer tools, is all that should be in the menu.
282 WebContents* web_contents = GetFirstShellWindowWebContents();
283 ASSERT_TRUE(web_contents);
284 WebKit::WebContextMenuData data;
285 content::ContextMenuParams params(data);
286 params.is_editable = true;
[email protected]85d3d5e2012-08-31 21:19:17287 scoped_ptr<PlatformAppContextMenu> menu;
288 menu.reset(new PlatformAppContextMenu(web_contents, params));
[email protected]b3c019b2012-08-28 02:43:48289 menu->Init();
290 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
291 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
[email protected]65992d52012-09-19 23:05:31292 ASSERT_TRUE(
293 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
[email protected]671c9702012-09-27 07:51:19294 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
[email protected]b3c019b2012-08-28 02:43:48295 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
296 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
297 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
298 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
299}
300
301IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuSelection) {
302 ExtensionTestMessageListener launched_listener("Launched", false);
303 LoadAndLaunchPlatformApp("context_menu");
304
305 // Wait for the extension to tell us it's initialized its context menus and
306 // launched a window.
307 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
308
309 // The context_menu app has one context menu item. This, along with a
310 // separator and the developer tools, is all that should be in the menu.
311 WebContents* web_contents = GetFirstShellWindowWebContents();
312 ASSERT_TRUE(web_contents);
313 WebKit::WebContextMenuData data;
314 content::ContextMenuParams params(data);
315 params.selection_text = ASCIIToUTF16("Hello World");
[email protected]85d3d5e2012-08-31 21:19:17316 scoped_ptr<PlatformAppContextMenu> menu;
317 menu.reset(new PlatformAppContextMenu(web_contents, params));
[email protected]b3c019b2012-08-28 02:43:48318 menu->Init();
319 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
320 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
[email protected]65992d52012-09-19 23:05:31321 ASSERT_TRUE(
322 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
[email protected]671c9702012-09-27 07:51:19323 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
[email protected]b3c019b2012-08-28 02:43:48324 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
325 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
326 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
327 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]375003a2011-12-13 02:53:21328}
[email protected]5f000f272012-01-19 05:25:08329
[email protected]dc83c582012-08-17 02:18:14330IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuClicked) {
331 ExtensionTestMessageListener launched_listener("Launched", false);
332 LoadAndLaunchPlatformApp("context_menu_click");
333
334 // Wait for the extension to tell us it's initialized its context menus and
335 // launched a window.
336 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
337
338 // Test that the menu item shows up
339 WebContents* web_contents = GetFirstShellWindowWebContents();
340 ASSERT_TRUE(web_contents);
341 WebKit::WebContextMenuData data;
342 content::ContextMenuParams params(data);
343 params.page_url = GURL("http://foo.bar");
[email protected]85d3d5e2012-08-31 21:19:17344 scoped_ptr<PlatformAppContextMenu> menu;
345 menu.reset(new PlatformAppContextMenu(web_contents, params));
[email protected]dc83c582012-08-17 02:18:14346 menu->Init();
347 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
348
349 // Execute the menu item
350 ExtensionTestMessageListener onclicked_listener("onClicked fired for id1",
351 false);
352 menu->ExecuteCommand(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST);
353
354 ASSERT_TRUE(onclicked_listener.WaitUntilSatisfied());
355}
356
[email protected]edd7ed692012-02-08 02:50:03357IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) {
[email protected]4d007b312012-10-17 03:00:48358 TabsAddedNotificationObserver observer(2);
359
[email protected]dc37b002012-04-23 23:02:26360 ASSERT_TRUE(StartTestServer());
361 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_;
[email protected]4d007b312012-10-17 03:00:48362
363 observer.Wait();
364 ASSERT_EQ(2U, observer.tabs().size());
365 EXPECT_EQ(std::string(chrome::kExtensionInvalidRequestURL),
366 observer.tabs()[0]->GetURL().spec());
367 EXPECT_EQ("http://chromium.org/",
368 observer.tabs()[1]->GetURL().spec());
[email protected]5f000f272012-01-19 05:25:08369}
[email protected]863e6472012-01-24 19:33:58370
[email protected]40eefd52012-10-04 22:54:22371IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Iframes) {
[email protected]fd3238af2012-05-22 18:55:30372 ASSERT_TRUE(StartTestServer());
373 ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_;
374}
375
[email protected]2aac7ff2012-01-25 18:05:11376// Tests that localStorage and WebSQL are disabled for platform apps.
[email protected]edd7ed692012-02-08 02:50:03377IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) {
[email protected]2aac7ff2012-01-25 18:05:11378 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
379}
[email protected]6a5a2e52012-03-22 03:21:12380
[email protected]c0cecd1f2012-04-05 16:50:12381IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) {
382 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_;
383}
384
[email protected]f0233ff2012-07-20 20:14:50385// Tests that platform apps can use the chrome.app.window.* API.
[email protected]dc37b002012-04-23 23:02:26386IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) {
[email protected]6a5a2e52012-03-22 03:21:12387 ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_;
388}
[email protected]605fb8102012-05-04 01:36:55389
[email protected]1df22042012-08-30 19:48:55390// Tests that extensions can't use platform-app-only APIs.
391IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, PlatformAppsOnly) {
392 ASSERT_TRUE(RunExtensionTestIgnoreManifestWarnings(
393 "platform_apps/apps_only")) << message_;
394}
395
[email protected]605fb8102012-05-04 01:36:55396// Tests that platform apps have isolated storage by default.
397IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Isolation) {
398 ASSERT_TRUE(StartTestServer());
399
400 // Load a (non-app) page under the "localhost" origin that sets a cookie.
401 GURL set_cookie_url = test_server()->GetURL(
402 "files/extensions/platform_apps/isolation/set_cookie.html");
403 GURL::Replacements replace_host;
404 std::string host_str("localhost"); // Must stay in scope with replace_host.
405 replace_host.SetHostStr(host_str);
406 set_cookie_url = set_cookie_url.ReplaceComponents(replace_host);
407
408 ui_test_utils::NavigateToURLWithDisposition(
409 browser(), set_cookie_url,
410 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
411
412 // Make sure the cookie is set.
413 int cookie_size;
414 std::string cookie_value;
415 automation_util::GetCookies(
416 set_cookie_url,
[email protected]52877dbc62012-06-29 22:22:03417 chrome::GetWebContentsAt(browser(), 0),
[email protected]605fb8102012-05-04 01:36:55418 &cookie_size,
419 &cookie_value);
420 ASSERT_EQ("testCookie=1", cookie_value);
421
422 // Let the platform app request the same URL, and make sure that it doesn't
423 // see the cookie.
424 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_;
425}
[email protected]31bdbfef2012-05-22 19:59:15426
427IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ExtensionWindowingApis) {
428 // Initially there should be just the one browser window visible to the
429 // extensions API.
430 const Extension* extension = LoadExtension(
431 test_data_dir_.AppendASCII("common/background_page"));
432 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
433
434 // And no shell windows.
435 ASSERT_EQ(0U, GetShellWindowCount());
436
437 // Launch a platform app that shows a window.
438 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]ad5bb8a92012-06-07 03:55:58439 LoadAndLaunchPlatformApp("minimal");
[email protected]31bdbfef2012-05-22 19:59:15440 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
441 ASSERT_EQ(1U, GetShellWindowCount());
442 ShellWindowRegistry::ShellWindowSet shell_windows =
443 ShellWindowRegistry::Get(browser()->profile())->shell_windows();
444 int shell_window_id = (*shell_windows.begin())->session_id().id();
445
446 // But it's not visible to the extensions API, it still thinks there's just
447 // one browser window.
448 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
449 // It can't look it up by ID either
450 ASSERT_FALSE(RunGetWindowFunctionForExtension(shell_window_id, extension));
451
452 // The app can also only see one window (its own).
[email protected]ad5bb8a92012-06-07 03:55:58453 // TODO(jeremya): add an extension function to get a shell window by ID, and
454 // to get a list of all the shell windows, so we can test this.
[email protected]31bdbfef2012-05-22 19:59:15455
456 // Launch another platform app that also shows a window.
457 ExtensionTestMessageListener launched_listener2("Launched", false);
[email protected]ad5bb8a92012-06-07 03:55:58458 LoadAndLaunchPlatformApp("context_menu");
[email protected]31bdbfef2012-05-22 19:59:15459 ASSERT_TRUE(launched_listener2.WaitUntilSatisfied());
460
461 // There are two total shell windows, but each app can only see its own.
462 ASSERT_EQ(2U, GetShellWindowCount());
[email protected]ad5bb8a92012-06-07 03:55:58463 // TODO(jeremya): as above, this requires more extension functions.
[email protected]31bdbfef2012-05-22 19:59:15464}
[email protected]12e540452012-05-26 07:09:36465
[email protected]a2ece522012-10-16 03:41:10466// ChromeOS does not support passing arguments on the command line, so the tests
467// that rely on this functionality are disabled.
[email protected]12e540452012-05-26 07:09:36468#if !defined(OS_CHROMEOS)
469// Tests that command line parameters get passed through to platform apps
470// via launchData correctly when launching with a file.
471IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFile) {
[email protected]85d3d5e2012-08-31 21:19:17472 SetCommandLineArg(kTestFilePath);
[email protected]12e540452012-05-26 07:09:36473 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file"))
474 << message_;
475}
476
[email protected]a5a0be02012-07-18 05:51:54477// Tests that relative paths can be passed through to the platform app.
478// This test doesn't use the normal test infrastructure as it needs to open
479// the application differently to all other platform app tests, by setting
480// the application_launch::LaunchParams.current_directory field.
481IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithRelativeFile) {
482 // Setup the command line
483 ClearCommandLineArgs();
484 CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]85d3d5e2012-08-31 21:19:17485 FilePath relative_test_doc = FilePath::FromUTF8Unsafe(kTestFilePath);
[email protected]a5a0be02012-07-18 05:51:54486 relative_test_doc = relative_test_doc.NormalizePathSeparators();
487 command_line->AppendArgPath(relative_test_doc);
488
489 // Load the extension
490 ResultCatcher catcher;
[email protected]d9ede582012-08-14 19:21:38491 const Extension* extension = LoadExtension(
[email protected]a5a0be02012-07-18 05:51:54492 test_data_dir_.AppendASCII("platform_apps/launch_file"));
493 ASSERT_TRUE(extension);
494
495 // Run the test
496 application_launch::LaunchParams params(browser()->profile(), extension,
497 extension_misc::LAUNCH_NONE,
498 NEW_WINDOW);
499 params.command_line = CommandLine::ForCurrentProcess();
500 params.current_directory = test_data_dir_;
501 application_launch::OpenApplication(params);
502
503 if (!catcher.GetNextResult()) {
504 message_ = catcher.message();
505 ASSERT_TRUE(0);
506 }
507}
508
[email protected]12e540452012-05-26 07:09:36509// Tests that no launch data is sent through if the platform app provides
510// an intent with the wrong action.
511IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongIntent) {
[email protected]85d3d5e2012-08-31 21:19:17512 SetCommandLineArg(kTestFilePath);
[email protected]12e540452012-05-26 07:09:36513 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_intent"))
514 << message_;
515}
516
517// Tests that no launch data is sent through if the file is of the wrong MIME
518// type.
519IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongType) {
[email protected]85d3d5e2012-08-31 21:19:17520 SetCommandLineArg(kTestFilePath);
[email protected]12e540452012-05-26 07:09:36521 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_type"))
522 << message_;
523}
524
525// Tests that no launch data is sent through if the platform app does not
526// provide an intent.
527IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNoIntent) {
[email protected]85d3d5e2012-08-31 21:19:17528 SetCommandLineArg(kTestFilePath);
[email protected]12e540452012-05-26 07:09:36529 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_no_intent"))
530 << message_;
531}
532
533// Tests that no launch data is sent through if the file MIME type cannot
534// be read.
535IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoType) {
536 SetCommandLineArg("platform_apps/launch_files/test.unknownextension");
537 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
538 << message_;
539}
540
541// Tests that no launch data is sent through if the file does not exist.
542IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoFile) {
543 SetCommandLineArg("platform_apps/launch_files/doesnotexist.txt");
544 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
545 << message_;
546}
547
548// Tests that no launch data is sent through if the argument is a directory.
549IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithDirectory) {
550 SetCommandLineArg("platform_apps/launch_files");
551 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
552 << message_;
553}
554
555// Tests that no launch data is sent through if there are no arguments passed
556// on the command line
557IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNothing) {
558 ClearCommandLineArgs();
559 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_nothing"))
560 << message_;
561}
[email protected]ffc7b4d2012-06-08 00:05:32562
563// Test that platform apps can use the chrome.fileSystem.getDisplayPath
564// function to get the native file system path of a file they are launched with.
565IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, GetDisplayPath) {
[email protected]85d3d5e2012-08-31 21:19:17566 SetCommandLineArg(kTestFilePath);
[email protected]ffc7b4d2012-06-08 00:05:32567 ASSERT_TRUE(RunPlatformAppTest("platform_apps/get_display_path"))
568 << message_;
569}
570
[email protected]12e540452012-05-26 07:09:36571#endif // defined(OS_CHROMEOS)
[email protected]5b1a04b42012-06-15 00:41:44572
573IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OpenLink) {
574 ASSERT_TRUE(StartTestServer());
[email protected]a7fe9112012-07-20 02:34:45575 content::WindowedNotificationObserver observer(
[email protected]5b1a04b42012-06-15 00:41:44576 chrome::NOTIFICATION_TAB_ADDED,
577 content::Source<content::WebContentsDelegate>(browser()));
578 LoadAndLaunchPlatformApp("open_link");
579 observer.Wait();
580 ASSERT_EQ(2, browser()->tab_count());
581}
[email protected]d9ede582012-08-14 19:21:38582
[email protected]3a8e61942012-08-23 01:46:45583IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MutationEventsDisabled) {
584 ASSERT_TRUE(RunPlatformAppTest("platform_apps/mutation_events")) << message_;
585}
586
[email protected]939532d02012-08-31 23:37:33587// Test that windows created with an id will remember and restore their
588// geometry when opening new windows.
589IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ShellWindowRestorePosition) {
590 ExtensionTestMessageListener page2_listener("WaitForPage2", true);
591 ExtensionTestMessageListener page3_listener("WaitForPage3", true);
592 ExtensionTestMessageListener done_listener("Done1", false);
593 ExtensionTestMessageListener done2_listener("Done2", false);
594 ExtensionTestMessageListener done3_listener("Done3", false);
595
596 ASSERT_TRUE(LoadAndLaunchPlatformApp("geometry"));
597
598 // Wait for the app to be launched (although this is mostly to have a
599 // message to reply to to let the script know it should create its second
600 // window.
601 ASSERT_TRUE(page2_listener.WaitUntilSatisfied());
602
603 // Wait for the first window to verify its geometry was correctly set
604 // from the default* attributes passed to the create function.
605 ASSERT_TRUE(done_listener.WaitUntilSatisfied());
606
607 // Programatically move and resize the window.
608 ShellWindow* window = GetFirstShellWindow();
609 ASSERT_TRUE(window);
610 gfx::Rect bounds(137, 143, 203, 187);
611 window->GetBaseWindow()->SetBounds(bounds);
612
613#if defined(TOOLKIT_GTK)
614 // TODO(mek): On GTK we have to wait for a roundtrip to the X server before
615 // a resize actually happens:
616 // "if you call gtk_window_resize() then immediately call
617 // gtk_window_get_size(), the size won't have taken effect yet. After the
618 // window manager processes the resize request, GTK+ receives notification
619 // that the size has changed via a configure event, and the size of the
620 // window gets updated."
621 // Because of this we have to wait for an unknown time for the resize to
622 // actually take effect. So wait some time or until the resize got
623 // handled.
624 base::TimeTicks end_time = base::TimeTicks::Now() +
625 TestTimeouts::action_timeout();
626 while (base::TimeTicks::Now() < end_time &&
627 bounds != window->GetBaseWindow()->GetBounds()) {
628 content::RunAllPendingInMessageLoop();
629 }
630
631 // In the GTK ShellWindow implementation there also is a delay between
632 // getting the correct bounds and it calling SaveWindowPosition, so call that
633 // method explicitly to make sure the value was stored.
634 window->SaveWindowPosition();
635#endif // defined(TOOLKIT_GTK)
636
637 // Make sure the window was properly moved&resized.
638 ASSERT_EQ(bounds, window->GetBaseWindow()->GetBounds());
639
640 // Tell javascript to open a second window.
641 page2_listener.Reply("continue");
642
643 // Wait for javascript to verify that the second window got the updated
644 // coordinates, ignoring the default coordinates passed to the create method.
645 ASSERT_TRUE(done2_listener.WaitUntilSatisfied());
646
647 // Tell javascript to open a third window.
648 page3_listener.Reply("continue");
649
650 // Wait for javascript to verify that the third window got the restored size
651 // and explicitly specified coordinates.
652 ASSERT_TRUE(done3_listener.WaitUntilSatisfied());
653}
[email protected]939532d02012-08-31 23:37:33654
[email protected]28c3eeb2012-10-15 05:47:53655// Tests that a running app is recorded in the preferences as such.
656IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, RunningAppsAreRecorded) {
657 content::WindowedNotificationObserver extension_suspended(
658 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
659 content::NotificationService::AllSources());
660
661 const Extension* extension = LoadExtension(
662 test_data_dir_.AppendASCII("platform_apps/restart_test"));
663 ASSERT_TRUE(extension);
664 ExtensionService* extension_service =
665 ExtensionSystem::Get(browser()->profile())->extension_service();
666 ExtensionPrefs* extension_prefs = extension_service->extension_prefs();
667
668 // App is running.
669 ASSERT_TRUE(extension_prefs->IsExtensionRunning(extension->id()));
670
671 // Wait for the extension to get suspended.
672 extension_suspended.Wait();
673
674 // App isn't running because it got suspended.
675 ASSERT_FALSE(extension_prefs->IsExtensionRunning(extension->id()));
676
677 // Pretend that the app is supposed to be running.
678 extension_prefs->SetExtensionRunning(extension->id(), true);
679
680 ExtensionTestMessageListener restart_listener("onRestarted", false);
681 AppRestoreServiceFactory::GetForProfile(browser()->profile())->RestoreApps();
682 restart_listener.WaitUntilSatisfied();
683}
684
[email protected]d9ede582012-08-14 19:21:38685} // namespace extensions