Skip to content

Commit

Permalink
Use GetConnectedDevice in Darwin tests (#10403)
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-apple authored and pull[bot] committed Oct 20, 2021
1 parent fe46fd3 commit d51d007
Show file tree
Hide file tree
Showing 5 changed files with 840 additions and 820 deletions.
1 change: 0 additions & 1 deletion src/darwin/Framework/CHIP/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ typedef void (^CHIPDeviceConnectionCallback)(CHIPDevice * _Nullable device, NSEr
- (BOOL)getConnectedDevice:(uint64_t)deviceID
queue:(dispatch_queue_t)queue
completionHandler:(CHIPDeviceConnectionCallback)completionHandler;
- (nullable CHIPDevice *)getPairedDevice:(uint64_t)deviceID error:(NSError * __autoreleasing *)error;

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
Expand Down
25 changes: 0 additions & 25 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -451,31 +451,6 @@ - (BOOL)getConnectedDevice:(uint64_t)deviceID
return YES;
}

- (CHIPDevice *)getPairedDevice:(uint64_t)deviceID error:(NSError * __autoreleasing *)error
{
__block CHIPDevice * chipDevice = nil;
__block CHIP_ERROR errorCode = CHIP_ERROR_INCORRECT_STATE;
if (![self isRunning]) {
[self checkForError:errorCode logMsg:kErrorNotRunning error:error];
return chipDevice;
}
dispatch_sync(_chipWorkQueue, ^{
chip::Controller::Device * device = nullptr;

if ([self isRunning]) {
errorCode = self.cppCommissioner->GetDevice(deviceID, &device);
}

if ([self checkForError:errorCode logMsg:kErrorGetPairedDevice error:error]) {
return;
}

chipDevice = [[CHIPDevice alloc] initWithDevice:device];
});

return chipDevice;
}

- (void)setListenPort:(uint16_t)port
{
_listenPort = port;
Expand Down
49 changes: 36 additions & 13 deletions src/darwin/Framework/CHIP/templates/clusters-tests.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#import <XCTest/XCTest.h>

const uint16_t kPairingTimeoutInSeconds = 10;
const uint16_t kAddressResolveTimeoutInSeconds = 10;
const uint16_t kCASESetupTimeoutInSeconds = 30;
const uint16_t kTimeoutInSeconds = 3;
const uint64_t kDeviceId = 1;
const uint16_t kDiscriminator = 3840;
Expand All @@ -19,6 +21,10 @@ const uint16_t kRemotePort = 5540;
const uint16_t kLocalPort = 5541;
NSString * kAddress = @"::1";

// This test suite reuses a device object to speed up the test process for CI.
// The following global variable holds the reference to the device object.
static CHIPDevice * mConnectedDevice;

// Test Util APIs
void WaitForMs(XCTestExpectation * expectation, dispatch_queue_t queue, unsigned int ms)
{
Expand All @@ -27,17 +33,10 @@ void WaitForMs(XCTestExpectation * expectation, dispatch_queue_t queue, unsigned
});
}

CHIPDevice * GetPairedDevice(uint64_t deviceId)
CHIPDevice * GetConnectedDevice()
{
CHIPDeviceController * controller = [CHIPDeviceController sharedController];
XCTAssertNotNil(controller);

NSError * pairingError;
CHIPDevice * device = [controller getPairedDevice:deviceId error:&pairingError];
XCTAssertEqual(pairingError.code, 0);
XCTAssertNotNil(device);

return device;
XCTAssertNotNil(mConnectedDevice);
return mConnectedDevice;
}

@interface CHIPToolPairingDelegate : NSObject <CHIPDevicePairingDelegate>
Expand All @@ -60,6 +59,13 @@ CHIPDevice * GetPairedDevice(uint64_t deviceId)
[_expectation fulfill];
_expectation = nil;
}

- (void)onAddressUpdated:(NSError *)error
{
XCTAssertEqual(error.code, 0);
[_expectation fulfill];
_expectation = nil;
}
@end

@interface CHIPClustersTests : XCTestCase
Expand Down Expand Up @@ -98,6 +104,23 @@ CHIPDevice * GetPairedDevice(uint64_t deviceId)
XCTAssertEqual(error.code, 0);

