File: chrome_main_process_singleton_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (191 lines) | stat: -rw-r--r-- 7,983 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "base/process/launch.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/buildflags.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "google_apis/gaia/gaia_id.h"
#include "net/base/filename_util.h"

#if !BUILDFLAG(ENABLE_PROCESS_SINGLETON)
#error Not supported on this platform.
#endif

class ChromeMainTest : public InProcessBrowserTest {
 public:
  ChromeMainTest() = default;

  void Relaunch(const base::CommandLine& new_command_line) {
    base::LaunchProcess(new_command_line, base::LaunchOptionsForTest());
  }

  Profile* CreateProfile(const base::FilePath& basename) {
    ProfileManager* profile_manager = g_browser_process->profile_manager();
    base::FilePath profile_path =
        profile_manager->user_data_dir().Append(basename);
    return &profiles::testing::CreateProfileSync(profile_manager, profile_path);
  }

  // Gets the relaunch command line with the kProfileEmail switch.
  base::CommandLine GetCommandLineForRelaunchWithEmail(
      const std::string& email) {
    base::CommandLine command_line = GetCommandLineForRelaunch();
    command_line.AppendArg(
        base::StringPrintf("--profile-email=%s", email.c_str()));
    return command_line;
  }
};

// Make sure that the second invocation creates a new window.
IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunch) {
  Relaunch(GetCommandLineForRelaunch());
  ui_test_utils::WaitForBrowserToOpen();
  ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile()));
}

IN_PROC_BROWSER_TEST_F(ChromeMainTest, ReuseBrowserInstanceWhenOpeningFile) {
  base::FilePath test_file_path = ui_test_utils::GetTestFilePath(
      base::FilePath(), base::FilePath().AppendASCII("empty.html"));
  base::CommandLine new_command_line(GetCommandLineForRelaunch());
  new_command_line.AppendArgPath(test_file_path);
  Relaunch(new_command_line);
  ui_test_utils::TabAddedWaiter(browser()).Wait();

  GURL url = net::FilePathToFileURL(test_file_path);
  content::WebContents* tab =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_EQ(url, tab->GetVisibleURL());
}

IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchWithIncognitoUrl) {
  // We should start with one normal window.
  ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile()));

  // Run with --incognito switch and an URL specified.
  base::FilePath test_file_path = ui_test_utils::GetTestFilePath(
      base::FilePath(), base::FilePath().AppendASCII("empty.html"));
  base::CommandLine new_command_line(GetCommandLineForRelaunch());
  new_command_line.AppendSwitch(switches::kIncognito);
  new_command_line.AppendArgPath(test_file_path);

  Relaunch(new_command_line);

  // There should be one normal and one incognito window now.
  Relaunch(new_command_line);
  ui_test_utils::WaitForBrowserToOpen();
  ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
  ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile()));
}

IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchFromIncognitoWithNormalUrl) {
  Profile* const profile = browser()->profile();

  // We should start with one normal window.
  ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(profile));

  // Create an incognito window.
  chrome::NewIncognitoWindow(profile);

  ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
  ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(profile));

  // Close the first window.
  CloseBrowserSynchronously(browser());

  // There should only be the incognito window open now.
  ASSERT_EQ(1u, chrome::GetTotalBrowserCount());
  ASSERT_EQ(0u, chrome::GetTabbedBrowserCount(profile));

  // Run with just an URL specified, no --incognito switch.
  base::FilePath test_file_path = ui_test_utils::GetTestFilePath(
      base::FilePath(), base::FilePath().AppendASCII("empty.html"));
  base::CommandLine new_command_line(GetCommandLineForRelaunch());
  new_command_line.AppendArgPath(test_file_path);
  Relaunch(new_command_line);
  ui_test_utils::WaitForBrowserToOpen();

  // There should be one normal and one incognito window now.
  ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
  ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(profile));
}

// Multi-profile is not supported on ChromeOS.
#if !BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchWithProfileDir) {
  const base::FilePath kProfileDir(FILE_PATH_LITERAL("Other"));
  Profile* other_profile = CreateProfile(kProfileDir);
  ASSERT_TRUE(other_profile);

  // Pass the other profile path on the command line.
  base::CommandLine other_command_line = GetCommandLineForRelaunch();
  other_command_line.AppendSwitchPath(switches::kProfileDirectory, kProfileDir);
  size_t original_browser_count = chrome::GetTotalBrowserCount();
  Relaunch(other_command_line);
  Browser* other_browser = ui_test_utils::WaitForBrowserToOpen();
  ASSERT_TRUE(other_browser);
  EXPECT_EQ(other_browser->profile(), other_profile);
  EXPECT_EQ(original_browser_count + 1, chrome::GetTotalBrowserCount());
}

IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchWithProfileEmail) {
  const base::FilePath kProfileDir1(FILE_PATH_LITERAL("Profile1"));
  const base::FilePath kProfileDir2(FILE_PATH_LITERAL("Profile2"));
  const std::string kProfileEmail1 = "example@gmail.com";
  // Unicode emails are supported.
  const std::string kProfileEmail2 =
      "\xe4\xbd\xa0\xe5\xa5\xbd\x40\x67\x6d\x61\x69\x6c\x2e\x63\x6f\x6d\x0a";
  ProfileAttributesStorage* storage =
      &g_browser_process->profile_manager()->GetProfileAttributesStorage();
  Profile* profile1 = CreateProfile(kProfileDir1);
  ASSERT_TRUE(profile1);
  storage->GetProfileAttributesWithPath(profile1->GetPath())
      ->SetAuthInfo(GaiaId("gaia_id_1"), base::UTF8ToUTF16(kProfileEmail1),
                    /*is_consented_primary_account=*/false);
  Profile* profile2 = CreateProfile(kProfileDir2);
  ASSERT_TRUE(profile2);
  storage->GetProfileAttributesWithPath(profile2->GetPath())
      ->SetAuthInfo(GaiaId("gaia_id_2"), base::UTF8ToUTF16(kProfileEmail2),
                    /*is_consented_primary_account=*/false);
  base::RunLoop run_loop;
  g_browser_process->FlushLocalStateAndReply(
      base::BindLambdaForTesting([&run_loop]() { run_loop.Quit(); }));
  run_loop.Run();

  // Normal email.
  size_t original_browser_count = chrome::GetTotalBrowserCount();
  Relaunch(GetCommandLineForRelaunchWithEmail(kProfileEmail1));
  Browser* new_browser = ui_test_utils::WaitForBrowserToOpen();
  ASSERT_TRUE(new_browser);
  EXPECT_EQ(new_browser->profile(), profile1);
  EXPECT_EQ(original_browser_count + 1, chrome::GetTotalBrowserCount());
  // Non-ASCII email.
  Relaunch(GetCommandLineForRelaunchWithEmail(kProfileEmail2));
  new_browser = ui_test_utils::WaitForBrowserToOpen();
  ASSERT_TRUE(new_browser);
  EXPECT_EQ(new_browser->profile(), profile2);
  EXPECT_EQ(original_browser_count + 2, chrome::GetTotalBrowserCount());
}
#endif  // !BUILDFLAG(IS_CHROMEOS)