From e64dcb40c81fc301d8595317960e6bfcfa0cf268 Mon Sep 17 00:00:00 2001 From: hai007 Date: Fri, 24 Mar 2023 14:21:28 -0700 Subject: [PATCH] Implement Fast Pair presenter for creating and managing UI component with Notification Controller. PiperOrigin-RevId: 519237240 --- fastpair/repository/device_metadata.h | 6 +- ...st_pair_notification_controller_observer.h | 2 +- .../fast_pair_notification_controller.cc | 8 +-- .../fast_pair_notification_controller.h | 6 +- .../fast_pair_notification_controller_test.cc | 2 +- fastpair/ui/fast_pair/fast_pair_presenter.h | 37 +++++++++++ .../ui/fast_pair/fast_pair_presenter_impl.cc | 48 ++++++++++++++ .../ui/fast_pair/fast_pair_presenter_impl.h | 48 ++++++++++++++ .../fast_pair_presenter_impl_test.cc | 62 +++++++++++++++++++ 9 files changed, 208 insertions(+), 11 deletions(-) create mode 100644 fastpair/ui/fast_pair/fast_pair_presenter.h create mode 100644 fastpair/ui/fast_pair/fast_pair_presenter_impl.cc create mode 100644 fastpair/ui/fast_pair/fast_pair_presenter_impl.h create mode 100644 fastpair/ui/fast_pair/fast_pair_presenter_impl_test.cc diff --git a/fastpair/repository/device_metadata.h b/fastpair/repository/device_metadata.h index 3bf4f623..92f2afd6 100644 --- a/fastpair/repository/device_metadata.h +++ b/fastpair/repository/device_metadata.h @@ -30,8 +30,10 @@ class DeviceMetadata { DeviceMetadata &operator=(const DeviceMetadata &) = delete; DeviceMetadata &operator=(DeviceMetadata &&) = delete; ~DeviceMetadata() = default; - const proto::Device &GetDetails() { return response_.device(); } - const proto::GetObservedDeviceResponse &GetResponse() { return response_; } + const proto::Device &GetDetails() const { return response_.device(); } + const proto::GetObservedDeviceResponse &GetResponse() const { + return response_; + } private: const proto::GetObservedDeviceResponse response_; 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 3eebfdb7..fdbb63e9 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 @@ -27,7 +27,7 @@ namespace fastpair { class FakeFastPairNotificationControllerObserver : public FastPairNotificationController::Observer { public: - void OnUpdateDevice(DeviceMetadata& device) override { + void OnUpdateDevice(const DeviceMetadata& device) override { device_metadata_name_list_.push_back(device.GetDetails().name()); on_update_device_count_++; } diff --git a/fastpair/ui/fast_pair/fast_pair_notification_controller.cc b/fastpair/ui/fast_pair/fast_pair_notification_controller.cc index 39778816..b9851496 100644 --- a/fastpair/ui/fast_pair/fast_pair_notification_controller.cc +++ b/fastpair/ui/fast_pair/fast_pair_notification_controller.cc @@ -26,16 +26,16 @@ void FastPairNotificationController::RemoveObserver(Observer* observer) { } void FastPairNotificationController::NotifyShowDiscovery( - DeviceMetadata& device) { + const DeviceMetadata& device) { for (Observer* observer : observers_) { observer->OnUpdateDevice(device); } } void FastPairNotificationController::ShowGuestDiscoveryNotification( - DeviceMetadata& device) { - NotifyShowDiscovery(device); - } + const DeviceMetadata& device) { + NotifyShowDiscovery(device); +} } // namespace fastpair } // namespace nearby diff --git a/fastpair/ui/fast_pair/fast_pair_notification_controller.h b/fastpair/ui/fast_pair/fast_pair_notification_controller.h index 73db461f..7095aa17 100644 --- a/fastpair/ui/fast_pair/fast_pair_notification_controller.h +++ b/fastpair/ui/fast_pair/fast_pair_notification_controller.h @@ -36,7 +36,7 @@ class FastPairNotificationController { class Observer { public: virtual ~Observer() = default; - virtual void OnUpdateDevice(DeviceMetadata& device) = 0; + virtual void OnUpdateDevice(const DeviceMetadata& device) = 0; }; FastPairNotificationController() = default; @@ -49,10 +49,10 @@ class FastPairNotificationController { // Observer process void AddObserver(Observer* observer); void RemoveObserver(Observer* observer); - void NotifyShowDiscovery(DeviceMetadata& device); + void NotifyShowDiscovery(const DeviceMetadata& device); // Creates and displays corresponding notification. - void ShowGuestDiscoveryNotification(DeviceMetadata& device_metadata); + void ShowGuestDiscoveryNotification(const DeviceMetadata& device_metadata); private: ObserverList observers_; 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 060b6707..86a40579 100644 --- a/fastpair/ui/fast_pair/fast_pair_notification_controller_test.cc +++ b/fastpair/ui/fast_pair/fast_pair_notification_controller_test.cc @@ -36,7 +36,7 @@ const char kDeviceName[] = "Pixel Buds Pro"; class FastPairNotificationControllerObserver : public FastPairNotificationController::Observer { public: - void OnUpdateDevice(DeviceMetadata& device) override { + void OnUpdateDevice(const DeviceMetadata& device) override { device_metadata_name_list_.push_back(device.GetDetails().name()); on_update_device_count_++; } diff --git a/fastpair/ui/fast_pair/fast_pair_presenter.h b/fastpair/ui/fast_pair/fast_pair_presenter.h new file mode 100644 index 00000000..93313f7e --- /dev/null +++ b/fastpair/ui/fast_pair/fast_pair_presenter.h @@ -0,0 +1,37 @@ +// 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_UI_FAST_PAIR_FAST_PAIR_PRESENTER_H_ +#define THIRD_PARTY_NEARBY_FASTPAIR_UI_FAST_PAIR_FAST_PAIR_PRESENTER_H_ + +#include "fastpair/common/fast_pair_device.h" +#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h" + +namespace nearby { +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( + const FastPairDevice& device, + FastPairNotificationController& notification_controller) = 0; + virtual ~FastPairPresenter() = default; +}; + +} // namespace fastpair +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_FASTPAIR_UI_FAST_PAIR_FAST_PAIR_PRESENTER_H_ diff --git a/fastpair/ui/fast_pair/fast_pair_presenter_impl.cc b/fastpair/ui/fast_pair/fast_pair_presenter_impl.cc new file mode 100644 index 00000000..511919c0 --- /dev/null +++ b/fastpair/ui/fast_pair/fast_pair_presenter_impl.cc @@ -0,0 +1,48 @@ +// 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/ui/fast_pair/fast_pair_presenter_impl.h" + +#include + +#include "absl/strings/string_view.h" +#include "fastpair/repository/device_metadata.h" +#include "fastpair/server_access/fast_pair_repository.h" +#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h" +#include "internal/platform/logging.h" + +namespace nearby { +namespace fastpair { + +void FastPairPresenterImpl::ShowDiscovery( + const FastPairDevice& device, + FastPairNotificationController& notification_controller) { + FastPairRepository::Get()->GetDeviceMetadata( + device.model_id, + [¬ification_controller, this](const DeviceMetadata& device_metadata) { + NEARBY_LOGS(INFO) << __func__ + << "Retrieved metadata to notification controller."; + FastPairPresenterImpl::OnDiscoveryMetadataRetrieved( + device_metadata, notification_controller); + }); +} + +void FastPairPresenterImpl::OnDiscoveryMetadataRetrieved( + const DeviceMetadata& device_metadata, + FastPairNotificationController& notification_controller) { + notification_controller.ShowGuestDiscoveryNotification(device_metadata); +} + +} // 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 new file mode 100644 index 00000000..1ee63c88 --- /dev/null +++ b/fastpair/ui/fast_pair/fast_pair_presenter_impl.h @@ -0,0 +1,48 @@ +// 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_UI_FAST_PAIR_FAST_PAIR_PRESENTER_IMPL_H_ +#define THIRD_PARTY_NEARBY_FASTPAIR_UI_FAST_PAIR_FAST_PAIR_PRESENTER_IMPL_H_ + +#include + +#include "fastpair/common/fast_pair_device.h" +#include "fastpair/repository/device_metadata.h" +#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h" +#include "fastpair/ui/fast_pair/fast_pair_presenter.h" + +namespace nearby { +namespace fastpair { + +class FastPairPresenterImpl : public FastPairPresenter { + public: + FastPairPresenterImpl() = default; + FastPairPresenterImpl(const FastPairPresenterImpl&) = delete; + FastPairPresenterImpl& operator=(const FastPairPresenterImpl&) = delete; + ~FastPairPresenterImpl() override = default; + + void ShowDiscovery( + const FastPairDevice& device, + FastPairNotificationController& notification_controller) override; + + private: + // observer_list of notification_controller is updated + void OnDiscoveryMetadataRetrieved( + const DeviceMetadata& device_metadata, + FastPairNotificationController& notification_controller); +}; +} // namespace fastpair +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_FASTPAIR_UI_FAST_PAIR_FAST_PAIR_PRESENTER_IMPL_H_ diff --git a/fastpair/ui/fast_pair/fast_pair_presenter_impl_test.cc b/fastpair/ui/fast_pair/fast_pair_presenter_impl_test.cc new file mode 100644 index 00000000..6929643e --- /dev/null +++ b/fastpair/ui/fast_pair/fast_pair_presenter_impl_test.cc @@ -0,0 +1,62 @@ +// 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/ui/fast_pair/fast_pair_presenter_impl.h" + +#include +#include +#include + +#include "gtest/gtest.h" +#include "fastpair/common/fast_pair_device.h" +#include "fastpair/proto/fastpair_rpcs.proto.h" +#include "fastpair/server_access/fake_fast_pair_repository.h" +#include "fastpair/ui/fast_pair/fake_fast_pair_notification_controller_observer.h" +#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h" +#include "fastpair/ui/fast_pair/fast_pair_presenter.h" + +namespace nearby { +namespace fastpair { + +constexpr absl::string_view kModelId = "718C17"; +constexpr absl::string_view kAddress = "74:74:46:01:6C:21"; +constexpr absl::string_view kDeviceName = "Pixel Buds A-Series"; + +namespace { +class FastPairPresenterImplTest : public ::testing::Test { + public: + FastPairPresenterImplTest() { + proto::Device device; + repository_.SetFakeMetadata(kModelId, device); + controller_.AddObserver(¬ification_controller_observer_); + } + + protected: + FakeFastPairRepository repository_; + FastPairPresenterImpl fast_pair_presenter_; + FastPairNotificationController controller_; + FakeFastPairNotificationControllerObserver notification_controller_observer_; +}; + +TEST_F(FastPairPresenterImplTest, ShowDiscovery) { + FastPairDevice device(kModelId.data(), kAddress.data(), + Protocol::kFastPairInitialPairing); + + EXPECT_EQ(0, notification_controller_observer_.on_update_device_count()); + fast_pair_presenter_.ShowDiscovery(device, controller_); + EXPECT_EQ(1, notification_controller_observer_.on_update_device_count()); +} +} // namespace +} // namespace fastpair +} // namespace nearby