Refactor fast_pair_discoverable_scanner

PiperOrigin-RevId: 549081197
This commit is contained in:
Qin Wang
2023-07-18 12:41:23 -07:00
committed by Copybara-Service
parent 0f8e5283a4
commit eec9c98a95
7 changed files with 157 additions and 240 deletions
+3 -5
View File
@@ -17,13 +17,12 @@ licenses(["notice"])
cc_library(
name = "scanning",
srcs = [
"fast_pair_discoverable_scanner_impl.cc",
"fast_pair_discoverable_scanner.cc",
"fast_pair_non_discoverable_scanner.cc",
"fast_pair_scanner_impl.cc",
],
hdrs = [
"fast_pair_discoverable_scanner.h",
"fast_pair_discoverable_scanner_impl.h",
"fast_pair_non_discoverable_scanner.h",
"fast_pair_scanner.h",
"fast_pair_scanner_impl.h",
@@ -57,7 +56,6 @@ cc_library(
"fake_fast_pair_scanner.cc",
],
hdrs = [
"fake_fast_pair_discoverable_scanner.h",
"fake_fast_pair_scanner.h",
],
visibility = [
@@ -95,10 +93,10 @@ cc_test(
)
cc_test(
name = "fast_pair_discoverable_scanner_impl_test",
name = "fast_pair_discoverable_scanner_test",
size = "small",
srcs = [
"fast_pair_discoverable_scanner_impl_test.cc",
"fast_pair_discoverable_scanner_test.cc",
],
shard_count = 16,
deps = [
@@ -1,50 +0,0 @@
// Copyright 2022 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_FASTPAIR_FAKE_FAST_PAIR_DISCOVERABLE_SCANNER_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_SCANNING_FASTPAIR_FAKE_FAST_PAIR_DISCOVERABLE_SCANNER_H_
#include <utility>
#include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h"
namespace nearby {
namespace fastpair {
class FakeFastPairDiscoverableScanner : public FastPairDiscoverableScanner {
public:
FakeFastPairDiscoverableScanner(DeviceCallback found_callback,
DeviceCallback lost_callback)
: found_callback_(std::move(found_callback)),
lost_callback_(std::move(lost_callback)) {}
~FakeFastPairDiscoverableScanner() override = default;
void TriggerDeviceFoundCallback(FastPairDevice& device) {
found_callback_(device);
}
void TriggerDeviceLostCallback(FastPairDevice& device) {
lost_callback_(device);
}
private:
DeviceCallback found_callback_;
DeviceCallback lost_callback_;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_SCANNING_FASTPAIR_FAKE_FAST_PAIR_DISCOVERABLE_SCANNER_H_
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h"
#include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h"
#include <algorithm>
#include <cstdint>
@@ -61,13 +61,13 @@ bool IsSupportedNotificationType(const proto::Device& device) {
} // namespace
// FastPairScannerImpl::Factory
FastPairDiscoverableScannerImpl::Factory*
FastPairDiscoverableScannerImpl::Factory::g_test_factory_ = nullptr;
FastPairDiscoverableScanner::Factory*
FastPairDiscoverableScanner::Factory::g_test_factory_ = nullptr;
std::unique_ptr<FastPairDiscoverableScanner>
FastPairDiscoverableScannerImpl::Factory::Create(
FastPairScanner& scanner, DeviceCallback found_callback,
DeviceCallback lost_callback, SingleThreadExecutor* executor,
FastPairDiscoverableScanner::Factory::Create(
FastPairScanner& scanner, DiscoverableScannerCallback found_callback,
DiscoverableScannerCallback lost_callback, SingleThreadExecutor* executor,
FastPairDeviceRepository* device_repository) {
if (g_test_factory_) {
return g_test_factory_->CreateInstance(scanner, std::move(found_callback),
@@ -75,22 +75,22 @@ FastPairDiscoverableScannerImpl::Factory::Create(
device_repository);
}
return std::make_unique<FastPairDiscoverableScannerImpl>(
return std::make_unique<FastPairDiscoverableScanner>(
scanner, std::move(found_callback), std::move(lost_callback), executor,
device_repository);
}
void FastPairDiscoverableScannerImpl::Factory::SetFactoryForTesting(
void FastPairDiscoverableScanner::Factory::SetFactoryForTesting(
Factory* g_test_factory) {
g_test_factory_ = g_test_factory;
}
FastPairDiscoverableScannerImpl::Factory::~Factory() = default;
FastPairDiscoverableScanner::Factory::~Factory() = default;
// FastPairScannerImpl
FastPairDiscoverableScannerImpl::FastPairDiscoverableScannerImpl(
FastPairScanner& scanner, DeviceCallback found_callback,
DeviceCallback lost_callback, SingleThreadExecutor* executor,
FastPairDiscoverableScanner::FastPairDiscoverableScanner(
FastPairScanner& scanner, DiscoverableScannerCallback found_callback,
DiscoverableScannerCallback lost_callback, SingleThreadExecutor* executor,
FastPairDeviceRepository* device_repository)
: scanner_(scanner),
found_callback_(std::move(found_callback)),
@@ -100,7 +100,11 @@ FastPairDiscoverableScannerImpl::FastPairDiscoverableScannerImpl(
scanner_.AddObserver(this);
}
void FastPairDiscoverableScannerImpl::OnDeviceFound(
FastPairDiscoverableScanner::~FastPairDiscoverableScanner() {
scanner_.RemoveObserver(this);
}
void FastPairDiscoverableScanner::OnDeviceFound(
const BlePeripheral& peripheral) {
std::string fast_pair_service_data =
peripheral.GetAdvertisementBytes(kServiceId).string_data();
@@ -131,7 +135,7 @@ void FastPairDiscoverableScannerImpl::OnDeviceFound(
});
}
void FastPairDiscoverableScannerImpl::OnModelIdRetrieved(
void FastPairDiscoverableScanner::OnModelIdRetrieved(
const std::string& address,
const std::optional<absl::string_view> model_id) {
if (!model_id.has_value()) {
@@ -152,12 +156,11 @@ void FastPairDiscoverableScannerImpl::OnModelIdRetrieved(
NEARBY_LOGS(INFO) << __func__ << ": Attempting to get device metadata.";
FastPairRepository::Get()->GetDeviceMetadata(
model_id.value(),
absl::bind_front(
&FastPairDiscoverableScannerImpl::OnDeviceMetadataRetrieved, this,
address, std::string(model_id.value())));
absl::bind_front(&FastPairDiscoverableScanner::OnDeviceMetadataRetrieved,
this, address, std::string(model_id.value())));
}
void FastPairDiscoverableScannerImpl::OnDeviceMetadataRetrieved(
void FastPairDiscoverableScanner::OnDeviceMetadataRetrieved(
const std::string address, const std::string model_id,
std::optional<DeviceMetadata> device_metadata) {
if (!device_metadata.has_value()) {
@@ -196,15 +199,14 @@ void FastPairDiscoverableScannerImpl::OnDeviceMetadataRetrieved(
});
}
void FastPairDiscoverableScannerImpl::NotifyDeviceFound(
FastPairDevice& device) {
void FastPairDiscoverableScanner::NotifyDeviceFound(FastPairDevice& device) {
NEARBY_LOGS(VERBOSE) << "Notify Device found:"
<< "BluetoothAddress = " << device.GetBleAddress()
<< ", Model id = " << device.GetModelId();
found_callback_(device);
}
void FastPairDiscoverableScannerImpl::OnDeviceLost(
void FastPairDiscoverableScanner::OnDeviceLost(
const BlePeripheral& peripheral) {
NEARBY_LOGS(INFO) << __func__ << ": Running lost callback";
executor_->Execute("device-lost",
@@ -15,22 +15,81 @@
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_SCANNING_FASTPAIR_FAST_PAIR_DISCOVERABLE_SCANNER_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_SCANNING_FASTPAIR_FAST_PAIR_DISCOVERABLE_SCANNER_H_
#include "absl/functional/any_invocable.h"
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "fastpair/common/device_metadata.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/scanning/fastpair/fast_pair_scanner.h"
#include "internal/base/observer_list.h"
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/logging.h"
#include "internal/platform/single_thread_executor.h"
namespace nearby {
namespace fastpair {
using DeviceCallback = absl::AnyInvocable<void(FastPairDevice& device)>;
using DiscoverableScannerCallback =
absl::AnyInvocable<void(FastPairDevice& device)>;
// This class detects Fast Pair 'discoverable' advertisements (see
// https://developers.google.com/nearby/fast-pair/spec#AdvertisingWhenDiscoverable)
// and invokes the |found_callback| when it finds a device within the
// appropriate range. |lost_callback| will be invoked when that device is lost
// to the bluetooth adapter.
class FastPairDiscoverableScanner {
class FastPairDiscoverableScanner : public FastPairScanner::Observer {
public:
virtual ~FastPairDiscoverableScanner() = default;
class Factory {
public:
static std::unique_ptr<FastPairDiscoverableScanner> Create(
FastPairScanner& scanner, DiscoverableScannerCallback found_callback,
DiscoverableScannerCallback lost_callback,
SingleThreadExecutor* executor,
FastPairDeviceRepository* device_repository);
static void SetFactoryForTesting(Factory* g_test_factory);
protected:
virtual ~Factory();
virtual std::unique_ptr<FastPairDiscoverableScanner> CreateInstance(
FastPairScanner& scanner, DiscoverableScannerCallback found_callback,
DiscoverableScannerCallback lost_callback,
SingleThreadExecutor* executor,
FastPairDeviceRepository* device_repository) = 0;
private:
static Factory* g_test_factory_;
};
FastPairDiscoverableScanner(FastPairScanner& scanner,
DiscoverableScannerCallback found_callback,
DiscoverableScannerCallback lost_callback,
SingleThreadExecutor* executor,
FastPairDeviceRepository* device_repository);
FastPairDiscoverableScanner(const FastPairDiscoverableScanner&) = delete;
FastPairDiscoverableScanner& operator=(const FastPairDiscoverableScanner&) =
delete;
~FastPairDiscoverableScanner();
// FastPairScanner::Observer
void OnDeviceFound(const BlePeripheral& peripheral) override;
void OnDeviceLost(const BlePeripheral& peripheral) override;
private:
void OnModelIdRetrieved(const std::string& address,
std::optional<absl::string_view> model_id);
void OnDeviceMetadataRetrieved(std::string address, std::string model_id,
std::optional<DeviceMetadata> device_metadata);
void NotifyDeviceFound(FastPairDevice& device)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_);
FastPairScanner& scanner_;
DiscoverableScannerCallback found_callback_ ABSL_GUARDED_BY(*executor_);
DiscoverableScannerCallback lost_callback_ ABSL_GUARDED_BY(*executor_);
SingleThreadExecutor* executor_;
FastPairDeviceRepository* device_repository_ ABSL_GUARDED_BY(*executor_);
ObserverList<FastPairScanner::Observer> observer_list_;
};
} // namespace fastpair
@@ -1,93 +0,0 @@
// Copyright 2022 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_FASTPAIR_FAST_PAIR_DISCOVERABLE_SCANNER_IMPL_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_SCANNING_FASTPAIR_FAST_PAIR_DISCOVERABLE_SCANNER_IMPL_H_
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "fastpair/common/device_metadata.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h"
#include "fastpair/scanning/fastpair/fast_pair_scanner.h"
#include "internal/base/observer_list.h"
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/logging.h"
#include "internal/platform/single_thread_executor.h"
namespace nearby {
namespace fastpair {
class FastPairDiscoverableScannerImpl : public FastPairDiscoverableScanner,
public FastPairScanner::Observer {
public:
class Factory {
public:
static std::unique_ptr<FastPairDiscoverableScanner> Create(
FastPairScanner& scanner, DeviceCallback found_callback,
DeviceCallback lost_callback, SingleThreadExecutor* executor,
FastPairDeviceRepository* device_repository);
static void SetFactoryForTesting(Factory* g_test_factory);
protected:
virtual ~Factory();
virtual std::unique_ptr<FastPairDiscoverableScanner> CreateInstance(
FastPairScanner& scanner, DeviceCallback found_callback,
DeviceCallback lost_callback, SingleThreadExecutor* executor,
FastPairDeviceRepository* device_repository) = 0;
private:
static Factory* g_test_factory_;
};
FastPairDiscoverableScannerImpl(FastPairScanner& scanner,
DeviceCallback found_callback,
DeviceCallback lost_callback,
SingleThreadExecutor* executor,
FastPairDeviceRepository* device_repository);
FastPairDiscoverableScannerImpl(const FastPairDiscoverableScannerImpl&) =
delete;
FastPairDiscoverableScannerImpl& operator=(
const FastPairDiscoverableScannerImpl&) = delete;
~FastPairDiscoverableScannerImpl() override = default;
// FastPairScanner::Observer
void OnDeviceFound(const BlePeripheral& peripheral) override;
void OnDeviceLost(const BlePeripheral& peripheral) override;
private:
void OnModelIdRetrieved(const std::string& address,
std::optional<absl::string_view> model_id);
void OnDeviceMetadataRetrieved(std::string address, std::string model_id,
std::optional<DeviceMetadata> device_metadata);
void NotifyDeviceFound(FastPairDevice& device)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_);
FastPairScanner& scanner_;
DeviceCallback found_callback_ ABSL_GUARDED_BY(*executor_);
DeviceCallback lost_callback_ ABSL_GUARDED_BY(*executor_);
SingleThreadExecutor* executor_;
FastPairDeviceRepository* device_repository_ ABSL_GUARDED_BY(*executor_);
ObserverList<FastPairScanner::Observer> observer_list_;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_SCANNING_FASTPAIR_FAST_PAIR_DISCOVERABLE_SCANNER_IMPL_H_
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h"
#include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h"
#include <memory>
#include <string>
@@ -20,9 +20,9 @@
#include "gtest/gtest.h"
#include "absl/synchronization/notification.h"
#include "fastpair/repository/fake_fast_pair_repository.h"
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/scanning/fastpair/fake_fast_pair_scanner.h"
#include "fastpair/repository/fake_fast_pair_repository.h"
#include "fastpair/testing/fast_pair_service_data_creator.h"
#include "internal/platform/bluetooth_adapter.h"
@@ -72,17 +72,18 @@ class FakeBlePeripheral : public api::BlePeripheral {
ByteArray advertisement_data_;
};
class FastPairDiscoverableScannerImplTest : public ::testing::Test {
class FastPairDiscoverableScannerTest : public ::testing::Test {
protected:
void TearDown() override { executor_.Shutdown(); }
SingleThreadExecutor executor_;
FastPairDeviceRepository devices_{&executor_};
std::unique_ptr<FakeFastPairScanner> scanner_;
std::unique_ptr<FastPairDiscoverableScanner> discoverable_scanner_;
};
TEST_F(FastPairDiscoverableScannerImplTest, ValidModelId) {
auto scanner = std::make_unique<FakeFastPairScanner>();
TEST_F(FastPairDiscoverableScannerTest, ValidModelId) {
scanner_ = std::make_unique<FakeFastPairScanner>();
auto repository = std::make_unique<FakeFastPairRepository>();
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::TRUE_WIRELESS_HEADPHONES);
@@ -90,21 +91,21 @@ TEST_F(FastPairDiscoverableScannerImplTest, ValidModelId) {
absl::Notification found_notification;
absl::Notification lost_notification;
discoverable_scanner_ = FastPairDiscoverableScannerImpl::Factory::Create(
*scanner, [&](FastPairDevice& device) { found_notification.Notify(); },
discoverable_scanner_ = FastPairDiscoverableScanner::Factory::Create(
*scanner_, [&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); }, &executor_,
&devices_);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
scanner->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
found_notification.WaitForNotification();
scanner->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
lost_notification.WaitForNotification();
}
TEST_F(FastPairDiscoverableScannerImplTest, InvalidModelId) {
auto scanner = std::make_unique<FakeFastPairScanner>();
TEST_F(FastPairDiscoverableScannerTest, InvalidModelId) {
scanner_ = std::make_unique<FakeFastPairScanner>();
auto repository = std::make_unique<FakeFastPairRepository>();
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::TRUE_WIRELESS_HEADPHONES);
@@ -112,21 +113,21 @@ TEST_F(FastPairDiscoverableScannerImplTest, InvalidModelId) {
absl::Notification found_notification;
absl::Notification lost_notification;
discoverable_scanner_ = FastPairDiscoverableScannerImpl::Factory::Create(
*scanner, [&](FastPairDevice& device) { found_notification.Notify(); },
discoverable_scanner_ = FastPairDiscoverableScanner::Factory::Create(
*scanner_, [&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); }, &executor_,
&devices_);
auto ble_peripheral = std::make_unique<FakeBlePeripheral>(
kTestBleDeviceAddress, kInvalidModelId);
scanner->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(found_notification.WaitForNotificationWithTimeout(kWaitTimeout));
scanner->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(lost_notification.WaitForNotificationWithTimeout(kWaitTimeout));
}
TEST_F(FastPairDiscoverableScannerImplTest, NoServiceData) {
auto scanner = std::make_unique<FakeFastPairScanner>();
TEST_F(FastPairDiscoverableScannerTest, NoServiceData) {
scanner_ = std::make_unique<FakeFastPairScanner>();
auto repository = std::make_unique<FakeFastPairRepository>();
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::TRUE_WIRELESS_HEADPHONES);
@@ -134,21 +135,21 @@ TEST_F(FastPairDiscoverableScannerImplTest, NoServiceData) {
absl::Notification found_notification;
absl::Notification lost_notification;
discoverable_scanner_ = FastPairDiscoverableScannerImpl::Factory::Create(
*scanner, [&](FastPairDevice& device) { found_notification.Notify(); },
discoverable_scanner_ = FastPairDiscoverableScanner::Factory::Create(
*scanner_, [&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); }, &executor_,
&devices_);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, "");
scanner->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(found_notification.WaitForNotificationWithTimeout(kWaitTimeout));
scanner->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(lost_notification.WaitForNotificationWithTimeout(kWaitTimeout));
}
TEST_F(FastPairDiscoverableScannerImplTest, UnsupportedDeviceType) {
auto scanner = std::make_unique<FakeFastPairScanner>();
TEST_F(FastPairDiscoverableScannerTest, UnsupportedDeviceType) {
scanner_ = std::make_unique<FakeFastPairScanner>();
auto repository = std::make_unique<FakeFastPairRepository>();
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::AUTOMOTIVE);
@@ -156,21 +157,21 @@ TEST_F(FastPairDiscoverableScannerImplTest, UnsupportedDeviceType) {
absl::Notification found_notification;
absl::Notification lost_notification;
discoverable_scanner_ = FastPairDiscoverableScannerImpl::Factory::Create(
*scanner, [&](FastPairDevice& device) { found_notification.Notify(); },
discoverable_scanner_ = FastPairDiscoverableScanner::Factory::Create(
*scanner_, [&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); }, &executor_,
&devices_);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
scanner->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(found_notification.WaitForNotificationWithTimeout(kWaitTimeout));
scanner->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(lost_notification.WaitForNotificationWithTimeout(kWaitTimeout));
}
TEST_F(FastPairDiscoverableScannerImplTest, UnsupportedNotifictionType) {
auto scanner = std::make_unique<FakeFastPairScanner>();
TEST_F(FastPairDiscoverableScannerTest, UnsupportedNotifictionType) {
scanner_ = std::make_unique<FakeFastPairScanner>();
auto repository = std::make_unique<FakeFastPairRepository>();
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::HEADPHONES);
@@ -179,21 +180,21 @@ TEST_F(FastPairDiscoverableScannerImplTest, UnsupportedNotifictionType) {
absl::Notification found_notification;
absl::Notification lost_notification;
discoverable_scanner_ = FastPairDiscoverableScannerImpl::Factory::Create(
*scanner, [&](FastPairDevice& device) { found_notification.Notify(); },
discoverable_scanner_ = FastPairDiscoverableScanner::Factory::Create(
*scanner_, [&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); }, &executor_,
&devices_);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
scanner->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(found_notification.WaitForNotificationWithTimeout(kWaitTimeout));
scanner->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(lost_notification.WaitForNotificationWithTimeout(kWaitTimeout));
}
TEST_F(FastPairDiscoverableScannerImplTest, UnspecifiedNotificationType) {
auto scanner = std::make_unique<FakeFastPairScanner>();
TEST_F(FastPairDiscoverableScannerTest, UnspecifiedNotificationType) {
scanner_ = std::make_unique<FakeFastPairScanner>();
// Set metadata to mimic a device that doesn't specify the notification
// or device type. Since we aren't sure what this device is, we'll show
// the notification to be safe.
@@ -206,21 +207,21 @@ TEST_F(FastPairDiscoverableScannerImplTest, UnspecifiedNotificationType) {
absl::Notification found_notification;
absl::Notification lost_notification;
discoverable_scanner_ = FastPairDiscoverableScannerImpl::Factory::Create(
*scanner, [&](FastPairDevice& device) { found_notification.Notify(); },
discoverable_scanner_ = FastPairDiscoverableScanner::Factory::Create(
*scanner_, [&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); }, &executor_,
&devices_);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
scanner->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
found_notification.WaitForNotification();
scanner->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
lost_notification.WaitForNotification();
}
TEST_F(FastPairDiscoverableScannerImplTest, V1NotificationType) {
auto scanner = std::make_unique<FakeFastPairScanner>();
TEST_F(FastPairDiscoverableScannerTest, V1NotificationType) {
scanner_ = std::make_unique<FakeFastPairScanner>();
// Set metadata to mimic a V1 device which advertises with no device
// type and a notification type of FAST_PAIR_ONE.
auto repository = std::make_unique<FakeFastPairRepository>();
@@ -231,21 +232,21 @@ TEST_F(FastPairDiscoverableScannerImplTest, V1NotificationType) {
absl::Notification found_notification;
absl::Notification lost_notification;
discoverable_scanner_ = FastPairDiscoverableScannerImpl::Factory::Create(
*scanner, [&](FastPairDevice& device) { found_notification.Notify(); },
discoverable_scanner_ = FastPairDiscoverableScanner::Factory::Create(
*scanner_, [&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); }, &executor_,
&devices_);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
scanner->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
found_notification.WaitForNotification();
scanner->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
lost_notification.WaitForNotification();
}
TEST_F(FastPairDiscoverableScannerImplTest, V2NotificationType) {
auto scanner = std::make_unique<FakeFastPairScanner>();
TEST_F(FastPairDiscoverableScannerTest, V2NotificationType) {
scanner_ = std::make_unique<FakeFastPairScanner>();
// Set metadata to mimic a V2 device which advertises with a device
// type of TRUE_WIRELESS_HEADPHONES and a notification type of FAST_PAIR.
auto repository = std::make_unique<FakeFastPairRepository>();
@@ -256,59 +257,59 @@ TEST_F(FastPairDiscoverableScannerImplTest, V2NotificationType) {
absl::Notification found_notification;
absl::Notification lost_notification;
discoverable_scanner_ = FastPairDiscoverableScannerImpl::Factory::Create(
*scanner, [&](FastPairDevice& device) { found_notification.Notify(); },
discoverable_scanner_ = FastPairDiscoverableScanner::Factory::Create(
*scanner_, [&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); }, &executor_,
&devices_);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
scanner->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
found_notification.WaitForNotification();
scanner->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
lost_notification.WaitForNotification();
}
TEST_F(FastPairDiscoverableScannerImplTest, NearbyShareModelId) {
auto scanner = std::make_unique<FakeFastPairScanner>();
TEST_F(FastPairDiscoverableScannerTest, NearbyShareModelId) {
scanner_ = std::make_unique<FakeFastPairScanner>();
auto repository = std::make_unique<FakeFastPairRepository>();
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::TRUE_WIRELESS_HEADPHONES);
repository->SetFakeMetadata(kValidModelId, metadata);
absl::Notification found_notification;
absl::Notification lost_notification;
discoverable_scanner_ = FastPairDiscoverableScannerImpl::Factory::Create(
*scanner, [&](FastPairDevice& device) { found_notification.Notify(); },
discoverable_scanner_ = FastPairDiscoverableScanner::Factory::Create(
*scanner_, [&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); }, &executor_,
&devices_);
auto ble_peripheral = std::make_unique<FakeBlePeripheral>(
kTestBleDeviceAddress, kNearbyShareModelId);
scanner->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceFound(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(found_notification.WaitForNotificationWithTimeout(kWaitTimeout));
scanner->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(lost_notification.WaitForNotificationWithTimeout(kWaitTimeout));
}
TEST_F(FastPairDiscoverableScannerImplTest,
TEST_F(FastPairDiscoverableScannerTest,
DoesntInvokeLostCallbackIfDidntInvokeFound) {
auto scanner = std::make_unique<FakeFastPairScanner>();
scanner_ = std::make_unique<FakeFastPairScanner>();
auto repository = std::make_unique<FakeFastPairRepository>();
proto::Device metadata;
metadata.set_device_type(proto::DeviceType::TRUE_WIRELESS_HEADPHONES);
repository->SetFakeMetadata(kValidModelId, metadata);
absl::Notification found_notification;
absl::Notification lost_notification;
discoverable_scanner_ = FastPairDiscoverableScannerImpl::Factory::Create(
*scanner, [&](FastPairDevice& device) { found_notification.Notify(); },
discoverable_scanner_ = FastPairDiscoverableScanner::Factory::Create(
*scanner_, [&](FastPairDevice& device) { found_notification.Notify(); },
[&](FastPairDevice& device) { lost_notification.Notify(); }, &executor_,
&devices_);
auto ble_peripheral =
std::make_unique<FakeBlePeripheral>(kTestBleDeviceAddress, kValidModelId);
scanner->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(lost_notification.WaitForNotificationWithTimeout(kWaitTimeout));
scanner->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
scanner_->NotifyDeviceLost(BlePeripheral(ble_peripheral.get()));
EXPECT_FALSE(lost_notification.WaitForNotificationWithTimeout(kWaitTimeout));
}
+2 -2
View File
@@ -19,7 +19,7 @@
#include "absl/functional/bind_front.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h"
#include "fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h"
#include "fastpair/scanning/fastpair/fast_pair_non_discoverable_scanner.h"
#include "fastpair/scanning/fastpair/fast_pair_scanner_impl.h"
#include "internal/platform/logging.h"
@@ -64,7 +64,7 @@ ScannerBrokerImpl::StartScanning(Protocol protocol) {
NEARBY_LOGS(VERBOSE) << "Starting Fast Pair Scanning.";
scanner_ = std::make_unique<FastPairScannerImpl>(mediums_, executor_);
fast_pair_discoverable_scanner_ =
FastPairDiscoverableScannerImpl::Factory::Create(
FastPairDiscoverableScanner::Factory::Create(
*scanner_,
absl::bind_front(&ScannerBrokerImpl::NotifyDeviceFound, this),
absl::bind_front(&ScannerBrokerImpl::NotifyDeviceLost, this),