Index: ios/web/shell/test/utils/web_view_egutil.mm |
diff --git a/ios/web/shell/test/utils/web_view_egutil.mm b/ios/web/shell/test/utils/web_view_egutil.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bd0ae3ad57ac48b9f9dc163ffc0e2608c6b50140 |
--- /dev/null |
+++ b/ios/web/shell/test/utils/web_view_egutil.mm |
@@ -0,0 +1,83 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#import "ios/web/shell/test/utils/web_view_egutil.h" |
+ |
+#import <WebKit/WebKit.h> |
+ |
+#import "base/test/ios/wait_util.h" |
+#import "ios/web/shell/view_controller.h" |
+#include "ios/web/shell/test/utils/eg_util.h" |
+ |
+namespace { |
+ |
+// Constant for UI wait loop. |
+const NSTimeInterval kSpinDelaySeconds = 0.01; |
+ |
+// Constant for timeout while waiting for web element. |
+const NSTimeInterval kMaxTimeout = 4.0; |
+ |
+// Script that returns document.body as a string. |
+NSString* const kGetDocumentBodyJavaScript = |
+ @"document.body ? document.body.textContent : null"; |
+} |
+ |
+@implementation GREYMatchers (WebShellAdditions) |
+ |
++ (id<GREYMatcher>)matcherForWebViewContainingText:(NSString*)text { |
+ MatchesBlock matches = ^BOOL(UIView* view) { |
+ if (![view isKindOfClass:[WKWebView class]]) { |
+ return NO; |
+ } |
+ |
+ CRWWebController* webController = earl_grey_util::GetCurrentWebController(); |
+ DCHECK(webController); |
+ |
+ __block id script = nil; |
+ __block BOOL didSuceed = NO; |
+ __block BOOL timeout = NO; |
+ NSDate* deadline = [NSDate dateWithTimeIntervalSinceNow:kMaxTimeout]; |
+ script = ^(NSString* evaluationResult, NSError* error) { |
+ if (![evaluationResult containsString:text]) { |
+ if ([[NSDate date] compare:deadline] == NSOrderedDescending) { |
+ timeout = YES; |
+ return; |
+ } |
+ [webController evaluateJavaScript:kGetDocumentBodyJavaScript |
+ stringResultHandler:script]; |
+ } else { |
+ didSuceed = YES; |
+ } |
+ }; |
+ |
+ [webController evaluateJavaScript:kGetDocumentBodyJavaScript |
+ stringResultHandler:script]; |
+ while (!didSuceed && !timeout) { |
+ base::test::ios::SpinRunLoopWithMaxDelay( |
+ base::TimeDelta::FromSecondsD(kSpinDelaySeconds)); |
+ } |
+ return didSuceed; |
+ |
+ }; |
+ |
+ DescribeToBlock describe = ^void(id<GREYDescription> description) { |
+ [description |
+ appendText:[NSString stringWithFormat:@"web view containing %@", text]]; |
+ }; |
+ |
+ base::scoped_nsobject<GREYElementMatcherBlock> matcherBlock( |
+ [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
+ descriptionBlock:describe] retain]); |
+ return matcherBlock; |
+} |
+ |
+@end |
+ |
+#if !(GREY_DISABLE_SHORTHAND) |
+ |
+id<GREYMatcher> shell_webViewContainingText(NSString* text) { |
+ return [GREYMatchers matcherForWebViewContainingText:text]; |
+} |
+ |
+#endif |