androidx.test.espresso.action.ViewActions Java Examples

The following examples show how to use androidx.test.espresso.action.ViewActions. 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: EspressoTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
/**
 * for this test to be useful, hardware keyboard must be disabled. Thus, soft keyboard must be
 * present.
 */
@Test
public void closeSoftKeyboardRetry() {
  onData(allOf(instanceOf(Map.class), hasValue(KeyboardTestActivity.class.getSimpleName())))
      .perform(click());
  // click on the edit text which bring the soft keyboard up
  onView(withId(R.id.editTextUserInput))
      .perform(typeText("Espresso"), ViewActions.closeSoftKeyboard());
  // the soft keyboard should be dismissed to expose the button
  onView(withId(R.id.changeTextBt)).perform(click());

  // repeat, to make sure the retry mechanism still works for a subsequent ViewAction.
  onView(withId(R.id.editTextUserInput))
      .perform(typeText(", just works!"), ViewActions.closeSoftKeyboard());
  onView(withId(R.id.changeTextBt)).perform(click());
}
 
Example #2
Source File: PlaybackControlTest.java    From CastVideos-android with Apache License 2.0 6 votes vote down vote up
/**
 * Click on Closed Caption button and select language from the subtitles dialog
 * Verify the Closed Caption button is clickable
 * Verify the active track exists
 */
@Test
public void testCaptionControl() throws InterruptedException, UiObjectNotFoundException {
    mTestUtils.connectToCastDevice();
    mTestUtils.playCastContent(VIDEO_WITH_SUBTITLES);

    mTestUtils.assertCaption(false);

    // Click CC button and open SUBTITLES dialog
    mDevice.findObject(new UiSelector().description("Closed captions")).click();
    assertTrue(mDevice.findObject(new UiSelector().textMatches("SUBTITLES")).exists());

    // Select subtitle and close SUBTITLES dialog
    mDevice.findObject(new UiSelector().text("English Subtitle")
            .fromParent(new UiSelector().resourceId(resources.getResourceName(R.id.radio)))).click();
    mDevice.findObject(new UiSelector().text("OK")).click();
    assertFalse(mDevice.findObject(new UiSelector().textMatches("SUBTITLES")).exists());

    // Assert subtitle is activated
    mTestUtils.assertCaption(true);

    onView(isRoot()).perform(ViewActions.pressBack());

    mTestUtils.disconnectFromCastDevice();
}
 
Example #3
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Test
@MediumTest
public void testSwipeDownToHide() {
  Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
      .perform(
          DesignViewActions.withCustomConstraints(
              ViewActions.swipeDown(), ViewMatchers.isDisplayingAtLeast(5)));
  registerIdlingResourceCallback();
  try {
    Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
        .check(ViewAssertions.matches(not(ViewMatchers.isDisplayed())));
    assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_HIDDEN));
  } finally {
    unregisterIdlingResourceCallback();
  }
}
 
Example #4
Source File: AuthenticatorActivityTest.java    From google-authenticator-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testContextMenuRename() {
  accountDb.add(HOTP_ACCOUNT_NAME, "7777777777777777", OtpType.HOTP, null, null, "Yahoo");

  activityTestRule.launchActivity(null);

  ListView userList = activityTestRule.getActivity().findViewById(R.id.user_list);
  openContextualActionBarOrMenuAndInvokeItem(userList, 0, R.string.rename);
  // Type new name and select save on dialog.
  onView(withId(R.id.rename_edittext))
      .check(matches(withText(HOTP_ACCOUNT_NAME)))
      .perform(ViewActions.clearText())
      .perform(typeText("[email protected]"));
  onView(withText(R.string.submit)).perform(click());
  // check main screen gets focus back;
  onView(is(userList)).check(matches(hasFocus()));
  // check update to database.
  assertThat(accountDb.getAccounts())
      .containsExactly(new AccountIndex("[email protected]", "Yahoo"))
      .inOrder();
}
 
