OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/earl_grey/js_test_util.h" | 5 #import "ios/web/public/test/earl_grey/js_test_util.h" |
6 | 6 |
7 #import <EarlGrey/EarlGrey.h> | 7 #import <EarlGrey/EarlGrey.h> |
8 #import <WebKit/WebKit.h> | 8 #import <WebKit/WebKit.h> |
9 | 9 |
10 #include "base/timer/elapsed_timer.h" | 10 #include "base/timer/elapsed_timer.h" |
11 #import "ios/testing/wait_util.h" | 11 #import "ios/testing/wait_util.h" |
12 #import "ios/web/interstitials/web_interstitial_impl.h" | 12 #import "ios/web/interstitials/web_interstitial_impl.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 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 16 #error "This file requires ARC support." |
| 17 #endif |
| 18 |
15 using testing::kWaitForJSCompletionTimeout; | 19 using testing::kWaitForJSCompletionTimeout; |
16 using testing::WaitUntilConditionOrTimeout; | 20 using testing::WaitUntilConditionOrTimeout; |
17 | 21 |
18 namespace web { | 22 namespace web { |
19 | 23 |
20 // Evaluates the given |script| on |interstitial|. | 24 // Evaluates the given |script| on |interstitial|. |
21 void ExecuteScriptForTesting(web::WebInterstitialImpl* interstitial, | 25 void ExecuteScriptForTesting(web::WebInterstitialImpl* interstitial, |
22 NSString* script, | 26 NSString* script, |
23 web::JavaScriptResultBlock handler) { | 27 web::JavaScriptResultBlock handler) { |
24 DCHECK(interstitial); | 28 DCHECK(interstitial); |
(...skipping 22 matching lines...) Expand all Loading... |
47 is_window_id_injected = [result isEqual:@0]; | 51 is_window_id_injected = [result isEqual:@0]; |
48 } | 52 } |
49 is_timeout = timeout < timer.Elapsed(); | 53 is_timeout = timeout < timer.Elapsed(); |
50 } | 54 } |
51 GREYAssertFalse(is_timeout, @"windowID injection timed out"); | 55 GREYAssertFalse(is_timeout, @"windowID injection timed out"); |
52 GREYAssertFalse(is_unrecoverable_error, @"script execution error"); | 56 GREYAssertFalse(is_unrecoverable_error, @"script execution error"); |
53 } | 57 } |
54 | 58 |
55 id ExecuteJavaScript(WebState* web_state, | 59 id ExecuteJavaScript(WebState* web_state, |
56 NSString* javascript, | 60 NSString* javascript, |
57 NSError** out_error) { | 61 NSError* __autoreleasing* out_error) { |
58 __block bool did_complete = false; | 62 __block bool did_complete = false; |
59 __block id result = nil; | 63 __block id result = nil; |
60 CRWJSInjectionReceiver* receiver = web_state->GetJSInjectionReceiver(); | 64 CRWJSInjectionReceiver* receiver = web_state->GetJSInjectionReceiver(); |
61 [receiver executeJavaScript:javascript | 65 [receiver executeJavaScript:javascript |
62 completionHandler:^(id value, NSError* error) { | 66 completionHandler:^(id value, NSError* error) { |
63 did_complete = true; | 67 did_complete = true; |
64 result = [value copy]; | 68 result = [value copy]; |
65 if (out_error) | 69 if (out_error) |
66 *out_error = [error copy]; | 70 *out_error = [error copy]; |
67 }]; | 71 }]; |
68 | 72 |
69 // Wait for completion. | 73 // Wait for completion. |
70 BOOL suceeded = WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{ | 74 BOOL suceeded = WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{ |
71 return did_complete; | 75 return did_complete; |
72 }); | 76 }); |
73 GREYAssert(suceeded, @"Script execution timed out"); | 77 GREYAssert(suceeded, @"Script execution timed out"); |
74 | 78 |
75 if (out_error) | 79 return result; |
76 [*out_error autorelease]; | |
77 return [result autorelease]; | |
78 } | 80 } |
79 | 81 |
80 id ExecuteScriptOnInterstitial(WebState* web_state, NSString* script) { | 82 id ExecuteScriptOnInterstitial(WebState* web_state, NSString* script) { |
81 web::WebInterstitialImpl* interstitial = | 83 web::WebInterstitialImpl* interstitial = |
82 static_cast<web::WebInterstitialImpl*>(web_state->GetWebInterstitial()); | 84 static_cast<web::WebInterstitialImpl*>(web_state->GetWebInterstitial()); |
83 | 85 |
84 __block id script_result = nil; | 86 __block id script_result = nil; |
85 __block bool did_finish = false; | 87 __block bool did_finish = false; |
86 web::ExecuteScriptForTesting(interstitial, script, ^(id result, NSError*) { | 88 web::ExecuteScriptForTesting(interstitial, script, ^(id result, NSError*) { |
87 script_result = [result copy]; | 89 script_result = [result copy]; |
88 did_finish = true; | 90 did_finish = true; |
89 }); | 91 }); |
90 BOOL suceeded = WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{ | 92 BOOL suceeded = WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{ |
91 return did_finish; | 93 return did_finish; |
92 }); | 94 }); |
93 GREYAssert(suceeded, @"Script execution timed out"); | 95 GREYAssert(suceeded, @"Script execution timed out"); |
94 return [script_result autorelease]; | 96 return script_result; |
95 } | 97 } |
96 | 98 |
97 } // namespace web | 99 } // namespace web |
OLD | NEW |