1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "device/bluetooth/test/bluetooth_test.h"
6
7 #include <iterator>
8 #include <memory>
9
10 #include "base/bind.h"
11 #include "base/check.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/notreached.h"
14 #include "base/run_loop.h"
15 #include "base/test/bind.h"
16 #include "device/bluetooth/bluetooth_adapter.h"
17 #include "device/bluetooth/bluetooth_common.h"
18
19 namespace device {
20
21 BluetoothTestBase::LowEnergyDeviceData::LowEnergyDeviceData() = default;
22
23 BluetoothTestBase::LowEnergyDeviceData::LowEnergyDeviceData(
24 LowEnergyDeviceData&& data) = default;
25
26 BluetoothTestBase::LowEnergyDeviceData::~LowEnergyDeviceData() = default;
27
28 const char BluetoothTestBase::kTestAdapterName[] = "FakeBluetoothAdapter";
29 const char BluetoothTestBase::kTestAdapterAddress[] = "A1:B2:C3:D4:E5:F6";
30
31 const char BluetoothTestBase::kTestDeviceName[] = "FakeBluetoothDevice";
32 const char BluetoothTestBase::kTestDeviceNameEmpty[] = "";
33 const char BluetoothTestBase::kTestDeviceNameU2f[] = "U2F FakeDevice";
34 const char BluetoothTestBase::kTestDeviceNameCable[] = "Cable FakeDevice";
35
36 const char BluetoothTestBase::kTestDeviceAddress1[] = "01:00:00:90:1E:BE";
37 const char BluetoothTestBase::kTestDeviceAddress2[] = "02:00:00:8B:74:63";
38 const char BluetoothTestBase::kTestDeviceAddress3[] = "03:00:00:17:C0:57";
39
40 // Service UUIDs
41 const char BluetoothTestBase::kTestUUIDGenericAccess[] =
42 "00001800-0000-1000-8000-00805f9b34fb";
43 const char BluetoothTestBase::kTestUUIDGenericAttribute[] =
44 "00001801-0000-1000-8000-00805f9b34fb";
45 const char BluetoothTestBase::kTestUUIDImmediateAlert[] =
46 "00001802-0000-1000-8000-00805f9b34fb";
47 const char BluetoothTestBase::kTestUUIDLinkLoss[] =
48 "00001803-0000-1000-8000-00805f9b34fb";
49 const char BluetoothTestBase::kTestUUIDHeartRate[] =
50 "0000180d-0000-1000-8000-00805f9b34fb";
51 const char BluetoothTestBase::kTestUUIDU2f[] =
52 "0000fffd-0000-1000-8000-00805f9b34fb";
53 // Characteristic UUIDs
54 const char BluetoothTestBase::kTestUUIDDeviceName[] =
55 "00002a00-0000-1000-8000-00805f9b34fb";
56 const char BluetoothTestBase::kTestUUIDAppearance[] =
57 "00002a01-0000-1000-8000-00805f9b34fb";
58 const char BluetoothTestBase::kTestUUIDReconnectionAddress[] =
59 "00002a03-0000-1000-8000-00805f9b34fb";
60 const char BluetoothTestBase::kTestUUIDHeartRateMeasurement[] =
61 "00002a37-0000-1000-8000-00805f9b34fb";
62 const char BluetoothTestBase::kTestUUIDU2fControlPointLength[] =
63 "f1d0fff3-deaa-ecee-b42f-c9ba7ed623bb";
64 // Descriptor UUIDs
65 const char BluetoothTestBase::kTestUUIDCharacteristicUserDescription[] =
66 "00002901-0000-1000-8000-00805f9b34fb";
67 const char BluetoothTestBase::kTestUUIDClientCharacteristicConfiguration[] =
68 "00002902-0000-1000-8000-00805f9b34fb";
69 const char BluetoothTestBase::kTestUUIDServerCharacteristicConfiguration[] =
70 "00002903-0000-1000-8000-00805f9b34fb";
71 const char BluetoothTestBase::kTestUUIDCharacteristicPresentationFormat[] =
72 "00002904-0000-1000-8000-00805f9b34fb";
73 const char BluetoothTestBase::kTestUUIDCableAdvertisement[] =
74 "0000fde2-0000-1000-8000-00805f9b34fb";
75 // Manufacturer kTestAdapterAddress
76 const uint16_t BluetoothTestBase::kTestManufacturerId = 0x00E0;
77 const uint8_t BluetoothTestBase::kTestCableEid[] = {
78 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
79 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15};
80 const char BluetoothTestBase::kTestUuidFormattedClientEid[] =
81 "00010203-0405-0607-0809-101112131415";
82
BluetoothTestBase()83 BluetoothTestBase::BluetoothTestBase() {}
84
85 BluetoothTestBase::~BluetoothTestBase() = default;
StartLowEnergyDiscoverySession()86 void BluetoothTestBase::StartLowEnergyDiscoverySession() {
87 adapter_->StartDiscoverySessionWithFilter(
88 std::make_unique<BluetoothDiscoveryFilter>(BLUETOOTH_TRANSPORT_LE),
89 GetDiscoverySessionCallback(Call::EXPECTED),
90 GetErrorCallback(Call::NOT_EXPECTED));
91 base::RunLoop().RunUntilIdle();
92 }
93
StartLowEnergyDiscoverySessionExpectedToFail()94 void BluetoothTestBase::StartLowEnergyDiscoverySessionExpectedToFail() {
95 adapter_->StartDiscoverySessionWithFilter(
96 std::make_unique<BluetoothDiscoveryFilter>(BLUETOOTH_TRANSPORT_LE),
97 GetDiscoverySessionCallback(Call::NOT_EXPECTED),
98 GetErrorCallback(Call::EXPECTED));
99 base::RunLoop().RunUntilIdle();
100 }
101
TearDown()102 void BluetoothTestBase::TearDown() {
103 EXPECT_EQ(expected_success_callback_calls_, actual_success_callback_calls_);
104 EXPECT_EQ(expected_error_callback_calls_, actual_error_callback_calls_);
105 EXPECT_FALSE(unexpected_success_callback_);
106 EXPECT_FALSE(unexpected_error_callback_);
107 }
108
DenyPermission()109 bool BluetoothTestBase::DenyPermission() {
110 return false;
111 }
112
SimulateLowEnergyDevice(int device_ordinal)113 BluetoothDevice* BluetoothTestBase::SimulateLowEnergyDevice(
114 int device_ordinal) {
115 NOTIMPLEMENTED();
116 return nullptr;
117 }
118
SimulateLowEnergyDiscoveryFailure()119 void BluetoothTestBase::SimulateLowEnergyDiscoveryFailure() {
120 NOTIMPLEMENTED();
121 }
122
SimulateClassicDevice()123 BluetoothDevice* BluetoothTestBase::SimulateClassicDevice() {
124 NOTIMPLEMENTED();
125 return nullptr;
126 }
127
ConnectGatt(BluetoothDevice * device,base::Optional<BluetoothUUID> service_uuid,base::Optional<base::OnceCallback<void (BluetoothDevice *)>> simulate_callback)128 bool BluetoothTestBase::ConnectGatt(
129 BluetoothDevice* device,
130 base::Optional<BluetoothUUID> service_uuid,
131 base::Optional<base::OnceCallback<void(BluetoothDevice*)>>
132 simulate_callback) {
133 base::RunLoop run_loop;
134 base::Optional<bool> result;
135 base::Optional<std::unique_ptr<BluetoothGattConnection>> connection;
136
137 device->CreateGattConnection(
138 base::BindLambdaForTesting(
139 [&result, &connection,
140 &run_loop](std::unique_ptr<BluetoothGattConnection> new_connection) {
141 result = true;
142 connection = std::move(new_connection);
143 run_loop.Quit();
144 }),
145 base::BindLambdaForTesting(
146 [this, &result, &run_loop](BluetoothDevice::ConnectErrorCode error) {
147 result = false;
148 last_connect_error_code_ = error;
149 run_loop.Quit();
150 }),
151 std::move(service_uuid));
152
153 // Run the event loop so that the mock devices can react to the GATT
154 // connection. Some of the |Simulate*| calls depend on it.
155 base::RunLoop().RunUntilIdle();
156
157 if (simulate_callback.has_value())
158 std::move(*simulate_callback).Run(device);
159 else
160 SimulateGattConnection(device);
161
162 run_loop.Run();
163 CHECK(result.has_value());
164 if (!*result)
165 return false;
166
167 gatt_connections_.emplace_back(std::move(*connection));
168 return true;
169 }
170
GetTargetGattService(BluetoothDevice * device)171 base::Optional<BluetoothUUID> BluetoothTestBase::GetTargetGattService(
172 BluetoothDevice* device) {
173 return base::nullopt;
174 }
175
SimulateDeviceBreaksConnection(BluetoothDevice * device)176 void BluetoothTestBase::SimulateDeviceBreaksConnection(
177 BluetoothDevice* device) {
178 SimulateGattDisconnection(device);
179 }
180
SimulateLocalGattCharacteristicNotificationsRequest(BluetoothLocalGattCharacteristic * characteristic,bool start)181 bool BluetoothTestBase::SimulateLocalGattCharacteristicNotificationsRequest(
182 BluetoothLocalGattCharacteristic* characteristic,
183 bool start) {
184 NOTIMPLEMENTED();
185 return false;
186 }
187
LastNotifactionValueForCharacteristic(BluetoothLocalGattCharacteristic * characteristic)188 std::vector<uint8_t> BluetoothTestBase::LastNotifactionValueForCharacteristic(
189 BluetoothLocalGattCharacteristic* characteristic) {
190 NOTIMPLEMENTED();
191 return std::vector<uint8_t>();
192 }
193
ExpectedChangeNotifyValueAttempts(int attempts)194 void BluetoothTestBase::ExpectedChangeNotifyValueAttempts(int attempts) {
195 EXPECT_EQ(attempts, gatt_write_descriptor_attempts_);
196 EXPECT_EQ(attempts, gatt_notify_characteristic_attempts_);
197 }
198
ExpectedNotifyValue(NotifyValueState expected_value_state)199 void BluetoothTestBase::ExpectedNotifyValue(
200 NotifyValueState expected_value_state) {
201 ASSERT_EQ(2u, last_write_value_.size());
202 switch (expected_value_state) {
203 case NotifyValueState::NONE:
204 EXPECT_EQ(0, last_write_value_[0]);
205 EXPECT_EQ(0, last_write_value_[1]);
206 break;
207 case NotifyValueState::NOTIFY:
208 EXPECT_EQ(1, last_write_value_[0]);
209 EXPECT_EQ(0, last_write_value_[1]);
210 break;
211 case NotifyValueState::INDICATE:
212 EXPECT_EQ(2, last_write_value_[0]);
213 EXPECT_EQ(0, last_write_value_[1]);
214 break;
215 }
216 }
217
218 std::vector<BluetoothLocalGattService*>
RegisteredGattServices()219 BluetoothTestBase::RegisteredGattServices() {
220 NOTIMPLEMENTED();
221 return std::vector<BluetoothLocalGattService*>();
222 }
223
DeleteDevice(BluetoothDevice * device)224 void BluetoothTestBase::DeleteDevice(BluetoothDevice* device) {
225 adapter_->DeleteDeviceForTesting(device->GetAddress());
226 }
227
Callback(Call expected)228 void BluetoothTestBase::Callback(Call expected) {
229 ++callback_count_;
230
231 if (expected == Call::EXPECTED)
232 ++actual_success_callback_calls_;
233 else
234 unexpected_success_callback_ = true;
235 }
236
CreateAdvertisementCallback(Call expected,scoped_refptr<BluetoothAdvertisement> advertisement)237 void BluetoothTestBase::CreateAdvertisementCallback(
238 Call expected,
239 scoped_refptr<BluetoothAdvertisement> advertisement) {
240 ++callback_count_;
241 advertisements_.push_back(std::move(advertisement));
242
243 if (expected == Call::EXPECTED)
244 ++actual_success_callback_calls_;
245 else
246 unexpected_success_callback_ = true;
247 }
248
DiscoverySessionCallback(Call expected,std::unique_ptr<BluetoothDiscoverySession> discovery_session)249 void BluetoothTestBase::DiscoverySessionCallback(
250 Call expected,
251 std::unique_ptr<BluetoothDiscoverySession> discovery_session) {
252 ++callback_count_;
253 discovery_sessions_.push_back(std::move(discovery_session));
254
255 if (expected == Call::EXPECTED)
256 ++actual_success_callback_calls_;
257 else
258 unexpected_success_callback_ = true;
259 }
260
GattConnectionCallback(Call expected,std::unique_ptr<BluetoothGattConnection> connection)261 void BluetoothTestBase::GattConnectionCallback(
262 Call expected,
263 std::unique_ptr<BluetoothGattConnection> connection) {
264 ++callback_count_;
265 gatt_connections_.push_back(std::move(connection));
266
267 if (expected == Call::EXPECTED)
268 ++actual_success_callback_calls_;
269 else
270 unexpected_success_callback_ = true;
271 }
272
NotifyCallback(Call expected,std::unique_ptr<BluetoothGattNotifySession> notify_session)273 void BluetoothTestBase::NotifyCallback(
274 Call expected,
275 std::unique_ptr<BluetoothGattNotifySession> notify_session) {
276 notify_sessions_.push_back(std::move(notify_session));
277
278 ++callback_count_;
279 if (expected == Call::EXPECTED)
280 ++actual_success_callback_calls_;
281 else
282 unexpected_success_callback_ = true;
283 }
284
NotifyCheckForPrecedingCalls(int num_of_preceding_calls,std::unique_ptr<BluetoothGattNotifySession> notify_session)285 void BluetoothTestBase::NotifyCheckForPrecedingCalls(
286 int num_of_preceding_calls,
287 std::unique_ptr<BluetoothGattNotifySession> notify_session) {
288 EXPECT_EQ(num_of_preceding_calls, callback_count_);
289
290 notify_sessions_.push_back(std::move(notify_session));
291
292 ++callback_count_;
293 ++actual_success_callback_calls_;
294 }
295
StopNotifyCallback(Call expected)296 void BluetoothTestBase::StopNotifyCallback(Call expected) {
297 ++callback_count_;
298
299 if (expected == Call::EXPECTED)
300 ++actual_success_callback_calls_;
301 else
302 unexpected_success_callback_ = true;
303 }
304
StopNotifyCheckForPrecedingCalls(int num_of_preceding_calls)305 void BluetoothTestBase::StopNotifyCheckForPrecedingCalls(
306 int num_of_preceding_calls) {
307 EXPECT_EQ(num_of_preceding_calls, callback_count_);
308
309 ++callback_count_;
310 ++actual_success_callback_calls_;
311 }
312
ReadValueCallback(Call expected,const std::vector<uint8_t> & value)313 void BluetoothTestBase::ReadValueCallback(Call expected,
314 const std::vector<uint8_t>& value) {
315 ++callback_count_;
316 last_read_value_ = value;
317
318 if (expected == Call::EXPECTED)
319 ++actual_success_callback_calls_;
320 else
321 unexpected_success_callback_ = true;
322 }
323
ErrorCallback(Call expected)324 void BluetoothTestBase::ErrorCallback(Call expected) {
325 ++error_callback_count_;
326
327 if (expected == Call::EXPECTED)
328 ++actual_error_callback_calls_;
329 else
330 unexpected_error_callback_ = true;
331 }
332
AdvertisementErrorCallback(Call expected,BluetoothAdvertisement::ErrorCode error_code)333 void BluetoothTestBase::AdvertisementErrorCallback(
334 Call expected,
335 BluetoothAdvertisement::ErrorCode error_code) {
336 ++error_callback_count_;
337 last_advertisement_error_code_ = error_code;
338
339 if (expected == Call::EXPECTED)
340 ++actual_error_callback_calls_;
341 else
342 unexpected_error_callback_ = true;
343 }
344
ConnectErrorCallback(Call expected,enum BluetoothDevice::ConnectErrorCode error_code)345 void BluetoothTestBase::ConnectErrorCallback(
346 Call expected,
347 enum BluetoothDevice::ConnectErrorCode error_code) {
348 ++error_callback_count_;
349 last_connect_error_code_ = error_code;
350
351 if (expected == Call::EXPECTED)
352 ++actual_error_callback_calls_;
353 else
354 unexpected_error_callback_ = true;
355 }
356
GattErrorCallback(Call expected,BluetoothRemoteGattService::GattErrorCode error_code)357 void BluetoothTestBase::GattErrorCallback(
358 Call expected,
359 BluetoothRemoteGattService::GattErrorCode error_code) {
360 ++error_callback_count_;
361 last_gatt_error_code_ = error_code;
362
363 if (expected == Call::EXPECTED)
364 ++actual_error_callback_calls_;
365 else
366 unexpected_error_callback_ = true;
367 }
368
ReentrantStartNotifySessionSuccessCallback(Call expected,BluetoothRemoteGattCharacteristic * characteristic,std::unique_ptr<BluetoothGattNotifySession> notify_session)369 void BluetoothTestBase::ReentrantStartNotifySessionSuccessCallback(
370 Call expected,
371 BluetoothRemoteGattCharacteristic* characteristic,
372 std::unique_ptr<BluetoothGattNotifySession> notify_session) {
373 ++callback_count_;
374 notify_sessions_.push_back(std::move(notify_session));
375
376 if (expected == Call::EXPECTED)
377 ++actual_success_callback_calls_;
378 else
379 unexpected_success_callback_ = true;
380
381 characteristic->StartNotifySession(GetNotifyCallback(Call::EXPECTED),
382 GetGattErrorCallback(Call::NOT_EXPECTED));
383 }
384
ReentrantStartNotifySessionErrorCallback(Call expected,BluetoothRemoteGattCharacteristic * characteristic,bool error_in_reentrant,BluetoothGattService::GattErrorCode error_code)385 void BluetoothTestBase::ReentrantStartNotifySessionErrorCallback(
386 Call expected,
387 BluetoothRemoteGattCharacteristic* characteristic,
388 bool error_in_reentrant,
389 BluetoothGattService::GattErrorCode error_code) {
390 ++error_callback_count_;
391 last_gatt_error_code_ = error_code;
392
393 if (expected == Call::EXPECTED)
394 ++actual_error_callback_calls_;
395 else
396 unexpected_error_callback_ = true;
397
398 if (error_in_reentrant) {
399 SimulateGattNotifySessionStartError(
400 characteristic, BluetoothRemoteGattService::GATT_ERROR_UNKNOWN);
401 characteristic->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED),
402 GetGattErrorCallback(Call::EXPECTED));
403 } else {
404 characteristic->StartNotifySession(
405 GetNotifyCallback(Call::EXPECTED),
406 GetGattErrorCallback(Call::NOT_EXPECTED));
407 }
408 }
409
GetCallback(Call expected)410 base::OnceClosure BluetoothTestBase::GetCallback(Call expected) {
411 if (expected == Call::EXPECTED)
412 ++expected_success_callback_calls_;
413 return base::BindOnce(&BluetoothTestBase::Callback,
414 weak_factory_.GetWeakPtr(), expected);
415 }
416
417 BluetoothAdapter::CreateAdvertisementCallback
GetCreateAdvertisementCallback(Call expected)418 BluetoothTestBase::GetCreateAdvertisementCallback(Call expected) {
419 if (expected == Call::EXPECTED)
420 ++expected_success_callback_calls_;
421 return base::BindOnce(&BluetoothTestBase::CreateAdvertisementCallback,
422 weak_factory_.GetWeakPtr(), expected);
423 }
424
425 BluetoothAdapter::DiscoverySessionCallback
GetDiscoverySessionCallback(Call expected)426 BluetoothTestBase::GetDiscoverySessionCallback(Call expected) {
427 if (expected == Call::EXPECTED)
428 ++expected_success_callback_calls_;
429 return base::BindOnce(&BluetoothTestBase::DiscoverySessionCallback,
430 weak_factory_.GetWeakPtr(), expected);
431 }
432
433 BluetoothDevice::GattConnectionCallback
GetGattConnectionCallback(Call expected)434 BluetoothTestBase::GetGattConnectionCallback(Call expected) {
435 if (expected == Call::EXPECTED)
436 ++expected_success_callback_calls_;
437 return base::BindOnce(&BluetoothTestBase::GattConnectionCallback,
438 weak_factory_.GetWeakPtr(), expected);
439 }
440
441 BluetoothRemoteGattCharacteristic::NotifySessionCallback
GetNotifyCallback(Call expected)442 BluetoothTestBase::GetNotifyCallback(Call expected) {
443 if (expected == Call::EXPECTED)
444 ++expected_success_callback_calls_;
445 return base::BindOnce(&BluetoothTestBase::NotifyCallback,
446 weak_factory_.GetWeakPtr(), expected);
447 }
448
449 BluetoothRemoteGattCharacteristic::NotifySessionCallback
GetNotifyCheckForPrecedingCalls(int num_of_preceding_calls)450 BluetoothTestBase::GetNotifyCheckForPrecedingCalls(int num_of_preceding_calls) {
451 ++expected_success_callback_calls_;
452 return base::BindOnce(&BluetoothTestBase::NotifyCheckForPrecedingCalls,
453 weak_factory_.GetWeakPtr(), num_of_preceding_calls);
454 }
455
GetStopNotifyCallback(Call expected)456 base::OnceClosure BluetoothTestBase::GetStopNotifyCallback(Call expected) {
457 if (expected == Call::EXPECTED)
458 ++expected_success_callback_calls_;
459 return base::BindOnce(&BluetoothTestBase::StopNotifyCallback,
460 weak_factory_.GetWeakPtr(), expected);
461 }
462
GetStopNotifyCheckForPrecedingCalls(int num_of_preceding_calls)463 base::OnceClosure BluetoothTestBase::GetStopNotifyCheckForPrecedingCalls(
464 int num_of_preceding_calls) {
465 ++expected_success_callback_calls_;
466 return base::BindOnce(&BluetoothTestBase::StopNotifyCheckForPrecedingCalls,
467 weak_factory_.GetWeakPtr(), num_of_preceding_calls);
468 }
469
470 BluetoothRemoteGattCharacteristic::ValueCallback
GetReadValueCallback(Call expected)471 BluetoothTestBase::GetReadValueCallback(Call expected) {
472 if (expected == Call::EXPECTED)
473 ++expected_success_callback_calls_;
474 return base::BindOnce(&BluetoothTestBase::ReadValueCallback,
475 weak_factory_.GetWeakPtr(), expected);
476 }
477
GetErrorCallback(Call expected)478 BluetoothAdapter::ErrorCallback BluetoothTestBase::GetErrorCallback(
479 Call expected) {
480 if (expected == Call::EXPECTED)
481 ++expected_error_callback_calls_;
482 return base::BindOnce(&BluetoothTestBase::ErrorCallback,
483 weak_factory_.GetWeakPtr(), expected);
484 }
485
486 BluetoothAdapter::AdvertisementErrorCallback
GetAdvertisementErrorCallback(Call expected)487 BluetoothTestBase::GetAdvertisementErrorCallback(Call expected) {
488 if (expected == Call::EXPECTED)
489 ++expected_error_callback_calls_;
490 return base::BindOnce(&BluetoothTestBase::AdvertisementErrorCallback,
491 weak_factory_.GetWeakPtr(), expected);
492 }
493
494 BluetoothDevice::ConnectErrorCallback
GetConnectErrorCallback(Call expected)495 BluetoothTestBase::GetConnectErrorCallback(Call expected) {
496 if (expected == Call::EXPECTED)
497 ++expected_error_callback_calls_;
498 return base::BindOnce(&BluetoothTestBase::ConnectErrorCallback,
499 weak_factory_.GetWeakPtr(), expected);
500 }
501
502 base::OnceCallback<void(BluetoothRemoteGattService::GattErrorCode)>
GetGattErrorCallback(Call expected)503 BluetoothTestBase::GetGattErrorCallback(Call expected) {
504 if (expected == Call::EXPECTED)
505 ++expected_error_callback_calls_;
506 return base::BindOnce(&BluetoothTestBase::GattErrorCallback,
507 weak_factory_.GetWeakPtr(), expected);
508 }
509
510 BluetoothRemoteGattCharacteristic::NotifySessionCallback
GetReentrantStartNotifySessionSuccessCallback(Call expected,BluetoothRemoteGattCharacteristic * characteristic)511 BluetoothTestBase::GetReentrantStartNotifySessionSuccessCallback(
512 Call expected,
513 BluetoothRemoteGattCharacteristic* characteristic) {
514 if (expected == Call::EXPECTED)
515 ++expected_success_callback_calls_;
516 return base::BindOnce(
517 &BluetoothTestBase::ReentrantStartNotifySessionSuccessCallback,
518 weak_factory_.GetWeakPtr(), expected, characteristic);
519 }
520
521 base::OnceCallback<void(BluetoothGattService::GattErrorCode)>
GetReentrantStartNotifySessionErrorCallback(Call expected,BluetoothRemoteGattCharacteristic * characteristic,bool error_in_reentrant)522 BluetoothTestBase::GetReentrantStartNotifySessionErrorCallback(
523 Call expected,
524 BluetoothRemoteGattCharacteristic* characteristic,
525 bool error_in_reentrant) {
526 if (expected == Call::EXPECTED)
527 ++expected_error_callback_calls_;
528 return base::BindOnce(
529 &BluetoothTestBase::ReentrantStartNotifySessionErrorCallback,
530 weak_factory_.GetWeakPtr(), expected, characteristic, error_in_reentrant);
531 }
532
ResetEventCounts()533 void BluetoothTestBase::ResetEventCounts() {
534 last_advertisement_error_code_ =
535 BluetoothAdvertisement::INVALID_ADVERTISEMENT_ERROR_CODE;
536 last_connect_error_code_ = BluetoothDevice::ERROR_UNKNOWN;
537 callback_count_ = 0;
538 error_callback_count_ = 0;
539 gatt_connection_attempts_ = 0;
540 gatt_disconnection_attempts_ = 0;
541 gatt_discovery_attempts_ = 0;
542 gatt_notify_characteristic_attempts_ = 0;
543 gatt_read_characteristic_attempts_ = 0;
544 gatt_write_characteristic_attempts_ = 0;
545 gatt_read_descriptor_attempts_ = 0;
546 gatt_write_descriptor_attempts_ = 0;
547 }
548
RemoveTimedOutDevices()549 void BluetoothTestBase::RemoveTimedOutDevices() {
550 adapter_->RemoveTimedOutDevices();
551 }
552
553 BluetoothTestBase::LowEnergyDeviceData
GetLowEnergyDeviceData(int device_ordinal) const554 BluetoothTestBase::GetLowEnergyDeviceData(int device_ordinal) const {
555 LowEnergyDeviceData device_data;
556 switch (device_ordinal) {
557 case 1:
558 device_data.name = kTestDeviceName;
559 device_data.address = kTestDeviceAddress1;
560 device_data.flags = 0x04;
561 device_data.rssi = static_cast<int>(TestRSSI::LOWEST);
562 device_data.advertised_uuids = {BluetoothUUID(kTestUUIDGenericAccess),
563 BluetoothUUID(kTestUUIDGenericAttribute)};
564 device_data.service_data = {{BluetoothUUID(kTestUUIDHeartRate), {1}}};
565 device_data.manufacturer_data = {{kTestManufacturerId, {1, 2, 3, 4}}};
566 device_data.tx_power = static_cast<int>(TestTxPower::LOWEST);
567 break;
568 case 2:
569 device_data.name = kTestDeviceName;
570 device_data.address = kTestDeviceAddress1;
571 device_data.flags = 0x05;
572 device_data.rssi = static_cast<int>(TestRSSI::LOWER);
573 device_data.advertised_uuids = {BluetoothUUID(kTestUUIDImmediateAlert),
574 BluetoothUUID(kTestUUIDLinkLoss)};
575 device_data.service_data = {
576 {BluetoothUUID(kTestUUIDHeartRate), {}},
577 {BluetoothUUID(kTestUUIDImmediateAlert), {0, 2}}};
578 device_data.manufacturer_data = {{kTestManufacturerId, {}}};
579 device_data.tx_power = static_cast<int>(TestTxPower::LOWER);
580 break;
581 case 3:
582 device_data.name = kTestDeviceNameEmpty;
583 device_data.address = kTestDeviceAddress1;
584 device_data.rssi = static_cast<int>(TestRSSI::LOW);
585 break;
586 case 4:
587 device_data.name = kTestDeviceNameEmpty;
588 device_data.address = kTestDeviceAddress2;
589 device_data.rssi = static_cast<int>(TestRSSI::MEDIUM);
590 break;
591 case 5:
592 device_data.address = kTestDeviceAddress1;
593 device_data.flags = 0x06;
594 device_data.rssi = static_cast<int>(TestRSSI::HIGH);
595 break;
596 case 6:
597 device_data.name = kTestDeviceName;
598 device_data.address = kTestDeviceAddress2;
599 device_data.flags = 0x18;
600 device_data.rssi = static_cast<int>(TestRSSI::LOWEST);
601 device_data.transport = BLUETOOTH_TRANSPORT_DUAL;
602 break;
603 case 7:
604 device_data.name = kTestDeviceNameU2f;
605 device_data.address = kTestDeviceAddress1;
606 device_data.flags = 0x07;
607 device_data.rssi = static_cast<int>(TestRSSI::LOWEST);
608 device_data.advertised_uuids = {BluetoothUUID(kTestUUIDU2f)};
609 device_data.service_data = {
610 {BluetoothUUID(kTestUUIDU2fControlPointLength), {0, 20}}};
611 break;
612 case 8:
613 device_data.name = kTestDeviceNameCable;
614 device_data.address = kTestDeviceAddress1;
615 device_data.flags = 0x07;
616 device_data.rssi = static_cast<int>(TestRSSI::LOWEST);
617 device_data.service_data = {
618 {BluetoothUUID(kTestUUIDCableAdvertisement),
619 std::vector<uint8_t>(std::begin(kTestCableEid),
620 std::end(kTestCableEid))}};
621 break;
622 case 9:
623 device_data.name = kTestDeviceNameCable;
624 device_data.address = kTestDeviceAddress2;
625 device_data.flags = 0x07;
626 device_data.rssi = static_cast<int>(TestRSSI::LOWEST);
627 device_data.advertised_uuids = {
628 BluetoothUUID(kTestUUIDCableAdvertisement),
629 BluetoothUUID(kTestUuidFormattedClientEid)};
630 break;
631 default:
632 NOTREACHED();
633 }
634
635 return device_data;
636 }
637
638 } // namespace device
639