blob: b8ea2de3abfd0655eda2128947b6f61b986f6174 [file] [log] [blame]
Keisuke Kuroyanagi2ff41d42015-12-01 16:58:53 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package android.widget.espresso;
18
19import org.hamcrest.Matcher;
20
21import android.support.test.espresso.UiController;
22import android.support.test.espresso.ViewAction;
23import android.support.test.espresso.action.CoordinatesProvider;
24import android.support.test.espresso.action.GeneralClickAction;
Keisuke Kuroyanagi46faad62015-12-06 10:03:23 -080025import android.support.test.espresso.action.MotionEvents;
26import android.support.test.espresso.action.MotionEvents.DownResultHolder;
Keisuke Kuroyanagi46faad62015-12-06 10:03:23 -080027import android.support.test.espresso.action.Press;
Keisuke Kuroyanagi2ff41d42015-12-01 16:58:53 -080028import android.support.test.espresso.action.Tapper;
Keisuke Kuroyanagia0b3c062015-12-07 11:09:40 -080029import android.view.MotionEvent;
Keisuke Kuroyanagi2ff41d42015-12-01 16:58:53 -080030import android.view.View;
Keisuke Kuroyanagi46faad62015-12-06 10:03:23 -080031import android.view.ViewConfiguration;
Keisuke Kuroyanagi2ff41d42015-12-01 16:58:53 -080032
33/**
34 * ViewAction for performing an click on View by a mouse.
35 */
36public final class MouseClickAction implements ViewAction {
37 private final GeneralClickAction mGeneralClickAction;
Keisuke Kuroyanagia0b3c062015-12-07 11:09:40 -080038 @MouseUiController.MouseButton
39 private final int mButton;
Keisuke Kuroyanagi2ff41d42015-12-01 16:58:53 -080040
Keisuke Kuroyanagi46faad62015-12-06 10:03:23 -080041 public enum CLICK implements Tapper {
42 TRIPLE {
43 @Override
44 public Tapper.Status sendTap(UiController uiController, float[] coordinates,
45 float[] precision) {
46 Tapper.Status stat = sendSingleTap(uiController, coordinates, precision);
47 boolean warning = false;
48 if (stat == Tapper.Status.FAILURE) {
49 return Tapper.Status.FAILURE;
50 } else if (stat == Tapper.Status.WARNING) {
51 warning = true;
52 }
53
54 long doubleTapMinimumTimeout = ViewConfiguration.getDoubleTapMinTime();
55 for (int i = 0; i < 2; i++) {
56 if (0 < doubleTapMinimumTimeout) {
57 uiController.loopMainThreadForAtLeast(doubleTapMinimumTimeout);
58 }
59 stat = sendSingleTap(uiController, coordinates, precision);
60 if (stat == Tapper.Status.FAILURE) {
61 return Tapper.Status.FAILURE;
62 } else if (stat == Tapper.Status.WARNING) {
63 warning = true;
64 }
65 }
66
67 if (warning) {
68 return Tapper.Status.WARNING;
69 } else {
70 return Tapper.Status.SUCCESS;
71 }
72 }
73 };
74
75 private static Tapper.Status sendSingleTap(UiController uiController,
76 float[] coordinates, float[] precision) {
77 DownResultHolder res = MotionEvents.sendDown(uiController, coordinates, precision);
78 try {
79 if (!MotionEvents.sendUp(uiController, res.down)) {
80 MotionEvents.sendCancel(uiController, res.down);
81 return Tapper.Status.FAILURE;
82 }
83 } finally {
84 res.down.recycle();
85 }
86 return res.longPress ? Tapper.Status.WARNING : Tapper.Status.SUCCESS;
87 }
88 };
89
90 public MouseClickAction(Tapper tapper, CoordinatesProvider coordinatesProvider) {
Keisuke Kuroyanagia0b3c062015-12-07 11:09:40 -080091 this(tapper, coordinatesProvider, MotionEvent.BUTTON_PRIMARY);
92 }
93
94 /**
95 * Constructs MouseClickAction
96 *
97 * @param tapper the tapper
98 * @param coordinatesProvider the provider of the event coordinates
99 * @param button the mouse button used to send motion events
100 */
101 public MouseClickAction(Tapper tapper, CoordinatesProvider coordinatesProvider,
102 @MouseUiController.MouseButton int button) {
103 mGeneralClickAction = new GeneralClickAction(tapper, coordinatesProvider, Press.PINPOINT);
104 mButton = button;
Keisuke Kuroyanagi2ff41d42015-12-01 16:58:53 -0800105 }
106
107 @Override
108 public Matcher<View> getConstraints() {
109 return mGeneralClickAction.getConstraints();
110 }
111
112 @Override
113 public String getDescription() {
114 return mGeneralClickAction.getDescription();
115 }
116
117 @Override
118 public void perform(UiController uiController, View view) {
Keisuke Kuroyanagia0b3c062015-12-07 11:09:40 -0800119 mGeneralClickAction.perform(new MouseUiController(uiController, mButton), view);
Keisuke Kuroyanagi46faad62015-12-06 10:03:23 -0800120 long doubleTapTimeout = ViewConfiguration.getDoubleTapTimeout();
121 if (0 < doubleTapTimeout) {
122 // Wait to avoid false gesture detection. Without this wait, consecutive clicks can be
123 // detected as a triple click. e.g. 2 double clicks are detected as a triple click and
124 // a single click because espresso isn't aware of triple click detection logic, which
125 // is TextView specific gesture.
126 uiController.loopMainThreadForAtLeast(doubleTapTimeout);
127 }
Keisuke Kuroyanagi2ff41d42015-12-01 16:58:53 -0800128 }
129}