1 // Copyright 2013 The Chromium Authors
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.
11 #include "apps/switches.h"
12 #include "base/command_line.h"
13 #include "base/process/launch.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/test/test_switches.h"
16 #include "base/test/test_timeouts.h"
17 #include "build/branding_buildflags.h"
18 #include "build/build_config.h"
19 #include "build/chromeos_buildflags.h"
20 #include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
21 #include "chrome/browser/extensions/extension_browsertest.h"
22 #include "chrome/browser/extensions/load_error_reporter.h"
23 #include "chrome/browser/profiles/profile_manager.h"
24 #include "chrome/browser/ui/simple_message_box_internal.h"
25 #include "chrome/common/chrome_result_codes.h"
26 #include "chrome/common/chrome_switches.h"
27 #include "content/public/common/content_switches.h"
28 #include "content/public/test/browser_test.h"
29 #include "content/public/test/test_launcher.h"
30 #include "extensions/browser/extension_registry.h"
31 #include "extensions/test/extension_test_message_listener.h"
32 #include "sandbox/policy/switches.h"
34 using extensions::PlatformAppBrowserTest;
40 constexpr char kTestExtensionId[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
42 // Lacros doesn't support launching with chrome already running. See the header
43 // comment for InProcessBrowserTest::GetCommandLineForRelaunch().
44 #if !BUILDFLAG(IS_CHROMEOS_LACROS)
46 const char* kSwitchesToCopy[] = {
47 sandbox::policy::switches::kNoSandbox,
48 switches::kUserDataDir,
51 // TODO(jackhou): Enable this test once it works on OSX. It currently does not
52 // work for the same reason --app-id doesn't. See http://crbug.com/148465
54 #define MAYBE_LoadAndLaunchAppChromeRunning \
55 DISABLED_LoadAndLaunchAppChromeRunning
57 #define MAYBE_LoadAndLaunchAppChromeRunning LoadAndLaunchAppChromeRunning
60 // Case where Chrome is already running.
61 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
62 MAYBE_LoadAndLaunchAppChromeRunning) {
63 ExtensionTestMessageListener launched_listener("Launched");
65 const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
66 base::CommandLine new_cmdline(cmdline.GetProgram());
67 new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy,
68 std::size(kSwitchesToCopy));
70 base::FilePath app_path = test_data_dir_
71 .AppendASCII("platform_apps")
72 .AppendASCII("minimal");
74 new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
77 new_cmdline.AppendSwitch(switches::kLaunchAsBrowser);
78 base::Process process =
79 base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest());
80 ASSERT_TRUE(process.IsValid());
82 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
84 ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(),
86 ASSERT_EQ(chrome::RESULT_CODE_NORMAL_EXIT_PROCESS_NOTIFIED, exit_code);
89 // TODO(jackhou): Enable this test once it works on OSX. It currently does not
90 // work for the same reason --app-id doesn't. See http://crbug.com/148465.
92 #define MAYBE_LoadAndLaunchAppWithFile DISABLED_LoadAndLaunchAppWithFile
94 #define MAYBE_LoadAndLaunchAppWithFile LoadAndLaunchAppWithFile
97 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
98 MAYBE_LoadAndLaunchAppWithFile) {
99 ExtensionTestMessageListener launched_listener("Launched");
101 const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
102 base::CommandLine new_cmdline(cmdline.GetProgram());
103 new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy,
104 std::size(kSwitchesToCopy));
106 base::FilePath app_path = test_data_dir_
107 .AppendASCII("platform_apps")
108 .AppendASCII("load_and_launch_file");
110 base::FilePath test_file_path = test_data_dir_
111 .AppendASCII("platform_apps")
112 .AppendASCII("launch_files")
113 .AppendASCII("test.txt");
115 new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
117 new_cmdline.AppendSwitch(switches::kLaunchAsBrowser);
118 new_cmdline.AppendArgPath(test_file_path);
120 base::Process process =
121 base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest());
122 ASSERT_TRUE(process.IsValid());
124 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
126 ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(),
128 ASSERT_EQ(chrome::RESULT_CODE_NORMAL_EXIT_PROCESS_NOTIFIED, exit_code);
131 #endif // !BUILDFLAG(IS_CHROMEOS_LACROS)
133 // TestFixture that appends --load-and-launch-app with an app before calling
135 class LoadAndLaunchPlatformAppBrowserTest : public PlatformAppBrowserTest {
137 LoadAndLaunchPlatformAppBrowserTest(
138 const LoadAndLaunchPlatformAppBrowserTest&) = delete;
139 LoadAndLaunchPlatformAppBrowserTest& operator=(
140 const LoadAndLaunchPlatformAppBrowserTest&) = delete;
143 LoadAndLaunchPlatformAppBrowserTest() = default;
145 void SetUpCommandLine(base::CommandLine* command_line) override {
146 PlatformAppBrowserTest::SetUpCommandLine(command_line);
147 base::FilePath app_path =
148 test_data_dir_.AppendASCII("platform_apps").AppendASCII("minimal");
149 command_line->AppendSwitchNative(apps::kLoadAndLaunchApp, app_path.value());
151 // |launched_listener_| needs to be instantiated before the app process is
152 // launched to ensure the test api observer is registered.
154 std::make_unique<ExtensionTestMessageListener>("Launched");
157 void TearDownOnMainThread() override { launched_listener_.reset(); }
159 void LoadAndLaunchApp() {
160 ASSERT_TRUE(launched_listener_->WaitUntilSatisfied());
162 // Start an actual browser because we can't shut down with just an app
164 CreateBrowser(profile());
167 std::unique_ptr<ExtensionTestMessageListener> launched_listener_;
170 // TestFixture that appends --load-and-launch-app with an extension before
171 // calling BrowserMain.
172 class LoadAndLaunchExtensionBrowserTest : public PlatformAppBrowserTest {
174 LoadAndLaunchExtensionBrowserTest(const LoadAndLaunchExtensionBrowserTest&) =
176 LoadAndLaunchExtensionBrowserTest& operator=(
177 const LoadAndLaunchExtensionBrowserTest&) = delete;
180 LoadAndLaunchExtensionBrowserTest() = default;
182 void SetUpCommandLine(base::CommandLine* command_line) override {
183 PlatformAppBrowserTest::SetUpCommandLine(command_line);
184 base::FilePath app_path = test_data_dir_.AppendASCII("good")
185 .AppendASCII("Extensions")
186 .AppendASCII(kTestExtensionId)
187 .AppendASCII("1.0.0.0");
188 command_line->AppendSwitchNative(apps::kLoadAndLaunchApp, app_path.value());
191 void SetUpInProcessBrowserTestFixture() override {
192 PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture();
194 // Skip showing the error message box to avoid freezing the main thread.
195 chrome::internal::g_should_skip_message_box_for_test = true;
199 // Case where Chrome is not running.
200 IN_PROC_BROWSER_TEST_F(LoadAndLaunchPlatformAppBrowserTest,
201 LoadAndLaunchAppChromeNotRunning) {
205 IN_PROC_BROWSER_TEST_F(LoadAndLaunchExtensionBrowserTest,
206 LoadAndLaunchExtension) {
207 const std::vector<std::u16string>* errors =
208 extensions::LoadErrorReporter::GetInstance()->GetErrors();
210 #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
211 // The error is skipped on official builds.
212 EXPECT_TRUE(errors->empty());
214 // Expect |extension_instead_of_app_error|.
215 EXPECT_EQ(1u, errors->size());
216 EXPECT_NE(std::u16string::npos,
218 u"App loading flags cannot be used to load extensions"));
221 extensions::ExtensionRegistry* registry =
222 extensions::ExtensionRegistry::Get(profile());
224 registry->GetExtensionById(
225 kTestExtensionId, extensions::ExtensionRegistry::EVERYTHING));