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

Side by Side Diff: device/bluetooth/test/bluetooth_test_mac.mm

Issue 2638653002: Bluetooth: macOS: DidModifyServices can happens while scanning (Closed)
Patch Set: Adding comments and Mac suffix Created 3 years, 7 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 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"
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 NSError* error = BluetoothDeviceMac::GetNSErrorFromGattErrorCode(error_code); 372 NSError* error = BluetoothDeviceMac::GetNSErrorFromGattErrorCode(error_code);
373 [characteristic_mock simulateWriteWithError:error]; 373 [characteristic_mock simulateWriteWithError:error];
374 } 374 }
375 375
376 void BluetoothTestMac::SimulateGattDescriptor( 376 void BluetoothTestMac::SimulateGattDescriptor(
377 BluetoothRemoteGattCharacteristic* characteristic, 377 BluetoothRemoteGattCharacteristic* characteristic,
378 const std::string& uuid) { 378 const std::string& uuid) {
379 MockCBCharacteristic* characteristic_mock = 379 MockCBCharacteristic* characteristic_mock =
380 GetCBMockCharacteristic(characteristic); 380 GetCBMockCharacteristic(characteristic);
381 CBUUID* cb_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; 381 CBUUID* cb_uuid = [CBUUID UUIDWithString:@(uuid.c_str())];
382 [characteristic_mock simulateDescriptorWithUUID:cb_uuid]; 382 [characteristic_mock addDescriptorWithUUID:cb_uuid];
383 MockCBPeripheral* peripheral_mock = GetMockCBPeripheral(characteristic); 383 MockCBPeripheral* peripheral_mock = GetMockCBPeripheral(characteristic);
384 [peripheral_mock didModifyServices:@[]]; 384 [peripheral_mock didModifyServices:@[]];
385 // After -[MockCBPeripheral didModifyServices:], BluetoothLowEnergyDeviceMac 385 // After -[MockCBPeripheral didModifyServices:], BluetoothLowEnergyDeviceMac
386 // is expected to call -[CBPeripheral discoverServices:] 386 // is expected to call -[CBPeripheral discoverServices:]
387 [peripheral_mock mockDidDiscoverEvents]; 387 [peripheral_mock mockDidDiscoverEvents];
388 } 388 }
389 389
390 void BluetoothTestMac::SimulateGattNotifySessionStarted( 390 void BluetoothTestMac::SimulateGattNotifySessionStarted(
391 BluetoothRemoteGattCharacteristic* characteristic) { 391 BluetoothRemoteGattCharacteristic* characteristic) {
392 MockCBCharacteristic* characteristic_mock = 392 MockCBCharacteristic* characteristic_mock =
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 MockCBCharacteristic* characteristic_mock = 444 MockCBCharacteristic* characteristic_mock =
445 ObjCCast<MockCBCharacteristic>(cb_characteristic); 445 ObjCCast<MockCBCharacteristic>(cb_characteristic);
446 [service_mock removeCharacteristicMock:characteristic_mock]; 446 [service_mock removeCharacteristicMock:characteristic_mock];
447 [peripheral_mock didModifyServices:@[]]; 447 [peripheral_mock didModifyServices:@[]];
448 [peripheral_mock mockDidDiscoverEvents]; 448 [peripheral_mock mockDidDiscoverEvents];
449 } 449 }
450 450
451 void BluetoothTestMac::SimulateGattDescriptorRead( 451 void BluetoothTestMac::SimulateGattDescriptorRead(
452 BluetoothRemoteGattDescriptor* descriptor, 452 BluetoothRemoteGattDescriptor* descriptor,
453 const std::vector<uint8_t>& value) { 453 const std::vector<uint8_t>& value) {
454 SimulateGattDescriptorReadNSData(descriptor, value); 454 SimulateGattDescriptorReadNSDataMac(descriptor, value);
455 } 455 }
456 456
457 void BluetoothTestMac::SimulateGattDescriptorReadError( 457 void BluetoothTestMac::SimulateGattDescriptorReadError(
458 BluetoothRemoteGattDescriptor* descriptor, 458 BluetoothRemoteGattDescriptor* descriptor,
459 BluetoothRemoteGattService::GattErrorCode error_code) { 459 BluetoothRemoteGattService::GattErrorCode error_code) {
460 NSError* error = BluetoothDeviceMac::GetNSErrorFromGattErrorCode(error_code); 460 NSError* error = BluetoothDeviceMac::GetNSErrorFromGattErrorCode(error_code);
461 [GetCBMockDescriptor(descriptor) simulateReadWithValue:nil error:error]; 461 [GetCBMockDescriptor(descriptor) simulateReadWithValue:nil error:error];
462 } 462 }
463 463
464 void BluetoothTestMac::SimulateGattDescriptorWrite( 464 void BluetoothTestMac::SimulateGattDescriptorWrite(
(...skipping 18 matching lines...) Expand all
483 case NotifyValueState::NONE: 483 case NotifyValueState::NONE:
484 EXPECT_FALSE(last_notify_value_); 484 EXPECT_FALSE(last_notify_value_);
485 break; 485 break;
486 case NotifyValueState::NOTIFY: 486 case NotifyValueState::NOTIFY:
487 case NotifyValueState::INDICATE: 487 case NotifyValueState::INDICATE:
488 EXPECT_TRUE(last_notify_value_); 488 EXPECT_TRUE(last_notify_value_);
489 break; 489 break;
490 } 490 }
491 } 491 }
492 492
493 void BluetoothTestMac::SimulateDidDiscoverServices( 493 void BluetoothTestMac::SimulateDidDiscoverServicesMac(BluetoothDevice* device) {
494 BluetoothDevice* device,
495 const std::vector<std::string>& uuids) {
496 AddServicesToDevice(device, uuids);
497 [GetMockCBPeripheral(device) mockDidDiscoverServices]; 494 [GetMockCBPeripheral(device) mockDidDiscoverServices];
498 } 495 }
499 496
500 void BluetoothTestMac::SimulateGattDescriptorReadNSData( 497 void BluetoothTestMac::SimulateDidDiscoverCharacteristicsMac(
498 BluetoothRemoteGattService* service) {
499 BluetoothRemoteGattServiceMac* mac_gatt_service =
500 static_cast<BluetoothRemoteGattServiceMac*>(service);
501 CBService* cb_service = mac_gatt_service->GetService();
502 [GetMockCBPeripheral(service)
503 mockDidDiscoverCharacteristicsForService:cb_service];
504 }
505
506 void BluetoothTestMac::SimulateDidDiscoverDescriptorsMac(
507 BluetoothRemoteGattCharacteristic* characteristic) {
508 BluetoothRemoteGattCharacteristicMac* mac_gatt_characteristic =
509 static_cast<BluetoothRemoteGattCharacteristicMac*>(characteristic);
510 CBCharacteristic* cb_characteristic =
511 mac_gatt_characteristic->GetCBCharacteristic();
512 [GetMockCBPeripheral(characteristic)
513 mockDidDiscoverDescriptorsForCharacteristic:cb_characteristic];
514 }
515
516 void BluetoothTestMac::SimulateGattDescriptorReadNSDataMac(
501 BluetoothRemoteGattDescriptor* descriptor, 517 BluetoothRemoteGattDescriptor* descriptor,
502 const std::vector<uint8_t>& value) { 518 const std::vector<uint8_t>& value) {
503 scoped_nsobject<NSData> data( 519 scoped_nsobject<NSData> data(
504 [[NSData alloc] initWithBytes:value.data() length:value.size()]); 520 [[NSData alloc] initWithBytes:value.data() length:value.size()]);
505 [GetCBMockDescriptor(descriptor) simulateReadWithValue:data error:nil]; 521 [GetCBMockDescriptor(descriptor) simulateReadWithValue:data error:nil];
506 } 522 }
507 523
508 void BluetoothTestMac::SimulateGattDescriptorReadNSString( 524 void BluetoothTestMac::SimulateGattDescriptorReadNSStringMac(
509 BluetoothRemoteGattDescriptor* descriptor, 525 BluetoothRemoteGattDescriptor* descriptor,
510 const std::string& value) { 526 const std::string& value) {
511 NSString* string = base::SysUTF8ToNSString(value); 527 NSString* string = base::SysUTF8ToNSString(value);
512 [GetCBMockDescriptor(descriptor) simulateReadWithValue:string error:nil]; 528 [GetCBMockDescriptor(descriptor) simulateReadWithValue:string error:nil];
513 } 529 }
514 530
515 void BluetoothTestMac::SimulateGattDescriptorReadNSNumber( 531 void BluetoothTestMac::SimulateGattDescriptorReadNSNumberMac(
516 BluetoothRemoteGattDescriptor* descriptor, 532 BluetoothRemoteGattDescriptor* descriptor,
517 short value) { 533 short value) {
518 NSNumber* number = [NSNumber numberWithShort:value]; 534 NSNumber* number = [NSNumber numberWithShort:value];
519 [GetCBMockDescriptor(descriptor) simulateReadWithValue:number error:nil]; 535 [GetCBMockDescriptor(descriptor) simulateReadWithValue:number error:nil];
520 } 536 }
521 537
538 void BluetoothTest::AddServicesToDevice(BluetoothDevice* device,
539 const std::vector<std::string>& uuids) {
540 scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]);
541 for (auto uuid : uuids) {
542 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())];
543 [services addObject:cb_service_uuid];
544 }
545 [GetMockCBPeripheral(device) addServices:services];
546 }
547
548 void BluetoothTestMac::AddCharacteristicToService(
549 BluetoothRemoteGattService* service,
550 const std::string& characteristic_uuid,
551 int properties) {
552 BluetoothRemoteGattServiceMac* mac_gatt_service =
553 static_cast<BluetoothRemoteGattServiceMac*>(service);
554 CBService* cb_service = mac_gatt_service->GetService();
555 MockCBService* service_mock = ObjCCast<MockCBService>(cb_service);
556 CBUUID* cb_uuid = [CBUUID UUIDWithString:@(characteristic_uuid.c_str())];
557 [service_mock addCharacteristicWithUUID:cb_uuid properties:properties];
558 }
559
560 void BluetoothTestMac::AddDescriptorToCharacteristic(
561 BluetoothRemoteGattCharacteristic* characteristic,
562 const std::string& uuid) {
563 BluetoothRemoteGattCharacteristicMac* mac_gatt_characteristic =
564 static_cast<BluetoothRemoteGattCharacteristicMac*>(characteristic);
565 CBCharacteristic* cb_characteristic =
566 mac_gatt_characteristic->GetCBCharacteristic();
567 MockCBCharacteristic* characteristic_mock =
568 ObjCCast<MockCBCharacteristic>(cb_characteristic);
569 CBUUID* cb_uuid = [CBUUID UUIDWithString:@(uuid.c_str())];
570 [characteristic_mock addDescriptorWithUUID:cb_uuid];
571 }
572
522 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { 573 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() {
523 gatt_connection_attempts_++; 574 gatt_connection_attempts_++;
524 } 575 }
525 576
526 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { 577 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() {
527 gatt_disconnection_attempts_++; 578 gatt_disconnection_attempts_++;
528 } 579 }
529 580
530 void BluetoothTestMac::OnFakeBluetoothServiceDiscovery() { 581 void BluetoothTestMac::OnFakeBluetoothServiceDiscovery() {
531 gatt_discovery_attempts_++; 582 gatt_discovery_attempts_++;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
614 return ObjCCast<MockCBCharacteristic>(cb_characteristic); 665 return ObjCCast<MockCBCharacteristic>(cb_characteristic);
615 } 666 }
616 667
617 MockCBDescriptor* BluetoothTest::GetCBMockDescriptor( 668 MockCBDescriptor* BluetoothTest::GetCBMockDescriptor(
618 BluetoothRemoteGattDescriptor* descriptor) const { 669 BluetoothRemoteGattDescriptor* descriptor) const {
619 device::BluetoothRemoteGattDescriptorMac* mac_gatt_descriptor = 670 device::BluetoothRemoteGattDescriptorMac* mac_gatt_descriptor =
620 static_cast<device::BluetoothRemoteGattDescriptorMac*>(descriptor); 671 static_cast<device::BluetoothRemoteGattDescriptorMac*>(descriptor);
621 CBDescriptor* cb_descriptor = mac_gatt_descriptor->GetCBDescriptor(); 672 CBDescriptor* cb_descriptor = mac_gatt_descriptor->GetCBDescriptor();
622 return ObjCCast<MockCBDescriptor>(cb_descriptor); 673 return ObjCCast<MockCBDescriptor>(cb_descriptor);
623 } 674 }
624
625 void BluetoothTest::AddServicesToDevice(BluetoothDevice* device,
626 const std::vector<std::string>& uuids) {
627 scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]);
628 for (auto uuid : uuids) {
629 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())];
630 [services addObject:cb_service_uuid];
631 }
632 [GetMockCBPeripheral(device) addServices:services];
633 }
634
635 // Utility function for generating new (CBUUID, address) pairs where CBUUID 675 // Utility function for generating new (CBUUID, address) pairs where CBUUID
636 // hashes to address. For use when adding a new device address to the testing 676 // hashes to address. For use when adding a new device address to the testing
637 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, 677 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses,
638 // and we construct fake addresses for them by hashing the CBUUID. By changing 678 // and we construct fake addresses for them by hashing the CBUUID. By changing
639 // |target| the user can generate sequentially numbered test addresses. 679 // |target| the user can generate sequentially numbered test addresses.
640 // 680 //
641 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { 681 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() {
642 // // The desired first 6 digits of the hash. For example 0100000, 020000, 682 // // The desired first 6 digits of the hash. For example 0100000, 020000,
643 // // 030000, ... 683 // // 030000, ...
644 // const std::string target = "010000"; 684 // const std::string target = "010000";
(...skipping 16 matching lines...) Expand all
661 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); 701 // crypto::SHA256HashString(input_str, raw, sizeof(raw));
662 // if (base::HexEncode(raw, sizeof(raw)) == target) { 702 // if (base::HexEncode(raw, sizeof(raw)) == target) {
663 // return input_str; 703 // return input_str;
664 // } 704 // }
665 // ++input[0]; 705 // ++input[0];
666 // } 706 // }
667 // return ""; 707 // return "";
668 // } 708 // }
669 709
670 } // namespace device 710 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698