OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/bluetooth/test/bluetooth_test_win.h" | 5 #include "device/bluetooth/test/bluetooth_test_win.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | |
8 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
9 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
11 #include "base/test/test_pending_task.h" | |
12 #include "base/time/time.h" | |
10 #include "device/bluetooth/bluetooth_adapter_win.h" | 13 #include "device/bluetooth/bluetooth_adapter_win.h" |
11 #include "device/bluetooth/bluetooth_low_energy_win.h" | 14 #include "device/bluetooth/bluetooth_low_energy_win.h" |
12 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h" | 15 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h" |
13 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" | 16 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" |
14 | 17 |
15 namespace { | 18 namespace { |
16 | 19 |
17 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS( | 20 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS( |
18 std::string device_address) { | 21 std::string device_address) { |
19 BLUETOOTH_ADDRESS win_addr; | 22 BLUETOOTH_ADDRESS win_addr; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 void BluetoothTestWin::InitWithoutDefaultAdapter() { | 95 void BluetoothTestWin::InitWithoutDefaultAdapter() { |
93 adapter_ = new BluetoothAdapterWin(base::Bind( | 96 adapter_ = new BluetoothAdapterWin(base::Bind( |
94 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); | 97 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); |
95 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); | 98 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); |
96 adapter_win_->InitForTest(ui_task_runner_, bluetooth_task_runner_); | 99 adapter_win_->InitForTest(ui_task_runner_, bluetooth_task_runner_); |
97 } | 100 } |
98 | 101 |
99 void BluetoothTestWin::InitWithFakeAdapter() { | 102 void BluetoothTestWin::InitWithFakeAdapter() { |
100 fake_bt_classic_wrapper_ = new win::BluetoothClassicWrapperFake(); | 103 fake_bt_classic_wrapper_ = new win::BluetoothClassicWrapperFake(); |
101 fake_bt_le_wrapper_ = new win::BluetoothLowEnergyWrapperFake(); | 104 fake_bt_le_wrapper_ = new win::BluetoothLowEnergyWrapperFake(); |
105 fake_bt_le_wrapper_->AddObserver(this); | |
102 win::BluetoothClassicWrapper::SetInstanceForTest(fake_bt_classic_wrapper_); | 106 win::BluetoothClassicWrapper::SetInstanceForTest(fake_bt_classic_wrapper_); |
103 win::BluetoothLowEnergyWrapper::SetInstanceForTest(fake_bt_le_wrapper_); | 107 win::BluetoothLowEnergyWrapper::SetInstanceForTest(fake_bt_le_wrapper_); |
104 fake_bt_classic_wrapper_->SimulateARadio( | 108 fake_bt_classic_wrapper_->SimulateARadio( |
105 base::SysUTF8ToWide(kTestAdapterName), | 109 base::SysUTF8ToWide(kTestAdapterName), |
106 CanonicalStringToBLUETOOTH_ADDRESS(kTestAdapterAddress)); | 110 CanonicalStringToBLUETOOTH_ADDRESS(kTestAdapterAddress)); |
107 | 111 |
108 adapter_ = new BluetoothAdapterWin(base::Bind( | 112 adapter_ = new BluetoothAdapterWin(base::Bind( |
109 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); | 113 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); |
110 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); | 114 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); |
111 adapter_win_->InitForTest(nullptr, bluetooth_task_runner_); | 115 adapter_win_->InitForTest(nullptr, bluetooth_task_runner_); |
116 adapter_win_->GetWinBluetoothTaskManager()->AddObserver(this); | |
112 FinishPendingTasks(); | 117 FinishPendingTasks(); |
113 } | 118 } |
114 | 119 |
115 bool BluetoothTestWin::DenyPermission() { | 120 bool BluetoothTestWin::DenyPermission() { |
116 return false; | 121 return false; |
117 } | 122 } |
118 | 123 |
119 void BluetoothTestWin::StartLowEnergyDiscoverySession() { | 124 void BluetoothTestWin::StartLowEnergyDiscoverySession() { |
120 __super ::StartLowEnergyDiscoverySession(); | 125 __super ::StartLowEnergyDiscoverySession(); |
121 FinishPendingTasks(); | 126 FinishPendingTasks(); |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
275 | 280 |
276 std::string characteristic_att_handle = std::to_string( | 281 std::string characteristic_att_handle = std::to_string( |
277 static_cast<BluetoothRemoteGattCharacteristicWin*>(characteristic) | 282 static_cast<BluetoothRemoteGattCharacteristicWin*>(characteristic) |
278 ->GetAttributeHandle()); | 283 ->GetAttributeHandle()); |
279 fake_bt_le_wrapper_->SimulateGattCharacteriscRemove( | 284 fake_bt_le_wrapper_->SimulateGattCharacteriscRemove( |
280 target_service, characteristic_att_handle); | 285 target_service, characteristic_att_handle); |
281 | 286 |
282 ForceRefreshDevice(); | 287 ForceRefreshDevice(); |
283 } | 288 } |
284 | 289 |
290 void BluetoothTestWin::RememberCharacteristicForSubsequentAction( | |
291 BluetoothGattCharacteristic* characteristic) { | |
292 remembered_characteristic_ = | |
293 static_cast<BluetoothRemoteGattCharacteristicWin*>(characteristic); | |
294 } | |
295 | |
296 void BluetoothTestWin::SimulateGattCharacteristicRead( | |
297 BluetoothGattCharacteristic* characteristic, | |
298 const std::vector<uint8_t>& value) { | |
299 BluetoothGattCharacteristic* target_characteristic = characteristic; | |
300 if (target_characteristic == nullptr) | |
301 target_characteristic = remembered_characteristic_; | |
302 CHECK(target_characteristic); | |
303 | |
304 win::GattCharacteristic* target_simulated_characteristic = | |
305 GetSimulatedCharacteristic(target_characteristic); | |
306 if (target_simulated_characteristic == nullptr) | |
307 return; | |
308 | |
309 fake_bt_le_wrapper_->SimulateGattCharacteristicValue( | |
310 target_simulated_characteristic, value); | |
311 | |
312 RunPendingBluetoothTasksUntilGetCallback(); | |
313 } | |
314 | |
315 void BluetoothTestWin::SimulateGattCharacteristicReadError( | |
316 BluetoothGattCharacteristic* characteristic, | |
317 BluetoothGattService::GattErrorCode error_code) { | |
318 win::GattCharacteristic* target_characteristic = | |
319 GetSimulatedCharacteristic(characteristic); | |
320 CHECK(target_characteristic); | |
321 HRESULT hr = ERROR_SEM_TIMEOUT; | |
322 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) | |
323 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; | |
324 fake_bt_le_wrapper_->SimulateGattCharacteristicReadError( | |
325 target_characteristic, hr); | |
326 | |
327 FinishPendingTasks(); | |
328 } | |
329 | |
330 void BluetoothTestWin::SimulateGattCharacteristicWrite( | |
331 BluetoothGattCharacteristic* characteristic) { | |
332 RunPendingBluetoothTasksUntilGetCallback(); | |
333 } | |
334 | |
335 void BluetoothTestWin::SimulateGattCharacteristicWriteError( | |
336 BluetoothGattCharacteristic* characteristic, | |
337 BluetoothGattService::GattErrorCode error_code) { | |
338 win::GattCharacteristic* target_characteristic = | |
339 GetSimulatedCharacteristic(characteristic); | |
340 CHECK(target_characteristic); | |
341 HRESULT hr = ERROR_SEM_TIMEOUT; | |
342 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) | |
343 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; | |
344 fake_bt_le_wrapper_->SimulateGattCharacteristicWriteError( | |
345 target_characteristic, hr); | |
346 | |
347 FinishPendingTasks(); | |
348 } | |
349 | |
350 void BluetoothTestWin::DeleteDevice(BluetoothDevice* device) { | |
351 CHECK(device); | |
352 fake_bt_le_wrapper_->RemoveSimulatedBLEDevice(device->GetAddress()); | |
353 } | |
354 | |
285 void BluetoothTestWin::SimulateGattDescriptor( | 355 void BluetoothTestWin::SimulateGattDescriptor( |
286 BluetoothGattCharacteristic* characteristic, | 356 BluetoothGattCharacteristic* characteristic, |
287 const std::string& uuid) { | 357 const std::string& uuid) { |
288 win::GattCharacteristic* target_characteristic = | 358 win::GattCharacteristic* target_characteristic = |
289 GetSimulatedCharacteristic(characteristic); | 359 GetSimulatedCharacteristic(characteristic); |
290 CHECK(target_characteristic); | 360 CHECK(target_characteristic); |
291 fake_bt_le_wrapper_->SimulateGattDescriptor( | 361 fake_bt_le_wrapper_->SimulateGattDescriptor( |
292 characteristic->GetService()->GetDevice()->GetAddress(), | 362 characteristic->GetService()->GetDevice()->GetAddress(), |
293 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid)); | 363 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid)); |
294 ForceRefreshDevice(); | 364 ForceRefreshDevice(); |
295 } | 365 } |
296 | 366 |
367 void BluetoothTestWin::OnAttemptReadGattCharacteristic() { | |
368 gatt_read_characteristic_attempts_++; | |
369 } | |
370 | |
371 void BluetoothTestWin::OnAttemptWriteGattCharacteristic() { | |
372 gatt_write_characteristic_attempts_++; | |
373 } | |
374 | |
375 void BluetoothTestWin::onWriteGattCharacteristicValue( | |
376 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) { | |
377 last_write_value_.clear(); | |
378 for (ULONG i = 0; i < value->DataSize; i++) | |
379 last_write_value_.push_back(value->Data[i]); | |
380 } | |
381 | |
297 win::GattService* BluetoothTestWin::GetSimulatedService( | 382 win::GattService* BluetoothTestWin::GetSimulatedService( |
298 win::BLEDevice* device, | 383 win::BLEDevice* device, |
299 BluetoothGattService* service) { | 384 BluetoothGattService* service) { |
300 CHECK(device); | 385 CHECK(device); |
301 CHECK(service); | 386 CHECK(service); |
302 | 387 |
303 std::vector<std::string> chain_of_att_handles; | 388 std::vector<std::string> chain_of_att_handles; |
304 BluetoothRemoteGattServiceWin* win_service = | 389 BluetoothRemoteGattServiceWin* win_service = |
305 static_cast<BluetoothRemoteGattServiceWin*>(service); | 390 static_cast<BluetoothRemoteGattServiceWin*>(service); |
306 chain_of_att_handles.insert( | 391 chain_of_att_handles.insert( |
(...skipping 19 matching lines...) Expand all Loading... | |
326 if (target_device == nullptr) | 411 if (target_device == nullptr) |
327 return nullptr; | 412 return nullptr; |
328 win::GattService* target_service = | 413 win::GattService* target_service = |
329 GetSimulatedService(target_device, win_characteristic->GetService()); | 414 GetSimulatedService(target_device, win_characteristic->GetService()); |
330 if (target_service == nullptr) | 415 if (target_service == nullptr) |
331 return nullptr; | 416 return nullptr; |
332 return fake_bt_le_wrapper_->GetSimulatedGattCharacteristic( | 417 return fake_bt_le_wrapper_->GetSimulatedGattCharacteristic( |
333 target_service, std::to_string(win_characteristic->GetAttributeHandle())); | 418 target_service, std::to_string(win_characteristic->GetAttributeHandle())); |
334 } | 419 } |
335 | 420 |
421 void BluetoothTestWin::RunPendingBluetoothTasksUntilGetCallback() { | |
422 // Run pending Bluetooth tasks until one monitored callback get called. | |
scheib
2016/03/03 19:03:28
"gets called" or "is called"
gogerald1
2016/03/03 19:27:33
Done.
| |
423 std::deque<base::TestPendingTask> tasks = | |
424 bluetooth_task_runner_->GetPendingTasks(); | |
425 bluetooth_task_runner_->ClearPendingTasks(); | |
426 int original_callback_count = callback_count_; | |
scheib
2016/03/03 19:03:27
You'd want to also stop on an error callback.
gogerald1
2016/03/03 19:27:33
Done.
| |
427 do { | |
428 base::TestPendingTask task = tasks.front(); | |
429 tasks.pop_front(); | |
430 task.task.Run(); | |
431 base::RunLoop().RunUntilIdle(); | |
432 } while (tasks.size() && callback_count_ == original_callback_count); | |
433 | |
434 // Put the rest of pending tasks back to Bluetooth task runner. | |
435 for (const auto& task : tasks) { | |
436 if (task.delay.is_zero()) { | |
437 bluetooth_task_runner_->PostTask(task.location, task.task); | |
438 } else { | |
439 bluetooth_task_runner_->PostDelayedTask(task.location, task.task, | |
440 task.delay); | |
441 } | |
442 } | |
443 } | |
444 | |
336 void BluetoothTestWin::ForceRefreshDevice() { | 445 void BluetoothTestWin::ForceRefreshDevice() { |
337 adapter_win_->force_update_device_for_test_ = true; | 446 adapter_win_->force_update_device_for_test_ = true; |
338 FinishPendingTasks(); | 447 FinishPendingTasks(); |
339 } | 448 } |
340 | 449 |
341 void BluetoothTestWin::FinishPendingTasks() { | 450 void BluetoothTestWin::FinishPendingTasks() { |
342 bluetooth_task_runner_->RunPendingTasks(); | 451 bluetooth_task_runner_->RunPendingTasks(); |
343 base::RunLoop().RunUntilIdle(); | 452 base::RunLoop().RunUntilIdle(); |
344 } | 453 } |
345 } | 454 } |
OLD | NEW |