Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions TestLib/AppleInternals/GREYXCTestAppleInternals.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,20 @@
+ (nullable id)sharedClient;

@end

/**
* XCTest class representing the active test run's configuration.
*/
@interface XCTestConfiguration : NSObject <NSSecureCoding, NSCopying>

/**
* Returns the singleton active test configuration instance.
*/
+ (nonnull instancetype)activeTestConfiguration;

/**
* The bundle identifier of the primary application under test.
*/
@property(copy, nullable) NSString *targetApplicationBundleID;

@end
71 changes: 46 additions & 25 deletions TestLib/EarlGreyImpl/XCUIApplication+GREYTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#import "GREYLogger.h"
#import "GREYSetup.h"
#import "GREYSwizzler.h"
#import "GREYXCTestAppleInternals.h"
#import "GREYTestConfiguration.h"
#import "XCUIApplication+GREYEnvironment.h"

Expand All @@ -52,36 +53,56 @@ + (NSString *)greyTestRigName {
- (void)grey_launch {
[self modifyKeyboardSettings];

// Setup the Launch Environments.
[self grey_configureApplicationForLaunch];
// Setup the Launch Arguments for eDO.
NSMutableArray<NSString *> *launchArgs = [self.launchArguments mutableCopy];
if (!launchArgs) {
launchArgs = [[NSMutableArray alloc] init];
}
GREYTestApplicationDistantObject *testDistantObject =
GREYTestApplicationDistantObject.sharedInstance;
[launchArgs addObjectsFromArray:@[
@"-edoTestPort",
@(testDistantObject.servicePort).stringValue,
@"-IsRunningEarlGreyTest",
@"YES",
]];

NSString *loggingValue = [NSProcessInfo processInfo].environment[kGREYAllowVerboseLogging];
if (loggingValue) {
AddVerboseLoggingIfNeeded(launchArgs, loggingValue);
}
self.launchArguments = launchArgs;
static NSString *gPrimaryAppBundleID;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
@try {
gPrimaryAppBundleID = XCTestConfiguration.activeTestConfiguration.targetApplicationBundleID;
} @catch (NSException *exception) {
GREYLog(@"Failed to read targetApplicationBundleID from XCTestConfiguration: %@", exception);
}
});

// Reset the port number for the app under test before every -[XCUIApplication launch] call.
[testDistantObject resetHostArguments];
// It is the primary app under test if the bundleID matches the static target,
// or if it is nil/empty (representing the default init rig target).
NSString *currentBundleID = self.bundleID;
BOOL isAppUnderTest = (gPrimaryAppBundleID.length == 0) || (currentBundleID.length == 0) ||
[gPrimaryAppBundleID isEqualToString:currentBundleID];
GREYLog(@"Launching application with bundle identifier: '%@' (app under test: '%@').",
currentBundleID, gPrimaryAppBundleID);

if (isAppUnderTest) {
// Setup the Launch Environments.
[self grey_configureApplicationForLaunch];
// Setup the Launch Arguments for eDO.
NSMutableArray<NSString *> *launchArgs = [self.launchArguments mutableCopy];
if (!launchArgs) {
launchArgs = [[NSMutableArray alloc] init];
}
GREYTestApplicationDistantObject *testDistantObject =
GREYTestApplicationDistantObject.sharedInstance;
[launchArgs addObjectsFromArray:@[
@"-edoTestPort",
@(testDistantObject.servicePort).stringValue,
@"-IsRunningEarlGreyTest",
@"YES",
]];

NSString *loggingValue = [NSProcessInfo processInfo].environment[kGREYAllowVerboseLogging];
if (loggingValue) {
AddVerboseLoggingIfNeeded(launchArgs, loggingValue);
}
self.launchArguments = launchArgs;

// Reset the port number for the app under test before every -[XCUIApplication launch] call.
[testDistantObject resetHostArguments];
}

NSTimer *validTimer = AddTimerForLaunchTimeout();
INVOKE_ORIGINAL_IMP(void, @selector(grey_launch));
[validTimer invalidate];
// When the identifier is @c nil or empty, it is the TestRig application being launched.
if (self.identifier.length == 0) {

if (isAppUnderTest) {
objc_setAssociatedObject([XCUIApplication class], @selector(greyTestRigName), self.label,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
Expand Down