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