| 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 namespace web { | |
| 32 | |
| 33 id<GREYMatcher> webViewContainingText(NSString* text) { | |
| 34 return [GREYMatchers matcherForWebViewContainingText:text]; | |
| 35 } | |
| 36 | |
| 37 } // namespace web | |
| 38 | |
| 39 @implementation GREYMatchers (WebViewAdditions) | |
| 40 | |
| 41 + (id<GREYMatcher>)matcherForWebViewContainingText:(NSString*)text { | |
| 42 MatchesBlock matches = ^BOOL(UIView* view) { | |
| 43 if (![view isKindOfClass:[WKWebView class]]) { | |
| 44 return NO; | |
| 45 } | |
| 46 | |
| 47 ViewController* viewController = | |
| 48 web::web_shell_test_util::GetCurrentViewController(); | |
| 49 web::WebState* webState = [viewController webState]; | |
| 50 | |
| 51 __block BOOL didSucceed = NO; | |
| 52 NSDate* deadline = | |
| 53 [NSDate dateWithTimeIntervalSinceNow:kWaitForWebElementTimeout]; | |
| 54 while (([[NSDate date] compare:deadline] != NSOrderedDescending) && | |
| 55 !didSucceed) { | |
| 56 webState->ExecuteJavaScript( | |
| 57 base::UTF8ToUTF16(kGetDocumentBodyJavaScript), | |
| 58 base::BindBlock(^(const base::Value* value) { | |
| 59 std::string response; | |
| 60 if (value && value->IsType(base::Value::TYPE_STRING) && | |
| 61 value->GetAsString(&response)) { | |
| 62 didSucceed = response.find(base::SysNSStringToUTF8(text)) != | |
| 63 std::string::npos; | |
| 64 } | |
| 65 })); | |
| 66 base::test::ios::SpinRunLoopWithMaxDelay( | |
| 67 base::TimeDelta::FromSecondsD(kSpinDelaySeconds)); | |
| 68 } | |
| 69 return didSucceed; | |
| 70 }; | |
| 71 | |
| 72 DescribeToBlock describe = ^(id<GREYDescription> description) { | |
| 73 [description | |
| 74 appendText:[NSString stringWithFormat:@"web view containing %@", text]]; | |
| 75 }; | |
| 76 | |
| 77 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | |
| 78 descriptionBlock:describe] | |
| 79 autorelease]; | |
| 80 } | |
| 81 | |
| 82 @end | |
| OLD | NEW |