OSDN Git Service

Merge "Add Connect and Disconnect methods to LowEnergyClient"
[android-x86/system-bt.git] / service / test / low_energy_client_unittest.cpp
1 //
2 //  Copyright (C) 2015 Google, Inc.
3 //
4 //  Licensed under the Apache License, Version 2.0 (the "License");
5 //  you may not use this file except in compliance with the License.
6 //  You may obtain a copy of the License at:
7 //
8 //  http://www.apache.org/licenses/LICENSE-2.0
9 //
10 //  Unless required by applicable law or agreed to in writing, software
11 //  distributed under the License is distributed on an "AS IS" BASIS,
12 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //  See the License for the specific language governing permissions and
14 //  limitations under the License.
15 //
16
17 #include <base/macros.h>
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20
21 #include "service/adapter.h"
22 #include "service/hal/fake_bluetooth_gatt_interface.h"
23 #include "service/low_energy_client.h"
24 #include "stack/include/bt_types.h"
25 #include "stack/include/hcidefs.h"
26 #include "test/mock_adapter.h"
27
28 using ::testing::_;
29 using ::testing::Return;
30 using ::testing::Pointee;
31 using ::testing::DoAll;
32 using ::testing::Invoke;
33
34 namespace bluetooth {
35 namespace {
36
37 class MockGattHandler
38     : public hal::FakeBluetoothGattInterface::TestClientHandler {
39  public:
40   MockGattHandler() {
41     ON_CALL(*this, Scan(false))
42         .WillByDefault(Return(BT_STATUS_SUCCESS));
43   }
44   ~MockGattHandler() override = default;
45
46   MOCK_METHOD1(RegisterClient, bt_status_t(bt_uuid_t*));
47   MOCK_METHOD1(UnregisterClient, bt_status_t(int));
48   MOCK_METHOD1(Scan, bt_status_t(bool));
49   MOCK_METHOD4(Connect, bt_status_t(int , const bt_bdaddr_t *, bool, int));
50   MOCK_METHOD3(Disconnect, bt_status_t(int , const bt_bdaddr_t *, int));
51   MOCK_METHOD7(MultiAdvEnable, bt_status_t(int, int, int, int, int, int, int));
52   MOCK_METHOD10(
53       MultiAdvSetInstDataMock,
54       bt_status_t(bool, bool, bool, int, int, char*, int, char*, int, char*));
55   MOCK_METHOD1(MultiAdvDisable, bt_status_t(int));
56
57   // GMock has macros for up to 10 arguments (11 is really just too many...).
58   // For now we forward this call to a 10 argument mock, omitting the
59   // |client_if| argument.
60   bt_status_t MultiAdvSetInstData(
61       int /* client_if */,
62       bool set_scan_rsp, bool include_name,
63       bool incl_txpower, int appearance,
64       int manufacturer_len, char* manufacturer_data,
65       int service_data_len, char* service_data,
66       int service_uuid_len, char* service_uuid) override {
67     return MultiAdvSetInstDataMock(
68         set_scan_rsp, include_name, incl_txpower, appearance,
69         manufacturer_len, manufacturer_data,
70         service_data_len, service_data,
71         service_uuid_len, service_uuid);
72   }
73
74  private:
75   DISALLOW_COPY_AND_ASSIGN(MockGattHandler);
76 };
77
78 class TestDelegate : public LowEnergyClient::Delegate {
79  public:
80   TestDelegate() : scan_result_count_(0), connection_state_count_(0) {
81   }
82
83   ~TestDelegate() override = default;
84
85   int scan_result_count() const { return scan_result_count_; }
86   const ScanResult& last_scan_result() const { return last_scan_result_; }
87
88   int connection_state_count() const { return connection_state_count_; }
89
90   void OnConnectionState(LowEnergyClient* client, int status,
91                          const char* address, bool connected)  {
92     ASSERT_TRUE(client);
93     connection_state_count_++;
94   }
95
96   void OnScanResult(LowEnergyClient* client, const ScanResult& scan_result) {
97     ASSERT_TRUE(client);
98     scan_result_count_++;
99     last_scan_result_ = scan_result;
100   }
101
102  private:
103   int scan_result_count_;
104   ScanResult last_scan_result_;
105
106   int connection_state_count_;
107
108   DISALLOW_COPY_AND_ASSIGN(TestDelegate);
109 };
110
111 // Created this class for testing Advertising Data Setting
112 // It provides a work around in order to verify the arguments
113 // in the arrays passed to MultiAdvSetInstData due to mocks
114 // not having an easy way to verify entire arrays
115 class AdvertiseDataHandler : public MockGattHandler {
116  public:
117   AdvertiseDataHandler() : call_count_(0) {}
118   ~AdvertiseDataHandler() override = default;
119
120   bt_status_t MultiAdvSetInstData(
121       int /* client_if */,
122       bool set_scan_rsp, bool include_name,
123       bool incl_txpower, int appearance,
124       int manufacturer_len, char* manufacturer_data,
125       int service_data_len, char* service_data,
126       int service_uuid_len, char* service_uuid) override {
127     call_count_++;
128     service_data_.assign(
129         service_data, service_data+service_data_len);
130     manufacturer_data_.assign(
131         manufacturer_data, manufacturer_data+manufacturer_len);
132     uuid_data_.assign(
133         service_uuid, service_uuid+service_uuid_len);
134     return BT_STATUS_SUCCESS;
135   }
136
137   const std::vector<uint8_t>& manufacturer_data() const {
138     return manufacturer_data_;
139   }
140   const std::vector<uint8_t>& service_data() const { return service_data_; }
141   const std::vector<uint8_t>& uuid_data() const { return uuid_data_; }
142   int call_count() const { return call_count_; }
143
144  private:
145   int call_count_;
146   std::vector<uint8_t> manufacturer_data_;
147   std::vector<uint8_t> service_data_;
148   std::vector<uint8_t> uuid_data_;
149 };
150
151 class LowEnergyClientTest : public ::testing::Test {
152  public:
153   LowEnergyClientTest() = default;
154   ~LowEnergyClientTest() override = default;
155
156   void SetUp() override {
157     // Only set |mock_handler_| if a test hasn't set it.
158     if (!mock_handler_)
159         mock_handler_.reset(new MockGattHandler());
160     fake_hal_gatt_iface_ = new hal::FakeBluetoothGattInterface(
161         std::static_pointer_cast<
162             hal::FakeBluetoothGattInterface::TestClientHandler>(mock_handler_),
163         nullptr);
164     hal::BluetoothGattInterface::InitializeForTesting(fake_hal_gatt_iface_);
165     ble_factory_.reset(new LowEnergyClientFactory(mock_adapter_));
166   }
167
168   void TearDown() override {
169     ble_factory_.reset();
170     hal::BluetoothGattInterface::CleanUp();
171   }
172
173  protected:
174   hal::FakeBluetoothGattInterface* fake_hal_gatt_iface_;
175   testing::MockAdapter mock_adapter_;
176   std::shared_ptr<MockGattHandler> mock_handler_;
177   std::unique_ptr<LowEnergyClientFactory> ble_factory_;
178
179  private:
180   DISALLOW_COPY_AND_ASSIGN(LowEnergyClientTest);
181 };
182
183 // Used for tests that operate on a pre-registered client.
184 class LowEnergyClientPostRegisterTest : public LowEnergyClientTest {
185  public:
186   LowEnergyClientPostRegisterTest() : next_client_id_(0) {
187   }
188   ~LowEnergyClientPostRegisterTest() override = default;
189
190   void SetUp() override {
191     LowEnergyClientTest::SetUp();
192     auto callback = [&](std::unique_ptr<LowEnergyClient> client) {
193       le_client_ = std::move(client);
194     };
195     RegisterTestClient(callback);
196   }
197
198   void TearDown() override {
199     EXPECT_CALL(*mock_handler_, MultiAdvDisable(_))
200         .Times(1)
201         .WillOnce(Return(BT_STATUS_SUCCESS));
202     EXPECT_CALL(*mock_handler_, UnregisterClient(_))
203         .Times(1)
204         .WillOnce(Return(BT_STATUS_SUCCESS));
205     le_client_.reset();
206     LowEnergyClientTest::TearDown();
207   }
208
209   void RegisterTestClient(
210       const std::function<void(std::unique_ptr<LowEnergyClient> client)>
211           callback) {
212     UUID uuid = UUID::GetRandom();
213     auto api_callback = [&](BLEStatus status, const UUID& in_uuid,
214                         std::unique_ptr<BluetoothInstance> in_client) {
215       CHECK(in_uuid == uuid);
216       CHECK(in_client.get());
217       CHECK(status == BLE_STATUS_SUCCESS);
218
219       callback(std::unique_ptr<LowEnergyClient>(
220           static_cast<LowEnergyClient*>(in_client.release())));
221     };
222
223     EXPECT_CALL(*mock_handler_, RegisterClient(_))
224         .Times(1)
225         .WillOnce(Return(BT_STATUS_SUCCESS));
226
227     ble_factory_->RegisterInstance(uuid, api_callback);
228
229     bt_uuid_t hal_uuid = uuid.GetBlueDroid();
230     fake_hal_gatt_iface_->NotifyRegisterClientCallback(
231         0, next_client_id_++, hal_uuid);
232     ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
233   }
234
235   void StartAdvertising() {
236     ASSERT_FALSE(le_client_->IsAdvertisingStarted());
237     ASSERT_FALSE(le_client_->IsStartingAdvertising());
238     ASSERT_FALSE(le_client_->IsStoppingAdvertising());
239
240     EXPECT_CALL(*mock_handler_, MultiAdvEnable(_, _, _, _, _, _, _))
241         .Times(1)
242         .WillOnce(Return(BT_STATUS_SUCCESS));
243     EXPECT_CALL(*mock_handler_,
244                 MultiAdvSetInstDataMock(_, _, _, _, _, _, _, _, _, _))
245         .Times(1)
246         .WillOnce(Return(BT_STATUS_SUCCESS));
247
248     AdvertiseSettings settings;
249     AdvertiseData adv, scan_rsp;
250     ASSERT_TRUE(le_client_->StartAdvertising(
251         settings, adv, scan_rsp, LowEnergyClient::StatusCallback()));
252     ASSERT_TRUE(le_client_->IsStartingAdvertising());
253
254     fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
255         le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
256     fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
257         le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
258
259     ASSERT_TRUE(le_client_->IsAdvertisingStarted());
260     ASSERT_FALSE(le_client_->IsStartingAdvertising());
261     ASSERT_FALSE(le_client_->IsStoppingAdvertising());
262   }
263
264   void AdvertiseDataTestHelper(AdvertiseData data, std::function<void(BLEStatus)> callback) {
265     AdvertiseSettings settings;
266     EXPECT_TRUE(le_client_->StartAdvertising(
267         settings, data, AdvertiseData(), callback));
268     fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
269         le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
270     fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
271         le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
272     EXPECT_TRUE(le_client_->StopAdvertising(LowEnergyClient::StatusCallback()));
273     fake_hal_gatt_iface_->NotifyMultiAdvDisableCallback(
274         le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
275   }
276
277  protected:
278   std::unique_ptr<LowEnergyClient> le_client_;
279
280  private:
281   int next_client_id_;
282
283   DISALLOW_COPY_AND_ASSIGN(LowEnergyClientPostRegisterTest);
284 };
285
286 TEST_F(LowEnergyClientTest, RegisterInstance) {
287   EXPECT_CALL(*mock_handler_, RegisterClient(_))
288       .Times(2)
289       .WillOnce(Return(BT_STATUS_FAIL))
290       .WillOnce(Return(BT_STATUS_SUCCESS));
291
292   // These will be asynchronously populated with a result when the callback
293   // executes.
294   BLEStatus status = BLE_STATUS_SUCCESS;
295   UUID cb_uuid;
296   std::unique_ptr<LowEnergyClient> client;
297   int callback_count = 0;
298
299   auto callback = [&](BLEStatus in_status, const UUID& uuid,
300                       std::unique_ptr<BluetoothInstance> in_client) {
301         status = in_status;
302         cb_uuid = uuid;
303         client = std::unique_ptr<LowEnergyClient>(
304             static_cast<LowEnergyClient*>(in_client.release()));
305         callback_count++;
306       };
307
308   UUID uuid0 = UUID::GetRandom();
309
310   // HAL returns failure.
311   EXPECT_FALSE(ble_factory_->RegisterInstance(uuid0, callback));
312   EXPECT_EQ(0, callback_count);
313
314   // HAL returns success.
315   EXPECT_TRUE(ble_factory_->RegisterInstance(uuid0, callback));
316   EXPECT_EQ(0, callback_count);
317
318   // Calling twice with the same UUID should fail with no additional call into
319   // the stack.
320   EXPECT_FALSE(ble_factory_->RegisterInstance(uuid0, callback));
321
322   ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
323
324   // Call with a different UUID while one is pending.
325   UUID uuid1 = UUID::GetRandom();
326   EXPECT_CALL(*mock_handler_, RegisterClient(_))
327       .Times(1)
328       .WillOnce(Return(BT_STATUS_SUCCESS));
329   EXPECT_TRUE(ble_factory_->RegisterInstance(uuid1, callback));
330
331   // Trigger callback with an unknown UUID. This should get ignored.
332   UUID uuid2 = UUID::GetRandom();
333   bt_uuid_t hal_uuid = uuid2.GetBlueDroid();
334   fake_hal_gatt_iface_->NotifyRegisterClientCallback(0, 0, hal_uuid);
335   EXPECT_EQ(0, callback_count);
336
337   // |uuid0| succeeds.
338   int client_if0 = 2;  // Pick something that's not 0.
339   hal_uuid = uuid0.GetBlueDroid();
340   fake_hal_gatt_iface_->NotifyRegisterClientCallback(
341       BT_STATUS_SUCCESS, client_if0, hal_uuid);
342
343   EXPECT_EQ(1, callback_count);
344   ASSERT_TRUE(client.get() != nullptr);  // Assert to terminate in case of error
345   EXPECT_EQ(BLE_STATUS_SUCCESS, status);
346   EXPECT_EQ(client_if0, client->GetInstanceId());
347   EXPECT_EQ(uuid0, client->GetAppIdentifier());
348   EXPECT_EQ(uuid0, cb_uuid);
349
350   // The client should unregister itself when deleted.
351   EXPECT_CALL(*mock_handler_, MultiAdvDisable(client_if0))
352       .Times(1)
353       .WillOnce(Return(BT_STATUS_SUCCESS));
354   EXPECT_CALL(*mock_handler_, UnregisterClient(client_if0))
355       .Times(1)
356       .WillOnce(Return(BT_STATUS_SUCCESS));
357   client.reset();
358   ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
359
360   // |uuid1| fails.
361   int client_if1 = 3;
362   hal_uuid = uuid1.GetBlueDroid();
363   fake_hal_gatt_iface_->NotifyRegisterClientCallback(
364       BT_STATUS_FAIL, client_if1, hal_uuid);
365
366   EXPECT_EQ(2, callback_count);
367   ASSERT_TRUE(client.get() == nullptr);  // Assert to terminate in case of error
368   EXPECT_EQ(BLE_STATUS_FAILURE, status);
369   EXPECT_EQ(uuid1, cb_uuid);
370 }
371
372 TEST_F(LowEnergyClientPostRegisterTest, StartAdvertisingBasic) {
373   EXPECT_FALSE(le_client_->IsAdvertisingStarted());
374   EXPECT_FALSE(le_client_->IsStartingAdvertising());
375   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
376
377   // Use default advertising settings and data.
378   AdvertiseSettings settings;
379   AdvertiseData adv_data, scan_rsp;
380   int callback_count = 0;
381   BLEStatus last_status = BLE_STATUS_FAILURE;
382   auto callback = [&](BLEStatus status) {
383     last_status = status;
384     callback_count++;
385   };
386
387   EXPECT_CALL(*mock_handler_, MultiAdvEnable(_, _, _, _, _, _, _))
388       .Times(5)
389       .WillOnce(Return(BT_STATUS_FAIL))
390       .WillRepeatedly(Return(BT_STATUS_SUCCESS));
391
392   // Stack call returns failure.
393   EXPECT_FALSE(le_client_->StartAdvertising(
394       settings, adv_data, scan_rsp, callback));
395   EXPECT_FALSE(le_client_->IsAdvertisingStarted());
396   EXPECT_FALSE(le_client_->IsStartingAdvertising());
397   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
398   EXPECT_EQ(0, callback_count);
399
400   // Stack call returns success.
401   EXPECT_TRUE(le_client_->StartAdvertising(
402       settings, adv_data, scan_rsp, callback));
403   EXPECT_FALSE(le_client_->IsAdvertisingStarted());
404   EXPECT_TRUE(le_client_->IsStartingAdvertising());
405   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
406   EXPECT_EQ(0, callback_count);
407
408   // Already starting.
409   EXPECT_FALSE(le_client_->StartAdvertising(
410       settings, adv_data, scan_rsp, callback));
411
412   // Notify failure.
413   fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
414       le_client_->GetInstanceId(), BT_STATUS_FAIL);
415   EXPECT_FALSE(le_client_->IsAdvertisingStarted());
416   EXPECT_FALSE(le_client_->IsStartingAdvertising());
417   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
418   EXPECT_EQ(1, callback_count);
419   EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
420
421   // Try again.
422   EXPECT_TRUE(le_client_->StartAdvertising(
423       settings, adv_data, scan_rsp, callback));
424   EXPECT_FALSE(le_client_->IsAdvertisingStarted());
425   EXPECT_TRUE(le_client_->IsStartingAdvertising());
426   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
427   EXPECT_EQ(1, callback_count);
428
429   // Success notification should trigger advertise data update.
430   EXPECT_CALL(*mock_handler_,
431               MultiAdvSetInstDataMock(
432                   false,  // set_scan_rsp
433                   false,  // include_name
434                   false,  // incl_txpower
435                   _, _, _, _, _, _, _))
436       .Times(3)
437       .WillOnce(Return(BT_STATUS_FAIL))
438       .WillRepeatedly(Return(BT_STATUS_SUCCESS));
439
440   // Notify success for enable. The procedure will fail since setting data will
441   // fail.
442   fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
443       le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
444   EXPECT_FALSE(le_client_->IsAdvertisingStarted());
445   EXPECT_FALSE(le_client_->IsStartingAdvertising());
446   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
447   EXPECT_EQ(2, callback_count);
448   EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
449
450   // Try again.
451   EXPECT_TRUE(le_client_->StartAdvertising(
452       settings, adv_data, scan_rsp, callback));
453   EXPECT_FALSE(le_client_->IsAdvertisingStarted());
454   EXPECT_TRUE(le_client_->IsStartingAdvertising());
455   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
456   EXPECT_EQ(2, callback_count);
457
458   // Notify success for enable. the advertise data call should succeed but
459   // operation will remain pending.
460   fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
461       le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
462   EXPECT_FALSE(le_client_->IsAdvertisingStarted());
463   EXPECT_TRUE(le_client_->IsStartingAdvertising());
464   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
465   EXPECT_EQ(2, callback_count);
466
467   // Notify failure from advertising call.
468   fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
469       le_client_->GetInstanceId(), BT_STATUS_FAIL);
470   EXPECT_FALSE(le_client_->IsAdvertisingStarted());
471   EXPECT_FALSE(le_client_->IsStartingAdvertising());
472   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
473   EXPECT_EQ(3, callback_count);
474   EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
475
476   // Try again. Make everything succeed.
477   EXPECT_TRUE(le_client_->StartAdvertising(
478       settings, adv_data, scan_rsp, callback));
479   EXPECT_FALSE(le_client_->IsAdvertisingStarted());
480   EXPECT_TRUE(le_client_->IsStartingAdvertising());
481   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
482   EXPECT_EQ(3, callback_count);
483
484   fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
485       le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
486   fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
487       le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
488   EXPECT_TRUE(le_client_->IsAdvertisingStarted());
489   EXPECT_FALSE(le_client_->IsStartingAdvertising());
490   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
491   EXPECT_EQ(4, callback_count);
492   EXPECT_EQ(BLE_STATUS_SUCCESS, last_status);
493
494   // Already started.
495   EXPECT_FALSE(le_client_->StartAdvertising(
496       settings, adv_data, scan_rsp, callback));
497 }
498
499 TEST_F(LowEnergyClientPostRegisterTest, StopAdvertisingBasic) {
500   AdvertiseSettings settings;
501
502   // Not enabled.
503   EXPECT_FALSE(le_client_->IsAdvertisingStarted());
504   EXPECT_FALSE(le_client_->StopAdvertising(LowEnergyClient::StatusCallback()));
505
506   // Start advertising for testing.
507   StartAdvertising();
508
509   int callback_count = 0;
510   BLEStatus last_status = BLE_STATUS_FAILURE;
511   auto callback = [&](BLEStatus status) {
512     last_status = status;
513     callback_count++;
514   };
515
516   EXPECT_CALL(*mock_handler_, MultiAdvDisable(_))
517       .Times(3)
518       .WillOnce(Return(BT_STATUS_FAIL))
519       .WillRepeatedly(Return(BT_STATUS_SUCCESS));
520
521   // Stack call returns failure.
522   EXPECT_FALSE(le_client_->StopAdvertising(callback));
523   EXPECT_TRUE(le_client_->IsAdvertisingStarted());
524   EXPECT_FALSE(le_client_->IsStartingAdvertising());
525   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
526   EXPECT_EQ(0, callback_count);
527
528   // Stack returns success.
529   EXPECT_TRUE(le_client_->StopAdvertising(callback));
530   EXPECT_TRUE(le_client_->IsAdvertisingStarted());
531   EXPECT_FALSE(le_client_->IsStartingAdvertising());
532   EXPECT_TRUE(le_client_->IsStoppingAdvertising());
533   EXPECT_EQ(0, callback_count);
534
535   // Already disabling.
536   EXPECT_FALSE(le_client_->StopAdvertising(callback));
537   EXPECT_TRUE(le_client_->IsAdvertisingStarted());
538   EXPECT_FALSE(le_client_->IsStartingAdvertising());
539   EXPECT_TRUE(le_client_->IsStoppingAdvertising());
540   EXPECT_EQ(0, callback_count);
541
542   // Notify failure.
543   fake_hal_gatt_iface_->NotifyMultiAdvDisableCallback(
544       le_client_->GetInstanceId(), BT_STATUS_FAIL);
545   EXPECT_TRUE(le_client_->IsAdvertisingStarted());
546   EXPECT_FALSE(le_client_->IsStartingAdvertising());
547   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
548   EXPECT_EQ(1, callback_count);
549   EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
550
551   // Try again.
552   EXPECT_TRUE(le_client_->StopAdvertising(callback));
553   EXPECT_TRUE(le_client_->IsAdvertisingStarted());
554   EXPECT_FALSE(le_client_->IsStartingAdvertising());
555   EXPECT_TRUE(le_client_->IsStoppingAdvertising());
556   EXPECT_EQ(1, callback_count);
557
558   // Notify success.
559   fake_hal_gatt_iface_->NotifyMultiAdvDisableCallback(
560       le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
561   EXPECT_FALSE(le_client_->IsAdvertisingStarted());
562   EXPECT_FALSE(le_client_->IsStartingAdvertising());
563   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
564   EXPECT_EQ(2, callback_count);
565   EXPECT_EQ(BLE_STATUS_SUCCESS, last_status);
566
567   // Already stopped.
568   EXPECT_FALSE(le_client_->StopAdvertising(callback));
569 }
570
571 TEST_F(LowEnergyClientPostRegisterTest, InvalidAdvertiseData) {
572   const std::vector<uint8_t> data0{ 0x02, HCI_EIR_FLAGS_TYPE, 0x00 };
573   const std::vector<uint8_t> data1{
574       0x04, HCI_EIR_MANUFACTURER_SPECIFIC_TYPE, 0x01, 0x02, 0x00
575   };
576   AdvertiseData invalid_adv(data0);
577   AdvertiseData valid_adv(data1);
578
579   AdvertiseSettings settings;
580
581   EXPECT_FALSE(le_client_->StartAdvertising(
582       settings, valid_adv, invalid_adv, LowEnergyClient::StatusCallback()));
583   EXPECT_FALSE(le_client_->StartAdvertising(
584       settings, invalid_adv, valid_adv, LowEnergyClient::StatusCallback()));
585
586   // Manufacturer data not correctly formatted according to spec. We let the
587   // stack handle this case.
588   const std::vector<uint8_t> data2{ 0x01, HCI_EIR_MANUFACTURER_SPECIFIC_TYPE };
589   AdvertiseData invalid_mfc(data2);
590
591   EXPECT_CALL(*mock_handler_, MultiAdvEnable(_, _, _, _, _, _, _))
592       .Times(1)
593       .WillOnce(Return(BT_STATUS_SUCCESS));
594   EXPECT_TRUE(le_client_->StartAdvertising(
595       settings, invalid_mfc, valid_adv, LowEnergyClient::StatusCallback()));
596 }
597
598 TEST_F(LowEnergyClientPostRegisterTest, ScanResponse) {
599   EXPECT_FALSE(le_client_->IsAdvertisingStarted());
600   EXPECT_FALSE(le_client_->IsStartingAdvertising());
601   EXPECT_FALSE(le_client_->IsStoppingAdvertising());
602
603   AdvertiseSettings settings(
604       AdvertiseSettings::MODE_LOW_POWER,
605       base::TimeDelta::FromMilliseconds(300),
606       AdvertiseSettings::TX_POWER_LEVEL_MEDIUM,
607       false /* connectable */);
608
609   const std::vector<uint8_t> data0;
610   const std::vector<uint8_t> data1{
611       0x04, HCI_EIR_MANUFACTURER_SPECIFIC_TYPE, 0x01, 0x02, 0x00
612   };
613
614   int callback_count = 0;
615   BLEStatus last_status = BLE_STATUS_FAILURE;
616   auto callback = [&](BLEStatus status) {
617     last_status = status;
618     callback_count++;
619   };
620
621   AdvertiseData adv0(data0);
622   adv0.set_include_tx_power_level(true);
623
624   AdvertiseData adv1(data1);
625   adv1.set_include_device_name(true);
626
627   EXPECT_CALL(*mock_handler_,
628               MultiAdvEnable(le_client_->GetInstanceId(), _, _,
629                              kAdvertisingEventTypeScannable,
630                              _, _, _))
631       .Times(2)
632       .WillRepeatedly(Return(BT_STATUS_SUCCESS));
633   EXPECT_CALL(
634       *mock_handler_,
635       MultiAdvSetInstDataMock(
636           false,  // set_scan_rsp
637           false,  // include_name
638           true,  // incl_txpower,
639           _,
640           0,  // 0 bytes
641           _, _, _, _, _))
642       .Times(2)
643       .WillRepeatedly(Return(BT_STATUS_SUCCESS));
644   EXPECT_CALL(
645       *mock_handler_,
646       MultiAdvSetInstDataMock(
647           true,  // set_scan_rsp
648           true,  // include_name
649           false,  // incl_txpower,
650           _,
651           data1.size() - 2,  // Mfc. Specific data field bytes.
652           _, _, _, _, _))
653       .Times(2)
654       .WillRepeatedly(Return(BT_STATUS_SUCCESS));
655
656   // Enable success; Adv. data success; Scan rsp. fail.
657   EXPECT_TRUE(le_client_->StartAdvertising(settings, adv0, adv1, callback));
658   fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
659       le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
660   fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
661       le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
662   fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
663       le_client_->GetInstanceId(), BT_STATUS_FAIL);
664
665   EXPECT_EQ(1, callback_count);
666   EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
667   EXPECT_FALSE(le_client_->IsAdvertisingStarted());
668
669   // Second time everything succeeds.
670   EXPECT_TRUE(le_client_->StartAdvertising(settings, adv0, adv1, callback));
671   fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
672       le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
673   fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
674       le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
675   fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
676       le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
677
678   EXPECT_EQ(2, callback_count);
679   EXPECT_EQ(BLE_STATUS_SUCCESS, last_status);
680   EXPECT_TRUE(le_client_->IsAdvertisingStarted());
681 }
682
683 TEST_F(LowEnergyClientPostRegisterTest, AdvertiseDataParsing) {
684   // Re-initialize the test with our own custom handler.
685   TearDown();
686   std::shared_ptr<AdvertiseDataHandler> adv_handler(new AdvertiseDataHandler());
687   mock_handler_ = std::static_pointer_cast<MockGattHandler>(adv_handler);
688   SetUp();
689
690   const std::vector<uint8_t> kUUID16BitData{
691     0x03, HCI_EIR_COMPLETE_16BITS_UUID_TYPE, 0xDE, 0xAD,
692   };
693
694   const std::vector<uint8_t> kUUID32BitData{
695     0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02
696   };
697
698   const std::vector<uint8_t> kUUID128BitData{
699     0x11, HCI_EIR_COMPLETE_128BITS_UUID_TYPE,
700     0xDE, 0xAD, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
701     0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E
702   };
703
704   const std::vector<uint8_t> kMultiUUIDData{
705     0x11, HCI_EIR_COMPLETE_128BITS_UUID_TYPE,
706     0xDE, 0xAD, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
707     0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
708     0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE, 0xDE, 0xAD, 0xBE, 0xEF
709   };
710
711   const std::vector<uint8_t> kServiceData16Bit{
712     0x05, HCI_EIR_SERVICE_DATA_16BITS_UUID_TYPE, 0xDE, 0xAD, 0xBE, 0xEF
713   };
714
715   const std::vector<uint8_t> kServiceData32Bit{
716     0x07, HCI_EIR_SERVICE_DATA_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02, 0xBE, 0xEF
717   };
718
719   const std::vector<uint8_t> kServiceData128Bit{
720     0x13, HCI_EIR_SERVICE_DATA_128BITS_UUID_TYPE,
721     0xDE, 0xAD, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
722     0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0xBE, 0xEF
723   };
724
725   const std::vector<uint8_t> kMultiServiceData{
726     0x13, HCI_EIR_SERVICE_DATA_128BITS_UUID_TYPE,
727     0xDE, 0xAD, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xBE, 0xEF,
728     0xDE, 0xAD, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
729     0x05, HCI_EIR_SERVICE_DATA_16BITS_UUID_TYPE, 0xDE, 0xAD, 0xBE, 0xEF
730   };
731
732   const std::vector<uint8_t> kServiceUUIDMatch{
733     0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02,
734     0x07, HCI_EIR_SERVICE_DATA_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02, 0xBE, 0xEF
735   };
736
737   const std::vector<uint8_t> kServiceUUIDMismatch{
738     0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x01,
739     0x07, HCI_EIR_SERVICE_DATA_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02, 0xBE, 0xEF
740   };
741
742   AdvertiseData uuid_16bit_adv(kUUID16BitData);
743   AdvertiseData uuid_32bit_adv(kUUID32BitData);
744   AdvertiseData uuid_128bit_adv(kUUID128BitData);
745   AdvertiseData multi_uuid_adv(kMultiUUIDData);
746
747   AdvertiseData service_16bit_adv(kServiceData16Bit);
748   AdvertiseData service_32bit_adv(kServiceData32Bit);
749   AdvertiseData service_128bit_adv(kServiceData128Bit);
750   AdvertiseData multi_service_adv(kMultiServiceData);
751
752   AdvertiseData service_uuid_match(kServiceUUIDMatch);
753   AdvertiseData service_uuid_mismatch(kServiceUUIDMismatch);
754
755   AdvertiseSettings settings;
756
757   int callback_count = 0;
758   BLEStatus last_status = BLE_STATUS_FAILURE;
759   auto callback = [&](BLEStatus status) {
760     last_status = status;
761     callback_count++;
762   };
763
764   EXPECT_CALL(*mock_handler_, MultiAdvEnable(_, _, _, _, _, _, _))
765       .WillRepeatedly(Return(BT_STATUS_SUCCESS));
766   EXPECT_CALL(*mock_handler_, MultiAdvDisable(_))
767       .WillRepeatedly(Return(BT_STATUS_SUCCESS));
768
769   // Multiple UUID test, should fail due to only one UUID allowed
770   EXPECT_TRUE(le_client_->StartAdvertising(
771               settings, multi_uuid_adv, AdvertiseData(), callback));
772   fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
773           le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
774   EXPECT_EQ(1, callback_count);
775   EXPECT_EQ(0, adv_handler->call_count());
776   EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
777
778   // Multiple Service Data test, should fail due to only one service data allowed
779   EXPECT_TRUE(le_client_->StartAdvertising(
780               settings, multi_uuid_adv, AdvertiseData(), callback));
781   fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
782           le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
783   EXPECT_EQ(2, callback_count);
784   EXPECT_EQ(0, adv_handler->call_count());
785   EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
786
787   // 16bit uuid test, should succeed with correctly parsed uuid in little-endian
788   // 128-bit format.
789   AdvertiseDataTestHelper(uuid_16bit_adv, callback);
790   EXPECT_EQ(3, callback_count);
791   EXPECT_EQ(1, adv_handler->call_count());
792   const std::vector<uint8_t> uuid_16bit_canonical{
793     0xFB, 0x34, 0x9b, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00,
794     0xDE, 0xAD, 0x00, 0x00
795   };
796   EXPECT_EQ(uuid_16bit_canonical, adv_handler->uuid_data());
797
798   // 32bit uuid test, should succeed with correctly parsed uuid
799   AdvertiseDataTestHelper(uuid_32bit_adv, callback);
800   EXPECT_EQ(4, callback_count);
801   EXPECT_EQ(2, adv_handler->call_count());
802   const std::vector<uint8_t> uuid_32bit_canonical{
803     0xFB, 0x34, 0x9b, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00,
804     0xDE, 0xAD, 0x01, 0x02
805   };
806   EXPECT_EQ(uuid_32bit_canonical, adv_handler->uuid_data());
807
808   // 128bit uuid test, should succeed with correctly parsed uuid
809   AdvertiseDataTestHelper(uuid_128bit_adv, callback);
810   EXPECT_EQ(5, callback_count);
811   EXPECT_EQ(3, adv_handler->call_count());
812   const std::vector<uint8_t> uuid_128bit{
813     0xDE, 0xAD, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
814     0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E
815   };
816   EXPECT_EQ(uuid_128bit, adv_handler->uuid_data());
817
818   const std::vector<uint8_t> service_data{ 0xBE, 0xEF };
819
820   // Service data with 16bit uuid included, should succeed with
821   // uuid and service data parsed out
822   AdvertiseDataTestHelper(service_16bit_adv, callback);
823   EXPECT_EQ(6, callback_count);
824   EXPECT_EQ(4, adv_handler->call_count());
825   EXPECT_EQ(service_data, adv_handler->service_data());
826   EXPECT_EQ(uuid_16bit_canonical, adv_handler->uuid_data());
827
828   // Service data with 32bit uuid included, should succeed with
829   // uuid and service data parsed out
830   AdvertiseDataTestHelper(service_32bit_adv, callback);
831   EXPECT_EQ(7, callback_count);
832   EXPECT_EQ(5, adv_handler->call_count());
833   EXPECT_EQ(service_data, adv_handler->service_data());
834   EXPECT_EQ(uuid_32bit_canonical, adv_handler->uuid_data());
835
836   // Service data with 128bit uuid included, should succeed with
837   // uuid and service data parsed out
838   AdvertiseDataTestHelper(service_128bit_adv, callback);
839   EXPECT_EQ(8, callback_count);
840   EXPECT_EQ(6, adv_handler->call_count());
841   EXPECT_EQ(service_data, adv_handler->service_data());
842   EXPECT_EQ(uuid_128bit, adv_handler->uuid_data());
843
844   // Service data and UUID where the UUID for both match, should succeed.
845   AdvertiseDataTestHelper(service_uuid_match, callback);
846   EXPECT_EQ(9, callback_count);
847   EXPECT_EQ(7, adv_handler->call_count());
848   EXPECT_EQ(service_data, adv_handler->service_data());
849   EXPECT_EQ(uuid_32bit_canonical, adv_handler->uuid_data());
850
851   // Service data and UUID where the UUID for dont match, should fail
852   EXPECT_TRUE(le_client_->StartAdvertising(
853               settings, service_uuid_mismatch, AdvertiseData(), callback));
854   fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
855       le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
856   EXPECT_EQ(10, callback_count);
857   EXPECT_EQ(7, adv_handler->call_count());
858   EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
859 }
860
861 TEST_F(LowEnergyClientPostRegisterTest, ScanSettings) {
862   EXPECT_CALL(mock_adapter_, IsEnabled())
863       .WillOnce(Return(false))
864       .WillRepeatedly(Return(true));
865
866   ScanSettings settings;
867   std::vector<ScanFilter> filters;
868
869   // Adapter is not enabled.
870   EXPECT_FALSE(le_client_->StartScan(settings, filters));
871
872   // TODO(jpawlowski): add tests checking settings and filter parsing when
873   // implemented
874
875   // These should succeed and result in a HAL call
876   EXPECT_CALL(*mock_handler_, Scan(true))
877       .Times(1)
878       .WillOnce(Return(BT_STATUS_SUCCESS));
879   EXPECT_TRUE(le_client_->StartScan(settings, filters));
880
881   // These should succeed and result in a HAL call
882   EXPECT_CALL(*mock_handler_, Scan(false))
883       .Times(1)
884       .WillOnce(Return(BT_STATUS_SUCCESS));
885   EXPECT_TRUE(le_client_->StopScan());
886
887   ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
888 }
889
890 TEST_F(LowEnergyClientPostRegisterTest, ScanRecord) {
891   TestDelegate delegate;
892   le_client_->SetDelegate(&delegate);
893
894   EXPECT_EQ(0, delegate.scan_result_count());
895
896   const uint8_t kTestRecord0[] = { 0x02, 0x01, 0x00, 0x00 };
897   const uint8_t kTestRecord1[] = { 0x00 };
898   const uint8_t kTestRecord2[] = {
899     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
900     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
901     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
902     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
903     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
904     0x01, 0x00
905   };
906   const bt_bdaddr_t kTestAddress = {
907     { 0x01, 0x02, 0x03, 0x0A, 0x0B, 0x0C }
908   };
909   const char kTestAddressStr[] = "01:02:03:0A:0B:0C";
910   const int kTestRssi = 64;
911
912   // Scan wasn't started. Result should be ignored.
913   fake_hal_gatt_iface_->NotifyScanResultCallback(
914       kTestAddress, kTestRssi, (uint8_t*) kTestRecord0);
915   EXPECT_EQ(0, delegate.scan_result_count());
916
917   // Start a scan session for |le_client_|.
918   EXPECT_CALL(mock_adapter_, IsEnabled())
919       .Times(1)
920       .WillOnce(Return(true));
921   EXPECT_CALL(*mock_handler_, Scan(_))
922       .Times(2)
923       .WillOnce(Return(BT_STATUS_SUCCESS))
924       .WillOnce(Return(BT_STATUS_SUCCESS));
925   ScanSettings settings;
926   std::vector<ScanFilter> filters;
927   ASSERT_TRUE(le_client_->StartScan(settings, filters));
928
929   fake_hal_gatt_iface_->NotifyScanResultCallback(
930       kTestAddress, kTestRssi, (uint8_t*) kTestRecord0);
931   EXPECT_EQ(1, delegate.scan_result_count());
932   EXPECT_EQ(kTestAddressStr, delegate.last_scan_result().device_address());
933   EXPECT_EQ(kTestRssi, delegate.last_scan_result().rssi());
934   EXPECT_EQ(3U, delegate.last_scan_result().scan_record().size());
935
936   fake_hal_gatt_iface_->NotifyScanResultCallback(
937       kTestAddress, kTestRssi, (uint8_t*) kTestRecord1);
938   EXPECT_EQ(2, delegate.scan_result_count());
939   EXPECT_EQ(kTestAddressStr, delegate.last_scan_result().device_address());
940   EXPECT_EQ(kTestRssi, delegate.last_scan_result().rssi());
941   EXPECT_TRUE(delegate.last_scan_result().scan_record().empty());
942
943   fake_hal_gatt_iface_->NotifyScanResultCallback(
944       kTestAddress, kTestRssi, (uint8_t*) kTestRecord2);
945   EXPECT_EQ(3, delegate.scan_result_count());
946   EXPECT_EQ(kTestAddressStr, delegate.last_scan_result().device_address());
947   EXPECT_EQ(kTestRssi, delegate.last_scan_result().rssi());
948   EXPECT_EQ(62U, delegate.last_scan_result().scan_record().size());
949
950   le_client_->SetDelegate(nullptr);
951 }
952
953 MATCHER_P(BitEq, x, std::string(negation ? "isn't" : "is") +
954                         " bitwise equal to " + ::testing::PrintToString(x)) {
955   static_assert(sizeof(x) == sizeof(arg), "Size mismatch");
956   return std::memcmp(&arg, &x, sizeof(x)) == 0;
957 }
958
959 TEST_F(LowEnergyClientPostRegisterTest, Connect) {
960   const bt_bdaddr_t kTestAddress = {
961     { 0x01, 0x02, 0x03, 0x0A, 0x0B, 0x0C }
962   };
963   const char kTestAddressStr[] = "01:02:03:0A:0B:0C";
964   const bool kTestDirect = false;
965   const int connId = 12;
966
967   TestDelegate delegate;
968   le_client_->SetDelegate(&delegate);
969
970   // TODO(jpawlowski): NotifyConnectCallback should be called after returning
971   // success, fix it when it becomes important.
972   // These should succeed and result in a HAL call
973   EXPECT_CALL(*mock_handler_, Connect(le_client_->GetInstanceId(),
974           Pointee(BitEq(kTestAddress)), kTestDirect, BT_TRANSPORT_LE))
975       .Times(1)
976       .WillOnce(DoAll(
977         Invoke([&](int client_id, const bt_bdaddr_t *bd_addr, bool is_direct,
978                    int transport){
979           fake_hal_gatt_iface_->NotifyConnectCallback(connId, BT_STATUS_SUCCESS,
980                                                       client_id, *bd_addr);
981         }),
982         Return(BT_STATUS_SUCCESS)));
983
984   EXPECT_TRUE(le_client_->Connect(kTestAddressStr, kTestDirect));
985   EXPECT_EQ(1, delegate.connection_state_count());
986
987   // TODO(jpawlowski): same as above
988   // These should succeed and result in a HAL call
989   EXPECT_CALL(*mock_handler_, Disconnect(le_client_->GetInstanceId(),
990         Pointee(BitEq(kTestAddress)), connId))
991       .Times(1)
992       .WillOnce(DoAll(
993         Invoke([&](int client_id, const bt_bdaddr_t *bd_addr, int connId){
994           fake_hal_gatt_iface_->NotifyDisconnectCallback(connId,
995                                                          BT_STATUS_SUCCESS,
996                                                          client_id, *bd_addr);
997         }),
998         Return(BT_STATUS_SUCCESS)));
999
1000   EXPECT_TRUE(le_client_->Disconnect(kTestAddressStr));
1001   EXPECT_EQ(2, delegate.connection_state_count());
1002
1003   le_client_->SetDelegate(nullptr);
1004   ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
1005 }
1006
1007
1008 }  // namespace
1009 }  // namespace bluetooth