Revise unit test for fast pair notification controller and remove redundant fake observer.

PiperOrigin-RevId: 521882831
This commit is contained in:
hai007
2023-04-04 15:33:34 -07:00
committed by Copybara-Service
parent d01bbcc2cd
commit 0575b3164b
@@ -14,16 +14,16 @@
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
#include <memory>
#include <vector>
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
#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"
#include "fastpair/ui/fast_pair/fake_fast_pair_notification_controller_observer.h"
namespace nearby {
namespace fastpair {
@@ -33,64 +33,29 @@ const int64_t kDeviceId = 10148625;
const char kModelId[] = "9adb11";
const char kDeviceName[] = "Pixel Buds Pro";
class FastPairNotificationControllerObserver
: public FastPairNotificationController::Observer {
public:
void OnUpdateDevice(const 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());
FastPairNotificationControllerTest() {
notification_controller_.AddObserver(&notification_controller_obsesrver_);
}
void TearDown() override { env_.Stop(); }
void TriggerOnUpdateDevice(DeviceMetadata& device) {
notification_controller_->ShowGuestDiscoveryNotification(device);
notification_controller_.ShowGuestDiscoveryNotification(device);
}
MediumEnvironment& env_{MediumEnvironment::Instance()};
std::shared_ptr<FastPairNotificationController> notification_controller_;
std::unique_ptr<FastPairNotificationControllerObserver>
notification_controller_obsesrver_;
FastPairNotificationController notification_controller_;
FakeFastPairNotificationControllerObserver 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();
.CheckDeviceMetadataListContainTestDevice(kDeviceName));
EXPECT_EQ(1, notification_controller_obsesrver_.on_update_device_count());
}
} // namespace