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 // Execute pending Bluetooth tasks until finishing the first read GATT | |
313 // characteristic value request. | |
314 ExecutePendingBluetoothTasksUntil("PostReadGattCharacteristicValue"); | |
315 base::RunLoop().RunUntilIdle(); | |
316 } | |
317 | |
318 void BluetoothTestWin::SimulateGattCharacteristicReadError( | |
319 BluetoothGattCharacteristic* characteristic, | |
320 BluetoothGattService::GattErrorCode error_code) { | |
321 win::GattCharacteristic* target_characteristic = | |
322 GetSimulatedCharacteristic(characteristic); | |
323 CHECK(target_characteristic); | |
324 HRESULT hr = ERROR_SEM_TIMEOUT; | |
325 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) | |
326 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; | |
327 fake_bt_le_wrapper_->SimulateGattCharacteristicReadError( | |
328 target_characteristic, hr); | |
329 | |
330 FinishPendingTasks(); | |
331 } | |
332 | |
333 void BluetoothTestWin::SimulateGattCharacteristicWrite( | |
334 BluetoothGattCharacteristic* characteristic) { | |
335 // Execute pending Bluetooth tasks until finishing the first write GATT | |
336 // characteristic value request. | |
337 ExecutePendingBluetoothTasksUntil("PostWriteGattCharacteristicValue"); | |
338 base::RunLoop().RunUntilIdle(); | |
339 } | |
340 | |
341 void BluetoothTestWin::SimulateGattCharacteristicWriteError( | |
342 BluetoothGattCharacteristic* characteristic, | |
343 BluetoothGattService::GattErrorCode error_code) { | |
344 win::GattCharacteristic* target_characteristic = | |
345 GetSimulatedCharacteristic(characteristic); | |
346 CHECK(target_characteristic); | |
347 HRESULT hr = ERROR_SEM_TIMEOUT; | |
348 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) | |
349 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; | |
350 fake_bt_le_wrapper_->SimulateGattCharacteristicWriteError( | |
351 target_characteristic, hr); | |
352 | |
353 FinishPendingTasks(); | |
354 } | |
355 | |
356 void BluetoothTestWin::DeleteDevice(BluetoothDevice* device) { | |
357 CHECK(device); | |
358 fake_bt_le_wrapper_->RemoveSimulatedBLEDevice(device->GetAddress()); | |
359 } | |
360 | |
285 void BluetoothTestWin::SimulateGattDescriptor( | 361 void BluetoothTestWin::SimulateGattDescriptor( |
286 BluetoothGattCharacteristic* characteristic, | 362 BluetoothGattCharacteristic* characteristic, |
287 const std::string& uuid) { | 363 const std::string& uuid) { |
288 win::GattCharacteristic* target_characteristic = | 364 win::GattCharacteristic* target_characteristic = |
289 GetSimulatedCharacteristic(characteristic); | 365 GetSimulatedCharacteristic(characteristic); |
290 CHECK(target_characteristic); | 366 CHECK(target_characteristic); |
291 fake_bt_le_wrapper_->SimulateGattDescriptor( | 367 fake_bt_le_wrapper_->SimulateGattDescriptor( |
292 characteristic->GetService()->GetDevice()->GetAddress(), | 368 characteristic->GetService()->GetDevice()->GetAddress(), |
293 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid)); | 369 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid)); |
294 ForceRefreshDevice(); | 370 ForceRefreshDevice(); |
295 } | 371 } |
296 | 372 |
373 void BluetoothTestWin::OnAttemptReadGattCharacteristic() { | |
374 gatt_read_characteristic_attempts_++; | |
375 } | |
376 | |
377 void BluetoothTestWin::OnAttemptWriteGattCharacteristic() { | |
378 gatt_write_characteristic_attempts_++; | |
379 } | |
380 | |
381 void BluetoothTestWin::onWriteGattCharacteristicValue( | |
382 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) { | |
383 last_write_value_.clear(); | |
384 for (ULONG i = 0; i < value->DataSize; i++) | |
385 last_write_value_.push_back(value->Data[i]); | |
386 } | |
387 | |
297 win::GattService* BluetoothTestWin::GetSimulatedService( | 388 win::GattService* BluetoothTestWin::GetSimulatedService( |
298 win::BLEDevice* device, | 389 win::BLEDevice* device, |
299 BluetoothGattService* service) { | 390 BluetoothGattService* service) { |
300 CHECK(device); | 391 CHECK(device); |
301 CHECK(service); | 392 CHECK(service); |
302 | 393 |
303 std::vector<std::string> chain_of_att_handles; | 394 std::vector<std::string> chain_of_att_handles; |
304 BluetoothRemoteGattServiceWin* win_service = | 395 BluetoothRemoteGattServiceWin* win_service = |
305 static_cast<BluetoothRemoteGattServiceWin*>(service); | 396 static_cast<BluetoothRemoteGattServiceWin*>(service); |
306 chain_of_att_handles.insert( | 397 chain_of_att_handles.insert( |
(...skipping 19 matching lines...) Expand all Loading... | |
326 if (target_device == nullptr) | 417 if (target_device == nullptr) |
327 return nullptr; | 418 return nullptr; |
328 win::GattService* target_service = | 419 win::GattService* target_service = |
329 GetSimulatedService(target_device, win_characteristic->GetService()); | 420 GetSimulatedService(target_device, win_characteristic->GetService()); |
330 if (target_service == nullptr) | 421 if (target_service == nullptr) |
331 return nullptr; | 422 return nullptr; |
332 return fake_bt_le_wrapper_->GetSimulatedGattCharacteristic( | 423 return fake_bt_le_wrapper_->GetSimulatedGattCharacteristic( |
333 target_service, std::to_string(win_characteristic->GetAttributeHandle())); | 424 target_service, std::to_string(win_characteristic->GetAttributeHandle())); |
334 } | 425 } |
335 | 426 |
427 void BluetoothTestWin::ExecutePendingBluetoothTasksUntil(std::string function) { | |
428 std::deque<base::TestPendingTask> tasks = | |
429 bluetooth_task_runner_->GetPendingTasks(); | |
430 bluetooth_task_runner_->ClearPendingTasks(); | |
431 while (tasks.size()) { | |
432 base::TestPendingTask task = tasks.front(); | |
433 tasks.pop_front(); | |
434 task.task.Run(); | |
435 if (task.ToString().find(function, 0) != std::string::npos) | |
scheib
2016/03/03 00:39:42
This seems to be too brittle and indirect. The tas
gogerald1
2016/03/03 16:09:24
Done.
| |
436 break; | |
437 } | |
438 | |
439 // Put the rest of pending tasks back to Bluetooth task runner. | |
440 for (const auto& task : tasks) { | |
441 if (task.delay.is_zero()) { | |
442 bluetooth_task_runner_->PostTask(task.location, task.task); | |
443 } else { | |
444 bluetooth_task_runner_->PostDelayedTask(task.location, task.task, | |
445 task.delay); | |
446 } | |
447 } | |
448 } | |
449 | |
336 void BluetoothTestWin::ForceRefreshDevice() { | 450 void BluetoothTestWin::ForceRefreshDevice() { |
337 adapter_win_->force_update_device_for_test_ = true; | 451 adapter_win_->force_update_device_for_test_ = true; |
338 FinishPendingTasks(); | 452 FinishPendingTasks(); |
339 } | 453 } |
340 | 454 |
341 void BluetoothTestWin::FinishPendingTasks() { | 455 void BluetoothTestWin::FinishPendingTasks() { |
342 bluetooth_task_runner_->RunPendingTasks(); | 456 bluetooth_task_runner_->RunPendingTasks(); |
343 base::RunLoop().RunUntilIdle(); | 457 base::RunLoop().RunUntilIdle(); |
344 } | 458 } |
345 } | 459 } |
OLD | NEW |