From eec9c98a95e528f9f93791f19fcdc7fe844cf3ec Mon Sep 17 00:00:00 2001 From: Qin Wang Date: Tue, 18 Jul 2023 12:39:54 -0700 Subject: [PATCH] Refactor fast_pair_discoverable_scanner PiperOrigin-RevId: 549081197 --- fastpair/scanning/fastpair/BUILD | 8 +- .../fake_fast_pair_discoverable_scanner.h | 50 ------- ...l.cc => fast_pair_discoverable_scanner.cc} | 44 +++--- .../fastpair/fast_pair_discoverable_scanner.h | 71 +++++++++- .../fast_pair_discoverable_scanner_impl.h | 93 ------------- ...=> fast_pair_discoverable_scanner_test.cc} | 127 +++++++++--------- fastpair/scanning/scanner_broker_impl.cc | 4 +- 7 files changed, 157 insertions(+), 240 deletions(-) delete mode 100644 fastpair/scanning/fastpair/fake_fast_pair_discoverable_scanner.h rename fastpair/scanning/fastpair/{fast_pair_discoverable_scanner_impl.cc => fast_pair_discoverable_scanner.cc} (86%) delete mode 100644 fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h rename fastpair/scanning/fastpair/{fast_pair_discoverable_scanner_impl_test.cc => fast_pair_discoverable_scanner_test.cc} (70%) diff --git a/fastpair/scanning/fastpair/BUILD b/fastpair/scanning/fastpair/BUILD index f5e218b6..3ece0245 100644 --- a/fastpair/scanning/fastpair/BUILD +++ b/fastpair/scanning/fastpair/BUILD @@ -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 = [ diff --git a/fastpair/scanning/fastpair/fake_fast_pair_discoverable_scanner.h b/fastpair/scanning/fastpair/fake_fast_pair_discoverable_scanner.h deleted file mode 100644 index 7dcbb8b9..00000000 --- a/fastpair/scanning/fastpair/fake_fast_pair_discoverable_scanner.h +++ /dev/null @@ -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 - -#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_ diff --git a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.cc b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner.cc similarity index 86% rename from fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.cc rename to fastpair/scanning/fastpair/fast_pair_discoverable_scanner.cc index 936050ef..e44f5172 100644 --- a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.cc +++ b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner.cc @@ -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 #include @@ -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 -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( + return std::make_unique( 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 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 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", diff --git a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h index db42ce0d..c53243e0 100644 --- a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h +++ b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner.h @@ -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 +#include +#include +#include + +#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; - +using DiscoverableScannerCallback = + absl::AnyInvocable; // 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 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 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 model_id); + void OnDeviceMetadataRetrieved(std::string address, std::string model_id, + std::optional 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 observer_list_; }; } // namespace fastpair diff --git a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h deleted file mode 100644 index 349db2b6..00000000 --- a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl.h +++ /dev/null @@ -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 -#include -#include -#include - -#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 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 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 model_id); - void OnDeviceMetadataRetrieved(std::string address, std::string model_id, - std::optional 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 observer_list_; -}; - -} // namespace fastpair -} // namespace nearby - -#endif // THIRD_PARTY_NEARBY_FASTPAIR_SCANNING_FASTPAIR_FAST_PAIR_DISCOVERABLE_SCANNER_IMPL_H_ diff --git a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl_test.cc b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_test.cc similarity index 70% rename from fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl_test.cc rename to fastpair/scanning/fastpair/fast_pair_discoverable_scanner_test.cc index 1be87a65..cc639a10 100644 --- a/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_impl_test.cc +++ b/fastpair/scanning/fastpair/fast_pair_discoverable_scanner_test.cc @@ -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 #include @@ -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 scanner_; std::unique_ptr discoverable_scanner_; }; -TEST_F(FastPairDiscoverableScannerImplTest, ValidModelId) { - auto scanner = std::make_unique(); +TEST_F(FastPairDiscoverableScannerTest, ValidModelId) { + scanner_ = std::make_unique(); auto repository = std::make_unique(); 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(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(); +TEST_F(FastPairDiscoverableScannerTest, InvalidModelId) { + scanner_ = std::make_unique(); auto repository = std::make_unique(); 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( 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(); +TEST_F(FastPairDiscoverableScannerTest, NoServiceData) { + scanner_ = std::make_unique(); auto repository = std::make_unique(); 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(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(); +TEST_F(FastPairDiscoverableScannerTest, UnsupportedDeviceType) { + scanner_ = std::make_unique(); auto repository = std::make_unique(); 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(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(); +TEST_F(FastPairDiscoverableScannerTest, UnsupportedNotifictionType) { + scanner_ = std::make_unique(); auto repository = std::make_unique(); 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(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(); +TEST_F(FastPairDiscoverableScannerTest, UnspecifiedNotificationType) { + scanner_ = std::make_unique(); // 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(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(); +TEST_F(FastPairDiscoverableScannerTest, V1NotificationType) { + scanner_ = std::make_unique(); // 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(); @@ -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(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(); +TEST_F(FastPairDiscoverableScannerTest, V2NotificationType) { + scanner_ = std::make_unique(); // 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(); @@ -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(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(); +TEST_F(FastPairDiscoverableScannerTest, NearbyShareModelId) { + scanner_ = std::make_unique(); auto repository = std::make_unique(); 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( 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(); + scanner_ = std::make_unique(); auto repository = std::make_unique(); 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(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)); } diff --git a/fastpair/scanning/scanner_broker_impl.cc b/fastpair/scanning/scanner_broker_impl.cc index 1a38c53c..92f1a7b3 100644 --- a/fastpair/scanning/scanner_broker_impl.cc +++ b/fastpair/scanning/scanner_broker_impl.cc @@ -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(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),