diff --git a/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m b/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m index 794dc96fc9a7e8..d39bd3d402a834 100644 --- a/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m +++ b/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m @@ -184,11 +184,10 @@ + (void)setUp @"MT:Y.K90SO527JA0648G00", @"MT:-24J0AFN00I40648G00", ]) { - __auto_type * appRunner = [[MTRTestServerAppRunner alloc] initCrossTestWithAppName:@"all-clusters" - arguments:@[] - payload:payload - testsuite:self]; - XCTAssertNotNil(appRunner); + BOOL started = [self startAppWithName:@"all-clusters" + arguments:@[] + payload:payload]; + XCTAssertTrue(started); } } diff --git a/src/darwin/Framework/CHIPTests/MTROTAProviderTests.m b/src/darwin/Framework/CHIPTests/MTROTAProviderTests.m index 3c8cad690ad83b..c17ffa1821749d 100644 --- a/src/darwin/Framework/CHIPTests/MTROTAProviderTests.m +++ b/src/darwin/Framework/CHIPTests/MTROTAProviderTests.m @@ -80,7 +80,7 @@ - (NSString *)createImageFromRawImage:(NSString *)rawImage withVersion:(NSNumber - (MTRDevice *)commissionDeviceWithPayload:(NSString *)payloadString nodeID:(NSNumber *)nodeID; @end -@interface MTROTARequestorAppRunner : MTRTestServerAppRunner +@interface MTROTARequestorAppRunner : NSObject @property (nonatomic, copy) NSString * downloadFilePath; - (instancetype)initWithPayload:(NSString *)payload testcase:(MTROTAProviderTests *)testcase; @@ -99,14 +99,15 @@ - (MTRDevice *)commissionWithNodeID:(NSNumber *)nodeID - (instancetype)initWithPayload:(NSString *)payload testcase:(MTROTAProviderTests *)testcase { - __auto_type * downloadFilePath = [NSString stringWithFormat:@"/tmp/chip-ota-requestor-downloaded-image%u", [MTRTestServerAppRunner nextUniqueIndex]]; + __auto_type * downloadFilePath = [NSString stringWithFormat:@"/tmp/chip-ota-requestor-downloaded-image%u", [MTROTAProviderTests nextUniqueIndex]]; __auto_type * extraArguments = @[ @"--otaDownloadPath", downloadFilePath, @"--autoApplyImage", ]; - if (!(self = [super initWithAppName:@"ota-requestor" arguments:extraArguments payload:payload testcase:testcase])) { + BOOL started = [testcase startAppWithName:@"ota-requestor" arguments:extraArguments payload:payload]; + if (!started) { return nil; } diff --git a/src/darwin/Framework/CHIPTests/MTRPairingTests.m b/src/darwin/Framework/CHIPTests/MTRPairingTests.m index 38f7667590bca7..d2c5ca0e6f86b5 100644 --- a/src/darwin/Framework/CHIPTests/MTRPairingTests.m +++ b/src/darwin/Framework/CHIPTests/MTRPairingTests.m @@ -186,14 +186,13 @@ - (void)tearDown - (void)startServerApp { // For manual testing, CASE retry code paths can be tested by adding --faults chip_CASEServerBusy_f1 (or similar) - __auto_type * appRunner = [[MTRTestServerAppRunner alloc] initWithAppName:@"all-clusters" - arguments:@[ - @"--dac_provider", - [self absolutePathFor:@"credentials/development/commissioner_dut/struct_cd_origin_pid_vid_correct/test_case_vector.json"], - ] - payload:kOnboardingPayload - testcase:self]; - XCTAssertNotNil(appRunner); + BOOL started = [self startAppWithName:@"all-clusters" + arguments:@[ + @"--dac_provider", + [self absolutePathFor:@"credentials/development/commissioner_dut/struct_cd_origin_pid_vid_correct/test_case_vector.json"], + ] + payload:kOnboardingPayload]; + XCTAssertTrue(started); } // attestationDelegate and failSafeExtension can both be nil diff --git a/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m b/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m index db8bd300df94e1..70f15ff948611b 100644 --- a/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m +++ b/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m @@ -2437,11 +2437,10 @@ - (void)testSubscriptionPool // Start our helper apps. __auto_type * sortedKeys = [[deviceOnboardingPayloads allKeys] sortedArrayUsingSelector:@selector(compare:)]; for (NSNumber * deviceID in sortedKeys) { - __auto_type * appRunner = [[MTRTestServerAppRunner alloc] initWithAppName:@"all-clusters" - arguments:@[] - payload:deviceOnboardingPayloads[deviceID] - testcase:self]; - XCTAssertNotNil(appRunner); + BOOL started = [self startAppWithName:@"all-clusters" + arguments:@[] + payload:deviceOnboardingPayloads[deviceID]]; + XCTAssertTrue(started); } [self doTestSubscriptionPoolWithSize:1 deviceOnboardingPayloads:deviceOnboardingPayloads]; diff --git a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCase.h b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCase.h index 45886833bbdad6..3b404f6f05b1f4 100644 --- a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCase.h +++ b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCase.h @@ -57,7 +57,7 @@ NS_ASSUME_NONNULL_BEGIN * Launch a cross-test task. The task will be automatically terminated when the testsuite * tearDown happens. */ -+ (void)launchCrossTestTask:(NSTask *)task; ++ (void)launchTask:(NSTask *)task; #endif // HAVE_NSTASK /** diff --git a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCase.mm b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCase.mm index 24741f356572a4..a418393a0fca79 100644 --- a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCase.mm +++ b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCase.mm @@ -85,6 +85,11 @@ - (void)tearDown } #if HAVE_NSTASK +- (NSTask *)createTaskForPath:(NSString *)path +{ + return [self.class createTaskForPath:path]; +} + + (NSTask *)createTaskForPath:(NSString *)path { NSTask * task = [[NSTask alloc] init]; @@ -92,11 +97,6 @@ + (NSTask *)createTaskForPath:(NSString *)path return task; } -- (NSTask *)createTaskForPath:(NSString *)path -{ - return [self.class createTaskForPath:path]; -} - - (void)runTask:(NSTask *)task { NSError * launchError; @@ -121,7 +121,7 @@ - (void)launchTask:(NSTask *)task [_runningTasks addObject:task]; } -+ (void)launchCrossTestTask:(NSTask *)task ++ (void)launchTask:(NSTask *)task { [self doLaunchTask:task]; @@ -129,6 +129,11 @@ + (void)launchCrossTestTask:(NSTask *)task } #endif // HAVE_NSTASK +- (NSString *)absolutePathFor:(NSString *)matterRootRelativePath +{ + return [self.class absolutePathFor:matterRootRelativePath]; +} + + (NSString *)absolutePathFor:(NSString *)matterRootRelativePath { // Start with the absolute path to our file, then remove the suffix that @@ -140,8 +145,4 @@ + (NSString *)absolutePathFor:(NSString *)matterRootRelativePath return [NSString pathWithComponents:pathComponents]; } -- (NSString *)absolutePathFor:(NSString *)matterRootRelativePath -{ - return [self.class absolutePathFor:matterRootRelativePath]; -} @end diff --git a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestServerAppRunner.h b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestServerAppRunner.h index ff36a20fefe3fe..8eb9384f67e01e 100644 --- a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestServerAppRunner.h +++ b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestServerAppRunner.h @@ -16,21 +16,14 @@ #import -@class MTRTestCase; +#import "MTRTestCase.h" NS_ASSUME_NONNULL_BEGIN -/** - * A representation of a server application instance. - * - * Server applications are assumed to be compiled into out/debug/${APPNAME}-app, - * with the binary being out/debug/${APPNAME}-app/chip-${APPNAME}-app. - */ -@interface MTRTestServerAppRunner : NSObject +@interface MTRTestCase (ServerAppRunner) /** - * Initialize the app runner with the given app name, arguments, setup payload, and testcase - * instance. + * Start a server app with the given app name, arguments, and setup payload. * * The payload will be used to determine the discriminator and passcode * arguments the app should use, in addition to the provided arguments. @@ -43,15 +36,13 @@ NS_ASSUME_NONNULL_BEGIN * subtracting 1111 from the discriminator and adding 5542 (so as not to collide * with any existing Matter things running on 5540/5541). */ -- (instancetype)initWithAppName:(NSString *)name arguments:(NSArray *)arguments payload:(NSString *)payload testcase:(MTRTestCase *)testcase; +- (BOOL)startAppWithName:(NSString *)name arguments:(NSArray *)arguments payload:(NSString *)payload; /** - * Same thing, but initialize as a "cross test" helper, which is not killed at - * the end of the current test (but is killed at the end of the current suite). - * - * The passed-in testsuite must be a subclass of MTRTestCase. + * Same thing, but the server will be killed at the end of the current suite, + * and is not bound to a particular test in the suite. */ -- (instancetype)initCrossTestWithAppName:(NSString *)name arguments:(NSArray *)arguments payload:(NSString *)payload testsuite:(Class)testsuite; ++ (BOOL)startAppWithName:(NSString *)name arguments:(NSArray *)arguments payload:(NSString *)payload; /** * Get the unique index that will be used for the next initialization. This diff --git a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestServerAppRunner.m b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestServerAppRunner.m index 8580ce9065c1c8..155f0355909e8a 100644 --- a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestServerAppRunner.m +++ b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestServerAppRunner.m @@ -16,7 +16,6 @@ #import -#import "MTRTestCase.h" #import "MTRTestServerAppRunner.h" static unsigned sAppRunnerIndex = 1; @@ -29,24 +28,12 @@ static const uint16_t kBasePort = 5542 - kMinDiscriminator; #endif // HAVE_NSTASK -@implementation MTRTestServerAppRunner { - unsigned _uniqueIndex; -#if HAVE_NSTASK - NSTask * _appTask; -#endif -} +@implementation MTRTestCase (ServerAppRunner) -- (instancetype)initInternalWithAppName:(NSString *)name arguments:(NSArray *)arguments payload:(NSString *)payload +#if HAVE_NSTASK ++ (NSTask *)doStartAppWithName:(NSString *)name arguments:(NSArray *)arguments payload:(NSString *)payload { -#if !HAVE_NSTASK - XCTFail("Unable to start server app when we do not have NSTask"); - return nil; -#else // HAVE_NSTASK - if (!(self = [super init])) { - return nil; - } - - _uniqueIndex = sAppRunnerIndex++; + __auto_type uniqueIndex = sAppRunnerIndex++; NSError * error; __auto_type * parsedPayload = [MTRSetupPayload setupPayloadWithOnboardingPayload:payload error:&error]; @@ -61,7 +48,7 @@ - (instancetype)initInternalWithAppName:(NSString *)name arguments:(NSArray *)arguments payload:(NSString *)payload testcase:(MTRTestCase *)testcase +- (BOOL)startAppWithName:(NSString *)name arguments:(NSArray *)arguments payload:(NSString *)payload { - if (!(self = [self initInternalWithAppName:name arguments:arguments payload:payload])) { - return nil; - } - - [testcase launchTask:_appTask]; - - return self; +#if !HAVE_NSTASK + XCTFail("Unable to start server app when we do not have NSTask"); + return NO; +#else + [self launchTask:[self.class doStartAppWithName:name arguments:arguments payload:payload]]; + return YES; +#endif // HAVE_NSTASK } -- (instancetype)initCrossTestWithAppName:(NSString *)name arguments:(NSArray *)arguments payload:(NSString *)payload testsuite:(Class)testsuite ++ (BOOL)startAppWithName:(NSString *)name arguments:(NSArray *)arguments payload:(NSString *)payload { - if (![testsuite isSubclassOfClass:MTRTestCase.class]) { - NSLog(@"%@ is not a subclass of MTRTestCase", testsuite); - return nil; - } - - if (!(self = [self initInternalWithAppName:name arguments:arguments payload:payload])) { - return nil; - } - - [MTRTestCase launchCrossTestTask:_appTask]; - - return self; +#if !HAVE_NSTASK + XCTFail("Unable to start server app when we do not have NSTask"); + return NO; +#else + [self launchTask:[self doStartAppWithName:name arguments:arguments payload:payload]]; + return YES; +#endif // HAVE_NSTASK } + (unsigned)nextUniqueIndex