load_and_launch_browsertest.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. // Tests for the --load-and-launch-app switch.
  5. // The two cases are when chrome is running and another process uses the switch
  6. // and when chrome is started from scratch.
  7. #include <iterator>
  8. #include "apps/switches.h"
  9. #include "base/process/launch.h"
  10. #include "base/strings/utf_string_conversions.h"
  11. #include "base/test/test_switches.h"
  12. #include "base/test/test_timeouts.h"
  13. #include "build/branding_buildflags.h"
  14. #include "build/build_config.h"
  15. #include "build/chromeos_buildflags.h"
  16. #include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
  17. #include "chrome/browser/extensions/extension_browsertest.h"
  18. #include "chrome/browser/extensions/load_error_reporter.h"
  19. #include "chrome/browser/profiles/profile_manager.h"
  20. #include "chrome/browser/ui/simple_message_box_internal.h"
  21. #include "chrome/common/chrome_switches.h"
  22. #include "content/public/common/content_switches.h"
  23. #include "content/public/test/browser_test.h"
  24. #include "content/public/test/test_launcher.h"
  25. #include "extensions/browser/extension_registry.h"
  26. #include "extensions/test/extension_test_message_listener.h"
  27. #include "sandbox/policy/switches.h"
  28. using extensions::PlatformAppBrowserTest;
  29. namespace apps {
  30. namespace {
  31. constexpr char kTestExtensionId[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
  32. // Lacros doesn't support launching with chrome already running. See the header
  33. // comment for InProcessBrowserTest::GetCommandLineForRelaunch().
  34. #if !BUILDFLAG(IS_CHROMEOS_LACROS)
  35. const char* kSwitchesToCopy[] = {
  36. sandbox::policy::switches::kNoSandbox,
  37. switches::kUserDataDir,
  38. };
  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 BUILDFLAG(IS_MAC)
  42. #define MAYBE_LoadAndLaunchAppChromeRunning \
  43. DISABLED_LoadAndLaunchAppChromeRunning
  44. #else
  45. #define MAYBE_LoadAndLaunchAppChromeRunning LoadAndLaunchAppChromeRunning
  46. #endif
  47. // Case where Chrome is already running.
  48. IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
  49. MAYBE_LoadAndLaunchAppChromeRunning) {
  50. ExtensionTestMessageListener launched_listener("Launched", false);
  51. const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
  52. base::CommandLine new_cmdline(cmdline.GetProgram());
  53. new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy,
  54. std::size(kSwitchesToCopy));
  55. base::FilePath app_path = test_data_dir_
  56. .AppendASCII("platform_apps")
  57. .AppendASCII("minimal");
  58. new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
  59. app_path.value());
  60. new_cmdline.AppendSwitch(switches::kLaunchAsBrowser);
  61. base::Process process =
  62. base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest());
  63. ASSERT_TRUE(process.IsValid());
  64. ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
  65. int exit_code;
  66. ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(),
  67. &exit_code));
  68. ASSERT_EQ(0, exit_code);
  69. }
  70. // TODO(jackhou): Enable this test once it works on OSX. It currently does not
  71. // work for the same reason --app-id doesn't. See http://crbug.com/148465.
  72. #if BUILDFLAG(IS_MAC)
  73. #define MAYBE_LoadAndLaunchAppWithFile DISABLED_LoadAndLaunchAppWithFile
  74. #else
  75. #define MAYBE_LoadAndLaunchAppWithFile LoadAndLaunchAppWithFile
  76. #endif
  77. IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
  78. MAYBE_LoadAndLaunchAppWithFile) {
  79. ExtensionTestMessageListener launched_listener("Launched", false);
  80. const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
  81. base::CommandLine new_cmdline(cmdline.GetProgram());
  82. new_cmdline.CopySwitchesFrom(cmdline, kSwitchesToCopy,
  83. std::size(kSwitchesToCopy));
  84. base::FilePath app_path = test_data_dir_
  85. .AppendASCII("platform_apps")
  86. .AppendASCII("load_and_launch_file");
  87. base::FilePath test_file_path = test_data_dir_
  88. .AppendASCII("platform_apps")
  89. .AppendASCII("launch_files")
  90. .AppendASCII("test.txt");
  91. new_cmdline.AppendSwitchNative(apps::kLoadAndLaunchApp,
  92. app_path.value());
  93. new_cmdline.AppendSwitch(switches::kLaunchAsBrowser);
  94. new_cmdline.AppendArgPath(test_file_path);
  95. base::Process process =
  96. base::LaunchProcess(new_cmdline, base::LaunchOptionsForTest());
  97. ASSERT_TRUE(process.IsValid());
  98. ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
  99. int exit_code;
  100. ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(),
  101. &exit_code));
  102. ASSERT_EQ(0, exit_code);
  103. }
  104. #endif // !BUILDFLAG(IS_CHROMEOS_LACROS)
  105. // TestFixture that appends --load-and-launch-app with an app before calling
  106. // BrowserMain.
  107. class LoadAndLaunchPlatformAppBrowserTest : public PlatformAppBrowserTest {
  108. public:
  109. LoadAndLaunchPlatformAppBrowserTest(
  110. const LoadAndLaunchPlatformAppBrowserTest&) = delete;
  111. LoadAndLaunchPlatformAppBrowserTest& operator=(
  112. const LoadAndLaunchPlatformAppBrowserTest&) = delete;
  113. protected:
  114. LoadAndLaunchPlatformAppBrowserTest() = default;
  115. void SetUpCommandLine(base::CommandLine* command_line) override {
  116. PlatformAppBrowserTest::SetUpCommandLine(command_line);
  117. base::FilePath app_path =
  118. test_data_dir_.AppendASCII("platform_apps").AppendASCII("minimal");
  119. command_line->AppendSwitchNative(apps::kLoadAndLaunchApp, app_path.value());
  120. }
  121. void LoadAndLaunchApp() {
  122. ExtensionTestMessageListener launched_listener("Launched", false);
  123. ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
  124. // Start an actual browser because we can't shut down with just an app
  125. // window.
  126. CreateBrowser(profile());
  127. }
  128. };
  129. // TestFixture that appends --load-and-launch-app with an extension before
  130. // calling BrowserMain.
  131. class LoadAndLaunchExtensionBrowserTest : public PlatformAppBrowserTest {
  132. public:
  133. LoadAndLaunchExtensionBrowserTest(const LoadAndLaunchExtensionBrowserTest&) =
  134. delete;
  135. LoadAndLaunchExtensionBrowserTest& operator=(
  136. const LoadAndLaunchExtensionBrowserTest&) = delete;
  137. protected:
  138. LoadAndLaunchExtensionBrowserTest() = default;
  139. void SetUpCommandLine(base::CommandLine* command_line) override {
  140. PlatformAppBrowserTest::SetUpCommandLine(command_line);
  141. base::FilePath app_path = test_data_dir_.AppendASCII("good")
  142. .AppendASCII("Extensions")
  143. .AppendASCII(kTestExtensionId)
  144. .AppendASCII("1.0.0.0");
  145. command_line->AppendSwitchNative(apps::kLoadAndLaunchApp, app_path.value());
  146. }
  147. void SetUpInProcessBrowserTestFixture() override {
  148. PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture();
  149. // Skip showing the error message box to avoid freezing the main thread.
  150. chrome::internal::g_should_skip_message_box_for_test = true;
  151. }
  152. };
  153. // Case where Chrome is not running.
  154. IN_PROC_BROWSER_TEST_F(LoadAndLaunchPlatformAppBrowserTest,
  155. LoadAndLaunchAppChromeNotRunning) {
  156. LoadAndLaunchApp();
  157. }
  158. // TODO(https://crbug.com/988160): Test is flaky on Windows.
  159. #if BUILDFLAG(IS_WIN)
  160. #define MAYBE_LoadAndLaunchExtension DISABLED_LoadAndLaunchExtension
  161. #else
  162. #define MAYBE_LoadAndLaunchExtension LoadAndLaunchExtension
  163. #endif
  164. IN_PROC_BROWSER_TEST_F(LoadAndLaunchExtensionBrowserTest,
  165. MAYBE_LoadAndLaunchExtension) {
  166. const std::vector<std::u16string>* errors =
  167. extensions::LoadErrorReporter::GetInstance()->GetErrors();
  168. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  169. // The error is skipped on official builds.
  170. EXPECT_TRUE(errors->empty());
  171. #else
  172. // Expect |extension_instead_of_app_error|.
  173. EXPECT_EQ(1u, errors->size());
  174. EXPECT_NE(std::u16string::npos,
  175. errors->at(0).find(
  176. u"App loading flags cannot be used to load extensions"));
  177. #endif
  178. extensions::ExtensionRegistry* registry =
  179. extensions::ExtensionRegistry::Get(profile());
  180. EXPECT_EQ(nullptr,
  181. registry->GetExtensionById(
  182. kTestExtensionId, extensions::ExtensionRegistry::EVERYTHING));
  183. }
  184. } // namespace
  185. } // namespace apps