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 #include "base/mac/bind_objc_block.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #import "base/test/ios/wait_util.h" |
| 12 #include "base/values.h" |
| 13 #import "ios/web/shell/view_controller.h" |
| 14 #include "ios/web/shell/test/utils/navigation_egutil.h" |
| 15 #include "ios/web/web_state/web_state_impl.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Constant for UI wait loop in seconds. |
| 20 const NSTimeInterval kSpinDelaySeconds = 0.01; |
| 21 |
| 22 // Constant for timeout in seconds while waiting for web element. |
| 23 const NSTimeInterval kWaitForWebElementTimeout = 4.0; |
| 24 |
| 25 // Script that returns document.body as a string. |
| 26 std::string kGetDocumentBodyJavaScript = |
| 27 "document.body ? document.body.textContent : null"; |
| 28 } |
| 29 |
| 30 @implementation GREYMatchers (WebShellAdditions) |
| 31 |
| 32 + (id<GREYMatcher>)matcherForWebViewContainingText:(NSString*)text { |
| 33 MatchesBlock matches = ^BOOL(UIView* view) { |
| 34 if (![view isKindOfClass:[WKWebView class]]) { |
| 35 return NO; |
| 36 } |
| 37 |
| 38 ViewController* viewController = |
| 39 (ViewController*)[[[[UIApplication sharedApplication] delegate] window] |
| 40 rootViewController]; |
| 41 DCHECK(viewController); |
| 42 web::WebState* webState = [viewController webState]; |
| 43 DCHECK(webState); |
| 44 |
| 45 __block BOOL didSucceed = NO; |
| 46 void (^searchForTextBlock)(const base::Value* value) = |
| 47 ^(const base::Value* value) { |
| 48 std::string response; |
| 49 if (value && value->IsType(base::Value::TYPE_STRING)) { |
| 50 value->GetAsString(&response); |
| 51 if (response.find([text UTF8String]) == std::string::npos) { |
| 52 } else { |
| 53 didSucceed = YES; |
| 54 } |
| 55 } |
| 56 }; |
| 57 NSDate* deadline = |
| 58 [NSDate dateWithTimeIntervalSinceNow:kWaitForWebElementTimeout]; |
| 59 while (!([[NSDate date] compare:deadline] == NSOrderedDescending) && |
| 60 !didSucceed) { |
| 61 webState->ExecuteJavaScript(base::UTF8ToUTF16(kGetDocumentBodyJavaScript), |
| 62 base::BindBlock(searchForTextBlock)); |
| 63 base::test::ios::SpinRunLoopWithMaxDelay( |
| 64 base::TimeDelta::FromSecondsD(kSpinDelaySeconds)); |
| 65 } |
| 66 return didSucceed; |
| 67 }; |
| 68 |
| 69 DescribeToBlock describe = ^void(id<GREYDescription> description) { |
| 70 [description |
| 71 appendText:[NSString stringWithFormat:@"web view containing %@", text]]; |
| 72 }; |
| 73 |
| 74 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| 75 descriptionBlock:describe] |
| 76 autorelease]; |
| 77 } |
| 78 |
| 79 @end |
| 80 |
| 81 #if !(GREY_DISABLE_SHORTHAND) |
| 82 |
| 83 id<GREYMatcher> shell_webViewContainingText(NSString* text) { |
| 84 return [GREYMatchers matcherForWebViewContainingText:text]; |
| 85 } |
| 86 |
| 87 #endif |
OLD | NEW |