Implement Fast Pair notification controller.

PiperOrigin-RevId: 513067597
This commit is contained in:
hai007
2023-02-28 16:15:29 -08:00
committed by Copybara-Service
parent f9dec58aff
commit 37adaf83c7
3 changed files with 202 additions and 0 deletions
@@ -0,0 +1,41 @@
// 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_notification_controller.h"
#include "fastpair/repository/device_metadata.h"
namespace nearby {
namespace fastpair {
void FastPairNotificationController::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void FastPairNotificationController::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void FastPairNotificationController::NotifyShowDiscovery(
DeviceMetadata& device) {
for (Observer* observer : observers_) {
observer->OnUpdateDevice(device);
}
}
void FastPairNotificationController::ShowGuestDiscoveryNotification(
DeviceMetadata& device) {
NotifyShowDiscovery(device);
}
} // namespace fastpair
} // namespace nearby
@@ -0,0 +1,63 @@
// 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_NOTIFICATION_CONTROLLER_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_UI_FAST_PAIR_FAST_PAIR_NOTIFICATION_CONTROLLER_H_
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
#include "fastpair/repository/device_metadata.h"
#include "internal/base/observer_list.h"
namespace nearby {
namespace fastpair {
using RepeatingClosure = absl::AnyInvocable<void()>;
enum class FastPairNotificationDismissReason {
kDismissedByUser,
kDismissedByOs,
kDismissedByTimeout,
};
// This controller creates and manages messages for each FastPair corresponding
// notification event.
class FastPairNotificationController {
public:
class Observer {
public:
virtual ~Observer() = default;
virtual void OnUpdateDevice(DeviceMetadata& device) = 0;
};
FastPairNotificationController() = default;
~FastPairNotificationController() = default;
FastPairNotificationController(const FastPairNotificationController&) =
delete;
FastPairNotificationController& operator=(
const FastPairNotificationController&) = delete;
// Observer process
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
void NotifyShowDiscovery(DeviceMetadata& device);
// Creates and displays corresponding notification.
void ShowGuestDiscoveryNotification(DeviceMetadata& device_metadata);
private:
ObserverList<Observer> observers_;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_UI_FAST_PAIR_FAST_PAIR_NOTIFICATION_CONTROLLER_H_
@@ -0,0 +1,98 @@
// 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_notification_controller.h"
#include <memory>
#include <vector>
#include <algorithm>
#include <string>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "fastpair/repository/device_metadata.h"
#include "internal/platform/medium_environment.h"
namespace nearby {
namespace fastpair {
namespace {
const int64_t kDeviceId = 10148625;
const char kModelId[] = "9adb11";
const char kDeviceName[] = "Pixel Buds Pro";
class FastPairNotificationControllerObserver
: public FastPairNotificationController::Observer {
public:
void OnUpdateDevice(DeviceMetadata& device) override {
device_metadata_name_list_.push_back(device.GetDetails().name());
on_update_device_count_++;
}
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();
}
int on_update_device_count() { return on_update_device_count_; }
private:
std::vector<std::string> device_metadata_name_list_;
int on_update_device_count_ = 0;
};
class FastPairNotificationControllerTest : public ::testing::Test {
protected:
FastPairNotificationControllerTest() {}
~FastPairNotificationControllerTest() override = default;
void SetUp() override {
notification_controller_ =
std::make_shared<FastPairNotificationController>();
notification_controller_obsesrver_ =
std::make_unique<FastPairNotificationControllerObserver>();
notification_controller_->AddObserver(
notification_controller_obsesrver_.get());
}
void TearDown() override { env_.Stop(); }
void TriggerOnUpdateDevice(DeviceMetadata& device) {
notification_controller_->ShowGuestDiscoveryNotification(device);
}
MediumEnvironment& env_{MediumEnvironment::Instance()};
std::shared_ptr<FastPairNotificationController> notification_controller_;
std::unique_ptr<FastPairNotificationControllerObserver>
notification_controller_obsesrver_;
};
TEST_F(FastPairNotificationControllerTest, ShowGuestDiscoveryNotification) {
env_.Start();
proto::GetObservedDeviceResponse response;
response.mutable_device()->set_id(kDeviceId);
response.mutable_device()->set_name(kDeviceName);
DeviceMetadata device_metadata(response);
TriggerOnUpdateDevice(device_metadata);
EXPECT_TRUE(notification_controller_obsesrver_
->CheckDeviceMetadataListContainTestDevice(kDeviceName));
EXPECT_EQ(1, notification_controller_obsesrver_->on_update_device_count());
env_.Stop();
}
} // namespace
} // namespace fastpair
} // namespace nearby