[self waitForExpectationsWithTimeout:kPairingTimeoutInSeconds handler:nil];

XCTestExpectation * addressExpectation = [self expectationWithDescription:@"Address Updated"];
pairing.expectation = addressExpectation;
[controller updateDevice:kDeviceId fabricId:0];

[self waitForExpectationsWithTimeout:kAddressResolveTimeoutInSeconds handler:nil];

__block XCTestExpectation * connectionExpectation = [self expectationWithDescription:@"CASE established"];
[controller getConnectedDevice:kDeviceId
queue:dispatch_get_main_queue()
completionHandler:^(CHIPDevice * _Nullable device, NSError * _Nullable error) {
XCTAssertEqual(error.code, 0);
[connectionExpectation fulfill];
connectionExpectation = nil;
mConnectedDevice = device;
}];
[self waitForExpectationsWithTimeout:kCASESetupTimeoutInSeconds handler:nil];
}

- (void)testShutdownStack
Expand All @@ -117,7 +140,7 @@ CHIPDevice * GetPairedDevice(uint64_t deviceId)
{
XCTestExpectation * expectation = [self expectationWithDescription:@"ReuseCHIPClusterObjectFirstCall"];

CHIPDevice * device = GetPairedDevice(kDeviceId);
CHIPDevice * device = GetConnectedDevice();
dispatch_queue_t queue = dispatch_get_main_queue();
CHIPTestCluster * cluster = [[CHIPTestCluster alloc] initWithDevice:device endpoint:1 queue:queue];
XCTAssertNotNil(cluster);
Expand Down Expand Up @@ -153,7 +176,7 @@ CHIPDevice * GetPairedDevice(uint64_t deviceId)
{
XCTestExpectation * expectation = [self expectationWithDescription:@"{{asUpperCamelCase parent.name}}ReadAttribute{{asUpperCamelCase name}}WithResponseHandler"];

CHIPDevice * device = GetPairedDevice(kDeviceId);
CHIPDevice * device = GetConnectedDevice();
dispatch_queue_t queue = dispatch_get_main_queue();
CHIP{{asUpperCamelCase parent.name}} * cluster = [[CHIP{{asUpperCamelCase parent.name}} alloc] initWithDevice:device endpoint:{{asExpectedEndpointForCluster (asUpperCamelCase parent.name)}} queue:queue];
XCTAssertNotNil(cluster);
Expand All @@ -172,7 +195,7 @@ CHIPDevice * GetPairedDevice(uint64_t deviceId)
{
XCTestExpectation * expectation = [self expectationWithDescription:@"{{asUpperCamelCase parent.name}}WriteAttribute{{asUpperCamelCase name}}WithValue"];

CHIPDevice * device = GetPairedDevice(kDeviceId);
CHIPDevice * device = GetConnectedDevice();
dispatch_queue_t queue = dispatch_get_main_queue();
CHIP{{asUpperCamelCase parent.name}} * cluster = [[CHIP{{asUpperCamelCase parent.name}} alloc] initWithDevice:device endpoint:{{asExpectedEndpointForCluster (asUpperCamelCase parent.name)}} queue:queue];
XCTAssertNotNil(cluster);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bool testSendCluster{{parent.filename}}_{{asTestIndex index}}_{{asUpperCamelCase
dispatch_queue_t queue = dispatch_get_main_queue();
{{command}}(expectation, queue{{#chip_tests_item_parameters}}, {{definedValue}}{{/chip_tests_item_parameters}});
{{else}}
CHIPDevice * device = GetPairedDevice(kDeviceId);
CHIPDevice * device = GetConnectedDevice();
dispatch_queue_t queue = dispatch_get_main_queue();
CHIPTest{{asUpperCamelCase cluster}} * cluster = [[CHIPTest{{asUpperCamelCase cluster}} alloc] initWithDevice:device endpoint:{{endpoint}} queue:queue];
XCTAssertNotNil(cluster);
Expand Down
Loading

0 comments on commit d51d007

Please sign in to comment.