Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: device/bluetooth/test/bluetooth_test_win.cc

Issue 1749403002: Implement BluetoothRemoteGattCharacteristicWin::StartNotifySession and related unit tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: adjust comments Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_service_win.h" 17 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h"
17 18
18 namespace { 19 namespace {
19 20
20 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS( 21 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS(
21 std::string device_address) { 22 std::string device_address) {
22 BLUETOOTH_ADDRESS win_addr; 23 BLUETOOTH_ADDRESS win_addr;
23 unsigned int data[6]; 24 unsigned int data[6];
24 int result = 25 int result =
25 sscanf_s(device_address.c_str(), "%02X:%02X:%02X:%02X:%02X:%02X", 26 sscanf_s(device_address.c_str(), "%02X:%02X:%02X:%02X:%02X:%02X",
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); 115 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get());
115 adapter_win_->InitForTest(nullptr, bluetooth_task_runner_); 116 adapter_win_->InitForTest(nullptr, bluetooth_task_runner_);
116 adapter_win_->GetWinBluetoothTaskManager()->AddObserver(this); 117 adapter_win_->GetWinBluetoothTaskManager()->AddObserver(this);
117 FinishPendingTasks(); 118 FinishPendingTasks();
118 } 119 }
119 120
120 bool BluetoothTestWin::DenyPermission() { 121 bool BluetoothTestWin::DenyPermission() {
121 return false; 122 return false;
122 } 123 }
123 124
125 void BluetoothTestWin::RunUntilIdle() {
126 FinishPendingTasks();
127 }
128
124 void BluetoothTestWin::StartLowEnergyDiscoverySession() { 129 void BluetoothTestWin::StartLowEnergyDiscoverySession() {
125 __super ::StartLowEnergyDiscoverySession(); 130 __super ::StartLowEnergyDiscoverySession();
126 FinishPendingTasks(); 131 FinishPendingTasks();
127 } 132 }
128 133
129 BluetoothDevice* BluetoothTestWin::DiscoverLowEnergyDevice(int device_ordinal) { 134 BluetoothDevice* BluetoothTestWin::DiscoverLowEnergyDevice(int device_ordinal) {
130 if (device_ordinal > 4 || device_ordinal < 1) 135 if (device_ordinal > 4 || device_ordinal < 1)
131 return nullptr; 136 return nullptr;
132 137
133 std::string device_name = kTestDeviceName; 138 std::string device_name = kTestDeviceName;
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 CHECK(target_characteristic); 345 CHECK(target_characteristic);
341 HRESULT hr = ERROR_SEM_TIMEOUT; 346 HRESULT hr = ERROR_SEM_TIMEOUT;
342 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) 347 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH)
343 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; 348 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH;
344 fake_bt_le_wrapper_->SimulateGattCharacteristicWriteError( 349 fake_bt_le_wrapper_->SimulateGattCharacteristicWriteError(
345 target_characteristic, hr); 350 target_characteristic, hr);
346 351
347 FinishPendingTasks(); 352 FinishPendingTasks();
348 } 353 }
349 354
355 void BluetoothTestWin::
356 SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce(
357 BluetoothGattCharacteristic* characteristic) {
358 win::GattCharacteristic* target_characteristic =
359 GetSimulatedCharacteristic(characteristic);
360 CHECK(target_characteristic);
361 fake_bt_le_wrapper_->SimulateGattCharacteristicSetNotifyError(
362 target_characteristic, E_BLUETOOTH_ATT_UNKNOWN_ERROR);
363 }
364
350 void BluetoothTestWin::DeleteDevice(BluetoothDevice* device) { 365 void BluetoothTestWin::DeleteDevice(BluetoothDevice* device) {
351 CHECK(device); 366 CHECK(device);
352 fake_bt_le_wrapper_->RemoveSimulatedBLEDevice(device->GetAddress()); 367 fake_bt_le_wrapper_->RemoveSimulatedBLEDevice(device->GetAddress());
353 } 368 }
354 369
355 void BluetoothTestWin::SimulateGattDescriptor( 370 void BluetoothTestWin::SimulateGattDescriptor(
356 BluetoothGattCharacteristic* characteristic, 371 BluetoothGattCharacteristic* characteristic,
357 const std::string& uuid) { 372 const std::string& uuid) {
358 win::GattCharacteristic* target_characteristic = 373 win::GattCharacteristic* target_characteristic =
359 GetSimulatedCharacteristic(characteristic); 374 GetSimulatedCharacteristic(characteristic);
360 CHECK(target_characteristic); 375 CHECK(target_characteristic);
361 fake_bt_le_wrapper_->SimulateGattDescriptor( 376 fake_bt_le_wrapper_->SimulateGattDescriptor(
362 characteristic->GetService()->GetDevice()->GetAddress(), 377 characteristic->GetService()->GetDevice()->GetAddress(),
363 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid)); 378 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid));
364 ForceRefreshDevice(); 379 ForceRefreshDevice();
365 } 380 }
366 381
382 void BluetoothTestWin::SimulateGattDescriptorWriteWillFailSynchronouslyOnce(
383 BluetoothGattDescriptor* descriptor) {
384 BluetoothRemoteGattDescriptorWin* win_descriptor =
385 (BluetoothRemoteGattDescriptorWin*)descriptor;
386 win::GattCharacteristic* target_characteristic =
387 GetSimulatedCharacteristic(win_descriptor->GetCharacteristic());
388 CHECK(target_characteristic);
389 win::GattDescriptor* target_descriptor =
390 fake_bt_le_wrapper_->GetSimulatedDescriptor(
391 target_characteristic,
392 std::to_string(win_descriptor->GetAttributeHandle()));
393 CHECK(target_descriptor);
394 fake_bt_le_wrapper_->SimulateGattDescriptorWriteError(
395 target_descriptor, E_BLUETOOTH_ATT_REQUEST_NOT_SUPPORTED);
396 }
397
398 void BluetoothTestWin::SimulateGattNotifySessionStarted(
399 BluetoothGattCharacteristic* characteristic) {
400 FinishPendingTasks();
401 }
402
403 void BluetoothTestWin::SimulateGattCharacteristicChanged(
404 BluetoothGattCharacteristic* characteristic,
405 const std::vector<uint8_t>& value) {
406 BluetoothGattCharacteristic* target_characteristic = characteristic;
407 if (target_characteristic == nullptr)
408 target_characteristic = remembered_characteristic_;
409 CHECK(target_characteristic);
410
411 win::GattCharacteristic* simulated_characteristic =
412 GetSimulatedCharacteristic(target_characteristic);
413 CHECK(simulated_characteristic);
414 fake_bt_le_wrapper_->SimulateGattCharacteristicValue(simulated_characteristic,
415 value);
416 std::string device_address =
417 characteristic->GetService()->GetDevice()->GetAddress();
418 win::BLEDevice* target_device =
419 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device_address);
420 CHECK(target_device);
421 win::GattService* parent_service =
422 GetSimulatedService(target_device, characteristic->GetService());
423 CHECK(parent_service);
424 fake_bt_le_wrapper_->SimulateCharacteristicValueChangeNotification(
425 parent_service, simulated_characteristic);
426
427 FinishPendingTasks();
428 }
429
367 void BluetoothTestWin::OnAttemptReadGattCharacteristic() { 430 void BluetoothTestWin::OnAttemptReadGattCharacteristic() {
368 gatt_read_characteristic_attempts_++; 431 gatt_read_characteristic_attempts_++;
369 } 432 }
370 433
371 void BluetoothTestWin::OnAttemptWriteGattCharacteristic() { 434 void BluetoothTestWin::OnAttemptWriteGattCharacteristic() {
372 gatt_write_characteristic_attempts_++; 435 gatt_write_characteristic_attempts_++;
373 } 436 }
374 437
375 void BluetoothTestWin::onWriteGattCharacteristicValue( 438 void BluetoothTestWin::OnWriteGattCharacteristicValue(
376 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) { 439 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) {
377 last_write_value_.clear(); 440 last_write_value_.clear();
378 for (ULONG i = 0; i < value->DataSize; i++) 441 for (ULONG i = 0; i < value->DataSize; i++)
379 last_write_value_.push_back(value->Data[i]); 442 last_write_value_.push_back(value->Data[i]);
380 } 443 }
381 444
445 void BluetoothTestWin::OnWriteGattDescriptorValue(
446 const PBTH_LE_GATT_DESCRIPTOR_VALUE value) {
447 gatt_write_descriptor_attempts_++;
448 last_write_value_.clear();
449 for (ULONG i = 0; i < value->DataSize; i++)
450 last_write_value_.push_back(value->Data[i]);
451 }
452
453 void BluetoothTestWin::OnStartCharacteristicNotification() {
454 gatt_notify_characteristic_attempts_++;
455 }
456
382 win::GattService* BluetoothTestWin::GetSimulatedService( 457 win::GattService* BluetoothTestWin::GetSimulatedService(
383 win::BLEDevice* device, 458 win::BLEDevice* device,
384 BluetoothGattService* service) { 459 BluetoothGattService* service) {
385 CHECK(device); 460 CHECK(device);
386 CHECK(service); 461 CHECK(service);
387 462
388 std::vector<std::string> chain_of_att_handles; 463 std::vector<std::string> chain_of_att_handles;
389 BluetoothRemoteGattServiceWin* win_service = 464 BluetoothRemoteGattServiceWin* win_service =
390 static_cast<BluetoothRemoteGattServiceWin*>(service); 465 static_cast<BluetoothRemoteGattServiceWin*>(service);
391 chain_of_att_handles.insert( 466 chain_of_att_handles.insert(
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 void BluetoothTestWin::ForceRefreshDevice() { 521 void BluetoothTestWin::ForceRefreshDevice() {
447 adapter_win_->force_update_device_for_test_ = true; 522 adapter_win_->force_update_device_for_test_ = true;
448 FinishPendingTasks(); 523 FinishPendingTasks();
449 } 524 }
450 525
451 void BluetoothTestWin::FinishPendingTasks() { 526 void BluetoothTestWin::FinishPendingTasks() {
452 bluetooth_task_runner_->RunPendingTasks(); 527 bluetooth_task_runner_->RunPendingTasks();
453 base::RunLoop().RunUntilIdle(); 528 base::RunLoop().RunUntilIdle();
454 } 529 }
455 } 530 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698