Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(350)

Unified Diff: ios/web/public/test/earl_grey/web_view_actions.mm

Issue 2321613002: Add helper that taps on elements in the webview. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ios/web/public/test/earl_grey/web_view_actions.mm
diff --git a/ios/web/public/test/earl_grey/web_view_actions.mm b/ios/web/public/test/earl_grey/web_view_actions.mm
index ec9df6b30ff32ac3003c42d15c22de15b048cd71..11c7031f94e19107bb477e255b97452ef8f4f7ed 100644
--- a/ios/web/public/test/earl_grey/web_view_actions.mm
+++ b/ios/web/public/test/earl_grey/web_view_actions.mm
@@ -105,6 +105,38 @@ void RemoveVerifierForElementWithId(web::WebState* web_state,
CallbackPrefixForElementId(element_id));
}
+// Finds the location of the element |element_id| in the webview of |state|.
+// If the element is found, returns the action created by |action_factory| with
+// the location of the element. Otherwise returns a failing action.
+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.
+ WebState* state,
+ const std::string& element_id,
+ id<GREYAction> (^action_factory)(CGPoint)) {
+ CGRect rect = web::test::GetBoundingRectOfElementWithId(state, element_id);
+ // Check if |rect| is empty; if it is, return an action that just throws an
+ // error.
+ if (CGRectIsEmpty(rect)) {
+ NSString* description = [NSString
+ stringWithFormat:@"Couldn't locate a bounding rect for element_id %s; "
+ @"either it isn't there or it has no area.",
+ element_id.c_str()];
+ GREYPerformBlock throw_error = ^BOOL(id /* element */,
+ __strong NSError** error) {
+ NSDictionary* user_info = @{NSLocalizedDescriptionKey : description};
+ *error = [NSError errorWithDomain:kGREYInteractionErrorDomain
+ code:kGREYInteractionActionFailedErrorCode
+ userInfo:user_info];
+ return NO;
+ };
+ return [GREYActionBlock actionWithName:@"Locate element bounds"
+ performBlock:throw_error];
+ }
+
+ // If there's a usable rect, long-press in the center.
+ CGPoint point = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
+ return action_factory(point);
+}
+
} // namespace
namespace web {
@@ -178,38 +210,29 @@ id<GREYAction> webViewLongPressElementForContextMenu(
WebState* state,
const std::string& element_id,
bool triggers_context_menu) {
- CGRect rect = web::test::GetBoundingRectOfElementWithId(state, element_id);
- // Check if |rect| is empty; if it is, return an action that just throws an
- // error.
- if (CGRectIsEmpty(rect)) {
- NSString* description = [NSString
- stringWithFormat:@"Couldn't locate a bounding rect for element_id %s; "
- @"either it isn't there or it has no area.",
- element_id.c_str()];
- GREYPerformBlock throw_error = ^BOOL(id /* element */,
- __strong NSError** error) {
- NSDictionary* user_info = @{NSLocalizedDescriptionKey : description};
- *error = [NSError errorWithDomain:kGREYInteractionErrorDomain
- code:kGREYInteractionActionFailedErrorCode
- userInfo:user_info];
- return NO;
- };
- return [GREYActionBlock actionWithName:@"Locate element bounds"
- performBlock:throw_error];
- }
-
- // If there's a usable rect, long-press in the center.
- CGPoint point = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
-
- id<GREYAction> longpress =
- grey_longPressAtPointWithDuration(point, kContextMenuLongPressDuration);
- id<GREYAction> action = longpress;
+ std::string element_id_copy = element_id;
+ id<GREYAction> (^action_factory)(CGPoint) = ^id<GREYAction>(CGPoint point) {
+ id<GREYAction> longpress =
+ grey_longPressAtPointWithDuration(point, kContextMenuLongPressDuration);
+ id<GREYAction> action = nil;
+ if (triggers_context_menu) {
+ action = longpress;
+ } else {
+ action =
+ webViewVerifiedActionOnElement(state, longpress, element_id_copy);
+ }
+ return action;
+ };
- if (!triggers_context_menu) {
- action = webViewVerifiedActionOnElement(state, longpress, element_id);
- }
+ return webviewRunActionOnElement(state, element_id, action_factory);
+}
- return action;
+id<GREYAction> webViewPressElement(WebState* state,
+ const std::string& element_id) {
+ id<GREYAction> (^action_factory)(CGPoint) = ^id<GREYAction>(CGPoint point) {
+ 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.
+ };
+ return webviewRunActionOnElement(state, element_id, action_factory);
}
} // namespace web

Powered by Google App Engine
This is Rietveld 408576698