| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "ios/web_view/test/web_view_interaction_test_util.h" | |
| 6 | |
| 7 #import <ChromeWebView/ChromeWebView.h> | |
| 8 | |
| 9 #import "ios/testing/wait_util.h" | |
| 10 | |
| 11 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 12 #error "This file requires ARC support." | |
| 13 #endif | |
| 14 | |
| 15 using testing::WaitUntilConditionOrTimeout; | |
| 16 | |
| 17 namespace ios_web_view { | |
| 18 namespace test { | |
| 19 | |
| 20 bool TapChromeWebViewElementWithId(CWVWebView* web_view, NSString* element_id) { | |
| 21 // TODO(crbug.com/725524): Share this script with Chrome. | |
| 22 NSString* script = [NSString | |
| 23 stringWithFormat:@"(function() {" | |
| 24 " var element = document.getElementById('%@');" | |
| 25 " if (element) {" | |
| 26 " element.click();" | |
| 27 " return true;" | |
| 28 " }" | |
| 29 " return false;" | |
| 30 "})();", | |
| 31 element_id]; | |
| 32 return [EvaluateJavaScript(web_view, script, nil) boolValue]; | |
| 33 } | |
| 34 | |
| 35 id EvaluateJavaScript(CWVWebView* web_view, NSString* script, NSError** error) { | |
| 36 __block bool did_complete = false; | |
| 37 __block id evaluation_result = nil; | |
| 38 __block id evaluation_error = nil; | |
| 39 [web_view evaluateJavaScript:script | |
| 40 completionHandler:^(id local_result, NSError* local_error) { | |
| 41 did_complete = true; | |
| 42 evaluation_result = [local_result copy]; | |
| 43 evaluation_error = [local_error copy]; | |
| 44 }]; | |
| 45 | |
| 46 WaitUntilConditionOrTimeout(testing::kWaitForJSCompletionTimeout, ^{ | |
| 47 return did_complete; | |
| 48 }); | |
| 49 | |
| 50 if (error) | |
| 51 *error = evaluation_error; | |
| 52 | |
| 53 return evaluation_result; | |
| 54 } | |
| 55 | |
| 56 bool WaitForWebViewContainingTextOrTimeout(CWVWebView* web_view, | |
| 57 NSString* text) { | |
| 58 return WaitUntilConditionOrTimeout(testing::kWaitForUIElementTimeout, ^{ | |
| 59 id body = ios_web_view::test::EvaluateJavaScript( | |
| 60 web_view, @"document.body ? document.body.textContent : null", nil); | |
| 61 return [body isKindOfClass:[NSString class]] && [body containsString:text]; | |
| 62 }); | |
| 63 } | |
| 64 | |
| 65 } // namespace test | |
| 66 } // namespace ios_web_view | |
| OLD | NEW |