Example #5
Source File: InteractionRequestTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void createInteractionResponse_NoRootMatcher_ThrowsISE() {
  try {
    new InteractionRequest.Builder()
        .setViewMatcher(withIdMatcher)
        .setViewAction(ViewActions.click())
        .setViewAssertion(viewAssertion)
        .build();
    fail("IllegalStateException expected!");
  } catch (IllegalStateException ise) {
    // expected
  }
}
 
Example #6
Source File: EspressoTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void closeSoftKeyboard() {
  onData(allOf(instanceOf(Map.class), hasValue(SendActivity.class.getSimpleName())))
      .perform(click());

  onView(withId(R.id.enter_data_edit_text))
      .perform(
          new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
              return any(View.class);
            }

            @Override
            public void perform(UiController uiController, View view) {
              // This doesn't do anything if hardware keyboard is present - that is, soft keyboard
              // is _not_ present. Whether it's present or not can be verified under the following
              // device settings: Settings > Language & Input > Under Keyboard and input method
              InputMethodManager imm =
                  (InputMethodManager)
                      getInstrumentation()
                          .getTargetContext()
                          .getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.showSoftInput(view, 0);
              uiController.loopMainThreadUntilIdle();
            }

            @Override
            public String getDescription() {
              return "show soft input";
            }
          });

  onView(withId(R.id.enter_data_edit_text)).perform(ViewActions.closeSoftKeyboard());
}
 
Example #7
Source File: AccessibilityChecks.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Enables accessibility checking as a global ViewAssertion in {@link ViewActions}.
 *
 * @return the backing {@link AccessibilityValidator}, on which options for check execution can be
 *     set
 */
public static AccessibilityValidator enable() {
  if (checksEnabled) {
    Log.w(TAG, "Accessibility checks already enabled.");
  } else {
    checksEnabled = true;
    ViewActions.addGlobalAssertion("Accessibility Checks", ACCESSIBILITY_CHECK_ASSERTION);
  }
  return CHECK_EXECUTOR;
}
 
Example #8
Source File: AccessibilityChecks.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Disables accessibility checking as a global ViewAssertion in {@link ViewActions}.
 *
 * @throws IllegalStateException if accessibility checking in not enabled.
 */
public static void disable() {
  if (!checksEnabled) {
    throw new IllegalStateException("Accessibility checks not enabled!");
  }
  checksEnabled = false;
  ViewActions.removeGlobalAssertion(ACCESSIBILITY_CHECK_ASSERTION);
}
 
Example #9
Source File: AccessibilityChecks.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Enables accessibility checking as a global ViewAssertion in {@link ViewActions}.
 *
 * @return the backing {@link AccessibilityValidator}, on which options for check execution can be
 *     set
 */
public static AccessibilityValidator enable() {
  if (checksEnabled) {
    Log.w(TAG, "Accessibility checks already enabled.");
  } else {
    checksEnabled = true;
    ViewActions.addGlobalAssertion("Accessibility Checks", ACCESSIBILITY_CHECK_ASSERTION);
  }
  return CHECK_EXECUTOR;
}
 
Example #10
Source File: DefaultProcessActivityTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
private void launchRemoteActivityAndVerify_PressBack_VerifyOnMainActivity() {
  Log.d(TAG, "Starting activity in a secondary process...");
  onView(withId(R.id.startActivityBtn)).perform(click());

  Log.d(TAG, "Checking private process name...");
  onView(withId(R.id.textPrivateProcessName))
      .check(matches(withText(is(DEFAULT_PROC_NAME + ":PID2"))));

  Log.d(TAG, "Pressing back to go back to the main activity...");
  onView(isRoot()).perform(ViewActions.pressBackUnconditionally());

  Log.d(TAG, "Checking main process name again...");
  onView(withId(R.id.textNamedProcess)).check(matches(withText(is(DEFAULT_PROC_NAME))));
}
 
