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
|
// 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 "chrome/browser/profiles/profile_test_util.h"
#include "base/files/file_path.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/test_future.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_keyed_service_factory.h"
#include "chrome/browser/profiles/profile_manager.h"
#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/profiles/profile_window.h"
#include "components/signin/public/identity_manager/account_managed_status_finder.h"
#include "testing/gtest/include/gtest/gtest.h"
#endif // BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/ash/components/login/login_state/login_state.h"
#endif // BUILDFLAG(IS_CHROMEOS)
namespace profiles::testing {
Profile& CreateProfileSync(ProfileManager* profile_manager,
const base::FilePath& path) {
base::test::TestFuture<Profile*> profile_future;
profile_manager->CreateProfileAsync(path, profile_future.GetCallback());
Profile* profile = profile_future.Get();
CHECK(profile);
return *profile;
}
#if !BUILDFLAG(IS_ANDROID)
void SwitchToProfileSync(const base::FilePath& path, bool always_create) {
base::test::TestFuture<Browser*> future;
profiles::SwitchToProfile(path, always_create, future.GetCallback());
ASSERT_TRUE(future.Wait()) << "profiles::SwitchToProfile() did not complete";
}
ScopedNonEnterpriseDomainSetterForTesting::
ScopedNonEnterpriseDomainSetterForTesting(const char* domain) {
signin::AccountManagedStatusFinder::SetNonEnterpriseDomainForTesting(domain);
}
ScopedNonEnterpriseDomainSetterForTesting::
~ScopedNonEnterpriseDomainSetterForTesting() {
signin::AccountManagedStatusFinder::SetNonEnterpriseDomainForTesting(nullptr);
}
#endif // !BUILDFLAG(IS_ANDROID)
ScopedProfileSelectionsForFactoryTesting::
ScopedProfileSelectionsForFactoryTesting(
ProfileKeyedServiceFactory* factory,
ProfileSelections selections)
: factory_(factory),
old_selections_(std::exchange(factory->profile_selections_, selections)) {
}
ScopedProfileSelectionsForFactoryTesting::
~ScopedProfileSelectionsForFactoryTesting() {
factory_->profile_selections_ = old_selections_;
}
ScopedTestManagedGuestSession::ScopedTestManagedGuestSession() {
#if BUILDFLAG(IS_CHROMEOS)
ash::LoginState::Initialize();
ash::LoginState::Get()->SetLoggedInState(
ash::LoginState::LOGGED_IN_ACTIVE,
ash::LoginState::LOGGED_IN_USER_PUBLIC_ACCOUNT);
#endif // BUILDFLAG(IS_CHROMEOS)
}
ScopedTestManagedGuestSession::~ScopedTestManagedGuestSession() {
#if BUILDFLAG(IS_CHROMEOS)
if (ash::LoginState::IsInitialized()) {
ash::LoginState::Shutdown();
}
#endif // BUILDFLAG(IS_CHROMEOS)
}
} // namespace profiles::testing
|