Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/js_test_util.h" | 5 #import "ios/web/public/test/js_test_util.h" |
| 6 | 6 |
| 7 #import <WebKit/WebKit.h> | 7 #import <WebKit/WebKit.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #import "base/mac/scoped_nsobject.h" | 10 #import "base/mac/scoped_nsobject.h" |
| 11 #import "base/test/ios/wait_util.h" | 11 #import "ios/testing/wait_util.h" |
| 12 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" | 12 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" |
| 13 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 13 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| 14 | 14 |
| 15 namespace web { | 15 namespace web { |
| 16 | 16 |
| 17 id ExecuteJavaScript(CRWJSInjectionManager* manager, NSString* script) { | 17 id ExecuteJavaScript(CRWJSInjectionManager* manager, NSString* script) { |
| 18 __block base::scoped_nsobject<NSString> result; | 18 __block base::scoped_nsobject<NSString> result; |
| 19 __block bool completed = false; | 19 __block bool completed = false; |
| 20 [manager executeJavaScript:script | 20 [manager executeJavaScript:script |
| 21 completionHandler:^(id execution_result, NSError* error) { | 21 completionHandler:^(id execution_result, NSError* error) { |
| 22 DCHECK(!error); | 22 DCHECK(!error); |
| 23 result.reset([execution_result copy]); | 23 result.reset([execution_result copy]); |
| 24 completed = true; | 24 completed = true; |
| 25 }]; | 25 }]; |
| 26 | 26 |
| 27 base::test::ios::WaitUntilCondition(^{ | 27 BOOL success = testing::WaitUntilConditionOrTimeout( |
| 28 return completed; | 28 testing::kWaitForJSCompletionTimeout, ^{ |
| 29 }); | 29 return completed; |
| 30 }); | |
| 31 DCHECK(success) | |
|
Eugene But (OOO till 7-30)
2017/03/02 21:27:26
Ideally we should not DCHECK in tests. Can we log
michaeldo
2017/03/02 21:34:37
We can log, however I didn't want to change the cu
| |
| 32 << "CRWJSInjectionManager failed to complete javascript execution."; | |
| 30 return [[result retain] autorelease]; | 33 return [[result retain] autorelease]; |
| 31 } | 34 } |
| 32 | 35 |
| 33 id ExecuteJavaScript(CRWJSInjectionReceiver* receiver, NSString* script) { | 36 id ExecuteJavaScript(CRWJSInjectionReceiver* receiver, NSString* script) { |
| 34 base::scoped_nsobject<CRWJSInjectionManager> manager( | 37 base::scoped_nsobject<CRWJSInjectionManager> manager( |
| 35 [[CRWJSInjectionManager alloc] initWithReceiver:receiver]); | 38 [[CRWJSInjectionManager alloc] initWithReceiver:receiver]); |
| 36 return ExecuteJavaScript(manager, script); | 39 return ExecuteJavaScript(manager, script); |
| 37 } | 40 } |
| 38 | 41 |
| 39 id ExecuteJavaScript(WKWebView* web_view, NSString* script) { | 42 id ExecuteJavaScript(WKWebView* web_view, NSString* script) { |
| 40 return ExecuteJavaScript(web_view, script, nullptr); | 43 return ExecuteJavaScript(web_view, script, nullptr); |
| 41 } | 44 } |
| 42 | 45 |
| 43 id ExecuteJavaScript(WKWebView* web_view, NSString* script, NSError** error) { | 46 id ExecuteJavaScript(WKWebView* web_view, NSString* script, NSError** error) { |
| 44 __block base::scoped_nsobject<id> result; | 47 __block base::scoped_nsobject<id> result; |
| 45 __block bool completed = false; | 48 __block bool completed = false; |
| 46 [web_view evaluateJavaScript:script | 49 [web_view evaluateJavaScript:script |
| 47 completionHandler:^(id script_result, NSError* script_error) { | 50 completionHandler:^(id script_result, NSError* script_error) { |
| 48 result.reset([script_result copy]); | 51 result.reset([script_result copy]); |
| 49 if (error) | 52 if (error) |
| 50 *error = [[script_error copy] autorelease]; | 53 *error = [[script_error copy] autorelease]; |
| 51 completed = true; | 54 completed = true; |
| 52 }]; | 55 }]; |
| 53 base::test::ios::WaitUntilCondition(^{ | 56 BOOL success = testing::WaitUntilConditionOrTimeout( |
| 54 return completed; | 57 testing::kWaitForJSCompletionTimeout, ^{ |
| 55 }); | 58 return completed; |
| 59 }); | |
| 60 DCHECK(success) << "WKWebView failed to complete javascript execution."; | |
| 56 return [[result retain] autorelease]; | 61 return [[result retain] autorelease]; |
| 57 } | 62 } |
| 58 | 63 |
| 59 } // namespace web | 64 } // namespace web |
| 60 | 65 |
| OLD | NEW |