| 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/location.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "base/test/test_pending_task.h" | 11 #include "base/test/test_pending_task.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "device/bluetooth/bluetooth_adapter_win.h" | 13 #include "device/bluetooth/bluetooth_adapter_win.h" |
| 14 #include "device/bluetooth/bluetooth_low_energy_win.h" | 14 #include "device/bluetooth/bluetooth_low_energy_win.h" |
| 15 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h" | 15 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h" |
| 16 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_win.h" | 16 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_win.h" |
| 17 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" | 17 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 void RunOnceClosure(base::OnceClosure closure) { |
| 22 std::move(closure).Run(); |
| 23 } |
| 24 |
| 21 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS( | 25 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS( |
| 22 std::string device_address) { | 26 std::string device_address) { |
| 23 BLUETOOTH_ADDRESS win_addr; | 27 BLUETOOTH_ADDRESS win_addr; |
| 24 unsigned int data[6]; | 28 unsigned int data[6]; |
| 25 int result = | 29 int result = |
| 26 sscanf_s(device_address.c_str(), "%02X:%02X:%02X:%02X:%02X:%02X", | 30 sscanf_s(device_address.c_str(), "%02X:%02X:%02X:%02X:%02X:%02X", |
| 27 &data[5], &data[4], &data[3], &data[2], &data[1], &data[0]); | 31 &data[5], &data[4], &data[3], &data[2], &data[1], &data[0]); |
| 28 CHECK(result == 6); | 32 CHECK(result == 6); |
| 29 for (int i = 0; i < 6; i++) { | 33 for (int i = 0; i < 6; i++) { |
| 30 win_addr.rgBytes[i] = data[i]; | 34 win_addr.rgBytes[i] = data[i]; |
| (...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 return fake_bt_le_wrapper_->GetSimulatedGattCharacteristic( | 473 return fake_bt_le_wrapper_->GetSimulatedGattCharacteristic( |
| 470 target_service, std::to_string(win_characteristic->GetAttributeHandle())); | 474 target_service, std::to_string(win_characteristic->GetAttributeHandle())); |
| 471 } | 475 } |
| 472 | 476 |
| 473 void BluetoothTestWin::RunPendingTasksUntilCallback() { | 477 void BluetoothTestWin::RunPendingTasksUntilCallback() { |
| 474 std::deque<base::TestPendingTask> tasks = | 478 std::deque<base::TestPendingTask> tasks = |
| 475 bluetooth_task_runner_->TakePendingTasks(); | 479 bluetooth_task_runner_->TakePendingTasks(); |
| 476 int original_callback_count = callback_count_; | 480 int original_callback_count = callback_count_; |
| 477 int original_error_callback_count = error_callback_count_; | 481 int original_error_callback_count = error_callback_count_; |
| 478 do { | 482 do { |
| 479 base::TestPendingTask task = tasks.front(); | 483 base::TestPendingTask task = std::move(tasks.front()); |
| 480 tasks.pop_front(); | 484 tasks.pop_front(); |
| 481 task.task.Run(); | 485 std::move(task.task).Run(); |
| 482 base::RunLoop().RunUntilIdle(); | 486 base::RunLoop().RunUntilIdle(); |
| 483 } while (tasks.size() && callback_count_ == original_callback_count && | 487 } while (tasks.size() && callback_count_ == original_callback_count && |
| 484 error_callback_count_ == original_error_callback_count); | 488 error_callback_count_ == original_error_callback_count); |
| 485 | 489 |
| 486 // Put the rest of pending tasks back to Bluetooth task runner. | 490 // Put the rest of pending tasks back to Bluetooth task runner. |
| 487 for (const auto& task : tasks) { | 491 for (auto& task : tasks) { |
| 492 // TODO(tzik): Remove RunOnceClosure once TaskRunner migrates from Closure |
| 493 // to OnceClosure. |
| 488 if (task.delay.is_zero()) { | 494 if (task.delay.is_zero()) { |
| 489 bluetooth_task_runner_->PostTask(task.location, task.task); | 495 bluetooth_task_runner_->PostTask( |
| 496 task.location, base::Bind(&RunOnceClosure, base::Passed(&task.task))); |
| 490 } else { | 497 } else { |
| 491 bluetooth_task_runner_->PostDelayedTask(task.location, task.task, | 498 bluetooth_task_runner_->PostDelayedTask( |
| 492 task.delay); | 499 task.location, base::Bind(&RunOnceClosure, base::Passed(&task.task)), |
| 500 task.delay); |
| 493 } | 501 } |
| 494 } | 502 } |
| 495 } | 503 } |
| 496 | 504 |
| 497 void BluetoothTestWin::ForceRefreshDevice() { | 505 void BluetoothTestWin::ForceRefreshDevice() { |
| 498 adapter_win_->force_update_device_for_test_ = true; | 506 adapter_win_->force_update_device_for_test_ = true; |
| 499 FinishPendingTasks(); | 507 FinishPendingTasks(); |
| 500 } | 508 } |
| 501 | 509 |
| 502 void BluetoothTestWin::FinishPendingTasks() { | 510 void BluetoothTestWin::FinishPendingTasks() { |
| 503 bluetooth_task_runner_->RunPendingTasks(); | 511 bluetooth_task_runner_->RunPendingTasks(); |
| 504 base::RunLoop().RunUntilIdle(); | 512 base::RunLoop().RunUntilIdle(); |
| 505 } | 513 } |
| 506 } | 514 } |
| OLD | NEW |