chrome_main_process_singleton_browsertest.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright (c) 2012 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. #include "base/command_line.h"
  5. #include "base/process/launch.h"
  6. #include "base/run_loop.h"
  7. #include "base/strings/stringprintf.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "base/test/bind.h"
  10. #include "build/build_config.h"
  11. #include "build/chromeos_buildflags.h"
  12. #include "chrome/browser/browser_process.h"
  13. #include "chrome/browser/buildflags.h"
  14. #include "chrome/browser/profiles/profile_attributes_entry.h"
  15. #include "chrome/browser/profiles/profile_attributes_storage.h"
  16. #include "chrome/browser/profiles/profile_manager.h"
  17. #include "chrome/browser/profiles/profile_test_util.h"
  18. #include "chrome/browser/ui/browser.h"
  19. #include "chrome/browser/ui/browser_commands.h"
  20. #include "chrome/browser/ui/browser_finder.h"
  21. #include "chrome/browser/ui/tabs/tab_strip_model.h"
  22. #include "chrome/common/chrome_paths.h"
  23. #include "chrome/common/chrome_switches.h"
  24. #include "chrome/test/base/in_process_browser_test.h"
  25. #include "chrome/test/base/ui_test_utils.h"
  26. #include "content/public/browser/navigation_controller.h"
  27. #include "content/public/browser/navigation_entry.h"
  28. #include "content/public/browser/web_contents.h"
  29. #include "content/public/test/browser_test.h"
  30. #include "net/base/filename_util.h"
  31. #if !BUILDFLAG(ENABLE_PROCESS_SINGLETON)
  32. #error Not supported on this platform.
  33. #endif
  34. class ChromeMainTest : public InProcessBrowserTest {
  35. public:
  36. ChromeMainTest() {}
  37. void Relaunch(const base::CommandLine& new_command_line) {
  38. base::LaunchProcess(new_command_line, base::LaunchOptionsForTest());
  39. }
  40. Profile* CreateProfile(const base::FilePath& basename) {
  41. ProfileManager* profile_manager = g_browser_process->profile_manager();
  42. base::FilePath profile_path =
  43. profile_manager->user_data_dir().Append(basename);
  44. return profiles::testing::CreateProfileSync(profile_manager, profile_path);
  45. }
  46. // Gets the relaunch command line with the kProfileEmail switch.
  47. base::CommandLine GetCommandLineForRelaunchWithEmail(
  48. const std::string& email) {
  49. base::CommandLine command_line = GetCommandLineForRelaunch();
  50. command_line.AppendArg(
  51. base::StringPrintf("--profile-email=%s", email.c_str()));
  52. return command_line;
  53. }
  54. };
  55. // Make sure that the second invocation creates a new window.
  56. IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunch) {
  57. Relaunch(GetCommandLineForRelaunch());
  58. ui_test_utils::WaitForBrowserToOpen();
  59. ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile()));
  60. }
  61. IN_PROC_BROWSER_TEST_F(ChromeMainTest, ReuseBrowserInstanceWhenOpeningFile) {
  62. base::FilePath test_file_path = ui_test_utils::GetTestFilePath(
  63. base::FilePath(), base::FilePath().AppendASCII("empty.html"));
  64. base::CommandLine new_command_line(GetCommandLineForRelaunch());
  65. new_command_line.AppendArgPath(test_file_path);
  66. Relaunch(new_command_line);
  67. ui_test_utils::TabAddedWaiter(browser()).Wait();
  68. GURL url = net::FilePathToFileURL(test_file_path);
  69. content::WebContents* tab =
  70. browser()->tab_strip_model()->GetActiveWebContents();
  71. ASSERT_EQ(url, tab->GetVisibleURL());
  72. }
  73. // ChromeMainTest.SecondLaunchWithIncognitoUrl is flaky on Win and Linux.
  74. // http://crbug.com/130395
  75. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  76. #define MAYBE_SecondLaunchWithIncognitoUrl DISABLED_SecondLaunchWithIncognitoUrl
  77. #else
  78. #define MAYBE_SecondLaunchWithIncognitoUrl SecondLaunchWithIncognitoUrl
  79. #endif
  80. IN_PROC_BROWSER_TEST_F(ChromeMainTest, MAYBE_SecondLaunchWithIncognitoUrl) {
  81. // We should start with one normal window.
  82. ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile()));
  83. // Run with --incognito switch and an URL specified.
  84. base::FilePath test_file_path = ui_test_utils::GetTestFilePath(
  85. base::FilePath(), base::FilePath().AppendASCII("empty.html"));
  86. base::CommandLine new_command_line(GetCommandLineForRelaunch());
  87. new_command_line.AppendSwitch(switches::kIncognito);
  88. new_command_line.AppendArgPath(test_file_path);
  89. Relaunch(new_command_line);
  90. // There should be one normal and one incognito window now.
  91. Relaunch(new_command_line);
  92. ui_test_utils::WaitForBrowserToOpen();
  93. ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
  94. ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile()));
  95. }
  96. IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchFromIncognitoWithNormalUrl) {
  97. Profile* const profile = browser()->profile();
  98. // We should start with one normal window.
  99. ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(profile));
  100. // Create an incognito window.
  101. chrome::NewIncognitoWindow(profile);
  102. ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
  103. ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(profile));
  104. // Close the first window.
  105. CloseBrowserSynchronously(browser());
  106. // There should only be the incognito window open now.
  107. ASSERT_EQ(1u, chrome::GetTotalBrowserCount());
  108. ASSERT_EQ(0u, chrome::GetTabbedBrowserCount(profile));
  109. // Run with just an URL specified, no --incognito switch.
  110. base::FilePath test_file_path = ui_test_utils::GetTestFilePath(
  111. base::FilePath(), base::FilePath().AppendASCII("empty.html"));
  112. base::CommandLine new_command_line(GetCommandLineForRelaunch());
  113. new_command_line.AppendArgPath(test_file_path);
  114. Relaunch(new_command_line);
  115. ui_test_utils::WaitForBrowserToOpen();
  116. // There should be one normal and one incognito window now.
  117. ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
  118. ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(profile));
  119. }
  120. // Multi-profile is not supported on Ash.
  121. #if !BUILDFLAG(IS_CHROMEOS_ASH)
  122. IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchWithProfileDir) {
  123. const base::FilePath kProfileDir(FILE_PATH_LITERAL("Other"));
  124. Profile* other_profile = CreateProfile(kProfileDir);
  125. ASSERT_TRUE(other_profile);
  126. // Pass the other profile path on the command line.
  127. base::CommandLine other_command_line = GetCommandLineForRelaunch();
  128. other_command_line.AppendSwitchPath(switches::kProfileDirectory, kProfileDir);
  129. size_t original_browser_count = chrome::GetTotalBrowserCount();
  130. Relaunch(other_command_line);
  131. Browser* other_browser = ui_test_utils::WaitForBrowserToOpen();
  132. ASSERT_TRUE(other_browser);
  133. EXPECT_EQ(other_browser->profile(), other_profile);
  134. EXPECT_EQ(original_browser_count + 1, chrome::GetTotalBrowserCount());
  135. }
  136. IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchWithProfileEmail) {
  137. const base::FilePath kProfileDir1(FILE_PATH_LITERAL("Profile1"));
  138. const base::FilePath kProfileDir2(FILE_PATH_LITERAL("Profile2"));
  139. const std::string kProfileEmail1 = "example@gmail.com";
  140. // Unicode emails are supported.
  141. const std::string kProfileEmail2 =
  142. "\xe4\xbd\xa0\xe5\xa5\xbd\x40\x67\x6d\x61\x69\x6c\x2e\x63\x6f\x6d\x0a";
  143. ProfileAttributesStorage* storage =
  144. &g_browser_process->profile_manager()->GetProfileAttributesStorage();
  145. Profile* profile1 = CreateProfile(kProfileDir1);
  146. ASSERT_TRUE(profile1);
  147. storage->GetProfileAttributesWithPath(profile1->GetPath())
  148. ->SetAuthInfo("gaia_id_1", base::UTF8ToUTF16(kProfileEmail1),
  149. /*is_consented_primary_account=*/false);
  150. Profile* profile2 = CreateProfile(kProfileDir2);
  151. ASSERT_TRUE(profile2);
  152. storage->GetProfileAttributesWithPath(profile2->GetPath())
  153. ->SetAuthInfo("gaia_id_2", base::UTF8ToUTF16(kProfileEmail2),
  154. /*is_consented_primary_account=*/false);
  155. base::RunLoop run_loop;
  156. g_browser_process->FlushLocalStateAndReply(
  157. base::BindLambdaForTesting([&run_loop]() { run_loop.Quit(); }));
  158. run_loop.Run();
  159. // Normal email.
  160. size_t original_browser_count = chrome::GetTotalBrowserCount();
  161. Relaunch(GetCommandLineForRelaunchWithEmail(kProfileEmail1));
  162. Browser* new_browser = ui_test_utils::WaitForBrowserToOpen();
  163. ASSERT_TRUE(new_browser);
  164. EXPECT_EQ(new_browser->profile(), profile1);
  165. EXPECT_EQ(original_browser_count + 1, chrome::GetTotalBrowserCount());
  166. // Non-ASCII email.
  167. Relaunch(GetCommandLineForRelaunchWithEmail(kProfileEmail2));
  168. new_browser = ui_test_utils::WaitForBrowserToOpen();
  169. ASSERT_TRUE(new_browser);
  170. EXPECT_EQ(new_browser->profile(), profile2);
  171. EXPECT_EQ(original_browser_count + 2, chrome::GetTotalBrowserCount());
  172. }
  173. #endif // !BUILDFLAG(IS_CHROMEOS_ASH)