| 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 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #import <WebKit/WebKit.h> | 9 #import <WebKit/WebKit.h> |
| 10 | 10 |
| 11 #include "base/mac/bind_objc_block.h" | 11 #include "base/mac/bind_objc_block.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/test/ios/wait_util.h" | 15 #include "base/test/ios/wait_util.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "ios/testing/earl_grey/wait_util.h" | 17 #include "ios/testing/earl_grey/wait_util.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // Script that returns document.body as a string. | 21 // Script that returns document.body as a string. |
| 22 char kGetDocumentBodyJavaScript[] = | 22 char kGetDocumentBodyJavaScript[] = |
| 23 "document.body ? document.body.textContent : null"; | 23 "document.body ? document.body.textContent : null"; |
| 24 // Script that tests presence of css selector. | 24 // Script that tests presence of css selector. |
| 25 char kTestCssSelectorJavaScriptTemplate[] = "!!document.querySelector(\"%s\");"; | 25 char kTestCssSelectorJavaScriptTemplate[] = "!!document.querySelector(\"%s\");"; |
| 26 | 26 |
| 27 // Matcher for WKWebView which belogs to the given |webState|. | |
| 28 id<GREYMatcher> webViewInWebState(web::WebState* web_state) { | |
| 29 MatchesBlock matches = ^BOOL(UIView* view) { | |
| 30 return [view isKindOfClass:[WKWebView class]] && | |
| 31 [view isDescendantOfView:web_state->GetView()]; | |
| 32 }; | |
| 33 | |
| 34 DescribeToBlock describe = ^(id<GREYDescription> description) { | |
| 35 [description appendText:@"web view in web state"]; | |
| 36 }; | |
| 37 | |
| 38 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | |
| 39 descriptionBlock:describe] | |
| 40 autorelease]; | |
| 41 } | |
| 42 | |
| 43 // Synchronously returns the result of executed JavaScript. | 27 // Synchronously returns the result of executed JavaScript. |
| 44 std::unique_ptr<base::Value> ExecuteScript(web::WebState* web_state, | 28 std::unique_ptr<base::Value> ExecuteScript(web::WebState* web_state, |
| 45 const std::string& script) { | 29 const std::string& script) { |
| 46 __block std::unique_ptr<base::Value> result; | 30 __block std::unique_ptr<base::Value> result; |
| 47 __block bool did_finish = false; | 31 __block bool did_finish = false; |
| 48 web_state->ExecuteJavaScript(base::UTF8ToUTF16(script), | 32 web_state->ExecuteJavaScript(base::UTF8ToUTF16(script), |
| 49 base::BindBlock(^(const base::Value* value) { | 33 base::BindBlock(^(const base::Value* value) { |
| 50 if (value) | 34 if (value) |
| 51 result = value->CreateDeepCopy(); | 35 result = value->CreateDeepCopy(); |
| 52 did_finish = true; | 36 did_finish = true; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 65 // ../web_view_matchers.mm:ll:cc: error: call to implicitly-deleted copy | 49 // ../web_view_matchers.mm:ll:cc: error: call to implicitly-deleted copy |
| 66 // constructor of 'std::unique_ptr<base::Value>' | 50 // constructor of 'std::unique_ptr<base::Value>' |
| 67 std::unique_ptr<base::Value> stack_result = std::move(result); | 51 std::unique_ptr<base::Value> stack_result = std::move(result); |
| 68 return stack_result; | 52 return stack_result; |
| 69 } | 53 } |
| 70 | 54 |
| 71 } // namespace | 55 } // namespace |
| 72 | 56 |
| 73 namespace web { | 57 namespace web { |
| 74 | 58 |
| 59 id<GREYMatcher> webViewInWebState(web::WebState* webState) { |
| 60 return [GREYMatchers matcherForWebViewInWebState:webState]; |
| 61 } |
| 62 |
| 75 id<GREYMatcher> webViewContainingText(const std::string& text, | 63 id<GREYMatcher> webViewContainingText(const std::string& text, |
| 76 web::WebState* webState) { | 64 web::WebState* webState) { |
| 77 return | 65 return |
| 78 [GREYMatchers matcherForWebViewContainingText:text inWebState:webState]; | 66 [GREYMatchers matcherForWebViewContainingText:text inWebState:webState]; |
| 79 } | 67 } |
| 80 | 68 |
| 81 id<GREYMatcher> webViewCssSelector(const std::string& selector, | 69 id<GREYMatcher> webViewCssSelector(const std::string& selector, |
| 82 web::WebState* webState) { | 70 web::WebState* webState) { |
| 83 return | 71 return |
| 84 [GREYMatchers matcherForWebWithCSSSelector:selector inWebState:webState]; | 72 [GREYMatchers matcherForWebWithCSSSelector:selector inWebState:webState]; |
| 85 } | 73 } |
| 86 | 74 |
| 87 id<GREYMatcher> webViewScrollView(web::WebState* webState) { | 75 id<GREYMatcher> webViewScrollView(web::WebState* webState) { |
| 88 return [GREYMatchers matcherForWebViewScrollViewInWebState:webState]; | 76 return [GREYMatchers matcherForWebViewScrollViewInWebState:webState]; |
| 89 } | 77 } |
| 90 | 78 |
| 91 } // namespace web | 79 } // namespace web |
| 92 | 80 |
| 93 @implementation GREYMatchers (WebViewAdditions) | 81 @implementation GREYMatchers (WebViewAdditions) |
| 94 | 82 |
| 83 + (id<GREYMatcher>)matcherForWebViewInWebState:(web::WebState*)webState { |
| 84 MatchesBlock matches = ^BOOL(UIView* view) { |
| 85 return [view isKindOfClass:[WKWebView class]] && |
| 86 [view isDescendantOfView:webState->GetView()]; |
| 87 }; |
| 88 |
| 89 DescribeToBlock describe = ^(id<GREYDescription> description) { |
| 90 [description appendText:@"web view in web state"]; |
| 91 }; |
| 92 |
| 93 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| 94 descriptionBlock:describe] |
| 95 autorelease]; |
| 96 } |
| 97 |
| 95 + (id<GREYMatcher>)matcherForWebViewContainingText:(const std::string&)text | 98 + (id<GREYMatcher>)matcherForWebViewContainingText:(const std::string&)text |
| 96 inWebState:(web::WebState*)webState { | 99 inWebState:(web::WebState*)webState { |
| 97 std::string textCopyForBlock = text; | 100 std::string textCopyForBlock = text; |
| 98 MatchesBlock matches = ^BOOL(WKWebView*) { | 101 MatchesBlock matches = ^BOOL(WKWebView*) { |
| 99 __block BOOL didSucceed = NO; | 102 __block BOOL didSucceed = NO; |
| 100 NSDate* deadline = | 103 NSDate* deadline = |
| 101 [NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout]; | 104 [NSDate dateWithTimeIntervalSinceNow:testing::kWaitForUIElementTimeout]; |
| 102 while (([[NSDate date] compare:deadline] != NSOrderedDescending) && | 105 while (([[NSDate date] compare:deadline] != NSOrderedDescending) && |
| 103 !didSucceed) { | 106 !didSucceed) { |
| 104 std::unique_ptr<base::Value> value = | 107 std::unique_ptr<base::Value> value = |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 DescribeToBlock describe = ^(id<GREYDescription> description) { | 171 DescribeToBlock describe = ^(id<GREYDescription> description) { |
| 169 [description appendText:@"web view scroll view"]; | 172 [description appendText:@"web view scroll view"]; |
| 170 }; | 173 }; |
| 171 | 174 |
| 172 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | 175 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| 173 descriptionBlock:describe] | 176 descriptionBlock:describe] |
| 174 autorelease]; | 177 autorelease]; |
| 175 } | 178 } |
| 176 | 179 |
| 177 @end | 180 @end |
| OLD | NEW |