| 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" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 id<GREYMatcher> webViewScrollView(web::WebState* webState) { | 31 id<GREYMatcher> webViewScrollView(web::WebState* webState) { |
| 32 return [GREYMatchers matcherForWebViewScrollViewInWebState:webState]; | 32 return [GREYMatchers matcherForWebViewScrollViewInWebState:webState]; |
| 33 } | 33 } |
| 34 | 34 |
| 35 } // namespace web | 35 } // namespace web |
| 36 | 36 |
| 37 @implementation GREYMatchers (WebViewAdditions) | 37 @implementation GREYMatchers (WebViewAdditions) |
| 38 | 38 |
| 39 + (id<GREYMatcher>)matcherForWebViewContainingText:(const std::string&)text | 39 + (id<GREYMatcher>)matcherForWebViewContainingText:(const std::string&)text |
| 40 inWebState:(web::WebState*)webState { | 40 inWebState:(web::WebState*)webState { |
| 41 std::string textCopyForBlock = text; |
| 41 MatchesBlock matches = ^BOOL(UIView* view) { | 42 MatchesBlock matches = ^BOOL(UIView* view) { |
| 42 if (![view isKindOfClass:[WKWebView class]]) { | 43 if (![view isKindOfClass:[WKWebView class]]) { |
| 43 return NO; | 44 return NO; |
| 44 } | 45 } |
| 45 if (![view isDescendantOfView:webState->GetView()]) { | 46 if (![view isDescendantOfView:webState->GetView()]) { |
| 46 return NO; | 47 return NO; |
| 47 } | 48 } |
| 48 | 49 |
| 49 __block BOOL didSucceed = NO; | 50 __block BOOL didSucceed = NO; |
| 50 NSDate* deadline = | 51 NSDate* deadline = |
| 51 [NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout]; | 52 [NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout]; |
| 52 while (([[NSDate date] compare:deadline] != NSOrderedDescending) && | 53 while (([[NSDate date] compare:deadline] != NSOrderedDescending) && |
| 53 !didSucceed) { | 54 !didSucceed) { |
| 54 webState->ExecuteJavaScript( | 55 webState->ExecuteJavaScript( |
| 55 base::UTF8ToUTF16(kGetDocumentBodyJavaScript), | 56 base::UTF8ToUTF16(kGetDocumentBodyJavaScript), |
| 56 base::BindBlock(^(const base::Value* value) { | 57 base::BindBlock(^(const base::Value* value) { |
| 57 std::string response; | 58 std::string response; |
| 58 if (value && value->IsType(base::Value::TYPE_STRING) && | 59 if (value && value->IsType(base::Value::TYPE_STRING) && |
| 59 value->GetAsString(&response)) { | 60 value->GetAsString(&response)) { |
| 60 didSucceed = response.find(text) != std::string::npos; | 61 didSucceed = response.find(textCopyForBlock) != std::string::npos; |
| 61 } | 62 } |
| 62 })); | 63 })); |
| 63 base::test::ios::SpinRunLoopWithMaxDelay( | 64 base::test::ios::SpinRunLoopWithMaxDelay( |
| 64 base::TimeDelta::FromSecondsD(testing::kSpinDelaySeconds)); | 65 base::TimeDelta::FromSecondsD(testing::kSpinDelaySeconds)); |
| 65 } | 66 } |
| 66 return didSucceed; | 67 return didSucceed; |
| 67 }; | 68 }; |
| 68 | 69 |
| 69 DescribeToBlock describe = ^(id<GREYDescription> description) { | 70 DescribeToBlock describe = ^(id<GREYDescription> description) { |
| 70 [description appendText:@"web view containing "]; | 71 [description appendText:@"web view containing "]; |
| 71 [description appendText:base::SysUTF8ToNSString(text)]; | 72 [description appendText:base::SysUTF8ToNSString(textCopyForBlock)]; |
| 72 }; | 73 }; |
| 73 | 74 |
| 74 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | 75 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| 75 descriptionBlock:describe] | 76 descriptionBlock:describe] |
| 76 autorelease]; | 77 autorelease]; |
| 77 } | 78 } |
| 78 | 79 |
| 79 + (id<GREYMatcher>)matcherForWebViewScrollViewInWebState: | 80 + (id<GREYMatcher>)matcherForWebViewScrollViewInWebState: |
| 80 (web::WebState*)webState { | 81 (web::WebState*)webState { |
| 81 MatchesBlock matches = ^BOOL(UIView* view) { | 82 MatchesBlock matches = ^BOOL(UIView* view) { |
| 82 return [view isKindOfClass:[UIScrollView class]] && | 83 return [view isKindOfClass:[UIScrollView class]] && |
| 83 [view.superview isKindOfClass:[WKWebView class]] && | 84 [view.superview isKindOfClass:[WKWebView class]] && |
| 84 [view isDescendantOfView:webState->GetView()]; | 85 [view isDescendantOfView:webState->GetView()]; |
| 85 }; | 86 }; |
| 86 | 87 |
| 87 DescribeToBlock describe = ^(id<GREYDescription> description) { | 88 DescribeToBlock describe = ^(id<GREYDescription> description) { |
| 88 [description appendText:@"web view scroll view"]; | 89 [description appendText:@"web view scroll view"]; |
| 89 }; | 90 }; |
| 90 | 91 |
| 91 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | 92 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| 92 descriptionBlock:describe] | 93 descriptionBlock:describe] |
| 93 autorelease]; | 94 autorelease]; |
| 94 } | 95 } |
| 95 | 96 |
| 96 @end | 97 @end |
| OLD | NEW |