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
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_