Example #11
Source File: InteractionRequestTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Before
public void createTargetTypes() {
  remoteDescriptorRegistry = RemoteDescriptorRegistry.getInstance();
  remoteDescriptorRegistry.clear();
  RemoteDescriptorRegistryInitializer.init(remoteDescriptorRegistry);
  RemoteHamcrestCoreMatchers13.init(remoteDescriptorRegistry);
  RemoteViewMatchers.init(remoteDescriptorRegistry);
  RemoteViewActions.init(remoteDescriptorRegistry);
  RemoteViewAssertions.init(remoteDescriptorRegistry);
  rootMatcher = new StubRootMatcher();
  withIdMatcher = withId(123);
  viewAction = ViewActions.click();
  viewAssertion = matches(withIdMatcher);
}
 
Example #12
Source File: InteractionRequestTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void createInteractionRequest_WithViewMatcherViewActionAndResultProto_ThrowsISE() {
  try {
    new InteractionRequest.Builder()
        .setRootMatcher(rootMatcher)
        .setViewMatcher(withIdMatcher)
        .setViewAction(ViewActions.click())
        .setViewAssertion(viewAssertion)
        .setRequestProto(new byte[256])
        .build();
    fail("IllegalStateException expected!");
  } catch (IllegalStateException ise) {
    // expected
  }
}
 
Example #13
Source File: BottomSheetBehaviorTouchTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testTouchCoordinatorLayout() {
  final CoordinatorLayoutActivity activity = activityTestRule.getActivity();
  down = false;
  Espresso.onView(sameInstance((View) activity.mCoordinatorLayout))
      .perform(ViewActions.click()) // Click outside the bottom sheet
      .check(
          (view, e) -> {
            assertThat(e, is(nullValue()));
            assertThat(view, is(notNullValue()));
            // Check that the touch event fell through to the container
            assertThat(down, is(true));
          });
}
 
Example #14
Source File: ConvalidaRobot.java    From convalida with Apache License 2.0 5 votes vote down vote up
public ConvalidaRobot typeText(String text) {
    onView(withId(_fieldResId))
            .perform(ViewActions.typeText(text))
            .perform(closeSoftKeyboard());

    return _robot;
}
 
Example #15
Source File: MultiSliderActions.java    From MultiSlider with Apache License 2.0 4 votes vote down vote up
public static ViewAction setThumbValue(int thumbId, int value) {
    return ViewActions.actionWithAssertions(new SetThumbValueAction(thumbId, value));
}
 
Example #16
Source File: MultiSliderActions.java    From MultiSlider with Apache License 2.0 4 votes vote down vote up
public static ViewAction moveThumbForward(int thumbId) {
    return ViewActions.actionWithAssertions(new SetThumbValueAction(thumbId, Integer.MAX_VALUE));
}
 
Example #17
Source File: MultiSliderActions.java    From MultiSlider with Apache License 2.0 4 votes vote down vote up
public static ViewAction moveThumbBackward(int thumbId) {
    return ViewActions.actionWithAssertions(new SetThumbValueAction(thumbId, Integer.MIN_VALUE));
}
 
Example #18
Source File: Espresso.java    From android-test with Apache License 2.0 4 votes vote down vote up
/** Closes soft keyboard if open. */
public static void closeSoftKeyboard() {
  onView(isRoot()).perform(ViewActions.closeSoftKeyboard());
}
 
Example #19
Source File: LandingScreenInteractor.java    From edx-app-android with Apache License 2.0 4 votes vote down vote up
public LogInScreenInteractor navigateToLogInScreen() {
    onLogInView().perform(ViewActions.click());
    return new LogInScreenInteractor();
}
 
Example #20
Source File: LandingScreenInteractor.java    From edx-app-android with Apache License 2.0 4 votes vote down vote up
public RegistrationScreenInteractor navigateToRegistrationScreen() {
    onRegistrationView().perform(ViewActions.click());
    return new RegistrationScreenInteractor();
}
 
