mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Rewrite scanner_broker_impl_test
PiperOrigin-RevId: 526759429
This commit is contained in:
committed by
Copybara-Service
parent
0e9d578e42
commit
cb48598cca
@@ -68,14 +68,15 @@ cc_test(
|
||||
deps = [
|
||||
":scanner",
|
||||
"//fastpair/common",
|
||||
"//fastpair/scanning/fastpair:scanning",
|
||||
"//fastpair/scanning/fastpair:test_support",
|
||||
"//fastpair/internal/ble",
|
||||
"//fastpair/proto:fastpair_cc_proto",
|
||||
"//fastpair/server_access:test_support",
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:test_util",
|
||||
"//internal/platform/implementation:types",
|
||||
"//internal/platform: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",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -16,190 +16,86 @@
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/escaping.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"
|
||||
#include "fastpair/scanning/fastpair/fake_fast_pair_scanner.h"
|
||||
#include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h"
|
||||
#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/implementation/system_clock.h"
|
||||
#include "fastpair/internal/ble/ble.h"
|
||||
#include "fastpair/proto/fastpair_rpcs.proto.h"
|
||||
#include "fastpair/scanning/scanner_broker.h"
|
||||
#include "fastpair/server_access/fake_fast_pair_repository.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/count_down_latch.h"
|
||||
#include "internal/platform/medium_environment.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace fastpair {
|
||||
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 {
|
||||
constexpr absl::Duration kTaskWaitTimeout = absl::Milliseconds(1000);
|
||||
constexpr absl::string_view kServiceID{"Fast Pair"};
|
||||
constexpr absl::string_view kModelId{"718c17"};
|
||||
constexpr absl::string_view kFastPairServiceUuid{
|
||||
"0000FE2C-0000-1000-8000-00805F9B34FB"};
|
||||
constexpr absl::string_view kPublicAntiSpoof =
|
||||
"Wuyr48lD3txnUhGiMF1IfzlTwRxxe+wMB1HLzP+"
|
||||
"0wVcljfT3XPoiy1fntlneziyLD5knDVAJSE+RM/zlPRP/Jg==";
|
||||
class ScannerBrokerObserver : public ScannerBroker::Observer {
|
||||
public:
|
||||
// FastPairScannerImpl::Factory:
|
||||
std::shared_ptr<FastPairScanner> CreateInstance() override {
|
||||
auto fake_fast_pair_scanner = std::shared_ptr<FakeFastPairScanner>();
|
||||
fake_fast_pair_scanner_ = fake_fast_pair_scanner.get();
|
||||
return fake_fast_pair_scanner;
|
||||
}
|
||||
|
||||
~FakeFastPairScannerFactory() override = default;
|
||||
|
||||
FakeFastPairScanner* fake_fast_pair_scanner() {
|
||||
return fake_fast_pair_scanner_;
|
||||
}
|
||||
|
||||
private:
|
||||
FakeFastPairScanner* fake_fast_pair_scanner_ = nullptr;
|
||||
};
|
||||
|
||||
class FakeFastPairDiscoverableScannerFactory
|
||||
: public FastPairDiscoverableScannerImpl::Factory {
|
||||
public:
|
||||
// FastPairDiscoverableScannerImpl::Factory:
|
||||
std::unique_ptr<FastPairDiscoverableScanner> CreateInstance(
|
||||
std::shared_ptr<FastPairScanner> scanner,
|
||||
std::shared_ptr<BluetoothAdapter> adapter, DeviceCallback found_callback,
|
||||
DeviceCallback lost_callback) override {
|
||||
create_instance_ = true;
|
||||
auto fake_fast_pair_discoverable_scanner =
|
||||
std::make_unique<FakeFastPairDiscoverableScanner>(
|
||||
std::move(found_callback), std::move(lost_callback));
|
||||
fake_fast_pair_discoverable_scanner_ =
|
||||
fake_fast_pair_discoverable_scanner.get();
|
||||
return fake_fast_pair_discoverable_scanner;
|
||||
}
|
||||
|
||||
FakeFastPairDiscoverableScanner* fake_fast_pair_discoverable_scanner() {
|
||||
return fake_fast_pair_discoverable_scanner_;
|
||||
}
|
||||
|
||||
bool create_instance() { return create_instance_; }
|
||||
|
||||
protected:
|
||||
bool create_instance_ = false;
|
||||
FakeFastPairDiscoverableScanner* fake_fast_pair_discoverable_scanner_ =
|
||||
nullptr;
|
||||
};
|
||||
|
||||
class ScannerBrokerImplTest : public testing::Test,
|
||||
public ScannerBroker::Observer {
|
||||
public:
|
||||
void SetUp() override {
|
||||
adapter_ = std::shared_ptr<BluetoothAdapter>();
|
||||
|
||||
scanner_factory_ = std::make_unique<FakeFastPairScannerFactory>();
|
||||
FastPairScannerImpl::Factory::SetFactoryForTesting(scanner_factory_.get());
|
||||
|
||||
discoverable_scanner_factory_ =
|
||||
std::make_unique<FakeFastPairDiscoverableScannerFactory>();
|
||||
FastPairDiscoverableScannerImpl::Factory::SetFactoryForTesting(
|
||||
discoverable_scanner_factory_.get());
|
||||
|
||||
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),
|
||||
Protocol::kFastPairInitialPairing);
|
||||
discoverable_scanner_factory_->fake_fast_pair_discoverable_scanner()
|
||||
->TriggerDeviceFoundCallback(device);
|
||||
}
|
||||
|
||||
void TriggerDiscoverableDeviceLost() {
|
||||
FastPairDevice device(std::string(kValidModelId),
|
||||
std::string(kTestDeviceAddress),
|
||||
Protocol::kFastPairInitialPairing);
|
||||
discoverable_scanner_factory_->fake_fast_pair_discoverable_scanner()
|
||||
->TriggerDeviceLostCallback(device);
|
||||
explicit ScannerBrokerObserver(ScannerBroker* scanner_broker,
|
||||
CountDownLatch* accept_latch,
|
||||
CountDownLatch* lost_latch) {
|
||||
accept_latch_ = accept_latch;
|
||||
lost_latch_ = lost_latch;
|
||||
scanner_broker->AddObserver(this);
|
||||
}
|
||||
|
||||
void OnDeviceFound(const FastPairDevice& device) override {
|
||||
device_found_ = true;
|
||||
accept_latch_->CountDown();
|
||||
}
|
||||
|
||||
void OnDeviceLost(const FastPairDevice& device) override {
|
||||
device_lost_ = true;
|
||||
lost_latch_->CountDown();
|
||||
}
|
||||
|
||||
protected:
|
||||
bool device_found_ = false;
|
||||
bool device_lost_ = false;
|
||||
std::shared_ptr<BluetoothAdapter> adapter_;
|
||||
std::unique_ptr<FakeFastPairScannerFactory> scanner_factory_;
|
||||
std::unique_ptr<FakeFastPairDiscoverableScannerFactory>
|
||||
discoverable_scanner_factory_;
|
||||
std::unique_ptr<ScannerBroker> scanner_broker_;
|
||||
CountDownLatch* accept_latch_ = nullptr;
|
||||
CountDownLatch* lost_latch_ = nullptr;
|
||||
};
|
||||
|
||||
TEST_F(ScannerBrokerImplTest, DiscoverableFound) {
|
||||
EXPECT_FALSE(discoverable_scanner_factory_->create_instance());
|
||||
class ScannerBrokerImplTest : public testing::Test {
|
||||
protected:
|
||||
MediumEnvironment& env_{MediumEnvironment::Instance()};
|
||||
};
|
||||
|
||||
scanner_broker_->StartScanning(Protocol::kFastPairInitialPairing);
|
||||
SystemClock::Sleep(kTaskWaitTimeout);
|
||||
EXPECT_FALSE(device_found_);
|
||||
EXPECT_TRUE(discoverable_scanner_factory_->create_instance());
|
||||
TEST_F(ScannerBrokerImplTest, CanStartScanning) {
|
||||
env_.Start();
|
||||
auto repository_ = std::make_unique<FakeFastPairRepository>();
|
||||
auto scanner_broker = std::make_unique<ScannerBrokerImpl>();
|
||||
proto::Device metadata;
|
||||
std::string decoded_key;
|
||||
absl::Base64Unescape(kPublicAntiSpoof, &decoded_key);
|
||||
metadata.mutable_anti_spoofing_key_pair()->set_public_key(decoded_key);
|
||||
repository_->SetFakeMetadata(kModelId, metadata);
|
||||
|
||||
TriggerDiscoverableDeviceFound();
|
||||
EXPECT_TRUE(device_found_);
|
||||
std::string service_id(kServiceID);
|
||||
ByteArray advertisement_bytes{absl::HexStringToBytes(kModelId)};
|
||||
std::string fast_pair_service_uuid(kFastPairServiceUuid);
|
||||
Ble ble;
|
||||
CountDownLatch accept_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
ScannerBrokerObserver observer(scanner_broker.get(), &accept_latch,
|
||||
&lost_latch);
|
||||
|
||||
ble.getMedium().StartAdvertising(service_id, advertisement_bytes,
|
||||
fast_pair_service_uuid);
|
||||
scanner_broker->StartScanning(Protocol::kFastPairInitialPairing);
|
||||
EXPECT_TRUE(accept_latch.Await(kTaskWaitTimeout).result());
|
||||
ble.getMedium().StopAdvertising(service_id);
|
||||
EXPECT_TRUE(lost_latch.Await(kTaskWaitTimeout).result());
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(ScannerBrokerImplTest, DiscoverableLost) {
|
||||
EXPECT_FALSE(discoverable_scanner_factory_->create_instance());
|
||||
|
||||
scanner_broker_->StartScanning(Protocol::kFastPairInitialPairing);
|
||||
SystemClock::Sleep(kTaskWaitTimeout);
|
||||
EXPECT_FALSE(device_found_);
|
||||
EXPECT_TRUE(discoverable_scanner_factory_->create_instance());
|
||||
|
||||
TriggerDiscoverableDeviceLost();
|
||||
EXPECT_TRUE(device_lost_);
|
||||
}
|
||||
|
||||
TEST_F(ScannerBrokerImplTest, RemoveObserver) {
|
||||
EXPECT_FALSE(discoverable_scanner_factory_->create_instance());
|
||||
|
||||
scanner_broker_->StartScanning(Protocol::kFastPairInitialPairing);
|
||||
SystemClock::Sleep(kTaskWaitTimeout);
|
||||
EXPECT_FALSE(device_found_);
|
||||
EXPECT_TRUE(discoverable_scanner_factory_->create_instance());
|
||||
|
||||
scanner_broker_->RemoveObserver(this);
|
||||
TriggerDiscoverableDeviceLost();
|
||||
EXPECT_FALSE(device_lost_);
|
||||
}
|
||||
|
||||
TEST_F(ScannerBrokerImplTest, StopScanning) {
|
||||
EXPECT_FALSE(discoverable_scanner_factory_->create_instance());
|
||||
|
||||
scanner_broker_->StartScanning(Protocol::kFastPairInitialPairing);
|
||||
SystemClock::Sleep(kTaskWaitTimeout);
|
||||
EXPECT_TRUE(discoverable_scanner_factory_->create_instance());
|
||||
|
||||
scanner_broker_->StopScanning(Protocol::kFastPairInitialPairing);
|
||||
SystemClock::Sleep(kTaskWaitTimeout);
|
||||
|
||||
scanner_broker_->StartScanning(Protocol::kFastPairInitialPairing);
|
||||
SystemClock::Sleep(kTaskWaitTimeout);
|
||||
EXPECT_TRUE(discoverable_scanner_factory_->create_instance());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace fastpair
|
||||
} // namespace nearby
|
||||
|
||||
Reference in New Issue
Block a user