12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Tests for the --load-and-launch-app switch.
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The two cases are when chrome is running and another process uses the switch
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// and when chrome is started from scratch.
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "apps/switches.h"
10a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch#include "base/process/launch.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/test/test_timeouts.h"
123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "chrome/browser/apps/app_browsertest_util.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/extension_browsertest.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/chrome_switches.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/test/test_launcher.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/test/extension_test_message_listener.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)using extensions::PlatformAppBrowserTest;
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace apps {
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// TODO(jackhou): Enable this test once it works on OSX. It currently does not
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// work for the same reason --app-id doesn't. See http://crbug.com/148465
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_MACOSX)
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define MAYBE_LoadAndLaunchAppChromeRunning \
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        DISABLED_LoadAndLaunchAppChromeRunning
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define MAYBE_LoadAndLaunchAppChromeRunning LoadAndLaunchAppChromeRunning
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Case where Chrome is already running.
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       MAYBE_LoadAndLaunchAppChromeRunning) {
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ExtensionTestMessageListener launched_listener("Launched", false);
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const CommandLine& cmdline = *CommandLine::ForCurrentProcess();
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CommandLine new_cmdline(cmdline.GetProgram());
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const char* kSwitchNames[] = {
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    switches::kUserDataDir,
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  new_cmdline.CopySwitchesFrom(cmdline, kSwitchNames, arraysize(kSwitchNames));
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath app_path = test_data_dir_
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      .AppendASCII("platform_apps")
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      .AppendASCII("minimal");
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 app_path.value());
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  new_cmdline.AppendSwitch(content::kLaunchAsBrowser);
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::ProcessHandle process;
54c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest(), &process);
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_NE(base::kNullProcessHandle, process);
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_TRUE(base::WaitForSingleProcess(
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      process, TestTimeouts::action_timeout()));
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
625e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)// TODO(jackhou): Enable this test once it works on OSX. It currently does not
635e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)// work for the same reason --app-id doesn't. See http://crbug.com/148465
645e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#if defined(OS_MACOSX)
655e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#define MAYBE_LoadAndLaunchAppWithFile DISABLED_LoadAndLaunchAppWithFile
665e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#else
675e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#define MAYBE_LoadAndLaunchAppWithFile LoadAndLaunchAppWithFile
685e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#endif
695e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
705e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
715e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)                       MAYBE_LoadAndLaunchAppWithFile) {
725e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  ExtensionTestMessageListener launched_listener("Launched", false);
735e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
745e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  const CommandLine& cmdline = *CommandLine::ForCurrentProcess();
755e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  CommandLine new_cmdline(cmdline.GetProgram());
765e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
775e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  const char* kSwitchNames[] = {
785e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)    switches::kUserDataDir,
795e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  };
805e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  new_cmdline.CopySwitchesFrom(cmdline, kSwitchNames, arraysize(kSwitchNames));
815e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
825e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  base::FilePath app_path = test_data_dir_
835e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      .AppendASCII("platform_apps")
845e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      .AppendASCII("load_and_launch_file");
855e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
865e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  base::FilePath test_file_path = test_data_dir_
875e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      .AppendASCII("platform_apps")
885e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      .AppendASCII("launch_files")
895e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      .AppendASCII("test.txt");
905e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
915e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
925e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)                                 app_path.value());
935e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  new_cmdline.AppendSwitch(content::kLaunchAsBrowser);
945e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  new_cmdline.AppendArgPath(test_file_path);
955e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
965e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  base::ProcessHandle process;
97c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest(), &process);
985e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  ASSERT_NE(base::kNullProcessHandle, process);
995e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
1005e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1015e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)  ASSERT_TRUE(base::WaitForSingleProcess(
1025e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)      process, TestTimeouts::action_timeout()));
1035e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)}
1045e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// TestFixture that appends --load-and-launch-app before calling BrowserMain.
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PlatformAppLoadAndLaunchBrowserTest : public PlatformAppBrowserTest {
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) protected:
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PlatformAppLoadAndLaunchBrowserTest() {}
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PlatformAppBrowserTest::SetUpCommandLine(command_line);
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    app_path_ = test_data_dir_
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        .AppendASCII("platform_apps")
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        .AppendASCII("minimal");
117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    command_line->AppendSwitchNative(apps::kLoadAndLaunchApp,
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     app_path_.value());
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void LoadAndLaunchApp() {
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ExtensionTestMessageListener launched_listener("Launched", false);
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Start an actual browser because we can't shut down with just an app
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // window.
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CreateBrowser(ProfileManager::GetActiveUserProfile());
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath app_path_;
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PlatformAppLoadAndLaunchBrowserTest);
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
139f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// TODO(jackhou): Make this test not flaky on Vista or Linux Aura. See
140f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// http://crbug.com/176897
141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#if defined(OS_WIN) || (defined(OS_LINUX) && defined(USE_AURA))
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define MAYBE_LoadAndLaunchAppChromeNotRunning \
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        DISABLED_LoadAndLaunchAppChromeNotRunning
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define MAYBE_LoadAndLaunchAppChromeNotRunning \
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        LoadAndLaunchAppChromeNotRunning
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Case where Chrome is not running.
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(PlatformAppLoadAndLaunchBrowserTest,
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       MAYBE_LoadAndLaunchAppChromeNotRunning) {
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LoadAndLaunchApp();
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace apps
156