Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / apps / app_browsertest.cc
bloba871a1f8e749f4f9486347476da8db2873b8c819
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 "apps/shell_window.h"
7 #include "apps/shell_window_registry.h"
8 #include "apps/ui/native_app_window.h"
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/stl_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/app/chrome_command_ids.h"
17 #include "chrome/browser/apps/app_browsertest_util.h"
18 #include "chrome/browser/automation/automation_util.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/devtools/devtools_window.h"
21 #include "chrome/browser/extensions/api/permissions/permissions_api.h"
22 #include "chrome/browser/extensions/component_loader.h"
23 #include "chrome/browser/extensions/extension_browsertest.h"
24 #include "chrome/browser/extensions/extension_service.h"
25 #include "chrome/browser/extensions/extension_system.h"
26 #include "chrome/browser/extensions/extension_test_message_listener.h"
27 #include "chrome/browser/tab_contents/render_view_context_menu.h"
28 #include "chrome/browser/ui/browser.h"
29 #include "chrome/browser/ui/extensions/application_launch.h"
30 #include "chrome/browser/ui/tabs/tab_strip_model.h"
31 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/extensions/api/app_runtime.h"
34 #include "chrome/common/pref_names.h"
35 #include "chrome/common/url_constants.h"
36 #include "chrome/test/base/test_switches.h"
37 #include "chrome/test/base/ui_test_utils.h"
38 #include "components/user_prefs/pref_registry_syncable.h"
39 #include "components/web_modal/web_contents_modal_dialog_manager.h"
40 #include "content/public/browser/devtools_agent_host.h"
41 #include "content/public/browser/render_process_host.h"
42 #include "content/public/browser/render_widget_host_view.h"
43 #include "content/public/browser/web_contents_view.h"
44 #include "content/public/test/test_utils.h"
45 #include "extensions/browser/event_router.h"
46 #include "extensions/browser/pref_names.h"
47 #include "net/test/embedded_test_server/embedded_test_server.h"
48 #include "url/gurl.h"
50 #if defined(OS_CHROMEOS)
51 #include "base/memory/scoped_ptr.h"
52 #include "chrome/browser/chromeos/login/mock_user_manager.h"
53 #include "chrome/browser/chromeos/login/user_manager.h"
54 #include "chromeos/dbus/dbus_thread_manager.h"
55 #include "chromeos/dbus/fake_dbus_thread_manager.h"
56 #include "chromeos/dbus/fake_power_manager_client.h"
57 #endif
59 using apps::ShellWindow;
60 using apps::ShellWindowRegistry;
61 using content::WebContents;
62 using web_modal::WebContentsModalDialogManager;
64 namespace extensions {
66 namespace app_runtime = api::app_runtime;
68 namespace {
70 // Non-abstract RenderViewContextMenu class.
71 class PlatformAppContextMenu : public RenderViewContextMenu {
72 public:
73 PlatformAppContextMenu(WebContents* web_contents,
74 const content::ContextMenuParams& params)
75 : RenderViewContextMenu(web_contents, params) {}
77 bool HasCommandWithId(int command_id) {
78 return menu_model_.GetIndexOfCommandId(command_id) != -1;
81 protected:
82 // RenderViewContextMenu implementation.
83 virtual bool GetAcceleratorForCommandId(
84 int command_id,
85 ui::Accelerator* accelerator) OVERRIDE {
86 return false;
88 virtual void PlatformInit() OVERRIDE {}
89 virtual void PlatformCancel() OVERRIDE {}
92 // This class keeps track of tabs as they are added to the browser. It will be
93 // "done" (i.e. won't block on Wait()) once |observations| tabs have been added.
94 class TabsAddedNotificationObserver
95 : public content::WindowedNotificationObserver {
96 public:
97 explicit TabsAddedNotificationObserver(size_t observations)
98 : content::WindowedNotificationObserver(
99 chrome::NOTIFICATION_TAB_ADDED,
100 content::NotificationService::AllSources()),
101 observations_(observations) {
104 virtual void Observe(int type,
105 const content::NotificationSource& source,
106 const content::NotificationDetails& details) OVERRIDE {
107 observed_tabs_.push_back(
108 content::Details<WebContents>(details).ptr());
109 if (observed_tabs_.size() == observations_)
110 content::WindowedNotificationObserver::Observe(type, source, details);
113 const std::vector<content::WebContents*>& tabs() { return observed_tabs_; }
115 private:
116 size_t observations_;
117 std::vector<content::WebContents*> observed_tabs_;
119 DISALLOW_COPY_AND_ASSIGN(TabsAddedNotificationObserver);
122 class ScopedPreviewTestingDelegate : PrintPreviewUI::TestingDelegate {
123 public:
124 explicit ScopedPreviewTestingDelegate(bool auto_cancel)
125 : auto_cancel_(auto_cancel),
126 total_page_count_(1),
127 rendered_page_count_(0) {
128 PrintPreviewUI::SetDelegateForTesting(this);
131 ~ScopedPreviewTestingDelegate() {
132 PrintPreviewUI::SetDelegateForTesting(NULL);
135 // PrintPreviewUI::TestingDelegate implementation.
136 virtual bool IsAutoCancelEnabled() OVERRIDE {
137 return auto_cancel_;
140 // PrintPreviewUI::TestingDelegate implementation.
141 virtual void DidGetPreviewPageCount(int page_count) OVERRIDE {
142 total_page_count_ = page_count;
145 // PrintPreviewUI::TestingDelegate implementation.
146 virtual void DidRenderPreviewPage(const content::WebContents& preview_dialog)
147 OVERRIDE {
148 dialog_size_ = preview_dialog.GetView()->GetContainerSize();
149 ++rendered_page_count_;
150 CHECK(rendered_page_count_ <= total_page_count_);
151 if (waiting_runner_ && rendered_page_count_ == total_page_count_) {
152 waiting_runner_->Quit();
156 void WaitUntilPreviewIsReady() {
157 CHECK(!waiting_runner_);
158 if (rendered_page_count_ < total_page_count_) {
159 waiting_runner_ = new content::MessageLoopRunner;
160 waiting_runner_->Run();
161 waiting_runner_ = NULL;
165 gfx::Size dialog_size() {
166 return dialog_size_;
169 private:
170 bool auto_cancel_;
171 int total_page_count_;
172 int rendered_page_count_;
173 scoped_refptr<content::MessageLoopRunner> waiting_runner_;
174 gfx::Size dialog_size_;
177 #if !defined(OS_CHROMEOS) && !defined(OS_WIN)
178 bool CopyTestDataAndSetCommandLineArg(
179 const base::FilePath& test_data_file,
180 const base::FilePath& temp_dir,
181 const char* filename) {
182 base::FilePath path = temp_dir.AppendASCII(
183 filename).NormalizePathSeparators();
184 if (!(base::CopyFile(test_data_file, path)))
185 return false;
187 CommandLine* command_line = CommandLine::ForCurrentProcess();
188 command_line->AppendArgPath(path);
189 return true;
191 #endif // !defined(OS_CHROMEOS) && !defined(OS_WIN)
193 #if !defined(OS_CHROMEOS)
194 const char kTestFilePath[] = "platform_apps/launch_files/test.txt";
195 #endif
197 } // namespace
199 // Tests that CreateShellWindow doesn't crash if you close it straight away.
200 // LauncherPlatformAppBrowserTest relies on this behaviour, but is only run for
201 // ash, so we test that it works here.
202 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, CreateAndCloseShellWindow) {
203 const Extension* extension = LoadAndLaunchPlatformApp("minimal");
204 ShellWindow* window = CreateShellWindow(extension);
205 CloseShellWindow(window);
208 // Tests that platform apps received the "launch" event when launched.
209 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
210 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
213 // Tests that platform apps cannot use certain disabled window properties, but
214 // can override them and then use them.
215 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisabledWindowProperties) {
216 ASSERT_TRUE(RunPlatformAppTest("platform_apps/disabled_window_properties"))
217 << message_;
220 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
221 ExtensionTestMessageListener launched_listener("Launched", false);
222 LoadAndLaunchPlatformApp("minimal");
224 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
226 // The empty app doesn't add any context menu items, so its menu should
227 // only include the developer tools.
228 WebContents* web_contents = GetFirstShellWindowWebContents();
229 ASSERT_TRUE(web_contents);
230 content::ContextMenuParams params;
231 scoped_ptr<PlatformAppContextMenu> menu;
232 menu.reset(new PlatformAppContextMenu(web_contents, params));
233 menu->Init();
234 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
235 ASSERT_TRUE(
236 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
237 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
238 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
239 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
242 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) {
243 ExtensionTestMessageListener launched_listener("Launched", false);
244 LoadAndLaunchPlatformApp("context_menu");
246 // Wait for the extension to tell us it's initialized its context menus and
247 // launched a window.
248 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
250 // The context_menu app has two context menu items. These, along with a
251 // separator and the developer tools, is all that should be in the menu.
252 WebContents* web_contents = GetFirstShellWindowWebContents();
253 ASSERT_TRUE(web_contents);
254 content::ContextMenuParams params;
255 scoped_ptr<PlatformAppContextMenu> menu;
256 menu.reset(new PlatformAppContextMenu(web_contents, params));
257 menu->Init();
258 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
259 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + 1));
260 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
261 ASSERT_TRUE(
262 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
263 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
264 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
265 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
266 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
269 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, InstalledAppWithContextMenu) {
270 ExtensionTestMessageListener launched_listener("Launched", false);
271 InstallAndLaunchPlatformApp("context_menu");
273 // Wait for the extension to tell us it's initialized its context menus and
274 // launched a window.
275 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
277 // The context_menu app has two context menu items. For an installed app
278 // these are all that should be in the menu.
279 WebContents* web_contents = GetFirstShellWindowWebContents();
280 ASSERT_TRUE(web_contents);
281 content::ContextMenuParams params;
282 scoped_ptr<PlatformAppContextMenu> menu;
283 menu.reset(new PlatformAppContextMenu(web_contents, params));
284 menu->Init();
285 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
286 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + 1));
287 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
288 ASSERT_FALSE(
289 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
290 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
291 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
292 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
293 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
296 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuTextField) {
297 ExtensionTestMessageListener launched_listener("Launched", false);
298 LoadAndLaunchPlatformApp("context_menu");
300 // Wait for the extension to tell us it's initialized its context menus and
301 // launched a window.
302 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
304 // The context_menu app has one context menu item. This, along with a
305 // separator and the developer tools, is all that should be in the menu.
306 WebContents* web_contents = GetFirstShellWindowWebContents();
307 ASSERT_TRUE(web_contents);
308 content::ContextMenuParams params;
309 params.is_editable = true;
310 scoped_ptr<PlatformAppContextMenu> menu;
311 menu.reset(new PlatformAppContextMenu(web_contents, params));
312 menu->Init();
313 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
314 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
315 ASSERT_TRUE(
316 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
317 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
318 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
319 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
320 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
321 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
324 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuSelection) {
325 ExtensionTestMessageListener launched_listener("Launched", false);
326 LoadAndLaunchPlatformApp("context_menu");
328 // Wait for the extension to tell us it's initialized its context menus and
329 // launched a window.
330 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
332 // The context_menu app has one context menu item. This, along with a
333 // separator and the developer tools, is all that should be in the menu.
334 WebContents* web_contents = GetFirstShellWindowWebContents();
335 ASSERT_TRUE(web_contents);
336 content::ContextMenuParams params;
337 params.selection_text = base::ASCIIToUTF16("Hello World");
338 scoped_ptr<PlatformAppContextMenu> menu;
339 menu.reset(new PlatformAppContextMenu(web_contents, params));
340 menu->Init();
341 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
342 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
343 ASSERT_TRUE(
344 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
345 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
346 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
347 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
348 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
349 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
352 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuClicked) {
353 ExtensionTestMessageListener launched_listener("Launched", false);
354 LoadAndLaunchPlatformApp("context_menu_click");
356 // Wait for the extension to tell us it's initialized its context menus and
357 // launched a window.
358 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
360 // Test that the menu item shows up
361 WebContents* web_contents = GetFirstShellWindowWebContents();
362 ASSERT_TRUE(web_contents);
363 content::ContextMenuParams params;
364 params.page_url = GURL("http://foo.bar");
365 scoped_ptr<PlatformAppContextMenu> menu;
366 menu.reset(new PlatformAppContextMenu(web_contents, params));
367 menu->Init();
368 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
370 // Execute the menu item
371 ExtensionTestMessageListener onclicked_listener("onClicked fired for id1",
372 false);
373 menu->ExecuteCommand(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, 0);
375 ASSERT_TRUE(onclicked_listener.WaitUntilSatisfied());
378 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
379 // TODO(erg): linux_aura bringup: http://crbug.com/163931
380 #define MAYBE_DisallowNavigation DISABLED_DisallowNavigation
381 #else
382 #define MAYBE_DisallowNavigation DisallowNavigation
383 #endif
385 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_DisallowNavigation) {
386 TabsAddedNotificationObserver observer(2);
388 ASSERT_TRUE(StartEmbeddedTestServer());
389 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_;
391 observer.Wait();
392 ASSERT_EQ(2U, observer.tabs().size());
393 EXPECT_EQ(std::string(chrome::kExtensionInvalidRequestURL),
394 observer.tabs()[0]->GetURL().spec());
395 EXPECT_EQ("http://chromium.org/",
396 observer.tabs()[1]->GetURL().spec());
399 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Iframes) {
400 ASSERT_TRUE(StartEmbeddedTestServer());
401 ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_;
404 // Tests that localStorage and WebSQL are disabled for platform apps.
405 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) {
406 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
409 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) {
410 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_;
413 // Tests that extensions can't use platform-app-only APIs.
414 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, PlatformAppsOnly) {
415 ASSERT_TRUE(RunExtensionTestIgnoreManifestWarnings(
416 "platform_apps/apps_only")) << message_;
419 // Tests that platform apps have isolated storage by default.
420 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Isolation) {
421 ASSERT_TRUE(StartEmbeddedTestServer());
423 // Load a (non-app) page under the "localhost" origin that sets a cookie.
424 GURL set_cookie_url = embedded_test_server()->GetURL(
425 "/extensions/platform_apps/isolation/set_cookie.html");
426 GURL::Replacements replace_host;
427 std::string host_str("localhost"); // Must stay in scope with replace_host.
428 replace_host.SetHostStr(host_str);
429 set_cookie_url = set_cookie_url.ReplaceComponents(replace_host);
431 ui_test_utils::NavigateToURLWithDisposition(
432 browser(), set_cookie_url,
433 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
435 // Make sure the cookie is set.
436 int cookie_size;
437 std::string cookie_value;
438 automation_util::GetCookies(
439 set_cookie_url,
440 browser()->tab_strip_model()->GetWebContentsAt(0),
441 &cookie_size,
442 &cookie_value);
443 ASSERT_EQ("testCookie=1", cookie_value);
445 // Let the platform app request the same URL, and make sure that it doesn't
446 // see the cookie.
447 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_;
450 // See crbug.com/248441
451 #if defined(OS_WIN)
452 #define MAYBE_ExtensionWindowingApis DISABLED_ExtensionWindowingApis
453 #else
454 #define MAYBE_ExtensionWindowingApis ExtensionWindowingApis
455 #endif
457 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_ExtensionWindowingApis) {
458 // Initially there should be just the one browser window visible to the
459 // extensions API.
460 const Extension* extension = LoadExtension(
461 test_data_dir_.AppendASCII("common/background_page"));
462 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
464 // And no shell windows.
465 ASSERT_EQ(0U, GetShellWindowCount());
467 // Launch a platform app that shows a window.
468 ExtensionTestMessageListener launched_listener("Launched", false);
469 LoadAndLaunchPlatformApp("minimal");
470 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
471 ASSERT_EQ(1U, GetShellWindowCount());
472 int shell_window_id = GetFirstShellWindow()->session_id().id();
474 // But it's not visible to the extensions API, it still thinks there's just
475 // one browser window.
476 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
477 // It can't look it up by ID either
478 ASSERT_FALSE(RunGetWindowFunctionForExtension(shell_window_id, extension));
480 // The app can also only see one window (its own).
481 // TODO(jeremya): add an extension function to get a shell window by ID, and
482 // to get a list of all the shell windows, so we can test this.
484 // Launch another platform app that also shows a window.
485 ExtensionTestMessageListener launched_listener2("Launched", false);
486 LoadAndLaunchPlatformApp("context_menu");
487 ASSERT_TRUE(launched_listener2.WaitUntilSatisfied());
489 // There are two total shell windows, but each app can only see its own.
490 ASSERT_EQ(2U, GetShellWindowCount());
491 // TODO(jeremya): as above, this requires more extension functions.
494 // ChromeOS does not support passing arguments on the command line, so the tests
495 // that rely on this functionality are disabled.
496 #if !defined(OS_CHROMEOS)
497 // Tests that command line parameters get passed through to platform apps
498 // via launchData correctly when launching with a file.
499 // TODO(benwells/jeremya): tests need a way to specify a handler ID.
500 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFile) {
501 SetCommandLineArg(kTestFilePath);
502 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file"))
503 << message_;
506 // Tests that relative paths can be passed through to the platform app.
507 // This test doesn't use the normal test infrastructure as it needs to open
508 // the application differently to all other platform app tests, by setting
509 // the AppLaunchParams.current_directory field.
510 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithRelativeFile) {
511 // Setup the command line
512 ClearCommandLineArgs();
513 CommandLine* command_line = CommandLine::ForCurrentProcess();
514 base::FilePath relative_test_doc =
515 base::FilePath::FromUTF8Unsafe(kTestFilePath);
516 relative_test_doc = relative_test_doc.NormalizePathSeparators();
517 command_line->AppendArgPath(relative_test_doc);
519 // Load the extension
520 ResultCatcher catcher;
521 const Extension* extension = LoadExtension(
522 test_data_dir_.AppendASCII("platform_apps/launch_file"));
523 ASSERT_TRUE(extension);
525 // Run the test
526 AppLaunchParams params(
527 browser()->profile(), extension, LAUNCH_CONTAINER_NONE, NEW_WINDOW);
528 params.command_line = *CommandLine::ForCurrentProcess();
529 params.current_directory = test_data_dir_;
530 OpenApplication(params);
532 if (!catcher.GetNextResult()) {
533 message_ = catcher.message();
534 ASSERT_TRUE(0);
538 // Tests that launch data is sent through if the file extension matches.
539 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFileExtension) {
540 SetCommandLineArg(kTestFilePath);
541 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file_by_extension"))
542 << message_;
545 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
546 // TODO(erg): linux_aura bringup: http://crbug.com/163931
547 #define MAYBE_LaunchWithFileExtensionAndMimeType DISABLED_LaunchWithFileExtensionAndMimeType
548 #else
549 #define MAYBE_LaunchWithFileExtensionAndMimeType LaunchWithFileExtensionAndMimeType
550 #endif
552 // Tests that launch data is sent through if the file extension and MIME type
553 // both match.
554 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
555 MAYBE_LaunchWithFileExtensionAndMimeType) {
556 SetCommandLineArg(kTestFilePath);
557 ASSERT_TRUE(RunPlatformAppTest(
558 "platform_apps/launch_file_by_extension_and_type")) << message_;
561 // Tests that launch data is sent through for a file with no extension if a
562 // handler accepts "".
563 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFileWithoutExtension) {
564 SetCommandLineArg("platform_apps/launch_files/test");
565 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file_with_no_extension"))
566 << message_;
569 #if !defined(OS_WIN)
570 // Tests that launch data is sent through for a file with an empty extension if
571 // a handler accepts "".
572 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFileEmptyExtension) {
573 base::ScopedTempDir temp_dir;
574 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
575 ClearCommandLineArgs();
576 ASSERT_TRUE(CopyTestDataAndSetCommandLineArg(
577 test_data_dir_.AppendASCII(kTestFilePath),
578 temp_dir.path(),
579 "test."));
580 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file_with_no_extension"))
581 << message_;
584 // Tests that launch data is sent through for a file with an empty extension if
585 // a handler accepts *.
586 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
587 LaunchWithFileEmptyExtensionAcceptAny) {
588 base::ScopedTempDir temp_dir;
589 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
590 ClearCommandLineArgs();
591 ASSERT_TRUE(CopyTestDataAndSetCommandLineArg(
592 test_data_dir_.AppendASCII(kTestFilePath),
593 temp_dir.path(),
594 "test."));
595 ASSERT_TRUE(RunPlatformAppTest(
596 "platform_apps/launch_file_with_any_extension")) << message_;
598 #endif
600 // Tests that launch data is sent through for a file with no extension if a
601 // handler accepts *.
602 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
603 LaunchWithFileWithoutExtensionAcceptAny) {
604 SetCommandLineArg("platform_apps/launch_files/test");
605 ASSERT_TRUE(RunPlatformAppTest(
606 "platform_apps/launch_file_with_any_extension")) << message_;
609 // Tests that launch data is sent through for a file with an extension if a
610 // handler accepts *.
611 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
612 LaunchWithFileAcceptAnyExtension) {
613 SetCommandLineArg(kTestFilePath);
614 ASSERT_TRUE(RunPlatformAppTest(
615 "platform_apps/launch_file_with_any_extension")) << message_;
618 // Tests that no launch data is sent through if the file has the wrong
619 // extension.
620 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongExtension) {
621 SetCommandLineArg(kTestFilePath);
622 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_extension"))
623 << message_;
626 // Tests that no launch data is sent through if the file has no extension but
627 // the handler requires a specific extension.
628 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongEmptyExtension) {
629 SetCommandLineArg("platform_apps/launch_files/test");
630 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_extension"))
631 << message_;
634 // Tests that no launch data is sent through if the file is of the wrong MIME
635 // type.
636 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongType) {
637 SetCommandLineArg(kTestFilePath);
638 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_type"))
639 << message_;
642 // Tests that no launch data is sent through if the platform app does not
643 // provide an intent.
644 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNoIntent) {
645 SetCommandLineArg(kTestFilePath);
646 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_no_intent"))
647 << message_;
650 // Tests that launch data is sent through with the MIME type set to
651 // application/octet-stream if the file MIME type cannot be read.
652 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoType) {
653 SetCommandLineArg("platform_apps/launch_files/test.unknownextension");
654 ASSERT_TRUE(RunPlatformAppTest(
655 "platform_apps/launch_application_octet_stream")) << message_;
658 // Tests that no launch data is sent through if the file does not exist.
659 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoFile) {
660 SetCommandLineArg("platform_apps/launch_files/doesnotexist.txt");
661 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
662 << message_;
665 // Tests that no launch data is sent through if the argument is a directory.
666 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithDirectory) {
667 SetCommandLineArg("platform_apps/launch_files");
668 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
669 << message_;
672 // Tests that no launch data is sent through if there are no arguments passed
673 // on the command line
674 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNothing) {
675 ClearCommandLineArgs();
676 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_nothing"))
677 << message_;
680 // Test that platform apps can use the chrome.fileSystem.getDisplayPath
681 // function to get the native file system path of a file they are launched with.
682 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, GetDisplayPath) {
683 SetCommandLineArg(kTestFilePath);
684 ASSERT_TRUE(RunPlatformAppTest("platform_apps/get_display_path"))
685 << message_;
688 // Tests that the file is created if the file does not exist and the app has the
689 // fileSystem.write permission.
690 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNewFile) {
691 base::ScopedTempDir temp_dir;
692 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
693 ClearCommandLineArgs();
694 CommandLine* command_line = CommandLine::ForCurrentProcess();
695 command_line->AppendArgPath(temp_dir.path().AppendASCII("new_file.txt"));
696 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_new_file")) << message_;
699 #endif // !defined(OS_CHROMEOS)
701 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OpenLink) {
702 ASSERT_TRUE(StartEmbeddedTestServer());
703 content::WindowedNotificationObserver observer(
704 chrome::NOTIFICATION_TAB_ADDED,
705 content::Source<content::WebContentsDelegate>(browser()));
706 LoadAndLaunchPlatformApp("open_link");
707 observer.Wait();
708 ASSERT_EQ(2, browser()->tab_strip_model()->count());
711 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MutationEventsDisabled) {
712 ASSERT_TRUE(RunPlatformAppTest("platform_apps/mutation_events")) << message_;
715 // This appears to be unreliable on linux.
716 // TODO(stevenjb): Investigate and enable
717 #if defined(OS_LINUX) && !defined(USE_ASH)
718 #define MAYBE_ShellWindowRestoreState DISABLED_ShellWindowRestoreState
719 #else
720 #define MAYBE_ShellWindowRestoreState ShellWindowRestoreState
721 #endif
722 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
723 MAYBE_ShellWindowRestoreState) {
724 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restore_state"));
727 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
728 ShellWindowAdjustBoundsToBeVisibleOnScreen) {
729 const Extension* extension = LoadAndLaunchPlatformApp("minimal");
730 ShellWindow* window = CreateShellWindow(extension);
732 // The screen bounds didn't change, the cached bounds didn't need to adjust.
733 gfx::Rect cached_bounds(80, 100, 400, 400);
734 gfx::Rect cached_screen_bounds(0, 0, 1600, 900);
735 gfx::Rect current_screen_bounds(0, 0, 1600, 900);
736 gfx::Size minimum_size(200, 200);
737 gfx::Rect bounds;
738 CallAdjustBoundsToBeVisibleOnScreenForShellWindow(window,
739 cached_bounds,
740 cached_screen_bounds,
741 current_screen_bounds,
742 minimum_size,
743 &bounds);
744 EXPECT_EQ(bounds, cached_bounds);
746 // We have an empty screen bounds, the cached bounds didn't need to adjust.
747 gfx::Rect empty_screen_bounds;
748 CallAdjustBoundsToBeVisibleOnScreenForShellWindow(window,
749 cached_bounds,
750 empty_screen_bounds,
751 current_screen_bounds,
752 minimum_size,
753 &bounds);
754 EXPECT_EQ(bounds, cached_bounds);
756 // Cached bounds is completely off the new screen bounds in horizontal
757 // locations. Expect to reposition the bounds.
758 gfx::Rect horizontal_out_of_screen_bounds(-800, 100, 400, 400);
759 CallAdjustBoundsToBeVisibleOnScreenForShellWindow(
760 window,
761 horizontal_out_of_screen_bounds,
762 gfx::Rect(-1366, 0, 1600, 900),
763 current_screen_bounds,
764 minimum_size,
765 &bounds);
766 EXPECT_EQ(bounds, gfx::Rect(0, 100, 400, 400));
768 // Cached bounds is completely off the new screen bounds in vertical
769 // locations. Expect to reposition the bounds.
770 gfx::Rect vertical_out_of_screen_bounds(10, 1000, 400, 400);
771 CallAdjustBoundsToBeVisibleOnScreenForShellWindow(
772 window,
773 vertical_out_of_screen_bounds,
774 gfx::Rect(-1366, 0, 1600, 900),
775 current_screen_bounds,
776 minimum_size,
777 &bounds);
778 EXPECT_EQ(bounds, gfx::Rect(10, 500, 400, 400));
780 // From a large screen resulotion to a small one. Expect it fit on screen.
781 gfx::Rect big_cache_bounds(10, 10, 1000, 1000);
782 CallAdjustBoundsToBeVisibleOnScreenForShellWindow(
783 window,
784 big_cache_bounds,
785 gfx::Rect(0, 0, 1600, 1000),
786 gfx::Rect(0, 0, 800, 600),
787 minimum_size,
788 &bounds);
789 EXPECT_EQ(bounds, gfx::Rect(0, 0, 800, 600));
791 // Don't resize the bounds smaller than minimum size, when the minimum size is
792 // larger than the screen.
793 CallAdjustBoundsToBeVisibleOnScreenForShellWindow(
794 window,
795 big_cache_bounds,
796 gfx::Rect(0, 0, 1600, 1000),
797 gfx::Rect(0, 0, 800, 600),
798 gfx::Size(900, 900),
799 &bounds);
800 EXPECT_EQ(bounds, gfx::Rect(0, 0, 900, 900));
803 namespace {
805 class PlatformAppDevToolsBrowserTest : public PlatformAppBrowserTest {
806 protected:
807 enum TestFlags {
808 RELAUNCH = 0x1,
809 HAS_ID = 0x2,
811 // Runs a test inside a harness that opens DevTools on a shell window.
812 void RunTestWithDevTools(const char* name, int test_flags);
815 void PlatformAppDevToolsBrowserTest::RunTestWithDevTools(
816 const char* name, int test_flags) {
817 using content::DevToolsAgentHost;
818 ExtensionTestMessageListener launched_listener("Launched", false);
819 const Extension* extension = LoadAndLaunchPlatformApp(name);
820 ASSERT_TRUE(extension);
821 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
822 ShellWindow* window = GetFirstShellWindow();
823 ASSERT_TRUE(window);
824 ASSERT_EQ(window->window_key().empty(), (test_flags & HAS_ID) == 0);
825 content::RenderViewHost* rvh = window->web_contents()->GetRenderViewHost();
826 ASSERT_TRUE(rvh);
828 // Ensure no DevTools open for the ShellWindow, then open one.
829 ASSERT_FALSE(DevToolsAgentHost::HasFor(rvh));
830 DevToolsWindow* devtools_window = DevToolsWindow::OpenDevToolsWindow(rvh);
831 content::WindowedNotificationObserver loaded_observer(
832 content::NOTIFICATION_LOAD_STOP,
833 content::Source<content::NavigationController>(
834 &devtools_window->web_contents()->GetController()));
835 loaded_observer.Wait();
836 ASSERT_TRUE(DevToolsAgentHost::HasFor(rvh));
838 if (test_flags & RELAUNCH) {
839 // Close the ShellWindow, and ensure it is gone.
840 CloseShellWindow(window);
841 ASSERT_FALSE(GetFirstShellWindow());
843 // Relaunch the app and get a new ShellWindow.
844 content::WindowedNotificationObserver app_loaded_observer(
845 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
846 content::NotificationService::AllSources());
847 OpenApplication(AppLaunchParams(
848 browser()->profile(), extension, LAUNCH_CONTAINER_NONE, NEW_WINDOW));
849 app_loaded_observer.Wait();
850 window = GetFirstShellWindow();
851 ASSERT_TRUE(window);
853 // DevTools should have reopened with the relaunch.
854 rvh = window->web_contents()->GetRenderViewHost();
855 ASSERT_TRUE(rvh);
856 ASSERT_TRUE(DevToolsAgentHost::HasFor(rvh));
860 } // namespace
862 // http://crbug.com/246634
863 #if defined(OS_CHROMEOS)
864 #define MAYBE_ReOpenedWithID DISABLED_ReOpenedWithID
865 #else
866 #define MAYBE_ReOpenedWithID ReOpenedWithID
867 #endif
868 IN_PROC_BROWSER_TEST_F(PlatformAppDevToolsBrowserTest, MAYBE_ReOpenedWithID) {
869 #if defined(OS_WIN) && defined(USE_ASH)
870 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
871 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
872 return;
873 #endif
874 RunTestWithDevTools("minimal_id", RELAUNCH | HAS_ID);
877 // http://crbug.com/246999
878 #if defined(OS_CHROMEOS) || defined(OS_WIN)
879 #define MAYBE_ReOpenedWithURL DISABLED_ReOpenedWithURL
880 #else
881 #define MAYBE_ReOpenedWithURL ReOpenedWithURL
882 #endif
883 IN_PROC_BROWSER_TEST_F(PlatformAppDevToolsBrowserTest, MAYBE_ReOpenedWithURL) {
884 RunTestWithDevTools("minimal", RELAUNCH);
887 // Test that showing a permission request as a constrained window works and is
888 // correctly parented.
889 #if defined(OS_MACOSX)
890 #define MAYBE_ConstrainedWindowRequest DISABLED_ConstrainedWindowRequest
891 #else
892 // TODO(sail): Enable this on other platforms once http://crbug.com/95455 is
893 // fixed.
894 #define MAYBE_ConstrainedWindowRequest DISABLED_ConstrainedWindowRequest
895 #endif
897 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_ConstrainedWindowRequest) {
898 PermissionsRequestFunction::SetIgnoreUserGestureForTests(true);
899 const Extension* extension =
900 LoadAndLaunchPlatformApp("optional_permission_request");
901 ASSERT_TRUE(extension) << "Failed to load extension.";
903 WebContents* web_contents = GetFirstShellWindowWebContents();
904 ASSERT_TRUE(web_contents);
906 // Verify that the shell window has a dialog attached.
907 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
908 WebContentsModalDialogManager::FromWebContents(web_contents);
909 EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive());
911 // Close the constrained window and wait for the reply to the permission
912 // request.
913 ExtensionTestMessageListener listener("PermissionRequestDone", false);
914 WebContentsModalDialogManager::TestApi test_api(
915 web_contents_modal_dialog_manager);
916 test_api.CloseAllDialogs();
917 ASSERT_TRUE(listener.WaitUntilSatisfied());
920 // Tests that an app calling chrome.runtime.reload will reload the app and
921 // relaunch it if it was running.
922 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ReloadRelaunches) {
923 ExtensionTestMessageListener launched_listener("Launched", true);
924 const Extension* extension = LoadAndLaunchPlatformApp("reload");
925 ASSERT_TRUE(extension);
926 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
927 ASSERT_TRUE(GetFirstShellWindow());
929 // Now tell the app to reload itself
930 ExtensionTestMessageListener launched_listener2("Launched", false);
931 launched_listener.Reply("reload");
932 ASSERT_TRUE(launched_listener2.WaitUntilSatisfied());
933 ASSERT_TRUE(GetFirstShellWindow());
936 namespace {
938 // Simple observer to check for NOTIFICATION_EXTENSION_INSTALLED events to
939 // ensure installation does or does not occur in certain scenarios.
940 class CheckExtensionInstalledObserver : public content::NotificationObserver {
941 public:
942 CheckExtensionInstalledObserver() : seen_(false) {
943 registrar_.Add(this,
944 chrome::NOTIFICATION_EXTENSION_INSTALLED,
945 content::NotificationService::AllSources());
948 bool seen() const {
949 return seen_;
952 // NotificationObserver:
953 virtual void Observe(int type,
954 const content::NotificationSource& source,
955 const content::NotificationDetails& details) OVERRIDE {
956 EXPECT_FALSE(seen_);
957 seen_ = true;
960 private:
961 bool seen_;
962 content::NotificationRegistrar registrar_;
965 } // namespace
967 // Component App Test 1 of 3: ensure that the initial load of a component
968 // extension utilizing a background page (e.g. a v2 platform app) has its
969 // background page run and is launchable. Waits for the Launched response from
970 // the script resource in the opened shell window.
971 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
972 PRE_PRE_ComponentAppBackgroundPage) {
973 CheckExtensionInstalledObserver should_install;
975 // Ensure that we wait until the background page is run (to register the
976 // OnLaunched listener) before trying to open the application. This is similar
977 // to LoadAndLaunchPlatformApp, but we want to load as a component extension.
978 content::WindowedNotificationObserver app_loaded_observer(
979 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
980 content::NotificationService::AllSources());
982 const Extension* extension = LoadExtensionAsComponent(
983 test_data_dir_.AppendASCII("platform_apps").AppendASCII("component"));
984 ASSERT_TRUE(extension);
986 app_loaded_observer.Wait();
987 ASSERT_TRUE(should_install.seen());
989 ExtensionTestMessageListener launched_listener("Launched", false);
990 OpenApplication(AppLaunchParams(
991 browser()->profile(), extension, LAUNCH_CONTAINER_NONE, NEW_WINDOW));
993 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
996 // Component App Test 2 of 3: ensure an installed component app can be launched
997 // on a subsequent browser start, without requiring any install/upgrade logic
998 // to be run, then perform setup for step 3.
999 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
1000 PRE_ComponentAppBackgroundPage) {
1002 // Since the component app is now installed, re-adding it in the same profile
1003 // should not cause it to be re-installed. Instead, we wait for the OnLaunched
1004 // in a different observer (which would timeout if not the app was not
1005 // previously installed properly) and then check this observer to make sure it
1006 // never saw the NOTIFICATION_EXTENSION_INSTALLED event.
1007 CheckExtensionInstalledObserver should_not_install;
1008 const Extension* extension = LoadExtensionAsComponent(
1009 test_data_dir_.AppendASCII("platform_apps").AppendASCII("component"));
1010 ASSERT_TRUE(extension);
1012 ExtensionTestMessageListener launched_listener("Launched", false);
1013 OpenApplication(AppLaunchParams(
1014 browser()->profile(), extension, LAUNCH_CONTAINER_NONE, NEW_WINDOW));
1016 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1017 ASSERT_FALSE(should_not_install.seen());
1019 // Simulate a "downgrade" from version 2 in the test manifest.json to 1.
1020 ExtensionPrefs* extension_prefs =
1021 extensions::ExtensionSystem::Get(browser()->profile())->
1022 extension_service()->extension_prefs();
1024 // Clear the registered events to ensure they are updated.
1025 extensions::ExtensionSystem::Get(browser()->profile())->event_router()->
1026 SetRegisteredEvents(extension->id(), std::set<std::string>());
1028 DictionaryPrefUpdate update(extension_prefs->pref_service(),
1029 extensions::pref_names::kExtensions);
1030 base::DictionaryValue* dict = update.Get();
1031 std::string key(extension->id());
1032 key += ".manifest.version";
1033 dict->SetString(key, "1");
1036 // Component App Test 3 of 3: simulate a component extension upgrade that
1037 // re-adds the OnLaunched event, and allows the app to be launched.
1038 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ComponentAppBackgroundPage) {
1039 CheckExtensionInstalledObserver should_install;
1040 // Since we are forcing an upgrade, we need to wait for the load again.
1041 content::WindowedNotificationObserver app_loaded_observer(
1042 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
1043 content::NotificationService::AllSources());
1045 const Extension* extension = LoadExtensionAsComponent(
1046 test_data_dir_.AppendASCII("platform_apps").AppendASCII("component"));
1047 ASSERT_TRUE(extension);
1048 app_loaded_observer.Wait();
1049 ASSERT_TRUE(should_install.seen());
1051 ExtensionTestMessageListener launched_listener("Launched", false);
1052 OpenApplication(AppLaunchParams(
1053 browser()->profile(), extension, LAUNCH_CONTAINER_NONE, NEW_WINDOW));
1055 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1058 // Flakes on Windows: http://crbug.com/171450
1059 #if defined(OS_WIN)
1060 #define MAYBE_Messaging DISABLED_Messaging
1061 #else
1062 #define MAYBE_Messaging Messaging
1063 #endif
1064 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_Messaging) {
1065 ExtensionApiTest::ResultCatcher result_catcher;
1066 LoadAndLaunchPlatformApp("messaging/app2");
1067 LoadAndLaunchPlatformApp("messaging/app1");
1068 EXPECT_TRUE(result_catcher.GetNextResult());
1071 // TODO(linux_aura) http://crbug.com/163931
1072 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
1073 #define MAYBE_WebContentsHasFocus DISABLED_WebContentsHasFocus
1074 #else
1075 // This test depends on focus and so needs to be in interactive_ui_tests.
1076 // http://crbug.com/227041
1077 #define MAYBE_WebContentsHasFocus DISABLED_WebContentsHasFocus
1078 #endif
1079 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_WebContentsHasFocus) {
1080 ExtensionTestMessageListener launched_listener("Launched", true);
1081 LoadAndLaunchPlatformApp("minimal");
1082 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1084 EXPECT_EQ(1LU, GetShellWindowCount());
1085 EXPECT_TRUE(GetFirstShellWindow()->web_contents()->
1086 GetRenderWidgetHostView()->HasFocus());
1089 // The next three tests will only run automatically with Chrome branded builds
1090 // because they require the PDF preview plug-in. To run these tests manually for
1091 // Chromium (non-Chrome branded) builds in a development environment:
1093 // 1) Remove "MAYBE_" in the first line of each test definition
1094 // 2) Build Chromium browser_tests
1095 // 3) Make a copy of the PDF plug-in from a recent version of Chrome (Canary
1096 // or a recent development build) to your Chromium build:
1097 // - On Linux and Chrome OS, copy /opt/google/chrome/libpdf.so to
1098 // <path-to-your-src>/out/Debug
1099 // - On OS X, copy PDF.plugin from
1100 // <recent-chrome-app-folder>/*/*/*/*/"Internet Plug-Ins" to
1101 // <path-to-your-src>/out/Debug/Chromium.app/*/*/*/*/"Internet Plug-Ins"
1102 // 4) Run browser_tests with the --enable-print-preview flag
1104 #if !defined(GOOGLE_CHROME_BUILD) || \
1105 (defined(GOOGLE_CHROME_BUILD) && (defined(OS_WIN) || defined(OS_LINUX)))
1106 #define MAYBE_WindowDotPrintShouldBringUpPrintPreview \
1107 DISABLED_WindowDotPrintShouldBringUpPrintPreview
1108 #else
1109 #define MAYBE_WindowDotPrintShouldBringUpPrintPreview \
1110 WindowDotPrintShouldBringUpPrintPreview
1111 #endif
1113 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
1114 MAYBE_WindowDotPrintShouldBringUpPrintPreview) {
1115 ScopedPreviewTestingDelegate preview_delegate(true);
1116 ASSERT_TRUE(RunPlatformAppTest("platform_apps/print_api")) << message_;
1117 preview_delegate.WaitUntilPreviewIsReady();
1120 #if !defined(GOOGLE_CHROME_BUILD)
1121 #define MAYBE_ClosingWindowWhilePrintingShouldNotCrash \
1122 DISABLED_ClosingWindowWhilePrintingShouldNotCrash
1123 #else
1124 #define MAYBE_ClosingWindowWhilePrintingShouldNotCrash \
1125 ClosingWindowWhilePrintingShouldNotCrash
1126 #endif
1128 // This test verifies that http://crbug.com/297179 is fixed.
1129 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
1130 MAYBE_ClosingWindowWhilePrintingShouldNotCrash) {
1131 ScopedPreviewTestingDelegate preview_delegate(false);
1132 ASSERT_TRUE(RunPlatformAppTest("platform_apps/print_api")) << message_;
1133 preview_delegate.WaitUntilPreviewIsReady();
1134 GetFirstShellWindow()->GetBaseWindow()->Close();
1137 // This test currently only passes on OS X (on other platforms the print preview
1138 // dialog's size is limited by the size of the window being printed).
1139 #if !defined(GOOGLE_CHROME_BUILD) || !defined(OS_MACOSX)
1140 #define MAYBE_PrintPreviewShouldNotBeTooSmall \
1141 DISABLED_PrintPreviewShouldNotBeTooSmall
1142 #else
1143 #define MAYBE_PrintPreviewShouldNotBeTooSmall \
1144 PrintPreviewShouldNotBeTooSmall
1145 #endif
1147 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
1148 MAYBE_PrintPreviewShouldNotBeTooSmall) {
1149 // Print preview dialogs with widths less than 410 pixels will have preview
1150 // areas that are too small, and ones with heights less than 191 pixels will
1151 // have vertical scrollers for their controls that are too small.
1152 gfx::Size minimum_dialog_size(410, 191);
1153 ScopedPreviewTestingDelegate preview_delegate(false);
1154 ASSERT_TRUE(RunPlatformAppTest("platform_apps/print_api")) << message_;
1155 preview_delegate.WaitUntilPreviewIsReady();
1156 EXPECT_GE(preview_delegate.dialog_size().width(),
1157 minimum_dialog_size.width());
1158 EXPECT_GE(preview_delegate.dialog_size().height(),
1159 minimum_dialog_size.height());
1160 GetFirstShellWindow()->GetBaseWindow()->Close();
1164 #if defined(OS_CHROMEOS)
1166 class PlatformAppIncognitoBrowserTest : public PlatformAppBrowserTest,
1167 public ShellWindowRegistry::Observer {
1168 public:
1169 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
1170 // Tell chromeos to launch in Guest mode, aka incognito.
1171 command_line->AppendSwitch(switches::kIncognito);
1172 PlatformAppBrowserTest::SetUpCommandLine(command_line);
1174 virtual void SetUp() OVERRIDE {
1175 // Make sure the file manager actually gets loaded.
1176 ComponentLoader::EnableBackgroundExtensionsForTesting();
1177 PlatformAppBrowserTest::SetUp();
1180 // ShellWindowRegistry::Observer implementation.
1181 virtual void OnShellWindowAdded(ShellWindow* shell_window) OVERRIDE {
1182 opener_app_ids_.insert(shell_window->extension()->id());
1184 virtual void OnShellWindowIconChanged(ShellWindow* shell_window) OVERRIDE {}
1185 virtual void OnShellWindowRemoved(ShellWindow* shell_window) OVERRIDE {}
1187 protected:
1188 // A set of ids of apps we've seen open a shell window.
1189 std::set<std::string> opener_app_ids_;
1192 IN_PROC_BROWSER_TEST_F(PlatformAppIncognitoBrowserTest, IncognitoComponentApp) {
1193 // Get the file manager app.
1194 const Extension* file_manager = extension_service()->GetExtensionById(
1195 "hhaomjibdihmijegdhdafkllkbggdgoj", false);
1196 ASSERT_TRUE(file_manager != NULL);
1197 Profile* incognito_profile = profile()->GetOffTheRecordProfile();
1198 ASSERT_TRUE(incognito_profile != NULL);
1200 // Wait until the file manager has had a chance to register its listener
1201 // for the launch event.
1202 EventRouter* router = ExtensionSystem::Get(incognito_profile)->event_router();
1203 ASSERT_TRUE(router != NULL);
1204 while (!router->ExtensionHasEventListener(
1205 file_manager->id(), app_runtime::OnLaunched::kEventName)) {
1206 content::RunAllPendingInMessageLoop();
1209 // Listen for new shell windows so we see the file manager app launch itself.
1210 ShellWindowRegistry* registry = ShellWindowRegistry::Get(incognito_profile);
1211 ASSERT_TRUE(registry != NULL);
1212 registry->AddObserver(this);
1214 OpenApplication(AppLaunchParams(
1215 incognito_profile, file_manager, 0, chrome::HOST_DESKTOP_TYPE_NATIVE));
1217 while (!ContainsKey(opener_app_ids_, file_manager->id())) {
1218 content::RunAllPendingInMessageLoop();
1222 class RestartDeviceTest : public PlatformAppBrowserTest {
1223 public:
1224 RestartDeviceTest()
1225 : power_manager_client_(NULL),
1226 mock_user_manager_(NULL) {}
1227 virtual ~RestartDeviceTest() {}
1229 // PlatformAppBrowserTest overrides
1230 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
1231 PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture();
1233 chromeos::FakeDBusThreadManager* dbus_manager =
1234 new chromeos::FakeDBusThreadManager;
1235 dbus_manager->SetFakeClients();
1236 power_manager_client_ = new chromeos::FakePowerManagerClient;
1237 dbus_manager->SetPowerManagerClient(
1238 scoped_ptr<chromeos::PowerManagerClient>(power_manager_client_));
1239 chromeos::DBusThreadManager::SetInstanceForTesting(dbus_manager);
1242 virtual void SetUpOnMainThread() OVERRIDE {
1243 PlatformAppBrowserTest::SetUpOnMainThread();
1245 mock_user_manager_ = new chromeos::MockUserManager;
1246 user_manager_enabler_.reset(
1247 new chromeos::ScopedUserManagerEnabler(mock_user_manager_));
1249 EXPECT_CALL(*mock_user_manager_, IsUserLoggedIn())
1250 .WillRepeatedly(testing::Return(true));
1251 EXPECT_CALL(*mock_user_manager_, IsLoggedInAsKioskApp())
1252 .WillRepeatedly(testing::Return(true));
1255 virtual void CleanUpOnMainThread() OVERRIDE {
1256 user_manager_enabler_.reset();
1257 PlatformAppBrowserTest::CleanUpOnMainThread();
1260 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE {
1261 PlatformAppBrowserTest::TearDownInProcessBrowserTestFixture();
1264 int num_request_restart_calls() const {
1265 return power_manager_client_->num_request_restart_calls();
1268 private:
1269 chromeos::FakePowerManagerClient* power_manager_client_;
1270 chromeos::MockUserManager* mock_user_manager_;
1271 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
1273 DISALLOW_COPY_AND_ASSIGN(RestartDeviceTest);
1276 // Tests that chrome.runtime.restart would request device restart in
1277 // ChromeOS kiosk mode.
1278 IN_PROC_BROWSER_TEST_F(RestartDeviceTest, Restart) {
1279 ASSERT_EQ(0, num_request_restart_calls());
1281 ExtensionTestMessageListener launched_listener("Launched", true);
1282 const Extension* extension = LoadAndLaunchPlatformApp("restart_device");
1283 ASSERT_TRUE(extension);
1284 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1286 launched_listener.Reply("restart");
1287 ExtensionTestMessageListener restart_requested_listener("restartRequested",
1288 false);
1289 ASSERT_TRUE(restart_requested_listener.WaitUntilSatisfied());
1291 EXPECT_EQ(1, num_request_restart_calls());
1294 #endif // defined(OS_CHROMEOS)
1296 // Test that when an application is uninstalled and re-install it does not have
1297 // access to the previously set data.
1298 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ReinstallDataCleanup) {
1299 // The application is installed and launched. After the 'Launched' message is
1300 // acknowledged by the browser process, the application will test that some
1301 // data are not installed and then install them. The application will then be
1302 // uninstalled and the same process will be repeated.
1303 std::string extension_id;
1306 ExtensionTestMessageListener launched_listener("Launched", false);
1307 const Extension* extension =
1308 LoadAndLaunchPlatformApp("reinstall_data_cleanup");
1309 ASSERT_TRUE(extension);
1310 extension_id = extension->id();
1312 ExtensionApiTest::ResultCatcher result_catcher;
1313 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1315 EXPECT_TRUE(result_catcher.GetNextResult());
1318 UninstallExtension(extension_id);
1319 content::RunAllPendingInMessageLoop();
1322 ExtensionTestMessageListener launched_listener("Launched", false);
1323 const Extension* extension =
1324 LoadAndLaunchPlatformApp("reinstall_data_cleanup");
1325 ASSERT_TRUE(extension);
1326 ASSERT_EQ(extension_id, extension->id());
1328 ExtensionApiTest::ResultCatcher result_catcher;
1330 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1332 EXPECT_TRUE(result_catcher.GetNextResult());
1336 } // namespace extensions