Skip to content

Commit

Permalink
Additional fixes and unit test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jtung-apple committed Sep 11, 2024
1 parent 2ae29fb commit 3ace852
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
5 changes: 1 addition & 4 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1788,7 +1788,7 @@ + (void)forceLocalhostAdvertisingOnly

#pragma mark - MTRDeviceControllerDelegate management

// Note these are implemented in the base class so that XPC subclass can use it as well when it
// Note these are implemented in the base class so that XPC subclass can use it as well
- (void)setDeviceControllerDelegate:(id<MTRDeviceControllerDelegate>)delegate queue:(dispatch_queue_t)queue
{
@synchronized(self) {
Expand Down Expand Up @@ -1842,9 +1842,6 @@ - (void)removeDeviceControllerDelegate:(id<MTRDeviceControllerDelegate>)delegate

if (delegateInfoToRemove) {
[_delegates removeObject:delegateInfoToRemove];
if (_strongDelegateForSetDelegateAPI == delegate) {
_strongDelegateForSetDelegateAPI = nil;
}
MTR_LOG("%@ removeDeviceControllerDelegate: removed %p remaining %lu", self, delegate, static_cast<unsigned long>(_delegates.count));
} else {
MTR_LOG("%@ removeDeviceControllerDelegate: delegate %p not found in %lu", self, delegate, static_cast<unsigned long>(_delegates.count));
Expand Down
20 changes: 19 additions & 1 deletion src/darwin/Framework/CHIPTests/MTRPairingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,35 @@ @interface MTRPairingTestMonitoringControllerDelegate : NSObject <MTRDeviceContr
@property (atomic, readwrite) BOOL commissioningSessionEstablishmentDoneCalled;
@property (atomic, readwrite) BOOL commissioningCompleteCalled;
@property (atomic, readwrite) BOOL readCommissioningInfoCalled;
@property (atomic, readwrite, strong) XCTestExpectation * allCallbacksCalledExpectation;
@end

@implementation MTRPairingTestMonitoringControllerDelegate
- (NSString *)description
{
return [NSString stringWithFormat:@"<MTRPairingTestMonitoringControllerDelegate: %p statusUpdateCalled %@ commissioningSessionEstablishmentDoneCalled %@ commissioningCompleteCalled %@ readCommissioningInfoCalled %@>", self, MTR_YES_NO(_statusUpdateCalled), MTR_YES_NO(_commissioningSessionEstablishmentDoneCalled), MTR_YES_NO(_commissioningCompleteCalled), MTR_YES_NO(_readCommissioningInfoCalled)];
}

- (void)_checkIfAllCallbacksCalled
{
if (self.allCallbacksCalledExpectation) {
if (self.statusUpdateCalled && self.commissioningSessionEstablishmentDoneCalled && self.commissioningCompleteCalled && self.readCommissioningInfoCalled) {
[self.allCallbacksCalledExpectation fulfill];
self.allCallbacksCalledExpectation = nil;
}
}
}

- (void)controller:(MTRDeviceController *)controller statusUpdate:(MTRCommissioningStatus)status
{
self.statusUpdateCalled = YES;
[self _checkIfAllCallbacksCalled];
}

- (void)controller:(MTRDeviceController *)controller commissioningSessionEstablishmentDone:(NSError * _Nullable)error
{
self.commissioningSessionEstablishmentDoneCalled = YES;
[self _checkIfAllCallbacksCalled];
}

- (void)controller:(MTRDeviceController *)controller
Expand All @@ -161,11 +175,13 @@ - (void)controller:(MTRDeviceController *)controller
metrics:(MTRMetrics *)metrics
{
self.commissioningCompleteCalled = YES;
[self _checkIfAllCallbacksCalled];
}

- (void)controller:(MTRDeviceController *)controller readCommissioningInfo:(MTRProductIdentity *)info
{
self.readCommissioningInfoCalled = YES;
[self _checkIfAllCallbacksCalled];
}
@end

Expand Down Expand Up @@ -259,6 +275,8 @@ - (void)doPairingTestWithAttestationDelegate:(id<MTRDeviceAttestationDelegate>)a

// Test that a monitoring delegate works
__auto_type * monitoringControllerDelegate = [[MTRPairingTestMonitoringControllerDelegate alloc] init];
XCTestExpectation * allCallbacksCalledExpectation = [self expectationWithDescription:@"All callbacks called on monitoring delegate"];
monitoringControllerDelegate.allCallbacksCalledExpectation = allCallbacksCalledExpectation;
[sController addDeviceControllerDelegate:monitoringControllerDelegate queue:callbackQueue];
XCTAssertEqual([sController unitTestDelegateCount], 2);

Expand All @@ -278,7 +296,7 @@ - (void)doPairingTestWithAttestationDelegate:(id<MTRDeviceAttestationDelegate>)a
XCTAssertTrue([sController setupCommissioningSessionWithPayload:payload newNodeID:@(sDeviceId) error:&error]);
XCTAssertNil(error);

[self waitForExpectations:@[ expectation ] timeout:kPairingTimeoutInSeconds];
[self waitForExpectations:@[ expectation, allCallbacksCalledExpectation ] timeout:kPairingTimeoutInSeconds];
XCTAssertNil(controllerDelegate.commissioningCompleteError);

// Test that the monitoring delegate got all the callbacks
Expand Down

0 comments on commit 3ace852

Please sign in to comment.