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/web_view_actions.h" | 5 #import "ios/web/public/test/earl_grey/web_view_actions.h" |
6 | 6 |
7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/mac/bind_objc_block.h" | 9 #include "base/mac/bind_objc_block.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 return true; | 98 return true; |
99 } | 99 } |
100 | 100 |
101 // Removes the injected callback. | 101 // Removes the injected callback. |
102 void RemoveVerifierForElementWithId(web::WebState* web_state, | 102 void RemoveVerifierForElementWithId(web::WebState* web_state, |
103 const std::string& element_id) { | 103 const std::string& element_id) { |
104 static_cast<web::WebStateImpl*>(web_state)->RemoveScriptCommandCallback( | 104 static_cast<web::WebStateImpl*>(web_state)->RemoveScriptCommandCallback( |
105 CallbackPrefixForElementId(element_id)); | 105 CallbackPrefixForElementId(element_id)); |
106 } | 106 } |
107 | 107 |
108 // Finds the location of the element |element_id| in the webview of |state|. | |
109 // If the element is found, returns the action created by |action_factory| with | |
110 // the location of the element. Otherwise returns a failing action. | |
111 id<GREYAction> webviewRunActionOnElement( | |
Eugene But (OOO till 7-30)
2016/09/07 16:44:43
This method does not run an action, but constructs
jif
2016/09/08 13:41:01
Sounds better, thanks.
| |
112 WebState* state, | |
113 const std::string& element_id, | |
114 id<GREYAction> (^action_factory)(CGPoint)) { | |
115 CGRect rect = web::test::GetBoundingRectOfElementWithId(state, element_id); | |
116 // Check if |rect| is empty; if it is, return an action that just throws an | |
117 // error. | |
118 if (CGRectIsEmpty(rect)) { | |
119 NSString* description = [NSString | |
120 stringWithFormat:@"Couldn't locate a bounding rect for element_id %s; " | |
121 @"either it isn't there or it has no area.", | |
122 element_id.c_str()]; | |
123 GREYPerformBlock throw_error = ^BOOL(id /* element */, | |
124 __strong NSError** error) { | |
125 NSDictionary* user_info = @{NSLocalizedDescriptionKey : description}; | |
126 *error = [NSError errorWithDomain:kGREYInteractionErrorDomain | |
127 code:kGREYInteractionActionFailedErrorCode | |
128 userInfo:user_info]; | |
129 return NO; | |
130 }; | |
131 return [GREYActionBlock actionWithName:@"Locate element bounds" | |
132 performBlock:throw_error]; | |
133 } | |
134 | |
135 // If there's a usable rect, long-press in the center. | |
136 CGPoint point = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)); | |
137 return action_factory(point); | |
138 } | |
139 | |
108 } // namespace | 140 } // namespace |
109 | 141 |
110 namespace web { | 142 namespace web { |
111 | 143 |
112 id<GREYAction> webViewVerifiedActionOnElement(WebState* state, | 144 id<GREYAction> webViewVerifiedActionOnElement(WebState* state, |
113 id<GREYAction> action, | 145 id<GREYAction> action, |
114 const std::string& element_id) { | 146 const std::string& element_id) { |
115 NSString* action_name = | 147 NSString* action_name = |
116 [NSString stringWithFormat:@"Verified action (%@) on webview element %s.", | 148 [NSString stringWithFormat:@"Verified action (%@) on webview element %s.", |
117 action.name, element_id.c_str()]; | 149 action.name, element_id.c_str()]; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 | 203 |
172 return [GREYActionBlock actionWithName:action_name | 204 return [GREYActionBlock actionWithName:action_name |
173 constraints:webViewInWebState(state) | 205 constraints:webViewInWebState(state) |
174 performBlock:verified_tap]; | 206 performBlock:verified_tap]; |
175 } | 207 } |
176 | 208 |
177 id<GREYAction> webViewLongPressElementForContextMenu( | 209 id<GREYAction> webViewLongPressElementForContextMenu( |
178 WebState* state, | 210 WebState* state, |
179 const std::string& element_id, | 211 const std::string& element_id, |
180 bool triggers_context_menu) { | 212 bool triggers_context_menu) { |
181 CGRect rect = web::test::GetBoundingRectOfElementWithId(state, element_id); | 213 std::string element_id_copy = element_id; |
182 // Check if |rect| is empty; if it is, return an action that just throws an | 214 id<GREYAction> (^action_factory)(CGPoint) = ^id<GREYAction>(CGPoint point) { |
183 // error. | 215 id<GREYAction> longpress = |
184 if (CGRectIsEmpty(rect)) { | 216 grey_longPressAtPointWithDuration(point, kContextMenuLongPressDuration); |
185 NSString* description = [NSString | 217 id<GREYAction> action = nil; |
186 stringWithFormat:@"Couldn't locate a bounding rect for element_id %s; " | 218 if (triggers_context_menu) { |
187 @"either it isn't there or it has no area.", | 219 action = longpress; |
188 element_id.c_str()]; | 220 } else { |
189 GREYPerformBlock throw_error = ^BOOL(id /* element */, | 221 action = |
190 __strong NSError** error) { | 222 webViewVerifiedActionOnElement(state, longpress, element_id_copy); |
191 NSDictionary* user_info = @{NSLocalizedDescriptionKey : description}; | 223 } |
192 *error = [NSError errorWithDomain:kGREYInteractionErrorDomain | 224 return action; |
193 code:kGREYInteractionActionFailedErrorCode | 225 }; |
194 userInfo:user_info]; | |
195 return NO; | |
196 }; | |
197 return [GREYActionBlock actionWithName:@"Locate element bounds" | |
198 performBlock:throw_error]; | |
199 } | |
200 | 226 |
201 // If there's a usable rect, long-press in the center. | 227 return webviewRunActionOnElement(state, element_id, action_factory); |
202 CGPoint point = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)); | 228 } |
203 | 229 |
204 id<GREYAction> longpress = | 230 id<GREYAction> webViewPressElement(WebState* state, |
205 grey_longPressAtPointWithDuration(point, kContextMenuLongPressDuration); | 231 const std::string& element_id) { |
206 id<GREYAction> action = longpress; | 232 id<GREYAction> (^action_factory)(CGPoint) = ^id<GREYAction>(CGPoint point) { |
207 | 233 return grey_tapAtPoint(point); |
Eugene But (OOO till 7-30)
2016/09/07 16:56:38
Oh and this should be webViewVerifiedActionOnEleme
jif
2016/09/08 13:41:01
Done.
| |
208 if (!triggers_context_menu) { | 234 }; |
209 action = webViewVerifiedActionOnElement(state, longpress, element_id); | 235 return webviewRunActionOnElement(state, element_id, action_factory); |
210 } | |
211 | |
212 return action; | |
213 } | 236 } |
214 | 237 |
215 } // namespace web | 238 } // namespace web |
OLD | NEW |