From e8c0ce3d89d6d2c136d3942ed6efa687d937812b Mon Sep 17 00:00:00 2001 From: Qin Wang Date: Thu, 3 Aug 2023 11:25:34 -0700 Subject: [PATCH] Deprecate Fast Pair Mediator PiperOrigin-RevId: 553538010 --- fastpair/keyed_service/BUILD | 79 ----- fastpair/keyed_service/fast_pair_mediator.cc | 242 --------------- fastpair/keyed_service/fast_pair_mediator.h | 129 -------- .../fast_pair_mediator_factory.cc | 47 --- .../fast_pair_mediator_factory.h | 39 --- .../fast_pair_mediator_factory_test.cc | 37 --- .../keyed_service/fast_pair_mediator_test.cc | 290 ------------------ 7 files changed, 863 deletions(-) delete mode 100644 fastpair/keyed_service/BUILD delete mode 100644 fastpair/keyed_service/fast_pair_mediator.cc delete mode 100644 fastpair/keyed_service/fast_pair_mediator.h delete mode 100644 fastpair/keyed_service/fast_pair_mediator_factory.cc delete mode 100644 fastpair/keyed_service/fast_pair_mediator_factory.h delete mode 100644 fastpair/keyed_service/fast_pair_mediator_factory_test.cc delete mode 100644 fastpair/keyed_service/fast_pair_mediator_test.cc diff --git a/fastpair/keyed_service/BUILD b/fastpair/keyed_service/BUILD deleted file mode 100644 index b5046e28..00000000 --- a/fastpair/keyed_service/BUILD +++ /dev/null @@ -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", - ], -) diff --git a/fastpair/keyed_service/fast_pair_mediator.cc b/fastpair/keyed_service/fast_pair_mediator.cc deleted file mode 100644 index 16850e2c..00000000 --- a/fastpair/keyed_service/fast_pair_mediator.cc +++ /dev/null @@ -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 -#include -#include -#include -#include - -#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 executor, - std::unique_ptr mediums, std::unique_ptr ui_broker, - std::unique_ptr notification_controller, - std::unique_ptr authentication_manager, - std::unique_ptr http_client, - std::unique_ptr 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::GetInstance()) - .SetFlags(fast_pair_feature_flags); - - devices_ = std::make_unique(executor_.get()); - scanner_broker_ = std::make_unique( - *mediums_, executor_.get(), devices_.get()); - task_runner_ = std::make_unique(1); - preferences_manager_ = std::make_unique( - kFastPairPreferencesFilePath); - account_manager_ = AccountManagerImpl::Factory::Create( - preferences_manager_.get(), prefs::kNearbyFastPairUsersName, - authentication_manager_.get(), task_runner_.get()); - fast_pair_client_ = std::make_unique( - authentication_manager_.get(), account_manager_.get(), http_client_.get(), - &fast_pair_http_notifier_, device_info_.get()); - fast_pair_repository_ = - std::make_unique(fast_pair_client_.get()); - pairer_broker_ = std::make_unique( - *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 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( - *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 diff --git a/fastpair/keyed_service/fast_pair_mediator.h b/fastpair/keyed_service/fast_pair_mediator.h deleted file mode 100644 index 16c4f6a2..00000000 --- a/fastpair/keyed_service/fast_pair_mediator.h +++ /dev/null @@ -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 -#include -#include - -#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 executor, - std::unique_ptr mediums, std::unique_ptr ui_broker, - std::unique_ptr notification_controller, - std::unique_ptr authentication_manager, - std::unique_ptr http_client, - std::unique_ptr 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 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 executor_; - std::unique_ptr mediums_; - std::unique_ptr scanner_broker_; - std::unique_ptr scanning_session_; - std::unique_ptr ui_broker_; - std::unique_ptr pairer_broker_; - std::unique_ptr notification_controller_; - std::unique_ptr fast_pair_repository_; - std::unique_ptr task_runner_; - std::unique_ptr devices_; - std::unique_ptr account_manager_; - std::unique_ptr preferences_manager_; - std::unique_ptr authentication_manager_; - std::unique_ptr fast_pair_client_; - std::unique_ptr http_client_; - std::unique_ptr device_info_; - bool is_screen_locked_ = false; -}; - -} // namespace fastpair -} // namespace nearby - -#endif // THIRD_PARTY_NEARBY_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_H_ diff --git a/fastpair/keyed_service/fast_pair_mediator_factory.cc b/fastpair/keyed_service/fast_pair_mediator_factory.cc deleted file mode 100644 index 5e87f8f1..00000000 --- a/fastpair/keyed_service/fast_pair_mediator_factory.cc +++ /dev/null @@ -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 - -#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( - std::make_unique(), std::make_unique(), - std::make_unique(), - std::make_unique(), - std::make_unique(), - std::make_unique(), - std::make_unique()); - return mediator_.get(); -} - -} // namespace fastpair -} // namespace nearby diff --git a/fastpair/keyed_service/fast_pair_mediator_factory.h b/fastpair/keyed_service/fast_pair_mediator_factory.h deleted file mode 100644 index f1117aed..00000000 --- a/fastpair/keyed_service/fast_pair_mediator_factory.h +++ /dev/null @@ -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 - -#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_; -}; - -} // namespace fastpair -} // namespace nearby - -#endif // THIRD_PARTY_NEARBY_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_FACTORY_H_ diff --git a/fastpair/keyed_service/fast_pair_mediator_factory_test.cc b/fastpair/keyed_service/fast_pair_mediator_factory_test.cc deleted file mode 100644 index 234112a2..00000000 --- a/fastpair/keyed_service/fast_pair_mediator_factory_test.cc +++ /dev/null @@ -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 diff --git a/fastpair/keyed_service/fast_pair_mediator_test.cc b/fastpair/keyed_service/fast_pair_mediator_test.cc deleted file mode 100644 index bd832bb7..00000000 --- a/fastpair/keyed_service/fast_pair_mediator_test.cc +++ /dev/null @@ -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 -#include -#include -#include - -#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(); - device_info_ = std::make_unique(); - authentication_manager_ = - std::make_unique(); - } - void SetUp() override { - env_.Start(); - GetAuthManager()->EnableSyncMode(); - mediums_ = std::make_unique(); - ui_broker_ = std::make_unique(); - mock_ui_broker_ = static_cast(ui_broker_.get()); - - notification_controller_ = - std::make_unique(); - - executor_ = std::make_unique(); - } - - 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( - authentication_manager_.get()); - } - - network::FakeHttpClient* GetHttpClient() { - return reinterpret_cast(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_; - std::unique_ptr ui_broker_; - std::unique_ptr notification_controller_; - std::unique_ptr executor_; - MockUIBroker* mock_ui_broker_; - std::unique_ptr mediator_; - FakeAccountManager::Factory account_manager_factory_; - std::unique_ptr authentication_manager_; - std::unique_ptr http_client_; - std::unique_ptr device_info_; -}; - -TEST_F(MediatorTest, StartScanningFoundDevice) { - SetUpDeviceMetadata(); - // Create Fast Pair Mediator - mediator_ = std::make_unique( - 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( - 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( - 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( - 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 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( - 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