| 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/strings/sys_string_conversions.h" |
| 8 #include "device/bluetooth/bluetooth_adapter_win.h" | 9 #include "device/bluetooth/bluetooth_adapter_win.h" |
| 9 #include "device/bluetooth/bluetooth_low_energy_win.h" | 10 #include "device/bluetooth/bluetooth_low_energy_win.h" |
| 10 | 11 |
| 12 namespace { |
| 13 |
| 14 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS( |
| 15 std::string device_address) { |
| 16 BLUETOOTH_ADDRESS win_addr; |
| 17 unsigned int data[6]; |
| 18 int result = |
| 19 sscanf_s(device_address.c_str(), "%02X:%02X:%02X:%02X:%02X:%02X", |
| 20 &data[5], &data[4], &data[3], &data[2], &data[1], &data[0]); |
| 21 CHECK(result == 6); |
| 22 for (int i = 0; i < 6; i++) { |
| 23 win_addr.rgBytes[i] = data[i]; |
| 24 } |
| 25 return win_addr; |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
| 11 namespace device { | 30 namespace device { |
| 12 BluetoothTestWin::BluetoothTestWin() | 31 BluetoothTestWin::BluetoothTestWin() |
| 13 : ui_task_runner_(new base::TestSimpleTaskRunner()), | 32 : ui_task_runner_(new base::TestSimpleTaskRunner()), |
| 14 bluetooth_task_runner_(new base::TestSimpleTaskRunner()) {} | 33 bluetooth_task_runner_(new base::TestSimpleTaskRunner()) {} |
| 15 BluetoothTestWin::~BluetoothTestWin() {} | 34 BluetoothTestWin::~BluetoothTestWin() {} |
| 16 | 35 |
| 17 bool BluetoothTestWin::PlatformSupportsLowEnergy() { | 36 bool BluetoothTestWin::PlatformSupportsLowEnergy() { |
| 18 return win::IsBluetoothLowEnergySupported(); | 37 return win::IsBluetoothLowEnergySupported(); |
| 19 } | 38 } |
| 20 | 39 |
| 21 void BluetoothTestWin::AdapterInitCallback() {} | 40 void BluetoothTestWin::AdapterInitCallback() {} |
| 22 | 41 |
| 23 void BluetoothTestWin::InitWithDefaultAdapter() { | 42 void BluetoothTestWin::InitWithDefaultAdapter() { |
| 24 adapter_ = new BluetoothAdapterWin(base::Bind( | 43 adapter_ = new BluetoothAdapterWin(base::Bind( |
| 25 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); | 44 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); |
| 26 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); | 45 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); |
| 27 adapter_win_->Init(); | 46 adapter_win_->Init(); |
| 28 } | 47 } |
| 29 | 48 |
| 30 void BluetoothTestWin::InitWithoutDefaultAdapter() { | 49 void BluetoothTestWin::InitWithoutDefaultAdapter() { |
| 31 adapter_ = new BluetoothAdapterWin(base::Bind( | 50 adapter_ = new BluetoothAdapterWin(base::Bind( |
| 32 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); | 51 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); |
| 33 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); | 52 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); |
| 34 adapter_win_->InitForTest(ui_task_runner_, bluetooth_task_runner_); | 53 adapter_win_->InitForTest(ui_task_runner_, bluetooth_task_runner_); |
| 35 } | 54 } |
| 36 | 55 |
| 37 void BluetoothTestWin::InitWithFakeAdapter() { | 56 void BluetoothTestWin::InitWithFakeAdapter() { |
| 57 fake_bt_classic_wrapper_ = new win::BluetoothClassicWrapperFake(); |
| 58 fake_bt_le_wrapper_ = new win::BluetoothLowEnergyWrapperFake(); |
| 59 win::BluetoothClassicWrapper::SetInstanceForTest(fake_bt_classic_wrapper_); |
| 60 win::BluetoothLowEnergyWrapper::SetInstanceForTest(fake_bt_le_wrapper_); |
| 61 fake_bt_classic_wrapper_->SimulateARadio( |
| 62 base::SysUTF8ToWide(kTestAdapterName), |
| 63 CanonicalStringToBLUETOOTH_ADDRESS(kTestAdapterAddress)); |
| 64 |
| 38 adapter_ = new BluetoothAdapterWin(base::Bind( | 65 adapter_ = new BluetoothAdapterWin(base::Bind( |
| 39 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); | 66 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); |
| 40 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); | 67 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); |
| 41 adapter_win_->InitForTest(ui_task_runner_, bluetooth_task_runner_); | 68 adapter_win_->InitForTest(ui_task_runner_, bluetooth_task_runner_); |
| 42 BluetoothTaskManagerWin::AdapterState* adapter_state = | 69 bluetooth_task_runner_->RunPendingTasks(); |
| 43 new BluetoothTaskManagerWin::AdapterState(); | 70 ui_task_runner_->RunPendingTasks(); |
| 44 adapter_state->name = kTestAdapterName; | |
| 45 adapter_state->address = kTestAdapterAddress; | |
| 46 adapter_state->powered = true; | |
| 47 adapter_win_->AdapterStateChanged(*adapter_state); | |
| 48 } | 71 } |
| 49 | 72 |
| 50 bool BluetoothTestWin::DenyPermission() { | 73 bool BluetoothTestWin::DenyPermission() { |
| 51 return false; | 74 return false; |
| 52 } | 75 } |
| 53 } | 76 } |
| OLD | NEW |