Optimize implementation of "replace substrings after offset" utility function to
[chromium-blink-merge.git] / apps / load_and_launch_browsertest.cc
blob9432420ffe6b0e7302db1d845dfbd352e731edfe
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.
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.
9 #include "apps/switches.h"
10 #include "base/process/launch.h"
11 #include "base/test/test_timeouts.h"
12 #include "chrome/browser/apps/app_browsertest_util.h"
13 #include "chrome/browser/extensions/extension_browsertest.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/public/test/test_launcher.h"
18 #include "extensions/test/extension_test_message_listener.h"
20 using extensions::PlatformAppBrowserTest;
22 namespace apps {
24 namespace {
26 const char* kSwitchesToCopy[] = {
27 switches::kUserDataDir,
28 switches::kNoSandbox,
31 } // namespace
33 // TODO(jackhou): Enable this test once it works on OSX. It currently does not
34 // work for the same reason --app-id doesn't. See http://crbug.com/148465
35 #if defined(OS_MACOSX)
36 #define MAYBE_LoadAndLaunchAppChromeRunning \
37 DISABLED_LoadAndLaunchAppChromeRunning
38 #else
39 #define MAYBE_LoadAndLaunchAppChromeRunning LoadAndLaunchAppChromeRunning
40 #endif
42 // Case where Chrome is already running.
43 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
44 MAYBE_LoadAndLaunchAppChromeRunning) {
45 ExtensionTestMessageListener launched_listener("Launched", false);
47 const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
48 base::CommandLine new_cmdline(cmdline.GetProgram());
49 new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy,
50 arraysize(kSwitchesToCopy));
52 base::FilePath app_path = test_data_dir_
53 .AppendASCII("platform_apps")
54 .AppendASCII("minimal");
56 new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
57 app_path.value());
59 new_cmdline.AppendSwitch(content::kLaunchAsBrowser);
60 base::Process process =
61 base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest());
62 ASSERT_TRUE(process.IsValid());
64 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
65 ASSERT_TRUE(base::WaitForSingleProcess(process.Handle(),
66 TestTimeouts::action_timeout()));
69 // TODO(jackhou): Enable this test once it works on OSX. It currently does not
70 // work for the same reason --app-id doesn't. See http://crbug.com/148465
71 #if defined(OS_MACOSX)
72 #define MAYBE_LoadAndLaunchAppWithFile DISABLED_LoadAndLaunchAppWithFile
73 #else
74 #define MAYBE_LoadAndLaunchAppWithFile LoadAndLaunchAppWithFile
75 #endif
77 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
78 MAYBE_LoadAndLaunchAppWithFile) {
79 ExtensionTestMessageListener launched_listener("Launched", false);
81 const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
82 base::CommandLine new_cmdline(cmdline.GetProgram());
83 new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy,
84 arraysize(kSwitchesToCopy));
86 base::FilePath app_path = test_data_dir_
87 .AppendASCII("platform_apps")
88 .AppendASCII("load_and_launch_file");
90 base::FilePath test_file_path = test_data_dir_
91 .AppendASCII("platform_apps")
92 .AppendASCII("launch_files")
93 .AppendASCII("test.txt");
95 new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
96 app_path.value());
97 new_cmdline.AppendSwitch(content::kLaunchAsBrowser);
98 new_cmdline.AppendArgPath(test_file_path);
100 base::Process process =
101 base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest());
102 ASSERT_TRUE(process.IsValid());
104 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
105 ASSERT_TRUE(base::WaitForSingleProcess(process.Handle(),
106 TestTimeouts::action_timeout()));
109 namespace {
111 // TestFixture that appends --load-and-launch-app before calling BrowserMain.
112 class PlatformAppLoadAndLaunchBrowserTest : public PlatformAppBrowserTest {
113 protected:
114 PlatformAppLoadAndLaunchBrowserTest() {}
116 void SetUpCommandLine(base::CommandLine* command_line) override {
117 PlatformAppBrowserTest::SetUpCommandLine(command_line);
118 app_path_ = test_data_dir_
119 .AppendASCII("platform_apps")
120 .AppendASCII("minimal");
121 command_line->AppendSwitchNative(apps::kLoadAndLaunchApp,
122 app_path_.value());
125 void LoadAndLaunchApp() {
126 ExtensionTestMessageListener launched_listener("Launched", false);
127 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
129 // Start an actual browser because we can't shut down with just an app
130 // window.
131 CreateBrowser(ProfileManager::GetActiveUserProfile());
134 private:
135 base::FilePath app_path_;
137 DISALLOW_COPY_AND_ASSIGN(PlatformAppLoadAndLaunchBrowserTest);
140 } // namespace
143 // TODO(jackhou): Make this test not flaky on Vista or Linux Aura. See
144 // http://crbug.com/176897
145 #if defined(OS_WIN) || (defined(OS_LINUX) && defined(USE_AURA))
146 #define MAYBE_LoadAndLaunchAppChromeNotRunning \
147 DISABLED_LoadAndLaunchAppChromeNotRunning
148 #else
149 #define MAYBE_LoadAndLaunchAppChromeNotRunning \
150 LoadAndLaunchAppChromeNotRunning
151 #endif
153 // Case where Chrome is not running.
154 IN_PROC_BROWSER_TEST_F(PlatformAppLoadAndLaunchBrowserTest,
155 MAYBE_LoadAndLaunchAppChromeNotRunning) {
156 LoadAndLaunchApp();
159 } // namespace apps