Commit b6c05583 authored by Kushagra Sinha's avatar Kushagra Sinha Committed by Commit Bot

Refactor crOS Account Manager tests

Split InitializeAccountManager utility method into two methods -
InitializeAccountManager and InitializeAccountManagerAsync - to clearly
communicate the contract of these methods.

Change-Id: Ifbc16205cea0ba37ab3d7cc5d8be287510a07082
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2255358
Commit-Queue: Kush Sinha <sinhak@chromium.org>
Reviewed-by: default avatarAnastasiia N <anastasiian@chromium.org>
Cr-Commit-Position: refs/heads/master@{#781370}
parent 6825df46
...@@ -116,37 +116,41 @@ class AccountManagerTest : public testing::Test { ...@@ -116,37 +116,41 @@ class AccountManagerTest : public testing::Test {
// parameters. // parameters.
void ResetAndInitializeAccountManager() { void ResetAndInitializeAccountManager() {
account_manager_ = std::make_unique<AccountManagerSpy>(); account_manager_ = std::make_unique<AccountManagerSpy>();
InitializeAccountManager(account_manager_.get(), base::DoNothing()); InitializeAccountManager(account_manager_.get());
} }
// |account_manager| is a non-owning pointer. // |account_manager| is a non-owning pointer.
// |initialization_callback| is called after initialization is complete. void InitializeAccountManager(AccountManager* account_manager) {
void InitializeAccountManager(AccountManager* account_manager, InitializeAccountManager(account_manager, tmp_dir_.GetPath());
base::OnceClosure initialization_callback) {
InitializeAccountManager(account_manager, tmp_dir_.GetPath(),
std::move(initialization_callback));
} }
// |account_manager| is a non-owning pointer. // |account_manager| is a non-owning pointer.
// |home_dir| is the cryptohome root. // |home_dir| is the cryptohome root.
// |initialization_callback| is called after initialization is complete.
void InitializeAccountManager(AccountManager* account_manager, void InitializeAccountManager(AccountManager* account_manager,
const base::FilePath& home_dir, const base::FilePath& home_dir) {
base::OnceClosure initialization_callback) { InitializeAccountManager(account_manager, home_dir,
account_manager->Initialize( /* initialization_callback= */ base::DoNothing());
home_dir, test_url_loader_factory_.GetSafeWeakWrapper(),
immediate_callback_runner_, base::SequencedTaskRunnerHandle::Get(),
std::move(initialization_callback));
account_manager->SetPrefService(&pref_service_);
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
EXPECT_EQ(account_manager->init_state_, EXPECT_EQ(account_manager->init_state_,
AccountManager::InitializationState::kInitialized); AccountManager::InitializationState::kInitialized);
EXPECT_TRUE(account_manager->IsInitialized()); EXPECT_TRUE(account_manager->IsInitialized());
} }
// |account_manager| is a non-owning pointer.
// |initialization_callback| will be called after initialization is complete
// (when |task_environment_.RunUntilIdle();| is called).
void InitializeAccountManagerAsync(
AccountManager* account_manager,
base::OnceClosure initialization_callback) {
InitializeAccountManager(account_manager,
/* home_dir= */ tmp_dir_.GetPath(),
std::move(initialization_callback));
}
// Check base/test/task_environment.h. This must be the first member / // Check base/test/task_environment.h. This must be the first member /
// declared before any member that cares about tasks. // declared before any member that cares about tasks.
base::test::TaskEnvironment task_environment_; base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::ThreadPoolExecutionMode::QUEUED};
base::ScopedTempDir tmp_dir_; base::ScopedTempDir tmp_dir_;
TestingPrefServiceSimple pref_service_; TestingPrefServiceSimple pref_service_;
network::TestURLLoaderFactory test_url_loader_factory_; network::TestURLLoaderFactory test_url_loader_factory_;
...@@ -162,6 +166,15 @@ class AccountManagerTest : public testing::Test { ...@@ -162,6 +166,15 @@ class AccountManagerTest : public testing::Test {
[](base::OnceClosure closure) -> void { std::move(closure).Run(); }); [](base::OnceClosure closure) -> void { std::move(closure).Run(); });
private: private:
void InitializeAccountManager(AccountManager* account_manager,
const base::FilePath& home_dir,
base::OnceClosure initialization_callback) {
account_manager->Initialize(
home_dir, test_url_loader_factory_.GetSafeWeakWrapper(),
immediate_callback_runner_, base::SequencedTaskRunnerHandle::Get(),
std::move(initialization_callback));
account_manager->SetPrefService(&pref_service_);
}
DISALLOW_COPY_AND_ASSIGN(AccountManagerTest); DISALLOW_COPY_AND_ASSIGN(AccountManagerTest);
}; };
...@@ -216,7 +229,7 @@ TEST_F(AccountManagerTest, TestInitializationCompletes) { ...@@ -216,7 +229,7 @@ TEST_F(AccountManagerTest, TestInitializationCompletes) {
EXPECT_EQ(account_manager.init_state_, EXPECT_EQ(account_manager.init_state_,
AccountManager::InitializationState::kNotStarted); AccountManager::InitializationState::kNotStarted);
// Test assertions will be made inside the method. // Test assertions will be made inside the method.
InitializeAccountManager(&account_manager, base::DoNothing()); InitializeAccountManager(&account_manager);
} }
TEST_F(AccountManagerTest, TestInitializationCallbackIsCalled) { TEST_F(AccountManagerTest, TestInitializationCallbackIsCalled) {
...@@ -224,7 +237,8 @@ TEST_F(AccountManagerTest, TestInitializationCallbackIsCalled) { ...@@ -224,7 +237,8 @@ TEST_F(AccountManagerTest, TestInitializationCallbackIsCalled) {
base::OnceClosure closure = base::BindLambdaForTesting( base::OnceClosure closure = base::BindLambdaForTesting(
[&init_callback_was_called]() { init_callback_was_called = true; }); [&init_callback_was_called]() { init_callback_was_called = true; });
AccountManager account_manager; AccountManager account_manager;
InitializeAccountManager(&account_manager, std::move(closure)); InitializeAccountManagerAsync(&account_manager, std::move(closure));
task_environment_.RunUntilIdle();
ASSERT_TRUE(init_callback_was_called); ASSERT_TRUE(init_callback_was_called);
} }
...@@ -235,13 +249,13 @@ TEST_F(AccountManagerTest, ...@@ -235,13 +249,13 @@ TEST_F(AccountManagerTest,
TestInitializationCallbackIsCalledIfAccountManagerIsAlreadyInitialized) { TestInitializationCallbackIsCalledIfAccountManagerIsAlreadyInitialized) {
// Make sure that Account Manager is fully initialized. // Make sure that Account Manager is fully initialized.
AccountManager account_manager; AccountManager account_manager;
InitializeAccountManager(&account_manager, base::DoNothing()); InitializeAccountManager(&account_manager);
// Send a duplicate initialization call. // Send a duplicate initialization call.
bool init_callback_was_called = false; bool init_callback_was_called = false;
base::OnceClosure closure = base::BindLambdaForTesting( base::OnceClosure closure = base::BindLambdaForTesting(
[&init_callback_was_called]() { init_callback_was_called = true; }); [&init_callback_was_called]() { init_callback_was_called = true; });
InitializeAccountManager(&account_manager, std::move(closure)); InitializeAccountManagerAsync(&account_manager, std::move(closure));
ASSERT_TRUE(init_callback_was_called); ASSERT_TRUE(init_callback_was_called);
} }
...@@ -277,16 +291,14 @@ TEST_F(AccountManagerTest, TestTokenTransience) { ...@@ -277,16 +291,14 @@ TEST_F(AccountManagerTest, TestTokenTransience) {
{ {
// Create a scoped |AccountManager|. // Create a scoped |AccountManager|.
AccountManager account_manager; AccountManager account_manager;
InitializeAccountManager(&account_manager, home_dir, InitializeAccountManager(&account_manager, home_dir);
/* initialization_callback= */ base::DoNothing());
account_manager.UpsertAccount(kGaiaAccountKey_, kRawUserEmail, kGaiaToken); account_manager.UpsertAccount(kGaiaAccountKey_, kRawUserEmail, kGaiaToken);
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
} }
// Create another |AccountManager| at the same path. // Create another |AccountManager| at the same path.
AccountManager account_manager; AccountManager account_manager;
InitializeAccountManager(&account_manager, home_dir, InitializeAccountManager(&account_manager, home_dir);
/* initialization_callback= */ base::DoNothing());
std::vector<AccountManager::Account> accounts = std::vector<AccountManager::Account> accounts =
GetAccountsBlocking(&account_manager); GetAccountsBlocking(&account_manager);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment