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 "ios/testing/earl_grey/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 void TapWebViewElementWithId(web::WebState* web_state, | 18 enum ElementAction { ELEMENT_ACTION_CLICK, ELEMENT_ACTION_FOCUS }; |
19 const std::string& element_id) { | |
20 RunActionOnWebViewElementWithId(web_state, element_id, CLICK); | |
21 } | |
22 | 19 |
23 void RunActionOnWebViewElementWithId(web::WebState* web_state, | 20 // Returns whether the Javascript action specified by |action| ran on |
| 21 // |element_id| in the passed |web_state|. |
| 22 bool RunActionOnWebViewElementWithId(web::WebState* web_state, |
24 const std::string& element_id, | 23 const std::string& element_id, |
25 ElementAction action) { | 24 ElementAction action) { |
26 CRWWebController* web_controller = | 25 CRWWebController* web_controller = |
27 static_cast<WebStateImpl*>(web_state)->GetWebController(); | 26 static_cast<WebStateImpl*>(web_state)->GetWebController(); |
28 const char* jsAction = nullptr; | 27 const char* js_action = nullptr; |
29 switch (action) { | 28 switch (action) { |
30 case CLICK: | 29 case ELEMENT_ACTION_CLICK: |
31 jsAction = ".click()"; | 30 js_action = ".click()"; |
32 break; | 31 break; |
33 case FOCUS: | 32 case ELEMENT_ACTION_FOCUS: |
34 jsAction = ".focus();"; | 33 js_action = ".focus();"; |
35 break; | 34 break; |
36 } | 35 } |
37 NSString* script = | 36 NSString* script = [NSString |
38 [NSString stringWithFormat:@"document.getElementById('%s')%s", | 37 stringWithFormat:@"(function() {" |
39 element_id.c_str(), jsAction]; | 38 " var element = document.getElementById('%s');" |
| 39 " if (element) {" |
| 40 " element%s;" |
| 41 " return true;" |
| 42 " }" |
| 43 " return false;" |
| 44 "})();", |
| 45 element_id.c_str(), js_action]; |
40 __block bool did_complete = false; | 46 __block bool did_complete = false; |
| 47 __block bool element_found = false; |
41 [web_controller executeUserJavaScript:script | 48 [web_controller executeUserJavaScript:script |
42 completionHandler:^(id, NSError*) { | 49 completionHandler:^(id result, NSError*) { |
43 did_complete = true; | 50 did_complete = true; |
| 51 element_found = [result boolValue]; |
44 }]; | 52 }]; |
45 | 53 |
46 testing::WaitUntilCondition(testing::kWaitForJSCompletionTimeout, ^{ | 54 testing::WaitUntilCondition(testing::kWaitForJSCompletionTimeout, ^{ |
47 return did_complete; | 55 return did_complete; |
48 }); | 56 }); |
| 57 |
| 58 return element_found; |
| 59 } |
| 60 |
| 61 bool TapWebViewElementWithId(web::WebState* web_state, |
| 62 const std::string& element_id) { |
| 63 return RunActionOnWebViewElementWithId(web_state, element_id, |
| 64 ELEMENT_ACTION_CLICK); |
| 65 } |
| 66 |
| 67 bool FocusWebViewElementWithId(web::WebState* web_state, |
| 68 const std::string& element_id) { |
| 69 return RunActionOnWebViewElementWithId(web_state, element_id, |
| 70 ELEMENT_ACTION_FOCUS); |
49 } | 71 } |
50 | 72 |
51 } // namespace test | 73 } // namespace test |
52 } // namespace web | 74 } // namespace web |
OLD | NEW |