| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_mac.h" | 5 #include "device/bluetooth/test/bluetooth_test_mac.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "device/bluetooth/bluetooth_adapter_mac.h" | 13 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 14 #include "device/bluetooth/bluetooth_device_mac.h" | 14 #include "device/bluetooth/bluetooth_device_mac.h" |
| 15 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" | 15 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" |
| 16 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_mac.h" |
| 16 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" | 17 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| 17 #include "device/bluetooth/test/mock_bluetooth_cbcharacteristic_mac.h" | 18 #include "device/bluetooth/test/mock_bluetooth_cbcharacteristic_mac.h" |
| 19 #include "device/bluetooth/test/mock_bluetooth_cbdescriptor_mac.h" |
| 18 #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" | 20 #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" |
| 19 #include "device/bluetooth/test/mock_bluetooth_cbservice_mac.h" | 21 #include "device/bluetooth/test/mock_bluetooth_cbservice_mac.h" |
| 20 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" | 22 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" |
| 21 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 23 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
| 22 #include "third_party/ocmock/OCMock/OCMock.h" | 24 #include "third_party/ocmock/OCMock/OCMock.h" |
| 23 | 25 |
| 24 #import <CoreBluetooth/CoreBluetooth.h> | 26 #import <CoreBluetooth/CoreBluetooth.h> |
| 25 | 27 |
| 26 using base::mac::ObjCCast; | 28 using base::mac::ObjCCast; |
| 27 using base::scoped_nsobject; | 29 using base::scoped_nsobject; |
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 static_cast<BluetoothRemoteGattCharacteristicMac*>(characteristic); | 441 static_cast<BluetoothRemoteGattCharacteristicMac*>(characteristic); |
| 440 CBCharacteristic* cb_characteristic = | 442 CBCharacteristic* cb_characteristic = |
| 441 characteristic_mac->GetCBCharacteristic(); | 443 characteristic_mac->GetCBCharacteristic(); |
| 442 MockCBCharacteristic* characteristic_mock = | 444 MockCBCharacteristic* characteristic_mock = |
| 443 ObjCCast<MockCBCharacteristic>(cb_characteristic); | 445 ObjCCast<MockCBCharacteristic>(cb_characteristic); |
| 444 [service_mock removeCharacteristicMock:characteristic_mock]; | 446 [service_mock removeCharacteristicMock:characteristic_mock]; |
| 445 [peripheral_mock didModifyServices:@[]]; | 447 [peripheral_mock didModifyServices:@[]]; |
| 446 [peripheral_mock mockDidDiscoverEvents]; | 448 [peripheral_mock mockDidDiscoverEvents]; |
| 447 } | 449 } |
| 448 | 450 |
| 451 void BluetoothTestMac::SimulateGattDescriptorRead( |
| 452 BluetoothRemoteGattDescriptor* descriptor, |
| 453 const std::vector<uint8_t>& value) { |
| 454 scoped_nsobject<NSData> data( |
| 455 [[NSData alloc] initWithBytes:value.data() length:value.size()]); |
| 456 [GetCBMockDescriptor(descriptor) simulateReadWithValue:data error:nil]; |
| 457 } |
| 458 |
| 459 void BluetoothTestMac::SimulateGattDescriptorReadError( |
| 460 BluetoothRemoteGattDescriptor* descriptor, |
| 461 BluetoothRemoteGattService::GattErrorCode error_code) { |
| 462 NSError* error = BluetoothDeviceMac::GetNSErrorFromGattErrorCode(error_code); |
| 463 [GetCBMockDescriptor(descriptor) simulateReadWithValue:nil error:error]; |
| 464 } |
| 465 |
| 466 void BluetoothTestMac::SimulateGattDescriptorWrite( |
| 467 BluetoothRemoteGattDescriptor* descriptor) { |
| 468 [GetCBMockDescriptor(descriptor) simulateWriteWithError:nil]; |
| 469 } |
| 470 |
| 471 void BluetoothTestMac::SimulateGattDescriptorWriteError( |
| 472 BluetoothRemoteGattDescriptor* descriptor, |
| 473 BluetoothRemoteGattService::GattErrorCode error_code) { |
| 474 NSError* error = BluetoothDeviceMac::GetNSErrorFromGattErrorCode(error_code); |
| 475 [GetCBMockDescriptor(descriptor) simulateWriteWithError:error]; |
| 476 } |
| 477 |
| 449 void BluetoothTestMac::ExpectedChangeNotifyValueAttempts(int attempts) { | 478 void BluetoothTestMac::ExpectedChangeNotifyValueAttempts(int attempts) { |
| 450 EXPECT_EQ(attempts, gatt_notify_characteristic_attempts_); | 479 EXPECT_EQ(attempts, gatt_notify_characteristic_attempts_); |
| 451 } | 480 } |
| 452 | 481 |
| 453 void BluetoothTestMac::ExpectedNotifyValue( | 482 void BluetoothTestMac::ExpectedNotifyValue( |
| 454 NotifyValueState expected_value_state) { | 483 NotifyValueState expected_value_state) { |
| 455 switch (expected_value_state) { | 484 switch (expected_value_state) { |
| 456 case NotifyValueState::NONE: | 485 case NotifyValueState::NONE: |
| 457 EXPECT_FALSE(last_notify_value_); | 486 EXPECT_FALSE(last_notify_value_); |
| 458 break; | 487 break; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 last_write_value_ = value; | 524 last_write_value_ = value; |
| 496 gatt_write_characteristic_attempts_++; | 525 gatt_write_characteristic_attempts_++; |
| 497 } | 526 } |
| 498 | 527 |
| 499 void BluetoothTest::OnFakeBluetoothGattSetCharacteristicNotification( | 528 void BluetoothTest::OnFakeBluetoothGattSetCharacteristicNotification( |
| 500 bool notify_value) { | 529 bool notify_value) { |
| 501 last_notify_value_ = notify_value; | 530 last_notify_value_ = notify_value; |
| 502 gatt_notify_characteristic_attempts_++; | 531 gatt_notify_characteristic_attempts_++; |
| 503 } | 532 } |
| 504 | 533 |
| 534 void BluetoothTest::OnFakeBluetoothDescriptorReadValue() { |
| 535 gatt_read_descriptor_attempts_++; |
| 536 } |
| 537 |
| 538 void BluetoothTest::OnFakeBluetoothDescriptorWriteValue( |
| 539 std::vector<uint8_t> value) { |
| 540 last_write_value_ = value; |
| 541 gatt_write_descriptor_attempts_++; |
| 542 } |
| 543 |
| 505 BluetoothDevice::UUIDSet | 544 BluetoothDevice::UUIDSet |
| 506 BluetoothTestMac::RetrieveConnectedPeripheralServiceUUIDs() { | 545 BluetoothTestMac::RetrieveConnectedPeripheralServiceUUIDs() { |
| 507 BluetoothDevice::UUIDSet service_uuids; | 546 BluetoothDevice::UUIDSet service_uuids; |
| 508 for (CBUUID* uuid in | 547 for (CBUUID* uuid in |
| 509 [mock_central_manager_->get() retrieveConnectedPeripheralServiceUUIDs]) { | 548 [mock_central_manager_->get() retrieveConnectedPeripheralServiceUUIDs]) { |
| 510 service_uuids.insert(BluetoothAdapterMac::BluetoothUUIDWithCBUUID(uuid)); | 549 service_uuids.insert(BluetoothAdapterMac::BluetoothUUIDWithCBUUID(uuid)); |
| 511 } | 550 } |
| 512 return service_uuids; | 551 return service_uuids; |
| 513 } | 552 } |
| 514 | 553 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 533 static_cast<BluetoothLowEnergyDeviceMac*>(device); | 572 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
| 534 CBPeripheral* cb_peripheral = device_mac->GetPeripheral(); | 573 CBPeripheral* cb_peripheral = device_mac->GetPeripheral(); |
| 535 return ObjCCast<MockCBPeripheral>(cb_peripheral); | 574 return ObjCCast<MockCBPeripheral>(cb_peripheral); |
| 536 } | 575 } |
| 537 | 576 |
| 538 MockCBPeripheral* BluetoothTestMac::GetMockCBPeripheral( | 577 MockCBPeripheral* BluetoothTestMac::GetMockCBPeripheral( |
| 539 BluetoothRemoteGattCharacteristic* characteristic) const { | 578 BluetoothRemoteGattCharacteristic* characteristic) const { |
| 540 return GetMockCBPeripheral(characteristic->GetService()); | 579 return GetMockCBPeripheral(characteristic->GetService()); |
| 541 } | 580 } |
| 542 | 581 |
| 582 MockCBPeripheral* BluetoothTestMac::GetMockCBPeripheral( |
| 583 BluetoothRemoteGattDescriptor* descriptor) const { |
| 584 return GetMockCBPeripheral(descriptor->GetCharacteristic()); |
| 585 } |
| 586 |
| 543 MockCBCharacteristic* BluetoothTest::GetCBMockCharacteristic( | 587 MockCBCharacteristic* BluetoothTest::GetCBMockCharacteristic( |
| 544 BluetoothRemoteGattCharacteristic* characteristic) const { | 588 BluetoothRemoteGattCharacteristic* characteristic) const { |
| 545 device::BluetoothRemoteGattCharacteristicMac* mac_gatt_characteristic = | 589 device::BluetoothRemoteGattCharacteristicMac* mac_gatt_characteristic = |
| 546 static_cast<device::BluetoothRemoteGattCharacteristicMac*>( | 590 static_cast<device::BluetoothRemoteGattCharacteristicMac*>( |
| 547 characteristic); | 591 characteristic); |
| 548 CBCharacteristic* cb_characteristic = | 592 CBCharacteristic* cb_characteristic = |
| 549 mac_gatt_characteristic->GetCBCharacteristic(); | 593 mac_gatt_characteristic->GetCBCharacteristic(); |
| 550 return ObjCCast<MockCBCharacteristic>(cb_characteristic); | 594 return ObjCCast<MockCBCharacteristic>(cb_characteristic); |
| 551 } | 595 } |
| 552 | 596 |
| 597 MockCBDescriptor* BluetoothTest::GetCBMockDescriptor( |
| 598 BluetoothRemoteGattDescriptor* descriptor) const { |
| 599 device::BluetoothRemoteGattDescriptorMac* mac_gatt_descriptor = |
| 600 static_cast<device::BluetoothRemoteGattDescriptorMac*>(descriptor); |
| 601 CBDescriptor* cb_descriptor = mac_gatt_descriptor->GetCBDescriptor(); |
| 602 return ObjCCast<MockCBDescriptor>(cb_descriptor); |
| 603 } |
| 604 |
| 553 void BluetoothTest::AddServicesToDevice(BluetoothDevice* device, | 605 void BluetoothTest::AddServicesToDevice(BluetoothDevice* device, |
| 554 const std::vector<std::string>& uuids) { | 606 const std::vector<std::string>& uuids) { |
| 555 scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]); | 607 scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]); |
| 556 for (auto uuid : uuids) { | 608 for (auto uuid : uuids) { |
| 557 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; | 609 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; |
| 558 [services addObject:cb_service_uuid]; | 610 [services addObject:cb_service_uuid]; |
| 559 } | 611 } |
| 560 [GetMockCBPeripheral(device) addServices:services]; | 612 [GetMockCBPeripheral(device) addServices:services]; |
| 561 } | 613 } |
| 562 | 614 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 589 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 641 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
| 590 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 642 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
| 591 // return input_str; | 643 // return input_str; |
| 592 // } | 644 // } |
| 593 // ++input[0]; | 645 // ++input[0]; |
| 594 // } | 646 // } |
| 595 // return ""; | 647 // return ""; |
| 596 // } | 648 // } |
| 597 | 649 |
| 598 } // namespace device | 650 } // namespace device |
| OLD | NEW |