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