blob: 7ebe7ca5645e31aa453312eb87a9de51e0b1c10e [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"
proberge80a37f32016-08-04 19:44:5511#include "base/strings/utf_string_conversions.h"
[email protected]9d02fa12013-02-19 05:12:5712#include "base/test/test_timeouts.h"
[email protected]bb4bec02013-08-15 11:26:5813#include "chrome/browser/apps/app_browsertest_util.h"
[email protected]9d02fa12013-02-19 05:12:5714#include "chrome/browser/extensions/extension_browsertest.h"
proberge80a37f32016-08-04 19:44:5515#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]9d02fa12013-02-19 05:12:5716#include "chrome/browser/profiles/profile_manager.h"
proberge80a37f32016-08-04 19:44:5517#include "chrome/browser/ui/simple_message_box_internal.h"
[email protected]9d02fa12013-02-19 05:12:5718#include "chrome/common/chrome_switches.h"
benwellsfa165112015-01-20 04:12:5019#include "content/public/common/content_switches.h"
[email protected]9d02fa12013-02-19 05:12:5720#include "content/public/test/test_launcher.h"
proberge80a37f32016-08-04 19:44:5521#include "extensions/browser/extension_registry.h"
lfg910f2f92014-09-19 05:31:0922#include "extensions/test/extension_test_message_listener.h"
[email protected]9d02fa12013-02-19 05:12:5723
[email protected]2a69b942013-05-31 09:37:5324using extensions::PlatformAppBrowserTest;
25
26namespace apps {
[email protected]9d02fa12013-02-19 05:12:5727
benwellsfa165112015-01-20 04:12:5028namespace {
29
30const char* kSwitchesToCopy[] = {
31 switches::kUserDataDir,
32 switches::kNoSandbox,
33};
34
proberge80a37f32016-08-04 19:44:5535constexpr char kTestExtensionId[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
36
benwellsfa165112015-01-20 04:12:5037} // namespace
38
[email protected]9d02fa12013-02-19 05:12:5739// 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.
49IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
50 MAYBE_LoadAndLaunchAppChromeRunning) {
51 ExtensionTestMessageListener launched_listener("Launched", false);
52
pgal.u-szeged214274b2014-10-28 11:59:4853 const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
54 base::CommandLine new_cmdline(cmdline.GetProgram());
benwellsfa165112015-01-20 04:12:5055 new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy,
56 arraysize(kSwitchesToCopy));
[email protected]9d02fa12013-02-19 05:12:5757
58 base::FilePath app_path = test_data_dir_
59 .AppendASCII("platform_apps")
60 .AppendASCII("minimal");
61
[email protected]2a69b942013-05-31 09:37:5362 new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
[email protected]9d02fa12013-02-19 05:12:5763 app_path.value());
64
65 new_cmdline.AppendSwitch(content::kLaunchAsBrowser);
rvargas6bfa2522014-12-10 02:09:5366 base::Process process =
67 base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest());
68 ASSERT_TRUE(process.IsValid());
[email protected]9d02fa12013-02-19 05:12:5769
70 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
rvargas2f70a152015-02-24 00:28:1171 int exit_code;
72 ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(),
73 &exit_code));
74 ASSERT_EQ(0, exit_code);
[email protected]9d02fa12013-02-19 05:12:5775}
76
[email protected]7b9faeb72013-06-11 12:20:1777// 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
85IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
86 MAYBE_LoadAndLaunchAppWithFile) {
87 ExtensionTestMessageListener launched_listener("Launched", false);
88
pgal.u-szeged214274b2014-10-28 11:59:4889 const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
90 base::CommandLine new_cmdline(cmdline.GetProgram());
benwellsfa165112015-01-20 04:12:5091 new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy,
92 arraysize(kSwitchesToCopy));
[email protected]7b9faeb72013-06-11 12:20:1793
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
rvargas6bfa2522014-12-10 02:09:53108 base::Process process =
109 base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest());
110 ASSERT_TRUE(process.IsValid());
[email protected]7b9faeb72013-06-11 12:20:17111
112 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
rvargas2f70a152015-02-24 00:28:11113 int exit_code;
114 ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(),
115 &exit_code));
116 ASSERT_EQ(0, exit_code);
[email protected]7b9faeb72013-06-11 12:20:17117}
118
[email protected]9d02fa12013-02-19 05:12:57119namespace {
120
proberge80a37f32016-08-04 19:44:55121// TestFixture that appends --load-and-launch-app with an app before calling
122// BrowserMain.
123class LoadAndLaunchPlatformAppBrowserTest : public PlatformAppBrowserTest {
[email protected]9d02fa12013-02-19 05:12:57124 protected:
proberge80a37f32016-08-04 19:44:55125 LoadAndLaunchPlatformAppBrowserTest() {}
[email protected]9d02fa12013-02-19 05:12:57126
pgal.u-szeged214274b2014-10-28 11:59:48127 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]9d02fa12013-02-19 05:12:57128 PlatformAppBrowserTest::SetUpCommandLine(command_line);
proberge80a37f32016-08-04 19:44:55129 base::FilePath app_path =
130 test_data_dir_.AppendASCII("platform_apps").AppendASCII("minimal");
131 command_line->AppendSwitchNative(apps::kLoadAndLaunchApp, app_path.value());
[email protected]9d02fa12013-02-19 05:12:57132 }
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]5d5bea82013-12-18 03:20:32140 CreateBrowser(ProfileManager::GetActiveUserProfile());
[email protected]9d02fa12013-02-19 05:12:57141 }
142
143 private:
proberge80a37f32016-08-04 19:44:55144 DISALLOW_COPY_AND_ASSIGN(LoadAndLaunchPlatformAppBrowserTest);
145};
[email protected]9d02fa12013-02-19 05:12:57146
proberge80a37f32016-08-04 19:44:55147// TestFixture that appends --load-and-launch-app with an extension before
148// calling BrowserMain.
149class 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]9d02fa12013-02-19 05:12:57170};
171
172} // namespace
173
174// Case where Chrome is not running.
proberge80a37f32016-08-04 19:44:55175IN_PROC_BROWSER_TEST_F(LoadAndLaunchPlatformAppBrowserTest,
proberge9c7cba52016-10-04 17:07:43176 LoadAndLaunchAppChromeNotRunning) {
[email protected]9d02fa12013-02-19 05:12:57177 LoadAndLaunchApp();
178}
179
proberge80a37f32016-08-04 19:44:55180IN_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]2a69b942013-05-31 09:37:53203} // namespace apps