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 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | 15 |
| 15 namespace web { | 16 namespace web { |
| 16 | 17 |
| 17 id ExecuteJavaScript(CRWJSInjectionManager* manager, NSString* script) { | 18 id ExecuteJavaScript(CRWJSInjectionManager* manager, NSString* script) { |
| 18 __block base::scoped_nsobject<NSString> result; | 19 __block base::scoped_nsobject<NSString> result; |
| 19 __block bool completed = false; | 20 __block bool completed = false; |
| 20 [manager executeJavaScript:script | 21 [manager executeJavaScript:script |
| 21 completionHandler:^(id execution_result, NSError* error) { | 22 completionHandler:^(id execution_result, NSError* error) { |
| 22 DCHECK(!error); | 23 DCHECK(!error); |
| 23 result.reset([execution_result copy]); | 24 result.reset([execution_result copy]); |
| 24 completed = true; | 25 completed = true; |
| 25 }]; | 26 }]; |
| 26 | 27 |
| 27 base::test::ios::WaitUntilCondition(^{ | 28 BOOL success = testing::WaitUntilConditionOrTimeout( |
| 28 return completed; | 29 testing::kWaitForJSCompletionTimeout, ^{ |
| 29 }); | 30 return completed; |
| 31 }); | |
| 32 // Log stack trace to provide some context. | |
| 33 EXPECT_TRUE(success) | |
|
michaeldo
2017/03/03 00:11:00
I already have another CL in which I'd like to add
Eugene But (OOO till 7-30)
2017/03/03 01:48:06
I believe we should not have different word ("Expe
michaeldo
2017/03/03 18:17:08
Acknowledged.
| |
| 34 << "CRWJSInjectionManager failed to complete javascript execution.\n" | |
| 35 << [[[NSThread callStackSymbols] componentsJoinedByString:@"\n"] | |
|
Eugene But (OOO till 7-30)
2017/03/03 01:48:06
Please use sys_string_conversions.h instead of NSS
michaeldo
2017/03/03 18:17:08
Of course, thank you.
| |
| 36 cStringUsingEncoding:NSUTF8StringEncoding]; | |
| 30 return [[result retain] autorelease]; | 37 return [[result retain] autorelease]; |
| 31 } | 38 } |
| 32 | 39 |
| 33 id ExecuteJavaScript(CRWJSInjectionReceiver* receiver, NSString* script) { | 40 id ExecuteJavaScript(CRWJSInjectionReceiver* receiver, NSString* script) { |
| 34 base::scoped_nsobject<CRWJSInjectionManager> manager( | 41 base::scoped_nsobject<CRWJSInjectionManager> manager( |
| 35 [[CRWJSInjectionManager alloc] initWithReceiver:receiver]); | 42 [[CRWJSInjectionManager alloc] initWithReceiver:receiver]); |
| 36 return ExecuteJavaScript(manager, script); | 43 return ExecuteJavaScript(manager, script); |
| 37 } | 44 } |
| 38 | 45 |
| 39 id ExecuteJavaScript(WKWebView* web_view, NSString* script) { | 46 id ExecuteJavaScript(WKWebView* web_view, NSString* script) { |
| 40 return ExecuteJavaScript(web_view, script, nullptr); | 47 return ExecuteJavaScript(web_view, script, nullptr); |
| 41 } | 48 } |
| 42 | 49 |
| 43 id ExecuteJavaScript(WKWebView* web_view, NSString* script, NSError** error) { | 50 id ExecuteJavaScript(WKWebView* web_view, NSString* script, NSError** error) { |
| 44 __block base::scoped_nsobject<id> result; | 51 __block base::scoped_nsobject<id> result; |
| 45 __block bool completed = false; | 52 __block bool completed = false; |
| 46 [web_view evaluateJavaScript:script | 53 [web_view evaluateJavaScript:script |
| 47 completionHandler:^(id script_result, NSError* script_error) { | 54 completionHandler:^(id script_result, NSError* script_error) { |
| 48 result.reset([script_result copy]); | 55 result.reset([script_result copy]); |
| 49 if (error) | 56 if (error) |
| 50 *error = [[script_error copy] autorelease]; | 57 *error = [[script_error copy] autorelease]; |
| 51 completed = true; | 58 completed = true; |
| 52 }]; | 59 }]; |
| 53 base::test::ios::WaitUntilCondition(^{ | 60 BOOL success = testing::WaitUntilConditionOrTimeout( |
| 54 return completed; | 61 testing::kWaitForJSCompletionTimeout, ^{ |
| 55 }); | 62 return completed; |
| 63 }); | |
| 64 // Log stack trace to provide some context. | |
| 65 EXPECT_TRUE(success) | |
| 66 << "WKWebView failed to complete javascript execution.\n" | |
| 67 << [[[NSThread callStackSymbols] componentsJoinedByString:@"\n"] | |
| 68 cStringUsingEncoding:NSUTF8StringEncoding]; | |
|
Eugene But (OOO till 7-30)
2017/03/03 01:48:06
ditto
michaeldo
2017/03/03 18:17:08
Done.
| |
| 56 return [[result retain] autorelease]; | 69 return [[result retain] autorelease]; |
| 57 } | 70 } |
| 58 | 71 |
| 59 } // namespace web | 72 } // namespace web |
| 60 | 73 |
| OLD | NEW |