Deprecate Fast Pair Mediator

PiperOrigin-RevId: 553538010
This commit is contained in:
Qin Wang
2023-08-09 19:10:24 -07:00
committed by Copybara-Service
parent b798798f5f
commit e8c0ce3d89
7 changed files with 0 additions and 863 deletions
-79
View File
@@ -1,79 +0,0 @@
# Copyright 2023 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.
licenses(["notice"])
cc_library(
name = "keyed_service",
srcs = [
"fast_pair_mediator.cc",
"fast_pair_mediator_factory.cc",
],
hdrs = [
"fast_pair_mediator.h",
"fast_pair_mediator_factory.h",
],
visibility = [
"//fastpair:__subpackages__",
],
deps = [
"//fastpair/common",
"//fastpair/internal/mediums",
"//fastpair/pairing",
"//fastpair/repository",
"//fastpair/repository:device_repository",
"//fastpair/repository:repository_impl",
"//fastpair/scanning:scanner",
"//fastpair/server_access",
"//fastpair/ui:fast_pair_ui",
"//internal/account",
"//internal/auth:oauth_lib",
"//internal/auth:types",
"//internal/flags:nearby_flags",
"//internal/network:nearby_http_client",
"//internal/network:types",
"//internal/platform:base",
"//internal/platform:logging",
"//internal/platform:types",
"//internal/platform/flags:platform_flags",
"//internal/preferences",
"@com_google_absl//absl/status",
],
)
cc_test(
name = "fast_pair_mediator_test",
size = "small",
srcs = [
"fast_pair_mediator_factory_test.cc",
"fast_pair_mediator_test.cc",
],
shard_count = 16,
deps = [
":keyed_service",
"//fastpair/testing",
"//fastpair/ui:fast_pair_ui",
"//fastpair/ui:mock_fast_pair_ui",
"//internal/account:test_support",
"//internal/network:nearby_http_client",
"//internal/platform:test_util",
"//internal/platform:types",
"//internal/platform/implementation/g3", # build_cleaner: keep
"//internal/test",
"//internal/test/google3_only:test",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
@@ -1,242 +0,0 @@
// Copyright 2023 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 "fastpair/keyed_service/fast_pair_mediator.h"
#include <ios>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "absl/status/status.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/common/fast_pair_prefs.h"
#include "fastpair/common/protocol.h"
#include "fastpair/internal/mediums/mediums.h"
#include "fastpair/pairing/pairer_broker_impl.h"
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/repository/fast_pair_repository_impl.h"
#include "fastpair/scanning/scanner_broker_impl.h"
#include "fastpair/server_access/fast_pair_client_impl.h"
#include "fastpair/ui/actions.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
#include "fastpair/ui/ui_broker_impl.h"
#include "internal/account/account_manager_impl.h"
#include "internal/flags/nearby_flags.h"
#include "internal/platform/feature_flags.h"
#include "internal/platform/flags/nearby_platform_feature_flags.h"
#include "internal/platform/logging.h"
#include "internal/platform/single_thread_executor.h"
#include "internal/platform/task_runner_impl.h"
#include "internal/preferences/preferences_manager.h"
namespace nearby {
namespace fastpair {
namespace {
constexpr char kFastPairPreferencesFilePath[] = "Google/Nearby/FastPair";
constexpr FeatureFlags::Flags fast_pair_feature_flags = FeatureFlags::Flags{
.enable_scan_for_fast_pair_advertisement = true,
};
}
Mediator::Mediator(
std::unique_ptr<SingleThreadExecutor> executor,
std::unique_ptr<Mediums> mediums, std::unique_ptr<UIBroker> ui_broker,
std::unique_ptr<FastPairNotificationController> notification_controller,
std::unique_ptr<auth::AuthenticationManager> authentication_manager,
std::unique_ptr<network::HttpClient> http_client,
std::unique_ptr<DeviceInfo> device_info)
: executor_(std::move(executor)),
mediums_(std::move(mediums)),
ui_broker_(std::move(ui_broker)),
notification_controller_(std::move(notification_controller)),
authentication_manager_(std::move(authentication_manager)),
http_client_(std::move(http_client)),
device_info_(std::move(device_info)) {
NearbyFlags::GetInstance().OverrideBoolFlagValue(
platform::config_package_nearby::nearby_platform_feature::
kEnableBleV2Gatt,
true);
const_cast<FeatureFlags&>(FeatureFlags::GetInstance())
.SetFlags(fast_pair_feature_flags);
devices_ = std::make_unique<FastPairDeviceRepository>(executor_.get());
scanner_broker_ = std::make_unique<ScannerBrokerImpl>(
*mediums_, executor_.get(), devices_.get());
task_runner_ = std::make_unique<TaskRunnerImpl>(1);
preferences_manager_ = std::make_unique<preferences::PreferencesManager>(
kFastPairPreferencesFilePath);
account_manager_ = AccountManagerImpl::Factory::Create(
preferences_manager_.get(), prefs::kNearbyFastPairUsersName,
authentication_manager_.get(), task_runner_.get());
fast_pair_client_ = std::make_unique<FastPairClientImpl>(
authentication_manager_.get(), account_manager_.get(), http_client_.get(),
&fast_pair_http_notifier_, device_info_.get());
fast_pair_repository_ =
std::make_unique<FastPairRepositoryImpl>(fast_pair_client_.get());
pairer_broker_ = std::make_unique<PairerBrokerImpl>(
*mediums_, executor_.get(), account_manager_.get());
scanner_broker_->AddObserver(this);
ui_broker_->AddObserver(this);
pairer_broker_->AddObserver(this);
}
void Mediator::OnDeviceFound(FastPairDevice& device) {
NEARBY_LOGS(INFO) << __func__ << ": " << device;
if (device.ShouldShowUiNotification().value_or(false)) {
NEARBY_LOGS(INFO) << __func__ << ": Ignoring because show UI flag is false";
return;
}
if (foreground_currently_showing_notification_) {
NEARBY_LOGS(VERBOSE)
<< __func__
<< ": Already showing a notification for a different device= ";
return;
}
// Show discovery notification
foreground_currently_showing_notification_ = true;
ui_broker_->ShowDiscovery(device, *notification_controller_);
}
void Mediator::OnDeviceLost(FastPairDevice& device) {
NEARBY_LOGS(INFO) << __func__ << ": " << device;
}
void Mediator::OnDiscoveryAction(FastPairDevice& device,
DiscoveryAction action) {
switch (action) {
case DiscoveryAction::kPairToDevice:
NEARBY_LOGS(INFO) << __func__ << ": Action = kPairToDevice";
pairer_broker_->PairDevice(device);
break;
case DiscoveryAction::kDismissedByOs:
NEARBY_LOGS(INFO) << __func__ << ": Action = kDismissedByOs";
break;
case DiscoveryAction::kDismissedByUser:
// When the user explicitly dismisses the discovery notification, update
// the device's block-list value accordingly.
NEARBY_LOGS(INFO) << __func__ << ": Action = kDismissedByUser";
foreground_currently_showing_notification_ = false;
// TODO(b/285453663): update discovery block list
break;
case DiscoveryAction::kDismissedByTimeout:
NEARBY_LOGS(INFO) << __func__ << ": Action = kDismissedByTimeout";
foreground_currently_showing_notification_ = false;
break;
case DiscoveryAction::kLearnMore:
NEARBY_LOGS(INFO) << __func__ << ": Action = kLearnMore";
break;
case DiscoveryAction::kDone:
NEARBY_LOGS(INFO) << __func__ << ": Action = kDone";
foreground_currently_showing_notification_ = false;
break;
default:
NEARBY_LOGS(INFO) << __func__ << ": Action = Unknown";
break;
}
}
void Mediator::OnDevicePaired(FastPairDevice& device) {
NEARBY_LOGS(INFO) << __func__ << ": " << device;
ui_broker_->ShowPairingResult(device, *notification_controller_, true);
}
void Mediator::OnAccountKeyWrite(FastPairDevice& device,
std::optional<PairFailure> error) {
if (error.has_value()) {
NEARBY_LOGS(INFO) << __func__ << ": Device=" << device
<< ",Error=" << error.value();
return;
}
NEARBY_LOGS(INFO) << __func__ << ": Device=" << device;
if (device.GetProtocol() == Protocol::kFastPairRetroactivePairing) {
// TODO: UI ShowAssociateAccount
}
}
void Mediator::OnPairingComplete(FastPairDevice& device) {
NEARBY_LOGS(INFO) << __func__ << ": " << device;
}
void Mediator::OnPairFailure(FastPairDevice& device, PairFailure failure) {
NEARBY_LOGS(INFO) << __func__ << ": " << device
<< " with PairFailure: " << failure;
ui_broker_->ShowPairingResult(device, *notification_controller_, false);
}
void Mediator::StartScanning() {
NEARBY_LOGS(VERBOSE) << __func__;
if (IsFastPairEnabled()) {
if (scanning_session_ != nullptr) {
return;
}
scanner_broker_ = std::make_unique<ScannerBrokerImpl>(
*mediums_, executor_.get(), devices_.get());
scanner_broker_->AddObserver(this);
scanning_session_ =
scanner_broker_->StartScanning(Protocol::kFastPairInitialPairing);
return;
}
scanning_session_.reset();
}
void Mediator::StopScanning() {
NEARBY_LOGS(VERBOSE) << __func__;
if (scanning_session_ == nullptr) {
return;
}
scanning_session_.reset();
scanner_broker_->RemoveObserver(this);
DestroyOnExecutor(std::move(scanner_broker_), executor_.get());
}
bool Mediator::IsFastPairEnabled() {
// TODO(b/275452353): Add feature_status_tracker IsFastPairEnabled()
// Currently default to true.
NEARBY_LOGS(VERBOSE) << __func__ << ": " << true;
return true;
}
void Mediator::SetIsScreenLocked(bool locked) {
executor_->Execute(
"on_lock_state_changed",
[this, locked]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_) {
NEARBY_LOGS(INFO) << __func__ << ": Screen lock state changed. ("
<< std::boolalpha << locked << ")";
is_screen_locked_ = locked;
InvalidateScanningState();
});
}
void Mediator::InvalidateScanningState() {
NEARBY_LOGS(INFO) << __func__;
// Stop scanning when screen is off.
if (is_screen_locked_) {
StopScanning();
NEARBY_LOGS(VERBOSE) << __func__
<< ": Stopping scanning because the screen is locked.";
return;
}
// TODO(b/275452353): Check if bluetooth and fast pair is enabled
// Screen is on, Bluetooth is enabled, and Fast Pair is enabled, start
// scanning.
StartScanning();
}
} // namespace fastpair
} // namespace nearby
-129
View File
@@ -1,129 +0,0 @@
// Copyright 2023 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_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_H_
#include <memory>
#include <optional>
#include <utility>
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/internal/mediums/mediums.h"
#include "fastpair/pairing/pairer_broker.h"
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/scanning/scanner_broker.h"
#include "fastpair/server_access/fast_pair_client.h"
#include "fastpair/server_access/fast_pair_http_notifier.h"
#include "fastpair/repository/fast_pair_repository.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
#include "fastpair/ui/ui_broker.h"
#include "internal/account/account_manager.h"
#include "internal/auth/authentication_manager.h"
#include "internal/network/http_client.h"
#include "internal/platform/device_info.h"
#include "internal/platform/single_thread_executor.h"
#include "internal/platform/task_runner.h"
#include "internal/preferences/preferences_manager.h"
namespace nearby {
namespace fastpair {
// Implements the Mediator design pattern for the components in the Fast Pair
class Mediator final : public ScannerBroker::Observer,
public UIBroker::Observer,
public PairerBroker::Observer {
public:
Mediator(
std::unique_ptr<SingleThreadExecutor> executor,
std::unique_ptr<Mediums> mediums, std::unique_ptr<UIBroker> ui_broker,
std::unique_ptr<FastPairNotificationController> notification_controller,
std::unique_ptr<auth::AuthenticationManager> authentication_manager,
std::unique_ptr<network::HttpClient> http_client,
std::unique_ptr<DeviceInfo> device_info);
Mediator(const Mediator&) = delete;
Mediator& operator=(const Mediator&) = delete;
~Mediator() override {
if (scanning_session_ == nullptr) {
NEARBY_LOGS(ERROR) << __func__ << "scanner is not running";
}
scanning_session_.reset();
scanner_broker_->RemoveObserver(this);
DestroyOnExecutor(std::move(scanner_broker_), executor_.get());
scanner_broker_.reset();
ui_broker_->RemoveObserver(this);
ui_broker_.reset();
};
AccountManager* GetAccountManager() { return account_manager_.get(); }
FastPairNotificationController* GetNotificationController() {
return notification_controller_.get();
}
FastPairRepository* GetFastPairRepository() {
return fast_pair_repository_.get();
}
void StartScanning();
void StopScanning();
// ScannerBroker::Observer
void OnDeviceFound(FastPairDevice& device) override;
void OnDeviceLost(FastPairDevice& device) override;
// UIBroker::Observer
void OnDiscoveryAction(FastPairDevice& device,
DiscoveryAction action) override;
// PairBroker:Observer
void OnDevicePaired(FastPairDevice& device) override;
void OnAccountKeyWrite(FastPairDevice& device,
std::optional<PairFailure> error) override;
void OnPairingComplete(FastPairDevice& device) override;
void OnPairFailure(FastPairDevice& device, PairFailure failure) override;
// Handle the state changes of screen lock.
void SetIsScreenLocked(bool is_locked);
private:
bool IsFastPairEnabled();
void InvalidateScanningState() ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_);
bool IsDeviceCurrentlyShowingNotification(const FastPairDevice& device);
bool foreground_currently_showing_notification_ = false;
FastPairHttpNotifier fast_pair_http_notifier_;
std::unique_ptr<SingleThreadExecutor> executor_;
std::unique_ptr<Mediums> mediums_;
std::unique_ptr<ScannerBroker> scanner_broker_;
std::unique_ptr<ScannerBroker::ScanningSession> scanning_session_;
std::unique_ptr<UIBroker> ui_broker_;
std::unique_ptr<PairerBroker> pairer_broker_;
std::unique_ptr<FastPairNotificationController> notification_controller_;
std::unique_ptr<FastPairRepository> fast_pair_repository_;
std::unique_ptr<TaskRunner> task_runner_;
std::unique_ptr<FastPairDeviceRepository> devices_;
std::unique_ptr<AccountManager> account_manager_;
std::unique_ptr<preferences::PreferencesManager> preferences_manager_;
std::unique_ptr<auth::AuthenticationManager> authentication_manager_;
std::unique_ptr<FastPairClient> fast_pair_client_;
std::unique_ptr<network::HttpClient> http_client_;
std::unique_ptr<DeviceInfo> device_info_;
bool is_screen_locked_ = false;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_H_
@@ -1,47 +0,0 @@
// Copyright 2023 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 "fastpair/keyed_service/fast_pair_mediator_factory.h"
#include <memory>
#include "fastpair/internal/mediums/mediums.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
#include "fastpair/ui/ui_broker_impl.h"
#include "internal/auth/authentication_manager_impl.h"
#include "internal/network/http_client_impl.h"
#include "internal/platform/device_info_impl.h"
#include "internal/platform/single_thread_executor.h"
namespace nearby {
namespace fastpair {
MediatorFactory* MediatorFactory::GetInstance() {
static MediatorFactory* instance = new MediatorFactory();
return instance;
}
Mediator* MediatorFactory::CreateMediator() {
mediator_ = std::make_unique<Mediator>(
std::make_unique<SingleThreadExecutor>(), std::make_unique<Mediums>(),
std::make_unique<UIBrokerImpl>(),
std::make_unique<FastPairNotificationController>(),
std::make_unique<auth::AuthenticationManagerImpl>(),
std::make_unique<network::NearbyHttpClient>(),
std::make_unique<DeviceInfoImpl>());
return mediator_.get();
}
} // namespace fastpair
} // namespace nearby
@@ -1,39 +0,0 @@
// Copyright 2023 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_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_FACTORY_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_FACTORY_H_
#include <memory>
#include "fastpair/keyed_service/fast_pair_mediator.h"
namespace nearby {
namespace fastpair {
class MediatorFactory {
public:
// Return a singleton instance of Mediator Factory
static MediatorFactory* GetInstance();
Mediator* CreateMediator();
private:
std::unique_ptr<Mediator> mediator_;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_FACTORY_H_
@@ -1,37 +0,0 @@
// Copyright 2023 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 "fastpair/keyed_service/fast_pair_mediator_factory.h"
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "fastpair/keyed_service/fast_pair_mediator.h"
namespace nearby {
namespace fastpair {
namespace {
TEST(MediatorFactoryTest, checkMediatorCreateWithFactory) {
MediatorFactory *factory_ = nullptr;
Mediator *mediator_ = nullptr;
factory_ = MediatorFactory::GetInstance();
EXPECT_THAT(factory_, testing::NotNull());
mediator_ = factory_->CreateMediator();
EXPECT_THAT(mediator_, testing::NotNull());
}
} // namespace
} // namespace fastpair
} // namespace nearby
@@ -1,290 +0,0 @@
// Copyright 2023 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 "fastpair/keyed_service/fast_pair_mediator.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/strings/escaping.h"
#include "absl/strings/string_view.h"
#include "fastpair/testing/fast_pair_service_data_creator.h"
#include "fastpair/ui/actions.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
#include "fastpair/ui/mock_ui_broker.h"
#include "fastpair/ui/ui_broker.h"
#include "internal/account/fake_account_manager.h"
#include "internal/network/http_client_impl.h"
#include "internal/platform/device_info_impl.h"
#include "internal/platform/medium_environment.h"
#include "internal/platform/single_thread_executor.h"
#include "internal/test/fake_device_info.h"
#include "internal/test/fake_http_client.h"
#include "internal/test/google3_only/fake_authentication_manager.h"
namespace nearby {
namespace fastpair {
namespace {
constexpr absl::string_view kModelId = "718c17";
constexpr absl::string_view kServiceID = "Fast Pair";
constexpr absl::string_view kFastPairServiceUuid =
"0000FE2C-0000-1000-8000-00805F9B34FB";
constexpr int kNotDiscoverableAdvHeader = 0b00000110;
constexpr int kAccountKeyFilterHeader = 0b01100000;
constexpr int kSaltHeader = 0b00010001;
constexpr absl::string_view kAccountKeyFilter("112233445566");
constexpr absl::string_view kSalt("01");
constexpr absl::string_view kModelId2 = "9adb11";
constexpr absl::string_view kAddress = "74:74:46:01:6C:21";
class MediatorTest : public testing::Test {
public:
MediatorTest() {
AccountManagerImpl::Factory::SetFactoryForTesting(
&account_manager_factory_);
http_client_ = std::make_unique<network::FakeHttpClient>();
device_info_ = std::make_unique<FakeDeviceInfo>();
authentication_manager_ =
std::make_unique<nearby::FakeAuthenticationManager>();
}
void SetUp() override {
env_.Start();
GetAuthManager()->EnableSyncMode();
mediums_ = std::make_unique<Mediums>();
ui_broker_ = std::make_unique<MockUIBroker>();
mock_ui_broker_ = static_cast<MockUIBroker*>(ui_broker_.get());
notification_controller_ =
std::make_unique<FastPairNotificationController>();
executor_ = std::make_unique<SingleThreadExecutor>();
}
void TearDown() override {
executor_.reset();
mediums_.reset();
ui_broker_.reset();
mock_ui_broker_ = nullptr;
notification_controller_.reset();
mediator_.reset();
env_.Stop();
}
nearby::FakeAuthenticationManager* GetAuthManager() {
return reinterpret_cast<nearby::FakeAuthenticationManager*>(
authentication_manager_.get());
}
network::FakeHttpClient* GetHttpClient() {
return reinterpret_cast<network::FakeHttpClient*>(http_client_.get());
}
void SetUpDeviceMetadata() {
proto::GetObservedDeviceResponse response_proto;
auto* device = response_proto.mutable_device();
int64_t device_id;
CHECK(absl::SimpleHexAtoi(kModelId, &device_id));
device->set_id(device_id);
network::HttpResponse response;
response.SetStatusCode(network::HttpStatusCode::kHttpOk);
response.SetBody(response_proto.SerializeAsString());
GetHttpClient()->SetResponseForSyncRequest(response);
}
protected:
MediumEnvironment& env_{MediumEnvironment::Instance()};
std::unique_ptr<Mediums> mediums_;
std::unique_ptr<UIBroker> ui_broker_;
std::unique_ptr<FastPairNotificationController> notification_controller_;
std::unique_ptr<SingleThreadExecutor> executor_;
MockUIBroker* mock_ui_broker_;
std::unique_ptr<Mediator> mediator_;
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_;
};
TEST_F(MediatorTest, StartScanningFoundDevice) {
SetUpDeviceMetadata();
// Create Fast Pair Mediator
mediator_ = std::make_unique<Mediator>(
std::move(executor_), std::move(mediums_), std::move(ui_broker_),
std::move(notification_controller_), std::move(authentication_manager_),
std::move(http_client_), std::move(device_info_));
absl::Notification done;
EXPECT_CALL(*mock_ui_broker_, ShowDiscovery).WillOnce([&done] {
done.Notify();
});
// Create Advertiser and startAdvertising
Mediums mediums_advertiser;
std::string service_id(kServiceID);
ByteArray advertisement_bytes{absl::HexStringToBytes(kModelId)};
std::string fast_pair_service_uuid(kFastPairServiceUuid);
mediums_advertiser.GetBle().GetMedium().StartAdvertising(
service_id, advertisement_bytes, fast_pair_service_uuid);
mediator_->StartScanning();
done.WaitForNotification();
}
TEST_F(MediatorTest, StartScanningFoundDifferentDeviceWhenDisplaying) {
SetUpDeviceMetadata();
// Create Fast Pair Mediator
mediator_ = std::make_unique<Mediator>(
std::move(executor_), std::move(mediums_), std::move(ui_broker_),
std::move(notification_controller_), std::move(authentication_manager_),
std::move(http_client_), std::move(device_info_));
absl::Notification done;
EXPECT_CALL(*mock_ui_broker_, ShowDiscovery).Times(1).WillOnce([&done] {
done.Notify();
});
// Create Advertiser and startAdvertising
Mediums mediums_advertiser;
std::string service_id(kServiceID);
ByteArray advertisement_bytes{absl::HexStringToBytes(kModelId)};
std::string fast_pair_service_uuid(kFastPairServiceUuid);
mediums_advertiser.GetBle().GetMedium().StartAdvertising(
service_id, advertisement_bytes, fast_pair_service_uuid);
// Create a different Advertiser and startAdvertising to cause confliction
Mediums mediums_advertiser2;
ByteArray advertisement_bytes2{absl::HexStringToBytes(kModelId2)};
mediums_advertiser2.GetBle().GetMedium().StartAdvertising(
service_id, advertisement_bytes2, fast_pair_service_uuid);
mediator_->StartScanning();
done.WaitForNotification();
}
TEST_F(MediatorTest, StartScanningFoundSameDeviceWhenDisplaying) {
SetUpDeviceMetadata();
// Create Fast Pair Mediator
mediator_ = std::make_unique<Mediator>(
std::move(executor_), std::move(mediums_), std::move(ui_broker_),
std::move(notification_controller_), std::move(authentication_manager_),
std::move(http_client_), std::move(device_info_));
absl::Notification done;
EXPECT_CALL(*mock_ui_broker_, ShowDiscovery).Times(1).WillOnce([&done] {
done.Notify();
});
// Create Advertiser and startAdvertising
Mediums mediums_advertiser;
std::string service_id(kServiceID);
ByteArray advertisement_bytes{absl::HexStringToBytes(kModelId)};
std::string fast_pair_service_uuid(kFastPairServiceUuid);
mediums_advertiser.GetBle().GetMedium().StartAdvertising(
service_id, advertisement_bytes, fast_pair_service_uuid);
// Create another same Advertiser and startAdvertising to cause confliction
Mediums mediums_advertiser2;
ByteArray advertisement_bytes2{absl::HexStringToBytes(kModelId)};
mediums_advertiser2.GetBle().GetMedium().StartAdvertising(
service_id, advertisement_bytes2, fast_pair_service_uuid);
mediator_->StartScanning();
done.WaitForNotification();
}
TEST_F(MediatorTest,
StartScanningForSubsequentPairingFoundSameDeviceWhenDisplaying) {
SetUpDeviceMetadata();
// Create Fast Pair Mediator
mediator_ = std::make_unique<Mediator>(
std::move(executor_), std::move(mediums_), std::move(ui_broker_),
std::move(notification_controller_), std::move(authentication_manager_),
std::move(http_client_), std::move(device_info_));
absl::Notification done;
EXPECT_CALL(*mock_ui_broker_, ShowDiscovery).Times(1).WillOnce([&done] {
done.Notify();
});
// Create Advertiser and advertising discoverable advertisement
Mediums mediums_advertiser;
std::string service_id(kServiceID);
ByteArray advertisement_bytes{absl::HexStringToBytes(kModelId)};
std::string fast_pair_service_uuid(kFastPairServiceUuid);
mediums_advertiser.GetBle().GetMedium().StartAdvertising(
service_id, advertisement_bytes, fast_pair_service_uuid);
// Create another same Advertiser and advertising non-discoverable
// advertisement to cause confliction
Mediums mediums_advertiser2;
std::vector<uint8_t> bytes = FastPairServiceDataCreator::Builder()
.SetHeader(kNotDiscoverableAdvHeader)
.SetModelId(kModelId)
.AddExtraFieldHeader(kAccountKeyFilterHeader)
.AddExtraField(kAccountKeyFilter)
.AddExtraFieldHeader(kSaltHeader)
.AddExtraField(kSalt)
.Build()
->CreateServiceData();
ByteArray advertisement_bytes2{std::string(bytes.begin(), bytes.end())};
mediums_advertiser2.GetBle().GetMedium().StartAdvertising(
service_id, advertisement_bytes2, fast_pair_service_uuid);
mediator_->StartScanning();
done.WaitForNotification();
}
TEST_F(MediatorTest, OnDiscoveryActionClicked) {
SetUpDeviceMetadata();
SetUpDeviceMetadata();
// Create Fast Pair Mediator
mediator_ = std::make_unique<Mediator>(
std::move(executor_), std::move(mediums_), std::move(ui_broker_),
std::move(notification_controller_), std::move(authentication_manager_),
std::move(http_client_), std::move(device_info_));
absl::Notification done;
EXPECT_CALL(*mock_ui_broker_, ShowDiscovery).Times(2).WillOnce([&done] {
done.Notify();
});
// Create Advertiser and startAdvertising
Mediums mediums_advertiser;
std::string service_id(kServiceID);
ByteArray advertisement_bytes{absl::HexStringToBytes(kModelId)};
std::string fast_pair_service_uuid(kFastPairServiceUuid);
mediums_advertiser.GetBle().GetMedium().StartAdvertising(
service_id, advertisement_bytes, fast_pair_service_uuid);
mediator_->StartScanning();
done.WaitForNotification();
FastPairDevice device(kModelId, kAddress, Protocol::kFastPairInitialPairing);
mock_ui_broker_->NotifyDiscoveryAction(device,
DiscoveryAction::kDismissedByTimeout);
// Create another same Advertiser and startAdvertising to cause confliction
Mediums mediums_advertiser2;
ByteArray advertisement_bytes2{absl::HexStringToBytes(kModelId)};
mediums_advertiser2.GetBle().GetMedium().StartAdvertising(
service_id, advertisement_bytes2, fast_pair_service_uuid);
done.WaitForNotification();
}
} // namespace
} // namespace fastpair
} // namespace nearby