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 // Tests for the --load-and-launch-app switch.
6 // The two cases are when chrome is running and another process uses the switch
7 // and when chrome is started from scratch.
9 #include "apps/switches.h"
10 #include "base/process/launch.h"
11 #include "base/test/test_timeouts.h"
12 #include "chrome/browser/apps/app_browsertest_util.h"
13 #include "chrome/browser/extensions/extension_browsertest.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "content/public/test/test_launcher.h"
17 #include "extensions/test/extension_test_message_listener.h"
19 using extensions::PlatformAppBrowserTest
;
23 // TODO(jackhou): Enable this test once it works on OSX. It currently does not
24 // work for the same reason --app-id doesn't. See http://crbug.com/148465
25 #if defined(OS_MACOSX)
26 #define MAYBE_LoadAndLaunchAppChromeRunning \
27 DISABLED_LoadAndLaunchAppChromeRunning
29 #define MAYBE_LoadAndLaunchAppChromeRunning LoadAndLaunchAppChromeRunning
32 // Case where Chrome is already running.
33 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest
,
34 MAYBE_LoadAndLaunchAppChromeRunning
) {
35 ExtensionTestMessageListener
launched_listener("Launched", false);
37 const base::CommandLine
& cmdline
= *base::CommandLine::ForCurrentProcess();
38 base::CommandLine
new_cmdline(cmdline
.GetProgram());
40 const char* kSwitchNames
[] = {
41 switches::kUserDataDir
,
43 new_cmdline
.CopySwitchesFrom(cmdline
, kSwitchNames
, arraysize(kSwitchNames
));
45 base::FilePath app_path
= test_data_dir_
46 .AppendASCII("platform_apps")
47 .AppendASCII("minimal");
49 new_cmdline
.AppendSwitchNative(apps::kLoadAndLaunchApp
,
52 new_cmdline
.AppendSwitch(content::kLaunchAsBrowser
);
53 base::Process process
=
54 base::LaunchProcess(new_cmdline
, base::LaunchOptionsForTest());
55 ASSERT_TRUE(process
.IsValid());
57 ASSERT_TRUE(launched_listener
.WaitUntilSatisfied());
58 ASSERT_TRUE(base::WaitForSingleProcess(process
.Handle(),
59 TestTimeouts::action_timeout()));
62 // TODO(jackhou): Enable this test once it works on OSX. It currently does not
63 // work for the same reason --app-id doesn't. See http://crbug.com/148465
64 #if defined(OS_MACOSX)
65 #define MAYBE_LoadAndLaunchAppWithFile DISABLED_LoadAndLaunchAppWithFile
67 #define MAYBE_LoadAndLaunchAppWithFile LoadAndLaunchAppWithFile
70 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest
,
71 MAYBE_LoadAndLaunchAppWithFile
) {
72 ExtensionTestMessageListener
launched_listener("Launched", false);
74 const base::CommandLine
& cmdline
= *base::CommandLine::ForCurrentProcess();
75 base::CommandLine
new_cmdline(cmdline
.GetProgram());
77 const char* kSwitchNames
[] = {
78 switches::kUserDataDir
,
80 new_cmdline
.CopySwitchesFrom(cmdline
, kSwitchNames
, arraysize(kSwitchNames
));
82 base::FilePath app_path
= test_data_dir_
83 .AppendASCII("platform_apps")
84 .AppendASCII("load_and_launch_file");
86 base::FilePath test_file_path
= test_data_dir_
87 .AppendASCII("platform_apps")
88 .AppendASCII("launch_files")
89 .AppendASCII("test.txt");
91 new_cmdline
.AppendSwitchNative(apps::kLoadAndLaunchApp
,
93 new_cmdline
.AppendSwitch(content::kLaunchAsBrowser
);
94 new_cmdline
.AppendArgPath(test_file_path
);
96 base::Process process
=
97 base::LaunchProcess(new_cmdline
, base::LaunchOptionsForTest());
98 ASSERT_TRUE(process
.IsValid());
100 ASSERT_TRUE(launched_listener
.WaitUntilSatisfied());
101 ASSERT_TRUE(base::WaitForSingleProcess(process
.Handle(),
102 TestTimeouts::action_timeout()));
107 // TestFixture that appends --load-and-launch-app before calling BrowserMain.
108 class PlatformAppLoadAndLaunchBrowserTest
: public PlatformAppBrowserTest
{
110 PlatformAppLoadAndLaunchBrowserTest() {}
112 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
113 PlatformAppBrowserTest::SetUpCommandLine(command_line
);
114 app_path_
= test_data_dir_
115 .AppendASCII("platform_apps")
116 .AppendASCII("minimal");
117 command_line
->AppendSwitchNative(apps::kLoadAndLaunchApp
,
121 void LoadAndLaunchApp() {
122 ExtensionTestMessageListener
launched_listener("Launched", false);
123 ASSERT_TRUE(launched_listener
.WaitUntilSatisfied());
125 // Start an actual browser because we can't shut down with just an app
127 CreateBrowser(ProfileManager::GetActiveUserProfile());
131 base::FilePath app_path_
;
133 DISALLOW_COPY_AND_ASSIGN(PlatformAppLoadAndLaunchBrowserTest
);
139 // TODO(jackhou): Make this test not flaky on Vista or Linux Aura. See
140 // http://crbug.com/176897
141 #if defined(OS_WIN) || (defined(OS_LINUX) && defined(USE_AURA))
142 #define MAYBE_LoadAndLaunchAppChromeNotRunning \
143 DISABLED_LoadAndLaunchAppChromeNotRunning
145 #define MAYBE_LoadAndLaunchAppChromeNotRunning \
146 LoadAndLaunchAppChromeNotRunning
149 // Case where Chrome is not running.
150 IN_PROC_BROWSER_TEST_F(PlatformAppLoadAndLaunchBrowserTest
,
151 MAYBE_LoadAndLaunchAppChromeNotRunning
) {