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

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

Issue 1948763003: Adding support for service scan on OS X (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cleanup
Patch Set: Adding disconnect with error Created 4 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/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;
ortuno 2016/05/06 16:03:34 How does this array get destroyed?
jlebel 2016/05/07 00:16:12 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;
ortuno 2016/05/06 16:03:34 Why do you need to manually synthesize these, I th
jlebel 2016/05/07 00:16:12 Unfortunately Chrome is not using ARC yet. Therefo
ortuno 2016/05/09 19:54:01 Ah. I see.
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;
(...skipping 12 matching lines...) Expand all
65 [aClass isSubclassOfClass:[CBPeripheral class]]) { 71 [aClass isSubclassOfClass:[CBPeripheral class]]) {
66 return YES; 72 return YES;
67 } 73 }
68 return [super isKindOfClass:aClass]; 74 return [super isKindOfClass:aClass];
69 } 75 }
70 76
71 - (void)setState:(CBPeripheralState)state { 77 - (void)setState:(CBPeripheralState)state {
72 _state = state; 78 _state = state;
73 } 79 }
74 80
81 - (void)discoverServices:(NSArray*)serviceUUIDs {
82 if (_bluetoothTestMac) {
83 _bluetoothTestMac->OnFakeBluetoothServiceDiscovery();
84 }
85 }
86
87 - (void)removeAllServices {
88 [_services removeAllObjects];
89 }
90
91 - (void)addServices:(NSArray*)services {
92 for (CBUUID* uuid in services) {
93 MockCBService* service =
ortuno 2016/05/06 16:03:34 Who manages the lifetime of this object? Does it's
jlebel 2016/05/07 00:16:12 Done.
94 [[MockCBService alloc] initWithCBUUID:uuid primary:YES];
95 [_services addObject:service.service];
96 }
97 }
98
99 - (void)didDiscoverWithError:(NSError*)error {
100 [_delegate peripheral:self.peripheral didDiscoverServices:error];
ortuno 2016/05/06 16:03:34 What is this called? I've never seen this pattern
jlebel 2016/05/07 00:16:12 What pattern are you talking about? "self.peripher
ortuno 2016/05/09 19:54:01 I guess I'm not sure what the second and third par
101 }
102
103 - (void)removeServiceWithCBUUID:(CBUUID*)uuid {
104 CBService* serviceToRemove = nil;
105 for (MockCBService* service in _services) {
106 if ([service.UUID isEqual:uuid]) {
107 serviceToRemove = service.service;
108 break;
109 }
110 }
111 NSAssert(serviceToRemove, @"Unknown service to remove %@", uuid);
112 [_services removeObject:serviceToRemove];
113 #pragma clang diagnostic push
ortuno 2016/05/06 16:03:34 Can you comment why you need this?
jlebel 2016/05/07 00:16:12 Done.
114 #pragma clang diagnostic ignored "-Wpartial-availability"
115 [_delegate peripheral:self.peripheral didModifyServices:@[ serviceToRemove ]];
116 #pragma clang diagnostic pop
117 }
118
75 - (NSUUID*)identifier { 119 - (NSUUID*)identifier {
76 return _identifier.get(); 120 return _identifier.get();
77 } 121 }
78 122
79 - (NSString*)name { 123 - (NSString*)name {
80 return _name.get(); 124 return _name.get();
81 } 125 }
82 126
127 - (NSArray*)services {
128 return _services;
129 }
130
83 - (CBPeripheral*)peripheral { 131 - (CBPeripheral*)peripheral {
84 return ObjCCast<CBPeripheral>(self); 132 return ObjCCast<CBPeripheral>(self);
85 } 133 }
86 134
87 @end 135 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698