[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 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. |
| 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] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 9 | #include "apps/switches.h" |
[email protected] | eb02ab6 | 2013-07-23 14:12:55 | [diff] [blame] | 10 | #include "base/process/launch.h" |
Avi Drissman | cf47385a | 2018-12-25 23:10:14 | [diff] [blame] | 11 | #include "base/stl_util.h" |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
Thomas Lukaszewicz | 2c5fb614 | 2019-10-14 19:20:05 | [diff] [blame] | 13 | #include "base/test/test_switches.h" |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 14 | #include "base/test/test_timeouts.h" |
Nico Weber | cda4ff54 | 2019-07-29 20:05:22 | [diff] [blame] | 15 | #include "build/branding_buildflags.h" |
Lutz Justen | 58448845 | 2019-07-29 11:23:51 | [diff] [blame] | 16 | #include "build/build_config.h" |
Dominick Ng | 6ff5105 | 2018-07-06 05:30:20 | [diff] [blame] | 17 | #include "chrome/browser/apps/platform_apps/app_browsertest_util.h" |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 18 | #include "chrome/browser/extensions/extension_browsertest.h" |
Devlin Cronin | 9722a72 | 2017-12-16 03:35:10 | [diff] [blame] | 19 | #include "chrome/browser/extensions/load_error_reporter.h" |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 20 | #include "chrome/browser/profiles/profile_manager.h" |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 21 | #include "chrome/browser/ui/simple_message_box_internal.h" |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 22 | #include "chrome/common/chrome_switches.h" |
benwells | fa16511 | 2015-01-20 04:12:50 | [diff] [blame] | 23 | #include "content/public/common/content_switches.h" |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 24 | #include "content/public/test/test_launcher.h" |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 25 | #include "extensions/browser/extension_registry.h" |
lfg | 910f2f9 | 2014-09-19 05:31:09 | [diff] [blame] | 26 | #include "extensions/test/extension_test_message_listener.h" |
Jay Civelli | 3ae893e | 2018-04-17 20:07:05 | [diff] [blame] | 27 | #include "services/service_manager/sandbox/switches.h" |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 28 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 29 | using extensions::PlatformAppBrowserTest; |
| 30 | |
| 31 | namespace apps { |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 32 | |
benwells | fa16511 | 2015-01-20 04:12:50 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | const char* kSwitchesToCopy[] = { |
Jay Civelli | 3ae893e | 2018-04-17 20:07:05 | [diff] [blame] | 36 | service_manager::switches::kNoSandbox, switches::kUserDataDir, |
benwells | fa16511 | 2015-01-20 04:12:50 | [diff] [blame] | 37 | }; |
| 38 | |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 39 | constexpr char kTestExtensionId[] = "behllobkkfkfnphdnhnkndlbkcpglgmj"; |
| 40 | |
benwells | fa16511 | 2015-01-20 04:12:50 | [diff] [blame] | 41 | } // namespace |
| 42 | |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 43 | // 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. |
| 53 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, |
| 54 | MAYBE_LoadAndLaunchAppChromeRunning) { |
| 55 | ExtensionTestMessageListener launched_listener("Launched", false); |
| 56 | |
pgal.u-szeged | 214274b | 2014-10-28 11:59:48 | [diff] [blame] | 57 | const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess(); |
| 58 | base::CommandLine new_cmdline(cmdline.GetProgram()); |
benwells | fa16511 | 2015-01-20 04:12:50 | [diff] [blame] | 59 | new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy, |
Avi Drissman | cf47385a | 2018-12-25 23:10:14 | [diff] [blame] | 60 | base::size(kSwitchesToCopy)); |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 61 | |
| 62 | base::FilePath app_path = test_data_dir_ |
| 63 | .AppendASCII("platform_apps") |
| 64 | .AppendASCII("minimal"); |
| 65 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 66 | new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp, |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 67 | app_path.value()); |
| 68 | |
Thomas Lukaszewicz | 2c5fb614 | 2019-10-14 19:20:05 | [diff] [blame] | 69 | new_cmdline.AppendSwitch(switches::kLaunchAsBrowser); |
rvargas | 6bfa252 | 2014-12-10 02:09:53 | [diff] [blame] | 70 | base::Process process = |
| 71 | base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest()); |
| 72 | ASSERT_TRUE(process.IsValid()); |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 73 | |
| 74 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
rvargas | 2f70a15 | 2015-02-24 00:28:11 | [diff] [blame] | 75 | int exit_code; |
| 76 | ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(), |
| 77 | &exit_code)); |
| 78 | ASSERT_EQ(0, exit_code); |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 81 | // 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 | |
| 89 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, |
| 90 | MAYBE_LoadAndLaunchAppWithFile) { |
| 91 | ExtensionTestMessageListener launched_listener("Launched", false); |
| 92 | |
pgal.u-szeged | 214274b | 2014-10-28 11:59:48 | [diff] [blame] | 93 | const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess(); |
| 94 | base::CommandLine new_cmdline(cmdline.GetProgram()); |
benwells | fa16511 | 2015-01-20 04:12:50 | [diff] [blame] | 95 | new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy, |
Avi Drissman | cf47385a | 2018-12-25 23:10:14 | [diff] [blame] | 96 | base::size(kSwitchesToCopy)); |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 97 | |
| 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 Lukaszewicz | 2c5fb614 | 2019-10-14 19:20:05 | [diff] [blame] | 109 | new_cmdline.AppendSwitch(switches::kLaunchAsBrowser); |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 110 | new_cmdline.AppendArgPath(test_file_path); |
| 111 | |
rvargas | 6bfa252 | 2014-12-10 02:09:53 | [diff] [blame] | 112 | base::Process process = |
| 113 | base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest()); |
| 114 | ASSERT_TRUE(process.IsValid()); |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 115 | |
| 116 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
rvargas | 2f70a15 | 2015-02-24 00:28:11 | [diff] [blame] | 117 | int exit_code; |
| 118 | ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(), |
| 119 | &exit_code)); |
| 120 | ASSERT_EQ(0, exit_code); |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 123 | namespace { |
| 124 | |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 125 | // TestFixture that appends --load-and-launch-app with an app before calling |
| 126 | // BrowserMain. |
| 127 | class LoadAndLaunchPlatformAppBrowserTest : public PlatformAppBrowserTest { |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 128 | protected: |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 129 | LoadAndLaunchPlatformAppBrowserTest() {} |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 130 | |
pgal.u-szeged | 214274b | 2014-10-28 11:59:48 | [diff] [blame] | 131 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 132 | PlatformAppBrowserTest::SetUpCommandLine(command_line); |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 133 | base::FilePath app_path = |
| 134 | test_data_dir_.AppendASCII("platform_apps").AppendASCII("minimal"); |
| 135 | command_line->AppendSwitchNative(apps::kLoadAndLaunchApp, app_path.value()); |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 136 | } |
| 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] | 5d5bea8 | 2013-12-18 03:20:32 | [diff] [blame] | 144 | CreateBrowser(ProfileManager::GetActiveUserProfile()); |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | private: |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 148 | DISALLOW_COPY_AND_ASSIGN(LoadAndLaunchPlatformAppBrowserTest); |
| 149 | }; |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 150 | |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 151 | // TestFixture that appends --load-and-launch-app with an extension before |
| 152 | // calling BrowserMain. |
| 153 | class 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] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | } // namespace |
| 177 | |
| 178 | // Case where Chrome is not running. |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 179 | IN_PROC_BROWSER_TEST_F(LoadAndLaunchPlatformAppBrowserTest, |
proberge | 9c7cba5 | 2016-10-04 17:07:43 | [diff] [blame] | 180 | LoadAndLaunchAppChromeNotRunning) { |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 181 | LoadAndLaunchApp(); |
| 182 | } |
| 183 | |
Lutz Justen | 58448845 | 2019-07-29 11:23:51 | [diff] [blame] | 184 | // 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 |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 190 | IN_PROC_BROWSER_TEST_F(LoadAndLaunchExtensionBrowserTest, |
Lutz Justen | 58448845 | 2019-07-29 11:23:51 | [diff] [blame] | 191 | MAYBE_LoadAndLaunchExtension) { |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 192 | const std::vector<base::string16>* errors = |
Devlin Cronin | 9722a72 | 2017-12-16 03:35:10 | [diff] [blame] | 193 | extensions::LoadErrorReporter::GetInstance()->GetErrors(); |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 194 | |
Nico Weber | cda4ff54 | 2019-07-29 20:05:22 | [diff] [blame] | 195 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 196 | // 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] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 213 | } // namespace apps |