Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / apps / app_browsertest.cc
blobdb3344defdb9b6c616d597c336259fbd3cf79419
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::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 CopyTestDataAndGetTestFilePath(
180 const base::FilePath& test_data_file,
181 const base::FilePath& temp_dir,
182 const char* filename,
183 base::FilePath* file_path) {
184 base::FilePath path = temp_dir.AppendASCII(
185 filename).NormalizePathSeparators();
186 if (!(base::CopyFile(test_data_file, path)))
187 return false;
189 *file_path = path;
190 return true;
192 #endif // !defined(OS_CHROMEOS) && !defined(OS_WIN)
194 class PlatformAppWithFileBrowserTest: public PlatformAppBrowserTest {
195 public:
196 PlatformAppWithFileBrowserTest() {
197 set_open_about_blank_on_browser_launch(false);
200 protected:
201 bool RunPlatformAppTestWithFileInTestDataDir(
202 const std::string& extension_name,
203 const std::string& test_file) {
204 base::FilePath test_doc(test_data_dir_.AppendASCII(test_file));
205 test_doc = test_doc.NormalizePathSeparators();
206 return RunPlatformAppTestWithCommandLine(
207 extension_name, MakeCommandLineWithTestFilePath(test_doc));
210 bool RunPlatformAppTestWithFile(const std::string& extension_name,
211 const base::FilePath& test_file_path) {
212 return RunPlatformAppTestWithCommandLine(
213 extension_name, MakeCommandLineWithTestFilePath(test_file_path));
216 bool RunPlatformAppTestWithNothing(const std::string& extension_name) {
217 return RunPlatformAppTestWithCommandLine(
218 extension_name, *base::CommandLine::ForCurrentProcess());
221 private:
222 bool RunPlatformAppTestWithCommandLine(
223 const std::string& extension_name,
224 const base::CommandLine& command_line) {
225 extensions::ResultCatcher catcher;
227 base::FilePath extension_path = test_data_dir_.AppendASCII(extension_name);
228 const extensions::Extension* extension =
229 LoadExtensionWithFlags(extension_path, ExtensionBrowserTest::kFlagNone);
230 if (!extension) {
231 message_ = "Failed to load extension.";
232 return false;
235 AppLaunchParams params(browser()->profile(), extension,
236 extensions::LAUNCH_CONTAINER_NONE, NEW_WINDOW,
237 extensions::SOURCE_TEST);
238 params.command_line = command_line;
239 params.current_directory = test_data_dir_;
240 OpenApplication(params);
242 if (!catcher.GetNextResult()) {
243 message_ = catcher.message();
244 return false;
247 return true;
250 base::CommandLine MakeCommandLineWithTestFilePath(
251 const base::FilePath& test_file) {
252 base::CommandLine command_line = *base::CommandLine::ForCurrentProcess();
253 command_line.AppendArgPath(test_file);
254 return command_line;
258 #if !defined(OS_CHROMEOS)
259 const char kTestFilePath[] = "platform_apps/launch_files/test.txt";
260 #endif
262 } // namespace
264 // Tests that CreateAppWindow doesn't crash if you close it straight away.
265 // LauncherPlatformAppBrowserTest relies on this behaviour, but is only run for
266 // ash, so we test that it works here.
267 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, CreateAndCloseAppWindow) {
268 const Extension* extension = LoadAndLaunchPlatformApp("minimal", "Launched");
269 AppWindow* window = CreateAppWindow(extension);
270 CloseAppWindow(window);
273 // Tests that platform apps received the "launch" event when launched.
274 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
275 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
278 // Tests that platform apps cannot use certain disabled window properties, but
279 // can override them and then use them.
280 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisabledWindowProperties) {
281 ASSERT_TRUE(RunPlatformAppTest("platform_apps/disabled_window_properties"))
282 << message_;
285 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
286 LoadAndLaunchPlatformApp("minimal", "Launched");
288 // The empty app doesn't add any context menu items, so its menu should
289 // only include the developer tools.
290 WebContents* web_contents = GetFirstAppWindowWebContents();
291 ASSERT_TRUE(web_contents);
292 content::ContextMenuParams params;
293 scoped_ptr<PlatformAppContextMenu> menu;
294 menu.reset(new PlatformAppContextMenu(web_contents->GetMainFrame(), params));
295 menu->Init();
296 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
297 ASSERT_TRUE(
298 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
299 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
300 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
301 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
304 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) {
305 LoadAndLaunchPlatformApp("context_menu", "Launched");
307 // The context_menu app has two context menu items. These, along with a
308 // separator and the developer tools, is all that should be in the menu.
309 WebContents* web_contents = GetFirstAppWindowWebContents();
310 ASSERT_TRUE(web_contents);
311 content::ContextMenuParams params;
312 scoped_ptr<PlatformAppContextMenu> menu;
313 menu.reset(new PlatformAppContextMenu(web_contents->GetMainFrame(), params));
314 menu->Init();
315 int first_extensions_command_id =
316 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0);
317 ASSERT_TRUE(menu->HasCommandWithId(first_extensions_command_id));
318 ASSERT_TRUE(menu->HasCommandWithId(first_extensions_command_id + 1));
319 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
320 ASSERT_TRUE(
321 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
322 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
323 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
324 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
325 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
328 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, InstalledAppWithContextMenu) {
329 ExtensionTestMessageListener launched_listener("Launched", false);
330 InstallAndLaunchPlatformApp("context_menu");
332 // Wait for the extension to tell us it's initialized its context menus and
333 // launched a window.
334 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
336 // The context_menu app has two context menu items. For an installed app
337 // these are all that should be in the menu.
338 WebContents* web_contents = GetFirstAppWindowWebContents();
339 ASSERT_TRUE(web_contents);
340 content::ContextMenuParams params;
341 scoped_ptr<PlatformAppContextMenu> menu;
342 menu.reset(new PlatformAppContextMenu(web_contents->GetMainFrame(), params));
343 menu->Init();
344 int extensions_custom_id =
345 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0);
346 ASSERT_TRUE(menu->HasCommandWithId(extensions_custom_id));
347 ASSERT_TRUE(menu->HasCommandWithId(extensions_custom_id + 1));
348 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
349 ASSERT_FALSE(
350 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
351 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
352 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
353 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
354 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
357 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuTextField) {
358 LoadAndLaunchPlatformApp("context_menu", "Launched");
360 // The context_menu app has one context menu item. This, along with a
361 // separator and the developer tools, is all that should be in the menu.
362 WebContents* web_contents = GetFirstAppWindowWebContents();
363 ASSERT_TRUE(web_contents);
364 content::ContextMenuParams params;
365 params.is_editable = true;
366 scoped_ptr<PlatformAppContextMenu> menu;
367 menu.reset(new PlatformAppContextMenu(web_contents->GetMainFrame(), params));
368 menu->Init();
369 int extensions_custom_id =
370 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0);
371 ASSERT_TRUE(menu->HasCommandWithId(extensions_custom_id));
372 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
373 ASSERT_TRUE(
374 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
375 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
376 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
377 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
378 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
379 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
382 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuSelection) {
383 LoadAndLaunchPlatformApp("context_menu", "Launched");
385 // The context_menu app has one context menu item. This, along with a
386 // separator and the developer tools, is all that should be in the menu.
387 WebContents* web_contents = GetFirstAppWindowWebContents();
388 ASSERT_TRUE(web_contents);
389 content::ContextMenuParams params;
390 params.selection_text = base::ASCIIToUTF16("Hello World");
391 scoped_ptr<PlatformAppContextMenu> menu;
392 menu.reset(new PlatformAppContextMenu(web_contents->GetMainFrame(), params));
393 menu->Init();
394 int extensions_custom_id =
395 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0);
396 ASSERT_TRUE(menu->HasCommandWithId(extensions_custom_id));
397 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
398 ASSERT_TRUE(
399 menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE));
400 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP));
401 ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
402 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
403 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
404 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
407 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuClicked) {
408 LoadAndLaunchPlatformApp("context_menu_click", "Launched");
410 // Test that the menu item shows up
411 WebContents* web_contents = GetFirstAppWindowWebContents();
412 ASSERT_TRUE(web_contents);
413 content::ContextMenuParams params;
414 params.page_url = GURL("http://foo.bar");
415 scoped_ptr<PlatformAppContextMenu> menu;
416 menu.reset(new PlatformAppContextMenu(web_contents->GetMainFrame(), params));
417 menu->Init();
418 int extensions_custom_id =
419 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0);
420 ASSERT_TRUE(menu->HasCommandWithId(extensions_custom_id));
422 // Execute the menu item
423 ExtensionTestMessageListener onclicked_listener("onClicked fired for id1",
424 false);
425 menu->ExecuteCommand(extensions_custom_id, 0);
427 ASSERT_TRUE(onclicked_listener.WaitUntilSatisfied());
430 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) {
431 TabsAddedNotificationObserver observer(2);
433 ASSERT_TRUE(StartEmbeddedTestServer());
434 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_;
436 observer.Wait();
437 ASSERT_EQ(2U, observer.tabs().size());
438 EXPECT_EQ(std::string(chrome::kExtensionInvalidRequestURL),
439 observer.tabs()[0]->GetURL().spec());
440 EXPECT_EQ("http://chromium.org/",
441 observer.tabs()[1]->GetURL().spec());
444 // Failing on some Win and Linux buildbots. See crbug.com/354425.
445 #if defined(OS_WIN) || defined(OS_LINUX)
446 #define MAYBE_Iframes DISABLED_Iframes
447 #else
448 #define MAYBE_Iframes Iframes
449 #endif
450 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_Iframes) {
451 ASSERT_TRUE(StartEmbeddedTestServer());
452 ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_;
455 // Tests that localStorage and WebSQL are disabled for platform apps.
456 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) {
457 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
460 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) {
461 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_;
464 // Tests that extensions can't use platform-app-only APIs.
465 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, PlatformAppsOnly) {
466 ASSERT_TRUE(RunExtensionTestIgnoreManifestWarnings(
467 "platform_apps/apps_only")) << message_;
470 // Tests that platform apps have isolated storage by default.
471 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Isolation) {
472 ASSERT_TRUE(StartEmbeddedTestServer());
474 // Load a (non-app) page under the "localhost" origin that sets a cookie.
475 GURL set_cookie_url = embedded_test_server()->GetURL(
476 "/extensions/platform_apps/isolation/set_cookie.html");
477 GURL::Replacements replace_host;
478 replace_host.SetHostStr("localhost");
479 set_cookie_url = set_cookie_url.ReplaceComponents(replace_host);
481 ui_test_utils::NavigateToURL(browser(), set_cookie_url);
483 // Make sure the cookie is set.
484 int cookie_size;
485 std::string cookie_value;
486 ui_test_utils::GetCookies(
487 set_cookie_url,
488 browser()->tab_strip_model()->GetWebContentsAt(0),
489 &cookie_size,
490 &cookie_value);
491 ASSERT_EQ("testCookie=1", cookie_value);
493 // Let the platform app request the same URL, and make sure that it doesn't
494 // see the cookie.
495 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_;
498 // See crbug.com/248441
499 #if defined(OS_WIN)
500 #define MAYBE_ExtensionWindowingApis DISABLED_ExtensionWindowingApis
501 #else
502 #define MAYBE_ExtensionWindowingApis ExtensionWindowingApis
503 #endif
505 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_ExtensionWindowingApis) {
506 // Initially there should be just the one browser window visible to the
507 // extensions API.
508 const Extension* extension = LoadExtension(
509 test_data_dir_.AppendASCII("common/background_page"));
510 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
512 // And no app windows.
513 ASSERT_EQ(0U, GetAppWindowCount());
515 // Launch a platform app that shows a window.
516 LoadAndLaunchPlatformApp("minimal", "Launched");
517 ASSERT_EQ(1U, GetAppWindowCount());
518 int app_window_id = GetFirstAppWindow()->session_id().id();
520 // But it's not visible to the extensions API, it still thinks there's just
521 // one browser window.
522 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
523 // It can't look it up by ID either
524 ASSERT_FALSE(RunGetWindowFunctionForExtension(app_window_id, extension));
526 // The app can also only see one window (its own).
527 // TODO(jeremya): add an extension function to get an app window by ID, and
528 // to get a list of all the app windows, so we can test this.
530 // Launch another platform app that also shows a window.
531 LoadAndLaunchPlatformApp("context_menu", "Launched");
533 // There are two total app windows, but each app can only see its own.
534 ASSERT_EQ(2U, GetAppWindowCount());
535 // TODO(jeremya): as above, this requires more extension functions.
538 // ChromeOS does not support passing arguments on the command line, so the tests
539 // that rely on this functionality are disabled.
540 #if !defined(OS_CHROMEOS)
541 // Tests that command line parameters get passed through to platform apps
542 // via launchData correctly when launching with a file.
543 // TODO(benwells/jeremya): tests need a way to specify a handler ID.
544 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest, LaunchWithFile) {
545 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
546 "platform_apps/launch_file", kTestFilePath)) << message_;
549 // Tests that relative paths can be passed through to the platform app.
550 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest, LaunchWithRelativeFile) {
551 ASSERT_TRUE(RunPlatformAppTestWithFile(
552 "platform_apps/launch_file",
553 base::FilePath::FromUTF8Unsafe(kTestFilePath))) << message_;
556 // Tests that launch data is sent through if the file extension matches.
557 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest,
558 LaunchWithFileExtension) {
559 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
560 "platform_apps/launch_file_by_extension", kTestFilePath)) << message_;
563 // Tests that launch data is sent through to a whitelisted extension if the file
564 // extension matches.
565 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest,
566 LaunchWhiteListedExtensionWithFile) {
567 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
568 "platform_apps/launch_whitelisted_ext_with_file",
569 kTestFilePath)) << message_;
572 // Tests that launch data is sent through if the file extension and MIME type
573 // both match.
574 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest,
575 LaunchWithFileExtensionAndMimeType) {
576 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
577 "platform_apps/launch_file_by_extension_and_type", kTestFilePath))
578 << message_;
581 // Tests that launch data is sent through for a file with no extension if a
582 // handler accepts "".
583 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest,
584 LaunchWithFileWithoutExtension) {
585 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
586 "platform_apps/launch_file_with_no_extension",
587 "platform_apps/launch_files/test")) << message_;
590 #if !defined(OS_WIN)
591 // Tests that launch data is sent through for a file with an empty extension if
592 // a handler accepts "".
593 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest,
594 LaunchWithFileEmptyExtension) {
595 base::ScopedTempDir temp_dir;
596 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
597 base::FilePath test_file;
598 ASSERT_TRUE(CopyTestDataAndGetTestFilePath(
599 test_data_dir_.AppendASCII(kTestFilePath),
600 temp_dir.path(),
601 "test.",
602 &test_file));
603 ASSERT_TRUE(RunPlatformAppTestWithFile(
604 "platform_apps/launch_file_with_no_extension", test_file)) << message_;
607 // Tests that launch data is sent through for a file with an empty extension if
608 // a handler accepts *.
609 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest,
610 LaunchWithFileEmptyExtensionAcceptAny) {
611 base::ScopedTempDir temp_dir;
612 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
613 base::FilePath test_file;
614 ASSERT_TRUE(CopyTestDataAndGetTestFilePath(
615 test_data_dir_.AppendASCII(kTestFilePath),
616 temp_dir.path(),
617 "test.",
618 &test_file));
619 ASSERT_TRUE(RunPlatformAppTestWithFile(
620 "platform_apps/launch_file_with_any_extension", test_file)) << message_;
622 #endif
624 // Tests that launch data is sent through for a file with no extension if a
625 // handler accepts *.
626 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest,
627 LaunchWithFileWithoutExtensionAcceptAny) {
628 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
629 "platform_apps/launch_file_with_any_extension",
630 "platform_apps/launch_files/test")) << message_;
633 // Tests that launch data is sent through for a file with an extension if a
634 // handler accepts *.
635 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest,
636 LaunchWithFileAcceptAnyExtension) {
637 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
638 "platform_apps/launch_file_with_any_extension", kTestFilePath))
639 << message_;
642 // Tests that no launch data is sent through if the file has the wrong
643 // extension.
644 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest,
645 LaunchWithWrongExtension) {
646 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
647 "platform_apps/launch_wrong_extension", kTestFilePath)) << message_;
650 // Tests that no launch data is sent through if the file has no extension but
651 // the handler requires a specific extension.
652 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest,
653 LaunchWithWrongEmptyExtension) {
654 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
655 "platform_apps/launch_wrong_extension",
656 "platform_apps/launch_files/test")) << message_;
659 // Tests that no launch data is sent through if the file is of the wrong MIME
660 // type.
661 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest, LaunchWithWrongType) {
662 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
663 "platform_apps/launch_wrong_type", kTestFilePath)) << message_;
666 // Tests that no launch data is sent through if the platform app does not
667 // provide an intent.
668 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest, LaunchWithNoIntent) {
669 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
670 "platform_apps/launch_no_intent", kTestFilePath)) << message_;
673 // Tests that launch data is sent through when the file has unknown extension
674 // but the MIME type can be sniffed and the sniffed type matches.
675 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest,
676 LaunchWithSniffableType) {
677 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
678 "platform_apps/launch_file_by_extension_and_type",
679 "platform_apps/launch_files/test.unknownextension")) << message_;
682 // Tests that launch data is sent through with the MIME type set to
683 // application/octet-stream if the file MIME type cannot be read.
684 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest, LaunchNoType) {
685 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
686 "platform_apps/launch_application_octet_stream",
687 "platform_apps/launch_files/test_binary.unknownextension")) << message_;
690 // Tests that no launch data is sent through if the file does not exist.
691 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest, LaunchNoFile) {
692 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
693 "platform_apps/launch_invalid",
694 "platform_apps/launch_files/doesnotexist.txt")) << message_;
697 // Tests that no launch data is sent through if the argument is a directory.
698 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest, LaunchWithDirectory) {
699 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
700 "platform_apps/launch_invalid",
701 "platform_apps/launch_files")) << message_;
704 // Tests that no launch data is sent through if there are no arguments passed
705 // on the command line
706 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest, LaunchWithNothing) {
707 ASSERT_TRUE(RunPlatformAppTestWithNothing("platform_apps/launch_nothing"))
708 << message_;
711 // Test that platform apps can use the chrome.fileSystem.getDisplayPath
712 // function to get the native file system path of a file they are launched with.
713 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest, GetDisplayPath) {
714 ASSERT_TRUE(RunPlatformAppTestWithFileInTestDataDir(
715 "platform_apps/get_display_path", kTestFilePath)) << message_;
718 // Tests that the file is created if the file does not exist and the app has the
719 // fileSystem.write permission.
720 IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest, LaunchNewFile) {
721 base::ScopedTempDir temp_dir;
722 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
723 ASSERT_TRUE(RunPlatformAppTestWithFile(
724 "platform_apps/launch_new_file",
725 temp_dir.path().AppendASCII("new_file.txt"))) << message_;
728 #endif // !defined(OS_CHROMEOS)
730 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OpenLink) {
731 ASSERT_TRUE(StartEmbeddedTestServer());
732 content::WindowedNotificationObserver observer(
733 chrome::NOTIFICATION_TAB_ADDED,
734 content::Source<content::WebContentsDelegate>(browser()));
735 LoadAndLaunchPlatformApp("open_link", "Launched");
736 observer.Wait();
737 ASSERT_EQ(2, browser()->tab_strip_model()->count());
740 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MutationEventsDisabled) {
741 ASSERT_TRUE(RunPlatformAppTest("platform_apps/mutation_events")) << message_;
744 // This appears to be unreliable on linux.
745 // TODO(stevenjb): Investigate and enable
746 #if defined(OS_LINUX) && !defined(USE_ASH)
747 #define MAYBE_AppWindowRestoreState DISABLED_AppWindowRestoreState
748 #else
749 #define MAYBE_AppWindowRestoreState AppWindowRestoreState
750 #endif
751 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_AppWindowRestoreState) {
752 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restore_state"));
755 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
756 AppWindowAdjustBoundsToBeVisibleOnScreen) {
757 const Extension* extension = LoadAndLaunchPlatformApp("minimal", "Launched");
759 AppWindow* window = CreateAppWindow(extension);
761 // The screen bounds didn't change, the cached bounds didn't need to adjust.
762 gfx::Rect cached_bounds(80, 100, 400, 400);
763 gfx::Rect cached_screen_bounds(0, 0, 1600, 900);
764 gfx::Rect current_screen_bounds(0, 0, 1600, 900);
765 gfx::Size minimum_size(200, 200);
766 gfx::Rect bounds;
767 CallAdjustBoundsToBeVisibleOnScreenForAppWindow(window,
768 cached_bounds,
769 cached_screen_bounds,
770 current_screen_bounds,
771 minimum_size,
772 &bounds);
773 EXPECT_EQ(bounds, cached_bounds);
775 // We have an empty screen bounds, the cached bounds didn't need to adjust.
776 gfx::Rect empty_screen_bounds;
777 CallAdjustBoundsToBeVisibleOnScreenForAppWindow(window,
778 cached_bounds,
779 empty_screen_bounds,
780 current_screen_bounds,
781 minimum_size,
782 &bounds);
783 EXPECT_EQ(bounds, cached_bounds);
785 // Cached bounds is completely off the new screen bounds in horizontal
786 // locations. Expect to reposition the bounds.
787 gfx::Rect horizontal_out_of_screen_bounds(-800, 100, 400, 400);
788 CallAdjustBoundsToBeVisibleOnScreenForAppWindow(
789 window,
790 horizontal_out_of_screen_bounds,
791 gfx::Rect(-1366, 0, 1600, 900),
792 current_screen_bounds,
793 minimum_size,
794 &bounds);
795 EXPECT_EQ(bounds, gfx::Rect(0, 100, 400, 400));
797 // Cached bounds is completely off the new screen bounds in vertical
798 // locations. Expect to reposition the bounds.
799 gfx::Rect vertical_out_of_screen_bounds(10, 1000, 400, 400);
800 CallAdjustBoundsToBeVisibleOnScreenForAppWindow(
801 window,
802 vertical_out_of_screen_bounds,
803 gfx::Rect(-1366, 0, 1600, 900),
804 current_screen_bounds,
805 minimum_size,
806 &bounds);
807 EXPECT_EQ(bounds, gfx::Rect(10, 500, 400, 400));
809 // From a large screen resulotion to a small one. Expect it fit on screen.
810 gfx::Rect big_cache_bounds(10, 10, 1000, 1000);
811 CallAdjustBoundsToBeVisibleOnScreenForAppWindow(window,
812 big_cache_bounds,
813 gfx::Rect(0, 0, 1600, 1000),
814 gfx::Rect(0, 0, 800, 600),
815 minimum_size,
816 &bounds);
817 EXPECT_EQ(bounds, gfx::Rect(0, 0, 800, 600));
819 // Don't resize the bounds smaller than minimum size, when the minimum size is
820 // larger than the screen.
821 CallAdjustBoundsToBeVisibleOnScreenForAppWindow(window,
822 big_cache_bounds,
823 gfx::Rect(0, 0, 1600, 1000),
824 gfx::Rect(0, 0, 800, 600),
825 gfx::Size(900, 900),
826 &bounds);
827 EXPECT_EQ(bounds, gfx::Rect(0, 0, 900, 900));
830 namespace {
832 class PlatformAppDevToolsBrowserTest : public PlatformAppBrowserTest {
833 protected:
834 enum TestFlags {
835 RELAUNCH = 0x1,
836 HAS_ID = 0x2,
838 // Runs a test inside a harness that opens DevTools on an app window.
839 void RunTestWithDevTools(const char* name, int test_flags);
842 void PlatformAppDevToolsBrowserTest::RunTestWithDevTools(
843 const char* name, int test_flags) {
844 using content::DevToolsAgentHost;
845 const Extension* extension = LoadAndLaunchPlatformApp(name, "Launched");
846 ASSERT_TRUE(extension);
847 AppWindow* window = GetFirstAppWindow();
848 ASSERT_TRUE(window);
849 ASSERT_EQ(window->window_key().empty(), (test_flags & HAS_ID) == 0);
850 content::WebContents* web_contents = window->web_contents();
851 ASSERT_TRUE(web_contents);
853 OpenDevToolsWindow(web_contents);
855 if (test_flags & RELAUNCH) {
856 // Close the AppWindow, and ensure it is gone.
857 CloseAppWindow(window);
858 ASSERT_FALSE(GetFirstAppWindow());
860 // Relaunch the app and get a new AppWindow.
861 content::WindowedNotificationObserver app_loaded_observer(
862 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
863 content::NotificationService::AllSources());
864 OpenApplication(AppLaunchParams(browser()->profile(), extension,
865 LAUNCH_CONTAINER_NONE, NEW_WINDOW,
866 extensions::SOURCE_TEST));
867 app_loaded_observer.Wait();
868 window = GetFirstAppWindow();
869 ASSERT_TRUE(window);
871 // DevTools should have reopened with the relaunch.
872 web_contents = window->web_contents();
873 ASSERT_TRUE(web_contents);
874 ASSERT_TRUE(DevToolsAgentHost::HasFor(web_contents));
878 } // namespace
880 // http://crbug.com/246634
881 #if defined(OS_CHROMEOS)
882 #define MAYBE_ReOpenedWithID DISABLED_ReOpenedWithID
883 #else
884 #define MAYBE_ReOpenedWithID ReOpenedWithID
885 #endif
886 IN_PROC_BROWSER_TEST_F(PlatformAppDevToolsBrowserTest, MAYBE_ReOpenedWithID) {
887 #if defined(OS_WIN) && defined(USE_ASH)
888 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
889 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
890 switches::kAshBrowserTests))
891 return;
892 #endif
893 RunTestWithDevTools("minimal_id", RELAUNCH | HAS_ID);
896 // http://crbug.com/246999
897 #if defined(OS_CHROMEOS) || defined(OS_WIN)
898 #define MAYBE_ReOpenedWithURL DISABLED_ReOpenedWithURL
899 #else
900 #define MAYBE_ReOpenedWithURL ReOpenedWithURL
901 #endif
902 IN_PROC_BROWSER_TEST_F(PlatformAppDevToolsBrowserTest, MAYBE_ReOpenedWithURL) {
903 RunTestWithDevTools("minimal", RELAUNCH);
906 // Test that showing a permission request as a constrained window works and is
907 // correctly parented.
908 #if defined(OS_MACOSX)
909 #define MAYBE_ConstrainedWindowRequest DISABLED_ConstrainedWindowRequest
910 #else
911 // TODO(sail): Enable this on other platforms once http://crbug.com/95455 is
912 // fixed.
913 #define MAYBE_ConstrainedWindowRequest DISABLED_ConstrainedWindowRequest
914 #endif
916 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_ConstrainedWindowRequest) {
917 PermissionsRequestFunction::SetIgnoreUserGestureForTests(true);
918 const Extension* extension =
919 LoadAndLaunchPlatformApp("optional_permission_request", "Launched");
920 ASSERT_TRUE(extension) << "Failed to load extension.";
922 WebContents* web_contents = GetFirstAppWindowWebContents();
923 ASSERT_TRUE(web_contents);
925 // Verify that the app window has a dialog attached.
926 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
927 WebContentsModalDialogManager::FromWebContents(web_contents);
928 EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive());
930 // Close the constrained window and wait for the reply to the permission
931 // request.
932 ExtensionTestMessageListener listener("PermissionRequestDone", false);
933 WebContentsModalDialogManager::TestApi test_api(
934 web_contents_modal_dialog_manager);
935 test_api.CloseAllDialogs();
936 ASSERT_TRUE(listener.WaitUntilSatisfied());
939 // Tests that an app calling chrome.runtime.reload will reload the app and
940 // relaunch it if it was running.
941 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ReloadRelaunches) {
942 ExtensionTestMessageListener launched_listener("Launched", true);
943 const Extension* extension =
944 LoadAndLaunchPlatformApp("reload", &launched_listener);
945 ASSERT_TRUE(extension);
946 ASSERT_TRUE(GetFirstAppWindow());
948 // Now tell the app to reload itself
949 ExtensionTestMessageListener launched_listener2("Launched", false);
950 launched_listener.Reply("reload");
951 ASSERT_TRUE(launched_listener2.WaitUntilSatisfied());
952 ASSERT_TRUE(GetFirstAppWindow());
955 namespace {
957 // Simple observer to check for
958 // NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED events to ensure
959 // installation does or does not occur in certain scenarios.
960 class CheckExtensionInstalledObserver : public content::NotificationObserver {
961 public:
962 CheckExtensionInstalledObserver() : seen_(false) {
963 registrar_.Add(
964 this,
965 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
966 content::NotificationService::AllSources());
969 bool seen() const {
970 return seen_;
973 // NotificationObserver:
974 void Observe(int type,
975 const content::NotificationSource& source,
976 const content::NotificationDetails& details) override {
977 EXPECT_FALSE(seen_);
978 seen_ = true;
981 private:
982 bool seen_;
983 content::NotificationRegistrar registrar_;
986 } // namespace
988 // Component App Test 1 of 3: ensure that the initial load of a component
989 // extension utilizing a background page (e.g. a v2 platform app) has its
990 // background page run and is launchable. Waits for the Launched response from
991 // the script resource in the opened app window.
992 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
993 PRE_PRE_ComponentAppBackgroundPage) {
994 CheckExtensionInstalledObserver should_install;
996 // Ensure that we wait until the background page is run (to register the
997 // OnLaunched listener) before trying to open the application. This is similar
998 // to LoadAndLaunchPlatformApp, but we want to load as a component extension.
999 content::WindowedNotificationObserver app_loaded_observer(
1000 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
1001 content::NotificationService::AllSources());
1003 const Extension* extension = LoadExtensionAsComponent(
1004 test_data_dir_.AppendASCII("platform_apps").AppendASCII("component"));
1005 ASSERT_TRUE(extension);
1007 app_loaded_observer.Wait();
1008 ASSERT_TRUE(should_install.seen());
1010 ExtensionTestMessageListener launched_listener("Launched", false);
1011 OpenApplication(AppLaunchParams(browser()->profile(), extension,
1012 LAUNCH_CONTAINER_NONE, NEW_WINDOW,
1013 extensions::SOURCE_TEST));
1015 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1018 // Component App Test 2 of 3: ensure an installed component app can be launched
1019 // on a subsequent browser start, without requiring any install/upgrade logic
1020 // to be run, then perform setup for step 3.
1021 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
1022 PRE_ComponentAppBackgroundPage) {
1023 // Since the component app is now installed, re-adding it in the same profile
1024 // should not cause it to be re-installed. Instead, we wait for the OnLaunched
1025 // in a different observer (which would timeout if not the app was not
1026 // previously installed properly) and then check this observer to make sure it
1027 // never saw the NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED event.
1028 CheckExtensionInstalledObserver should_not_install;
1029 const Extension* extension = LoadExtensionAsComponent(
1030 test_data_dir_.AppendASCII("platform_apps").AppendASCII("component"));
1031 ASSERT_TRUE(extension);
1033 ExtensionTestMessageListener launched_listener("Launched", false);
1034 OpenApplication(AppLaunchParams(browser()->profile(), extension,
1035 LAUNCH_CONTAINER_NONE, NEW_WINDOW,
1036 extensions::SOURCE_TEST));
1038 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1039 ASSERT_FALSE(should_not_install.seen());
1041 // Simulate a "downgrade" from version 2 in the test manifest.json to 1.
1042 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser()->profile());
1044 // Clear the registered events to ensure they are updated.
1045 extensions::EventRouter::Get(browser()->profile())
1046 ->SetRegisteredEvents(extension->id(), std::set<std::string>());
1048 DictionaryPrefUpdate update(extension_prefs->pref_service(),
1049 extensions::pref_names::kExtensions);
1050 base::DictionaryValue* dict = update.Get();
1051 std::string key(extension->id());
1052 key += ".manifest.version";
1053 dict->SetString(key, "1");
1056 // Component App Test 3 of 3: simulate a component extension upgrade that
1057 // re-adds the OnLaunched event, and allows the app to be launched.
1058 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ComponentAppBackgroundPage) {
1059 CheckExtensionInstalledObserver should_install;
1060 // Since we are forcing an upgrade, we need to wait for the load again.
1061 content::WindowedNotificationObserver app_loaded_observer(
1062 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
1063 content::NotificationService::AllSources());
1065 const Extension* extension = LoadExtensionAsComponent(
1066 test_data_dir_.AppendASCII("platform_apps").AppendASCII("component"));
1067 ASSERT_TRUE(extension);
1068 app_loaded_observer.Wait();
1069 ASSERT_TRUE(should_install.seen());
1071 ExtensionTestMessageListener launched_listener("Launched", false);
1072 OpenApplication(AppLaunchParams(browser()->profile(), extension,
1073 LAUNCH_CONTAINER_NONE, NEW_WINDOW,
1074 extensions::SOURCE_TEST));
1076 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1079 // Disabled due to flakiness. http://crbug.com/468609
1080 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
1081 DISABLED_ComponentExtensionRuntimeReload) {
1082 // Ensure that we wait until the background page is run (to register the
1083 // OnLaunched listener) before trying to open the application. This is similar
1084 // to LoadAndLaunchPlatformApp, but we want to load as a component extension.
1085 content::WindowedNotificationObserver app_loaded_observer(
1086 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
1087 content::NotificationService::AllSources());
1089 const Extension* extension = LoadExtensionAsComponent(
1090 test_data_dir_.AppendASCII("platform_apps").AppendASCII("component"));
1091 ASSERT_TRUE(extension);
1093 app_loaded_observer.Wait();
1096 ExtensionTestMessageListener launched_listener("Launched", false);
1097 OpenApplication(AppLaunchParams(browser()->profile(), extension,
1098 LAUNCH_CONTAINER_NONE, NEW_WINDOW,
1099 extensions::SOURCE_TEST));
1100 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1104 ExtensionTestMessageListener launched_listener("Launched", false);
1105 ASSERT_TRUE(ExecuteScriptInBackgroundPageNoWait(
1106 extension->id(),
1107 // NoWait actually waits for a domAutomationController.send() which is
1108 // implicitly append to the script. Since reload() restarts the
1109 // extension, the send after reload may not get executed. To get around
1110 // this, send first, then execute the reload().
1111 "window.domAutomationController.send(0);"
1112 "chrome.runtime.reload();"));
1113 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1117 // Fails on Win7. http://crbug.com/171450
1118 #if defined(OS_WIN)
1119 #define MAYBE_Messaging DISABLED_Messaging
1120 #else
1121 #define MAYBE_Messaging Messaging
1122 #endif
1123 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_Messaging) {
1124 ResultCatcher result_catcher;
1125 LoadAndLaunchPlatformApp("messaging/app2", "Ready");
1126 LoadAndLaunchPlatformApp("messaging/app1", "Launched");
1127 EXPECT_TRUE(result_catcher.GetNextResult());
1130 // This test depends on focus and so needs to be in interactive_ui_tests.
1131 // http://crbug.com/227041
1132 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DISABLED_WebContentsHasFocus) {
1133 LoadAndLaunchPlatformApp("minimal", "Launched");
1135 EXPECT_EQ(1LU, GetAppWindowCount());
1136 EXPECT_TRUE(GetFirstAppWindow()
1137 ->web_contents()
1138 ->GetRenderWidgetHostView()
1139 ->HasFocus());
1142 #if defined(ENABLE_PRINT_PREVIEW)
1144 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX)
1145 #define MAYBE_WindowDotPrintShouldBringUpPrintPreview \
1146 DISABLED_WindowDotPrintShouldBringUpPrintPreview
1147 #else
1148 #define MAYBE_WindowDotPrintShouldBringUpPrintPreview \
1149 WindowDotPrintShouldBringUpPrintPreview
1150 #endif
1152 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
1153 MAYBE_WindowDotPrintShouldBringUpPrintPreview) {
1154 ScopedPreviewTestingDelegate preview_delegate(true);
1155 ASSERT_TRUE(RunPlatformAppTest("platform_apps/print_api")) << message_;
1156 preview_delegate.WaitUntilPreviewIsReady();
1159 // This test verifies that http://crbug.com/297179 is fixed.
1160 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
1161 DISABLED_ClosingWindowWhilePrintingShouldNotCrash) {
1162 ScopedPreviewTestingDelegate preview_delegate(false);
1163 ASSERT_TRUE(RunPlatformAppTest("platform_apps/print_api")) << message_;
1164 preview_delegate.WaitUntilPreviewIsReady();
1165 GetFirstAppWindow()->GetBaseWindow()->Close();
1168 // This test currently only passes on OS X (on other platforms the print preview
1169 // dialog's size is limited by the size of the window being printed).
1170 #if !defined(OS_MACOSX)
1171 #define MAYBE_PrintPreviewShouldNotBeTooSmall \
1172 DISABLED_PrintPreviewShouldNotBeTooSmall
1173 #else
1174 #define MAYBE_PrintPreviewShouldNotBeTooSmall \
1175 PrintPreviewShouldNotBeTooSmall
1176 #endif
1178 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
1179 MAYBE_PrintPreviewShouldNotBeTooSmall) {
1180 // Print preview dialogs with widths less than 410 pixels will have preview
1181 // areas that are too small, and ones with heights less than 191 pixels will
1182 // have vertical scrollers for their controls that are too small.
1183 gfx::Size minimum_dialog_size(410, 191);
1184 ScopedPreviewTestingDelegate preview_delegate(false);
1185 ASSERT_TRUE(RunPlatformAppTest("platform_apps/print_api")) << message_;
1186 preview_delegate.WaitUntilPreviewIsReady();
1187 EXPECT_GE(preview_delegate.dialog_size().width(),
1188 minimum_dialog_size.width());
1189 EXPECT_GE(preview_delegate.dialog_size().height(),
1190 minimum_dialog_size.height());
1191 GetFirstAppWindow()->GetBaseWindow()->Close();
1193 #endif // ENABLE_PRINT_PREVIEW
1195 #if defined(OS_CHROMEOS)
1197 class PlatformAppIncognitoBrowserTest : public PlatformAppBrowserTest,
1198 public AppWindowRegistry::Observer {
1199 public:
1200 void SetUpCommandLine(base::CommandLine* command_line) override {
1201 // Tell chromeos to launch in Guest mode, aka incognito.
1202 command_line->AppendSwitch(switches::kIncognito);
1203 PlatformAppBrowserTest::SetUpCommandLine(command_line);
1205 void SetUp() override {
1206 // Make sure the file manager actually gets loaded.
1207 ComponentLoader::EnableBackgroundExtensionsForTesting();
1208 PlatformAppBrowserTest::SetUp();
1211 // AppWindowRegistry::Observer implementation.
1212 void OnAppWindowAdded(AppWindow* app_window) override {
1213 opener_app_ids_.insert(app_window->extension_id());
1216 protected:
1217 // A set of ids of apps we've seen open a app window.
1218 std::set<std::string> opener_app_ids_;
1221 IN_PROC_BROWSER_TEST_F(PlatformAppIncognitoBrowserTest, IncognitoComponentApp) {
1222 // Get the file manager app.
1223 const Extension* file_manager = extension_service()->GetExtensionById(
1224 "hhaomjibdihmijegdhdafkllkbggdgoj", false);
1225 ASSERT_TRUE(file_manager != NULL);
1226 Profile* incognito_profile = profile()->GetOffTheRecordProfile();
1227 ASSERT_TRUE(incognito_profile != NULL);
1229 // Wait until the file manager has had a chance to register its listener
1230 // for the launch event.
1231 EventRouter* router = EventRouter::Get(incognito_profile);
1232 ASSERT_TRUE(router != NULL);
1233 while (!router->ExtensionHasEventListener(
1234 file_manager->id(), app_runtime::OnLaunched::kEventName)) {
1235 content::RunAllPendingInMessageLoop();
1238 // Listen for new app windows so we see the file manager app launch itself.
1239 AppWindowRegistry* registry = AppWindowRegistry::Get(incognito_profile);
1240 ASSERT_TRUE(registry != NULL);
1241 registry->AddObserver(this);
1243 OpenApplication(AppLaunchParams(incognito_profile, file_manager, CURRENT_TAB,
1244 chrome::HOST_DESKTOP_TYPE_NATIVE,
1245 extensions::SOURCE_TEST));
1247 while (!ContainsKey(opener_app_ids_, file_manager->id())) {
1248 content::RunAllPendingInMessageLoop();
1252 class RestartDeviceTest : public PlatformAppBrowserTest {
1253 public:
1254 RestartDeviceTest()
1255 : power_manager_client_(NULL),
1256 mock_user_manager_(NULL) {}
1257 ~RestartDeviceTest() override {}
1259 // PlatformAppBrowserTest overrides
1260 void SetUpInProcessBrowserTestFixture() override {
1261 PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture();
1263 power_manager_client_ = new chromeos::FakePowerManagerClient;
1264 chromeos::DBusThreadManager::GetSetterForTesting()->SetPowerManagerClient(
1265 scoped_ptr<chromeos::PowerManagerClient>(power_manager_client_));
1268 void SetUpOnMainThread() override {
1269 PlatformAppBrowserTest::SetUpOnMainThread();
1271 mock_user_manager_ = new chromeos::MockUserManager;
1272 user_manager_enabler_.reset(
1273 new chromeos::ScopedUserManagerEnabler(mock_user_manager_));
1275 EXPECT_CALL(*mock_user_manager_, IsUserLoggedIn())
1276 .WillRepeatedly(testing::Return(true));
1277 EXPECT_CALL(*mock_user_manager_, IsLoggedInAsKioskApp())
1278 .WillRepeatedly(testing::Return(true));
1279 EXPECT_CALL(*mock_user_manager_, GetLoggedInUsers())
1280 .WillRepeatedly(testing::Invoke(mock_user_manager_,
1281 &chromeos::MockUserManager::GetUsers));
1284 void TearDownOnMainThread() override {
1285 user_manager_enabler_.reset();
1286 PlatformAppBrowserTest::TearDownOnMainThread();
1289 void TearDownInProcessBrowserTestFixture() override {
1290 PlatformAppBrowserTest::TearDownInProcessBrowserTestFixture();
1293 int num_request_restart_calls() const {
1294 return power_manager_client_->num_request_restart_calls();
1297 private:
1298 chromeos::FakePowerManagerClient* power_manager_client_;
1299 chromeos::MockUserManager* mock_user_manager_;
1300 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
1302 DISALLOW_COPY_AND_ASSIGN(RestartDeviceTest);
1305 // Tests that chrome.runtime.restart would request device restart in
1306 // ChromeOS kiosk mode.
1307 IN_PROC_BROWSER_TEST_F(RestartDeviceTest, Restart) {
1308 ASSERT_EQ(0, num_request_restart_calls());
1310 ExtensionTestMessageListener launched_listener("Launched", true);
1311 const Extension* extension = LoadAndLaunchPlatformApp("restart_device",
1312 &launched_listener);
1313 ASSERT_TRUE(extension);
1315 launched_listener.Reply("restart");
1316 ExtensionTestMessageListener restart_requested_listener("restartRequested",
1317 false);
1318 ASSERT_TRUE(restart_requested_listener.WaitUntilSatisfied());
1320 EXPECT_EQ(1, num_request_restart_calls());
1323 #endif // defined(OS_CHROMEOS)
1325 // Test that when an application is uninstalled and re-install it does not have
1326 // access to the previously set data.
1327 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ReinstallDataCleanup) {
1328 // The application is installed and launched. After the 'Launched' message is
1329 // acknowledged by the browser process, the application will test that some
1330 // data are not installed and then install them. The application will then be
1331 // uninstalled and the same process will be repeated.
1332 std::string extension_id;
1335 const Extension* extension =
1336 LoadAndLaunchPlatformApp("reinstall_data_cleanup", "Launched");
1337 ASSERT_TRUE(extension);
1338 extension_id = extension->id();
1340 ResultCatcher result_catcher;
1341 EXPECT_TRUE(result_catcher.GetNextResult());
1344 UninstallExtension(extension_id);
1345 content::RunAllPendingInMessageLoop();
1348 const Extension* extension =
1349 LoadAndLaunchPlatformApp("reinstall_data_cleanup", "Launched");
1350 ASSERT_TRUE(extension);
1351 ASSERT_EQ(extension_id, extension->id());
1353 ResultCatcher result_catcher;
1354 EXPECT_TRUE(result_catcher.GetNextResult());
1358 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppsIgnoreDefaultZoom) {
1359 const Extension* extension = LoadAndLaunchPlatformApp("minimal", "Launched");
1361 // Set the browser default zoom to something other than the default (which is
1362 // 0).
1363 browser()->profile()->GetZoomLevelPrefs()->SetDefaultZoomLevelPref(1);
1365 // Launch another window. This is a simple way to guarantee that any messages
1366 // that would have been delivered to the app renderer and back for zoom have
1367 // made it through.
1368 ExtensionTestMessageListener launched_listener("Launched", false);
1369 LaunchPlatformApp(extension);
1370 launched_listener.WaitUntilSatisfied();
1372 // Now check that the app window's default zoom, and actual zoom level,
1373 // have not been changed from the default.
1374 WebContents* web_contents = GetFirstAppWindowWebContents();
1375 content::HostZoomMap* app_host_zoom_map = content::HostZoomMap::Get(
1376 web_contents->GetSiteInstance());
1377 EXPECT_EQ(0, app_host_zoom_map->GetDefaultZoomLevel());
1378 EXPECT_EQ(0, app_host_zoom_map->GetZoomLevel(web_contents));
1381 } // namespace extensions