Refactor unit test for fast pair scanner and add mock_scanner_broker

PiperOrigin-RevId: 520386239
This commit is contained in:
Qin Wang
2023-03-29 11:06:04 -07:00
committed by Copybara-Service
parent 64ff0ee034
commit 564bca0a85
14 changed files with 141 additions and 85 deletions
+2 -1
View File
@@ -29,7 +29,8 @@ cc_library(
"//fastpair:__subpackages__",
],
deps = [
"//fastpair/scanning/fastpair:scanning",
"//fastpair/common",
"//fastpair/scanning:scanner",
"//internal/platform:logging",
"//internal/platform/implementation:types",
],
+5 -3
View File
@@ -16,8 +16,10 @@
#include <memory>
#include "fastpair/scanning/fastpair/fast_pair_scanner_impl.h"
#include "fastpair/scanning/scanner_broker_impl.h"
#include "internal/platform/logging.h"
#include "fastpair/common/protocol.h"
namespace nearby {
namespace fastpair {
@@ -28,13 +30,13 @@ FastPairWrapperImpl::FastPairWrapperImpl() {
FastPairWrapperImpl::~FastPairWrapperImpl() = default;
void FastPairWrapperImpl::StartScan() {
scanner_ = std::make_unique<FastPairScannerImpl>();
scanner_broker_ = std::make_unique<ScannerBrokerImpl>();
if (is_scanning_) {
NEARBY_LOGS(VERBOSE) << __func__ << ": We're currently scanning. ";
return;
}
is_scanning_ = true;
scanner_->StartScanning();
scanner_broker_->StartScanning(Protocol::kFastPairInitialPairing);
}
bool FastPairWrapperImpl::IsScanning() { return is_scanning_; }
+3 -3
View File
@@ -19,14 +19,14 @@
#include <memory>
#include "fastpair/dart/fast_pair_wrapper.h"
#include "fastpair/scanning/fastpair/fast_pair_scanner_impl.h"
#include "fastpair/scanning/scanner_broker.h"
namespace nearby {
namespace fastpair {
class FastPairScannerImpl;
class FastPairWrapperImpl : public FastPairWrapper, public FastPairScannerImpl {
class FastPairWrapperImpl : public FastPairWrapper {
public:
explicit FastPairWrapperImpl();
~FastPairWrapperImpl() override;
@@ -37,7 +37,7 @@ class FastPairWrapperImpl : public FastPairWrapper, public FastPairScannerImpl {
void StartScan() override;
private:
std::unique_ptr<FastPairScannerImpl> scanner_;
std::unique_ptr<ScannerBroker> scanner_broker_;
// True if we are currently scanning for remote devices.
bool is_scanning_ = false;
+19
View File
@@ -41,6 +41,23 @@ cc_library(
],
)
cc_library(
name = "mocks",
testonly = 1,
hdrs = [
"mock_scanner_broker.h",
],
visibility = [
"//fastpair:__subpackages__",
],
deps = [
":scanner",
"//fastpair/common",
"//internal/base",
"@com_google_googletest//:gtest_for_library_testonly",
],
)
cc_test(
name = "scanner_broker_impl_test",
size = "small",
@@ -54,8 +71,10 @@ cc_test(
"//fastpair/scanning/fastpair:scanning",
"//fastpair/scanning/fastpair:test_support",
"//internal/platform:test_util",
"//internal/platform/implementation:types",
"//internal/platform/implementation/g3", # build_cleaner: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest_main",
],
+1
View File
@@ -99,6 +99,7 @@ cc_test(
shard_count = 16,
deps = [
":scanning",
":test_support",
"//fastpair/dataparser",
"//fastpair/server_access:test_support",
"//fastpair/testing",
@@ -26,6 +26,7 @@ class FakeFastPairScanner final : public FastPairScanner {
FakeFastPairScanner() = default;
FakeFastPairScanner(const FakeFastPairScanner&) = delete;
FakeFastPairScanner& operator=(const FakeFastPairScanner&) = delete;
~FakeFastPairScanner() override = default;
void AddObserver(Observer* observer) override;
void RemoveObserver(Observer* observer) override;
@@ -33,8 +34,6 @@ class FakeFastPairScanner final : public FastPairScanner {
void NotifyDeviceLost(const BlePeripheral& peripheral);
private:
~FakeFastPairScanner() override = default;
ObserverList<FastPairScanner::Observer> observer_;
};
@@ -23,11 +23,10 @@
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/synchronization/notification.h"
#include "fastpair/scanning/fastpair/fast_pair_scanner_impl.h"
#include "fastpair/scanning/fastpair/fake_fast_pair_scanner.h"
#include "fastpair/server_access/fake_fast_pair_repository.h"
#include "fastpair/testing/fast_pair_service_data_creator.h"
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/medium_environment.h"
namespace nearby {
namespace fastpair {
@@ -79,10 +78,16 @@ class FastPairDiscoverableScannerImplTest : public testing::Test {
public:
void SetUp() override {
SetUpMetadata();
scanner_ = std::make_shared<FastPairScannerImpl>();
scanner_ = std::make_shared<FakeFastPairScanner>();
adapter_ = std::make_shared<BluetoothAdapter>();
}
void TearDown() override {
scanner_.reset();
adapter_.reset();
repository_.reset();
}
void SetUpMetadata() {
repository_ = std::make_unique<FakeFastPairRepository>();
proto::Device metadata;
@@ -93,7 +98,7 @@ class FastPairDiscoverableScannerImplTest : public testing::Test {
// void TearDown() override { discoverable_scanner_.reset(); }
protected:
std::shared_ptr<FastPairScannerImpl> scanner_;
std::shared_ptr<FakeFastPairScanner> scanner_;
std::unique_ptr<FakeFastPairRepository> repository_;
std::shared_ptr<BluetoothAdapter> adapter_;
DeviceCallback found_device_callback_;
@@ -117,9 +122,9 @@ TEST_F(FastPairDiscoverableScannerImplTest, ValidModelId) {
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
scanner_->OnDeviceFound(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
EXPECT_TRUE(found_notification.WaitForNotificationWithTimeout(kWaitTimeout));
scanner_->OnDeviceLost(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
EXPECT_TRUE(lost_notification.WaitForNotificationWithTimeout(kWaitTimeout));
}
@@ -136,7 +141,7 @@ TEST_F(FastPairDiscoverableScannerImplTest, InvalidModelId) {
auto ble_peripheral = std::make_unique<FakeBlePeripheral>(
kTestBleDeviceAddress, kInvalidModelId);
scanner_->OnDeviceFound(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(found_notification.WaitForNotificationWithTimeout(kWaitTimeout));
}
@@ -287,7 +292,7 @@ TEST_F(FastPairDiscoverableScannerImplTest, NearbyShareModelId) {
auto ble_peripheral = std::make_unique<FakeBlePeripheral>(
kTestBleDeviceAddress, kNearbyShareModelId);
scanner_->OnDeviceFound(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(found_notification.WaitForNotificationWithTimeout(kWaitTimeout));
}
@@ -309,7 +314,7 @@ TEST_F(FastPairDiscoverableScannerImplTest,
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
scanner_->OnDeviceLost(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(lost_notification.WaitForNotificationWithTimeout(kWaitTimeout));
}
@@ -55,6 +55,7 @@ FastPairScannerImpl::Factory::~Factory() = default;
// FastPairScannerImpl
FastPairScannerImpl::FastPairScannerImpl() {
task_runner_ = std::make_unique<TaskRunnerImpl>(1);
StartScanning();
}
void FastPairScannerImpl::AddObserver(FastPairScanner::Observer* observer) {
@@ -57,9 +57,6 @@ class FastPairScannerImpl : public FastPairScanner {
void AddObserver(FastPairScanner::Observer* observer) override;
void RemoveObserver(FastPairScanner::Observer* observer) override;
void StartScanning();
void StopScanning();
// Fast Pair discovered peripheral callback
void OnDeviceFound(const BlePeripheral& peripheral);
void OnDeviceLost(const BlePeripheral& peripheral);
@@ -72,6 +69,9 @@ class FastPairScannerImpl : public FastPairScanner {
Ble& GetBle() { return ble_; }
private:
void StartScanning();
void StopScanning();
std::unique_ptr<TaskRunner> task_runner_;
// Map of a Bluetooth device address to a set of advertisement data we have
@@ -22,7 +22,6 @@
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "fastpair/common/constant.h"
#include "fastpair/scanning/fastpair/fast_pair_scanner.h"
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/byte_array.h"
@@ -35,6 +34,7 @@ namespace {
// Below constants are used to construct MockBluetoothDevice for testing.
constexpr char kTestBleDeviceAddress[] = "11:12:13:14:15:16";
constexpr char kTestModelId[] = "112233";
constexpr absl::Duration kTaskWaitTimeout = absl::Milliseconds(200);
class FakeBlePeripheral : public api::BlePeripheral {
public:
@@ -98,13 +98,19 @@ class FastPairScannerObserver : public FastPairScanner::Observer {
class FastPairScannerImplTest : public testing::Test {
public:
void SetUp() override {
scanner_.reset();
env_.Start();
scanner_ = std::make_shared<FastPairScannerImpl>();
SystemClock::Sleep(kTaskWaitTimeout);
scanner_observer_ = std::make_unique<FastPairScannerObserver>();
scanner_->AddObserver(scanner_observer_.get());
}
void TearDown() override { env_.Stop(); }
void TearDown() override {
scanner_->RemoveObserver(scanner_observer_.get());
scanner_.reset();
scanner_observer_.reset();
env_.Stop();
}
void TriggerOnDeviceFound(absl::string_view address, absl::string_view data) {
auto ble_peripheral = std::make_unique<FakeBlePeripheral>(address, data);
@@ -122,58 +128,37 @@ class FastPairScannerImplTest : public testing::Test {
std::unique_ptr<FastPairScannerObserver> scanner_observer_;
};
TEST_F(FastPairScannerImplTest, FactoryCreatSuccessfully) {
env_.Start();
std::shared_ptr<FastPairScanner> scanner =
FastPairScannerImpl::Factory::Create();
EXPECT_TRUE(scanner);
scanner.reset();
env_.Stop();
}
TEST_F(FastPairScannerImplTest, StartScanningSuccessfully) {
env_.Start();
scanner_->StartScanning();
SystemClock::Sleep(absl::Milliseconds(200));
EXPECT_TRUE(scanner_->GetBle().IsScanning());
// Not StopScanning as FastPairLowPowerDisabled
env_.Stop();
}
TEST_F(FastPairScannerImplTest, DeviceFoundNotifiesObservers) {
env_.Start();
TriggerOnDeviceFound(kTestBleDeviceAddress, kTestModelId);
EXPECT_TRUE(scanner_observer_->DoesDeviceListContainTestDevice(
kTestBleDeviceAddress));
env_.Stop();
}
TEST_F(FastPairScannerImplTest, DeviceLostNotifiesObservers) {
env_.Start();
TriggerOnDeviceFound(kTestBleDeviceAddress, kTestModelId);
EXPECT_TRUE(scanner_observer_->DoesDeviceListContainTestDevice(
kTestBleDeviceAddress));
TriggerOnDeviceLost(kTestBleDeviceAddress, kTestModelId);
EXPECT_FALSE(scanner_observer_->DoesDeviceListContainTestDevice(
kTestBleDeviceAddress));
env_.Stop();
}
TEST_F(FastPairScannerImplTest, DeviceFoundWithNoServiceData) {
env_.Start();
TEST_F(FastPairScannerImplTest, DeviceFoundWithNoServiceData) {;
TriggerOnDeviceFound(kTestBleDeviceAddress, "");
EXPECT_FALSE(scanner_observer_->DoesDeviceListContainTestDevice(
kTestBleDeviceAddress));
env_.Stop();
}
TEST_F(FastPairScannerImplTest, RemoveObserver) {
env_.Start();
scanner_->RemoveObserver(scanner_observer_.get());
TriggerOnDeviceFound(kTestBleDeviceAddress, kTestModelId);
EXPECT_FALSE(scanner_observer_->DoesDeviceListContainTestDevice(
kTestBleDeviceAddress));
env_.Stop();
}
} // namespace
+58
View File
@@ -0,0 +1,58 @@
// 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_SCANNING_MOCK_SCANNER_BROKER_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_SCANNING_MOCK_SCANNER_BROKER_H_
#include "gmock/gmock.h"
#include "fastpair/scanning/scanner_broker.h"
#include "internal/base/observer_list.h"
#include "fastpair/common/fast_pair_device.h"
namespace nearby {
namespace fastpair {
class MockScannerBroker : public ScannerBroker {
public:
MOCK_METHOD(void, StartScanning, (Protocol), (override));
MOCK_METHOD(void, StopScanning, (Protocol), (override));
void AddObserver(Observer* observer) override {
observers_.AddObserver(observer);
}
void RemoveObserver(Observer* observer) override {
observers_.RemoveObserver(observer);
}
void NotifyDeviceFound(const FastPairDevice& device) {
for (auto& observer : observers_){
observer->OnDeviceFound(device);
}
}
void NotifyDeviceLost(const FastPairDevice& device) {
for (auto& observer : observers_){
observer->OnDeviceLost(device);
}
}
private:
ObserverList<Observer> observers_;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_SCANNING_MOCK_SCANNER_BROKER_H_
+2 -4
View File
@@ -54,10 +54,7 @@ void ScannerBrokerImpl::StartFastPairScanning() {
DCHECK(!fast_pair_discoverable_scanner_);
DCHECK(adapter_);
NEARBY_LOGS(VERBOSE) << "Starting Fast Pair Scanning.";
scanner_impl_ = std::make_shared<FastPairScannerImpl>();
scanner_impl_->StartScanning();
scanner_ = std::move(scanner_impl_);
scanner_ = std::make_shared<FastPairScannerImpl>();
fast_pair_discoverable_scanner_ =
FastPairDiscoverableScannerImpl::Factory::Create(
scanner_, adapter_,
@@ -67,6 +64,7 @@ void ScannerBrokerImpl::StartFastPairScanning() {
void ScannerBrokerImpl::StopFastPairScanning() {
fast_pair_discoverable_scanner_.reset();
scanner_.reset();
observers_.Clear();
NEARBY_LOGS(VERBOSE) << __func__ << "Stopping Fast Pair Scanning.";
}
-2
View File
@@ -22,7 +22,6 @@
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h"
#include "fastpair/scanning/fastpair/fast_pair_scanner.h"
#include "fastpair/scanning/fastpair/fast_pair_scanner_impl.h"
#include "fastpair/scanning/scanner_broker.h"
#include "internal/base/observer_list.h"
#include "internal/platform/bluetooth_adapter.h"
@@ -50,7 +49,6 @@ class ScannerBrokerImpl : public ScannerBroker {
std::unique_ptr<TaskRunner> task_runner_;
std::shared_ptr<FastPairScanner> scanner_;
std::shared_ptr<FastPairScannerImpl> scanner_impl_;
std::shared_ptr<BluetoothAdapter> adapter_;
std::unique_ptr<FastPairDiscoverableScanner> fast_pair_discoverable_scanner_;
ObserverList<Observer> observers_;
+22 -33
View File
@@ -18,9 +18,8 @@
#include <string>
#include <utility>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/common/protocol.h"
#include "fastpair/scanning/fastpair/fake_fast_pair_discoverable_scanner.h"
@@ -29,7 +28,7 @@
#include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h"
#include "fastpair/scanning/fastpair/fast_pair_scanner.h"
#include "fastpair/scanning/fastpair/fast_pair_scanner_impl.h"
#include "internal/platform/medium_environment.h"
#include "internal/platform/implementation/system_clock.h"
namespace nearby {
namespace fastpair {
@@ -37,6 +36,7 @@ namespace {
constexpr absl::string_view kTestDeviceAddress("11:12:13:14:15:16");
constexpr absl::string_view kValidModelId("718c17");
constexpr absl::Duration kTaskWaitTimeout = absl::Milliseconds(200);
class FakeFastPairScannerFactory : public FastPairScannerImpl::Factory {
public:
@@ -74,8 +74,6 @@ class FakeFastPairDiscoverableScannerFactory
return fake_fast_pair_discoverable_scanner;
}
~FakeFastPairDiscoverableScannerFactory() override = default;
FakeFastPairDiscoverableScanner* fake_fast_pair_discoverable_scanner() {
return fake_fast_pair_discoverable_scanner_;
}
@@ -91,8 +89,9 @@ class FakeFastPairDiscoverableScannerFactory
class ScannerBrokerImplTest : public testing::Test,
public ScannerBroker::Observer {
public:
ScannerBrokerImplTest() {
void SetUp() override {
adapter_ = std::shared_ptr<BluetoothAdapter>();
scanner_factory_ = std::make_unique<FakeFastPairScannerFactory>();
FastPairScannerImpl::Factory::SetFactoryForTesting(scanner_factory_.get());
@@ -100,19 +99,21 @@ class ScannerBrokerImplTest : public testing::Test,
std::make_unique<FakeFastPairDiscoverableScannerFactory>();
FastPairDiscoverableScannerImpl::Factory::SetFactoryForTesting(
discoverable_scanner_factory_.get());
}
~ScannerBrokerImplTest() override {
scanner_broker_->RemoveObserver(this);
scanner_broker_.reset();
adapter_.reset();
}
void CreateScannerBroker() {
scanner_broker_ = std::make_unique<ScannerBrokerImpl>();
scanner_broker_->AddObserver(this);
}
void TearDown() override {
scanner_broker_->RemoveObserver(this);
scanner_broker_.reset();
scanner_factory_.reset();
discoverable_scanner_factory_.reset();
FastPairScannerImpl::Factory::SetFactoryForTesting(nullptr);
FastPairDiscoverableScannerImpl::Factory::SetFactoryForTesting(nullptr);
adapter_.reset();
}
void TriggerDiscoverableDeviceFound() {
FastPairDevice device(std::string(kValidModelId),
std::string(kTestDeviceAddress),
@@ -140,7 +141,6 @@ class ScannerBrokerImplTest : public testing::Test,
protected:
bool device_found_ = false;
bool device_lost_ = false;
MediumEnvironment& env_{MediumEnvironment::Instance()};
std::shared_ptr<BluetoothAdapter> adapter_;
std::unique_ptr<FakeFastPairScannerFactory> scanner_factory_;
std::unique_ptr<FakeFastPairDiscoverableScannerFactory>
@@ -149,66 +149,55 @@ class ScannerBrokerImplTest : public testing::Test,
};
TEST_F(ScannerBrokerImplTest, DiscoverableFound) {
env_.Start();
EXPECT_FALSE(discoverable_scanner_factory_->create_instance());
CreateScannerBroker();
scanner_broker_->StartScanning(Protocol::kFastPairInitialPairing);
SystemClock::Sleep(absl::Milliseconds(200));
SystemClock::Sleep(kTaskWaitTimeout);
EXPECT_FALSE(device_found_);
EXPECT_TRUE(discoverable_scanner_factory_->create_instance());
TriggerDiscoverableDeviceFound();
EXPECT_TRUE(device_found_);
env_.Stop();
}
TEST_F(ScannerBrokerImplTest, DiscoverableLost) {
env_.Start();
EXPECT_FALSE(discoverable_scanner_factory_->create_instance());
CreateScannerBroker();
scanner_broker_->StartScanning(Protocol::kFastPairInitialPairing);
SystemClock::Sleep(absl::Milliseconds(200));
SystemClock::Sleep(kTaskWaitTimeout);
EXPECT_FALSE(device_found_);
EXPECT_TRUE(discoverable_scanner_factory_->create_instance());
TriggerDiscoverableDeviceLost();
EXPECT_TRUE(device_lost_);
env_.Stop();
}
TEST_F(ScannerBrokerImplTest, RemoveObserver) {
env_.Start();
EXPECT_FALSE(discoverable_scanner_factory_->create_instance());
CreateScannerBroker();
scanner_broker_->StartScanning(Protocol::kFastPairInitialPairing);
SystemClock::Sleep(absl::Milliseconds(200));
SystemClock::Sleep(kTaskWaitTimeout);
EXPECT_FALSE(device_found_);
EXPECT_TRUE(discoverable_scanner_factory_->create_instance());
scanner_broker_->RemoveObserver(this);
TriggerDiscoverableDeviceLost();
EXPECT_FALSE(device_lost_);
env_.Stop();
}
TEST_F(ScannerBrokerImplTest, StopScanning) {
env_.Start();
CreateScannerBroker();
EXPECT_FALSE(discoverable_scanner_factory_->create_instance());
scanner_broker_->StartScanning(Protocol::kFastPairInitialPairing);
SystemClock::Sleep(absl::Milliseconds(200));
SystemClock::Sleep(kTaskWaitTimeout);
EXPECT_TRUE(discoverable_scanner_factory_->create_instance());
scanner_broker_->StopScanning(Protocol::kFastPairInitialPairing);
SystemClock::Sleep(absl::Milliseconds(200));
SystemClock::Sleep(kTaskWaitTimeout);
scanner_broker_->StartScanning(Protocol::kFastPairInitialPairing);
SystemClock::Sleep(absl::Milliseconds(200));
SystemClock::Sleep(kTaskWaitTimeout);
EXPECT_TRUE(discoverable_scanner_factory_->create_instance());
env_.Stop();
}
} // namespace