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/web_view_interaction_test_util.h" | 5 #import "ios/web/public/test/web_view_interaction_test_util.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 | 8 |
9 #include "ios/testing/earl_grey/wait_util.h" | 9 #include "base/test/ios/wait_util.h" |
10 #import "ios/web/web_state/ui/crw_web_controller.h" | 10 #import "ios/web/web_state/ui/crw_web_controller.h" |
11 #include "ios/web/web_state/web_state_impl.h" | 11 #include "ios/web/web_state/web_state_impl.h" |
12 | 12 |
13 using web::NavigationManager; | 13 using web::NavigationManager; |
14 | 14 |
15 namespace web { | 15 namespace web { |
16 namespace test { | 16 namespace test { |
17 | 17 |
| 18 const NSTimeInterval kWaitForJSCompletionTimeout = 2.0; |
| 19 const NSTimeInterval kSpinDelaySeconds = 0.01; |
| 20 |
18 enum ElementAction { ELEMENT_ACTION_CLICK, ELEMENT_ACTION_FOCUS }; | 21 enum ElementAction { ELEMENT_ACTION_CLICK, ELEMENT_ACTION_FOCUS }; |
19 | 22 |
20 // Returns whether the Javascript action specified by |action| ran on | 23 // Returns whether the Javascript action specified by |action| ran on |
21 // |element_id| in the passed |web_state|. | 24 // |element_id| in the passed |web_state|. |
22 bool RunActionOnWebViewElementWithId(web::WebState* web_state, | 25 bool RunActionOnWebViewElementWithId(web::WebState* web_state, |
23 const std::string& element_id, | 26 const std::string& element_id, |
24 ElementAction action) { | 27 ElementAction action) { |
25 CRWWebController* web_controller = | 28 CRWWebController* web_controller = |
26 static_cast<WebStateImpl*>(web_state)->GetWebController(); | 29 static_cast<WebStateImpl*>(web_state)->GetWebController(); |
27 const char* js_action = nullptr; | 30 const char* js_action = nullptr; |
(...skipping 16 matching lines...) Expand all Loading... |
44 "})();", | 47 "})();", |
45 element_id.c_str(), js_action]; | 48 element_id.c_str(), js_action]; |
46 __block bool did_complete = false; | 49 __block bool did_complete = false; |
47 __block bool element_found = false; | 50 __block bool element_found = false; |
48 [web_controller executeUserJavaScript:script | 51 [web_controller executeUserJavaScript:script |
49 completionHandler:^(id result, NSError*) { | 52 completionHandler:^(id result, NSError*) { |
50 did_complete = true; | 53 did_complete = true; |
51 element_found = [result boolValue]; | 54 element_found = [result boolValue]; |
52 }]; | 55 }]; |
53 | 56 |
54 testing::WaitUntilCondition(testing::kWaitForJSCompletionTimeout, ^{ | 57 NSDate* deadline = |
55 return did_complete; | 58 [NSDate dateWithTimeIntervalSinceNow:kWaitForJSCompletionTimeout]; |
56 }); | 59 while (!did_complete && |
| 60 [[NSDate date] compare:deadline] != NSOrderedDescending) { |
| 61 base::test::ios::SpinRunLoopWithMaxDelay( |
| 62 base::TimeDelta::FromSecondsD(kSpinDelaySeconds)); |
| 63 } |
57 | 64 |
58 return element_found; | 65 return did_complete && element_found; |
59 } | 66 } |
60 | 67 |
61 bool TapWebViewElementWithId(web::WebState* web_state, | 68 bool TapWebViewElementWithId(web::WebState* web_state, |
62 const std::string& element_id) { | 69 const std::string& element_id) { |
63 return RunActionOnWebViewElementWithId(web_state, element_id, | 70 return RunActionOnWebViewElementWithId(web_state, element_id, |
64 ELEMENT_ACTION_CLICK); | 71 ELEMENT_ACTION_CLICK); |
65 } | 72 } |
66 | 73 |
67 bool FocusWebViewElementWithId(web::WebState* web_state, | 74 bool FocusWebViewElementWithId(web::WebState* web_state, |
68 const std::string& element_id) { | 75 const std::string& element_id) { |
69 return RunActionOnWebViewElementWithId(web_state, element_id, | 76 return RunActionOnWebViewElementWithId(web_state, element_id, |
70 ELEMENT_ACTION_FOCUS); | 77 ELEMENT_ACTION_FOCUS); |
71 } | 78 } |
72 | 79 |
73 } // namespace test | 80 } // namespace test |
74 } // namespace web | 81 } // namespace web |
OLD | NEW |