mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
[NC Apple coverage] Add tests for CBPeripheralManager category methods.
PiperOrigin-RevId: 814030295
This commit is contained in:
committed by
Copybara-Service
parent
bbd6bbb9b9
commit
a211c5b527
@@ -41,6 +41,7 @@ objc_library(
|
||||
"GNCMBleUtilsTest.m",
|
||||
"GNCMConnectionsTest.m",
|
||||
"GNCMFakeConnection.mm",
|
||||
"GNCPeripheralManagerTest.m",
|
||||
"GNCPeripheralTest.m",
|
||||
"NSData+GNCBase85Test.m",
|
||||
"NSData+GNCWebSafeBase64Test.m",
|
||||
|
||||
+25
-15
@@ -65,7 +65,7 @@ static const uint16_t kPSM = 192;
|
||||
NSMutableArray<CBService *> *_services;
|
||||
}
|
||||
|
||||
@synthesize peripheralDelegate;
|
||||
@synthesize peripheralDelegate = _peripheralDelegate;
|
||||
|
||||
#pragma mark Public
|
||||
|
||||
@@ -99,7 +99,9 @@ static const uint16_t kPSM = 192;
|
||||
if (!_didAddServiceError) {
|
||||
[_services addObject:service];
|
||||
}
|
||||
[peripheralDelegate gnc_peripheralManager:self didAddService:service error:_didAddServiceError];
|
||||
[_peripheralDelegate gnc_peripheralManager:self
|
||||
didAddService:service
|
||||
error:_didAddServiceError];
|
||||
}
|
||||
|
||||
- (void)removeService:(CBMutableService *)service {
|
||||
@@ -113,8 +115,8 @@ static const uint16_t kPSM = 192;
|
||||
- (void)startAdvertising:(NSDictionary<NSString *, id> *)advertisementData {
|
||||
_isAdvertising = _didStartAdvertisingError == nil;
|
||||
_advertisementData = advertisementData;
|
||||
[peripheralDelegate gnc_peripheralManagerDidStartAdvertising:self
|
||||
error:_didStartAdvertisingError];
|
||||
[_peripheralDelegate gnc_peripheralManagerDidStartAdvertising:self
|
||||
error:_didStartAdvertisingError];
|
||||
}
|
||||
|
||||
- (void)respondToRequest:(CBATTRequest *)request withResult:(CBATTError)result {
|
||||
@@ -134,20 +136,20 @@ static const uint16_t kPSM = 192;
|
||||
if (!_didPublishL2CAPChannelError) {
|
||||
localPSM = _PSM;
|
||||
}
|
||||
[peripheralDelegate gnc_peripheralManager:self
|
||||
didPublishL2CAPChannel:localPSM
|
||||
error:_didPublishL2CAPChannelError];
|
||||
[_peripheralDelegate gnc_peripheralManager:self
|
||||
didPublishL2CAPChannel:localPSM
|
||||
error:_didPublishL2CAPChannelError];
|
||||
if (!_didPublishL2CAPChannelError) {
|
||||
[peripheralDelegate gnc_peripheralManager:self
|
||||
didOpenL2CAPChannel:[[CBL2CAPChannel alloc] init]
|
||||
error:nil];
|
||||
[_peripheralDelegate gnc_peripheralManager:self
|
||||
didOpenL2CAPChannel:[[CBL2CAPChannel alloc] init]
|
||||
error:nil];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)unpublishL2CAPChannel:(CBL2CAPPSM)PSM {
|
||||
[peripheralDelegate gnc_peripheralManager:self
|
||||
didUnpublishL2CAPChannel:_PSM
|
||||
error:_didUnPublishL2CAPChannelError];
|
||||
[_peripheralDelegate gnc_peripheralManager:self
|
||||
didUnpublishL2CAPChannel:_PSM
|
||||
error:_didUnPublishL2CAPChannelError];
|
||||
[_unpublishExpectation fulfill];
|
||||
}
|
||||
|
||||
@@ -163,14 +165,22 @@ static const uint16_t kPSM = 192;
|
||||
|
||||
- (void)simulatePeripheralManagerDidUpdateState:(CBManagerState)fakeState {
|
||||
_state = fakeState;
|
||||
[peripheralDelegate gnc_peripheralManagerDidUpdateState:self];
|
||||
[_peripheralDelegate gnc_peripheralManagerDidUpdateState:self];
|
||||
}
|
||||
|
||||
- (void)simulatePeripheralManagerDidReceiveReadRequestForService:(CBUUID *)service
|
||||
characteristic:(CBUUID *)characteristic {
|
||||
CBATTRequest *request = [[CBATTRequest alloc] initWithService:service
|
||||
characteristic:characteristic];
|
||||
[peripheralDelegate gnc_peripheralManager:self didReceiveReadRequest:request];
|
||||
[_peripheralDelegate gnc_peripheralManager:self didReceiveReadRequest:request];
|
||||
}
|
||||
|
||||
- (void)setDelegate:(id<CBPeripheralManagerDelegate>)delegate {
|
||||
self.peripheralDelegate = (id<GNCPeripheralManagerDelegate>)delegate;
|
||||
}
|
||||
|
||||
- (id<CBPeripheralManagerDelegate>)delegate {
|
||||
return (id<CBPeripheralManagerDelegate>)self.peripheralDelegate;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
// 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/GNCPeripheralManager.h"
|
||||
|
||||
#import <CoreBluetooth/CoreBluetooth.h>
|
||||
#import <XCTest/XCTest.h>
|
||||
#import <objc/runtime.h>
|
||||
|
||||
#import "internal/platform/implementation/apple/Mediums/BLE/Tests/GNCFakePeripheralManager.h"
|
||||
|
||||
/** Fake delegate of @c GNCPeripheralManagerDelegate */
|
||||
@interface GNCFakePeripheralManagerDelegate : NSObject <GNCPeripheralManagerDelegate>
|
||||
@end
|
||||
|
||||
@implementation GNCFakePeripheralManagerDelegate
|
||||
|
||||
// Add dummy implementations for protocol methods
|
||||
- (void)gnc_peripheralManagerDidUpdateState:(id<GNCPeripheralManager>)peripheral {
|
||||
}
|
||||
- (void)gnc_peripheralManager:(id<GNCPeripheralManager>)peripheral
|
||||
didAddService:(CBService *)service
|
||||
error:(nullable NSError *)error {
|
||||
}
|
||||
- (void)gnc_peripheralManagerDidStartAdvertising:(id<GNCPeripheralManager>)peripheral
|
||||
error:(nullable NSError *)error {
|
||||
}
|
||||
- (void)gnc_peripheralManager:(id<GNCPeripheralManager>)peripheral
|
||||
didReceiveReadRequest:(CBATTRequest *)request {
|
||||
}
|
||||
- (void)gnc_peripheralManager:(id<GNCPeripheralManager>)peripheral
|
||||
didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests {
|
||||
}
|
||||
- (void)gnc_peripheralManager:(id<GNCPeripheralManager>)peripheral
|
||||
isReadyToSendWriteWithoutResponse:(id)sender {
|
||||
}
|
||||
- (void)gnc_peripheralManager:(id<GNCPeripheralManager>)peripheral
|
||||
didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {
|
||||
}
|
||||
- (void)gnc_peripheralManager:(id<GNCPeripheralManager>)peripheral
|
||||
didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic {
|
||||
}
|
||||
- (void)gnc_peripheralManager:(id<GNCPeripheralManager>)peripheral
|
||||
didPublishL2CAPChannel:(CBL2CAPPSM)PSM
|
||||
error:(nullable NSError *)error {
|
||||
}
|
||||
- (void)gnc_peripheralManager:(id<GNCPeripheralManager>)peripheral
|
||||
didUnpublishL2CAPChannel:(CBL2CAPPSM)PSM
|
||||
error:(NSError *)error {
|
||||
}
|
||||
- (void)gnc_peripheralManager:(id<GNCPeripheralManager>)peripheral
|
||||
didOpenL2CAPChannel:(nullable CBL2CAPChannel *)channel
|
||||
error:(nullable NSError *)error {
|
||||
}
|
||||
|
||||
// CBPeripheralManagerDelegate methods
|
||||
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@interface GNCPeripheralManagerTest : XCTestCase
|
||||
@end
|
||||
|
||||
@implementation GNCPeripheralManagerTest
|
||||
|
||||
- (void)testSetPeripheralDelegate_SetsDelegate {
|
||||
GNCFakePeripheralManager *fakeManager = [[GNCFakePeripheralManager alloc] init];
|
||||
id<GNCPeripheralManagerDelegate> delegate = [[GNCFakePeripheralManagerDelegate alloc] init];
|
||||
|
||||
fakeManager.peripheralDelegate = delegate;
|
||||
|
||||
XCTAssertEqual(fakeManager.peripheralDelegate, delegate);
|
||||
}
|
||||
|
||||
- (void)testPeripheralDelegate_GetsDelegate {
|
||||
GNCFakePeripheralManager *fakeManager = [[GNCFakePeripheralManager alloc] init];
|
||||
id<GNCPeripheralManagerDelegate> delegate = [[GNCFakePeripheralManagerDelegate alloc] init];
|
||||
|
||||
// Set the underlying delegate property in the fake.
|
||||
fakeManager.peripheralDelegate = delegate;
|
||||
|
||||
// Get IMP of the category method from CBPeripheralManager.
|
||||
Method getMethod =
|
||||
class_getInstanceMethod([CBPeripheralManager class], @selector(peripheralDelegate));
|
||||
id (*getImp)(id, SEL) = (id (*)(id, SEL))method_getImplementation(getMethod);
|
||||
|
||||
// Call the implementation directly on the fake.
|
||||
id result = getImp(fakeManager, @selector(peripheralDelegate));
|
||||
|
||||
XCTAssertEqual(result, delegate);
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user