blob: b0a4d76f43686a55e0bbde790ce605399db6bc9e [file] [log] [blame]
Steve McKayb9a20d12016-02-19 12:57:05 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui.bots;
18
Ben Linda75c242016-05-11 17:38:18 -070019import static android.support.test.espresso.Espresso.onView;
20import static android.support.test.espresso.action.ViewActions.pressImeActionButton;
21import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
22
Steve McKayb9a20d12016-02-19 12:57:05 -080023import android.content.Context;
24import android.support.test.uiautomator.UiDevice;
25import android.view.inputmethod.InputMethodManager;
Ben Linda75c242016-05-11 17:38:18 -070026import android.widget.EditText;
Steve McKayb9a20d12016-02-19 12:57:05 -080027
28/**
29 * A test helper class that provides support for keyboard manipulation.
30 */
31public class KeyboardBot extends BaseBot {
32
33 public KeyboardBot(UiDevice device, Context context, int timeout) {
34 super(device, context, timeout);
35 }
36
37 public void dismissKeyboardIfPresent() {
38 if(isKeyboardPresent()) {
39 mDevice.pressBack();
40 }
41 }
42
43 // Indirect way to detect the keyboard.
44 private boolean isKeyboardPresent() {
45 InputMethodManager inputManager = (InputMethodManager) mContext
46 .getSystemService(Context.INPUT_METHOD_SERVICE);
47 return inputManager.isAcceptingText();
48 }
49
50 public void pressEnter() {
Ben Linda75c242016-05-11 17:38:18 -070051 //TODO: There seems to be a bug on N/Espresso that makes pressing Enter not work
52 // This is a temporary workaround that somehow works
53 // See b/28399576
54 onView(isAssignableFrom(EditText.class)).perform(pressImeActionButton());
Steve McKayb9a20d12016-02-19 12:57:05 -080055 }
56}