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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
// 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 "chrome/test/base/browser_with_test_window_test.h"
#include <memory>
#include <vector>
#include "base/command_line.h"
#include "base/location.h"
#include "base/run_loop.h"
#include "build/build_config.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/profiles/profile_destroyer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/sync_preferences/pref_service_syncable.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/navigation_simulator.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_utils.h"
#include "ui/base/page_transition_types.h"
#if defined(TOOLKIT_VIEWS)
#include "chrome/browser/ui/views/chrome_constrained_window_views_client.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "components/constrained_window/constrained_window_views.h"
#include "ui/views/test/views_test_utils.h"
#endif
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/crosapi/crosapi_manager.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/browser_process.h"
#include "chromeos/ash/components/browser_context_helper/annotated_account_id.h"
#include "chromeos/ash/components/dbus/cros_disks/cros_disks_client.h"
#include "chromeos/ash/components/disks/disk_mount_manager.h"
#include "chromeos/ash/components/disks/fake_disk_mount_manager.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/test_helper.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/context_factory.h"
#include "google_apis/gaia/gaia_auth_util.h"
#include "google_apis/gaia/gaia_id.h"
#endif
using content::NavigationController;
using content::RenderFrameHost;
using content::RenderFrameHostTester;
using content::WebContents;
BrowserWithTestWindowTest::~BrowserWithTestWindowTest() = default;
void BrowserWithTestWindowTest::SetUp() {
testing::Test::SetUp();
base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kNoFirstRun);
if (!profile_manager_) {
SetUpProfileManager();
}
#if BUILDFLAG(IS_CHROMEOS)
ash::CrosDisksClient::InitializeFake();
if (!ash::disks::DiskMountManager::GetInstance()) {
ash::disks::DiskMountManager::InitializeForTesting(
new ash::disks::FakeDiskMountManager());
}
if (!user_manager::UserManager::IsInitialized()) {
user_manager_.Reset(std::make_unique<user_manager::FakeUserManager>(
g_browser_process->local_state()));
}
{
ash::AshTestHelper::InitParams ash_init;
ash_init.local_state = g_browser_process->local_state();
ash_init.start_session = false;
// Do not auto create user pref service. PrefService will be created by
// TestingProfile.
ash_init.auto_create_prefs_services = false;
ash_test_helper_.SetUp(std::move(ash_init));
}
#endif
// This must be created after |ash_test_helper_| is set up so that it doesn't
// create a DeviceDataManager.
rvh_test_enabler_ = std::make_unique<content::RenderViewHostTestEnabler>();
#if defined(TOOLKIT_VIEWS)
SetConstrainedWindowViewsClient(CreateChromeConstrainedWindowViewsClient());
#endif
user_performance_tuning_manager_environment_.SetUp(
profile_manager_->local_state()->Get());
#if BUILDFLAG(IS_CHROMEOS)
manager_ = std::make_unique<crosapi::CrosapiManager>();
kiosk_chrome_app_manager_ = std::make_unique<ash::KioskChromeAppManager>();
#endif
// Subclasses can provide their own Profile name.
std::optional<std::string> profile_name = GetDefaultProfileName();
if (profile_name) {
#if BUILDFLAG(IS_CHROMEOS)
LogIn(*profile_name, GaiaId("fakegaia"));
#endif
profile_ = CreateProfile(*profile_name)->GetWeakPtr();
#if BUILDFLAG(IS_CHROMEOS)
SwitchActiveUser(*profile_name);
#endif
window_ = CreateBrowserWindow();
browser_ =
CreateBrowser(profile(), browser_type_, hosted_app_, window_.get());
}
}
void BrowserWithTestWindowTest::TearDown() {
// Some tests end up posting tasks to the DB thread that must be completed
// before the profile can be destroyed and the test safely shut down.
base::RunLoop().RunUntilIdle();
// Close the browser tabs and destroy the browser and window instances.
if (browser_) {
browser_->tab_strip_model()->CloseAllTabs();
browser_->GetFeatures().TearDownPreBrowserWindowDestruction();
browser_.reset();
}
window_.reset();
#if defined(TOOLKIT_VIEWS)
constrained_window::SetConstrainedWindowViewsClient(nullptr);
#endif
// Depends on LocalState owned by |profile_manager_|.
if (SystemNetworkContextManager::GetInstance()) {
SystemNetworkContextManager::DeleteInstance();
}
#if BUILDFLAG(IS_CHROMEOS)
manager_.reset();
kiosk_chrome_app_manager_.reset();
#endif
user_performance_tuning_manager_environment_.TearDown();
#if BUILDFLAG(IS_CHROMEOS)
ash_test_helper_.TearDown();
#endif
// Calling DeleteAllTestingProfiles() first can cause issues in some tests, if
// they're still holding a ScopedProfileKeepAlive.
profile_ = nullptr;
profile_manager_.reset();
#if BUILDFLAG(IS_CHROMEOS)
test_views_delegate_.reset();
user_manager_.Reset();
ash::disks::DiskMountManager::Shutdown();
ash::CrosDisksClient::Shutdown();
#elif defined(TOOLKIT_VIEWS)
views_test_helper_.reset();
#endif
testing::Test::TearDown();
// A Task is leaked if we don't destroy everything, then run all pending
// tasks. This includes backend tasks which could otherwise be affected by the
// deletion of the temp dir.
task_environment_->RunUntilIdle();
}
void BrowserWithTestWindowTest::SetUpProfileManager(
const base::FilePath& profiles_path,
std::unique_ptr<ProfileManager> profile_manager) {
profile_manager_ = std::make_unique<TestingProfileManager>(
TestingBrowserProcess::GetGlobal());
#if BUILDFLAG(IS_CHROMEOS)
profile_manager_->set_on_profile_created_callback(
base::BindRepeating(&BrowserWithTestWindowTest::PostUserProfileCreation,
base::Unretained(this)));
#endif
ASSERT_TRUE(
profile_manager_->SetUp(profiles_path, std::move(profile_manager)));
}
gfx::NativeWindow BrowserWithTestWindowTest::GetContext() {
#if BUILDFLAG(IS_CHROMEOS)
return ash_test_helper_.GetContext();
#elif defined(TOOLKIT_VIEWS)
return views_test_helper_->GetContext();
#else
return nullptr;
#endif
}
void BrowserWithTestWindowTest::AddTab(Browser* browser, const GURL& url) {
NavigateParams params(browser, url, ui::PAGE_TRANSITION_TYPED);
params.tabstrip_index = 0;
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
Navigate(¶ms);
CommitPendingLoad(¶ms.navigated_or_inserted_contents->GetController());
#if defined(TOOLKIT_VIEWS)
BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
if (browser_view) {
views::test::RunScheduledLayout(browser_view);
}
#endif
}
void BrowserWithTestWindowTest::CommitPendingLoad(
NavigationController* controller) {
if (!controller->GetPendingEntry()) {
return; // Nothing to commit.
}
RenderFrameHostTester::CommitPendingLoad(controller);
}
void BrowserWithTestWindowTest::NavigateAndCommit(WebContents* web_contents,
const GURL& url) {
content::NavigationSimulator::NavigateAndCommitFromBrowser(web_contents, url);
}
void BrowserWithTestWindowTest::NavigateAndCommitActiveTab(const GURL& url) {
NavigateAndCommit(browser()->tab_strip_model()->GetActiveWebContents(), url);
}
void BrowserWithTestWindowTest::NavigateAndCommitActiveTabWithTitle(
Browser* navigating_browser,
const GURL& url,
const std::u16string& title) {
WebContents* contents =
navigating_browser->tab_strip_model()->GetActiveWebContents();
NavigateAndCommit(contents, url);
contents->UpdateTitleForEntry(contents->GetController().GetActiveEntry(),
title);
}
void BrowserWithTestWindowTest::FocusMainFrameOfActiveWebContents() {
WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
content::FocusWebContentsOnFrame(contents, contents->GetPrimaryMainFrame());
}
std::optional<std::string> BrowserWithTestWindowTest::GetDefaultProfileName() {
return TestingProfile::kDefaultProfileUserName;
}
TestingProfile* BrowserWithTestWindowTest::CreateProfile(
const std::string& profile_name) {
auto* profile = profile_manager_->CreateTestingProfile(
profile_name, /*prefs=*/nullptr, /*user_name=*/std::u16string(),
/*avatar_id=*/0, GetTestingFactories());
return profile;
}
void BrowserWithTestWindowTest::DeleteProfile(const std::string& profile_name) {
if (profile_name == GetDefaultProfileName()) {
if (browser_) {
browser_->tab_strip_model()->CloseAllTabs();
browser_.reset();
}
profile_ = nullptr;
}
profile_manager_->DeleteTestingProfile(profile_name);
}
TestingProfile::TestingFactories
BrowserWithTestWindowTest::GetTestingFactories() {
return {};
}
std::unique_ptr<BrowserWindow>
BrowserWithTestWindowTest::CreateBrowserWindow() {
return std::make_unique<TestBrowserWindow>();
}
std::unique_ptr<Browser> BrowserWithTestWindowTest::CreateBrowser(
Profile* profile,
Browser::Type browser_type,
bool hosted_app,
BrowserWindow* browser_window) {
Browser::CreateParams params(profile, true);
if (hosted_app) {
params = Browser::CreateParams::CreateForApp(
"Test", /*trusted_source=*/true, /*window_bounds=*/gfx::Rect(), profile,
/*user_gesture=*/true);
} else if (browser_type == Browser::TYPE_DEVTOOLS) {
params = Browser::CreateParams::CreateForDevTools(profile);
} else {
params.type = browser_type;
}
params.window = browser_window;
return Browser::DeprecatedCreateOwnedForTesting(params);
}
#if BUILDFLAG(IS_CHROMEOS)
void BrowserWithTestWindowTest::LogIn(std::string_view email,
const GaiaId& gaia_id) {
const AccountId account_id = AccountId::FromUserEmailGaiaId(email, gaia_id);
user_manager_->AddGaiaUser(account_id, user_manager::UserType::kRegular);
user_manager_->UserLoggedIn(
account_id, user_manager::TestHelper::GetFakeUsernameHash(account_id));
}
void BrowserWithTestWindowTest::OnUserProfileCreated(const std::string& email,
Profile* profile) {
CHECK(profile);
// TODO(b/40225390): Unset for_test explicit param after subclasses are
// migrated.
AccountId account_id = AccountId::FromUserEmail(email);
ash::AnnotatedAccountId::Set(profile, account_id,
/*for_test=*/false);
// Do not use the member directly, because another UserManager instance
// may be injected.
auto* user_manager = user_manager::UserManager::Get();
user_manager->OnUserProfileCreated(account_id, profile->GetPrefs());
GetSessionControllerClient()->SetUnownedUserPrefService(account_id,
profile->GetPrefs());
auto observation =
std::make_unique<base::ScopedObservation<Profile, ProfileObserver>>(this);
observation->Observe(profile);
profile_observations_.push_back(std::move(observation));
}
void BrowserWithTestWindowTest::SwitchActiveUser(const std::string& email) {
GetSessionControllerClient()->SwitchActiveUser(
AccountId::FromUserEmail(email));
GetSessionControllerClient()->SetSessionState(
session_manager::SessionState::ACTIVE);
}
void BrowserWithTestWindowTest::OnProfileWillBeDestroyed(Profile* profile) {
CHECK(
std::erase_if(profile_observations_, [profile](const auto& observation) {
return observation->IsObservingSource(profile);
}));
const AccountId* account_id = ash::AnnotatedAccountId::Get(profile);
CHECK(account_id);
// Do not use the member directly, because another UserManager instance
// may be injected.
user_manager::UserManager::Get()->OnUserProfileWillBeDestroyed(*account_id);
}
ash::ScopedCrosSettingsTestHelper*
BrowserWithTestWindowTest::GetCrosSettingsHelper() {
return &cros_settings_test_helper_;
}
ash::StubInstallAttributes* BrowserWithTestWindowTest::GetInstallAttributes() {
return GetCrosSettingsHelper()->InstallAttributes();
}
ash::TestSessionControllerClient*
BrowserWithTestWindowTest::GetSessionControllerClient() {
return ash_test_helper()->test_session_controller_client(
base::PassKey<BrowserWithTestWindowTest>());
}
void BrowserWithTestWindowTest::PostUserProfileCreation(
const std::string& email,
Profile* profile) {
// The test profile is not for gaia login.
if (email != gaia::CanonicalizeEmail(email)) {
return;
}
auto* user = user_manager::UserManager::Get()->FindUser(
AccountId::FromUserEmail(email));
if (user) {
OnUserProfileCreated(email, profile);
GetSessionControllerClient()->AddUserSession({email, user->GetType()});
}
}
#endif // BUILDFLAG(IS_CHROMEOS)
BrowserWithTestWindowTest::BrowserWithTestWindowTest(
std::unique_ptr<content::BrowserTaskEnvironment> task_environment,
Browser::Type browser_type,
bool hosted_app)
: task_environment_(std::move(task_environment)),
browser_type_(browser_type),
hosted_app_(hosted_app) {}
|