| 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/shell/test/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/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 | 14 |
| 18 namespace { | 15 namespace { |
| 19 | 16 |
| 20 // Constant for UI wait loop in seconds. | 17 // Constant for UI wait loop in seconds. |
| 21 const NSTimeInterval kSpinDelaySeconds = 0.01; | 18 const NSTimeInterval kSpinDelaySeconds = 0.01; |
| 22 | 19 |
| 23 // Constant for timeout in seconds while waiting for web element. | 20 // Constant for timeout in seconds while waiting for web element. |
| 24 const NSTimeInterval kWaitForWebElementTimeout = 4.0; | 21 const NSTimeInterval kWaitForWebElementTimeout = 4.0; |
| 25 | 22 |
| 26 // Script that returns document.body as a string. | 23 // Script that returns document.body as a string. |
| 27 char kGetDocumentBodyJavaScript[] = | 24 char kGetDocumentBodyJavaScript[] = |
| 28 "document.body ? document.body.textContent : null"; | 25 "document.body ? document.body.textContent : null"; |
| 29 } | 26 } |
| 30 | 27 |
| 31 namespace web { | 28 namespace web { |
| 32 | 29 |
| 33 id<GREYMatcher> webViewContainingText(NSString* text) { | 30 id<GREYMatcher> webViewContainingText(NSString* text, web::WebState* webState) { |
| 34 return [GREYMatchers matcherForWebViewContainingText:text]; | 31 return |
| 32 [GREYMatchers matcherForWebViewContainingText:text inWebState:webState]; |
| 35 } | 33 } |
| 36 | 34 |
| 37 } // namespace web | 35 } // namespace web |
| 38 | 36 |
| 39 @implementation GREYMatchers (WebViewAdditions) | 37 @implementation GREYMatchers (WebViewAdditions) |
| 40 | 38 |
| 41 + (id<GREYMatcher>)matcherForWebViewContainingText:(NSString*)text { | 39 + (id<GREYMatcher>)matcherForWebViewContainingText:(NSString*)text |
| 40 inWebState:(web::WebState*)webState { |
| 42 MatchesBlock matches = ^BOOL(UIView* view) { | 41 MatchesBlock matches = ^BOOL(UIView* view) { |
| 43 if (![view isKindOfClass:[WKWebView class]]) { | 42 if (![view isKindOfClass:[WKWebView class]]) { |
| 44 return NO; | 43 return NO; |
| 45 } | 44 } |
| 46 | 45 if (![view isDescendantOfView:webState->GetView()]) { |
| 47 ViewController* viewController = | 46 return NO; |
| 48 web::web_shell_test_util::GetCurrentViewController(); | 47 } |
| 49 web::WebState* webState = [viewController webState]; | |
| 50 | 48 |
| 51 __block BOOL didSucceed = NO; | 49 __block BOOL didSucceed = NO; |
| 52 NSDate* deadline = | 50 NSDate* deadline = |
| 53 [NSDate dateWithTimeIntervalSinceNow:kWaitForWebElementTimeout]; | 51 [NSDate dateWithTimeIntervalSinceNow:kWaitForWebElementTimeout]; |
| 54 while (([[NSDate date] compare:deadline] != NSOrderedDescending) && | 52 while (([[NSDate date] compare:deadline] != NSOrderedDescending) && |
| 55 !didSucceed) { | 53 !didSucceed) { |
| 56 webState->ExecuteJavaScript( | 54 webState->ExecuteJavaScript( |
| 57 base::UTF8ToUTF16(kGetDocumentBodyJavaScript), | 55 base::UTF8ToUTF16(kGetDocumentBodyJavaScript), |
| 58 base::BindBlock(^(const base::Value* value) { | 56 base::BindBlock(^(const base::Value* value) { |
| 59 std::string response; | 57 std::string response; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 72 DescribeToBlock describe = ^(id<GREYDescription> description) { | 70 DescribeToBlock describe = ^(id<GREYDescription> description) { |
| 73 [description | 71 [description |
| 74 appendText:[NSString stringWithFormat:@"web view containing %@", text]]; | 72 appendText:[NSString stringWithFormat:@"web view containing %@", text]]; |
| 75 }; | 73 }; |
| 76 | 74 |
| 77 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | 75 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| 78 descriptionBlock:describe] | 76 descriptionBlock:describe] |
| 79 autorelease]; | 77 autorelease]; |
| 80 } | 78 } |
| 81 | 79 |
| 82 @end | 80 @end |
| OLD | NEW |