diff --git a/fastpair/BUILD b/fastpair/BUILD index 453b6db5..8f1c42aa 100644 --- a/fastpair/BUILD +++ b/fastpair/BUILD @@ -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", diff --git a/fastpair/fast_pair_service_test.cc b/fastpair/fast_pair_service_test.cc index 8a3407a6..7ab94f97 100644 --- a/fastpair/fast_pair_service_test.cc +++ b/fastpair/fast_pair_service_test.cc @@ -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(); + }); http_client_ = std::make_unique(); device_info_ = std::make_unique(); 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( @@ -84,7 +89,6 @@ class FastPairServiceTest : public ::testing::Test { GetHttpClient()->SetResponseForSyncRequest(response); } - FakeAccountManager::Factory account_manager_factory_; std::unique_ptr authentication_manager_; std::unique_ptr http_client_; std::unique_ptr device_info_; diff --git a/fastpair/internal/BUILD b/fastpair/internal/BUILD index 3742f983..65041ef1 100644 --- a/fastpair/internal/BUILD +++ b/fastpair/internal/BUILD @@ -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", diff --git a/fastpair/internal/fast_pair_seeker_impl_test.cc b/fastpair/internal/fast_pair_seeker_impl_test.cc index f3e11089..f49f262b 100644 --- a/fastpair/internal/fast_pair_seeker_impl_test.cc +++ b/fastpair/internal/fast_pair_seeker_impl_test.cc @@ -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; diff --git a/fastpair/pairing/BUILD b/fastpair/pairing/BUILD index 65a485aa..ab0fe961 100644 --- a/fastpair/pairing/BUILD +++ b/fastpair/pairing/BUILD @@ -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", diff --git a/fastpair/pairing/fastpair/BUILD b/fastpair/pairing/fastpair/BUILD index c748b211..b584b20d 100644 --- a/fastpair/pairing/fastpair/BUILD +++ b/fastpair/pairing/fastpair/BUILD @@ -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", diff --git a/fastpair/pairing/fastpair/fast_pair_pairer_impl_test.cc b/fastpair/pairing/fastpair/fast_pair_pairer_impl_test.cc index 8fa4dbde..19e31b5d 100644 --- a/fastpair/pairing/fastpair/fast_pair_pairer_impl_test.cc +++ b/fastpair/pairing/fastpair/fast_pair_pairer_impl_test.cc @@ -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 { diff --git a/fastpair/pairing/pairer_broker_impl_test.cc b/fastpair/pairing/pairer_broker_impl_test.cc index 334e290a..4a138f31 100644 --- a/fastpair/pairing/pairer_broker_impl_test.cc +++ b/fastpair/pairing/pairer_broker_impl_test.cc @@ -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 { diff --git a/fastpair/server_access/BUILD b/fastpair/server_access/BUILD index 6ca7f1dc..939df87f 100644 --- a/fastpair/server_access/BUILD +++ b/fastpair/server_access/BUILD @@ -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", diff --git a/fastpair/server_access/fast_pair_client_impl_test.cc b/fastpair/server_access/fast_pair_client_impl_test.cc index 80665de0..e8b86a60 100644 --- a/fastpair/server_access/fast_pair_client_impl_test.cc +++ b/fastpair/server_access/fast_pair_client_impl_test.cc @@ -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" diff --git a/internal/base/BUILD b/internal/base/BUILD index a07f30d3..35d66268 100644 --- a/internal/base/BUILD +++ b/internal/base/BUILD @@ -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__", diff --git a/internal/test/BUILD b/internal/test/BUILD index e3227fe4..7512570d 100644 --- a/internal/test/BUILD +++ b/internal/test/BUILD @@ -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", diff --git a/internal/test/fake_account_manager.cc b/internal/test/fake_account_manager.cc new file mode 100644 index 00000000..91ac547c --- /dev/null +++ b/internal/test/fake_account_manager.cc @@ -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 +#include + +#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 FakeAccountManager::GetCurrentAccount() { + if (user_name_.has_value()) { + return account_; + } + return std::nullopt; +} + +void FakeAccountManager::Login( + absl::AnyInvocable login_success_callback, + absl::AnyInvocable 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 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 success_callback, + absl::AnyInvocable 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; + 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 diff --git a/internal/test/fake_account_manager.h b/internal/test/fake_account_manager.h new file mode 100644 index 00000000..a1e5791c --- /dev/null +++ b/internal/test/fake_account_manager.h @@ -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 +#include + +#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 GetCurrentAccount() override; + + void Login(absl::AnyInvocable login_success_callback, + absl::AnyInvocable login_failure_callback) override; + + void Logout(absl::AnyInvocable logout_callback) override; + + bool GetAccessToken( + absl::string_view account_id, + absl::AnyInvocable success_callback, + absl::AnyInvocable failure_callback) override; + void AddObserver(Observer* observer) override; + void RemoveObserver(Observer* observer) override; + + // Methods to set API response. + void SetAccount(std::optional 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_; + + // Logout will fail when is_logout_success_ is false; + bool is_logout_success_ = true; + nearby::ObserverList observers_; + std::optional user_name_; +}; + +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_INTERNAL_TEST_FAKE_ACCOUNT_MANAGER_H_