blob: de73e18fb61cfce10eff7d206f0f6bd6b1cc0a91 [file] [log] [blame]
[email protected]9d02fa12013-02-19 05:12:571// 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.
4
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.
8
[email protected]2a69b942013-05-31 09:37:539#include "apps/switches.h"
[email protected]eb02ab62013-07-23 14:12:5510#include "base/process/launch.h"
Avi Drissmancf47385a2018-12-25 23:10:1411#include "base/stl_util.h"
proberge80a37f32016-08-04 19:44:5512#include "base/strings/utf_string_conversions.h"
Thomas Lukaszewicz2c5fb6142019-10-14 19:20:0513#include "base/test/test_switches.h"
[email protected]9d02fa12013-02-19 05:12:5714#include "base/test/test_timeouts.h"
Nico Webercda4ff542019-07-29 20:05:2215#include "build/branding_buildflags.h"
Lutz Justen584488452019-07-29 11:23:5116#include "build/build_config.h"
Dominick Ng6ff51052018-07-06 05:30:2017#include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
[email protected]9d02fa12013-02-19 05:12:5718#include "chrome/browser/extensions/extension_browsertest.h"
Devlin Cronin9722a722017-12-16 03:35:1019#include "chrome/browser/extensions/load_error_reporter.h"
[email protected]9d02fa12013-02-19 05:12:5720#include "chrome/browser/profiles/profile_manager.h"
proberge80a37f32016-08-04 19:44:5521#include "chrome/browser/ui/simple_message_box_internal.h"
[email protected]9d02fa12013-02-19 05:12:5722#include "chrome/common/chrome_switches.h"
benwellsfa165112015-01-20 04:12:5023#include "content/public/common/content_switches.h"
[email protected]9d02fa12013-02-19 05:12:5724#include "content/public/test/test_launcher.h"
proberge80a37f32016-08-04 19:44:5525#include "extensions/browser/extension_registry.h"
lfg910f2f92014-09-19 05:31:0926#include "extensions/test/extension_test_message_listener.h"
Jay Civelli3ae893e2018-04-17 20:07:0527#include "services/service_manager/sandbox/switches.h"
[email protected]9d02fa12013-02-19 05:12:5728
[email protected]2a69b942013-05-31 09:37:5329using extensions::PlatformAppBrowserTest;
30
31namespace apps {
[email protected]9d02fa12013-02-19 05:12:5732
benwellsfa165112015-01-20 04:12:5033namespace {
34
35const char* kSwitchesToCopy[] = {
Jay Civelli3ae893e2018-04-17 20:07:0536 service_manager::switches::kNoSandbox, switches::kUserDataDir,
benwellsfa165112015-01-20 04:12:5037};
38
proberge80a37f32016-08-04 19:44:5539constexpr char kTestExtensionId[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
40
benwellsfa165112015-01-20 04:12:5041} // namespace
42
[email protected]9d02fa12013-02-19 05:12:5743// TODO(jackhou): Enable this test once it works on OSX. It currently does not
44// work for the same reason --app-id doesn't. See http://crbug.com/148465
45#if defined(OS_MACOSX)
46#define MAYBE_LoadAndLaunchAppChromeRunning \
47 DISABLED_LoadAndLaunchAppChromeRunning
48#else
49#define MAYBE_LoadAndLaunchAppChromeRunning LoadAndLaunchAppChromeRunning
50#endif
51
52// Case where Chrome is already running.
53IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
54 MAYBE_LoadAndLaunchAppChromeRunning) {
55 ExtensionTestMessageListener launched_listener("Launched", false);
56
pgal.u-szeged214274b2014-10-28 11:59:4857 const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
58 base::CommandLine new_cmdline(cmdline.GetProgram());
benwellsfa165112015-01-20 04:12:5059 new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy,
Avi Drissmancf47385a2018-12-25 23:10:1460 base::size(kSwitchesToCopy));
[email protected]9d02fa12013-02-19 05:12:5761
62 base::FilePath app_path = test_data_dir_
63 .AppendASCII("platform_apps")
64 .AppendASCII("minimal");
65
[email protected]2a69b942013-05-31 09:37:5366 new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
[email protected]9d02fa12013-02-19 05:12:5767 app_path.value());
68
Thomas Lukaszewicz2c5fb6142019-10-14 19:20:0569 new_cmdline.AppendSwitch(switches::kLaunchAsBrowser);
rvargas6bfa2522014-12-10 02:09:5370 base::Process process =
71 base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest());
72 ASSERT_TRUE(process.IsValid());
[email protected]9d02fa12013-02-19 05:12:5773
74 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
rvargas2f70a152015-02-24 00:28:1175 int exit_code;
76 ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(),
77 &exit_code));
78 ASSERT_EQ(0, exit_code);
[email protected]9d02fa12013-02-19 05:12:5779}
80
[email protected]7b9faeb72013-06-11 12:20:1781// TODO(jackhou): Enable this test once it works on OSX. It currently does not
82// work for the same reason --app-id doesn't. See http://crbug.com/148465
83#if defined(OS_MACOSX)
84#define MAYBE_LoadAndLaunchAppWithFile DISABLED_LoadAndLaunchAppWithFile
85#else
86#define MAYBE_LoadAndLaunchAppWithFile LoadAndLaunchAppWithFile
87#endif
88
89IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
90 MAYBE_LoadAndLaunchAppWithFile) {
91 ExtensionTestMessageListener launched_listener("Launched", false);
92
pgal.u-szeged214274b2014-10-28 11:59:4893 const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
94 base::CommandLine new_cmdline(cmdline.GetProgram());
benwellsfa165112015-01-20 04:12:5095 new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy,
Avi Drissmancf47385a2018-12-25 23:10:1496 base::size(kSwitchesToCopy));
[email protected]7b9faeb72013-06-11 12:20:1797
98 base::FilePath app_path = test_data_dir_
99 .AppendASCII("platform_apps")
100 .AppendASCII("load_and_launch_file");
101
102 base::FilePath test_file_path = test_data_dir_
103 .AppendASCII("platform_apps")
104 .AppendASCII("launch_files")
105 .AppendASCII("test.txt");
106
107 new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
108 app_path.value());
Thomas Lukaszewicz2c5fb6142019-10-14 19:20:05109 new_cmdline.AppendSwitch(switches::kLaunchAsBrowser);
[email protected]7b9faeb72013-06-11 12:20:17110 new_cmdline.AppendArgPath(test_file_path);
111
rvargas6bfa2522014-12-10 02:09:53112 base::Process process =
113 base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest());
114 ASSERT_TRUE(process.IsValid());
[email protected]7b9faeb72013-06-11 12:20:17115
116 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
rvargas2f70a152015-02-24 00:28:11117 int exit_code;
118 ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(),
119 &exit_code));
120 ASSERT_EQ(0, exit_code);
[email protected]7b9faeb72013-06-11 12:20:17121}
122
[email protected]9d02fa12013-02-19 05:12:57123namespace {
124
proberge80a37f32016-08-04 19:44:55125// TestFixture that appends --load-and-launch-app with an app before calling
126// BrowserMain.
127class LoadAndLaunchPlatformAppBrowserTest : public PlatformAppBrowserTest {
[email protected]9d02fa12013-02-19 05:12:57128 protected:
proberge80a37f32016-08-04 19:44:55129 LoadAndLaunchPlatformAppBrowserTest() {}
[email protected]9d02fa12013-02-19 05:12:57130
pgal.u-szeged214274b2014-10-28 11:59:48131 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]9d02fa12013-02-19 05:12:57132 PlatformAppBrowserTest::SetUpCommandLine(command_line);
proberge80a37f32016-08-04 19:44:55133 base::FilePath app_path =
134 test_data_dir_.AppendASCII("platform_apps").AppendASCII("minimal");
135 command_line->AppendSwitchNative(apps::kLoadAndLaunchApp, app_path.value());
[email protected]9d02fa12013-02-19 05:12:57136 }
137
138 void LoadAndLaunchApp() {
139 ExtensionTestMessageListener launched_listener("Launched", false);
140 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
141
142 // Start an actual browser because we can't shut down with just an app
143 // window.
[email protected]5d5bea82013-12-18 03:20:32144 CreateBrowser(ProfileManager::GetActiveUserProfile());
[email protected]9d02fa12013-02-19 05:12:57145 }
146
147 private:
proberge80a37f32016-08-04 19:44:55148 DISALLOW_COPY_AND_ASSIGN(LoadAndLaunchPlatformAppBrowserTest);
149};
[email protected]9d02fa12013-02-19 05:12:57150
proberge80a37f32016-08-04 19:44:55151// TestFixture that appends --load-and-launch-app with an extension before
152// calling BrowserMain.
153class LoadAndLaunchExtensionBrowserTest : public PlatformAppBrowserTest {
154 protected:
155 LoadAndLaunchExtensionBrowserTest() {}
156
157 void SetUpCommandLine(base::CommandLine* command_line) override {
158 PlatformAppBrowserTest::SetUpCommandLine(command_line);
159 base::FilePath app_path = test_data_dir_.AppendASCII("good")
160 .AppendASCII("Extensions")
161 .AppendASCII(kTestExtensionId)
162 .AppendASCII("1.0.0.0");
163 command_line->AppendSwitchNative(apps::kLoadAndLaunchApp, app_path.value());
164 }
165
166 void SetUpInProcessBrowserTestFixture() override {
167 PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture();
168
169 // Skip showing the error message box to avoid freezing the main thread.
170 chrome::internal::g_should_skip_message_box_for_test = true;
171 }
172
173 DISALLOW_COPY_AND_ASSIGN(LoadAndLaunchExtensionBrowserTest);
[email protected]9d02fa12013-02-19 05:12:57174};
175
176} // namespace
177
178// Case where Chrome is not running.
proberge80a37f32016-08-04 19:44:55179IN_PROC_BROWSER_TEST_F(LoadAndLaunchPlatformAppBrowserTest,
proberge9c7cba52016-10-04 17:07:43180 LoadAndLaunchAppChromeNotRunning) {
[email protected]9d02fa12013-02-19 05:12:57181 LoadAndLaunchApp();
182}
183
Lutz Justen584488452019-07-29 11:23:51184// TODO(https://crbug.com/988160): Test is flaky on Windows.
185#if defined(OS_WIN)
186#define MAYBE_LoadAndLaunchExtension DISABLED_LoadAndLaunchExtension
187#else
188#define MAYBE_LoadAndLaunchExtension LoadAndLaunchExtension
189#endif
proberge80a37f32016-08-04 19:44:55190IN_PROC_BROWSER_TEST_F(LoadAndLaunchExtensionBrowserTest,
Lutz Justen584488452019-07-29 11:23:51191 MAYBE_LoadAndLaunchExtension) {
proberge80a37f32016-08-04 19:44:55192 const std::vector<base::string16>* errors =
Devlin Cronin9722a722017-12-16 03:35:10193 extensions::LoadErrorReporter::GetInstance()->GetErrors();
proberge80a37f32016-08-04 19:44:55194
Nico Webercda4ff542019-07-29 20:05:22195#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
proberge80a37f32016-08-04 19:44:55196 // The error is skipped on official builds.
197 EXPECT_TRUE(errors->empty());
198#else
199 // Expect |extension_instead_of_app_error|.
200 EXPECT_EQ(1u, errors->size());
201 EXPECT_NE(base::string16::npos,
202 errors->at(0).find(base::ASCIIToUTF16(
203 "App loading flags cannot be used to load extensions")));
204#endif
205
206 extensions::ExtensionRegistry* registry =
207 extensions::ExtensionRegistry::Get(profile());
208 EXPECT_EQ(nullptr,
209 registry->GetExtensionById(
210 kTestExtensionId, extensions::ExtensionRegistry::EVERYTHING));
211}
212
[email protected]2a69b942013-05-31 09:37:53213} // namespace apps