Skip to content

Commit

Permalink
Added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jtung-apple committed Sep 9, 2024
1 parent 198edc1 commit 6d0ddeb
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP/MTRDefines_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,7 @@ typedef struct {} variable_hidden_by_mtr_hide;
} \
}
#endif

#ifndef YES_NO
#define YES_NO(x) ((x) ? @"YES" : @"NO")
#endif
16 changes: 16 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,11 @@ - (void)setDeviceControllerDelegate:(id<MTRDeviceControllerDelegate>)delegate qu
{
@synchronized(self) {
if (_strongDelegateForSetDelegateAPI) {
if (_strongDelegateForSetDelegateAPI == delegate) {
MTR_LOG("%@ setDeviceControllerDelegate: delegate %p is already set", self, delegate);
return;
}

MTR_LOG("%@ setDeviceControllerDelegate: replacing %p with %p", self, _strongDelegateForSetDelegateAPI, delegate);
[self removeDeviceControllerDelegate:_strongDelegateForSetDelegateAPI];
}
Expand All @@ -1799,6 +1804,10 @@ - (void)addDeviceControllerDelegate:(id<MTRDeviceControllerDelegate>)delegate qu
- (void)removeDeviceControllerDelegate:(id<MTRDeviceControllerDelegate>)delegate
{
@synchronized(self) {
if (_strongDelegateForSetDelegateAPI == delegate) {
_strongDelegateForSetDelegateAPI = nil;
}

__block MTRDeviceControllerDelegateInfo * delegateInfoToRemove = nil;
[self _iterateDelegateInfoWithBlock:^(MTRDeviceControllerDelegateInfo * delegateInfo) {
if (delegateInfo.delegate == delegate) {
Expand Down Expand Up @@ -1865,6 +1874,13 @@ - (void)_callDelegatesWithBlock:(void (^_Nullable)(id<MTRDeviceControllerDelegat
MTR_LOG("%@ %lu delegates called for %s", self, static_cast<unsigned long>(delegatesCalled), logString);
}

#if DEBUG
- (NSUInteger)unitTestDelegateCount
{
return [self _iterateDelegateInfoWithBlock:nil];
}
#endif

- (void)controller:(MTRDeviceController *)controller statusUpdate:(MTRCommissioningStatus)status
{
[self _callDelegatesWithBlock:^(id<MTRDeviceControllerDelegate> delegate) {
Expand Down
4 changes: 0 additions & 4 deletions src/darwin/Framework/CHIP/MTRError_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@

NS_ASSUME_NONNULL_BEGIN

#ifndef YES_NO
#define YES_NO(x) ((x) ? @"YES" : @"NO")
#endif

MTR_DIRECT_MEMBERS
@interface MTRError : NSObject
+ (NSError *)errorWithCode:(MTRErrorCode)code;
Expand Down
58 changes: 58 additions & 0 deletions src/darwin/Framework/CHIPTests/MTRPairingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
// module headers
#import <Matter/Matter.h>

#import "MTRDefines_Internal.h"
#import "MTRErrorTestUtils.h"
#import "MTRTestCase+ServerAppRunner.h"
#import "MTRTestCase.h"
#import "MTRTestDeclarations.h"
#import "MTRTestKeys.h"
#import "MTRTestStorage.h"

Expand Down Expand Up @@ -131,6 +133,42 @@ - (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSEr

@end

@interface MTRPairingTestMonitoringControllerDelegate : NSObject <MTRDeviceControllerDelegate>
@property(nonatomic, readonly) BOOL statusUpdateCalled;
@property(nonatomic, readonly) BOOL commissioningSessionEstablishmentDoneCalled;
@property(nonatomic, readonly) BOOL commissioningCompleteCalled;
@property(nonatomic, readonly) BOOL readCommissioningInfoCalled;
@end

@implementation MTRPairingTestMonitoringControllerDelegate
- (NSString *)description
{
return [NSString stringWithFormat:@"<MTRPairingTestMonitoringControllerDelegate: %p statusUpdateCalled %@ commissioningSessionEstablishmentDoneCalled %@ commissioningCompleteCalled %@ readCommissioningInfoCalled %@>", self, YES_NO(_statusUpdateCalled), YES_NO(_commissioningSessionEstablishmentDoneCalled), YES_NO(_commissioningCompleteCalled), YES_NO(_readCommissioningInfoCalled)];
}
- (void)controller:(MTRDeviceController *)controller statusUpdate:(MTRCommissioningStatus)status
{
_statusUpdateCalled = YES;
}

- (void)controller:(MTRDeviceController *)controller commissioningSessionEstablishmentDone:(NSError * _Nullable)error
{
_commissioningSessionEstablishmentDoneCalled = YES;
}

- (void)controller:(MTRDeviceController *)controller
commissioningComplete:(NSError * _Nullable)error
nodeID:(NSNumber * _Nullable)nodeID
metrics:(MTRMetrics *)metrics
{
_commissioningCompleteCalled = YES;
}

- (void)controller:(MTRDeviceController *)controller readCommissioningInfo:(MTRProductIdentity *)info
{
_readCommissioningInfoCalled = YES;
}
@end

@interface MTRPairingTests : MTRTestCase
@property (nullable) MTRPairingTestControllerDelegate * controllerDelegate;
@end
Expand Down Expand Up @@ -219,6 +257,19 @@ - (void)doPairingTestWithAttestationDelegate:(id<MTRDeviceAttestationDelegate>)a
[sController setDeviceControllerDelegate:controllerDelegate queue:callbackQueue];
self.controllerDelegate = controllerDelegate;

// Test that a monitoring delegate works
__auto_type * monitoringControllerDelegate = [[MTRPairingTestMonitoringControllerDelegate alloc] init];
[sController addDeviceControllerDelegate:monitoringControllerDelegate queue:callbackQueue];
XCTAssertEqual([sController unitTestDelegateCount], 2);

// Test that the addDeviceControllerDelegate delegate is held weakly by the controller
@autoreleasepool {
__auto_type * monitoringControllerDelegate = [[MTRPairingTestMonitoringControllerDelegate alloc] init];
[sController addDeviceControllerDelegate:monitoringControllerDelegate queue:callbackQueue];
XCTAssertEqual([sController unitTestDelegateCount], 3);
}
XCTAssertEqual([sController unitTestDelegateCount], 2);

NSError * error;
__auto_type * payload = [MTRSetupPayload setupPayloadWithOnboardingPayload:kOnboardingPayload error:&error];
XCTAssertNotNil(payload);
Expand All @@ -229,6 +280,13 @@ - (void)doPairingTestWithAttestationDelegate:(id<MTRDeviceAttestationDelegate>)a

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

// Test that the monitoring delegate got all the callbacks
XCTAssertTrue(monitoringControllerDelegate.statusUpdateCalled);
XCTAssertTrue(monitoringControllerDelegate.commissioningSessionEstablishmentDoneCalled);
XCTAssertTrue(monitoringControllerDelegate.commissioningCompleteCalled);
XCTAssertTrue(monitoringControllerDelegate.readCommissioningInfoCalled);
[sController removeDeviceControllerDelegate:monitoringControllerDelegate];
}

- (void)test001_PairWithoutAttestationDelegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#import <Foundation/Foundation.h>
#import <Matter/Matter.h>

// For MTRDeviceDataValueDictionary:
#import "MTRDevice_Internal.h"

NS_ASSUME_NONNULL_BEGIN

#pragma mark - Declarations for internal methods
Expand Down Expand Up @@ -55,6 +58,7 @@ NS_ASSUME_NONNULL_BEGIN
#ifdef DEBUG
@interface MTRDeviceController (TestDebug)
- (NSDictionary<NSNumber *, NSNumber *> *)unitTestGetDeviceAttributeCounts;
- (NSUInteger)unitTestDelegateCount;
@end

@interface MTRBaseDevice (TestDebug)
Expand Down

0 comments on commit 6d0ddeb

Please sign in to comment.