Example #21
Source File: AppendTextAction.java    From Emoji with Apache License 2.0 4 votes vote down vote up
static ViewAction append(final String text) {
  return ViewActions.actionWithAssertions(new AppendTextAction(text));
}
 
Example #22
Source File: PlaceAutocompleteFragmentTest.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void onScrollChanged_showsDropShadowWhenVerticalNotZero() throws Exception {
  onView(withId(R.id.scroll_view_results)).perform(ViewActions.swipeUp());
  onView(withId(R.id.scroll_drop_shadow)).check(matches(isDisplayed()));
}
 
Example #23
Source File: Espresso.java    From android-test with Apache License 2.0 3 votes vote down vote up
/**
 * Opens the overflow menu displayed in the contextual options of an ActionMode.
 *
 * <p>This works with both native and SherlockActionBar action modes.
 *
 * <p>Note the significant difference in UX between ActionMode and ActionBar overflows -
 * ActionMode will always present an overflow icon and that icon only responds to clicks. The menu
 * button (if present) has no impact on it.
 */
@SuppressWarnings("unchecked")
public static void openContextualActionModeOverflowMenu() {
  onView(isRoot()).perform(new TransitionBridgingViewAction());

  // provide an pressBack rollback action to the click, to handle occasional flakiness where the
  // click is interpreted as a long press
  onView(OVERFLOW_BUTTON_MATCHER).perform(click(ViewActions.pressBack()));
}
 
Example #24
Source File: Espresso.java    From android-test with Apache License 2.0 2 votes vote down vote up
/**
 * Press on the back button.
 *
 * @throws PerformException if currently displayed activity is root activity, since pressing back
 *     button would result in application closing.
 */
public static void pressBack() {
  onView(isRoot()).perform(ViewActions.pressBack());
}
 
Example #25
Source File: Espresso.java    From android-test with Apache License 2.0 2 votes vote down vote up
/**
 * Similar to {@link #pressBack()} but will <b>not</b> throw an exception when Espresso navigates
 * outside the application or process under test.
 */
public static void pressBackUnconditionally() {
  onView(isRoot()).perform(ViewActions.pressBackUnconditionally());
}
 
Example #26
Source File: DescendantViewActions.java    From EspressoDescendantActions with Apache License 2.0 2 votes vote down vote up
/**
 * Perform a check against a descendant view where the root only allows actions such
 * a view found by RecyclerView actions
 *
 * @param viewMatcher   used to select the descendant view in the hierarchy
 * @param viewAssertion the assertion to check
 * @return A ViewAction to be performed on the parent view
 */
public static ViewAction checkDescendantViewAction(Matcher<View> viewMatcher, ViewAssertion viewAssertion) {
    return ViewActions.actionWithAssertions(new CheckDescendantViewAction(viewMatcher, viewAssertion));
}
 
Example #27
Source File: DescendantViewActions.java    From EspressoDescendantActions with Apache License 2.0 2 votes vote down vote up
/**
 * Perform a check against a view that only allows actions such as a view found by
 * RecyclerViewActions
 *
 * @param viewAssertion the assertion to check.
 * @return The ViewAction to perform
 */
public static ViewAction checkViewAction(ViewAssertion viewAssertion) {
    return ViewActions.actionWithAssertions(new CheckAssertionAction(viewAssertion));
}
 
Example #28
Source File: DescendantViewActions.java    From EspressoDescendantActions with Apache License 2.0 2 votes vote down vote up
/**
 * Perform an action on a descendant view.
 *
 * @param viewMatcher used to select the descendant view in the hierarchy
 * @param viewAction the action to perform against the descendant view
 * @return A ViewAction object that should be performed on the parent view
 */
public static ViewAction performDescendantAction(Matcher<View> viewMatcher, ViewAction viewAction) {
    return ViewActions.actionWithAssertions(new DescendantViewAction(viewMatcher, viewAction));
}