OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/web/shell/test/utils/web_view_egutil.h" |
| 6 |
| 7 #import <WebKit/WebKit.h> |
| 8 |
| 9 #import "base/test/ios/wait_util.h" |
| 10 #import "ios/web/shell/view_controller.h" |
| 11 #include "ios/web/shell/test/utils/eg_util.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 // Constant for UI wait loop. |
| 16 const NSTimeInterval kSpinDelaySeconds = 0.01; |
| 17 |
| 18 // Constant for timeout while waiting for web element. |
| 19 const NSTimeInterval kMaxTimeout = 4.0; |
| 20 |
| 21 // Script that returns document.body as a string. |
| 22 NSString* const kGetDocumentBodyJavaScript = |
| 23 @"document.body ? document.body.textContent : null"; |
| 24 } |
| 25 |
| 26 @implementation GREYMatchers (WebShellAdditions) |
| 27 |
| 28 + (id<GREYMatcher>)matcherForWebViewContainingText:(NSString*)text { |
| 29 MatchesBlock matches = ^BOOL(UIView* view) { |
| 30 if (![view isKindOfClass:[WKWebView class]]) { |
| 31 return NO; |
| 32 } |
| 33 |
| 34 CRWWebController* webController = earl_grey_util::GetCurrentWebController(); |
| 35 DCHECK(webController); |
| 36 |
| 37 __block id script = nil; |
| 38 __block BOOL didSuceed = NO; |
| 39 __block BOOL timeout = NO; |
| 40 NSDate* deadline = [NSDate dateWithTimeIntervalSinceNow:kMaxTimeout]; |
| 41 script = ^(NSString* evaluationResult, NSError* error) { |
| 42 if (![evaluationResult containsString:text]) { |
| 43 if ([[NSDate date] compare:deadline] == NSOrderedDescending) { |
| 44 timeout = YES; |
| 45 return; |
| 46 } |
| 47 [webController evaluateJavaScript:kGetDocumentBodyJavaScript |
| 48 stringResultHandler:script]; |
| 49 } else { |
| 50 didSuceed = YES; |
| 51 } |
| 52 }; |
| 53 |
| 54 [webController evaluateJavaScript:kGetDocumentBodyJavaScript |
| 55 stringResultHandler:script]; |
| 56 while (!didSuceed && !timeout) { |
| 57 base::test::ios::SpinRunLoopWithMaxDelay( |
| 58 base::TimeDelta::FromSecondsD(kSpinDelaySeconds)); |
| 59 } |
| 60 return didSuceed; |
| 61 |
| 62 }; |
| 63 |
| 64 DescribeToBlock describe = ^void(id<GREYDescription> description) { |
| 65 [description |
| 66 appendText:[NSString stringWithFormat:@"web view containing %@", text]]; |
| 67 }; |
| 68 |
| 69 base::scoped_nsobject<GREYElementMatcherBlock> matcherBlock( |
| 70 [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| 71 descriptionBlock:describe] retain]); |
| 72 return matcherBlock; |
| 73 } |
| 74 |
| 75 @end |
| 76 |
| 77 #if !(GREY_DISABLE_SHORTHAND) |
| 78 |
| 79 id<GREYMatcher> shell_webViewContainingText(NSString* text) { |
| 80 return [GREYMatchers matcherForWebViewContainingText:text]; |
| 81 } |
| 82 |
| 83 #endif |
OLD | NEW |