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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <vector>
#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/strcat.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_browser_main.h"
#include "chrome/browser/chrome_browser_main_extra_parts.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/profiles/profile_window.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/profiles/profile_picker.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "testing/gmock/include/gmock/gmock.h"
using ::testing::_;
using ::testing::InSequence;
using ::testing::Matcher;
using ::testing::Mock;
using ::testing::Property;
using ::testing::ValuesIn;
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
#error Not supported on this platform.
#endif
namespace {
class MockMainExtraParts : public ChromeBrowserMainExtraParts {
public:
MOCK_METHOD(void, PreProfileInit, ());
MOCK_METHOD(void, PostProfileInit, (Profile*, bool));
MOCK_METHOD(void, PreBrowserStart, ());
MOCK_METHOD(void, PostBrowserStart, ());
MOCK_METHOD(void, PreMainMessageLoopRun, ());
};
const char kOtherProfileDirPath[] = "Other";
MATCHER_P(BaseNameEquals,
basename,
base::StrCat({negation ? "doesn't equal " : "equals ", basename})) {
return arg == base::FilePath::FromASCII(basename);
}
Matcher<Profile*> HasBaseName(const char* basename) {
return Property("basename", &Profile::GetBaseName, BaseNameEquals(basename));
}
struct MultiProfileStartupTestParam {
// Whether the profile picker should be shown on startup.
const bool should_show_profile_picker;
struct PostInitExpectedCall {
// Matcher for the expected `profile` argument to `PostProfileInit()`
const Matcher<Profile*> profile_matcher;
// Expected value for the `is_initial_profile` argument to
// `PostProfileInit()`
const bool is_initial_profile;
};
// Call expectations for the `PostProfileInit()` method. The expectations
// should themselves be listed in the expected call order.
//
// The first one is checked in `CreatedBrowserMainParts()` as part of startup,
// and the remaining ones in the test body.
const std::vector<PostInitExpectedCall> expected_post_profile_init_call_args;
};
const MultiProfileStartupTestParam kTestParams[] = {
{.should_show_profile_picker = false,
.expected_post_profile_init_call_args =
{{HasBaseName(chrome::kInitialProfile), true},
{HasBaseName(kOtherProfileDirPath), false}}},
{.should_show_profile_picker = true,
.expected_post_profile_init_call_args = {
{HasBaseName(chrome::kInitialProfile), true},
{HasBaseName(kOtherProfileDirPath), false}}}};
// Creates a new profile to be picked up on the actual test.
void SetUpSecondaryProfileForPreTest(
const base::FilePath& profile_dir_basename) {
ProfileManager* profile_manager = g_browser_process->profile_manager();
base::FilePath profile_path =
profile_manager->user_data_dir().Append(profile_dir_basename);
profiles::testing::CreateProfileSync(profile_manager, profile_path);
// Mark newly created profile as active.
ProfileAttributesEntry* entry =
profile_manager->GetProfileAttributesStorage()
.GetProfileAttributesWithPath(profile_path);
ASSERT_NE(entry, nullptr);
entry->SetActiveTimeToNow();
}
void CreateBrowserForProfileDir(const base::FilePath& profile_dir_basename) {
profiles::testing::SwitchToProfileSync(
g_browser_process->profile_manager()->user_data_dir().Append(
profile_dir_basename));
}
} // namespace
class ChromeMultiProfileStartupBrowserTestBase
: public InProcessBrowserTest,
public testing::WithParamInterface<MultiProfileStartupTestParam> {
public:
ChromeMultiProfileStartupBrowserTestBase() {
// Avoid providing a URL for the browser to open, allows the profile picker
// to be displayed on startup when it is enabled.
set_open_about_blank_on_browser_launch(false);
}
void CreatedBrowserMainParts(content::BrowserMainParts* parts) override {
InProcessBrowserTest::CreatedBrowserMainParts(parts);
// Skip expectations preparation for the PRE_ step.
if (GetTestPreCount() != 0)
return;
auto mock_part = std::make_unique<MockMainExtraParts>();
mock_part_ = mock_part.get();
static_cast<ChromeBrowserMainParts*>(parts)->AddParts(std::move(mock_part));
// At least one entry for the initial call is needed.
ASSERT_FALSE(GetParam().expected_post_profile_init_call_args.empty());
// The basic callbacks should be called only once.
EXPECT_CALL(*mock_part_, PreProfileInit()).Times(1);
EXPECT_CALL(*mock_part_, PreBrowserStart()).Times(1);
EXPECT_CALL(*mock_part_, PostBrowserStart()).Times(1);
EXPECT_CALL(*mock_part_, PreMainMessageLoopRun()).Times(1);
{
const auto& call_args = GetParam().expected_post_profile_init_call_args;
InSequence s;
for (const auto& expected_args : call_args) {
EXPECT_CALL(*mock_part_,
PostProfileInit(expected_args.profile_matcher,
expected_args.is_initial_profile));
}
}
}
raw_ptr<MockMainExtraParts, AcrossTasksDanglingUntriaged> mock_part_;
};
IN_PROC_BROWSER_TEST_P(ChromeMultiProfileStartupBrowserTestBase,
PRE_PostProfileInitInvocation) {
SetUpSecondaryProfileForPreTest(
base::FilePath::FromASCII(kOtherProfileDirPath));
g_browser_process->local_state()->SetBoolean(
prefs::kBrowserShowProfilePickerOnStartup,
GetParam().should_show_profile_picker);
// Need to close the browser window manually so that the real test does not
// treat it as session restore.
CloseAllBrowsers();
}
// Make sure that the second profile creation causes `PostProfileInit()` to be
// called a second time.
IN_PROC_BROWSER_TEST_P(ChromeMultiProfileStartupBrowserTestBase,
PostProfileInitInvocation) {
EXPECT_EQ(2u, g_browser_process->profile_manager()->GetNumberOfProfiles());
if (GetParam().should_show_profile_picker) {
EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
EXPECT_TRUE(ProfilePicker::IsOpen());
} else {
EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
EXPECT_NE(base::FilePath::FromASCII(kOtherProfileDirPath),
browser()->profile()->GetPath().BaseName());
EXPECT_FALSE(ProfilePicker::IsOpen());
}
// TODO(crbug.com/40817107): In some cases, profile creation is
// triggered by restoring the previously opened profile, and the test
// expectations in terms of `PostProfileInit()` calls can
// be met without opening browsers. We still open them for consistency, at
// least until we can make the test behaviour stricter.
if (GetParam().should_show_profile_picker) {
// No browser was previously open, as verified at the beginning of the test.
// So we start by opening the browser for the default profile.
CreateBrowserForProfileDir(
base::FilePath::FromASCII(chrome::kInitialProfile));
}
CreateBrowserForProfileDir(base::FilePath::FromASCII(kOtherProfileDirPath));
EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
}
INSTANTIATE_TEST_SUITE_P(All,
ChromeMultiProfileStartupBrowserTestBase,
ValuesIn(kTestParams));
|