Skip to content

Commit

Permalink
UIWebView deprecation.
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-rao authored and khandpur committed Oct 30, 2019
1 parent 7c7ecca commit 3d51df2
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 18 deletions.
22 changes: 19 additions & 3 deletions EarlGrey/Action/GREYActions.m
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,12 @@ + (void)initialize {
performBlock:^BOOL (id element, __strong NSError **errorOrNil) {
NSString *textStr;
if ([element grey_isWebAccessibilityElement]) {
#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
[GREYActions grey_setText:@"" onWebElement:element];
return YES;
#else
return NO;
#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
} else if ([element isKindOfClass:gAccessibilityTextFieldElementClass]) {
element = [element textField];
} else if ([element respondsToSelector:@selector(text)]) {
Expand Down Expand Up @@ -298,8 +302,13 @@ + (void)initialize {
// TODO: JS Errors should be propagated up.
id<GREYMatcher> constraints =
grey_allOf(grey_not(grey_systemAlertViewShown()),
grey_anyOf(grey_kindOfClass([UIWebView class]),
grey_kindOfClass([WKWebView class]), nil),
#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
grey_anyOf(
grey_kindOfClass([UIWebView class]),
grey_kindOfClass([WKWebView class]), nil),
#else
grey_kindOfClass([WKWebView class]),
#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
nil);
BOOL (^performBlock)(id webView, __strong NSError **errorOrNil) = ^(
id webView, __strong NSError **errorOrNil) {
Expand All @@ -320,7 +329,9 @@ + (void)initialize {
*outResult = resultString;
}
return completionDone;
} else if ([webView isKindOfClass:[UIWebView class]]) {
}
#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
else if ([webView isKindOfClass:[UIWebView class]]) {
UIWebView *uiWebView = webView;
if (outResult) {
*outResult = [uiWebView stringByEvaluatingJavaScriptFromString:js];
Expand All @@ -331,6 +342,7 @@ + (void)initialize {
[[GREYUIThreadExecutor sharedInstance] drainForTime:0.5]; // Wait for actions to register.
return YES;
}
#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
return NO;
};
return [[GREYActionBlock alloc] initWithName:@"Execute JavaScript"
Expand Down Expand Up @@ -360,6 +372,7 @@ + (void)initialize {

#pragma mark - Private

#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
/**
* Sets WebView input text value.
*
Expand Down Expand Up @@ -388,6 +401,7 @@ + (void)grey_setText:(NSString *)text onWebElement:(id)element {
UIWebView *parentWebView = (UIWebView *)[element grey_viewContainingSelf];
[parentWebView stringByEvaluatingJavaScriptFromString:jsForTitle];
}
#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0

/**
* Set the UITextField text value directly, bypassing the iOS keyboard.
Expand All @@ -410,7 +424,9 @@ + (void)grey_setText:(NSString *)text onWebElement:(id)element {
constraints:constraints
performBlock:^BOOL (id element, __strong NSError **errorOrNil) {
if ([element grey_isWebAccessibilityElement]) {
#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
[GREYActions grey_setText:text onWebElement:element];
#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
} else {
if ([element isKindOfClass:gAccessibilityTextFieldElementClass]) {
element = [element textField];
Expand Down
18 changes: 13 additions & 5 deletions EarlGrey/Action/GREYScrollAction.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,16 @@ - (instancetype)initWithDirection:(GREYDirection)direction
NSString *name =
[NSString stringWithFormat:@"Scroll %@ for %g", NSStringFromGREYDirection(direction), amount];
self = [super initWithName:name
constraints:grey_allOf(grey_anyOf(grey_kindOfClass([UIScrollView class]),
grey_kindOfClass([UIWebView class]),
nil),
grey_not(grey_systemAlertViewShown()),
nil)];
constraints:grey_allOf(
#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
grey_anyOf(grey_kindOfClass([UIScrollView class]),
grey_kindOfClass([UIWebView class]),
nil),
#else
grey_kindOfClass([UIScrollView class]),
#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
grey_not(grey_systemAlertViewShown()),
nil)];
if (self) {
_direction = direction;
_amount = amount;
Expand All @@ -98,10 +103,13 @@ - (BOOL)perform:(id)element error:(__strong NSError **)errorOrNil {
if (![self satisfiesConstraintsForElement:element error:errorOrNil]) {
return NO;
}

#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
// To scroll UIWebView we must use the UIScrollView in its hierarchy and scroll it.
if ([element isKindOfClass:[UIWebView class]]) {
element = [(UIWebView *)element scrollView];
}
#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0

CGFloat amountRemaining = _amount;
BOOL success = YES;
Expand Down
17 changes: 12 additions & 5 deletions EarlGrey/Action/GREYScrollToContentEdgeAction.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ - (instancetype)initWithEdge:(GREYContentEdge)edge startPointPercents:(CGPoint)s
NSString *name =
[NSString stringWithFormat:@"Scroll To %@ content edge", NSStringFromGREYContentEdge(edge)];
self = [super initWithName:name
constraints:grey_allOf(grey_anyOf(grey_kindOfClass([UIScrollView class]),
grey_kindOfClass([UIWebView class]),
nil),
grey_not(grey_systemAlertViewShown()),
nil)];
constraints:grey_allOf(
#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
grey_anyOf(grey_kindOfClass([UIScrollView class]),
grey_kindOfClass([UIWebView class]),
nil),
#else
grey_kindOfClass([UIScrollView class]),
#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
grey_not(grey_systemAlertViewShown()),
nil)];
if (self) {
_edge = edge;
_startPointPercents = startPointPercents;
Expand All @@ -78,10 +83,12 @@ - (BOOL)perform:(UIScrollView *)element error:(__strong NSError **)errorOrNil {
if (![self satisfiesConstraintsForElement:element error:errorOrNil]) {
return NO;
}
#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
// To scroll UIWebView we must use the UIScrollView in its hierarchy and scroll it.
if ([element isKindOfClass:[UIWebView class]]) {
element = [(UIWebView *)element scrollView];
}
#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0

// Get the maximum scrollable amount in any direction and keep applying it until the edge
// is reached.
Expand Down
3 changes: 3 additions & 0 deletions EarlGrey/Additions/NSObject+GREYAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ - (NSString *)grey_recursiveDescription {

- (UIView *)grey_viewContainingSelf {
if ([self grey_isWebAccessibilityElement]) {
#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
return [[self grey_containersAssignableFromClass:[UIWebView class]] firstObject];
#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
return nil;
} else if ([self isKindOfClass:[UIView class]]) {
return [self grey_container];
} else if ([self respondsToSelector:@selector(accessibilityContainer)]) {
Expand Down
4 changes: 4 additions & 0 deletions EarlGrey/Additions/UIWebView+GREYAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import <UIKit/UIKit.h>

#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0

NS_ASSUME_NONNULL_BEGIN

/**
Expand Down Expand Up @@ -61,3 +63,5 @@ NS_ASSUME_NONNULL_BEGIN
@end

NS_ASSUME_NONNULL_END

#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
4 changes: 4 additions & 0 deletions EarlGrey/Additions/UIWebView+GREYAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import "Additions/UIWebView+GREYAdditions.h"

#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0

#import <UIKit/UIKit.h>
#include <objc/runtime.h>

Expand Down Expand Up @@ -170,3 +172,5 @@ - (void)greyswizzled_setDelegate:(id<UIWebViewDelegate>)delegate {
}

@end

#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
4 changes: 4 additions & 0 deletions EarlGrey/Delegate/GREYUIWebViewDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import <UIKit/UIKit.h>

#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0

#import "Delegate/GREYSurrogateDelegate.h"

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -41,3 +43,5 @@ NS_ASSUME_NONNULL_BEGIN
@end

NS_ASSUME_NONNULL_END

#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
4 changes: 4 additions & 0 deletions EarlGrey/Delegate/GREYUIWebViewDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import "Delegate/GREYUIWebViewDelegate.h"

#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0

#import "Additions/UIWebView+GREYAdditions.h"

#import "Synchronization/GREYAppStateTracker.h"
Expand Down Expand Up @@ -148,3 +150,5 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
}

@end

#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
14 changes: 9 additions & 5 deletions EarlGrey/Event/GREYTouchInjector.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@
const NSTimeInterval kGREYTouchInjectionFrequency = 60.0;

/**
* Maximum time to wait for UIWebView delegates to get called after the
* last touch (i.e. @c isLastTouch is @c YES).
* The time interval in seconds between each touch injection.
*/
static const NSTimeInterval kGREYMaxIntervalForUIWebViewResponse = 2.0;
static const NSTimeInterval kGREYTouchInjectionInterval = 1.0 / kGREYTouchInjectionFrequency;

#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
/**
* The time interval in seconds between each touch injection.
* Maximum time to wait for UIWebView delegates to get called after the
* last touch (i.e. @c isLastTouch is @c YES).
*/
static const NSTimeInterval kGREYTouchInjectionInterval = 1.0 / kGREYTouchInjectionFrequency;
static const NSTimeInterval kGREYMaxIntervalForUIWebViewResponse = 2.0;
#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0

@interface GREYTouchInjector() <GREYZeroToleranceTimerTarget>
@end
Expand Down Expand Up @@ -282,6 +284,7 @@ - (void)grey_injectTouches:(GREYTouchInfo *)touchInfo {
touchViewContainsWKWebView = YES;
}
if (touchInfo.phase == GREYTouchInfoPhaseTouchEnded) {
#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
UIWebView *touchWebView = nil;
if ([currentTouchView isKindOfClass:[UIWebView class]]) {
touchWebView = (UIWebView *)currentTouchView;
Expand All @@ -293,6 +296,7 @@ - (void)grey_injectTouches:(GREYTouchInfo *)touchInfo {
}
}
[touchWebView grey_pendingInteractionForTime:kGREYMaxIntervalForUIWebViewResponse];
#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
}
}
} @catch (NSException *e) {
Expand Down
4 changes: 4 additions & 0 deletions EarlGrey/Synchronization/GREYUIWebViewIdlingResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import <UIKit/UIKit.h>

#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0

#import <EarlGrey/GREYIdlingResource.h>

NS_ASSUME_NONNULL_BEGIN
Expand Down Expand Up @@ -43,3 +45,5 @@ NS_ASSUME_NONNULL_BEGIN
@end

NS_ASSUME_NONNULL_END

#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0
4 changes: 4 additions & 0 deletions EarlGrey/Synchronization/GREYUIWebViewIdlingResource.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import "Synchronization/GREYUIWebViewIdlingResource.h"

#if !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0

#import "Additions/UIWebView+GREYAdditions.h"
#import "Assertion/GREYAssertionDefines.h"
#import "Common/GREYDefines.h"
Expand Down Expand Up @@ -209,3 +211,5 @@ - (NSString *)grey_evaluateAndAssertNoErrorsJavaScriptInString:(NSString *)jsStr
}

@end

#endif // !defined(__IPHONE_12_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_12_0

0 comments on commit 3d51df2

Please sign in to comment.