From bd855577103c0afe944c1419ea64f01ad204bf15 Mon Sep 17 00:00:00 2001 From: hai007 Date: Thu, 11 May 2023 19:40:40 -0700 Subject: [PATCH] Add version update in presenter and mock UI Discovery services for further unit test. PiperOrigin-RevId: 531371755 --- fastpair/ui/BUILD | 34 ++++++++++++ .../ui/fast_pair/fake_fast_pair_presenter.h | 10 +++- fastpair/ui/fast_pair/fast_pair_presenter.h | 6 +-- .../ui/fast_pair/fast_pair_presenter_impl.cc | 13 +++-- .../ui/fast_pair/fast_pair_presenter_impl.h | 5 +- .../fast_pair_presenter_impl_test.cc | 39 +++++++++----- .../mock_fast_pair_notification_controller.h | 45 ++++++++++++++++ fastpair/ui/mock_ui_broker.h | 53 +++++++++++++++++++ fastpair/ui/ui_broker.h | 3 +- fastpair/ui/ui_broker_impl.cc | 13 ++--- fastpair/ui/ui_broker_impl.h | 5 +- fastpair/ui/ui_broker_impl_test.cc | 7 +-- 12 files changed, 188 insertions(+), 45 deletions(-) create mode 100644 fastpair/ui/fast_pair/mock_fast_pair_notification_controller.h create mode 100644 fastpair/ui/mock_ui_broker.h diff --git a/fastpair/ui/BUILD b/fastpair/ui/BUILD index 754f8eb9..58b72050 100644 --- a/fastpair/ui/BUILD +++ b/fastpair/ui/BUILD @@ -1,3 +1,17 @@ +# 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( @@ -32,6 +46,7 @@ cc_library( cc_library( name = "fake_fast_pair_ui", + testonly = True, hdrs = [ "fast_pair/fake_fast_pair_notification_controller_observer.h", "fast_pair/fake_fast_pair_presenter.h", @@ -41,10 +56,29 @@ cc_library( ], deps = [ ":fast_pair_ui", + "//fastpair/common", "//fastpair/repository", ], ) +cc_library( + name = "mock_fast_pair_ui", + hdrs = [ + "fast_pair/mock_fast_pair_notification_controller.h", + "mock_ui_broker.h", + ], + visibility = [ + "//fastpair:__subpackages__", + ], + deps = [ + ":fast_pair_ui", + "//fastpair/common", + "//fastpair/repository", + "//internal/base", + "//testing/base/public:gunit_for_library", + ], +) + cc_test( name = "fast_pair_ui_test", srcs = [ diff --git a/fastpair/ui/fast_pair/fake_fast_pair_presenter.h b/fastpair/ui/fast_pair/fake_fast_pair_presenter.h index c5ee2038..b5a1c89e 100644 --- a/fastpair/ui/fast_pair/fake_fast_pair_presenter.h +++ b/fastpair/ui/fast_pair/fake_fast_pair_presenter.h @@ -17,8 +17,10 @@ #include +#include "fastpair/common/fast_pair_device.h" #include "fastpair/ui/actions.h" #include "fastpair/ui/fast_pair/fast_pair_notification_controller.h" +#include "fastpair/ui/fast_pair/fast_pair_presenter.h" #include "fastpair/ui/fast_pair/fast_pair_presenter_impl.h" namespace nearby { @@ -26,16 +28,20 @@ namespace fastpair { class FakeFastPairPresenter : public FastPairPresenter { public: - void ShowDiscovery(const FastPairDevice& device, + void ShowDiscovery(FastPairDevice& device, FastPairNotificationController& notification_controller, DiscoveryCallback callback) override { show_discovery_ = true; + version_changed_ = true; callback(DiscoveryAction::kPairToDevice); } - bool show_deiscovery() { return show_discovery_; } + bool show_discovery() { return show_discovery_; } + + bool version_changed() { return version_changed_; } private: bool show_discovery_ = false; + bool version_changed_ = false; }; class FakeFastPairPresenterFactory : public FastPairPresenterImpl::Factory { diff --git a/fastpair/ui/fast_pair/fast_pair_presenter.h b/fastpair/ui/fast_pair/fast_pair_presenter.h index 8b758531..6bd980b4 100644 --- a/fastpair/ui/fast_pair/fast_pair_presenter.h +++ b/fastpair/ui/fast_pair/fast_pair_presenter.h @@ -15,11 +15,7 @@ #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 - -#include "absl/functional/any_invocable.h" #include "fastpair/common/fast_pair_device.h" -#include "fastpair/ui/actions.h" #include "fastpair/ui/fast_pair/fast_pair_notification_controller.h" namespace nearby { @@ -30,7 +26,7 @@ class FastPairPresenter { public: // observer_list of notification_controller is updated virtual void ShowDiscovery( - const FastPairDevice& device, + FastPairDevice& device, FastPairNotificationController& notification_controller, DiscoveryCallback callback) = 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 78b80f71..e8990454 100644 --- a/fastpair/ui/fast_pair/fast_pair_presenter_impl.cc +++ b/fastpair/ui/fast_pair/fast_pair_presenter_impl.cc @@ -15,13 +15,12 @@ #include "fastpair/ui/fast_pair/fast_pair_presenter_impl.h" #include +#include #include -#include "absl/functional/any_invocable.h" -#include "absl/strings/string_view.h" +#include "fastpair/common/fast_pair_device.h" #include "fastpair/repository/device_metadata.h" #include "fastpair/server_access/fast_pair_repository.h" -#include "fastpair/ui/actions.h" #include "fastpair/ui/fast_pair/fast_pair_notification_controller.h" #include "internal/platform/logging.h" @@ -49,13 +48,12 @@ void FastPairPresenterImpl::Factory::SetFactoryForTesting( FastPairPresenterImpl::Factory::~Factory() = default; void FastPairPresenterImpl::ShowDiscovery( - const FastPairDevice& device, + FastPairDevice& device, FastPairNotificationController& notification_controller, DiscoveryCallback callback) { callback_ = std::move(callback); FastPairRepository::Get()->GetDeviceMetadata( - device.GetModelId(), [&device, ¬ification_controller, - this](const DeviceMetadata& device_metadata) { + device.GetModelId(), [&](const DeviceMetadata& device_metadata) { NEARBY_LOGS(INFO) << __func__ << "Retrieved metadata to notification controller."; OnDiscoveryMetadataRetrieved(device, device_metadata, @@ -64,8 +62,9 @@ void FastPairPresenterImpl::ShowDiscovery( } void FastPairPresenterImpl::OnDiscoveryMetadataRetrieved( - const FastPairDevice& device, const DeviceMetadata& device_metadata, + FastPairDevice& device, const DeviceMetadata& device_metadata, FastPairNotificationController& notification_controller) { + device.set_version(device_metadata.GetFastPairVersion()); notification_controller.ShowGuestDiscoveryNotification(device_metadata, std::move(callback_)); } diff --git a/fastpair/ui/fast_pair/fast_pair_presenter_impl.h b/fastpair/ui/fast_pair/fast_pair_presenter_impl.h index c5ee2d97..dcc561b3 100644 --- a/fastpair/ui/fast_pair/fast_pair_presenter_impl.h +++ b/fastpair/ui/fast_pair/fast_pair_presenter_impl.h @@ -19,7 +19,6 @@ #include "fastpair/common/fast_pair_device.h" #include "fastpair/repository/device_metadata.h" -#include "fastpair/ui/actions.h" #include "fastpair/ui/fast_pair/fast_pair_notification_controller.h" #include "fastpair/ui/fast_pair/fast_pair_presenter.h" @@ -46,14 +45,14 @@ class FastPairPresenterImpl : public FastPairPresenter { FastPairPresenterImpl(const FastPairPresenterImpl&) = delete; FastPairPresenterImpl& operator=(const FastPairPresenterImpl&) = delete; - void ShowDiscovery(const FastPairDevice& device, + void ShowDiscovery(FastPairDevice& device, FastPairNotificationController& notification_controller, DiscoveryCallback callback) override; private: // observer_list of notification_controller is updated void OnDiscoveryMetadataRetrieved( - const FastPairDevice& device, const DeviceMetadata& device_metadata, + FastPairDevice& device, const DeviceMetadata& device_metadata, FastPairNotificationController& notification_controller); DiscoveryCallback callback_; }; 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 9c33a7b8..06cd1b39 100644 --- a/fastpair/ui/fast_pair/fast_pair_presenter_impl_test.cc +++ b/fastpair/ui/fast_pair/fast_pair_presenter_impl_test.cc @@ -14,10 +14,6 @@ #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" @@ -29,16 +25,14 @@ 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"; +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"; namespace { class FastPairPresenterImplTest : public ::testing::Test { public: FastPairPresenterImplTest() { - proto::Device device; - repository_.SetFakeMetadata(kModelId, device); controller_.AddObserver(¬ification_controller_observer_); } @@ -46,16 +40,16 @@ class FastPairPresenterImplTest : public ::testing::Test { protected: DiscoveryAction discovery_action_; - FakeFastPairRepository repository_; FastPairPresenterImpl fast_pair_presenter_; FastPairNotificationController controller_; FakeFastPairNotificationControllerObserver notification_controller_observer_; }; -TEST_F(FastPairPresenterImplTest, ShowDiscovery) { - FastPairDevice device(kModelId.data(), kAddress.data(), - Protocol::kFastPairInitialPairing); +TEST_F(FastPairPresenterImplTest, ShowDiscoveryForV1Version) { + proto::Device v1_version_device; + repository_.SetFakeMetadata(kModelId, v1_version_device); + FastPairDevice device(kModelId, kAddress, Protocol::kFastPairInitialPairing); EXPECT_EQ(0, notification_controller_observer_.on_update_device_count()); fast_pair_presenter_.ShowDiscovery( @@ -64,7 +58,26 @@ TEST_F(FastPairPresenterImplTest, ShowDiscovery) { EXPECT_EQ(1, notification_controller_observer_.on_update_device_count()); controller_.OnDiscoveryClicked(DiscoveryAction::kPairToDevice); EXPECT_EQ(DiscoveryAction::kPairToDevice, discovery_action_); + EXPECT_EQ(DeviceFastPairVersion::kV1, device.version()); } + +TEST_F(FastPairPresenterImplTest, ShowDiscoveryForHigherThanV1Version) { + proto::Device higher_than_v1_version_device; + higher_than_v1_version_device.mutable_anti_spoofing_key_pair() + ->set_public_key(kPublicKey); + repository_.SetFakeMetadata(kModelId, higher_than_v1_version_device); + FastPairDevice device(kModelId, kAddress, Protocol::kFastPairInitialPairing); + + EXPECT_EQ(0, notification_controller_observer_.on_update_device_count()); + fast_pair_presenter_.ShowDiscovery( + device, controller_, + [this](DiscoveryAction action) { OnDiscoveryAction(action); }); + EXPECT_EQ(1, notification_controller_observer_.on_update_device_count()); + controller_.OnDiscoveryClicked(DiscoveryAction::kPairToDevice); + EXPECT_EQ(DiscoveryAction::kPairToDevice, discovery_action_); + EXPECT_EQ(DeviceFastPairVersion::kHigherThanV1, device.version()); +} + } // namespace } // namespace fastpair } // namespace nearby diff --git a/fastpair/ui/fast_pair/mock_fast_pair_notification_controller.h b/fastpair/ui/fast_pair/mock_fast_pair_notification_controller.h new file mode 100644 index 00000000..64a6249b --- /dev/null +++ b/fastpair/ui/fast_pair/mock_fast_pair_notification_controller.h @@ -0,0 +1,45 @@ +// 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_MOCK_FAST_PAIR_NOTIFICATION_CONTROLLER_H_ +#define THIRD_PARTY_NEARBY_FASTPAIR_UI_FAST_PAIR_MOCK_FAST_PAIR_NOTIFICATION_CONTROLLER_H_ + +#include "gmock/gmock.h" +#include "fastpair/repository/device_metadata.h" +#include "fastpair/ui/actions.h" +#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h" + +namespace nearby { +namespace fastpair { +class MockFastPairNotificationController + : public FastPairNotificationController { + public: + MOCK_METHOD(void, ShowGuestDiscoveryNotification, + (const DeviceMetadata&, DiscoveryCallback)); + MOCK_METHOD(void, OnDiscoveryClicked, (DiscoveryAction)); + MOCK_METHOD(void, AddObserver, (Observer*)); + MOCK_METHOD(void, RemoveObserver, (Observer*)); + + void NotifyShowDiscovery(const DeviceMetadata& device) { + for (Observer* observer : observers_.GetObservers()) { + observer->OnUpdateDevice(device); + } + } + + private: + ObserverList observers_; +}; +} // namespace fastpair +} // namespace nearby +#endif // THIRD_PARTY_NEARBY_FASTPAIR_UI_FAST_PAIR_MOCK_FAST_PAIR_NOTIFICATION_CONTROLLER_H_ diff --git a/fastpair/ui/mock_ui_broker.h b/fastpair/ui/mock_ui_broker.h new file mode 100644 index 00000000..9e0aa60e --- /dev/null +++ b/fastpair/ui/mock_ui_broker.h @@ -0,0 +1,53 @@ +// 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_MOCK_UI_BROKER_H_ +#define THIRD_PARTY_NEARBY_FASTPAIR_UI_MOCK_UI_BROKER_H_ + +#include "gmock/gmock.h" +#include "fastpair/common/fast_pair_device.h" +#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h" +#include "fastpair/ui/ui_broker.h" +#include "internal/base/observer_list.h" + +namespace nearby { +namespace fastpair { + +class MockUIBroker : public UIBroker { + public: + MOCK_METHOD(void, ShowDiscovery, + (FastPairDevice&, FastPairNotificationController&), (override)); + + void AddObserver(Observer* observer) override { + observers_.AddObserver(observer); + } + + void RemoveObserver(Observer* observer) override { + observers_.RemoveObserver(observer); + } + + void NotifyDiscoveryAction(const FastPairDevice& device, + DiscoveryAction action) { + for (auto& observer : observers_.GetObservers()) + observer->OnDiscoveryAction(device, action); + } + + private: + ObserverList observers_; +}; + +} // namespace fastpair +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_FASTPAIR_UI_MOCK_UI_BROKER_H_ diff --git a/fastpair/ui/ui_broker.h b/fastpair/ui/ui_broker.h index 046bceea..405a6b2d 100644 --- a/fastpair/ui/ui_broker.h +++ b/fastpair/ui/ui_broker.h @@ -16,6 +16,7 @@ #define THIRD_PARTY_NEARBY_FASTPAIR_UI_UI_BROKER_H_ #include "fastpair/common/fast_pair_device.h" +#include "fastpair/common/protocol.h" #include "fastpair/ui/actions.h" #include "fastpair/ui/fast_pair/fast_pair_notification_controller.h" @@ -40,7 +41,7 @@ class UIBroker { virtual void RemoveObserver(Observer* observer) = 0; virtual void ShowDiscovery( - const FastPairDevice& device, + FastPairDevice& device, FastPairNotificationController& notification_controller) = 0; }; diff --git a/fastpair/ui/ui_broker_impl.cc b/fastpair/ui/ui_broker_impl.cc index 41e33c2e..027e5db4 100644 --- a/fastpair/ui/ui_broker_impl.cc +++ b/fastpair/ui/ui_broker_impl.cc @@ -15,6 +15,7 @@ #include "fastpair/ui/ui_broker_impl.h" #include +#include #include "fastpair/common/fast_pair_device.h" #include "fastpair/common/protocol.h" @@ -38,16 +39,16 @@ void UIBrokerImpl::RemoveObserver(Observer* observer) { } void UIBrokerImpl::ShowDiscovery( - const FastPairDevice& device, + FastPairDevice& device, FastPairNotificationController& notification_controller) { switch (device.GetProtocol()) { case Protocol::kFastPairInitialPairing: case Protocol::kFastPairSubsequentPairing: fast_pair_presenter_->ShowDiscovery( - device, notification_controller, - [&device, this](DiscoveryAction action) { - NEARBY_LOGS(INFO) - << __func__ << ": Notify discovery action to all observers."; + device, notification_controller, [&](DiscoveryAction action) { + NEARBY_LOGS(VERBOSE) + << __func__ + << "ui broker notify discovery action of device: " << device; NotifyDiscoveryAction(device, action); }); break; @@ -59,7 +60,7 @@ void UIBrokerImpl::ShowDiscovery( } } -void UIBrokerImpl::NotifyDiscoveryAction(const FastPairDevice& device, +void UIBrokerImpl::NotifyDiscoveryAction(FastPairDevice& device, DiscoveryAction action) { for (auto& observer : observers_.GetObservers()) observer->OnDiscoveryAction(device, action); diff --git a/fastpair/ui/ui_broker_impl.h b/fastpair/ui/ui_broker_impl.h index 75bd7a78..3dae1743 100644 --- a/fastpair/ui/ui_broker_impl.h +++ b/fastpair/ui/ui_broker_impl.h @@ -35,12 +35,11 @@ class UIBrokerImpl : public UIBroker { void AddObserver(Observer *observer) override; void RemoveObserver(Observer *observer) override; void ShowDiscovery( - const FastPairDevice &device, + FastPairDevice &device, FastPairNotificationController ¬ification_controller) override; private: - void NotifyDiscoveryAction(const FastPairDevice &device, - DiscoveryAction action); + void NotifyDiscoveryAction(FastPairDevice &device, DiscoveryAction action); std::unique_ptr fast_pair_presenter_; ObserverList observers_; diff --git a/fastpair/ui/ui_broker_impl_test.cc b/fastpair/ui/ui_broker_impl_test.cc index b93a74fa..35295bef 100644 --- a/fastpair/ui/ui_broker_impl_test.cc +++ b/fastpair/ui/ui_broker_impl_test.cc @@ -15,7 +15,6 @@ #include "fastpair/ui/ui_broker_impl.h" #include -#include #include "gtest/gtest.h" #include "fastpair/ui/actions.h" @@ -56,8 +55,7 @@ class UIBrokerImplTest : public ::testing::Test, public UIBroker::Observer { TEST_F(UIBrokerImplTest, ShowDiscovery) { FastPairDevice device(kModelId, kAddress, Protocol::kFastPairInitialPairing); ui_broker_->ShowDiscovery(device, notification_controller_); - EXPECT_TRUE( - presenter_factory_->fake_fast_pair_presenter()->show_deiscovery()); + EXPECT_TRUE(presenter_factory_->fake_fast_pair_presenter()->show_discovery()); EXPECT_TRUE(on_discovery_action_notified_); EXPECT_EQ(DiscoveryAction::kPairToDevice, discovery_action_); } @@ -66,8 +64,7 @@ TEST_F(UIBrokerImplTest, ShowDiscoveryWithoutObserver) { FastPairDevice device(kModelId, kAddress, Protocol::kFastPairInitialPairing); ui_broker_->RemoveObserver(this); ui_broker_->ShowDiscovery(device, notification_controller_); - EXPECT_TRUE( - presenter_factory_->fake_fast_pair_presenter()->show_deiscovery()); + EXPECT_TRUE(presenter_factory_->fake_fast_pair_presenter()->show_discovery()); EXPECT_FALSE(on_discovery_action_notified_); }