|
11 | 11 | #include "base/stl_util.h"
|
12 | 12 | #include "build/build_config.h"
|
13 | 13 | #include "device/bluetooth/bluetooth_adapter_factory.h"
|
| 14 | +#include "device/bluetooth/bluetooth_advertisement.h" |
14 | 15 | #include "device/bluetooth/test/bluetooth_test.h"
|
15 | 16 | #include "device/bluetooth/test/mock_bluetooth_adapter.h"
|
16 |
| -#include "device/bluetooth/test/mock_bluetooth_advertisement.h" |
17 | 17 | #include "device/fido/fido_ble_device.h"
|
18 | 18 | #include "device/fido/fido_ble_uuids.h"
|
19 | 19 | #include "device/fido/fido_parsing_utils.h"
|
@@ -112,6 +112,16 @@ MATCHER_P2(IsAdvertisementContent,
|
112 | 112 | return true;
|
113 | 113 | }
|
114 | 114 |
|
| 115 | +class CableMockBluetoothAdvertisement : public BluetoothAdvertisement { |
| 116 | + public: |
| 117 | + MOCK_METHOD2(Unregister, |
| 118 | + void(const SuccessCallback& success_callback, |
| 119 | + const ErrorCallback& error_callback)); |
| 120 | + |
| 121 | + private: |
| 122 | + ~CableMockBluetoothAdvertisement() override = default; |
| 123 | +}; |
| 124 | + |
115 | 125 | // Mock BLE adapter that abstracts out authenticator logic with the following
|
116 | 126 | // logic:
|
117 | 127 | // - Responds to BluetoothAdapter::RegisterAdvertisement() by always invoking
|
@@ -150,18 +160,23 @@ class CableMockAdapter : public MockBluetoothAdapter {
|
150 | 160 | void ExpectRegisterAdvertisementWithResponse(
|
151 | 161 | bool simulate_success,
|
152 | 162 | base::span<const uint8_t> expected_client_eid,
|
153 |
| - base::StringPiece expected_uuid_formatted_client_eid) { |
| 163 | + base::StringPiece expected_uuid_formatted_client_eid, |
| 164 | + scoped_refptr<CableMockBluetoothAdvertisement> advertisement_ptr = |
| 165 | + nullptr) { |
| 166 | + if (!advertisement_ptr) |
| 167 | + advertisement_ptr = |
| 168 | + base::MakeRefCounted<CableMockBluetoothAdvertisement>(); |
| 169 | + |
154 | 170 | EXPECT_CALL(*this,
|
155 | 171 | RegisterAdvertisement(
|
156 | 172 | IsAdvertisementContent(expected_client_eid,
|
157 | 173 | expected_uuid_formatted_client_eid),
|
158 | 174 | _, _))
|
159 | 175 | .WillOnce(::testing::WithArgs<1, 2>(
|
160 |
| - [simulate_success](const auto& success_callback, |
161 |
| - const auto& failure_callback) { |
| 176 | + [simulate_success, advertisement_ptr]( |
| 177 | + const auto& success_callback, const auto& failure_callback) { |
162 | 178 | simulate_success
|
163 |
| - ? success_callback.Run( |
164 |
| - base::MakeRefCounted<MockBluetoothAdvertisement>()) |
| 179 | + ? success_callback.Run(advertisement_ptr) |
165 | 180 | : failure_callback.Run(BluetoothAdvertisement::ErrorCode::
|
166 | 181 | INVALID_ADVERTISEMENT_ERROR_CODE);
|
167 | 182 | }));
|
@@ -338,4 +353,26 @@ TEST_F(FidoCableDiscoveryTest, TestDiscoveryWithAdvertisementFailures) {
|
338 | 353 | scoped_task_environment_.RunUntilIdle();
|
339 | 354 | }
|
340 | 355 |
|
| 356 | +TEST_F(FidoCableDiscoveryTest, TestUnregisterAdvertisementUponDestruction) { |
| 357 | + auto cable_discovery = CreateDiscovery(); |
| 358 | + CableMockBluetoothAdvertisement* advertisement = |
| 359 | + new CableMockBluetoothAdvertisement(); |
| 360 | + EXPECT_CALL(*advertisement, Unregister(_, _)).Times(1); |
| 361 | + |
| 362 | + ::testing::InSequence testing_sequence; |
| 363 | + auto mock_adapter = |
| 364 | + base::MakeRefCounted<::testing::NiceMock<CableMockAdapter>>(); |
| 365 | + mock_adapter->ExpectSuccessCallbackToSetPowered(); |
| 366 | + mock_adapter->ExpectRegisterAdvertisementWithResponse( |
| 367 | + true /* simulate_success */, kClientEid, kUuidFormattedClientEid, |
| 368 | + base::WrapRefCounted(advertisement)); |
| 369 | + |
| 370 | + BluetoothAdapterFactory::SetAdapterForTesting(mock_adapter); |
| 371 | + cable_discovery->Start(); |
| 372 | + scoped_task_environment_.RunUntilIdle(); |
| 373 | + |
| 374 | + EXPECT_EQ(1u, cable_discovery->advertisements_.size()); |
| 375 | + cable_discovery.reset(); |
| 376 | +} |
| 377 | + |
341 | 378 | } // namespace device
|
0 commit comments