Commit 6c581e60 authored by Boris Sazonov's avatar Boris Sazonov Committed by Chromium LUCI CQ

[Lacros] Add basic unittests for AccountManagerFacadeLacros

Adds chrome/browser/lacros/account_manager_facade_lacros_unittest.cc and
a couple of basic tests for AccountManagerFacadeLacros. To make that
class testable, the mojo::Remote is now passed explicitly to the ctor.

Bug: 1117472
Change-Id: I59826ff3c7c0d6561b8fd4cb94a164884eb91a4e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2545923Reviewed-by: default avatarRoland Bock <rbock@google.com>
Reviewed-by: default avatarKush Sinha <sinhak@chromium.org>
Commit-Queue: Boris Sazonov <bsazonov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#835836}
parent 62c351af
......@@ -18,10 +18,15 @@ constexpr uint32_t kMinVersionWithObserver = 1;
} // namespace
AccountManagerFacadeLacros::AccountManagerFacadeLacros(
mojo::Remote<crosapi::mojom::AccountManager> account_manager_remote)
: account_manager_remote_(std::move(account_manager_remote)) {
if (!account_manager_remote_)
mojo::Remote<crosapi::mojom::AccountManager> account_manager_remote,
base::OnceClosure init_finished)
: account_manager_remote_(std::move(account_manager_remote)),
init_finished_(std::move(init_finished)) {
DCHECK(init_finished_);
if (!account_manager_remote_) {
std::move(init_finished_).Run();
return;
}
account_manager_remote_.QueryVersion(base::BindOnce(
&AccountManagerFacadeLacros::OnVersionCheck, weak_factory_.GetWeakPtr()));
......@@ -34,8 +39,10 @@ bool AccountManagerFacadeLacros::IsInitialized() {
}
void AccountManagerFacadeLacros::OnVersionCheck(uint32_t version) {
if (version < kMinVersionWithObserver)
if (version < kMinVersionWithObserver) {
std::move(init_finished_).Run();
return;
}
account_manager_remote_->AddObserver(
base::BindOnce(&AccountManagerFacadeLacros::OnReceiverReceived,
......@@ -56,6 +63,7 @@ void AccountManagerFacadeLacros::OnInitialized(bool is_initialized) {
if (is_initialized)
is_initialized_ = true;
// else: We will receive a notification in |OnTokenUpserted|.
std::move(init_finished_).Run();
}
void AccountManagerFacadeLacros::OnTokenUpserted(
......
......@@ -19,8 +19,9 @@ class AccountManagerFacadeLacros
: public account_manager::AccountManagerFacade,
public crosapi::mojom::AccountManagerObserver {
public:
explicit AccountManagerFacadeLacros(
mojo::Remote<crosapi::mojom::AccountManager> account_manager_remote);
AccountManagerFacadeLacros(
mojo::Remote<crosapi::mojom::AccountManager> account_manager_remote,
base::OnceClosure init_finished = base::DoNothing());
AccountManagerFacadeLacros(const AccountManagerFacadeLacros&) = delete;
AccountManagerFacadeLacros& operator=(const AccountManagerFacadeLacros&) =
delete;
......@@ -39,8 +40,9 @@ class AccountManagerFacadeLacros
mojo::PendingReceiver<AccountManagerObserver> receiver);
void OnInitialized(bool is_initialized);
bool is_initialized_ = false;
mojo::Remote<crosapi::mojom::AccountManager> account_manager_remote_;
base::OnceClosure init_finished_;
bool is_initialized_ = false;
std::unique_ptr<mojo::Receiver<crosapi::mojom::AccountManagerObserver>>
receiver_;
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/lacros/account_manager_facade_lacros.h"
#include "base/test/task_environment.h"
#include "chromeos/crosapi/mojom/account_manager.mojom.h"
#include "components/account_manager_core/account.h"
#include "components/account_manager_core/account_manager_test_util.h"
#include "components/account_manager_core/account_manager_util.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/remote_set.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class FakeAccountManager : public crosapi::mojom::AccountManager {
public:
FakeAccountManager() = default;
FakeAccountManager(const FakeAccountManager&) = delete;
FakeAccountManager& operator=(const FakeAccountManager&) = delete;
~FakeAccountManager() override = default;
void IsInitialized(IsInitializedCallback cb) override {
std::move(cb).Run(is_initialized_);
}
void SetIsInitialized(bool is_initialized) {
is_initialized_ = is_initialized;
}
void AddObserver(AddObserverCallback cb) override {
mojo::Remote<crosapi::mojom::AccountManagerObserver> observer;
std::move(cb).Run(observer.BindNewPipeAndPassReceiver());
observers_.Add(std::move(observer));
}
mojo::Remote<crosapi::mojom::AccountManager> CreateRemote() {
mojo::Remote<crosapi::mojom::AccountManager> remote;
receivers_.Add(this, remote.BindNewPipeAndPassReceiver());
return remote;
}
void NotifyOnTokenUpsertedObservers(const account_manager::Account& account) {
for (auto& observer : observers_) {
observer->OnTokenUpserted(ToMojoAccount(account));
}
}
private:
bool is_initialized_{false};
mojo::ReceiverSet<crosapi::mojom::AccountManager> receivers_;
mojo::RemoteSet<crosapi::mojom::AccountManagerObserver> observers_;
};
} // namespace
class AccountManagerFacadeLacrosTest : public testing::Test {
public:
AccountManagerFacadeLacrosTest() = default;
AccountManagerFacadeLacrosTest(const AccountManagerFacadeLacrosTest&) =
delete;
AccountManagerFacadeLacrosTest& operator=(
const AccountManagerFacadeLacrosTest&) = delete;
~AccountManagerFacadeLacrosTest() override = default;
protected:
FakeAccountManager& account_manager() { return account_manager_; }
std::unique_ptr<AccountManagerFacadeLacros> CreateFacade() {
base::RunLoop run_loop;
auto result = std::make_unique<AccountManagerFacadeLacros>(
account_manager().CreateRemote(), run_loop.QuitClosure());
run_loop.Run();
return result;
}
private:
base::test::SingleThreadTaskEnvironment task_environment_;
FakeAccountManager account_manager_;
};
TEST_F(AccountManagerFacadeLacrosTest,
FacadeIsInitializedOnConnectIfAccountManagerIsInitialized) {
account_manager().SetIsInitialized(true);
std::unique_ptr<AccountManagerFacadeLacros> account_manager_facade =
CreateFacade();
EXPECT_TRUE(account_manager_facade->IsInitialized());
}
TEST_F(AccountManagerFacadeLacrosTest, FacadeIsUninitializedByDefault) {
std::unique_ptr<AccountManagerFacadeLacros> account_manager_facade =
CreateFacade();
EXPECT_FALSE(account_manager_facade->IsInitialized());
}
// TODO(https://crbug.com/1117472): Add more tests after implementing observers.
......@@ -4907,11 +4907,17 @@ test("unit_tests") {
if (is_chromeos_lacros) {
assert(enable_native_notifications)
sources += [
"../browser/lacros/account_manager_facade_lacros_unittest.cc",
"../browser/lacros/lacros_chrome_service_delegate_impl_unittest.cc",
"../browser/lacros/metrics_reporting_observer_unittest.cc",
"../browser/notifications/notification_platform_bridge_lacros_unittest.cc",
"../common/chrome_paths_lacros_unittest.cc",
]
deps += [
"//components/account_manager_core",
"//components/account_manager_core:test_support",
]
}
if (is_chromeos_ash) {
......
......@@ -24,3 +24,14 @@ component("account_manager_core") {
defines = [ "IS_ACCOUNT_MANAGER_CORE_IMPL" ]
}
source_set("test_support") {
testonly = true
sources = [
"account_manager_test_util.cc",
"account_manager_test_util.h",
]
deps = [ ":account_manager_core" ]
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/account_manager_core/account_manager_test_util.h"
namespace account_manager {
account_manager::Account CreateTestGaiaAccount(const std::string& raw_email) {
account_manager::Account account;
account.key.account_type = account_manager::AccountType::kGaia;
// TODO(https://crbug.com/1150770): Use signin::GetTestGaiaIdForEmail here.
account.key.id = std::string("gaia_id_for_") + raw_email;
account.raw_email = raw_email;
return account;
}
} // namespace account_manager
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_ACCOUNT_MANAGER_CORE_ACCOUNT_MANAGER_TEST_UTIL_H_
#define COMPONENTS_ACCOUNT_MANAGER_CORE_ACCOUNT_MANAGER_TEST_UTIL_H_
#include "components/account_manager_core/account.h"
// Helper functions for Account Manager tests.
namespace account_manager {
// Generates a Gaia ID from |raw_email| and creates an account.
account_manager::Account CreateTestGaiaAccount(const std::string& raw_email);
} // namespace account_manager
#endif // COMPONENTS_ACCOUNT_MANAGER_CORE_ACCOUNT_MANAGER_TEST_UTIL_H_
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