How to use injectMotionEvent method of android.support.test.espresso.UiController class

Best Appium-espresso-driver code snippet using android.support.test.espresso.UiController.injectMotionEvent

GesturesUiTestUtils.kt

Source:GesturesUiTestUtils.kt Github

copy

Full Screen

...143 startTime, eventTime,144 MotionEvent.ACTION_DOWN, 1, properties,145 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0146 )147 injectMotionEventToUiController(uiController, event)148 // Step 2149 event = MotionEvent.obtain(150 startTime, eventTime,151 MotionEvent.ACTION_POINTER_DOWN + (pp2.id shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 2,152 properties, pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0153 )154 injectMotionEventToUiController(uiController, event)155 // Step 3, 4156 val moveEventNumber = duration / eventMinInterval157 val stepX1: Float158 val stepY1: Float159 val stepX2: Float160 val stepY2: Float161 stepX1 = (endPoint1.x - startPoint1.x) / moveEventNumber162 stepY1 = (endPoint1.y - startPoint1.y) / moveEventNumber163 stepX2 = (endPoint2.x - startPoint2.x) / moveEventNumber164 stepY2 = (endPoint2.y - startPoint2.y) / moveEventNumber165 for (i in 0 until moveEventNumber) {166 // Update the move events167 eventTime += eventMinInterval168 eventX1 += stepX1169 eventY1 += stepY1170 eventX2 += stepX2171 eventY2 += stepY2172 pc1.x = eventX1173 pc1.y = eventY1174 pc2.x = eventX2175 pc2.y = eventY2176 pointerCoords[0] = pc1177 pointerCoords[1] = pc2178 event = MotionEvent.obtain(179 startTime, eventTime,180 MotionEvent.ACTION_MOVE, 2, properties,181 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0182 )183 injectMotionEventToUiController(uiController, event)184 }185 // Step 5186 pc1.x = endPoint1.x187 pc1.y = endPoint1.y188 pc2.x = endPoint2.x189 pc2.y = endPoint2.y190 pointerCoords[0] = pc1191 pointerCoords[1] = pc2192 eventTime += eventMinInterval193 event = MotionEvent.obtain(194 startTime, eventTime,195 MotionEvent.ACTION_POINTER_UP + (pp2.id shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 2, properties,196 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0197 )198 injectMotionEventToUiController(uiController, event)199 // Step 6200 eventTime += eventMinInterval201 event = MotionEvent.obtain(202 startTime, eventTime,203 MotionEvent.ACTION_UP, 1, properties,204 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0205 )206 injectMotionEventToUiController(uiController, event)207 } catch (ex: InjectEventSecurityException) {208 throw RuntimeException("Could not perform pinch", ex)209 }210 }211 private fun performQuickScale(212 uiController: UiController,213 startPoint: PointF,214 endPoint: PointF,215 withVelocity: Boolean,216 duration: Long,217 interrupt: Boolean218 ) {219 val eventMinInterval: Long = 10220 val tapDownMinInterval: Long = 40 // ViewConfiguration#DOUBLE_TAP_MIN_TIME = 40221 val startTime = SystemClock.uptimeMillis()222 var eventTime = startTime223 var event: MotionEvent224 var eventX1: Float = startPoint.x225 var eventY1: Float = startPoint.y226 var properties = arrayOfNulls<MotionEvent.PointerProperties>(1)227 val pp1 = MotionEvent.PointerProperties()228 pp1.id = 0229 pp1.toolType = MotionEvent.TOOL_TYPE_FINGER230 properties[0] = pp1231 var pointerCoords = arrayOfNulls<MotionEvent.PointerCoords>(1)232 val pc1 = MotionEvent.PointerCoords()233 pc1.x = eventX1234 pc1.y = eventY1235 pc1.pressure = 1f236 pc1.size = 1f237 pointerCoords[0] = pc1238 /*239 * Events sequence of quick scale gesture:240 *241 * 1. Send ACTION_DOWN242 * 2. Send ACTION_UP243 * 3. Send ACTION_DOWN244 * 4. Send ACTION_MOVE with updated middle points (x,y), until reach the end points245 * 5. Send ACTION_UP of one end point246 */247 try {248 // Step 1249 event = MotionEvent.obtain(250 startTime, eventTime,251 MotionEvent.ACTION_DOWN, 1, properties,252 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0253 )254 injectMotionEventToUiController(uiController, event)255 // Step 2256 eventTime += eventMinInterval257 event = MotionEvent.obtain(258 startTime, eventTime,259 MotionEvent.ACTION_UP, 1, properties,260 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0261 )262 injectMotionEventToUiController(uiController, event)263 // Step 3264 eventTime += tapDownMinInterval265 event = MotionEvent.obtain(266 startTime, eventTime,267 MotionEvent.ACTION_DOWN, 1, properties,268 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0269 )270 injectMotionEventToUiController(uiController, event)271 // Step 4272 val moveEventNumber = duration / eventMinInterval273 val stepX1: Float274 val stepY1: Float275 stepX1 = (endPoint.x - startPoint.x) / moveEventNumber276 stepY1 = (endPoint.y - startPoint.y) / moveEventNumber277 var interrupted = false278 for (i in 0 until moveEventNumber) {279 // Update the move events280 eventTime += eventMinInterval281 eventX1 += stepX1282 eventY1 += stepY1283 pc1.x = eventX1284 pc1.y = eventY1285 pointerCoords[0] = pc1286 event = MotionEvent.obtain(287 startTime, eventTime,288 MotionEvent.ACTION_MOVE, if (interrupted) 2 else 1, properties,289 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0290 )291 injectMotionEventToUiController(uiController, event)292 if (interrupt && i == moveEventNumber / 2) {293 // Specify the property for the two touch points294 properties = arrayOfNulls<MotionEvent.PointerProperties>(2)295 val pp2 = MotionEvent.PointerProperties()296 pp2.id = 1297 pp2.toolType = MotionEvent.TOOL_TYPE_FINGER298 properties[0] = pp1299 properties[1] = pp2300 // Specify the coordinations of the two touch points301 // NOTE: you MUST set the pressure and size value, or it doesn't work302 pointerCoords = arrayOfNulls<MotionEvent.PointerCoords>(2)303 val pc2 = MotionEvent.PointerCoords()304 pc2.x = startPoint.x305 pc2.y = startPoint.y306 pc2.pressure = 1f307 pc2.size = 1f308 pointerCoords[0] = pc1309 pointerCoords[1] = pc2310 eventTime += eventMinInterval311 event = MotionEvent.obtain(312 startTime, eventTime,313 MotionEvent.ACTION_POINTER_DOWN + (pp2.id shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 2,314 properties, pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0315 )316 injectMotionEventToUiController(uiController, event)317 interrupted = true318 }319 }320 if (!withVelocity) {321 eventTime += eventMinInterval322 event = MotionEvent.obtain(323 startTime, eventTime,324 MotionEvent.ACTION_MOVE, 1, properties,325 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0326 )327 injectMotionEventToUiController(uiController, event)328 }329 // Step 5330 eventTime += eventMinInterval331 event = MotionEvent.obtain(332 startTime, eventTime,333 MotionEvent.ACTION_UP, 1, properties,334 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0335 )336 injectMotionEventToUiController(uiController, event)337 } catch (ex: InjectEventSecurityException) {338 throw RuntimeException("Could not perform quick scale", ex)339 }340 }341 private fun performMove(342 uiController: UiController,343 startPoint: PointF,344 endPoint: PointF,345 withVelocity: Boolean,346 duration: Long347 ) {348 val eventMinInterval: Long = 10349 val startTime = SystemClock.uptimeMillis()350 var eventTime = startTime351 var event: MotionEvent352 var eventX1: Float = startPoint.x353 var eventY1: Float = startPoint.y354 val properties = arrayOfNulls<MotionEvent.PointerProperties>(1)355 val pp1 = MotionEvent.PointerProperties()356 pp1.id = 0357 pp1.toolType = MotionEvent.TOOL_TYPE_FINGER358 properties[0] = pp1359 val pointerCoords = arrayOfNulls<MotionEvent.PointerCoords>(1)360 val pc1 = MotionEvent.PointerCoords()361 pc1.x = eventX1362 pc1.y = eventY1363 pc1.pressure = 1f364 pc1.size = 1f365 pointerCoords[0] = pc1366 /*367 * Events sequence of move gesture:368 *369 * 1. Send ACTION_DOWN370 * 2. Send ACTION_MOVE with updated middle points (x,y), until reach the end points371 * 3. Send ACTION_UP372 */373 try {374 // Step 1375 event = MotionEvent.obtain(376 startTime, eventTime,377 MotionEvent.ACTION_DOWN, 1, properties,378 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0379 )380 injectMotionEventToUiController(uiController, event)381 // Step 2382 val moveEventNumber = duration / eventMinInterval383 val stepX1: Float384 val stepY1: Float385 stepX1 = (endPoint.x - startPoint.x) / moveEventNumber386 stepY1 = (endPoint.y - startPoint.y) / moveEventNumber387 for (i in 0 until moveEventNumber) {388 // Update the move events389 eventTime += eventMinInterval390 eventX1 += stepX1391 eventY1 += stepY1392 pc1.x = eventX1393 pc1.y = eventY1394 pointerCoords[0] = pc1395 event = MotionEvent.obtain(396 startTime, eventTime,397 MotionEvent.ACTION_MOVE, 1, properties,398 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0399 )400 injectMotionEventToUiController(uiController, event)401 }402 if (!withVelocity) {403 eventTime += eventMinInterval404 event = MotionEvent.obtain(405 startTime, eventTime,406 MotionEvent.ACTION_MOVE, 1, properties,407 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0408 )409 injectMotionEventToUiController(uiController, event)410 }411 // Step 3412 eventTime += eventMinInterval413 event = MotionEvent.obtain(414 startTime, eventTime,415 MotionEvent.ACTION_UP, 1, properties,416 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0417 )418 injectMotionEventToUiController(uiController, event)419 } catch (ex: InjectEventSecurityException) {420 throw RuntimeException("Could not perform quick scale", ex)421 }422 }423 /**424 * Safely call uiController.injectMotionEvent(event): Detect any error and "convert" it to an425 * IllegalStateException426 */427 @Throws(InjectEventSecurityException::class)428 private fun injectMotionEventToUiController(uiController: UiController, event: MotionEvent) {429 val injectEventSucceeded = uiController.injectMotionEvent(event)430 if (!injectEventSucceeded) {431 throw IllegalStateException("Error performing event $event")432 }433 }434}435fun Int.loopFor(millis: Long) {436 Espresso.onView(ViewMatchers.withId(this)).perform(object : ViewAction {437 override fun getDescription(): String = "waiting for $millis"438 override fun getConstraints(): Matcher<View> = ViewMatchers.isEnabled()439 override fun perform(uiController: UiController?, view: View?) {440 uiController?.loopMainThreadForAtLeast(millis)441 }442 })443}...

