diff options
author | jlebel <jlebel@chromium.org> | 2016-03-23 04:05:53 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-23 11:07:19 +0000 |
commit | f11faf4f50e851673fae351cc9e01aeafbddc0e8 (patch) | |
tree | ac35f543799b00af473c7ab8917ecc9857349c50 /device | |
parent | 6d73369e32f4cb2dbeec4d8080c377f545ff8640 (diff) | |
download | chromium_src-f11faf4f50e851673fae351cc9e01aeafbddc0e8.zip chromium_src-f11faf4f50e851673fae351cc9e01aeafbddc0e8.tar.gz chromium_src-f11faf4f50e851673fae351cc9e01aeafbddc0e8.tar.bz2 |
Simplifying Objective-C
Simplifying Objective-C. Only using MockCBPeripheral (and removing OCMock). Adding |using base::scoped_nsobject;|.
BUG=None
Review URL: https://codereview.chromium.org/1758863002
Cr-Commit-Position: refs/heads/master@{#382826}
Diffstat (limited to 'device')
4 files changed, 101 insertions, 88 deletions
diff --git a/device/bluetooth/bluetooth_adapter_mac_unittest.mm b/device/bluetooth/bluetooth_adapter_mac_unittest.mm index f855e72..2436375 100644 --- a/device/bluetooth/bluetooth_adapter_mac_unittest.mm +++ b/device/bluetooth/bluetooth_adapter_mac_unittest.mm @@ -11,6 +11,7 @@ #include "device/bluetooth/bluetooth_discovery_session.h" #include "device/bluetooth/bluetooth_discovery_session_outcome.h" #include "device/bluetooth/bluetooth_low_energy_device_mac.h" +#include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/ocmock/OCMock/OCMock.h" @@ -25,7 +26,7 @@ namespace { // |kTestHashAddress| is the hash corresponding to identifier |kTestNSUUID|. -NSString* const kTestNSUUID = @"00000000-1111-2222-3333-444444444444"; +const char* const kTestNSUUID = "00000000-1111-2222-3333-444444444444"; const std::string kTestHashAddress = "D1:6F:E3:22:FD:5B"; const int kTestRssi = 0; } // namespace @@ -56,25 +57,14 @@ class BluetoothAdapterMacTest : public testing::Test { return adapter_->GetDevice(address); } - CBPeripheral* CreateMockPeripheral(NSString* identifier) { + CBPeripheral* CreateMockPeripheral(const char* identifier) { if (!BluetoothAdapterMac::IsLowEnergyAvailable()) { LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; return nil; } - Class peripheral_class = NSClassFromString(@"CBPeripheral"); - id mock_peripheral = - [[OCMockObject mockForClass:[peripheral_class class]] retain]; - [static_cast<CBPeripheral*>([[mock_peripheral stub] - andReturnValue:@(CBPeripheralStateDisconnected)]) - performSelector:@selector(state)]; - [[[mock_peripheral stub] andReturn:[NSString string]] name]; - Class uuid_class = NSClassFromString(@"NSUUID"); - [[[mock_peripheral stub] - andReturn:[[uuid_class performSelector:@selector(UUID)] - performSelector:@selector(initWithUUIDString:) - withObject:identifier]] identifier]; - - return mock_peripheral; + base::scoped_nsobject<MockCBPeripheral> mock_peripheral( + [[MockCBPeripheral alloc] initWithUTF8StringIdentifier:identifier]); + return [mock_peripheral.get().peripheral retain]; } NSDictionary* CreateAdvertisementData() { @@ -82,8 +72,7 @@ class BluetoothAdapterMacTest : public testing::Test { CBAdvertisementDataIsConnectable : @(YES), CBAdvertisementDataServiceDataKey : [NSDictionary dictionary], }; - [advertisement_data retain]; - return advertisement_data; + return [advertisement_data retain]; } std::string GetHashAddress(CBPeripheral* peripheral) { @@ -255,7 +244,8 @@ TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilterFail) { TEST_F(BluetoothAdapterMacTest, CheckGetPeripheralHashAddress) { if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) return; - base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID)); + base::scoped_nsobject<CBPeripheral> mock_peripheral( + CreateMockPeripheral(kTestNSUUID)); if (mock_peripheral.get() == nil) return; EXPECT_EQ(kTestHashAddress, GetHashAddress(mock_peripheral)); @@ -264,7 +254,8 @@ TEST_F(BluetoothAdapterMacTest, CheckGetPeripheralHashAddress) { TEST_F(BluetoothAdapterMacTest, LowEnergyDeviceUpdatedNewDevice) { if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) return; - base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID)); + base::scoped_nsobject<CBPeripheral> mock_peripheral( + CreateMockPeripheral(kTestNSUUID)); if (mock_peripheral.get() == nil) return; base::scoped_nsobject<NSDictionary> advertisement_data( @@ -280,7 +271,8 @@ TEST_F(BluetoothAdapterMacTest, LowEnergyDeviceUpdatedNewDevice) { TEST_F(BluetoothAdapterMacTest, LowEnergyDeviceUpdatedOldDevice) { if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) return; - base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID)); + base::scoped_nsobject<CBPeripheral> mock_peripheral( + CreateMockPeripheral(kTestNSUUID)); if (mock_peripheral.get() == nil) return; base::scoped_nsobject<NSDictionary> advertisement_data( @@ -312,7 +304,8 @@ TEST_F(BluetoothAdapterMacTest, LowEnergyDeviceUpdatedOldDevice) { TEST_F(BluetoothAdapterMacTest, UpdateDevicesRemovesLowEnergyDevice) { if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) return; - base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID)); + base::scoped_nsobject<CBPeripheral> mock_peripheral( + CreateMockPeripheral(kTestNSUUID)); if (mock_peripheral.get() == nil) return; base::scoped_nsobject<NSDictionary> advertisement_data( diff --git a/device/bluetooth/test/bluetooth_test_mac.mm b/device/bluetooth/test/bluetooth_test_mac.mm index 9ad4583..0563fd4 100644 --- a/device/bluetooth/test/bluetooth_test_mac.mm +++ b/device/bluetooth/test/bluetooth_test_mac.mm @@ -40,34 +40,18 @@ class BluetoothTestMac::ScopedMockCentralManager { namespace { -CBPeripheral* CreateMockPeripheral(NSString* peripheral_identifier) { - Class peripheral_class = NSClassFromString(@"CBPeripheral"); - id mock_peripheral = [OCMockObject mockForClass:[peripheral_class class]]; - [[[mock_peripheral stub] andReturnValue:@(CBPeripheralStateDisconnected)] - performSelector:@selector(state)]; - Class uuid_class = NSClassFromString(@"NSUUID"); - [[[mock_peripheral stub] - andReturn:[[uuid_class performSelector:@selector(UUID)] - performSelector:@selector(initWithUUIDString:) - withObject:peripheral_identifier]] - performSelector:@selector(identifier)]; - [[[mock_peripheral stub] - andReturn:[NSString stringWithUTF8String:BluetoothTest::kTestDeviceName - .c_str()]] name]; - return mock_peripheral; -} - NSDictionary* CreateAdvertisementData(NSString* name, NSArray* uuids) { NSMutableDictionary* advertisement_data = [NSMutableDictionary dictionaryWithDictionary:@{ CBAdvertisementDataLocalNameKey : name, - CBAdvertisementDataServiceDataKey : [NSDictionary dictionary], + CBAdvertisementDataServiceDataKey : @{}, CBAdvertisementDataIsConnectable : @(YES), }]; - if (uuids) + if (uuids) { [advertisement_data setObject:uuids forKey:CBAdvertisementDataServiceUUIDsKey]; - return advertisement_data; + } + return [advertisement_data retain]; } } // namespace @@ -129,75 +113,60 @@ BluetoothDevice* BluetoothTestMac::DiscoverLowEnergyDevice(int device_ordinal) { CBCentralManager* central_manager = adapter_mac_->low_energy_central_manager_; BluetoothLowEnergyCentralManagerDelegate* central_manager_delegate = adapter_mac_->low_energy_central_manager_delegate_; - Class cbuuid_class = NSClassFromString(@"CBUUID"); switch (device_ordinal) { case 1: { - CBPeripheral* peripheral = CreateMockPeripheral( - [NSString stringWithUTF8String:kTestPeripheralUUID1.c_str()]); - NSString* name = [NSString stringWithUTF8String:kTestDeviceName.c_str()]; + scoped_nsobject<MockCBPeripheral> mock_peripheral( + [[MockCBPeripheral alloc] + initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); NSArray* uuids = @[ - [cbuuid_class - UUIDWithString:[NSString stringWithUTF8String:kTestUUIDGenericAccess - .c_str()]], - [cbuuid_class - UUIDWithString:[NSString - stringWithUTF8String:kTestUUIDGenericAttribute - .c_str()]] + [CBUUID UUIDWithString:@(kTestUUIDGenericAccess.c_str())], + [CBUUID UUIDWithString:@(kTestUUIDGenericAttribute.c_str())] ]; - NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); + scoped_nsobject<NSDictionary> advertisement_data = + CreateAdvertisementData(@(kTestDeviceName.c_str()), uuids); [central_manager_delegate centralManager:central_manager - didDiscoverPeripheral:peripheral + didDiscoverPeripheral:mock_peripheral.get().peripheral advertisementData:advertisement_data RSSI:@(0)]; break; } case 2: { - CBPeripheral* peripheral = CreateMockPeripheral( - [NSString stringWithUTF8String:kTestPeripheralUUID1.c_str()]); - NSString* name = [NSString stringWithUTF8String:kTestDeviceName.c_str()]; + scoped_nsobject<MockCBPeripheral> mock_peripheral( + [[MockCBPeripheral alloc] + initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); NSArray* uuids = @[ - [cbuuid_class - UUIDWithString:[NSString - stringWithUTF8String:kTestUUIDImmediateAlert - .c_str()]], - [cbuuid_class - UUIDWithString:[NSString - stringWithUTF8String:kTestUUIDLinkLoss.c_str()]] + [CBUUID UUIDWithString:@(kTestUUIDImmediateAlert.c_str())], + [CBUUID UUIDWithString:@(kTestUUIDLinkLoss.c_str())] ]; - NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); + scoped_nsobject<NSDictionary> advertisement_data = + CreateAdvertisementData(@(kTestDeviceName.c_str()), uuids); [central_manager_delegate centralManager:central_manager - didDiscoverPeripheral:peripheral + didDiscoverPeripheral:mock_peripheral.get().peripheral advertisementData:advertisement_data RSSI:@(0)]; break; } case 3: { - scoped_nsobject<NSString> uuid_string( - [[NSString alloc] initWithUTF8String:kTestPeripheralUUID1.c_str()]); - scoped_nsobject<NSUUID> identifier( - [[NSUUID alloc] initWithUUIDString:uuid_string]); - scoped_nsobject<CBPeripheral> peripheral; - peripheral.reset(static_cast<CBPeripheral*>( - [[MockCBPeripheral alloc] initWithIdentifier:identifier])); - scoped_nsobject<NSString> name( - [[NSString alloc] initWithUTF8String:kTestDeviceNameEmpty.c_str()]); + scoped_nsobject<MockCBPeripheral> mock_peripheral( + [[MockCBPeripheral alloc] + initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); scoped_nsobject<NSDictionary> advertisement_data( - [CreateAdvertisementData(name, nil) retain]); + CreateAdvertisementData(@(kTestDeviceNameEmpty.c_str()), nil)); [central_manager_delegate centralManager:central_manager - didDiscoverPeripheral:peripheral + didDiscoverPeripheral:mock_peripheral.get().peripheral advertisementData:advertisement_data RSSI:@(0)]; break; } case 4: { - CBPeripheral* peripheral = CreateMockPeripheral( - [NSString stringWithUTF8String:kTestPeripheralUUID2.c_str()]); - NSString* name = - [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; + scoped_nsobject<MockCBPeripheral> mock_peripheral( + [[MockCBPeripheral alloc] + initWithUTF8StringIdentifier:kTestPeripheralUUID2.c_str()]); NSArray* uuids = nil; - NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); + scoped_nsobject<NSDictionary> advertisement_data = + CreateAdvertisementData(@(kTestDeviceNameEmpty.c_str()), uuids); [central_manager_delegate centralManager:central_manager - didDiscoverPeripheral:peripheral + didDiscoverPeripheral:mock_peripheral.get().peripheral advertisementData:advertisement_data RSSI:@(0)]; break; diff --git a/device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h b/device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h index f2e66da..fee3dc6 100644 --- a/device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h +++ b/device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h @@ -16,8 +16,14 @@ @property(nonatomic, readonly) CBPeripheralState state; @property(nonatomic, readonly) NSUUID* identifier; @property(nonatomic, readonly) NSString* name; +@property(nonatomic, readonly) CBPeripheral* peripheral; +- (instancetype)init NS_UNAVAILABLE; - (instancetype)initWithIdentifier:(NSUUID*)identifier; +- (instancetype)initWithUTF8StringIdentifier:(const char*)identifier; +- (instancetype)initWithIdentifier:(NSUUID*)identifier + name:(NSString*)name NS_DESIGNATED_INITIALIZER; + - (void)setState:(CBPeripheralState)state; @end diff --git a/device/bluetooth/test/mock_bluetooth_cbperipheral_mac.mm b/device/bluetooth/test/mock_bluetooth_cbperipheral_mac.mm index 0ca4a46..d549fc3 100644 --- a/device/bluetooth/test/mock_bluetooth_cbperipheral_mac.mm +++ b/device/bluetooth/test/mock_bluetooth_cbperipheral_mac.mm @@ -2,13 +2,18 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#import "mock_bluetooth_cbperipheral_mac.h" +#include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" +#include "base/mac/foundation_util.h" #include "base/mac/scoped_nsobject.h" +#include "device/bluetooth/test/bluetooth_test.h" + +using base::mac::ObjCCast; +using base::scoped_nsobject; @interface MockCBPeripheral () { - base::scoped_nsobject<NSUUID> _identifier; - base::scoped_nsobject<NSString> _name; + scoped_nsobject<NSUUID> _identifier; + scoped_nsobject<NSString> _name; } @end @@ -17,16 +22,52 @@ @synthesize state = _state; +- (instancetype)init { + [self doesNotRecognizeSelector:_cmd]; + return self; +} + +- (instancetype)initWithUTF8StringIdentifier:(const char*)utf8Identifier { + scoped_nsobject<NSUUID> identifier( + [[NSUUID alloc] initWithUUIDString:@(utf8Identifier)]); + return [self initWithIdentifier:identifier name:nil]; +} + - (instancetype)initWithIdentifier:(NSUUID*)identifier { + return [self initWithIdentifier:identifier name:nil]; +} + +- (instancetype)initWithIdentifier:(NSUUID*)identifier name:(NSString*)name { self = [super init]; if (self) { _identifier.reset([identifier retain]); - _name.reset([@"FakeBluetoothDevice" retain]); + if (name) { + _name.reset([name retain]); + } else { + _name.reset( + [@(device::BluetoothTestBase::kTestDeviceName.c_str()) retain]); + } _state = CBPeripheralStateDisconnected; } return self; } +- (BOOL)isKindOfClass:(Class)aClass { + if (aClass == [CBPeripheral class] || + [aClass isSubclassOfClass:[CBPeripheral class]]) { + return YES; + } + return [super isKindOfClass:aClass]; +} + +- (BOOL)isMemberOfClass:(Class)aClass { + if (aClass == [CBPeripheral class] || + [aClass isSubclassOfClass:[CBPeripheral class]]) { + return YES; + } + return [super isKindOfClass:aClass]; +} + - (void)setState:(CBPeripheralState)state { _state = state; } @@ -39,4 +80,8 @@ return _name.get(); } +- (CBPeripheral*)peripheral { + return ObjCCast<CBPeripheral>(self); +} + @end |