Skip to content

Commit 6d1c4be

Browse files
mebrianlinmobile-devx-github-bot
authored andcommitted
Only reset host when app under test is launched.
This prevents unnecessarily resetting when another non-EarlGrey is launched, causing subsequent EarlGrey calls to fail. PiperOrigin-RevId: 912235927
1 parent cd2f3c2 commit 6d1c4be

5 files changed

Lines changed: 84 additions & 2 deletions

File tree

CommonLib/Config/GREYConfigKey.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,13 @@ GREY_EXTERN GREYConfigKey const kGREYConfigKeyAutoUntrackMDCActivityIndicators;
185185
*/
186186
GREY_EXTERN GREYConfigKey const kGREYConfigKeyAutoHideScrollViewIndicators;
187187

188+
/**
189+
* Configuration that holds a list of regular expressions for bundle identifiers
190+
* that should not be configured for EarlGrey (e.g. system apps). EarlGrey will
191+
* skip injecting eDO arguments and resetting the host port for these apps.
192+
*
193+
* Accepted values: @c NSArray of valid regular expressions as @c NSString.
194+
*/
195+
GREY_EXTERN GREYConfigKey const kGREYConfigKeyBundleIDsExcludedFromSetup;
196+
188197
NS_ASSUME_NONNULL_END

CommonLib/Config/GREYConfigKey.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@
4040
@"kGREYConfigKeyAutoUntrackMDCActivityIndicators";
4141
GREYConfigKey const kGREYConfigKeyAutoHideScrollViewIndicators =
4242
@"GREYConfigKeyAutoHideScrollViewIndicators";
43+
GREYConfigKey const kGREYConfigKeyBundleIDsExcludedFromSetup =
44+
@"GREYConfigKeyBundleIDsExcludedFromSetup";

TestLib/AppleInternals/GREYXCTestAppleInternals.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,20 @@
5959
+ (nullable id)sharedClient;
6060

6161
@end
62+
63+
/**
64+
* XCTest class representing the active test run's configuration.
65+
*/
66+
@interface XCTestConfiguration : NSObject <NSSecureCoding, NSCopying>
67+
68+
/**
69+
* Returns the singleton active test configuration instance.
70+
*/
71+
+ (nonnull instancetype)activeTestConfiguration;
72+
73+
/**
74+
* The bundle identifier of the primary application under test.
75+
*/
76+
@property(copy, nullable) NSString *targetApplicationBundleID;
77+
78+
@end

TestLib/Config/GREYTestConfiguration.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ - (instancetype)init {
6969
[self setDefaultValue:@(kGREYAppLaunchTimeout) forConfigKey:kGREYConfigKeyAppLaunchTimeout];
7070
[self setDefaultValue:@NO forConfigKey:kGREYConfigKeyAutoUntrackMDCActivityIndicators];
7171
[self setDefaultValue:@NO forConfigKey:kGREYConfigKeyAutoHideScrollViewIndicators];
72+
[self setDefaultValue:@[
73+
@"^com\\.apple\\..*",
74+
]
75+
forConfigKey:kGREYConfigKeyBundleIDsExcludedFromSetup];
7276
}
7377
return self;
7478
}

TestLib/EarlGreyImpl/XCUIApplication+GREYTest.m

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#import "GREYLogger.h"
2828
#import "GREYSetup.h"
2929
#import "GREYSwizzler.h"
30+
#import "GREYXCTestAppleInternals.h"
3031
#import "GREYTestConfiguration.h"
3132
#import "XCUIApplication+GREYEnvironment.h"
3233

@@ -52,6 +53,55 @@ + (NSString *)greyTestRigName {
5253
- (void)grey_launch {
5354
[self modifyKeyboardSettings];
5455

56+
static NSString *gPrimaryAppBundleID;
57+
static dispatch_once_t onceToken;
58+
dispatch_once(&onceToken, ^{
59+
@try {
60+
gPrimaryAppBundleID = XCTestConfiguration.activeTestConfiguration.targetApplicationBundleID;
61+
} @catch (NSException *exception) {
62+
GREYLog(@"Failed to read targetApplicationBundleID from XCTestConfiguration: %@", exception);
63+
}
64+
});
65+
66+
// It is the primary app under test if the bundleID matches the static target,
67+
// or if it is nil/empty (representing the default init rig target).
68+
NSString *currentBundleID = self.bundleID;
69+
BOOL isPrimaryApp = (gPrimaryAppBundleID.length == 0) || (currentBundleID.length == 0) ||
70+
[gPrimaryAppBundleID isEqualToString:currentBundleID];
71+
72+
NSArray<NSString *> *excludedRegexes =
73+
GREY_CONFIG_ARRAY(kGREYConfigKeyBundleIDsExcludedFromSetup);
74+
BOOL isExcluded = NO;
75+
if (currentBundleID.length > 0) {
76+
NSError *error;
77+
for (NSString *regexStr in excludedRegexes) {
78+
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr
79+
options:0
80+
error:&error];
81+
GREYFatalAssertWithMessage(!error, @"Invalid regex:\"%@\". See error: %@", regexStr, error);
82+
NSRange firstMatch =
83+
[regex rangeOfFirstMatchInString:currentBundleID
84+
options:0
85+
range:NSMakeRange(0, [currentBundleID length])];
86+
if (firstMatch.location != NSNotFound) {
87+
isExcluded = YES;
88+
break;
89+
}
90+
}
91+
}
92+
93+
if (isExcluded) {
94+
GREYLog(@"Launching excluded application: %@. Skipping EarlGrey setup.", currentBundleID);
95+
INVOKE_ORIGINAL_IMP(void, @selector(grey_launch));
96+
return;
97+
}
98+
99+
if (!isPrimaryApp) {
100+
GREYLog(@"WARNING: Launching secondary EarlGrey application: %@. "
101+
@"Ensure it links EarlGrey AppFramework and matches your multi-app setup.",
102+
currentBundleID);
103+
}
104+
55105
// Setup the Launch Environments.
56106
[self grey_configureApplicationForLaunch];
57107
// Setup the Launch Arguments for eDO.
@@ -80,8 +130,8 @@ - (void)grey_launch {
80130
NSTimer *validTimer = AddTimerForLaunchTimeout();
81131
INVOKE_ORIGINAL_IMP(void, @selector(grey_launch));
82132
[validTimer invalidate];
83-
// When the identifier is @c nil or empty, it is the TestRig application being launched.
84-
if (self.identifier.length == 0) {
133+
134+
if (isPrimaryApp) {
85135
objc_setAssociatedObject([XCUIApplication class], @selector(greyTestRigName), self.label,
86136
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
87137
}

0 commit comments

Comments
 (0)