| 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 #include "ios/testing/earl_grey/wait_util.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // Script that returns document.body as a string. | 18 // Script that returns document.body as a string. |
| 19 char kGetDocumentBodyJavaScript[] = | 19 char kGetDocumentBodyJavaScript[] = |
| 20 "document.body ? document.body.textContent : null"; | 20 "document.body ? document.body.textContent : null"; |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace web { | 23 namespace web { |
| 24 | 24 |
| 25 id<GREYMatcher> webViewContainingText(NSString* text, web::WebState* webState) { | 25 id<GREYMatcher> webViewContainingText(NSString* text, web::WebState* webState) { |
| 26 return [GREYMatchers |
| 27 matcherForWebViewContainingText:base::SysNSStringToUTF8(text) |
| 28 inWebState:webState]; |
| 29 } |
| 30 |
| 31 id<GREYMatcher> webViewContainingText(const std::string& text, |
| 32 web::WebState* webState) { |
| 26 return | 33 return |
| 27 [GREYMatchers matcherForWebViewContainingText:text inWebState:webState]; | 34 [GREYMatchers matcherForWebViewContainingText:text inWebState:webState]; |
| 28 } | 35 } |
| 29 | 36 |
| 30 } // namespace web | 37 } // namespace web |
| 31 | 38 |
| 32 @implementation GREYMatchers (WebViewAdditions) | 39 @implementation GREYMatchers (WebViewAdditions) |
| 33 | 40 |
| 34 + (id<GREYMatcher>)matcherForWebViewContainingText:(NSString*)text | 41 + (id<GREYMatcher>)matcherForWebViewContainingText:(const std::string&)text |
| 35 inWebState:(web::WebState*)webState { | 42 inWebState:(web::WebState*)webState { |
| 36 MatchesBlock matches = ^BOOL(UIView* view) { | 43 MatchesBlock matches = ^BOOL(UIView* view) { |
| 37 if (![view isKindOfClass:[WKWebView class]]) { | 44 if (![view isKindOfClass:[WKWebView class]]) { |
| 38 return NO; | 45 return NO; |
| 39 } | 46 } |
| 40 if (![view isDescendantOfView:webState->GetView()]) { | 47 if (![view isDescendantOfView:webState->GetView()]) { |
| 41 return NO; | 48 return NO; |
| 42 } | 49 } |
| 43 | 50 |
| 44 __block BOOL didSucceed = NO; | 51 __block BOOL didSucceed = NO; |
| 45 NSDate* deadline = | 52 NSDate* deadline = |
| 46 [NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout]; | 53 [NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout]; |
| 47 while (([[NSDate date] compare:deadline] != NSOrderedDescending) && | 54 while (([[NSDate date] compare:deadline] != NSOrderedDescending) && |
| 48 !didSucceed) { | 55 !didSucceed) { |
| 49 webState->ExecuteJavaScript( | 56 webState->ExecuteJavaScript( |
| 50 base::UTF8ToUTF16(kGetDocumentBodyJavaScript), | 57 base::UTF8ToUTF16(kGetDocumentBodyJavaScript), |
| 51 base::BindBlock(^(const base::Value* value) { | 58 base::BindBlock(^(const base::Value* value) { |
| 52 std::string response; | 59 std::string response; |
| 53 if (value && value->IsType(base::Value::TYPE_STRING) && | 60 if (value && value->IsType(base::Value::TYPE_STRING) && |
| 54 value->GetAsString(&response)) { | 61 value->GetAsString(&response)) { |
| 55 didSucceed = response.find(base::SysNSStringToUTF8(text)) != | 62 didSucceed = response.find(text) != std::string::npos; |
| 56 std::string::npos; | |
| 57 } | 63 } |
| 58 })); | 64 })); |
| 59 base::test::ios::SpinRunLoopWithMaxDelay( | 65 base::test::ios::SpinRunLoopWithMaxDelay( |
| 60 base::TimeDelta::FromSecondsD(testing::kSpinDelaySeconds)); | 66 base::TimeDelta::FromSecondsD(testing::kSpinDelaySeconds)); |
| 61 } | 67 } |
| 62 return didSucceed; | 68 return didSucceed; |
| 63 }; | 69 }; |
| 64 | 70 |
| 65 DescribeToBlock describe = ^(id<GREYDescription> description) { | 71 DescribeToBlock describe = ^(id<GREYDescription> description) { |
| 66 [description | 72 [description appendText:@"web view containing "]; |
| 67 appendText:[NSString stringWithFormat:@"web view containing %@", text]]; | 73 [description appendText:base::SysUTF8ToNSString(text)]; |
| 68 }; | 74 }; |
| 69 | 75 |
| 70 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | 76 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| 71 descriptionBlock:describe] | 77 descriptionBlock:describe] |
| 72 autorelease]; | 78 autorelease]; |
| 73 } | 79 } |
| 74 | 80 |
| 75 @end | 81 @end |
| OLD | NEW |