Add version update in presenter and mock UI Discovery services for further unit test.

PiperOrigin-RevId: 531371755
This commit is contained in:
hai007
2023-05-11 19:42:08 -07:00
committed by Copybara-Service
parent 0bd34a1fdf
commit bd85557710
12 changed files with 188 additions and 45 deletions
+34
View File
@@ -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 = [
@@ -17,8 +17,10 @@
#include <memory>
#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 {
+1 -5
View File
@@ -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 <functional>
#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;
@@ -15,13 +15,12 @@
#include "fastpair/ui/fast_pair/fast_pair_presenter_impl.h"
#include <memory>
#include <optional>
#include <utility>
#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, &notification_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_));
}
@@ -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_;
};
@@ -14,10 +14,6 @@
#include "fastpair/ui/fast_pair/fast_pair_presenter_impl.h"
#include <memory>
#include <optional>
#include <string>
#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(&notification_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
@@ -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<Observer> observers_;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_UI_FAST_PAIR_MOCK_FAST_PAIR_NOTIFICATION_CONTROLLER_H_
+53
View File
@@ -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<Observer> observers_;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_UI_MOCK_UI_BROKER_H_
+2 -1
View File
@@ -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;
};
+7 -6
View File
@@ -15,6 +15,7 @@
#include "fastpair/ui/ui_broker_impl.h"
#include <memory>
#include <utility>
#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);
+2 -3
View File
@@ -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 &notification_controller) override;
private:
void NotifyDiscoveryAction(const FastPairDevice &device,
DiscoveryAction action);
void NotifyDiscoveryAction(FastPairDevice &device, DiscoveryAction action);
std::unique_ptr<FastPairPresenter> fast_pair_presenter_;
ObserverList<Observer> observers_;
+2 -5
View File
@@ -15,7 +15,6 @@
#include "fastpair/ui/ui_broker_impl.h"
#include <memory>
#include <string>
#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_);
}