Skip to content

Commit

Permalink
Refactor: Simplify error handling in matchedElementsWithTimeout:error:
Browse files Browse the repository at this point in the history
  • Loading branch information
axi0mX committed Sep 7, 2016
1 parent a3be2a8 commit 24753ab
Showing 1 changed file with 28 additions and 41 deletions.
69 changes: 28 additions & 41 deletions EarlGrey/Core/GREYElementInteraction.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,65 +113,52 @@ - (NSArray *)matchedElementsWithTimeout:(CFTimeInterval)timeout error:(__strong

GREYElementProvider *entireRootHierarchyProvider =
[GREYElementProvider providerWithRootProvider:[strongDataSource rootElementProvider]];
id<GREYMatcher> elementMatcher = _elementMatcher;
if (_rootMatcher) {
elementMatcher = grey_allOf(elementMatcher, grey_ancestor(_rootMatcher), nil);
}
GREYElementFinder *elementFinder = [[GREYElementFinder alloc] initWithMatcher:elementMatcher];
id<GREYMatcher> matcher = _rootMatcher
? grey_allOf(_elementMatcher, grey_ancestor(_rootMatcher), nil) : _elementMatcher;
GREYElementFinder *elementFinder = [[GREYElementFinder alloc] initWithMatcher:matcher];
NSError *searchActionError = nil;
CFTimeInterval timeoutTime = CACurrentMediaTime() + timeout;
BOOL timedOut = NO;

while (YES) {
@autoreleasepool {
// Find the element in the current UI hierarchy.
NSArray *elements = [elementFinder elementsMatchedInProvider:entireRootHierarchyProvider];
if (elements.count > 0) {
return elements;
} else if (!_searchAction) {
if (error) {
NSString *desc =
@"Interaction cannot continue because the desired element was not found.";
*error = [NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionElementNotFoundErrorCode
userInfo:@{ NSLocalizedDescriptionKey : desc }];
}
return nil;
NSString *description =
@"Interaction cannot continue because the desired element was not found.";
*error = [NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionElementNotFoundErrorCode
userInfo:@{ NSLocalizedDescriptionKey : description }];
break;
} else if (searchActionError) {
*error = [NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionElementNotFoundErrorCode
userInfo:@{ NSUnderlyingErrorKey : searchActionError }];
break;
}

CFTimeInterval currentTime = CACurrentMediaTime();
if (currentTime >= timeoutTime) {
timedOut = YES;
} else if (CACurrentMediaTime() >= timeoutTime) {
NSString *description = [NSString stringWithFormat:@"Interaction timed out after %g "
@"seconds while searching for element.",
timeout];
NSError *timeoutError =
[NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionTimeoutErrorCode
userInfo:@{ NSLocalizedDescriptionKey : description }];
*error = [NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionElementNotFoundErrorCode
userInfo:@{ NSUnderlyingErrorKey : timeoutError }];
break;
}
// Keep applying search action.
id<GREYInteraction> interaction =
[[GREYElementInteraction alloc] initWithElementMatcher:_searchActionElementMatcher];
// Don't fail if this interaction error's out. It might still have revealed the element
// we're looking for.
[interaction performAction:_searchAction error:&searchActionError];
// Keep applying search action. Don't fail if this interaction errors out. It might have
// revealed the element we are looking for.
[[[GREYElementInteraction alloc] initWithElementMatcher:_searchActionElementMatcher]
performAction:_searchAction error:&searchActionError];
// Drain here so that search at the beginning of the loop looks at stable UI.
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
}
}

NSDictionary *userInfo = nil;
if (searchActionError) {
userInfo = @{ NSUnderlyingErrorKey : searchActionError };
} else if (timedOut) {
CFTimeInterval interactionTimeout =
GREY_CONFIG_DOUBLE(kGREYConfigKeyInteractionTimeoutDuration);
NSString *desc = [NSString stringWithFormat:@"Interaction timed out after %g seconds while "
@"searching for element.", interactionTimeout];
NSError *timeoutError = [NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionTimeoutErrorCode
userInfo:@{ NSLocalizedDescriptionKey : desc }];
userInfo = @{ NSUnderlyingErrorKey : timeoutError };
}
*error = [NSError errorWithDomain:kGREYInteractionErrorDomain
code:kGREYInteractionElementNotFoundErrorCode
userInfo:userInfo];
return nil;
}

Expand Down

0 comments on commit 24753ab

Please sign in to comment.