diff --git a/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m b/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m index 8c9eb603d08518..2acd1c2d379d83 100644 --- a/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m +++ b/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m @@ -36,7 +36,7 @@ static MTRDeviceController * sController = nil; @interface DeviceScannerDelegate : NSObject -@property (nonatomic) XCTestExpectation * expectation; +@property (nonatomic, nullable) XCTestExpectation * expectation; @property (nonatomic) NSNumber * resultsCount; - (instancetype)initWithExpectation:(XCTestExpectation *)expectation; @@ -56,8 +56,26 @@ - (instancetype)initWithExpectation:(XCTestExpectation *)expectation return self; } +- (instancetype)init +{ + if (!(self = [super init])) { + return nil; + } + + _resultsCount = 0; + _expectation = nil; + return self; +} + - (void)controller:(MTRDeviceController *)controller didFindCommissionableDevice:(MTRCommissionableBrowserResult *)device { + if (self.expectation == nil) { + // We are not actually supposed to be looking at results; don't do it, + // because we may be starting/stopping browse multiple times and seeing + // odd numbers of results. + return; + } + _resultsCount = @(_resultsCount.unsignedLongValue + 1); if ([_resultsCount isEqual:@(kExpectedDiscoveredDevicesCount)]) { [self.expectation fulfill];