diff --git a/fastpair/keyed_service/fast_pair_mediator.cc b/fastpair/keyed_service/fast_pair_mediator.cc index 44913753..e6ddd041 100644 --- a/fastpair/keyed_service/fast_pair_mediator.cc +++ b/fastpair/keyed_service/fast_pair_mediator.cc @@ -21,14 +21,15 @@ #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/repository/fast_pair_repository_impl.h" #include "fastpair/ui/actions.h" #include "fastpair/ui/fast_pair/fast_pair_notification_controller.h" #include "fastpair/ui/ui_broker_impl.h" @@ -98,21 +99,14 @@ void Mediator::OnDeviceFound(FastPairDevice& device) { NEARBY_LOGS(INFO) << __func__ << ": Ignoring because show UI flag is false"; return; } - if (IsDeviceCurrentlyShowingNotification(device)) { - NEARBY_LOGS(VERBOSE) << __func__ - << ": Extending notification for re-discovered device=" - << *device_currently_showing_notification_; - // TODO(b/278768167): Add ui_broker_->ExtendNotification(); - return; - } else if (device_currently_showing_notification_) { + if (foreground_currently_showing_notification_) { NEARBY_LOGS(VERBOSE) << __func__ - << ": Already showing a notification for a different device= " - << *device_currently_showing_notification_; + << ": Already showing a notification for a different device= "; return; } // Show discovery notification - device_currently_showing_notification_ = &device; + foreground_currently_showing_notification_ = true; ui_broker_->ShowDiscovery(device, *notification_controller_); } @@ -125,8 +119,6 @@ void Mediator::OnDiscoveryAction(FastPairDevice& device, switch (action) { case DiscoveryAction::kPairToDevice: NEARBY_LOGS(INFO) << __func__ << ": Action = kPairToDevice"; - // TODO(285451051): Adding show pairing for higher than v1 version in ui - // broker pairer_broker_->PairDevice(device); break; case DiscoveryAction::kDismissedByOs: @@ -136,23 +128,29 @@ void Mediator::OnDiscoveryAction(FastPairDevice& device, // When the user explicitly dismisses the discovery notification, update // the device's block-list value accordingly. NEARBY_LOGS(INFO) << __func__ << ": Action = kDismissedByUser"; - // TODO(285453663): update discovery block list + foreground_currently_showing_notification_ = false; + // TODO(b/285453663): update discovery block list [[fallthrough]]; case DiscoveryAction::kDismissedByTimeout: NEARBY_LOGS(INFO) << __func__ << ": Action = kDismissedByTimeout"; - device_currently_showing_notification_ = nullptr; + 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 = kUnknow"; + 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, @@ -176,7 +174,7 @@ void Mediator::OnPairingComplete(FastPairDevice& device) { void Mediator::OnPairFailure(FastPairDevice& device, PairFailure failure) { NEARBY_LOGS(INFO) << __func__ << ": " << device << " with PairFailure: " << failure; - // TODO: UI showPairingFailed + ui_broker_->ShowPairingResult(device, *notification_controller_, false); } void Mediator::StartScanning() { @@ -212,24 +210,6 @@ bool Mediator::IsFastPairEnabled() { return true; } -bool Mediator::IsDeviceCurrentlyShowingNotification( - const FastPairDevice& device) { - // BLE addresses could have rotated, causing this check to return false for - // the same device. Fast Pair considers a device different if they have - // different BLE addresses. Similarly, the this check will fail if it is the - // same physical device under different scenarios: for example, if a device - // is found via the initial scenario and via the subsequent scenario, Fast - // Pair does not consider them the same device. - - return device_currently_showing_notification_ && - device_currently_showing_notification_->GetModelId() == - device.GetModelId() && - device_currently_showing_notification_->GetBleAddress() == - device.GetBleAddress() && - device_currently_showing_notification_->GetProtocol() != - device.GetProtocol(); -} - void Mediator::SetIsScreenLocked(bool locked) { executor_->Execute( "on_lock_state_changed", diff --git a/fastpair/keyed_service/fast_pair_mediator.h b/fastpair/keyed_service/fast_pair_mediator.h index 7b2fbeef..16c4f6a2 100644 --- a/fastpair/keyed_service/fast_pair_mediator.h +++ b/fastpair/keyed_service/fast_pair_mediator.h @@ -102,9 +102,7 @@ class Mediator final : public ScannerBroker::Observer, void InvalidateScanningState() ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_); bool IsDeviceCurrentlyShowingNotification(const FastPairDevice& device); - // |device_currently_showing_notification_| can be null if there is no - // notification currently displayed to the user. - FastPairDevice* device_currently_showing_notification_ = nullptr; + bool foreground_currently_showing_notification_ = false; FastPairHttpNotifier fast_pair_http_notifier_; std::unique_ptr executor_; std::unique_ptr mediums_; diff --git a/fastpair/ui/BUILD b/fastpair/ui/BUILD index 52fef3f8..16f74cbc 100644 --- a/fastpair/ui/BUILD +++ b/fastpair/ui/BUILD @@ -88,6 +88,7 @@ cc_test( ":fake_fast_pair_ui", ":fast_pair_ui", "//fastpair/common", + "//internal/platform:types", "//internal/platform/implementation/g3", # build_cleaner: keep "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_googletest//:gtest_main", diff --git a/fastpair/ui/actions.h b/fastpair/ui/actions.h index 6228d4ee..a7910281 100644 --- a/fastpair/ui/actions.h +++ b/fastpair/ui/actions.h @@ -24,7 +24,8 @@ enum class DiscoveryAction { kDismissedByUser = 2, kDismissedByOs = 3, kLearnMore = 4, - kDismissedByTimeout = 5, + kDone = 5, + kDismissedByTimeout = 6, }; } // namespace fastpair diff --git a/fastpair/ui/fast_pair/fake_fast_pair_notification_controller_observer.h b/fastpair/ui/fast_pair/fake_fast_pair_notification_controller_observer.h index 2c1a15d9..e494fb8d 100644 --- a/fastpair/ui/fast_pair/fake_fast_pair_notification_controller_observer.h +++ b/fastpair/ui/fast_pair/fake_fast_pair_notification_controller_observer.h @@ -23,6 +23,7 @@ #include "fastpair/common/device_metadata.h" #include "fastpair/ui/fast_pair/fast_pair_notification_controller.h" #include "internal/platform/count_down_latch.h" +#include "internal/platform/mutex_lock.h" namespace nearby { namespace fastpair { @@ -30,35 +31,45 @@ class FakeFastPairNotificationControllerObserver : public FastPairNotificationController::Observer { public: explicit FakeFastPairNotificationControllerObserver( - std::optional latch) { - latch_ = latch; + CountDownLatch* on_device_updated_latch, + CountDownLatch* on_pairing_result_latch) { + on_device_updated_latch_ = on_device_updated_latch; + on_pairing_result_latch_ = on_pairing_result_latch; } void OnUpdateDevice(const DeviceMetadata& device) override { - device_metadata_name_list_.push_back(device.GetDetails().name()); - on_update_device_count_++; - if (latch_.has_value()) { - latch_->CountDown(); + MutexLock lock(&mutex_); + device_ = &const_cast(device); + if (on_device_updated_latch_) { + on_device_updated_latch_->CountDown(); } } - bool CheckDeviceMetadataListContainTestDevice( - const std::string& device_name) { - auto it = std::find(device_metadata_name_list_.begin(), - device_metadata_name_list_.end(), device_name); - return it != device_metadata_name_list_.end(); + void OnPairingResult(const DeviceMetadata& device, bool success) override { + MutexLock lock(&mutex_); + pairing_result_ = success; + device_ = &const_cast(device); + if (on_pairing_result_latch_) { + on_pairing_result_latch_->CountDown(); + } } - int on_update_device_count() { return on_update_device_count_; } + DeviceMetadata* GetDevice() { + MutexLock lock(&mutex_); + return device_; + } - std::vector device_metadata_name_list() { - return device_metadata_name_list_; + std::optional GetPairingResult() { + MutexLock lock(&mutex_); + return pairing_result_; } private: - std::vector device_metadata_name_list_; - int on_update_device_count_ = 0; - std::optional latch_; + Mutex mutex_; + CountDownLatch* on_device_updated_latch_; + CountDownLatch* on_pairing_result_latch_; + DeviceMetadata* device_ ABSL_GUARDED_BY(mutex_) = nullptr; + std::optional pairing_result_ ABSL_GUARDED_BY(mutex_); }; } // namespace fastpair } // namespace nearby diff --git a/fastpair/ui/fast_pair/fake_fast_pair_presenter.h b/fastpair/ui/fast_pair/fake_fast_pair_presenter.h index b5a1c89e..052fd955 100644 --- a/fastpair/ui/fast_pair/fake_fast_pair_presenter.h +++ b/fastpair/ui/fast_pair/fake_fast_pair_presenter.h @@ -35,13 +35,24 @@ class FakeFastPairPresenter : public FastPairPresenter { version_changed_ = true; callback(DiscoveryAction::kPairToDevice); } + + void ShowPairingResult( + FastPairDevice& device, + FastPairNotificationController& notification_controller, + bool success) override { + pairing_result_changed_ = true; + } + bool show_discovery() { return show_discovery_; } bool version_changed() { return version_changed_; } + bool pairing_result_changed() { return pairing_result_changed_; } + private: bool show_discovery_ = false; bool version_changed_ = false; + bool pairing_result_changed_ = false; }; class FakeFastPairPresenterFactory : public FastPairPresenterImpl::Factory { diff --git a/fastpair/ui/fast_pair/fast_pair_notification_controller.cc b/fastpair/ui/fast_pair/fast_pair_notification_controller.cc index e38e4405..f19396dd 100644 --- a/fastpair/ui/fast_pair/fast_pair_notification_controller.cc +++ b/fastpair/ui/fast_pair/fast_pair_notification_controller.cc @@ -39,6 +39,14 @@ void FastPairNotificationController::NotifyShowDiscovery( } } +void FastPairNotificationController::NotifyShowPairingResult( + const DeviceMetadata& device, bool success) { + NEARBY_LOGS(INFO) << __func__; + for (Observer* observer : observers_.GetObservers()) { + observer->OnPairingResult(device, success); + } +} + void FastPairNotificationController::ShowGuestDiscoveryNotification( const DeviceMetadata& device, DiscoveryCallback callback) { callback_ = std::move(callback); @@ -46,6 +54,12 @@ void FastPairNotificationController::ShowGuestDiscoveryNotification( NotifyShowDiscovery(device); } +void FastPairNotificationController::ShowPairingResultNotification( + const DeviceMetadata& device, bool success) { + NEARBY_LOGS(INFO) << __func__ << "Notify show pairing result notification. "; + NotifyShowPairingResult(device, success); +} + void FastPairNotificationController::OnDiscoveryClicked( DiscoveryAction action) { NEARBY_LOGS(INFO) << __func__ diff --git a/fastpair/ui/fast_pair/fast_pair_notification_controller.h b/fastpair/ui/fast_pair/fast_pair_notification_controller.h index 9f792225..7978ee4d 100644 --- a/fastpair/ui/fast_pair/fast_pair_notification_controller.h +++ b/fastpair/ui/fast_pair/fast_pair_notification_controller.h @@ -39,6 +39,8 @@ class FastPairNotificationController { public: virtual ~Observer() = default; virtual void OnUpdateDevice(const DeviceMetadata& device) = 0; + virtual void OnPairingResult(const DeviceMetadata& device, + bool success) = 0; }; FastPairNotificationController() = default; @@ -52,11 +54,15 @@ class FastPairNotificationController { void AddObserver(Observer* observer); void RemoveObserver(Observer* observer); void NotifyShowDiscovery(const DeviceMetadata& device); + void NotifyShowPairingResult(const DeviceMetadata& device, bool success); // Creates and displays corresponding notification. void ShowGuestDiscoveryNotification(const DeviceMetadata& device_metadata, DiscoveryCallback callback); + void ShowPairingResultNotification(const DeviceMetadata& device_metadata, + bool success); + // Triggers callback when the related action is clicked. void OnDiscoveryClicked(DiscoveryAction action); diff --git a/fastpair/ui/fast_pair/fast_pair_notification_controller_test.cc b/fastpair/ui/fast_pair/fast_pair_notification_controller_test.cc index 871dd4a5..081f6f79 100644 --- a/fastpair/ui/fast_pair/fast_pair_notification_controller_test.cc +++ b/fastpair/ui/fast_pair/fast_pair_notification_controller_test.cc @@ -25,6 +25,7 @@ #include "fastpair/common/device_metadata.h" #include "fastpair/ui/actions.h" #include "fastpair/ui/fast_pair/fake_fast_pair_notification_controller_observer.h" +#include "internal/platform/count_down_latch.h" namespace nearby { namespace fastpair { @@ -34,46 +35,46 @@ const int64_t kDeviceId = 10148625; const char kModelId[] = "9adb11"; const char kDeviceName[] = "Pixel Buds Pro"; -class FastPairNotificationControllerTest : public ::testing::Test { - protected: - FastPairNotificationControllerTest() { - notification_controller_obsesrver_ = - std::make_unique( - std::nullopt); - notification_controller_.AddObserver( - notification_controller_obsesrver_.get()); - } - - void TriggerOnUpdateDevice(DeviceMetadata& device, - DiscoveryCallback callback) { - notification_controller_.ShowGuestDiscoveryNotification( - device, std::move(callback)); - } - - void DiscoveryActionClicked(DiscoveryAction action) { - discovery_action_ = action; - } - - FastPairNotificationController notification_controller_; - std::unique_ptr - notification_controller_obsesrver_; - DiscoveryAction discovery_action_; -}; - -TEST_F(FastPairNotificationControllerTest, ShowGuestDiscoveryNotification) { +TEST(FastPairNotificationControllerTest, ShowGuestDiscoveryNotification) { + FastPairNotificationController notification_controller; proto::GetObservedDeviceResponse response; - response.mutable_device()->set_id(kDeviceId); - response.mutable_device()->set_name(kDeviceName); DeviceMetadata device_metadata(response); - TriggerOnUpdateDevice(device_metadata, [this](DiscoveryAction action) { - DiscoveryActionClicked(action); - }); - EXPECT_TRUE(notification_controller_obsesrver_ - ->CheckDeviceMetadataListContainTestDevice(kDeviceName)); - EXPECT_EQ(1, notification_controller_obsesrver_->on_update_device_count()); - notification_controller_.OnDiscoveryClicked(DiscoveryAction::kPairToDevice); - EXPECT_EQ(DiscoveryAction::kPairToDevice, discovery_action_); + CountDownLatch on_update_device_latch(1); + CountDownLatch on_click_latch(1); + FakeFastPairNotificationControllerObserver observer(&on_update_device_latch, + nullptr); + notification_controller.AddObserver(&observer); + EXPECT_EQ(observer.GetDevice(), nullptr); + DiscoveryAction discovery_action = DiscoveryAction::kUnknown; + notification_controller.ShowGuestDiscoveryNotification( + device_metadata, [&](DiscoveryAction action) { + on_click_latch.CountDown(); + discovery_action = action; + }); + on_update_device_latch.Await(); + EXPECT_EQ(observer.GetDevice(), &device_metadata); + notification_controller.OnDiscoveryClicked(DiscoveryAction::kPairToDevice); + on_click_latch.Await(); + EXPECT_EQ(discovery_action, DiscoveryAction::kPairToDevice); +} + +TEST(FastPairNotificationControllerTest, ShowPairingResultNotification) { + FastPairNotificationController notification_controller; + proto::GetObservedDeviceResponse response; + DeviceMetadata device_metadata(response); + + CountDownLatch on_pairing_result_latch(1); + FakeFastPairNotificationControllerObserver observer(nullptr, + &on_pairing_result_latch); + notification_controller.AddObserver(&observer); + EXPECT_FALSE(observer.GetPairingResult().has_value()); + EXPECT_EQ(observer.GetDevice(), nullptr); + notification_controller.ShowPairingResultNotification(device_metadata, true); + on_pairing_result_latch.Await(); + EXPECT_EQ(observer.GetDevice(), &device_metadata); + EXPECT_TRUE(observer.GetPairingResult().has_value()); + EXPECT_TRUE(observer.GetPairingResult().value()); } } // namespace diff --git a/fastpair/ui/fast_pair/fast_pair_presenter.h b/fastpair/ui/fast_pair/fast_pair_presenter.h index 6bd980b4..50da908a 100644 --- a/fastpair/ui/fast_pair/fast_pair_presenter.h +++ b/fastpair/ui/fast_pair/fast_pair_presenter.h @@ -24,11 +24,15 @@ namespace fastpair { // This Presenter creates and manages UI component with Notification Controller. class FastPairPresenter { public: - // observer_list of notification_controller is updated virtual void ShowDiscovery( FastPairDevice& device, FastPairNotificationController& notification_controller, DiscoveryCallback callback) = 0; + virtual void ShowPairingResult( + FastPairDevice& device, + FastPairNotificationController& notification_controller, + bool success) = 0; + virtual ~FastPairPresenter() = default; }; diff --git a/fastpair/ui/fast_pair/fast_pair_presenter_impl.cc b/fastpair/ui/fast_pair/fast_pair_presenter_impl.cc index d242096c..6b9ff70c 100644 --- a/fastpair/ui/fast_pair/fast_pair_presenter_impl.cc +++ b/fastpair/ui/fast_pair/fast_pair_presenter_impl.cc @@ -54,5 +54,12 @@ void FastPairPresenterImpl::ShowDiscovery( notification_controller.ShowGuestDiscoveryNotification(*device.GetMetadata(), std::move(callback)); } + +void FastPairPresenterImpl::ShowPairingResult( + FastPairDevice& device, + FastPairNotificationController& notification_controller, bool success) { + notification_controller.ShowPairingResultNotification(*device.GetMetadata(), + success); +} } // namespace fastpair } // namespace nearby diff --git a/fastpair/ui/fast_pair/fast_pair_presenter_impl.h b/fastpair/ui/fast_pair/fast_pair_presenter_impl.h index cc6595d2..f606658c 100644 --- a/fastpair/ui/fast_pair/fast_pair_presenter_impl.h +++ b/fastpair/ui/fast_pair/fast_pair_presenter_impl.h @@ -48,6 +48,10 @@ class FastPairPresenterImpl : public FastPairPresenter { void ShowDiscovery(FastPairDevice& device, FastPairNotificationController& notification_controller, DiscoveryCallback callback) override; + void ShowPairingResult( + FastPairDevice& device, + FastPairNotificationController& notification_controller, + bool success) override; }; } // namespace fastpair } // namespace nearby diff --git a/fastpair/ui/fast_pair/fast_pair_presenter_impl_test.cc b/fastpair/ui/fast_pair/fast_pair_presenter_impl_test.cc index cb699f4f..221004e5 100644 --- a/fastpair/ui/fast_pair/fast_pair_presenter_impl_test.cc +++ b/fastpair/ui/fast_pair/fast_pair_presenter_impl_test.cc @@ -14,8 +14,8 @@ #include "fastpair/ui/fast_pair/fast_pair_presenter_impl.h" -#include - +#include "gmock/gmock.h" +#include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" #include "fastpair/common/fast_pair_device.h" #include "fastpair/proto/fastpair_rpcs.proto.h" @@ -26,78 +26,88 @@ namespace nearby { namespace fastpair { +const int64_t kDeviceId = 10148625; constexpr absl::string_view kModelId = "000000"; constexpr absl::string_view kAddress = "00:00:00:00:00:00"; constexpr absl::string_view kPublicKey = "test public key"; +constexpr absl::string_view kInitialPairingdescription = + "InitialPairingdescription"; namespace { -TEST(FastPairPresenterImplTest, ShowDiscoveryForV1Version) { - // Setup repository with v1 version device metadata - FastPairDevice device(kModelId, kAddress, Protocol::kFastPairInitialPairing); - proto::GetObservedDeviceResponse response; - DeviceMetadata device_metadata(response); - device.SetMetadata(device_metadata); - - // Register FastPairNotificationControllerObserver - auto latch_1 = std::make_optional(1); - FastPairNotificationController controller; - FakeFastPairNotificationControllerObserver notification_controller_observer( - latch_1); - controller.AddObserver(¬ification_controller_observer); - EXPECT_EQ(notification_controller_observer.on_update_device_count(), 0); - - // FastPairPresenter ShowDiscovery - CountDownLatch latch_2(1); - FastPairPresenterImpl fast_pair_presenter; - DiscoveryAction discovery_action = DiscoveryAction::kUnknown; - fast_pair_presenter.ShowDiscovery(device, controller, - [&](DiscoveryAction action) { - discovery_action = action; - latch_2.CountDown(); - }); - latch_1->Await(); - EXPECT_EQ(notification_controller_observer.on_update_device_count(), 1); - EXPECT_EQ(device.GetVersion(), DeviceFastPairVersion::kV1); - controller.OnDiscoveryClicked(DiscoveryAction::kPairToDevice); - latch_2.Await(); - EXPECT_EQ(discovery_action, DiscoveryAction::kPairToDevice); +// A gMock matcher to match proto values. Use this matcher like: +// request/response proto, expected_proto; +// EXPECT_THAT(proto, MatchesProto(expected_proto)); +MATCHER_P( + MatchesProto, expected_proto, + absl::StrCat(negation ? "does not match" : "matches", + testing::PrintToString(expected_proto.SerializeAsString()))) { + return arg.SerializeAsString() == expected_proto.SerializeAsString(); } -TEST(FastPairPresenterImplTest, ShowDiscoveryForHigherThanV1Version) { - // Setup repository with HigherThanV1Version device metadata - FastPairDevice device(kModelId, kAddress, Protocol::kFastPairInitialPairing); - proto::GetObservedDeviceResponse response; - auto* higher_than_v1_version_device = response.mutable_device(); - higher_than_v1_version_device->mutable_anti_spoofing_key_pair() - ->set_public_key(kPublicKey); - DeviceMetadata device_metadata(response); - device.SetMetadata(device_metadata); +TEST(FastPairPresenterImplTest, ShowDiscovery) { + // Sets up proto::GetObservedDeviceResponse + proto::GetObservedDeviceResponse response_proto; + auto* device = response_proto.mutable_device(); + device->set_id(kDeviceId); + auto* observed_device_strings = response_proto.mutable_strings(); + observed_device_strings->set_initial_pairing_description( + kInitialPairingdescription); + DeviceMetadata device_metadata(response_proto); + FastPairDevice fast_pair_device(kModelId, kAddress, + Protocol::kFastPairInitialPairing); - // Register FastPairNotificationControllerObserver - auto latch_1 = std::make_optional(1); - FastPairNotificationController controller; - FakeFastPairNotificationControllerObserver notification_controller_observer( - latch_1); - controller.AddObserver(¬ification_controller_observer); - EXPECT_EQ(notification_controller_observer.on_update_device_count(), 0); + fast_pair_device.SetMetadata(device_metadata); - // FastPairPresenter ShowDiscovery - CountDownLatch latch_2(1); - FastPairPresenterImpl fast_pair_presenter; + FastPairNotificationController notification_controller; + CountDownLatch on_update_device_latch(1); + CountDownLatch on_click_latch(1); + FakeFastPairNotificationControllerObserver observer(&on_update_device_latch, + nullptr); + notification_controller.AddObserver(&observer); DiscoveryAction discovery_action = DiscoveryAction::kUnknown; - fast_pair_presenter.ShowDiscovery(device, controller, + FastPairPresenterImpl fast_pair_presenter; + fast_pair_presenter.ShowDiscovery(fast_pair_device, notification_controller, [&](DiscoveryAction action) { + on_click_latch.CountDown(); discovery_action = action; - latch_2.CountDown(); }); - latch_1->Await(); - EXPECT_EQ(notification_controller_observer.on_update_device_count(), 1); - EXPECT_EQ(device.GetVersion(), DeviceFastPairVersion::kHigherThanV1); - controller.OnDiscoveryClicked(DiscoveryAction::kDismissedByUser); - latch_2.Await(); - EXPECT_EQ(discovery_action, DiscoveryAction::kDismissedByUser); + on_update_device_latch.Await(); + EXPECT_EQ(observer.GetDevice()->GetFastPairVersion(), + DeviceFastPairVersion::kV1); + EXPECT_THAT(observer.GetDevice()->GetResponse(), + MatchesProto(response_proto)); } +TEST(FastPairPresenterImplTest, ShowPairingResult) { + // Sets up proto::GetObservedDeviceResponse + proto::GetObservedDeviceResponse response_proto; + auto* device = response_proto.mutable_device(); + device->set_id(kDeviceId); + auto* observed_device_strings = response_proto.mutable_strings(); + observed_device_strings->set_initial_pairing_description( + kInitialPairingdescription); + DeviceMetadata device_metadata(response_proto); + FastPairDevice fast_pair_device(kModelId, kAddress, + Protocol::kFastPairInitialPairing); + + fast_pair_device.SetMetadata(device_metadata); + + FastPairNotificationController notification_controller; + CountDownLatch on_pairing_result_latch(1); + FakeFastPairNotificationControllerObserver observer(nullptr, + &on_pairing_result_latch); + notification_controller.AddObserver(&observer); + FastPairPresenterImpl fast_pair_presenter; + fast_pair_presenter.ShowPairingResult(fast_pair_device, + notification_controller, true); + on_pairing_result_latch.Await(); + EXPECT_EQ(observer.GetDevice()->GetFastPairVersion(), + DeviceFastPairVersion::kV1); + EXPECT_THAT(observer.GetDevice()->GetResponse(), + MatchesProto(response_proto)); + EXPECT_TRUE(observer.GetPairingResult().has_value()); + EXPECT_TRUE(observer.GetPairingResult().value()); +} } // namespace } // namespace fastpair } // namespace nearby diff --git a/fastpair/ui/mock_ui_broker.h b/fastpair/ui/mock_ui_broker.h index f0467eae..70241c68 100644 --- a/fastpair/ui/mock_ui_broker.h +++ b/fastpair/ui/mock_ui_broker.h @@ -29,6 +29,10 @@ class MockUIBroker : public UIBroker { MOCK_METHOD(void, ShowDiscovery, (FastPairDevice&, FastPairNotificationController&), (override)); + MOCK_METHOD(void, ShowPairingResult, + (FastPairDevice&, FastPairNotificationController&, bool), + (override)); + void AddObserver(Observer* observer) override { observers_.AddObserver(observer); } diff --git a/fastpair/ui/ui_broker.h b/fastpair/ui/ui_broker.h index 6a414234..a6cfe1f2 100644 --- a/fastpair/ui/ui_broker.h +++ b/fastpair/ui/ui_broker.h @@ -43,6 +43,11 @@ class UIBroker { virtual void ShowDiscovery( FastPairDevice& device, FastPairNotificationController& notification_controller) = 0; + + virtual void ShowPairingResult( + FastPairDevice& device, + FastPairNotificationController& notification_controller, + bool success) = 0; }; } // namespace fastpair diff --git a/fastpair/ui/ui_broker_impl.cc b/fastpair/ui/ui_broker_impl.cc index 027e5db4..6b3c7b3c 100644 --- a/fastpair/ui/ui_broker_impl.cc +++ b/fastpair/ui/ui_broker_impl.cc @@ -60,6 +60,23 @@ void UIBrokerImpl::ShowDiscovery( } } +void UIBrokerImpl::ShowPairingResult( + FastPairDevice& device, + FastPairNotificationController& notification_controller, bool success) { + NEARBY_LOGS(VERBOSE) << __func__; + switch (device.GetProtocol()) { + case Protocol::kFastPairInitialPairing: + case Protocol::kFastPairSubsequentPairing: + fast_pair_presenter_->ShowPairingResult(device, notification_controller, + success); + break; + case Protocol::kFastPairRetroactivePairing: + // In this scenario, we don't show the error UI because it would be + // misleading, since a pair failure is a retroactive pair failure. + break; + } +} + void UIBrokerImpl::NotifyDiscoveryAction(FastPairDevice& device, DiscoveryAction action) { for (auto& observer : observers_.GetObservers()) diff --git a/fastpair/ui/ui_broker_impl.h b/fastpair/ui/ui_broker_impl.h index 3dae1743..039b448b 100644 --- a/fastpair/ui/ui_broker_impl.h +++ b/fastpair/ui/ui_broker_impl.h @@ -37,6 +37,10 @@ class UIBrokerImpl : public UIBroker { void ShowDiscovery( FastPairDevice &device, FastPairNotificationController ¬ification_controller) override; + void ShowPairingResult( + FastPairDevice &device, + FastPairNotificationController ¬ification_controller, + bool success) override; private: void NotifyDiscoveryAction(FastPairDevice &device, DiscoveryAction action); diff --git a/fastpair/ui/ui_broker_impl_test.cc b/fastpair/ui/ui_broker_impl_test.cc index 70600c86..8fe29107 100644 --- a/fastpair/ui/ui_broker_impl_test.cc +++ b/fastpair/ui/ui_broker_impl_test.cc @@ -31,7 +31,7 @@ constexpr absl::string_view kAddress = "74:74:46:01:6C:21"; class UIBrokerImplTest : public ::testing::Test, public UIBroker::Observer { protected: - UIBrokerImplTest() { + void SetUp() override { presenter_factory_ = std::make_unique(); FastPairPresenterImpl::Factory::SetFactoryForTesting( presenter_factory_.get()); @@ -68,6 +68,13 @@ TEST_F(UIBrokerImplTest, ShowDiscoveryWithoutObserver) { EXPECT_FALSE(on_discovery_action_notified_); } +TEST_F(UIBrokerImplTest, ShowPairingResult) { + FastPairDevice device(kModelId, kAddress, Protocol::kFastPairInitialPairing); + ui_broker_->ShowPairingResult(device, notification_controller_, true); + EXPECT_TRUE( + presenter_factory_->fake_fast_pair_presenter()->pairing_result_changed()); +} + } // namespace } // namespace fastpair } // namespace nearby