| 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 // TODO(tzik): Remove this after base::TaskRunner migrates from base::Closure to |
| 22 // base::OnceClosure. |
| 23 base::RepeatingClosure UnsafeConvertOnceClosureToRepeating( |
| 24 base::OnceClosure closure) { |
| 25 return base::BindRepeating( |
| 26 [](base::OnceClosure closure) { std::move(closure).Run(); }, |
| 27 base::Passed(&closure)); |
| 28 } |
| 29 |
| 21 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS( | 30 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS( |
| 22 std::string device_address) { | 31 std::string device_address) { |
| 23 BLUETOOTH_ADDRESS win_addr; | 32 BLUETOOTH_ADDRESS win_addr; |
| 24 unsigned int data[6]; | 33 unsigned int data[6]; |
| 25 int result = | 34 int result = |
| 26 sscanf_s(device_address.c_str(), "%02X:%02X:%02X:%02X:%02X:%02X", | 35 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]); | 36 &data[5], &data[4], &data[3], &data[2], &data[1], &data[0]); |
| 28 CHECK(result == 6); | 37 CHECK(result == 6); |
| 29 for (int i = 0; i < 6; i++) { | 38 for (int i = 0; i < 6; i++) { |
| 30 win_addr.rgBytes[i] = data[i]; | 39 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( | 478 return fake_bt_le_wrapper_->GetSimulatedGattCharacteristic( |
| 470 target_service, std::to_string(win_characteristic->GetAttributeHandle())); | 479 target_service, std::to_string(win_characteristic->GetAttributeHandle())); |
| 471 } | 480 } |
| 472 | 481 |
| 473 void BluetoothTestWin::RunPendingTasksUntilCallback() { | 482 void BluetoothTestWin::RunPendingTasksUntilCallback() { |
| 474 std::deque<base::TestPendingTask> tasks = | 483 std::deque<base::TestPendingTask> tasks = |
| 475 bluetooth_task_runner_->TakePendingTasks(); | 484 bluetooth_task_runner_->TakePendingTasks(); |
| 476 int original_callback_count = callback_count_; | 485 int original_callback_count = callback_count_; |
| 477 int original_error_callback_count = error_callback_count_; | 486 int original_error_callback_count = error_callback_count_; |
| 478 do { | 487 do { |
| 479 base::TestPendingTask task = tasks.front(); | 488 base::TestPendingTask task = std::move(tasks.front()); |
| 480 tasks.pop_front(); | 489 tasks.pop_front(); |
| 481 task.task.Run(); | 490 std::move(task.task).Run(); |
| 482 base::RunLoop().RunUntilIdle(); | 491 base::RunLoop().RunUntilIdle(); |
| 483 } while (tasks.size() && callback_count_ == original_callback_count && | 492 } while (tasks.size() && callback_count_ == original_callback_count && |
| 484 error_callback_count_ == original_error_callback_count); | 493 error_callback_count_ == original_error_callback_count); |
| 485 | 494 |
| 486 // Put the rest of pending tasks back to Bluetooth task runner. | 495 // Put the rest of pending tasks back to Bluetooth task runner. |
| 487 for (const auto& task : tasks) { | 496 for (auto& task : tasks) { |
| 488 if (task.delay.is_zero()) { | 497 if (task.delay.is_zero()) { |
| 489 bluetooth_task_runner_->PostTask(task.location, task.task); | 498 bluetooth_task_runner_->PostTask( |
| 499 task.location, |
| 500 UnsafeConvertOnceClosureToRepeating(std::move(task.task))); |
| 490 } else { | 501 } else { |
| 491 bluetooth_task_runner_->PostDelayedTask(task.location, task.task, | 502 bluetooth_task_runner_->PostDelayedTask( |
| 492 task.delay); | 503 task.location, |
| 504 UnsafeConvertOnceClosureToRepeating(std::move(task.task)), |
| 505 task.delay); |
| 493 } | 506 } |
| 494 } | 507 } |
| 495 } | 508 } |
| 496 | 509 |
| 497 void BluetoothTestWin::ForceRefreshDevice() { | 510 void BluetoothTestWin::ForceRefreshDevice() { |
| 498 adapter_win_->force_update_device_for_test_ = true; | 511 adapter_win_->force_update_device_for_test_ = true; |
| 499 FinishPendingTasks(); | 512 FinishPendingTasks(); |
| 500 } | 513 } |
| 501 | 514 |
| 502 void BluetoothTestWin::FinishPendingTasks() { | 515 void BluetoothTestWin::FinishPendingTasks() { |
| 503 bluetooth_task_runner_->RunPendingTasks(); | 516 bluetooth_task_runner_->RunPendingTasks(); |
| 504 base::RunLoop().RunUntilIdle(); | 517 base::RunLoop().RunUntilIdle(); |
| 505 } | 518 } |
| 506 } | 519 } |
| OLD | NEW |