Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / apps / app_browsertest.cc
blob81404492df242ff36c272ef6ea4370928045d95f
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "apps/launcher.h"
6 #include "base/bind.h"
7 #include "base/command_line.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/stl_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/app/chrome_command_ids.h"
14 #include "chrome/browser/apps/app_browsertest_util.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/devtools/devtools_window.h"
17 #include "chrome/browser/extensions/api/permissions/permissions_api.h"
18 #include "chrome/browser/extensions/component_loader.h"
19 #include "chrome/browser/extensions/extension_browsertest.h"
20 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/extensions/app_launch_params.h"
24 #include "chrome/browser/ui/extensions/application_launch.h"
25 #include "chrome/browser/ui/tabs/tab_strip_model.h"
26 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
27 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/url_constants.h"
30 #include "chrome/test/base/test_switches.h"
31 #include "chrome/test/base/ui_test_utils.h"
32 #include "components/pref_registry/pref_registry_syncable.h"
33 #include "components/web_modal/web_contents_modal_dialog_manager.h"
34 #include "content/public/browser/devtools_agent_host.h"
35 #include "content/public/browser/host_zoom_map.h"
36 #include "content/public/browser/render_process_host.h"
37 #include "content/public/browser/render_widget_host_view.h"
38 #include "content/public/test/test_utils.h"
39 #include "extensions/browser/app_window/app_window.h"
40 #include "extensions/browser/app_window/app_window_registry.h"
41 #include "extensions/browser/app_window/native_app_window.h"
42 #include "extensions/browser/event_router.h"
43 #include "extensions/browser/extension_prefs.h"
44 #include "extensions/browser/extension_system.h"
45 #include "extensions/browser/notification_types.h"
46 #include "extensions/browser/pref_names.h"
47 #include "extensions/common/api/app_runtime.h"
48 #include "extensions/common/constants.h"
49 #include "extensions/test/extension_test_message_listener.h"
50 #include "extensions/test/result_catcher.h"
51 #include "net/test/embedded_test_server/embedded_test_server.h"
52 #include "ui/base/window_open_disposition.h"
53 #include "url/gurl.h"
55 #if defined(OS_CHROMEOS)
56 #include "base/memory/scoped_ptr.h"
57 #include "chrome/browser/chromeos/login/users/mock_user_manager.h"
58 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
59 #include "chromeos/dbus/dbus_thread_manager.h"
60 #include "chromeos/dbus/fake_power_manager_client.h"
61 #endif
63 using content::WebContents;
64 using web_modal::WebContentsModalDialogManager;
66 namespace app_runtime = extensions::core_api::app_runtime;
68 namespace extensions {
70 namespace {
72 // Non-abstract RenderViewContextMenu class.
73 class PlatformAppContextMenu : public RenderViewContextMenu {
74 public:
75 PlatformAppContextMenu(content::RenderFrameHost* render_frame_host,
76 const content::ContextMenuParams& params)
77 : RenderViewContextMenu(render_frame_host, params) {}
79 bool HasCommandWithId(int command_id) {
80 return menu_model_.GetIndexOfCommandId(command_id) != -1;
83 void Show() override {}
85 protected:
86 // RenderViewContextMenu implementation.
87 bool GetAcceleratorForCommandId(int command_id,
88 ui::Accelerator* accelerator) override {
89 return false;
93 // This class keeps track of tabs as they are added to the browser. It will be
94 // "done" (i.e. won't block on Wait()) once |observations| tabs have been added.
95 class TabsAddedNotificationObserver
96 : public content::WindowedNotificationObserver {
97 public:
98 explicit TabsAddedNotificationObserver(size_t observations)
99 : content::WindowedNotificationObserver(
100 chrome::NOTIFICATION_TAB_ADDED,
101 content::NotificationService::AllSources()),
102 observations_(observations) {
105 void Observe(int type,
106 const content::NotificationSource& source,
107 const content::NotificationDetails& details) override {
108 observed_tabs_.push_back(
109 content::Details<WebContents>(details).ptr());
110 if (observed_tabs_.size() == observations_)
111 content::WindowedNotificationObserver::Observe(type, source, details);
114 const std::vector<content::WebContents*>& tabs() { return observed_tabs_; }
116 private:
117 size_t observations_;
118 std::vector<content::WebContents*> observed_tabs_;
120 DISALLOW_COPY_AND_ASSIGN(TabsAddedNotificationObserver);
123 #if defined(ENABLE_PRINT_PREVIEW)
124 class ScopedPreviewTestingDelegate : PrintPreviewUI::TestingDelegate {
125 public:
126 explicit ScopedPreviewTestingDelegate(bool auto_cancel)
127 : auto_cancel_(auto_cancel),
128 total_page_count_(1),
129 rendered_page_count_(0) {
130 PrintPreviewUI::SetDelegateForTesting(this);
133 ~ScopedPreviewTestingDelegate() {
134 PrintPreviewUI::SetDelegateForTesting(NULL);
137 // PrintPreviewUI::TestingDelegate implementation.
138 bool IsAutoCancelEnabled() override { return auto_cancel_; }
140 // PrintPreviewUI::TestingDelegate implementation.
141 void DidGetPreviewPageCount(int page_count) override {
142 total_page_count_ = page_count;
145 // PrintPreviewUI::TestingDelegate implementation.
146 void DidRenderPreviewPage(content::WebContents* preview_dialog) override {
147 dialog_size_ = preview_dialog->GetContainerBounds().size();
148 ++rendered_page_count_;
149 CHECK(rendered_page_count_ <= total_page_count_);
150 if (waiting_runner_.get() && rendered_page_count_ == total_page_count_) {
151 waiting_runner_->Quit();
155 void WaitUntilPreviewIsReady() {
156 CHECK(!waiting_runner_.get());
157 if (rendered_page_count_ < total_page_count_) {
158 waiting_runner_ = new content::MessageLoopRunner;
159 waiting_runner_->Run();
160 waiting_runner_ = NULL;
164 gfx::Size dialog_size() {
165 return dialog_size_;
168 private:
169 bool auto_cancel_;
170 int total_page_count_;
171 int rendered_page_count_;
172 scoped_refptr<content::MessageLoopRunner> waiting_runner_;
173 gfx::Size dialog_size_;
176 #endif // ENABLE_PRINT_PREVIEW
178 #if !defined(OS_CHROMEOS) && !defined(OS_WIN)
179 bool CopyTestDataAndSetCommandLineArg(
180 const base::FilePath& test_data_file,
181 const base::FilePath& temp_dir,
182 const char* filename) {
183 base::FilePath path = temp_dir.AppendASCII(
184 filename).NormalizePathSeparators();
185 if (!(base::CopyFile(test_data_file, path)))
186 return false;
188 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
189 command_line->AppendArgPath(path);
190 return true;
192 #endif // !defined(OS_CHROMEOS) && !defined(OS_WIN)
194 #if !defined(OS_CHROMEOS)
195 const char kTestFilePath[] = "platform_apps/launch_files/test.txt";
196 #endif
198 } // namespace
200 // Tests that CreateAppWindow doesn't crash if you close it straight away.
201 // LauncherPlatformAppBrowserTest relies on this behaviour, but is only run for
202 // ash, so we test that it works here.
203 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, CreateAndCloseAppWindow) {
204 const Extension* extension = LoadAndLaunchPlatformApp("minimal", "Launched");
205 AppWindow* window = CreateAppWindow(extension);
206 CloseAppWindow(window);
209 // Tests that platform apps received the "launch" event when launched.
210 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
211 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
214 // Tests that platform apps cannot use certain disabled window properties, but
215 // can override them and then use them.
216 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisabledWindowProperties) {
217 ASSERT_TRUE(RunPlatformAppTest("platform_apps/disabled_window_properties"))
218 << message_;
221 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
222 LoadAndLaunchPlatformApp("minimal", "Launched");
224 // The empty app doesn't add any context menu items, so its menu should
225 // only include the developer tools.
226 WebContents* web_contents = GetFirstAppWindowWebContents();
227 ASSERT_TRUE(web_contents);
228 content::ContextMenuParams params;
229 scoped_ptr<PlatformAppContextMenu> menu;
230 menu.reset(new PlatformAppContextMenu(web_contents->GetMainFrame(), params));
231 menu->Init();
232 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
233 ASSERT_TRUE(
234 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
235 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
236 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
237 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
240 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) {
241 LoadAndLaunchPlatformApp("context_menu", "Launched");
243 // The context_menu app has two context menu items. These, along with a
244 // separator and the developer tools, is all that should be in the menu.
245 WebContents* web_contents = GetFirstAppWindowWebContents();
246 ASSERT_TRUE(web_contents);
247 content::ContextMenuParams params;
248 scoped_ptr<PlatformAppContextMenu> menu;
249 menu.reset(new PlatformAppContextMenu(web_contents->GetMainFrame(), params));
250 menu->Init();
251 int first_extensions_command_id =
252 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0);
253 ASSERT_TRUE(menu->HasCommandWithId(first_extensions_command_id));
254 ASSERT_TRUE(menu->HasCommandWithId(first_extensions_command_id + 1));
255 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
256 ASSERT_TRUE(
257 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
258 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
259 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
260 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
261 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
264 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, InstalledAppWithContextMenu) {
265 ExtensionTestMessageListener launched_listener("Launched", false);
266 InstallAndLaunchPlatformApp("context_menu");
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());
272 // The context_menu app has two context menu items. For an installed app
273 // these are all that should be in the menu.
274 WebContents* web_contents = GetFirstAppWindowWebContents();
275 ASSERT_TRUE(web_contents);
276 content::ContextMenuParams params;
277 scoped_ptr<PlatformAppContextMenu> menu;
278 menu.reset(new PlatformAppContextMenu(web_contents->GetMainFrame(), params));
279 menu->Init();
280 int extensions_custom_id =
281 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0);
282 ASSERT_TRUE(menu->HasCommandWithId(extensions_custom_id));
283 ASSERT_TRUE(menu->HasCommandWithId(extensions_custom_id + 1));
284 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
285 ASSERT_FALSE(
286 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
287 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
288 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
289 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
290 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
293 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuTextField) {
294 LoadAndLaunchPlatformApp("context_menu", "Launched");
296 // The context_menu app has one context menu item. This, along with a
297 // separator and the developer tools, is all that should be in the menu.
298 WebContents* web_contents = GetFirstAppWindowWebContents();
299 ASSERT_TRUE(web_contents);
300 content::ContextMenuParams params;
301 params.is_editable = true;
302 scoped_ptr<PlatformAppContextMenu> menu;
303 menu.reset(new PlatformAppContextMenu(web_contents->GetMainFrame(), params));
304 menu->Init();
305 int extensions_custom_id =
306 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0);
307 ASSERT_TRUE(menu->HasCommandWithId(extensions_custom_id));
308 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
309 ASSERT_TRUE(
310 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
311 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
312 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
313 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
314 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
315 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
318 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuSelection) {
319 LoadAndLaunchPlatformApp("context_menu", "Launched");
321 // The context_menu app has one context menu item. This, along with a
322 // separator and the developer tools, is all that should be in the menu.
323 WebContents* web_contents = GetFirstAppWindowWebContents();
324 ASSERT_TRUE(web_contents);
325 content::ContextMenuParams params;
326 params.selection_text = base::ASCIIToUTF16("Hello World");
327 scoped_ptr<PlatformAppContextMenu> menu;
328 menu.reset(new PlatformAppContextMenu(web_contents->GetMainFrame(), params));
329 menu->Init();
330 int extensions_custom_id =
331 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0);
332 ASSERT_TRUE(menu->HasCommandWithId(extensions_custom_id));
333 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
334 ASSERT_TRUE(
335 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
336 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
337 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
338 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
339 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
340 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
343 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuClicked) {
344 LoadAndLaunchPlatformApp("context_menu_click", "Launched");
346 // Test that the menu item shows up
347 WebContents* web_contents = GetFirstAppWindowWebContents();
348 ASSERT_TRUE(web_contents);
349 content::ContextMenuParams params;
350 params.page_url = GURL("http://foo.bar");
351 scoped_ptr<PlatformAppContextMenu> menu;
352 menu.reset(new PlatformAppContextMenu(web_contents->GetMainFrame(), params));
353 menu->Init();
354 int extensions_custom_id =
355 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0);
356 ASSERT_TRUE(menu->HasCommandWithId(extensions_custom_id));
358 // Execute the menu item
359 ExtensionTestMessageListener onclicked_listener("onClicked fired for id1",
360 false);
361 menu->ExecuteCommand(extensions_custom_id, 0);
363 ASSERT_TRUE(onclicked_listener.WaitUntilSatisfied());
366 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) {
367 TabsAddedNotificationObserver observer(2);
369 ASSERT_TRUE(StartEmbeddedTestServer());
370 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_;
372 observer.Wait();
373 ASSERT_EQ(2U, observer.tabs().size());
374 EXPECT_EQ(std::string(chrome::kExtensionInvalidRequestURL),
375 observer.tabs()[0]->GetURL().spec());
376 EXPECT_EQ("http://chromium.org/",
377 observer.tabs()[1]->GetURL().spec());
380 // Failing on some Win and Linux buildbots. See crbug.com/354425.
381 #if defined(OS_WIN) || defined(OS_LINUX)
382 #define MAYBE_Iframes DISABLED_Iframes
383 #else
384 #define MAYBE_Iframes Iframes
385 #endif
386 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_Iframes) {
387 ASSERT_TRUE(StartEmbeddedTestServer());
388 ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_;
391 // Tests that localStorage and WebSQL are disabled for platform apps.
392 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) {
393 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
396 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) {
397 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_;
400 // Tests that extensions can't use platform-app-only APIs.
401 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, PlatformAppsOnly) {
402 ASSERT_TRUE(RunExtensionTestIgnoreManifestWarnings(
403 "platform_apps/apps_only")) << message_;
406 // Tests that platform apps have isolated storage by default.
407 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Isolation) {
408 ASSERT_TRUE(StartEmbeddedTestServer());
410 // Load a (non-app) page under the "localhost" origin that sets a cookie.
411 GURL set_cookie_url = embedded_test_server()->GetURL(
412 "/extensions/platform_apps/isolation/set_cookie.html");
413 GURL::Replacements replace_host;
414 replace_host.SetHostStr("localhost");
415 set_cookie_url = set_cookie_url.ReplaceComponents(replace_host);
417 ui_test_utils::NavigateToURL(browser(), set_cookie_url);
419 // Make sure the cookie is set.
420 int cookie_size;
421 std::string cookie_value;
422 ui_test_utils::GetCookies(
423 set_cookie_url,
424 browser()->tab_strip_model()->GetWebContentsAt(0),
425 &cookie_size,
426 &cookie_value);
427 ASSERT_EQ("testCookie=1", cookie_value);
429 // Let the platform app request the same URL, and make sure that it doesn't
430 // see the cookie.
431 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_;
434 // See crbug.com/248441
435 #if defined(OS_WIN)
436 #define MAYBE_ExtensionWindowingApis DISABLED_ExtensionWindowingApis
437 #else
438 #define MAYBE_ExtensionWindowingApis ExtensionWindowingApis
439 #endif
441 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_ExtensionWindowingApis) {
442 // Initially there should be just the one browser window visible to the
443 // extensions API.
444 const Extension* extension = LoadExtension(
445 test_data_dir_.AppendASCII("common/background_page"));
446 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
448 // And no app windows.
449 ASSERT_EQ(0U, GetAppWindowCount());
451 // Launch a platform app that shows a window.
452 LoadAndLaunchPlatformApp("minimal", "Launched");
453 ASSERT_EQ(1U, GetAppWindowCount());
454 int app_window_id = GetFirstAppWindow()->session_id().id();
456 // But it's not visible to the extensions API, it still thinks there's just
457 // one browser window.
458 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
459 // It can't look it up by ID either
460 ASSERT_FALSE(RunGetWindowFunctionForExtension(app_window_id, extension));
462 // The app can also only see one window (its own).
463 // TODO(jeremya): add an extension function to get an app window by ID, and
464 // to get a list of all the app windows, so we can test this.
466 // Launch another platform app that also shows a window.
467 LoadAndLaunchPlatformApp("context_menu", "Launched");
469 // There are two total app windows, but each app can only see its own.
470 ASSERT_EQ(2U, GetAppWindowCount());
471 // TODO(jeremya): as above, this requires more extension functions.
474 // ChromeOS does not support passing arguments on the command line, so the tests
475 // that rely on this functionality are disabled.
476 #if !defined(OS_CHROMEOS)
477 // Tests that command line parameters get passed through to platform apps
478 // via launchData correctly when launching with a file.
479 // TODO(benwells/jeremya): tests need a way to specify a handler ID.
480 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFile) {
481 SetCommandLineArg(kTestFilePath);
482 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file"))
483 << message_;
486 // Tests that relative paths can be passed through to the platform app.
487 // This test doesn't use the normal test infrastructure as it needs to open
488 // the application differently to all other platform app tests, by setting
489 // the AppLaunchParams.current_directory field.
490 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithRelativeFile) {
491 // Setup the command line
492 ClearCommandLineArgs();
493 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
494 base::FilePath relative_test_doc =
495 base::FilePath::FromUTF8Unsafe(kTestFilePath);
496 relative_test_doc = relative_test_doc.NormalizePathSeparators();
497 command_line->AppendArgPath(relative_test_doc);
499 // Load the extension
500 ResultCatcher catcher;
501 const Extension* extension = LoadExtension(
502 test_data_dir_.AppendASCII("platform_apps/launch_file"));
503 ASSERT_TRUE(extension);
505 // Run the test
506 AppLaunchParams params(browser()->profile(), extension, LAUNCH_CONTAINER_NONE,
507 NEW_WINDOW, extensions::SOURCE_TEST);
508 params.command_line = *base::CommandLine::ForCurrentProcess();
509 params.current_directory = test_data_dir_;
510 OpenApplication(params);
512 if (!catcher.GetNextResult()) {
513 message_ = catcher.message();
514 ASSERT_TRUE(0);
518 // Tests that launch data is sent through if the file extension matches.
519 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFileExtension) {
520 SetCommandLineArg(kTestFilePath);
521 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file_by_extension"))
522 << message_;
525 // Tests that launch data is sent through to a whitelisted extension if the file
526 // extension matches.
527 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
528 LaunchWhiteListedExtensionWithFile) {
529 SetCommandLineArg(kTestFilePath);
530 ASSERT_TRUE(RunPlatformAppTest(
531 "platform_apps/launch_whitelisted_ext_with_file"))
532 << message_;
535 // Tests that launch data is sent through if the file extension and MIME type
536 // both match.
537 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
538 LaunchWithFileExtensionAndMimeType) {
539 SetCommandLineArg(kTestFilePath);
540 ASSERT_TRUE(RunPlatformAppTest(
541 "platform_apps/launch_file_by_extension_and_type")) << message_;
544 // Tests that launch data is sent through for a file with no extension if a
545 // handler accepts "".
546 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFileWithoutExtension) {
547 SetCommandLineArg("platform_apps/launch_files/test");
548 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file_with_no_extension"))
549 << message_;
552 #if !defined(OS_WIN)
553 // Tests that launch data is sent through for a file with an empty extension if
554 // a handler accepts "".
555 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFileEmptyExtension) {
556 base::ScopedTempDir temp_dir;
557 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
558 ClearCommandLineArgs();
559 ASSERT_TRUE(CopyTestDataAndSetCommandLineArg(
560 test_data_dir_.AppendASCII(kTestFilePath),
561 temp_dir.path(),
562 "test."));
563 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file_with_no_extension"))
564 << message_;
567 // Tests that launch data is sent through for a file with an empty extension if
568 // a handler accepts *.
569 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
570 LaunchWithFileEmptyExtensionAcceptAny) {
571 base::ScopedTempDir temp_dir;
572 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
573 ClearCommandLineArgs();
574 ASSERT_TRUE(CopyTestDataAndSetCommandLineArg(
575 test_data_dir_.AppendASCII(kTestFilePath),
576 temp_dir.path(),
577 "test."));
578 ASSERT_TRUE(RunPlatformAppTest(
579 "platform_apps/launch_file_with_any_extension")) << message_;
581 #endif
583 // Tests that launch data is sent through for a file with no extension if a
584 // handler accepts *.
585 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
586 LaunchWithFileWithoutExtensionAcceptAny) {
587 SetCommandLineArg("platform_apps/launch_files/test");
588 ASSERT_TRUE(RunPlatformAppTest(
589 "platform_apps/launch_file_with_any_extension")) << message_;
592 // Tests that launch data is sent through for a file with an extension if a
593 // handler accepts *.
594 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
595 LaunchWithFileAcceptAnyExtension) {
596 SetCommandLineArg(kTestFilePath);
597 ASSERT_TRUE(RunPlatformAppTest(
598 "platform_apps/launch_file_with_any_extension")) << message_;
601 // Tests that no launch data is sent through if the file has the wrong
602 // extension.
603 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongExtension) {
604 SetCommandLineArg(kTestFilePath);
605 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_extension"))
606 << message_;
609 // Tests that no launch data is sent through if the file has no extension but
610 // the handler requires a specific extension.
611 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongEmptyExtension) {
612 SetCommandLineArg("platform_apps/launch_files/test");
613 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_extension"))
614 << message_;
617 // Tests that no launch data is sent through if the file is of the wrong MIME
618 // type.
619 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongType) {
620 SetCommandLineArg(kTestFilePath);
621 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_type"))
622 << message_;
625 // Tests that no launch data is sent through if the platform app does not
626 // provide an intent.
627 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNoIntent) {
628 SetCommandLineArg(kTestFilePath);
629 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_no_intent"))
630 << message_;
633 // Tests that launch data is sent through when the file has unknown extension
634 // but the MIME type can be sniffed and the sniffed type matches.
635 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithSniffableType) {
636 SetCommandLineArg("platform_apps/launch_files/test.unknownextension");
637 ASSERT_TRUE(RunPlatformAppTest(
638 "platform_apps/launch_file_by_extension_and_type")) << message_;
641 // Tests that launch data is sent through with the MIME type set to
642 // application/octet-stream if the file MIME type cannot be read.
643 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoType) {
644 SetCommandLineArg("platform_apps/launch_files/test_binary.unknownextension");
645 ASSERT_TRUE(RunPlatformAppTest(
646 "platform_apps/launch_application_octet_stream")) << message_;
649 // Tests that no launch data is sent through if the file does not exist.
650 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoFile) {
651 SetCommandLineArg("platform_apps/launch_files/doesnotexist.txt");
652 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
653 << message_;
656 // Tests that no launch data is sent through if the argument is a directory.
657 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithDirectory) {
658 SetCommandLineArg("platform_apps/launch_files");
659 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
660 << message_;
663 // Tests that no launch data is sent through if there are no arguments passed
664 // on the command line
665 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNothing) {
666 ClearCommandLineArgs();
667 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_nothing"))
668 << message_;
671 // Test that platform apps can use the chrome.fileSystem.getDisplayPath
672 // function to get the native file system path of a file they are launched with.
673 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, GetDisplayPath) {
674 SetCommandLineArg(kTestFilePath);
675 ASSERT_TRUE(RunPlatformAppTest("platform_apps/get_display_path"))
676 << message_;
679 // Tests that the file is created if the file does not exist and the app has the
680 // fileSystem.write permission.
681 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNewFile) {
682 base::ScopedTempDir temp_dir;
683 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
684 ClearCommandLineArgs();
685 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
686 command_line->AppendArgPath(temp_dir.path().AppendASCII("new_file.txt"));
687 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_new_file")) << message_;
690 #endif // !defined(OS_CHROMEOS)
692 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OpenLink) {
693 ASSERT_TRUE(StartEmbeddedTestServer());
694 content::WindowedNotificationObserver observer(
695 chrome::NOTIFICATION_TAB_ADDED,
696 content::Source<content::WebContentsDelegate>(browser()));
697 LoadAndLaunchPlatformApp("open_link", "Launched");
698 observer.Wait();
699 ASSERT_EQ(2, browser()->tab_strip_model()->count());
702 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MutationEventsDisabled) {
703 ASSERT_TRUE(RunPlatformAppTest("platform_apps/mutation_events")) << message_;
706 // This appears to be unreliable on linux.
707 // TODO(stevenjb): Investigate and enable
708 #if defined(OS_LINUX) && !defined(USE_ASH)
709 #define MAYBE_AppWindowRestoreState DISABLED_AppWindowRestoreState
710 #else
711 #define MAYBE_AppWindowRestoreState AppWindowRestoreState
712 #endif
713 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_AppWindowRestoreState) {
714 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restore_state"));
717 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
718 AppWindowAdjustBoundsToBeVisibleOnScreen) {
719 const Extension* extension = LoadAndLaunchPlatformApp("minimal", "Launched");
721 AppWindow* window = CreateAppWindow(extension);
723 // The screen bounds didn't change, the cached bounds didn't need to adjust.
724 gfx::Rect cached_bounds(80, 100, 400, 400);
725 gfx::Rect cached_screen_bounds(0, 0, 1600, 900);
726 gfx::Rect current_screen_bounds(0, 0, 1600, 900);
727 gfx::Size minimum_size(200, 200);
728 gfx::Rect bounds;
729 CallAdjustBoundsToBeVisibleOnScreenForAppWindow(window,
730 cached_bounds,
731 cached_screen_bounds,
732 current_screen_bounds,
733 minimum_size,
734 &bounds);
735 EXPECT_EQ(bounds, cached_bounds);
737 // We have an empty screen bounds, the cached bounds didn't need to adjust.
738 gfx::Rect empty_screen_bounds;
739 CallAdjustBoundsToBeVisibleOnScreenForAppWindow(window,
740 cached_bounds,
741 empty_screen_bounds,
742 current_screen_bounds,
743 minimum_size,
744 &bounds);
745 EXPECT_EQ(bounds, cached_bounds);
747 // Cached bounds is completely off the new screen bounds in horizontal
748 // locations. Expect to reposition the bounds.
749 gfx::Rect horizontal_out_of_screen_bounds(-800, 100, 400, 400);
750 CallAdjustBoundsToBeVisibleOnScreenForAppWindow(
751 window,
752 horizontal_out_of_screen_bounds,
753 gfx::Rect(-1366, 0, 1600, 900),
754 current_screen_bounds,
755 minimum_size,
756 &bounds);
757 EXPECT_EQ(bounds, gfx::Rect(0, 100, 400, 400));
759 // Cached bounds is completely off the new screen bounds in vertical
760 // locations. Expect to reposition the bounds.
761 gfx::Rect vertical_out_of_screen_bounds(10, 1000, 400, 400);
762 CallAdjustBoundsToBeVisibleOnScreenForAppWindow(
763 window,
764 vertical_out_of_screen_bounds,
765 gfx::Rect(-1366, 0, 1600, 900),
766 current_screen_bounds,
767 minimum_size,
768 &bounds);
769 EXPECT_EQ(bounds, gfx::Rect(10, 500, 400, 400));
771 // From a large screen resulotion to a small one. Expect it fit on screen.
772 gfx::Rect big_cache_bounds(10, 10, 1000, 1000);
773 CallAdjustBoundsToBeVisibleOnScreenForAppWindow(window,
774 big_cache_bounds,
775 gfx::Rect(0, 0, 1600, 1000),
776 gfx::Rect(0, 0, 800, 600),
777 minimum_size,
778 &bounds);
779 EXPECT_EQ(bounds, gfx::Rect(0, 0, 800, 600));
781 // Don't resize the bounds smaller than minimum size, when the minimum size is
782 // larger than the screen.
783 CallAdjustBoundsToBeVisibleOnScreenForAppWindow(window,
784 big_cache_bounds,
785 gfx::Rect(0, 0, 1600, 1000),
786 gfx::Rect(0, 0, 800, 600),
787 gfx::Size(900, 900),
788 &bounds);
789 EXPECT_EQ(bounds, gfx::Rect(0, 0, 900, 900));
792 namespace {
794 class PlatformAppDevToolsBrowserTest : public PlatformAppBrowserTest {
795 protected:
796 enum TestFlags {
797 RELAUNCH = 0x1,
798 HAS_ID = 0x2,
800 // Runs a test inside a harness that opens DevTools on an app window.
801 void RunTestWithDevTools(const char* name, int test_flags);
804 void PlatformAppDevToolsBrowserTest::RunTestWithDevTools(
805 const char* name, int test_flags) {
806 using content::DevToolsAgentHost;
807 const Extension* extension = LoadAndLaunchPlatformApp(name, "Launched");
808 ASSERT_TRUE(extension);
809 AppWindow* window = GetFirstAppWindow();
810 ASSERT_TRUE(window);
811 ASSERT_EQ(window->window_key().empty(), (test_flags & HAS_ID) == 0);
812 content::WebContents* web_contents = window->web_contents();
813 ASSERT_TRUE(web_contents);
815 // Ensure no DevTools open for the AppWindow, then open one.
816 ASSERT_FALSE(DevToolsAgentHost::HasFor(web_contents));
817 DevToolsWindow::OpenDevToolsWindow(web_contents);
818 ASSERT_TRUE(DevToolsAgentHost::HasFor(web_contents));
820 if (test_flags & RELAUNCH) {
821 // Close the AppWindow, and ensure it is gone.
822 CloseAppWindow(window);
823 ASSERT_FALSE(GetFirstAppWindow());
825 // Relaunch the app and get a new AppWindow.
826 content::WindowedNotificationObserver app_loaded_observer(
827 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
828 content::NotificationService::AllSources());
829 OpenApplication(AppLaunchParams(browser()->profile(), extension,
830 LAUNCH_CONTAINER_NONE, NEW_WINDOW,
831 extensions::SOURCE_TEST));
832 app_loaded_observer.Wait();
833 window = GetFirstAppWindow();
834 ASSERT_TRUE(window);
836 // DevTools should have reopened with the relaunch.
837 web_contents = window->web_contents();
838 ASSERT_TRUE(web_contents);
839 ASSERT_TRUE(DevToolsAgentHost::HasFor(web_contents));
843 } // namespace
845 // http://crbug.com/246634
846 #if defined(OS_CHROMEOS)
847 #define MAYBE_ReOpenedWithID DISABLED_ReOpenedWithID
848 #else
849 #define MAYBE_ReOpenedWithID ReOpenedWithID
850 #endif
851 IN_PROC_BROWSER_TEST_F(PlatformAppDevToolsBrowserTest, MAYBE_ReOpenedWithID) {
852 #if defined(OS_WIN) && defined(USE_ASH)
853 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
854 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
855 switches::kAshBrowserTests))
856 return;
857 #endif
858 RunTestWithDevTools("minimal_id", RELAUNCH | HAS_ID);
861 // http://crbug.com/246999
862 #if defined(OS_CHROMEOS) || defined(OS_WIN)
863 #define MAYBE_ReOpenedWithURL DISABLED_ReOpenedWithURL
864 #else
865 #define MAYBE_ReOpenedWithURL ReOpenedWithURL
866 #endif
867 IN_PROC_BROWSER_TEST_F(PlatformAppDevToolsBrowserTest, MAYBE_ReOpenedWithURL) {
868 RunTestWithDevTools("minimal", RELAUNCH);
871 // Test that showing a permission request as a constrained window works and is
872 // correctly parented.
873 #if defined(OS_MACOSX)
874 #define MAYBE_ConstrainedWindowRequest DISABLED_ConstrainedWindowRequest
875 #else
876 // TODO(sail): Enable this on other platforms once http://crbug.com/95455 is
877 // fixed.
878 #define MAYBE_ConstrainedWindowRequest DISABLED_ConstrainedWindowRequest
879 #endif
881 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_ConstrainedWindowRequest) {
882 PermissionsRequestFunction::SetIgnoreUserGestureForTests(true);
883 const Extension* extension =
884 LoadAndLaunchPlatformApp("optional_permission_request", "Launched");
885 ASSERT_TRUE(extension) << "Failed to load extension.";
887 WebContents* web_contents = GetFirstAppWindowWebContents();
888 ASSERT_TRUE(web_contents);
890 // Verify that the app window has a dialog attached.
891 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
892 WebContentsModalDialogManager::FromWebContents(web_contents);
893 EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive());
895 // Close the constrained window and wait for the reply to the permission
896 // request.
897 ExtensionTestMessageListener listener("PermissionRequestDone", false);
898 WebContentsModalDialogManager::TestApi test_api(
899 web_contents_modal_dialog_manager);
900 test_api.CloseAllDialogs();
901 ASSERT_TRUE(listener.WaitUntilSatisfied());
904 // Tests that an app calling chrome.runtime.reload will reload the app and
905 // relaunch it if it was running.
906 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ReloadRelaunches) {
907 ExtensionTestMessageListener launched_listener("Launched", true);
908 const Extension* extension =
909 LoadAndLaunchPlatformApp("reload", &launched_listener);
910 ASSERT_TRUE(extension);
911 ASSERT_TRUE(GetFirstAppWindow());
913 // Now tell the app to reload itself
914 ExtensionTestMessageListener launched_listener2("Launched", false);
915 launched_listener.Reply("reload");
916 ASSERT_TRUE(launched_listener2.WaitUntilSatisfied());
917 ASSERT_TRUE(GetFirstAppWindow());
920 namespace {
922 // Simple observer to check for
923 // NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED events to ensure
924 // installation does or does not occur in certain scenarios.
925 class CheckExtensionInstalledObserver : public content::NotificationObserver {
926 public:
927 CheckExtensionInstalledObserver() : seen_(false) {
928 registrar_.Add(
929 this,
930 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
931 content::NotificationService::AllSources());
934 bool seen() const {
935 return seen_;
938 // NotificationObserver:
939 void Observe(int type,
940 const content::NotificationSource& source,
941 const content::NotificationDetails& details) override {
942 EXPECT_FALSE(seen_);
943 seen_ = true;
946 private:
947 bool seen_;
948 content::NotificationRegistrar registrar_;
951 } // namespace
953 // Component App Test 1 of 3: ensure that the initial load of a component
954 // extension utilizing a background page (e.g. a v2 platform app) has its
955 // background page run and is launchable. Waits for the Launched response from
956 // the script resource in the opened app window.
957 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
958 PRE_PRE_ComponentAppBackgroundPage) {
959 CheckExtensionInstalledObserver should_install;
961 // Ensure that we wait until the background page is run (to register the
962 // OnLaunched listener) before trying to open the application. This is similar
963 // to LoadAndLaunchPlatformApp, but we want to load as a component extension.
964 content::WindowedNotificationObserver app_loaded_observer(
965 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
966 content::NotificationService::AllSources());
968 const Extension* extension = LoadExtensionAsComponent(
969 test_data_dir_.AppendASCII("platform_apps").AppendASCII("component"));
970 ASSERT_TRUE(extension);
972 app_loaded_observer.Wait();
973 ASSERT_TRUE(should_install.seen());
975 ExtensionTestMessageListener launched_listener("Launched", false);
976 OpenApplication(AppLaunchParams(browser()->profile(), extension,
977 LAUNCH_CONTAINER_NONE, NEW_WINDOW,
978 extensions::SOURCE_TEST));
980 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
983 // Component App Test 2 of 3: ensure an installed component app can be launched
984 // on a subsequent browser start, without requiring any install/upgrade logic
985 // to be run, then perform setup for step 3.
986 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
987 PRE_ComponentAppBackgroundPage) {
988 // Since the component app is now installed, re-adding it in the same profile
989 // should not cause it to be re-installed. Instead, we wait for the OnLaunched
990 // in a different observer (which would timeout if not the app was not
991 // previously installed properly) and then check this observer to make sure it
992 // never saw the NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED event.
993 CheckExtensionInstalledObserver should_not_install;
994 const Extension* extension = LoadExtensionAsComponent(
995 test_data_dir_.AppendASCII("platform_apps").AppendASCII("component"));
996 ASSERT_TRUE(extension);
998 ExtensionTestMessageListener launched_listener("Launched", false);
999 OpenApplication(AppLaunchParams(browser()->profile(), extension,
1000 LAUNCH_CONTAINER_NONE, NEW_WINDOW,
1001 extensions::SOURCE_TEST));
1003 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1004 ASSERT_FALSE(should_not_install.seen());
1006 // Simulate a "downgrade" from version 2 in the test manifest.json to 1.
1007 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser()->profile());
1009 // Clear the registered events to ensure they are updated.
1010 extensions::EventRouter::Get(browser()->profile())
1011 ->SetRegisteredEvents(extension->id(), std::set<std::string>());
1013 DictionaryPrefUpdate update(extension_prefs->pref_service(),
1014 extensions::pref_names::kExtensions);
1015 base::DictionaryValue* dict = update.Get();
1016 std::string key(extension->id());
1017 key += ".manifest.version";
1018 dict->SetString(key, "1");
1021 // Component App Test 3 of 3: simulate a component extension upgrade that
1022 // re-adds the OnLaunched event, and allows the app to be launched.
1023 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ComponentAppBackgroundPage) {
1024 CheckExtensionInstalledObserver should_install;
1025 // Since we are forcing an upgrade, we need to wait for the load again.
1026 content::WindowedNotificationObserver app_loaded_observer(
1027 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
1028 content::NotificationService::AllSources());
1030 const Extension* extension = LoadExtensionAsComponent(
1031 test_data_dir_.AppendASCII("platform_apps").AppendASCII("component"));
1032 ASSERT_TRUE(extension);
1033 app_loaded_observer.Wait();
1034 ASSERT_TRUE(should_install.seen());
1036 ExtensionTestMessageListener launched_listener("Launched", false);
1037 OpenApplication(AppLaunchParams(browser()->profile(), extension,
1038 LAUNCH_CONTAINER_NONE, NEW_WINDOW,
1039 extensions::SOURCE_TEST));
1041 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1044 // Disabled due to flakiness. http://crbug.com/468609
1045 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
1046 DISABLED_ComponentExtensionRuntimeReload) {
1047 // Ensure that we wait until the background page is run (to register the
1048 // OnLaunched listener) before trying to open the application. This is similar
1049 // to LoadAndLaunchPlatformApp, but we want to load as a component extension.
1050 content::WindowedNotificationObserver app_loaded_observer(
1051 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
1052 content::NotificationService::AllSources());
1054 const Extension* extension = LoadExtensionAsComponent(
1055 test_data_dir_.AppendASCII("platform_apps").AppendASCII("component"));
1056 ASSERT_TRUE(extension);
1058 app_loaded_observer.Wait();
1061 ExtensionTestMessageListener launched_listener("Launched", false);
1062 OpenApplication(AppLaunchParams(browser()->profile(), extension,
1063 LAUNCH_CONTAINER_NONE, NEW_WINDOW,
1064 extensions::SOURCE_TEST));
1065 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1069 ExtensionTestMessageListener launched_listener("Launched", false);
1070 ASSERT_TRUE(ExecuteScriptInBackgroundPageNoWait(
1071 extension->id(),
1072 // NoWait actually waits for a domAutomationController.send() which is
1073 // implicitly append to the script. Since reload() restarts the
1074 // extension, the send after reload may not get executed. To get around
1075 // this, send first, then execute the reload().
1076 "window.domAutomationController.send(0);"
1077 "chrome.runtime.reload();"));
1078 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1082 // Fails on Win7. http://crbug.com/171450
1083 #if defined(OS_WIN)
1084 #define MAYBE_Messaging DISABLED_Messaging
1085 #else
1086 #define MAYBE_Messaging Messaging
1087 #endif
1088 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_Messaging) {
1089 ResultCatcher result_catcher;
1090 LoadAndLaunchPlatformApp("messaging/app2", "Ready");
1091 LoadAndLaunchPlatformApp("messaging/app1", "Launched");
1092 EXPECT_TRUE(result_catcher.GetNextResult());
1095 // This test depends on focus and so needs to be in interactive_ui_tests.
1096 // http://crbug.com/227041
1097 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DISABLED_WebContentsHasFocus) {
1098 LoadAndLaunchPlatformApp("minimal", "Launched");
1100 EXPECT_EQ(1LU, GetAppWindowCount());
1101 EXPECT_TRUE(GetFirstAppWindow()
1102 ->web_contents()
1103 ->GetRenderWidgetHostView()
1104 ->HasFocus());
1107 #if defined(ENABLE_PRINT_PREVIEW)
1109 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX)
1110 #define MAYBE_WindowDotPrintShouldBringUpPrintPreview \
1111 DISABLED_WindowDotPrintShouldBringUpPrintPreview
1112 #else
1113 #define MAYBE_WindowDotPrintShouldBringUpPrintPreview \
1114 WindowDotPrintShouldBringUpPrintPreview
1115 #endif
1117 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
1118 MAYBE_WindowDotPrintShouldBringUpPrintPreview) {
1119 ScopedPreviewTestingDelegate preview_delegate(true);
1120 ASSERT_TRUE(RunPlatformAppTest("platform_apps/print_api")) << message_;
1121 preview_delegate.WaitUntilPreviewIsReady();
1124 // This test verifies that http://crbug.com/297179 is fixed.
1125 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
1126 DISABLED_ClosingWindowWhilePrintingShouldNotCrash) {
1127 ScopedPreviewTestingDelegate preview_delegate(false);
1128 ASSERT_TRUE(RunPlatformAppTest("platform_apps/print_api")) << message_;
1129 preview_delegate.WaitUntilPreviewIsReady();
1130 GetFirstAppWindow()->GetBaseWindow()->Close();
1133 // This test currently only passes on OS X (on other platforms the print preview
1134 // dialog's size is limited by the size of the window being printed).
1135 #if !defined(OS_MACOSX)
1136 #define MAYBE_PrintPreviewShouldNotBeTooSmall \
1137 DISABLED_PrintPreviewShouldNotBeTooSmall
1138 #else
1139 #define MAYBE_PrintPreviewShouldNotBeTooSmall \
1140 PrintPreviewShouldNotBeTooSmall
1141 #endif
1143 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
1144 MAYBE_PrintPreviewShouldNotBeTooSmall) {
1145 // Print preview dialogs with widths less than 410 pixels will have preview
1146 // areas that are too small, and ones with heights less than 191 pixels will
1147 // have vertical scrollers for their controls that are too small.
1148 gfx::Size minimum_dialog_size(410, 191);
1149 ScopedPreviewTestingDelegate preview_delegate(false);
1150 ASSERT_TRUE(RunPlatformAppTest("platform_apps/print_api")) << message_;
1151 preview_delegate.WaitUntilPreviewIsReady();
1152 EXPECT_GE(preview_delegate.dialog_size().width(),
1153 minimum_dialog_size.width());
1154 EXPECT_GE(preview_delegate.dialog_size().height(),
1155 minimum_dialog_size.height());
1156 GetFirstAppWindow()->GetBaseWindow()->Close();
1158 #endif // ENABLE_PRINT_PREVIEW
1160 #if defined(OS_CHROMEOS)
1162 class PlatformAppIncognitoBrowserTest : public PlatformAppBrowserTest,
1163 public AppWindowRegistry::Observer {
1164 public:
1165 void SetUpCommandLine(base::CommandLine* command_line) override {
1166 // Tell chromeos to launch in Guest mode, aka incognito.
1167 command_line->AppendSwitch(switches::kIncognito);
1168 PlatformAppBrowserTest::SetUpCommandLine(command_line);
1170 void SetUp() override {
1171 // Make sure the file manager actually gets loaded.
1172 ComponentLoader::EnableBackgroundExtensionsForTesting();
1173 PlatformAppBrowserTest::SetUp();
1176 // AppWindowRegistry::Observer implementation.
1177 void OnAppWindowAdded(AppWindow* app_window) override {
1178 opener_app_ids_.insert(app_window->extension_id());
1181 protected:
1182 // A set of ids of apps we've seen open a app window.
1183 std::set<std::string> opener_app_ids_;
1186 IN_PROC_BROWSER_TEST_F(PlatformAppIncognitoBrowserTest, IncognitoComponentApp) {
1187 // Get the file manager app.
1188 const Extension* file_manager = extension_service()->GetExtensionById(
1189 "hhaomjibdihmijegdhdafkllkbggdgoj", false);
1190 ASSERT_TRUE(file_manager != NULL);
1191 Profile* incognito_profile = profile()->GetOffTheRecordProfile();
1192 ASSERT_TRUE(incognito_profile != NULL);
1194 // Wait until the file manager has had a chance to register its listener
1195 // for the launch event.
1196 EventRouter* router = EventRouter::Get(incognito_profile);
1197 ASSERT_TRUE(router != NULL);
1198 while (!router->ExtensionHasEventListener(
1199 file_manager->id(), app_runtime::OnLaunched::kEventName)) {
1200 content::RunAllPendingInMessageLoop();
1203 // Listen for new app windows so we see the file manager app launch itself.
1204 AppWindowRegistry* registry = AppWindowRegistry::Get(incognito_profile);
1205 ASSERT_TRUE(registry != NULL);
1206 registry->AddObserver(this);
1208 OpenApplication(AppLaunchParams(incognito_profile, file_manager, CURRENT_TAB,
1209 chrome::HOST_DESKTOP_TYPE_NATIVE,
1210 extensions::SOURCE_TEST));
1212 while (!ContainsKey(opener_app_ids_, file_manager->id())) {
1213 content::RunAllPendingInMessageLoop();
1217 class RestartDeviceTest : public PlatformAppBrowserTest {
1218 public:
1219 RestartDeviceTest()
1220 : power_manager_client_(NULL),
1221 mock_user_manager_(NULL) {}
1222 ~RestartDeviceTest() override {}
1224 // PlatformAppBrowserTest overrides
1225 void SetUpInProcessBrowserTestFixture() override {
1226 PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture();
1228 power_manager_client_ = new chromeos::FakePowerManagerClient;
1229 chromeos::DBusThreadManager::GetSetterForTesting()->SetPowerManagerClient(
1230 scoped_ptr<chromeos::PowerManagerClient>(power_manager_client_));
1233 void SetUpOnMainThread() override {
1234 PlatformAppBrowserTest::SetUpOnMainThread();
1236 mock_user_manager_ = new chromeos::MockUserManager;
1237 user_manager_enabler_.reset(
1238 new chromeos::ScopedUserManagerEnabler(mock_user_manager_));
1240 EXPECT_CALL(*mock_user_manager_, IsUserLoggedIn())
1241 .WillRepeatedly(testing::Return(true));
1242 EXPECT_CALL(*mock_user_manager_, IsLoggedInAsKioskApp())
1243 .WillRepeatedly(testing::Return(true));
1246 void TearDownOnMainThread() override {
1247 user_manager_enabler_.reset();
1248 PlatformAppBrowserTest::TearDownOnMainThread();
1251 void TearDownInProcessBrowserTestFixture() override {
1252 PlatformAppBrowserTest::TearDownInProcessBrowserTestFixture();
1255 int num_request_restart_calls() const {
1256 return power_manager_client_->num_request_restart_calls();
1259 private:
1260 chromeos::FakePowerManagerClient* power_manager_client_;
1261 chromeos::MockUserManager* mock_user_manager_;
1262 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
1264 DISALLOW_COPY_AND_ASSIGN(RestartDeviceTest);
1267 // Tests that chrome.runtime.restart would request device restart in
1268 // ChromeOS kiosk mode.
1269 IN_PROC_BROWSER_TEST_F(RestartDeviceTest, Restart) {
1270 ASSERT_EQ(0, num_request_restart_calls());
1272 ExtensionTestMessageListener launched_listener("Launched", true);
1273 const Extension* extension = LoadAndLaunchPlatformApp("restart_device",
1274 &launched_listener);
1275 ASSERT_TRUE(extension);
1277 launched_listener.Reply("restart");
1278 ExtensionTestMessageListener restart_requested_listener("restartRequested",
1279 false);
1280 ASSERT_TRUE(restart_requested_listener.WaitUntilSatisfied());
1282 EXPECT_EQ(1, num_request_restart_calls());
1285 #endif // defined(OS_CHROMEOS)
1287 // Test that when an application is uninstalled and re-install it does not have
1288 // access to the previously set data.
1289 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ReinstallDataCleanup) {
1290 // The application is installed and launched. After the 'Launched' message is
1291 // acknowledged by the browser process, the application will test that some
1292 // data are not installed and then install them. The application will then be
1293 // uninstalled and the same process will be repeated.
1294 std::string extension_id;
1297 const Extension* extension =
1298 LoadAndLaunchPlatformApp("reinstall_data_cleanup", "Launched");
1299 ASSERT_TRUE(extension);
1300 extension_id = extension->id();
1302 ResultCatcher result_catcher;
1303 EXPECT_TRUE(result_catcher.GetNextResult());
1306 UninstallExtension(extension_id);
1307 content::RunAllPendingInMessageLoop();
1310 const Extension* extension =
1311 LoadAndLaunchPlatformApp("reinstall_data_cleanup", "Launched");
1312 ASSERT_TRUE(extension);
1313 ASSERT_EQ(extension_id, extension->id());
1315 ResultCatcher result_catcher;
1316 EXPECT_TRUE(result_catcher.GetNextResult());
1320 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppsIgnoreDefaultZoom) {
1321 const Extension* extension = LoadAndLaunchPlatformApp("minimal", "Launched");
1323 // Set the browser default zoom to something other than the default (which is
1324 // 0).
1325 browser()->profile()->GetZoomLevelPrefs()->SetDefaultZoomLevelPref(1);
1327 // Launch another window. This is a simple way to guarantee that any messages
1328 // that would have been delivered to the app renderer and back for zoom have
1329 // made it through.
1330 ExtensionTestMessageListener launched_listener("Launched", false);
1331 LaunchPlatformApp(extension);
1332 launched_listener.WaitUntilSatisfied();
1334 // Now check that the app window's default zoom, and actual zoom level,
1335 // have not been changed from the default.
1336 WebContents* web_contents = GetFirstAppWindowWebContents();
1337 content::HostZoomMap* app_host_zoom_map = content::HostZoomMap::Get(
1338 web_contents->GetSiteInstance());
1339 EXPECT_EQ(0, app_host_zoom_map->GetDefaultZoomLevel());
1340 EXPECT_EQ(0, app_host_zoom_map->GetZoomLevel(web_contents));
1343 } // namespace extensions