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