Keisuke Kuroyanagi | 2ff41d4 | 2015-12-01 16:58:53 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.widget.espresso; |
| 18 | |
| 19 | import org.hamcrest.Matcher; |
| 20 | |
| 21 | import android.support.test.espresso.UiController; |
| 22 | import android.support.test.espresso.ViewAction; |
| 23 | import android.support.test.espresso.action.CoordinatesProvider; |
| 24 | import android.support.test.espresso.action.GeneralClickAction; |
Keisuke Kuroyanagi | 46faad6 | 2015-12-06 10:03:23 -0800 | [diff] [blame] | 25 | import android.support.test.espresso.action.MotionEvents; |
| 26 | import android.support.test.espresso.action.MotionEvents.DownResultHolder; |
Keisuke Kuroyanagi | 46faad6 | 2015-12-06 10:03:23 -0800 | [diff] [blame] | 27 | import android.support.test.espresso.action.Press; |
Keisuke Kuroyanagi | 2ff41d4 | 2015-12-01 16:58:53 -0800 | [diff] [blame] | 28 | import android.support.test.espresso.action.Tapper; |
Keisuke Kuroyanagi | a0b3c06 | 2015-12-07 11:09:40 -0800 | [diff] [blame] | 29 | import android.view.MotionEvent; |
Keisuke Kuroyanagi | 2ff41d4 | 2015-12-01 16:58:53 -0800 | [diff] [blame] | 30 | import android.view.View; |
Keisuke Kuroyanagi | 46faad6 | 2015-12-06 10:03:23 -0800 | [diff] [blame] | 31 | import android.view.ViewConfiguration; |
Keisuke Kuroyanagi | 2ff41d4 | 2015-12-01 16:58:53 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * ViewAction for performing an click on View by a mouse. |
| 35 | */ |
| 36 | public final class MouseClickAction implements ViewAction { |
| 37 | private final GeneralClickAction mGeneralClickAction; |
Keisuke Kuroyanagi | a0b3c06 | 2015-12-07 11:09:40 -0800 | [diff] [blame] | 38 | @MouseUiController.MouseButton |
| 39 | private final int mButton; |
Keisuke Kuroyanagi | 2ff41d4 | 2015-12-01 16:58:53 -0800 | [diff] [blame] | 40 | |
Keisuke Kuroyanagi | 46faad6 | 2015-12-06 10:03:23 -0800 | [diff] [blame] | 41 | 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 Kuroyanagi | a0b3c06 | 2015-12-07 11:09:40 -0800 | [diff] [blame] | 91 | 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 Kuroyanagi | 2ff41d4 | 2015-12-01 16:58:53 -0800 | [diff] [blame] | 105 | } |
| 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 Kuroyanagi | a0b3c06 | 2015-12-07 11:09:40 -0800 | [diff] [blame] | 119 | mGeneralClickAction.perform(new MouseUiController(uiController, mButton), view); |
Keisuke Kuroyanagi | 46faad6 | 2015-12-06 10:03:23 -0800 | [diff] [blame] | 120 | 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 Kuroyanagi | 2ff41d4 | 2015-12-01 16:58:53 -0800 | [diff] [blame] | 128 | } |
| 129 | } |