public final class

EspressoKey

extends java.lang.Object

 java.lang.Object

↳androidx.test.espresso.action.EspressoKey

Gradle dependencies

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

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

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

Androidx artifact mapping:

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

Androidx class mapping:

androidx.test.espresso.action.EspressoKey android.support.test.espresso.action.EspressoKey

Overview

Class that wraps the key code and meta state of the desired key press.

Summary

Methods
public intgetKeyCode()

public intgetMetaState()

public java.lang.StringtoString()

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

Methods

public int getKeyCode()

public int getMetaState()

public java.lang.String toString()

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.action;

import static androidx.test.internal.util.Checks.checkState;

import android.view.KeyEvent;
import androidx.test.espresso.remote.annotation.RemoteMsgConstructor;
import androidx.test.espresso.remote.annotation.RemoteMsgField;
import java.util.Locale;

/** Class that wraps the key code and meta state of the desired key press. */
public final class EspressoKey {
  @RemoteMsgField(order = 0)
  private final int keyCode;

  @RemoteMsgField(order = 1)
  private final int metaState;

  private EspressoKey(Builder builder) {
    this(builder.builderKeyCode, builder.getMetaState());
  }

  @RemoteMsgConstructor
  EspressoKey(int keyCode, int metaState) {
    this.keyCode = keyCode;
    this.metaState = metaState;
  }

  public int getKeyCode() {
    return keyCode;
  }

  public int getMetaState() {
    return metaState;
  }

  @Override
  public String toString() {
    return String.format(Locale.ROOT, "keyCode: %s, metaState: %s", keyCode, metaState);
  }

  /** Builder for the EspressoKey class. */
  public static class Builder {
    private int builderKeyCode = -1;
    private boolean isShiftPressed;
    private boolean isAltPressed;
    private boolean isCtrlPressed;

    public Builder withKeyCode(int keyCode) {
      builderKeyCode = keyCode;
      return this;
    }

    /** Sets the SHIFT_ON meta state of the resulting key. */
    public Builder withShiftPressed(boolean shiftPressed) {
      isShiftPressed = shiftPressed;
      return this;
    }

    /** Sets the CTRL_ON meta state of the resulting key. */
    public Builder withCtrlPressed(boolean ctrlPressed) {
      isCtrlPressed = ctrlPressed;
      return this;
    }

    /** Sets the ALT_ON meta state of the resulting key. */
    public Builder withAltPressed(boolean altPressed) {
      isAltPressed = altPressed;
      return this;
    }

    private int getMetaState() {
      int metaState = 0;
      if (isShiftPressed) {
        metaState |= KeyEvent.META_SHIFT_ON;
      }

      if (isAltPressed) {
        metaState |= KeyEvent.META_ALT_ON;
      }

      if (isCtrlPressed) {
        metaState |= KeyEvent.META_CTRL_ON;
      }

      return metaState;
    }

    public EspressoKey build() {
      checkState(
          builderKeyCode > 0 && builderKeyCode < KeyEvent.getMaxKeyCode(),
          "Invalid key code: %s",
          builderKeyCode);
      return new EspressoKey(this);
    }
  }
}