mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
[NC Apple coverage] Add tests for CBPeripheral category methods and update GNCFakePeripheral.
PiperOrigin-RevId: 814026340
This commit is contained in:
committed by
Copybara-Service
parent
2c7f4de3e6
commit
bbd6bbb9b9
@@ -41,6 +41,7 @@ objc_library(
|
||||
"GNCMBleUtilsTest.m",
|
||||
"GNCMConnectionsTest.m",
|
||||
"GNCMFakeConnection.mm",
|
||||
"GNCPeripheralTest.m",
|
||||
"NSData+GNCBase85Test.m",
|
||||
"NSData+GNCWebSafeBase64Test.m",
|
||||
],
|
||||
@@ -62,6 +63,7 @@ objc_library(
|
||||
"//internal/platform/implementation/apple/Mediums/BLE/Sockets:Shared",
|
||||
"//third_party/apple_frameworks:CoreBluetooth",
|
||||
"//third_party/apple_frameworks:Foundation",
|
||||
"//third_party/apple_frameworks:ObjectiveC",
|
||||
"//third_party/apple_frameworks:XCTest",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -22,6 +22,9 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
/** A fake implementation of @c GNCPeripheral to inject for testing. */
|
||||
@interface GNCFakePeripheral : NSObject <GNCPeripheral>
|
||||
|
||||
/** The peripheral's delegate. */
|
||||
@property(nonatomic, nullable, readwrite) id<CBPeripheralDelegate> delegate;
|
||||
|
||||
/**
|
||||
* Similates a @c discoverServices: error.
|
||||
*
|
||||
|
||||
@@ -41,8 +41,6 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
NSUUID *_identifier;
|
||||
}
|
||||
|
||||
@synthesize peripheralDelegate;
|
||||
|
||||
- (instancetype)init {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
@@ -52,6 +50,14 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setPeripheralDelegate:(nullable id<GNCPeripheralDelegate>)peripheralDelegate {
|
||||
self.delegate = peripheralDelegate;
|
||||
}
|
||||
|
||||
- (nullable id<GNCPeripheralDelegate>)peripheralDelegate {
|
||||
return (id<GNCPeripheralDelegate>)self.delegate;
|
||||
}
|
||||
|
||||
- (NSUUID *)identifier {
|
||||
return _identifier;
|
||||
}
|
||||
@@ -68,7 +74,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
}
|
||||
}
|
||||
|
||||
[peripheralDelegate gnc_peripheral:self didDiscoverServices:_discoverServicesError];
|
||||
[self.peripheralDelegate gnc_peripheral:self didDiscoverServices:_discoverServicesError];
|
||||
}];
|
||||
}
|
||||
|
||||
@@ -90,7 +96,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
service.characteristics = characteristics;
|
||||
}
|
||||
|
||||
[peripheralDelegate gnc_peripheral:self
|
||||
[self.peripheralDelegate gnc_peripheral:self
|
||||
didDiscoverCharacteristicsForService:service
|
||||
error:_discoverCharacteristicsForServiceError];
|
||||
}];
|
||||
@@ -102,9 +108,9 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
characteristic.value = [NSData data];
|
||||
}
|
||||
|
||||
[peripheralDelegate gnc_peripheral:self
|
||||
didUpdateValueForCharacteristic:characteristic
|
||||
error:_readValueForCharacteristicError];
|
||||
[self.peripheralDelegate gnc_peripheral:self
|
||||
didUpdateValueForCharacteristic:characteristic
|
||||
error:_readValueForCharacteristicError];
|
||||
}];
|
||||
}
|
||||
|
||||
@@ -112,11 +118,11 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
[self delayDelegateUsingBlock:^() {
|
||||
CBL2CAPChannel *channel = [[CBL2CAPChannel alloc] init];
|
||||
if (_openL2CAPChannelError) {
|
||||
[peripheralDelegate gnc_peripheral:self
|
||||
didOpenL2CAPChannel:channel
|
||||
error:_openL2CAPChannelError];
|
||||
[self.peripheralDelegate gnc_peripheral:self
|
||||
didOpenL2CAPChannel:channel
|
||||
error:_openL2CAPChannelError];
|
||||
} else {
|
||||
[peripheralDelegate gnc_peripheral:self didOpenL2CAPChannel:channel error:nil];
|
||||
[self.peripheralDelegate gnc_peripheral:self didOpenL2CAPChannel:channel error:nil];
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
// Copyright 2025 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#import "internal/platform/implementation/apple/Mediums/BLE/GNCPeripheral.h"
|
||||
|
||||
#import <CoreBluetooth/CoreBluetooth.h>
|
||||
#import <XCTest/XCTest.h>
|
||||
#import <objc/runtime.h>
|
||||
|
||||
#import "internal/platform/implementation/apple/Mediums/BLE/Tests/GNCFakePeripheral.h"
|
||||
|
||||
/** Fake delegate of @c GNCPeripheralDelegate */
|
||||
@interface GNCFakePeripheralDelegate : NSObject <GNCPeripheralDelegate>
|
||||
@end
|
||||
|
||||
@implementation GNCFakePeripheralDelegate
|
||||
@end
|
||||
|
||||
@interface GNCPeripheralTest : XCTestCase
|
||||
@end
|
||||
|
||||
@implementation GNCPeripheralTest
|
||||
|
||||
- (void)testSetPeripheralDelegate_SetsDelegate {
|
||||
GNCFakePeripheral *fakePeripheral = [[GNCFakePeripheral alloc] init];
|
||||
id<GNCPeripheralDelegate> delegate = [[GNCFakePeripheralDelegate alloc] init];
|
||||
|
||||
// Get IMP of the category method.
|
||||
Method setMethod =
|
||||
class_getInstanceMethod([CBPeripheral class], @selector(setPeripheralDelegate:));
|
||||
void (*setImp)(id, SEL, id) = (void (*)(id, SEL, id))method_getImplementation(setMethod);
|
||||
|
||||
// Call the implementation directly on the fake.
|
||||
setImp(fakePeripheral, @selector(setPeripheralDelegate:), delegate);
|
||||
|
||||
// Verify that the underlying delegate setter was called.
|
||||
XCTAssertEqual(fakePeripheral.delegate, delegate);
|
||||
}
|
||||
|
||||
- (void)testPeripheralDelegate_GetsDelegate {
|
||||
GNCFakePeripheral *fakePeripheral = [[GNCFakePeripheral alloc] init];
|
||||
id<GNCPeripheralDelegate> delegate = [[GNCFakePeripheralDelegate alloc] init];
|
||||
|
||||
// Set the underlying delegate property.
|
||||
fakePeripheral.delegate = delegate;
|
||||
|
||||
// Get IMP of the category method.
|
||||
Method getMethod = class_getInstanceMethod([CBPeripheral class], @selector(peripheralDelegate));
|
||||
id (*getImp)(id, SEL) = (id (*)(id, SEL))method_getImplementation(getMethod);
|
||||
|
||||
// Call the implementation directly and check the result.
|
||||
id result = getImp(fakePeripheral, @selector(peripheralDelegate));
|
||||
|
||||
XCTAssertEqual(result, delegate);
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user