androidx.test.espresso.action.MotionEvents Java Examples

The following examples show how to use androidx.test.espresso.action.MotionEvents. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void perform(UiController uiController, View view) {
  float[] precision = precisionDescriber.describePrecision();
  float[] start = this.start.calculateCoordinates(view);
  float[] end = this.end.calculateCoordinates(view);
  float[][] steps = interpolate(start, end, STEPS);
  int delayBetweenMovements = DURATION / steps.length;
  // Down
  MotionEvent downEvent = MotionEvents.sendDown(uiController, start, precision).down;
  try {
    for (int i = 0; i < steps.length; i++) {
      // Wait
      long desiredTime = downEvent.getDownTime() + (long) (delayBetweenMovements * i);
      long timeUntilDesired = desiredTime - SystemClock.uptimeMillis();
      if (timeUntilDesired > 10L) {
        uiController.loopMainThreadForAtLeast(timeUntilDesired);
      }
      // Move
      if (!MotionEvents.sendMovement(uiController, downEvent, steps[i])) {
        MotionEvents.sendCancel(uiController, downEvent);
        throw new RuntimeException("Cannot drag: failed to send a move event.");
      }
    }
    int duration = ViewConfiguration.getPressedStateDuration();
    if (duration > 0) {
      uiController.loopMainThreadForAtLeast((long) duration);
    }
  } finally {
    downEvent.recycle();
  }
}
 
Example #2
Source File: UiControllerImplIntegrationTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void injectMotionEventSequence() throws InterruptedException {
  try (ActivityScenario<SendActivity> scenario = ActivityScenario.launch(SendActivity.class)) {
    getInstrumentation().waitForIdleSync();
    final float[][] steps = CoordinatesUtil.getCoordinatesToDrag();

    getInstrumentation()
        .runOnMainSync(
            new Runnable() {
              @Override
              public void run() {
                long downTime = SystemClock.uptimeMillis();
                List<MotionEvent> events = new ArrayList<>();
                try {
                  MotionEvent down =
                      MotionEvents.obtainDownEvent(steps[0], new float[] {16f, 16f});
                  events.add(down);
                  for (int i = 1; i < events.size() - 1; i++) {
                    events.add(
                        MotionEvents.obtainMovement(
                            downTime, SystemClock.uptimeMillis(), steps[i]));
                  }
                  events.add(MotionEvents.obtainUpEvent(down, steps[steps.length - 1]));

                  injectEventWorked.set(uiController.injectMotionEventSequence(events));
                  latch.countDown();
                } catch (InjectEventSecurityException e) {
                  injectEventThrewSecurityException.set(true);
                }
              }
            });

    assertFalse(
        "SecurityException exception was thrown.", injectEventThrewSecurityException.get());
    assertTrue("Timed out!", latch.await(10, TimeUnit.SECONDS));
    assertTrue(injectEventWorked.get());
  }
}
 
Example #3
Source File: ViewActions.java    From Kore with Apache License 2.0 4 votes vote down vote up
public static ViewAction slideSeekBar(final int progress) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return new TypeSafeMatcher<View>() {
                @Override
                protected boolean matchesSafely(View item) {
                    return item instanceof SeekBar;
                }

                @Override
                public void describeTo(Description description) {
                    description.appendText("is a SeekBar.");
                }
            };
        }

        @Override
        public String getDescription() {
            return "Slides seekbar to progress position " + progress;
        }

        @Override
        public void perform(UiController uiController, View view) {
            SeekBar seekBar = (SeekBar) view;

            int[] seekBarPos = {0,0};
            view.getLocationOnScreen(seekBarPos);
            float[] startPos = {seekBarPos[0], seekBarPos[1]};

            MotionEvents.DownResultHolder downResultHolder =
                    MotionEvents.sendDown(uiController, startPos,
                                          Press.PINPOINT.describePrecision());

            while(seekBar.getProgress() < progress) {
                startPos[0]++;
                MotionEvents.sendMovement(uiController, downResultHolder.down, startPos);
                uiController.loopMainThreadForAtLeast(10);
            }

            MotionEvents.sendUp(uiController, downResultHolder.down, startPos);
        }
    };
}