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
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/test/test_launcher.h"
17#include "extensions/test/extension_test_message_listener.h"
18
19using extensions::PlatformAppBrowserTest;
20
21namespace apps {
22
23// TODO(jackhou): Enable this test once it works on OSX. It currently does not
24// work for the same reason --app-id doesn't. See http://crbug.com/148465
25#if defined(OS_MACOSX)
26#define MAYBE_LoadAndLaunchAppChromeRunning \
27        DISABLED_LoadAndLaunchAppChromeRunning
28#else
29#define MAYBE_LoadAndLaunchAppChromeRunning LoadAndLaunchAppChromeRunning
30#endif
31
32// Case where Chrome is already running.
33IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
34                       MAYBE_LoadAndLaunchAppChromeRunning) {
35  ExtensionTestMessageListener launched_listener("Launched", false);
36
37  const CommandLine& cmdline = *CommandLine::ForCurrentProcess();
38  CommandLine new_cmdline(cmdline.GetProgram());
39
40  const char* kSwitchNames[] = {
41    switches::kUserDataDir,
42  };
43  new_cmdline.CopySwitchesFrom(cmdline, kSwitchNames, arraysize(kSwitchNames));
44
45  base::FilePath app_path = test_data_dir_
46      .AppendASCII("platform_apps")
47      .AppendASCII("minimal");
48
49  new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
50                                 app_path.value());
51
52  new_cmdline.AppendSwitch(content::kLaunchAsBrowser);
53  base::ProcessHandle process;
54  base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest(), &process);
55  ASSERT_NE(base::kNullProcessHandle, process);
56
57  ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
58  ASSERT_TRUE(base::WaitForSingleProcess(
59      process, TestTimeouts::action_timeout()));
60}
61
62// TODO(jackhou): Enable this test once it works on OSX. It currently does not
63// work for the same reason --app-id doesn't. See http://crbug.com/148465
64#if defined(OS_MACOSX)
65#define MAYBE_LoadAndLaunchAppWithFile DISABLED_LoadAndLaunchAppWithFile
66#else
67#define MAYBE_LoadAndLaunchAppWithFile LoadAndLaunchAppWithFile
68#endif
69
70IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
71                       MAYBE_LoadAndLaunchAppWithFile) {
72  ExtensionTestMessageListener launched_listener("Launched", false);
73
74  const CommandLine& cmdline = *CommandLine::ForCurrentProcess();
75  CommandLine new_cmdline(cmdline.GetProgram());
76
77  const char* kSwitchNames[] = {
78    switches::kUserDataDir,
79  };
80  new_cmdline.CopySwitchesFrom(cmdline, kSwitchNames, arraysize(kSwitchNames));
81
82  base::FilePath app_path = test_data_dir_
83      .AppendASCII("platform_apps")
84      .AppendASCII("load_and_launch_file");
85
86  base::FilePath test_file_path = test_data_dir_
87      .AppendASCII("platform_apps")
88      .AppendASCII("launch_files")
89      .AppendASCII("test.txt");
90
91  new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
92                                 app_path.value());
93  new_cmdline.AppendSwitch(content::kLaunchAsBrowser);
94  new_cmdline.AppendArgPath(test_file_path);
95
96  base::ProcessHandle process;
97  base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest(), &process);
98  ASSERT_NE(base::kNullProcessHandle, process);
99
100  ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
101  ASSERT_TRUE(base::WaitForSingleProcess(
102      process, TestTimeouts::action_timeout()));
103}
104
105namespace {
106
107// TestFixture that appends --load-and-launch-app before calling BrowserMain.
108class PlatformAppLoadAndLaunchBrowserTest : public PlatformAppBrowserTest {
109 protected:
110  PlatformAppLoadAndLaunchBrowserTest() {}
111
112  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
113    PlatformAppBrowserTest::SetUpCommandLine(command_line);
114    app_path_ = test_data_dir_
115        .AppendASCII("platform_apps")
116        .AppendASCII("minimal");
117    command_line->AppendSwitchNative(apps::kLoadAndLaunchApp,
118                                     app_path_.value());
119  }
120
121  void LoadAndLaunchApp() {
122    ExtensionTestMessageListener launched_listener("Launched", false);
123    ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
124
125    // Start an actual browser because we can't shut down with just an app
126    // window.
127    CreateBrowser(ProfileManager::GetActiveUserProfile());
128  }
129
130 private:
131  base::FilePath app_path_;
132
133  DISALLOW_COPY_AND_ASSIGN(PlatformAppLoadAndLaunchBrowserTest);
134};
135
136}  // namespace
137
138
139// TODO(jackhou): Make this test not flaky on Vista or Linux Aura. See
140// http://crbug.com/176897
141#if defined(OS_WIN) || (defined(OS_LINUX) && defined(USE_AURA))
142#define MAYBE_LoadAndLaunchAppChromeNotRunning \
143        DISABLED_LoadAndLaunchAppChromeNotRunning
144#else
145#define MAYBE_LoadAndLaunchAppChromeNotRunning \
146        LoadAndLaunchAppChromeNotRunning
147#endif
148
149// Case where Chrome is not running.
150IN_PROC_BROWSER_TEST_F(PlatformAppLoadAndLaunchBrowserTest,
151                       MAYBE_LoadAndLaunchAppChromeNotRunning) {
152  LoadAndLaunchApp();
153}
154
155}  // namespace apps
156