Full Screen

Full Screen

TestUtils.kt

Source:TestUtils.kt Github

copy

Full Screen

...150 startTime, eventTime,151 MotionEvent.ACTION_DOWN, 1, properties,152 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0153 )154 injectMotionEventToUiController(uiController, event)155 // Step 2156 event = MotionEvent.obtain(157 startTime,158 eventTime,159 MotionEvent.ACTION_POINTER_DOWN + (pp2.id shl MotionEvent.ACTION_POINTER_INDEX_SHIFT),160 2,161 properties,162 pointerCoords,163 0,164 0,165 1f,166 1f,167 0,168 0,169 0,170 0171 )172 injectMotionEventToUiController(uiController, event)173 // Step 3, 4174 val moveEventNumber = duration / eventMinInterval175 val stepX1: Float176 val stepY1: Float177 val stepX2: Float178 val stepY2: Float179 stepX1 = ((endPoint1.x - startPoint1.x) / moveEventNumber).toFloat()180 stepY1 = ((endPoint1.y - startPoint1.y) / moveEventNumber).toFloat()181 stepX2 = ((endPoint2.x - startPoint2.x) / moveEventNumber).toFloat()182 stepY2 = ((endPoint2.y - startPoint2.y) / moveEventNumber).toFloat()183 for (i in 0 until moveEventNumber) {184 // Update the move events185 eventTime += eventMinInterval186 eventX1 += stepX1187 eventY1 += stepY1188 eventX2 += stepX2189 eventY2 += stepY2190 pc1.x = eventX1191 pc1.y = eventY1192 pc2.x = eventX2193 pc2.y = eventY2194 pointerCoords[0] = pc1195 pointerCoords[1] = pc2196 event = MotionEvent.obtain(197 startTime, eventTime,198 MotionEvent.ACTION_MOVE, 2, properties,199 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0200 )201 injectMotionEventToUiController(uiController, event)202 }203 // Step 5204 pc1.x = endPoint1.x.toFloat()205 pc1.y = endPoint1.y.toFloat()206 pc2.x = endPoint2.x.toFloat()207 pc2.y = endPoint2.y.toFloat()208 pointerCoords[0] = pc1209 pointerCoords[1] = pc2210 eventTime += eventMinInterval211 event = MotionEvent.obtain(212 startTime,213 eventTime,214 MotionEvent.ACTION_POINTER_UP + (pp2.id shl MotionEvent.ACTION_POINTER_INDEX_SHIFT),215 2,216 properties,217 pointerCoords,218 0,219 0,220 1f,221 1f,222 0,223 0,224 0,225 0226 )227 injectMotionEventToUiController(uiController, event)228 // Step 6229 eventTime += eventMinInterval230 event = MotionEvent.obtain(231 startTime, eventTime,232 MotionEvent.ACTION_UP, 1, properties,233 pointerCoords, 0, 0, 1f, 1f, 0, 0, 0, 0234 )235 injectMotionEventToUiController(uiController, event)236 } catch (e: InjectEventSecurityException) {237 throw RuntimeException("Could not perform pinch", e)238 }239}240@Throws(InjectEventSecurityException::class)241private fun injectMotionEventToUiController(uiController: UiController, event: MotionEvent) {242 val injectEventSucceeded = uiController.injectMotionEvent(event)243 if (!injectEventSucceeded) {244 throw IllegalStateException("Error performing event " + event)245 }246}247/**248 * Returns ArgumentCaptor.capture() as nullable type to avoid java.lang.IllegalStateException249 * when null is returned.250 */251fun <T> capture(argumentCaptor: ArgumentCaptor<T>): T = argumentCaptor.capture()252fun suspendViaGLSurface() {253 Espresso.onView(ViewMatchers.withId(R.id.glSurface)).check(254 ViewAssertions.matches(255 ViewMatchers.isDisplayed()256 )...

