public final class

PickerActions

extends java.lang.Object

 java.lang.Object

↳androidx.test.espresso.contrib.PickerActions

Gradle dependencies

compile group: 'androidx.test.espresso', name: 'espresso-contrib', version: '3.6.1'

  • groupId: androidx.test.espresso
  • artifactId: espresso-contrib
  • version: 3.6.1

Artifact androidx.test.espresso:espresso-contrib:3.6.1 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test.espresso:espresso-contrib com.android.support.test.espresso:espresso-contrib

Androidx class mapping:

androidx.test.espresso.contrib.PickerActions android.support.test.espresso.contrib.PickerActions

Overview

Espresso action for interacting with and .

See Pickers API guide

Summary

Methods
public static ViewActionsetDate(int year, int monthOfYear, int dayOfMonth)

Returns a ViewAction that sets a date on a .

public static ViewActionsetTime(int hours, int minutes)

Returns a ViewAction that sets a time on a .

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static ViewAction setDate(int year, int monthOfYear, int dayOfMonth)

Returns a ViewAction that sets a date on a .

Parameters:

year: The year.
monthOfYear: The month which is starting from zero.
dayOfMonth: The day of the month.

public static ViewAction setTime(int hours, int minutes)

Returns a ViewAction that sets a time on a .

Parameters:

hours: the hour to set, in the range (0-23).
minutes: the minute to set, in the range (0-59).

Source

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.test.espresso.contrib;

import static androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static org.hamcrest.Matchers.allOf;

import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import org.hamcrest.Matcher;

/**
 * Espresso action for interacting with {@link DatePicker} and {@link TimePicker}.
 *
 * <p>See <a href="http://developer.android.com/guide/topics/ui/controls/pickers.html">Pickers API
 * guide</a>
 */
public final class PickerActions {

  private PickerActions() {
    // no Instance
  }

  /**
   * Returns a {@link ViewAction} that sets a date on a {@link DatePicker}.
   *
   * @param year The year.
   * @param monthOfYear The month which is starting from zero.
   * @param dayOfMonth The day of the month.
   */
  public static ViewAction setDate(final int year, final int monthOfYear, final int dayOfMonth) {

    // monthOfYear which starts with zero in DatePicker widget.
    final int normalizedMonthOfYear = monthOfYear - 1;

    return new ViewAction() {

      @Override
      public void perform(UiController uiController, View view) {
        final DatePicker datePicker = (DatePicker) view;
        datePicker.updateDate(year, normalizedMonthOfYear, dayOfMonth);
      }

      @Override
      public String getDescription() {
        return "set date";
      }

      @Override
      public Matcher<View> getConstraints() {
        return allOf(isAssignableFrom(DatePicker.class), isDisplayed());
      }
    };
  }

  /**
   * Returns a {@link ViewAction} that sets a time on a {@link TimePicker}.
   *
   * @param hours the hour to set, in the range (0-23).
   * @param minutes the minute to set, in the range (0-59).
   */
  public static ViewAction setTime(final int hours, final int minutes) {

    return new ViewAction() {

      @Override
      public void perform(UiController uiController, View view) {
        final TimePicker timePicker = (TimePicker) view;
        timePicker.setCurrentHour(hours);
        timePicker.setCurrentMinute(minutes);
      }

      @Override
      public String getDescription() {
        return "set time";
      }

      @Override
      public Matcher<View> getConstraints() {
        return allOf(isAssignableFrom(TimePicker.class), isDisplayed());
      }
    };
  }
}