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 android.support.test.espresso.UiController; 20 import android.support.test.espresso.ViewAction; 21 import android.support.test.espresso.action.CoordinatesProvider; 22 import android.support.test.espresso.action.MotionEvents; 23 import android.support.test.espresso.action.MotionEvents.DownResultHolder; 24 import android.support.test.espresso.action.Press; 25 import android.support.test.espresso.action.Tapper; 26 import android.view.InputDevice; 27 import android.view.MotionEvent; 28 import android.view.View; 29 import android.view.ViewConfiguration; 30 31 import org.hamcrest.Matcher; 32 33 /** 34 * ViewAction for performing an click on View by a mouse. 35 */ 36 public final class MouseClickAction implements ViewAction { 37 private final ViewClickAction mViewClickAction; 38 @MouseUiController.MouseButton 39 private final int mButton; 40 41 public enum CLICK implements Tapper { 42 TRIPLE { 43 @Override 44 public Tapper.Status sendTap(UiController uiController, float[] coordinates, 45 float[] precision, int inputDevice, int buttonState) { 46 Tapper.Status stat = sendSingleTap(uiController, coordinates, precision, 47 inputDevice, buttonState); 48 boolean warning = false; 49 if (stat == Tapper.Status.FAILURE) { 50 return Tapper.Status.FAILURE; 51 } else if (stat == Tapper.Status.WARNING) { 52 warning = true; 53 } 54 55 long doubleTapMinimumTimeout = ViewConfiguration.getDoubleTapMinTime(); 56 for (int i = 0; i < 2; i++) { 57 if (0 < doubleTapMinimumTimeout) { 58 uiController.loopMainThreadForAtLeast(doubleTapMinimumTimeout); 59 } 60 stat = sendSingleTap(uiController, coordinates, precision, inputDevice, 61 buttonState); 62 if (stat == Tapper.Status.FAILURE) { 63 return Tapper.Status.FAILURE; 64 } else if (stat == Tapper.Status.WARNING) { 65 warning = true; 66 } 67 } 68 69 if (warning) { 70 return Tapper.Status.WARNING; 71 } else { 72 return Tapper.Status.SUCCESS; 73 } 74 } 75 76 @Override 77 public Tapper.Status sendTap(UiController uiController, float[] coordinates, 78 float[] precision) { 79 return sendTap(uiController, coordinates, precision, InputDevice.SOURCE_UNKNOWN, 80 MotionEvent.BUTTON_PRIMARY); 81 } 82 }; 83 84 private static Tapper.Status sendSingleTap(UiController uiController, 85 float[] coordinates, float[] precision, int inputDevice, int buttonState) { 86 DownResultHolder res = MotionEvents.sendDown(uiController, coordinates, precision, 87 inputDevice, buttonState); 88 try { 89 if (!MotionEvents.sendUp(uiController, res.down)) { 90 MotionEvents.sendCancel(uiController, res.down); 91 return Tapper.Status.FAILURE; 92 } 93 } finally { 94 res.down.recycle(); 95 } 96 return res.longPress ? Tapper.Status.WARNING : Tapper.Status.SUCCESS; 97 } 98 }; 99 100 public MouseClickAction(Tapper tapper, CoordinatesProvider coordinatesProvider) { 101 this(tapper, coordinatesProvider, MotionEvent.BUTTON_PRIMARY); 102 } 103 104 /** 105 * Constructs MouseClickAction 106 * 107 * @param tapper the tapper 108 * @param coordinatesProvider the provider of the event coordinates 109 * @param button the mouse button used to send motion events 110 */ 111 public MouseClickAction(Tapper tapper, CoordinatesProvider coordinatesProvider, 112 @MouseUiController.MouseButton int button) { 113 mViewClickAction = new ViewClickAction(tapper, coordinatesProvider, Press.PINPOINT); 114 mButton = button; 115 } 116 117 @Override 118 public Matcher<View> getConstraints() { 119 return mViewClickAction.getConstraints(); 120 } 121 122 @Override 123 public String getDescription() { 124 return mViewClickAction.getDescription(); 125 } 126 127 @Override 128 public void perform(UiController uiController, View view) { 129 mViewClickAction.perform(new MouseUiController(uiController, mButton), view); 130 } 131 } 132