Chromium Code Reviews| 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/mock_bluetooth_cbperipheral_mac.h" | 5 #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" |
| 6 | 6 |
| 7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
| 8 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "device/bluetooth/test/bluetooth_test.h" | 9 #include "device/bluetooth/test/bluetooth_test_mac.h" |
| 10 #include "device/bluetooth/test/mock_bluetooth_cbservice_mac.h" | |
| 10 | 11 |
| 11 using base::mac::ObjCCast; | 12 using base::mac::ObjCCast; |
| 12 using base::scoped_nsobject; | 13 using base::scoped_nsobject; |
| 13 | 14 |
| 14 @interface MockCBPeripheral () { | 15 @interface MockCBPeripheral () { |
| 15 scoped_nsobject<NSUUID> _identifier; | 16 scoped_nsobject<NSUUID> _identifier; |
| 16 scoped_nsobject<NSString> _name; | 17 scoped_nsobject<NSString> _name; |
| 18 id<CBPeripheralDelegate> _delegate; | |
| 19 NSMutableArray* _services; | |
|
erikchen
2016/05/09 20:00:08
used a scoped_nsobject?
jlebel
2016/05/09 23:05:59
Done.
| |
| 17 } | 20 } |
| 18 | 21 |
| 19 @end | 22 @end |
| 20 | 23 |
| 21 @implementation MockCBPeripheral | 24 @implementation MockCBPeripheral |
| 22 | 25 |
| 23 @synthesize state = _state; | 26 @synthesize state = _state; |
| 27 @synthesize delegate = _delegate; | |
| 28 @synthesize bluetoothTestMac = _bluetoothTestMac; | |
| 24 | 29 |
| 25 - (instancetype)init { | 30 - (instancetype)init { |
| 26 [self doesNotRecognizeSelector:_cmd]; | 31 [self doesNotRecognizeSelector:_cmd]; |
| 27 return self; | 32 return self; |
| 28 } | 33 } |
| 29 | 34 |
| 30 - (instancetype)initWithUTF8StringIdentifier:(const char*)utf8Identifier { | 35 - (instancetype)initWithUTF8StringIdentifier:(const char*)utf8Identifier { |
| 31 scoped_nsobject<NSUUID> identifier( | 36 scoped_nsobject<NSUUID> identifier( |
| 32 [[NSUUID alloc] initWithUUIDString:@(utf8Identifier)]); | 37 [[NSUUID alloc] initWithUUIDString:@(utf8Identifier)]); |
| 33 return [self initWithIdentifier:identifier name:nil]; | 38 return [self initWithIdentifier:identifier name:nil]; |
| 34 } | 39 } |
| 35 | 40 |
| 36 - (instancetype)initWithIdentifier:(NSUUID*)identifier { | 41 - (instancetype)initWithIdentifier:(NSUUID*)identifier { |
| 37 return [self initWithIdentifier:identifier name:nil]; | 42 return [self initWithIdentifier:identifier name:nil]; |
| 38 } | 43 } |
| 39 | 44 |
| 40 - (instancetype)initWithIdentifier:(NSUUID*)identifier name:(NSString*)name { | 45 - (instancetype)initWithIdentifier:(NSUUID*)identifier name:(NSString*)name { |
| 41 self = [super init]; | 46 self = [super init]; |
| 42 if (self) { | 47 if (self) { |
| 48 _services = [[NSMutableArray alloc] init]; | |
| 43 _identifier.reset([identifier retain]); | 49 _identifier.reset([identifier retain]); |
| 44 if (name) { | 50 if (name) { |
| 45 _name.reset([name retain]); | 51 _name.reset([name retain]); |
| 46 } else { | 52 } else { |
| 47 _name.reset( | 53 _name.reset( |
| 48 [@(device::BluetoothTestBase::kTestDeviceName.c_str()) retain]); | 54 [@(device::BluetoothTestBase::kTestDeviceName.c_str()) retain]); |
| 49 } | 55 } |
| 50 _state = CBPeripheralStateDisconnected; | 56 _state = CBPeripheralStateDisconnected; |
| 51 } | 57 } |
| 52 return self; | 58 return self; |
| 53 } | 59 } |
| 54 | 60 |
| 61 - (void)dealloc { | |
| 62 [_services release]; | |
| 63 [super dealloc]; | |
| 64 } | |
| 65 | |
| 55 - (BOOL)isKindOfClass:(Class)aClass { | 66 - (BOOL)isKindOfClass:(Class)aClass { |
| 56 if (aClass == [CBPeripheral class] || | 67 if (aClass == [CBPeripheral class] || |
| 57 [aClass isSubclassOfClass:[CBPeripheral class]]) { | 68 [aClass isSubclassOfClass:[CBPeripheral class]]) { |
| 58 return YES; | 69 return YES; |
| 59 } | 70 } |
| 60 return [super isKindOfClass:aClass]; | 71 return [super isKindOfClass:aClass]; |
| 61 } | 72 } |
| 62 | 73 |
| 63 - (BOOL)isMemberOfClass:(Class)aClass { | 74 - (BOOL)isMemberOfClass:(Class)aClass { |
| 64 if (aClass == [CBPeripheral class] || | 75 if (aClass == [CBPeripheral class] || |
| 65 [aClass isSubclassOfClass:[CBPeripheral class]]) { | 76 [aClass isSubclassOfClass:[CBPeripheral class]]) { |
| 66 return YES; | 77 return YES; |
| 67 } | 78 } |
| 68 return [super isKindOfClass:aClass]; | 79 return [super isKindOfClass:aClass]; |
| 69 } | 80 } |
| 70 | 81 |
| 71 - (void)setState:(CBPeripheralState)state { | 82 - (void)setState:(CBPeripheralState)state { |
| 72 _state = state; | 83 _state = state; |
| 73 } | 84 } |
| 74 | 85 |
| 86 - (void)discoverServices:(NSArray*)serviceUUIDs { | |
| 87 if (_bluetoothTestMac) { | |
| 88 _bluetoothTestMac->OnFakeBluetoothServiceDiscovery(); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 - (void)removeAllServices { | |
| 93 [_services removeAllObjects]; | |
| 94 } | |
| 95 | |
| 96 - (void)addServices:(NSArray*)services { | |
| 97 for (CBUUID* uuid in services) { | |
| 98 base::scoped_nsobject<MockCBService> service( | |
| 99 [[MockCBService alloc] initWithCBUUID:uuid primary:YES]); | |
| 100 [_services addObject:service.get().service]; | |
|
ortuno
2016/05/09 19:54:01
Will this make it so that the service is retained
jlebel
2016/05/09 23:05:59
yes.
| |
| 101 } | |
| 102 } | |
| 103 | |
| 104 - (void)didDiscoverWithError:(NSError*)error { | |
| 105 [_delegate peripheral:self.peripheral didDiscoverServices:error]; | |
| 106 } | |
| 107 | |
| 108 - (void)removeService:(CBService*)service { | |
| 109 base::scoped_nsobject<CBService> serviceToRemove(service, | |
| 110 base::scoped_policy::RETAIN); | |
| 111 [_services removeObject:serviceToRemove]; | |
| 112 NSAssert(serviceToRemove, @"Unknown service to remove %@", service); | |
| 113 [_services removeObject:serviceToRemove]; | |
| 114 // -[CBPeripheralDelegate peripheral:didModifyServices:] is only available | |
|
erikchen
2016/05/09 20:00:08
Ah, you should probably just do:
if ([_delegate r
ortuno
2016/05/09 20:25:36
Can confirm that this works:
DCHECK([_delegate re
jlebel
2016/05/09 23:05:59
Done.
jlebel
2016/05/09 23:05:59
Done.
| |
| 115 // with 10.9. It is safe to call this method (even if chrome is compiled with | |
| 116 // 10.8) since WebBluetooth is enabled only with 10.10. | |
| 117 #pragma clang diagnostic push | |
| 118 #pragma clang diagnostic ignored "-Wpartial-availability" | |
| 119 [_delegate peripheral:self.peripheral didModifyServices:@[ serviceToRemove ]]; | |
| 120 #pragma clang diagnostic pop | |
| 121 } | |
| 122 | |
| 75 - (NSUUID*)identifier { | 123 - (NSUUID*)identifier { |
| 76 return _identifier.get(); | 124 return _identifier.get(); |
| 77 } | 125 } |
| 78 | 126 |
| 79 - (NSString*)name { | 127 - (NSString*)name { |
| 80 return _name.get(); | 128 return _name.get(); |
| 81 } | 129 } |
| 82 | 130 |
| 131 - (NSArray*)services { | |
| 132 return _services; | |
| 133 } | |
| 134 | |
| 83 - (CBPeripheral*)peripheral { | 135 - (CBPeripheral*)peripheral { |
| 84 return ObjCCast<CBPeripheral>(self); | 136 return ObjCCast<CBPeripheral>(self); |
| 85 } | 137 } |
| 86 | 138 |
| 87 @end | 139 @end |
| OLD | NEW |