| 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 #include "base/strings/sys_string_conversions.h" |
| 12 #import "ios/testing/wait_util.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_manager.h" |
| 13 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 14 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 16 |
| 15 namespace web { | 17 namespace web { |
| 16 | 18 |
| 17 id ExecuteJavaScript(CRWJSInjectionManager* manager, NSString* script) { | 19 id ExecuteJavaScript(CRWJSInjectionManager* manager, NSString* script) { |
| 18 __block base::scoped_nsobject<NSString> result; | 20 __block base::scoped_nsobject<NSString> result; |
| 19 __block bool completed = false; | 21 __block bool completed = false; |
| 20 [manager executeJavaScript:script | 22 [manager executeJavaScript:script |
| 21 completionHandler:^(id execution_result, NSError* error) { | 23 completionHandler:^(id execution_result, NSError* error) { |
| 22 DCHECK(!error); | 24 DCHECK(!error); |
| 23 result.reset([execution_result copy]); | 25 result.reset([execution_result copy]); |
| 24 completed = true; | 26 completed = true; |
| 25 }]; | 27 }]; |
| 26 | 28 |
| 27 base::test::ios::WaitUntilCondition(^{ | 29 BOOL success = testing::WaitUntilConditionOrTimeout( |
| 28 return completed; | 30 testing::kWaitForJSCompletionTimeout, ^{ |
| 29 }); | 31 return completed; |
| 32 }); |
| 33 // Log stack trace to provide some context. |
| 34 EXPECT_TRUE(success) |
| 35 << "CRWJSInjectionManager failed to complete javascript execution.\n" |
| 36 << base::SysNSStringToUTF8( |
| 37 [[NSThread callStackSymbols] componentsJoinedByString:@"\n"]); |
| 30 return [[result retain] autorelease]; | 38 return [[result retain] autorelease]; |
| 31 } | 39 } |
| 32 | 40 |
| 33 id ExecuteJavaScript(CRWJSInjectionReceiver* receiver, NSString* script) { | 41 id ExecuteJavaScript(CRWJSInjectionReceiver* receiver, NSString* script) { |
| 34 base::scoped_nsobject<CRWJSInjectionManager> manager( | 42 base::scoped_nsobject<CRWJSInjectionManager> manager( |
| 35 [[CRWJSInjectionManager alloc] initWithReceiver:receiver]); | 43 [[CRWJSInjectionManager alloc] initWithReceiver:receiver]); |
| 36 return ExecuteJavaScript(manager, script); | 44 return ExecuteJavaScript(manager, script); |
| 37 } | 45 } |
| 38 | 46 |
| 39 id ExecuteJavaScript(WKWebView* web_view, NSString* script) { | 47 id ExecuteJavaScript(WKWebView* web_view, NSString* script) { |
| 40 return ExecuteJavaScript(web_view, script, nullptr); | 48 return ExecuteJavaScript(web_view, script, nullptr); |
| 41 } | 49 } |
| 42 | 50 |
| 43 id ExecuteJavaScript(WKWebView* web_view, NSString* script, NSError** error) { | 51 id ExecuteJavaScript(WKWebView* web_view, NSString* script, NSError** error) { |
| 44 __block base::scoped_nsobject<id> result; | 52 __block base::scoped_nsobject<id> result; |
| 45 __block bool completed = false; | 53 __block bool completed = false; |
| 46 [web_view evaluateJavaScript:script | 54 [web_view evaluateJavaScript:script |
| 47 completionHandler:^(id script_result, NSError* script_error) { | 55 completionHandler:^(id script_result, NSError* script_error) { |
| 48 result.reset([script_result copy]); | 56 result.reset([script_result copy]); |
| 49 if (error) | 57 if (error) |
| 50 *error = [[script_error copy] autorelease]; | 58 *error = [[script_error copy] autorelease]; |
| 51 completed = true; | 59 completed = true; |
| 52 }]; | 60 }]; |
| 53 base::test::ios::WaitUntilCondition(^{ | 61 BOOL success = testing::WaitUntilConditionOrTimeout( |
| 54 return completed; | 62 testing::kWaitForJSCompletionTimeout, ^{ |
| 55 }); | 63 return completed; |
| 64 }); |
| 65 // Log stack trace to provide some context. |
| 66 EXPECT_TRUE(success) << "WKWebView failed to complete javascript execution.\n" |
| 67 << base::SysNSStringToUTF8([[NSThread callStackSymbols] |
| 68 componentsJoinedByString:@"\n"]); |
| 56 return [[result retain] autorelease]; | 69 return [[result retain] autorelease]; |
| 57 } | 70 } |
| 58 | 71 |
| 59 } // namespace web | 72 } // namespace web |
| 60 | 73 |
| OLD | NEW |