| 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 SimulateGattDescriptorReadNSData(descriptor, value); |
| 455 } |
| 456 |
| 457 void BluetoothTestMac::SimulateGattDescriptorReadError( |
| 458 BluetoothRemoteGattDescriptor* descriptor, |
| 459 BluetoothRemoteGattService::GattErrorCode error_code) { |
| 460 NSError* error = BluetoothDeviceMac::GetNSErrorFromGattErrorCode(error_code); |
| 461 [GetCBMockDescriptor(descriptor) simulateReadWithValue:nil error:error]; |
| 462 } |
| 463 |
| 464 void BluetoothTestMac::SimulateGattDescriptorWrite( |
| 465 BluetoothRemoteGattDescriptor* descriptor) { |
| 466 [GetCBMockDescriptor(descriptor) simulateWriteWithError:nil]; |
| 467 } |
| 468 |
| 469 void BluetoothTestMac::SimulateGattDescriptorWriteError( |
| 470 BluetoothRemoteGattDescriptor* descriptor, |
| 471 BluetoothRemoteGattService::GattErrorCode error_code) { |
| 472 NSError* error = BluetoothDeviceMac::GetNSErrorFromGattErrorCode(error_code); |
| 473 [GetCBMockDescriptor(descriptor) simulateWriteWithError:error]; |
| 474 } |
| 475 |
| 449 void BluetoothTestMac::ExpectedChangeNotifyValueAttempts(int attempts) { | 476 void BluetoothTestMac::ExpectedChangeNotifyValueAttempts(int attempts) { |
| 450 EXPECT_EQ(attempts, gatt_notify_characteristic_attempts_); | 477 EXPECT_EQ(attempts, gatt_notify_characteristic_attempts_); |
| 451 } | 478 } |
| 452 | 479 |
| 453 void BluetoothTestMac::ExpectedNotifyValue( | 480 void BluetoothTestMac::ExpectedNotifyValue( |
| 454 NotifyValueState expected_value_state) { | 481 NotifyValueState expected_value_state) { |
| 455 switch (expected_value_state) { | 482 switch (expected_value_state) { |
| 456 case NotifyValueState::NONE: | 483 case NotifyValueState::NONE: |
| 457 EXPECT_FALSE(last_notify_value_); | 484 EXPECT_FALSE(last_notify_value_); |
| 458 break; | 485 break; |
| 459 case NotifyValueState::NOTIFY: | 486 case NotifyValueState::NOTIFY: |
| 460 case NotifyValueState::INDICATE: | 487 case NotifyValueState::INDICATE: |
| 461 EXPECT_TRUE(last_notify_value_); | 488 EXPECT_TRUE(last_notify_value_); |
| 462 break; | 489 break; |
| 463 } | 490 } |
| 464 } | 491 } |
| 465 | 492 |
| 466 void BluetoothTestMac::SimulateDidDiscoverServices( | 493 void BluetoothTestMac::SimulateDidDiscoverServices( |
| 467 BluetoothDevice* device, | 494 BluetoothDevice* device, |
| 468 const std::vector<std::string>& uuids) { | 495 const std::vector<std::string>& uuids) { |
| 469 AddServicesToDevice(device, uuids); | 496 AddServicesToDevice(device, uuids); |
| 470 [GetMockCBPeripheral(device) mockDidDiscoverServices]; | 497 [GetMockCBPeripheral(device) mockDidDiscoverServices]; |
| 471 } | 498 } |
| 472 | 499 |
| 500 void BluetoothTestMac::SimulateGattDescriptorReadNSData( |
| 501 BluetoothRemoteGattDescriptor* descriptor, |
| 502 const std::vector<uint8_t>& value) { |
| 503 scoped_nsobject<NSData> data( |
| 504 [[NSData alloc] initWithBytes:value.data() length:value.size()]); |
| 505 [GetCBMockDescriptor(descriptor) simulateReadWithValue:data error:nil]; |
| 506 } |
| 507 |
| 508 void BluetoothTestMac::SimulateGattDescriptorReadNSString( |
| 509 BluetoothRemoteGattDescriptor* descriptor, |
| 510 const std::string& value) { |
| 511 NSString* string = base::SysUTF8ToNSString(value); |
| 512 [GetCBMockDescriptor(descriptor) simulateReadWithValue:string error:nil]; |
| 513 } |
| 514 |
| 515 void BluetoothTestMac::SimulateGattDescriptorReadNSNumber( |
| 516 BluetoothRemoteGattDescriptor* descriptor, |
| 517 short value) { |
| 518 NSNumber* number = [NSNumber numberWithShort:value]; |
| 519 [GetCBMockDescriptor(descriptor) simulateReadWithValue:number error:nil]; |
| 520 } |
| 521 |
| 473 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { | 522 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { |
| 474 gatt_connection_attempts_++; | 523 gatt_connection_attempts_++; |
| 475 } | 524 } |
| 476 | 525 |
| 477 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { | 526 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { |
| 478 gatt_disconnection_attempts_++; | 527 gatt_disconnection_attempts_++; |
| 479 } | 528 } |
| 480 | 529 |
| 481 void BluetoothTestMac::OnFakeBluetoothServiceDiscovery() { | 530 void BluetoothTestMac::OnFakeBluetoothServiceDiscovery() { |
| 482 gatt_discovery_attempts_++; | 531 gatt_discovery_attempts_++; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 495 last_write_value_ = value; | 544 last_write_value_ = value; |
| 496 gatt_write_characteristic_attempts_++; | 545 gatt_write_characteristic_attempts_++; |
| 497 } | 546 } |
| 498 | 547 |
| 499 void BluetoothTest::OnFakeBluetoothGattSetCharacteristicNotification( | 548 void BluetoothTest::OnFakeBluetoothGattSetCharacteristicNotification( |
| 500 bool notify_value) { | 549 bool notify_value) { |
| 501 last_notify_value_ = notify_value; | 550 last_notify_value_ = notify_value; |
| 502 gatt_notify_characteristic_attempts_++; | 551 gatt_notify_characteristic_attempts_++; |
| 503 } | 552 } |
| 504 | 553 |
| 554 void BluetoothTest::OnFakeBluetoothDescriptorReadValue() { |
| 555 gatt_read_descriptor_attempts_++; |
| 556 } |
| 557 |
| 558 void BluetoothTest::OnFakeBluetoothDescriptorWriteValue( |
| 559 std::vector<uint8_t> value) { |
| 560 last_write_value_ = value; |
| 561 gatt_write_descriptor_attempts_++; |
| 562 } |
| 563 |
| 505 BluetoothDevice::UUIDSet | 564 BluetoothDevice::UUIDSet |
| 506 BluetoothTestMac::RetrieveConnectedPeripheralServiceUUIDs() { | 565 BluetoothTestMac::RetrieveConnectedPeripheralServiceUUIDs() { |
| 507 BluetoothDevice::UUIDSet service_uuids; | 566 BluetoothDevice::UUIDSet service_uuids; |
| 508 for (CBUUID* uuid in | 567 for (CBUUID* uuid in |
| 509 [mock_central_manager_->get() retrieveConnectedPeripheralServiceUUIDs]) { | 568 [mock_central_manager_->get() retrieveConnectedPeripheralServiceUUIDs]) { |
| 510 service_uuids.insert(BluetoothAdapterMac::BluetoothUUIDWithCBUUID(uuid)); | 569 service_uuids.insert(BluetoothAdapterMac::BluetoothUUIDWithCBUUID(uuid)); |
| 511 } | 570 } |
| 512 return service_uuids; | 571 return service_uuids; |
| 513 } | 572 } |
| 514 | 573 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 533 static_cast<BluetoothLowEnergyDeviceMac*>(device); | 592 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
| 534 CBPeripheral* cb_peripheral = device_mac->GetPeripheral(); | 593 CBPeripheral* cb_peripheral = device_mac->GetPeripheral(); |
| 535 return ObjCCast<MockCBPeripheral>(cb_peripheral); | 594 return ObjCCast<MockCBPeripheral>(cb_peripheral); |
| 536 } | 595 } |
| 537 | 596 |
| 538 MockCBPeripheral* BluetoothTestMac::GetMockCBPeripheral( | 597 MockCBPeripheral* BluetoothTestMac::GetMockCBPeripheral( |
| 539 BluetoothRemoteGattCharacteristic* characteristic) const { | 598 BluetoothRemoteGattCharacteristic* characteristic) const { |
| 540 return GetMockCBPeripheral(characteristic->GetService()); | 599 return GetMockCBPeripheral(characteristic->GetService()); |
| 541 } | 600 } |
| 542 | 601 |
| 602 MockCBPeripheral* BluetoothTestMac::GetMockCBPeripheral( |
| 603 BluetoothRemoteGattDescriptor* descriptor) const { |
| 604 return GetMockCBPeripheral(descriptor->GetCharacteristic()); |
| 605 } |
| 606 |
| 543 MockCBCharacteristic* BluetoothTest::GetCBMockCharacteristic( | 607 MockCBCharacteristic* BluetoothTest::GetCBMockCharacteristic( |
| 544 BluetoothRemoteGattCharacteristic* characteristic) const { | 608 BluetoothRemoteGattCharacteristic* characteristic) const { |
| 545 device::BluetoothRemoteGattCharacteristicMac* mac_gatt_characteristic = | 609 device::BluetoothRemoteGattCharacteristicMac* mac_gatt_characteristic = |
| 546 static_cast<device::BluetoothRemoteGattCharacteristicMac*>( | 610 static_cast<device::BluetoothRemoteGattCharacteristicMac*>( |
| 547 characteristic); | 611 characteristic); |
| 548 CBCharacteristic* cb_characteristic = | 612 CBCharacteristic* cb_characteristic = |
| 549 mac_gatt_characteristic->GetCBCharacteristic(); | 613 mac_gatt_characteristic->GetCBCharacteristic(); |
| 550 return ObjCCast<MockCBCharacteristic>(cb_characteristic); | 614 return ObjCCast<MockCBCharacteristic>(cb_characteristic); |
| 551 } | 615 } |
| 552 | 616 |
| 617 MockCBDescriptor* BluetoothTest::GetCBMockDescriptor( |
| 618 BluetoothRemoteGattDescriptor* descriptor) const { |
| 619 device::BluetoothRemoteGattDescriptorMac* mac_gatt_descriptor = |
| 620 static_cast<device::BluetoothRemoteGattDescriptorMac*>(descriptor); |
| 621 CBDescriptor* cb_descriptor = mac_gatt_descriptor->GetCBDescriptor(); |
| 622 return ObjCCast<MockCBDescriptor>(cb_descriptor); |
| 623 } |
| 624 |
| 553 void BluetoothTest::AddServicesToDevice(BluetoothDevice* device, | 625 void BluetoothTest::AddServicesToDevice(BluetoothDevice* device, |
| 554 const std::vector<std::string>& uuids) { | 626 const std::vector<std::string>& uuids) { |
| 555 scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]); | 627 scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]); |
| 556 for (auto uuid : uuids) { | 628 for (auto uuid : uuids) { |
| 557 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; | 629 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; |
| 558 [services addObject:cb_service_uuid]; | 630 [services addObject:cb_service_uuid]; |
| 559 } | 631 } |
| 560 [GetMockCBPeripheral(device) addServices:services]; | 632 [GetMockCBPeripheral(device) addServices:services]; |
| 561 } | 633 } |
| 562 | 634 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 589 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 661 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
| 590 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 662 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
| 591 // return input_str; | 663 // return input_str; |
| 592 // } | 664 // } |
| 593 // ++input[0]; | 665 // ++input[0]; |
| 594 // } | 666 // } |
| 595 // return ""; | 667 // return ""; |
| 596 // } | 668 // } |
| 597 | 669 |
| 598 } // namespace device | 670 } // namespace device |
| OLD | NEW |