Full Screen

Full Screen

injectMotionEvent

Using AI Code Generation

copy

Full Screen

1public static void injectMotionEvent(UiController uiController, int action, float x, float y) {2long downTime = SystemClock.uptimeMillis();3long eventTime = SystemClock.uptimeMillis();4MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, 0);5uiController.injectMotionEvent(event);6}7public static void injectMotionEvent(UiController uiController, int action, float x, float y) {8long downTime = SystemClock.uptimeMillis();9long eventTime = SystemClock.uptimeMillis();10MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, 0);11uiController.injectMotionEvent(event);12}13public static void injectMotionEvent(UiController uiController, int action, float x, float y) {14long downTime = SystemClock.uptimeMillis();15long eventTime = SystemClock.uptimeMillis();16MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, 0);17uiController.injectMotionEvent(event);18}19public static void injectMotionEvent(UiController uiController, int action, float x, float y) {20long downTime = SystemClock.uptimeMillis();21long eventTime = SystemClock.uptimeMillis();22MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, 0);23uiController.injectMotionEvent(event);24}25public static void injectMotionEvent(UiController uiController, int action, float x, float y) {26long downTime = SystemClock.uptimeMillis();27long eventTime = SystemClock.uptimeMillis();28MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, 0);29uiController.injectMotionEvent(event);30}31public static void injectMotionEvent(UiController uiController, int action, float x, float y) {32long downTime = SystemClock.uptimeMillis();33long eventTime = SystemClock.uptimeMillis();34MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x,

Full Screen

Full Screen

injectMotionEvent

Using AI Code Generation

copy

Full Screen

1public static void injectMotionEvent(int action, float x, float y) {2UiController uiController = InstrumentationRegistry.getInstrumentation().getUiController();3MotionEvent event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), action, x, y, 0);4uiController.injectMotionEvent(event);5}6public static void injectKeyEvent(int action, int keycode) {7UiController uiController = InstrumentationRegistry.getInstrumentation().getUiController();8KeyEvent event = new KeyEvent(action, keycode);9uiController.injectKeyEvent(event);10}11public static void injectString(String text) {12UiController uiController = InstrumentationRegistry.getInstrumentation().getUiController();13uiController.injectString(text);14}15public static void injectString(int resId) {16UiController uiController = InstrumentationRegistry.getInstrumentation().getUiController();17uiController.injectString(InstrumentationRegistry.getTargetContext().getString(resId));18}19public static void injectString(String text, int repeat) {20UiController uiController = InstrumentationRegistry.getInstrumentation().getUiController();21for (int i = 0; i < repeat; i++) {22uiController.injectString(text);23}24}25public static void injectString(int resId, int repeat) {26UiController uiController = InstrumentationRegistry.getInstrumentation().getUiController();27for (int i = 0; i < repeat; i++) {28uiController.injectString(InstrumentationRegistry.getTargetContext().getString(resId));29}30}31public static void injectString(String text, int repeat, long delay) {32UiController uiController = InstrumentationRegistry.getInstrumentation().getUiController();33for (int i = 0; i < repeat; i++) {34uiController.injectString(text);35SystemClock.sleep(delay);36}37}38public static void injectString(int resId, int repeat, long delay) {39UiController uiController = InstrumentationRegistry.getInstrumentation().getUi

Full Screen

Full Screen

injectMotionEvent

Using AI Code Generation

copy

Full Screen

1public static void injectMotionEvent ( int action , float x , float y ) { UiController uiController = ( UiController ) InstrumentationRegistry . getInstrumentation (); uiController . injectMotionEvent ( MotionEvents . sendDown ( uiController , x , y , 0 ). down , action ); }2public static void injectKeyEvent ( int action , int keyCode ) { UiController uiController = ( UiController ) InstrumentationRegistry . getInstrumentation (); uiController . injectKeyEvent ( new KeyEvent ( action , keyCode )); }3public static void injectString ( String string ) { UiController uiController = ( UiController ) InstrumentationRegistry . getInstrumentation (); uiController . injectString ( string ); }4public static void injectString ( String string ) { UiController uiController = ( UiController ) InstrumentationRegistry . getInstrumentation (); uiController . injectString ( string ); }5public static void injectString ( String string ) { UiController uiController = ( UiController ) InstrumentationRegistry . getInstrumentation (); uiController . injectString ( string ); }6public static void injectString ( String string ) { UiController uiController = ( UiController ) InstrumentationRegistry . getInstrumentation (); uiController . injectString ( string ); }7public static void injectString ( String string ) { UiController uiController = ( UiController ) InstrumentationRegistry . getInstrumentation (); uiController . injectString ( string ); }8public static void injectString ( String string ) { UiController uiController = ( UiController ) InstrumentationRegistry . getInstrumentation (); uiController . injectString ( string ); }9public static void injectString ( String string ) { UiController uiController = ( UiController ) InstrumentationRegistry . getInstrumentation (); uiController . injectString ( string ); }

Full Screen

Full Screen

injectMotionEvent

Using AI Code Generation

copy

Full Screen

1public static void swipeLeft() {2UiController uiController = InstrumentationRegistry.getInstrumentation().getUiController();3MotionEvent downEvent = MotionEvent.obtain(4SystemClock.uptimeMillis(),5SystemClock.uptimeMillis(),60);7MotionEvent upEvent = MotionEvent.obtain(8SystemClock.uptimeMillis(),9SystemClock.uptimeMillis(),100);11uiController.injectMotionEvent(downEvent);12uiController.injectMotionEvent(upEvent);13}14public static void swipeRight() {15UiController uiController = InstrumentationRegistry.getInstrumentation().getUiController();16MotionEvent downEvent = MotionEvent.obtain(17SystemClock.uptimeMillis(),18SystemClock.uptimeMillis(),190);20MotionEvent upEvent = MotionEvent.obtain(21SystemClock.uptimeMillis(),22SystemClock.uptimeMillis(),230);24uiController.injectMotionEvent(downEvent);25uiController.injectMotionEvent(upEvent);26}27}28public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);29public void swipeLeftTest() {30SwipeUtils.swipeLeft();31onView(withId(R.id.text_view))32.check(matches(withText("Left")));33}34public void swipeRightTest() {35SwipeUtils.swipeRight();36onView(withId(R.id.text_view))37.check(matches(withText("Right")));38}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Appium-espresso-driver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in UiController

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful