[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" |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 12 | #include "base/test/test_timeouts.h" |
[email protected] | bb4bec0 | 2013-08-15 11:26:58 | [diff] [blame] | 13 | #include "chrome/browser/apps/app_browsertest_util.h" |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 14 | #include "chrome/browser/extensions/extension_browsertest.h" |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 15 | #include "chrome/browser/extensions/extension_error_reporter.h" |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 16 | #include "chrome/browser/profiles/profile_manager.h" |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 17 | #include "chrome/browser/ui/simple_message_box_internal.h" |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 18 | #include "chrome/common/chrome_switches.h" |
benwells | fa16511 | 2015-01-20 04:12:50 | [diff] [blame] | 19 | #include "content/public/common/content_switches.h" |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 20 | #include "content/public/test/test_launcher.h" |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 21 | #include "extensions/browser/extension_registry.h" |
lfg | 910f2f9 | 2014-09-19 05:31:09 | [diff] [blame] | 22 | #include "extensions/test/extension_test_message_listener.h" |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 23 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 24 | using extensions::PlatformAppBrowserTest; |
| 25 | |
| 26 | namespace apps { |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 27 | |
benwells | fa16511 | 2015-01-20 04:12:50 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | const char* kSwitchesToCopy[] = { |
| 31 | switches::kUserDataDir, |
| 32 | switches::kNoSandbox, |
| 33 | }; |
| 34 | |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 35 | constexpr char kTestExtensionId[] = "behllobkkfkfnphdnhnkndlbkcpglgmj"; |
| 36 | |
benwells | fa16511 | 2015-01-20 04:12:50 | [diff] [blame] | 37 | } // namespace |
| 38 | |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 39 | // TODO(jackhou): Enable this test once it works on OSX. It currently does not |
| 40 | // work for the same reason --app-id doesn't. See http://crbug.com/148465 |
| 41 | #if defined(OS_MACOSX) |
| 42 | #define MAYBE_LoadAndLaunchAppChromeRunning \ |
| 43 | DISABLED_LoadAndLaunchAppChromeRunning |
| 44 | #else |
| 45 | #define MAYBE_LoadAndLaunchAppChromeRunning LoadAndLaunchAppChromeRunning |
| 46 | #endif |
| 47 | |
| 48 | // Case where Chrome is already running. |
| 49 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, |
| 50 | MAYBE_LoadAndLaunchAppChromeRunning) { |
| 51 | ExtensionTestMessageListener launched_listener("Launched", false); |
| 52 | |
pgal.u-szeged | 214274b | 2014-10-28 11:59:48 | [diff] [blame] | 53 | const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess(); |
| 54 | base::CommandLine new_cmdline(cmdline.GetProgram()); |
benwells | fa16511 | 2015-01-20 04:12:50 | [diff] [blame] | 55 | new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy, |
| 56 | arraysize(kSwitchesToCopy)); |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 57 | |
| 58 | base::FilePath app_path = test_data_dir_ |
| 59 | .AppendASCII("platform_apps") |
| 60 | .AppendASCII("minimal"); |
| 61 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 62 | new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp, |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 63 | app_path.value()); |
| 64 | |
| 65 | new_cmdline.AppendSwitch(content::kLaunchAsBrowser); |
rvargas | 6bfa252 | 2014-12-10 02:09:53 | [diff] [blame] | 66 | base::Process process = |
| 67 | base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest()); |
| 68 | ASSERT_TRUE(process.IsValid()); |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 69 | |
| 70 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
rvargas | 2f70a15 | 2015-02-24 00:28:11 | [diff] [blame] | 71 | int exit_code; |
| 72 | ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(), |
| 73 | &exit_code)); |
| 74 | ASSERT_EQ(0, exit_code); |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 77 | // TODO(jackhou): Enable this test once it works on OSX. It currently does not |
| 78 | // work for the same reason --app-id doesn't. See http://crbug.com/148465 |
| 79 | #if defined(OS_MACOSX) |
| 80 | #define MAYBE_LoadAndLaunchAppWithFile DISABLED_LoadAndLaunchAppWithFile |
| 81 | #else |
| 82 | #define MAYBE_LoadAndLaunchAppWithFile LoadAndLaunchAppWithFile |
| 83 | #endif |
| 84 | |
| 85 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, |
| 86 | MAYBE_LoadAndLaunchAppWithFile) { |
| 87 | ExtensionTestMessageListener launched_listener("Launched", false); |
| 88 | |
pgal.u-szeged | 214274b | 2014-10-28 11:59:48 | [diff] [blame] | 89 | const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess(); |
| 90 | base::CommandLine new_cmdline(cmdline.GetProgram()); |
benwells | fa16511 | 2015-01-20 04:12:50 | [diff] [blame] | 91 | new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy, |
| 92 | arraysize(kSwitchesToCopy)); |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 93 | |
| 94 | base::FilePath app_path = test_data_dir_ |
| 95 | .AppendASCII("platform_apps") |
| 96 | .AppendASCII("load_and_launch_file"); |
| 97 | |
| 98 | base::FilePath test_file_path = test_data_dir_ |
| 99 | .AppendASCII("platform_apps") |
| 100 | .AppendASCII("launch_files") |
| 101 | .AppendASCII("test.txt"); |
| 102 | |
| 103 | new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp, |
| 104 | app_path.value()); |
| 105 | new_cmdline.AppendSwitch(content::kLaunchAsBrowser); |
| 106 | new_cmdline.AppendArgPath(test_file_path); |
| 107 | |
rvargas | 6bfa252 | 2014-12-10 02:09:53 | [diff] [blame] | 108 | base::Process process = |
| 109 | base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest()); |
| 110 | ASSERT_TRUE(process.IsValid()); |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 111 | |
| 112 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
rvargas | 2f70a15 | 2015-02-24 00:28:11 | [diff] [blame] | 113 | int exit_code; |
| 114 | ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(), |
| 115 | &exit_code)); |
| 116 | ASSERT_EQ(0, exit_code); |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 119 | namespace { |
| 120 | |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 121 | // TestFixture that appends --load-and-launch-app with an app before calling |
| 122 | // BrowserMain. |
| 123 | class LoadAndLaunchPlatformAppBrowserTest : public PlatformAppBrowserTest { |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 124 | protected: |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 125 | LoadAndLaunchPlatformAppBrowserTest() {} |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 126 | |
pgal.u-szeged | 214274b | 2014-10-28 11:59:48 | [diff] [blame] | 127 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 128 | PlatformAppBrowserTest::SetUpCommandLine(command_line); |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 129 | base::FilePath app_path = |
| 130 | test_data_dir_.AppendASCII("platform_apps").AppendASCII("minimal"); |
| 131 | command_line->AppendSwitchNative(apps::kLoadAndLaunchApp, app_path.value()); |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void LoadAndLaunchApp() { |
| 135 | ExtensionTestMessageListener launched_listener("Launched", false); |
| 136 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
| 137 | |
| 138 | // Start an actual browser because we can't shut down with just an app |
| 139 | // window. |
[email protected] | 5d5bea8 | 2013-12-18 03:20:32 | [diff] [blame] | 140 | CreateBrowser(ProfileManager::GetActiveUserProfile()); |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | private: |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 144 | DISALLOW_COPY_AND_ASSIGN(LoadAndLaunchPlatformAppBrowserTest); |
| 145 | }; |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 146 | |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 147 | // TestFixture that appends --load-and-launch-app with an extension before |
| 148 | // calling BrowserMain. |
| 149 | class LoadAndLaunchExtensionBrowserTest : public PlatformAppBrowserTest { |
| 150 | protected: |
| 151 | LoadAndLaunchExtensionBrowserTest() {} |
| 152 | |
| 153 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 154 | PlatformAppBrowserTest::SetUpCommandLine(command_line); |
| 155 | base::FilePath app_path = test_data_dir_.AppendASCII("good") |
| 156 | .AppendASCII("Extensions") |
| 157 | .AppendASCII(kTestExtensionId) |
| 158 | .AppendASCII("1.0.0.0"); |
| 159 | command_line->AppendSwitchNative(apps::kLoadAndLaunchApp, app_path.value()); |
| 160 | } |
| 161 | |
| 162 | void SetUpInProcessBrowserTestFixture() override { |
| 163 | PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 164 | |
| 165 | // Skip showing the error message box to avoid freezing the main thread. |
| 166 | chrome::internal::g_should_skip_message_box_for_test = true; |
| 167 | } |
| 168 | |
| 169 | DISALLOW_COPY_AND_ASSIGN(LoadAndLaunchExtensionBrowserTest); |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | } // namespace |
| 173 | |
| 174 | // Case where Chrome is not running. |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 175 | IN_PROC_BROWSER_TEST_F(LoadAndLaunchPlatformAppBrowserTest, |
proberge | 9c7cba5 | 2016-10-04 17:07:43 | [diff] [blame] | 176 | LoadAndLaunchAppChromeNotRunning) { |
[email protected] | 9d02fa1 | 2013-02-19 05:12:57 | [diff] [blame] | 177 | LoadAndLaunchApp(); |
| 178 | } |
| 179 | |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 180 | IN_PROC_BROWSER_TEST_F(LoadAndLaunchExtensionBrowserTest, |
| 181 | LoadAndLaunchExtension) { |
| 182 | const std::vector<base::string16>* errors = |
| 183 | ExtensionErrorReporter::GetInstance()->GetErrors(); |
| 184 | |
| 185 | #if defined(GOOGLE_CHROME_BUILD) |
| 186 | // The error is skipped on official builds. |
| 187 | EXPECT_TRUE(errors->empty()); |
| 188 | #else |
| 189 | // Expect |extension_instead_of_app_error|. |
| 190 | EXPECT_EQ(1u, errors->size()); |
| 191 | EXPECT_NE(base::string16::npos, |
| 192 | errors->at(0).find(base::ASCIIToUTF16( |
| 193 | "App loading flags cannot be used to load extensions"))); |
| 194 | #endif |
| 195 | |
| 196 | extensions::ExtensionRegistry* registry = |
| 197 | extensions::ExtensionRegistry::Get(profile()); |
| 198 | EXPECT_EQ(nullptr, |
| 199 | registry->GetExtensionById( |
| 200 | kTestExtensionId, extensions::ExtensionRegistry::EVERYTHING)); |
| 201 | } |
| 202 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 203 | } // namespace apps |