OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/web/public/test/earl_grey/js_test_util.h" |
| 6 |
| 7 #import <EarlGrey/EarlGrey.h> |
| 8 #import <WebKit/WebKit.h> |
| 9 |
| 10 #include "base/timer/elapsed_timer.h" |
| 11 #import "ios/testing/earl_grey/wait_util.h" |
| 12 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| 13 |
| 14 namespace web { |
| 15 |
| 16 void WaitUntilWindowIdInjected(WebState* web_state) { |
| 17 bool is_window_id_injected = false; |
| 18 bool is_timeout = false; |
| 19 bool is_unrecoverable_error = false; |
| 20 |
| 21 base::ElapsedTimer timer; |
| 22 base::TimeDelta timeout = |
| 23 base::TimeDelta::FromSeconds(testing::kWaitForJSCompletionTimeout); |
| 24 |
| 25 // Keep polling until either the JavaScript execution returns with expected |
| 26 // value (indicating that Window ID is set), the timeout occurs, or an |
| 27 // unrecoverable error occurs. |
| 28 while (!is_window_id_injected && !is_timeout && !is_unrecoverable_error) { |
| 29 NSError* error = nil; |
| 30 id result = ExecuteJavaScript(web_state, @"0", &error); |
| 31 if (error) { |
| 32 is_unrecoverable_error = ![error.domain isEqual:WKErrorDomain] || |
| 33 error.code != WKErrorJavaScriptExceptionOccurred; |
| 34 } else { |
| 35 is_window_id_injected = [result isEqual:@0]; |
| 36 } |
| 37 is_timeout = timeout < timer.Elapsed(); |
| 38 } |
| 39 GREYAssertFalse(is_timeout, @"windowID injection timed out"); |
| 40 GREYAssertFalse(is_unrecoverable_error, @"script execution error"); |
| 41 } |
| 42 |
| 43 id ExecuteJavaScript(WebState* web_state, |
| 44 NSString* javascript, |
| 45 NSError** out_error) { |
| 46 __block BOOL did_complete = NO; |
| 47 __block id result = nil; |
| 48 CRWJSInjectionReceiver* receiver = web_state->GetJSInjectionReceiver(); |
| 49 [receiver executeJavaScript:javascript |
| 50 completionHandler:^(id value, NSError* error) { |
| 51 did_complete = YES; |
| 52 result = [value copy]; |
| 53 if (out_error) |
| 54 *out_error = [error copy]; |
| 55 }]; |
| 56 |
| 57 // Wait for completion. |
| 58 GREYCondition* condition = [GREYCondition |
| 59 conditionWithName:@"Wait for JavaScript execution to complete." |
| 60 block:^{ |
| 61 return did_complete; |
| 62 }]; |
| 63 GREYAssert([condition waitWithTimeout:testing::kWaitForJSCompletionTimeout], |
| 64 @"Script execution timed out"); |
| 65 |
| 66 if (out_error) |
| 67 [*out_error autorelease]; |
| 68 return [result autorelease]; |
| 69 } |
| 70 |
| 71 } // namespace web |
OLD | NEW |