Skip to content

Commit

Permalink
Updated BLEPeripheralManager tests (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
Henryforce authored Aug 15, 2024
1 parent 1c44853 commit 8ed2474
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public protocol BLEPeripheralManager {
func updateValue(
_ value: Data,
for characteristic: CBMutableCharacteristic,
onSubscribedCentrals centrals: [CBCentral]?
onSubscribedCentrals centrals: [BLECentral]?
) -> Bool

/// Continuous observer for `CBPeripheralManagerDelegate.peripheralManagerIsReady(toUpdateSubscribers:)` results
Expand Down Expand Up @@ -148,7 +148,7 @@ public protocol BLEPeripheralManager {
/// * `BLEError.bluetoothPoweredOff`
/// * `BLEError.bluetoothInUnknownState`
/// * `BLEError.bluetoothResetting`
func observeOnSubscribe() -> AnyPublisher<(CBCentral, CBCharacteristic), Never>
func observeOnSubscribe() -> AnyPublisher<(BLECentral, CBCharacteristic), Never>

/// Continuous observer for `CBPeripheralManagerDelegate.peripheralManager(_:central:didUnsubscribeFrom:)` results
/// - Returns: Observable that emits `next` event whenever didUnsubscribeFrom occurs.
Expand All @@ -162,7 +162,7 @@ public protocol BLEPeripheralManager {
/// * `BLEError.bluetoothPoweredOff`
/// * `BLEError.bluetoothInUnknownState`
/// * `BLEError.bluetoothResetting`
func observeOnUnsubscribe() -> AnyPublisher<(CBCentral, CBCharacteristic), Never>
func observeOnUnsubscribe() -> AnyPublisher<(BLECentral, CBCharacteristic), Never>

#if os(iOS) || os(tvOS) || os(watchOS)
/// Starts publishing L2CAP channel on a subscription. It creates an infinite observable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,11 @@ final class BLEPeripheralManagerDelegate: NSObject, CBPeripheralManagerDelegate
let willRestoreState = CurrentValueSubject<[String: Any], Never>([:])
let didAddService = PassthroughSubject<(CBService, Error?), Never>()
let didReceiveWrite = PassthroughSubject<[BLEATTRequest], Never>()
let didSubscribeTo = PassthroughSubject<(CBCentral, CBCharacteristic), Never>()
let didUnsubscribeFrom = PassthroughSubject<(CBCentral, CBCharacteristic), Never>()
let didSubscribeTo = PassthroughSubject<(BLECentral, CBCharacteristic), Never>()
let didUnsubscribeFrom = PassthroughSubject<(BLECentral, CBCharacteristic), Never>()
let didPublishL2CAPChannel = PassthroughSubject<(CBL2CAPPSM, Error?), Never>()
let didUnpublishL2CAPChannel = PassthroughSubject<(CBL2CAPPSM, Error?), Never>()
private var _didOpenChannel: Any?
var didOpenChannel: PassthroughSubject<(CBL2CAPChannel?, Error?), Never> {
if _didOpenChannel == nil {
_didOpenChannel = PassthroughSubject<(CBL2CAPChannel?, Error?), Never>()
}
return _didOpenChannel as! PassthroughSubject<(CBL2CAPChannel?, Error?), Never>
}
let didOpenChannel = PassthroughSubject<(CBL2CAPChannel?, Error?), Never>()

func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
didUpdateState.send(peripheral.state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ final class StandardBLEPeripheralManager: BLEPeripheralManager {
func updateValue(
_ value: Data,
for characteristic: CBMutableCharacteristic,
onSubscribedCentrals centrals: [CBCentral]?
onSubscribedCentrals centrals: [BLECentral]?
) -> Bool {
return manager.updateValue(value, for: characteristic, onSubscribedCentrals: centrals)
let validCentrals = centrals?.compactMap { $0.associatedCentral }
return manager.updateValue(value, for: characteristic, onSubscribedCentrals: validCentrals)
}

func observeIsReadyToUpdateSubscribers() -> AnyPublisher<Void, Never> {
Expand All @@ -201,11 +202,11 @@ final class StandardBLEPeripheralManager: BLEPeripheralManager {

// MARK: Subscribing

func observeOnSubscribe() -> AnyPublisher<(CBCentral, CBCharacteristic), Never> {
func observeOnSubscribe() -> AnyPublisher<(BLECentral, CBCharacteristic), Never> {
delegate.didSubscribeTo.ensure(.poweredOn, manager: self)
}

func observeOnUnsubscribe() -> AnyPublisher<(CBCentral, CBCharacteristic), Never> {
func observeOnUnsubscribe() -> AnyPublisher<(BLECentral, CBCharacteristic), Never> {
delegate.didUnsubscribeFrom.ensure(.poweredOn, manager: self)
}

Expand Down
76 changes: 76 additions & 0 deletions Tests/BLECombineKitTests/BLEPeripheralManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,80 @@ final class BLEPeripheralManagerTests: XCTestCase {
XCTAssertEqual(managerWrapper.updateValueStack, expectedStack)
}

func testObserveIsReadyToUpdateSubscribers() {
// Given.
let expectation = XCTestExpectation(description: #function)
managerWrapper.mutableState = .poweredOn

// When.
manager.observeIsReadyToUpdateSubscribers()
.sink { _ in
expectation.fulfill()
}.store(in: &cancellables)
delegate.isReady.send()

// Then.
wait(for: [expectation], timeout: 0.01)
}

func testObserveOnSubscribe() {
// Given.
let expectation = XCTestExpectation(description: #function)
let uuid = CBUUID(string: "0xFF00")
let expectedCentral = MockBLECentral()
let expectedCharacteristic = CBMutableCharacteristic(
type: uuid,
properties: .read,
value: nil,
permissions: .readable
)
var receivedCentral: BLECentral?
var receivedCharacteristic: CBCharacteristic?
managerWrapper.mutableState = .poweredOn

// When.
manager.observeOnSubscribe()
.sink { (central, characteristic) in
receivedCentral = central
receivedCharacteristic = characteristic
expectation.fulfill()
}.store(in: &cancellables)
delegate.didSubscribeTo.send((expectedCentral, expectedCharacteristic))

// Then.
wait(for: [expectation], timeout: 0.01)
XCTAssertEqual(receivedCentral?.identifier, expectedCentral.identifier)
XCTAssertEqual(receivedCharacteristic, expectedCharacteristic)
}

func testObserveOnUnsubscribe() {
// Given.
let expectation = XCTestExpectation(description: #function)
let uuid = CBUUID(string: "0xFF00")
let expectedCentral = MockBLECentral()
let expectedCharacteristic = CBMutableCharacteristic(
type: uuid,
properties: .read,
value: nil,
permissions: .readable
)
var receivedCentral: BLECentral?
var receivedCharacteristic: CBCharacteristic?
managerWrapper.mutableState = .poweredOn

// When.
manager.observeOnUnsubscribe()
.sink { (central, characteristic) in
receivedCentral = central
receivedCharacteristic = characteristic
expectation.fulfill()
}.store(in: &cancellables)
delegate.didUnsubscribeFrom.send((expectedCentral, expectedCharacteristic))

// Then.
wait(for: [expectation], timeout: 0.01)
XCTAssertEqual(receivedCentral?.identifier, expectedCentral.identifier)
XCTAssertEqual(receivedCharacteristic, expectedCharacteristic)
}

}

0 comments on commit 8ed2474

Please sign in to comment.