Implement Fast Pair presenter for creating and managing UI component with Notification Controller.

PiperOrigin-RevId: 519237240
This commit is contained in:
hai007
2023-03-24 14:23:32 -07:00
committed by Copybara-Service
parent 4ff3464679
commit e64dcb40c8
9 changed files with 208 additions and 11 deletions
+4 -2
View File
@@ -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_;
@@ -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_++;
}
@@ -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
@@ -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<Observer> observers_;
@@ -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_++;
}
@@ -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_
@@ -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 <memory>
#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,
[&notification_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
@@ -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 <memory>
#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_
@@ -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 <memory>
#include <optional>
#include <string>
#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(&notification_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