Move FakeAccountManager to nearby/internal/test

PiperOrigin-RevId: 592055356
This commit is contained in:
Francis Tsui
2023-12-18 17:30:16 -08:00
committed by Copybara-Service
parent cf69c87d95
commit 32740f8591
14 changed files with 208 additions and 19 deletions
+1 -1
View File
@@ -132,7 +132,7 @@ cc_test(
"//fastpair/internal",
"//fastpair/message_stream:fake_provider",
"//fastpair/plugins:fake_fast_pair_plugin",
"//internal/account:test_support",
"//internal/account",
"//internal/network:types",
"//internal/platform:test_util",
"//internal/platform:types",
+9 -5
View File
@@ -25,11 +25,12 @@
#include "fastpair/internal/fast_pair_seeker_impl.h"
#include "fastpair/message_stream/fake_provider.h"
#include "fastpair/plugins/fake_fast_pair_plugin.h"
#include "internal/account/fake_account_manager.h"
#include "internal/account/account_manager_impl.h"
#include "internal/network/http_client.h"
#include "internal/platform/device_info.h"
#include "internal/platform/logging.h"
#include "internal/platform/medium_environment.h"
#include "internal/test/fake_account_manager.h"
#include "internal/test/fake_device_info.h"
#include "internal/test/fake_http_client.h"
#include "internal/test/google3_only/fake_authentication_manager.h"
@@ -48,8 +49,9 @@ using ::testing::status::StatusIs;
class FastPairServiceTest : public ::testing::Test {
protected:
FastPairServiceTest() {
AccountManagerImpl::Factory::SetFactoryForTesting(
&account_manager_factory_);
AccountManagerImpl::Factory::SetFactoryForTesting([]() {
return std::make_unique<nearby::FakeAccountManager>();
});
http_client_ = std::make_unique<network::FakeHttpClient>();
device_info_ = std::make_unique<FakeDeviceInfo>();
authentication_manager_ =
@@ -61,7 +63,10 @@ class FastPairServiceTest : public ::testing::Test {
GetAuthManager()->EnableSyncMode();
}
void TearDown() override { MediumEnvironment::Instance().Stop(); }
void TearDown() override {
AccountManagerImpl::Factory::SetFactoryForTesting(nullptr);
MediumEnvironment::Instance().Stop();
}
nearby::FakeAuthenticationManager* GetAuthManager() {
return reinterpret_cast<nearby::FakeAuthenticationManager*>(
@@ -84,7 +89,6 @@ class FastPairServiceTest : public ::testing::Test {
GetHttpClient()->SetResponseForSyncRequest(response);
}
FakeAccountManager::Factory account_manager_factory_;
std::unique_ptr<auth::AuthenticationManager> authentication_manager_;
std::unique_ptr<network::HttpClient> http_client_;
std::unique_ptr<DeviceInfo> device_info_;
+1 -2
View File
@@ -44,11 +44,10 @@ cc_test(
"//fastpair/repository",
"//fastpair/repository:device_repository",
"//fastpair/repository:test_support",
"//internal/account:test_support",
"//internal/platform:test_util",
"//internal/platform:types",
"//internal/platform/implementation/g3", # build_cleaner: keep
"//internal/test/google3_only:test",
"//internal/test",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
@@ -35,11 +35,11 @@
#include "fastpair/repository/fake_fast_pair_repository.h"
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/repository/fast_pair_repository.h"
#include "internal/account/fake_account_manager.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/logging.h"
#include "internal/platform/medium_environment.h"
#include "internal/platform/single_thread_executor.h"
#include "internal/test/fake_account_manager.h"
namespace nearby {
namespace fastpair {
@@ -55,8 +55,6 @@ constexpr absl::string_view kBobPublicKey =
"F7D496A62ECA416351540AA343BC690A6109F551500666B83B1251FB84FA2860795EBD63D3"
"B8836F44A9A3E28BB34017E015F5979305D849FDF8DE10123B61D2";
constexpr absl::string_view kPasskey = "123456";
constexpr absl::string_view kFastPairPreferencesFilePath =
"Google/Nearby/FastPair";
constexpr absl::string_view kTestAccountId = "test_account_id";
using ::testing::status::StatusIs;
+1 -2
View File
@@ -60,14 +60,13 @@ cc_test(
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/repository:test_support",
"//internal/account",
"//internal/account:test_support",
"//internal/auth:credential",
"//internal/base:bluetooth_address",
"//internal/platform:comm",
"//internal/platform:test_util",
"//internal/platform:types",
"//internal/platform/implementation/g3", # build_cleaner: keep
"//internal/test/google3_only:test",
"//internal/test",
"@boringssl//:crypto",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/functional:bind_front",
+1 -2
View File
@@ -58,14 +58,13 @@ cc_test(
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/repository:test_support",
"//internal/account",
"//internal/account:test_support",
"//internal/auth:credential",
"//internal/base:bluetooth_address",
"//internal/platform:comm",
"//internal/platform:test_util",
"//internal/platform:types",
"//internal/platform/implementation/g3", # build_cleaner: keep
"//internal/test/google3_only:test",
"//internal/test",
"@boringssl//:crypto",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/functional:any_invocable",
@@ -46,13 +46,13 @@
#include "fastpair/pairing/fastpair/fast_pair_pairer.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/repository/fake_fast_pair_repository.h"
#include "internal/account/fake_account_manager.h"
#include "internal/base/bluetooth_address.h"
#include "internal/platform/ble_v2.h"
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/medium_environment.h"
#include "internal/platform/single_thread_executor.h"
#include "internal/test/fake_account_manager.h"
namespace nearby {
namespace fastpair {
+1 -1
View File
@@ -36,11 +36,11 @@
#include "fastpair/internal/mediums/mediums.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/repository/fake_fast_pair_repository.h"
#include "internal/account/fake_account_manager.h"
#include "internal/base/bluetooth_address.h"
#include "internal/platform/ble_v2.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/medium_environment.h"
#include "internal/test/fake_account_manager.h"
namespace nearby {
namespace fastpair {
-1
View File
@@ -93,7 +93,6 @@ cc_test(
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/proto:proto_builder",
"//internal/account",
"//internal/account:test_support",
"//internal/auth:credential",
"//internal/auth:types",
"//internal/network:types",
@@ -44,7 +44,6 @@
#include "fastpair/proto/proto_builder.h"
#include "fastpair/server_access/fast_pair_client.h"
#include "fastpair/server_access/fast_pair_http_notifier.h"
#include "internal/account/fake_account_manager.h"
#include "internal/auth/auth_status_util.h"
#include "internal/auth/authentication_manager.h"
#include "internal/network/http_client.h"
@@ -53,6 +52,7 @@
#include "internal/network/http_status_code.h"
#include "internal/network/url.h"
#include "internal/platform/device_info.h"
#include "internal/test/fake_account_manager.h"
#include "internal/test/fake_device_info.h"
#include "internal/test/google3_only/fake_authentication_manager.h"
+1
View File
@@ -15,6 +15,7 @@ cc_library(
"//internal/account:__subpackages__",
"//internal/interop:__pkg__",
"//internal/platform:__pkg__",
"//internal/test:__pkg__",
"//location/nearby/cpp/experiments:__subpackages__",
"//location/nearby/cpp/sharing:__subpackages__",
"//third_party/nearby/sharing:__subpackages__",
+3
View File
@@ -17,6 +17,7 @@ licenses(["notice"])
cc_library(
name = "test",
srcs = [
"fake_account_manager.cc",
"fake_clock.cc",
"fake_single_thread_executor.cc",
"fake_task_runner.cc",
@@ -24,6 +25,7 @@ cc_library(
"fake_webrtc.cc",
],
hdrs = [
"fake_account_manager.h",
"fake_clock.h",
"fake_data_set.h",
"fake_device_info.h",
@@ -39,6 +41,7 @@ cc_library(
],
visibility = ["//visibility:public"],
deps = [
"//internal/base",
"//internal/base:bluetooth_address",
"//internal/data:data_manager",
"//internal/network:types",
+112
View File
@@ -0,0 +1,112 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/test/fake_account_manager.h"
#include <optional>
#include <string>
#include "absl/functional/any_invocable.h"
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/account_manager.h"
namespace nearby {
std::optional<AccountManager::Account> FakeAccountManager::GetCurrentAccount() {
if (user_name_.has_value()) {
return account_;
}
return std::nullopt;
}
void FakeAccountManager::Login(
absl::AnyInvocable<void(Account)> login_success_callback,
absl::AnyInvocable<void()> login_failure_callback) {
if (account_.has_value()) {
login_success_callback(*account_);
UpdateCurrentUser(account_->id);
NotifyLogin(account_->id);
return;
}
login_failure_callback();
}
void FakeAccountManager::Logout(
absl::AnyInvocable<void(absl::Status)> logout_callback) {
if (is_logout_success_) {
std::string account_id = account_->id;
SetAccount(std::nullopt);
logout_callback(absl::OkStatus());
NotifyLogout(account_id);
return;
}
logout_callback(absl::NotFoundError("No account login."));
}
bool FakeAccountManager::GetAccessToken(
absl::string_view account_id,
absl::AnyInvocable<void(absl::string_view)> success_callback,
absl::AnyInvocable<void(absl::Status)> failure_callback) {
if (!account_.has_value()) {
failure_callback(absl::UnavailableError("No current user."));
return false;
}
success_callback(account_id);
return true;
}
void FakeAccountManager::SetAccount(std::optional<Account> account) {
account_ = account;
if (account_.has_value()) {
UpdateCurrentUser(account_->id);
} else {
ClearCurrentUser();
}
}
void FakeAccountManager::UpdateCurrentUser(absl::string_view current_user) {
user_name_ = current_user;
}
void FakeAccountManager::ClearCurrentUser() {
user_name_.reset();
}
void FakeAccountManager::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void FakeAccountManager::RemoveObserver(Observer* observer) {
if (!observers_.HasObserver(observer)) {
return;
}
observers_.RemoveObserver(observer);
}
void FakeAccountManager::NotifyLogin(absl::string_view account_id) {
for (const auto& observer : observers_.GetObservers()) {
observer->OnLoginSucceeded(account_id);
}
}
void FakeAccountManager::NotifyLogout(absl::string_view account_id) {
for (const auto& observer : observers_.GetObservers()) {
observer->OnLogoutSucceeded(account_id);
}
}
} // namespace nearby
+75
View File
@@ -0,0 +1,75 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_INTERNAL_TEST_FAKE_ACCOUNT_MANAGER_H_
#define THIRD_PARTY_NEARBY_INTERNAL_TEST_FAKE_ACCOUNT_MANAGER_H_
#include <optional>
#include <string>
#include "absl/functional/any_invocable.h"
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "internal/base/observer_list.h"
#include "internal/platform/implementation/account_manager.h"
namespace nearby {
// A fake implementation of FakeAccountManager, along with a fake
// factory, to be used in tests.
class FakeAccountManager : public AccountManager {
public:
FakeAccountManager() = default;
~FakeAccountManager() override = default;
std::optional<Account> GetCurrentAccount() override;
void Login(absl::AnyInvocable<void(Account)> login_success_callback,
absl::AnyInvocable<void()> login_failure_callback) override;
void Logout(absl::AnyInvocable<void(absl::Status)> logout_callback) override;
bool GetAccessToken(
absl::string_view account_id,
absl::AnyInvocable<void(absl::string_view)> success_callback,
absl::AnyInvocable<void(absl::Status)> failure_callback) override;
void AddObserver(Observer* observer) override;
void RemoveObserver(Observer* observer) override;
// Methods to set API response.
void SetAccount(std::optional<Account> account);
void SetLogoutSuccess(bool is_logout_success) {
is_logout_success_ = is_logout_success;
}
private:
// Updates current username to preference.
void UpdateCurrentUser(absl::string_view current_user);
void ClearCurrentUser();
void NotifyLogin(absl::string_view account_id);
void NotifyLogout(absl::string_view account_id);
// Login will fail when account_ is empty.
std::optional<Account> account_;
// Logout will fail when is_logout_success_ is false;
bool is_logout_success_ = true;
nearby::ObserverList<Observer> observers_;
std::optional<std::string> user_name_;
};
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_TEST_FAKE_ACCOUNT_MANAGER_H_