1/*
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
19import static junit.framework.Assert.assertNotNull;
20
21import android.content.Context;
22import android.support.test.uiautomator.By;
23import android.support.test.uiautomator.BySelector;
24import android.support.test.uiautomator.UiDevice;
25import android.support.test.uiautomator.UiObject;
26import android.support.test.uiautomator.UiObject2;
27import android.support.test.uiautomator.UiSelector;
28import android.support.test.uiautomator.Until;
29
30/**
31 * A test helper class that provides support for controlling directory list
32 * and making assertions against the state of it.
33 */
34abstract class BaseBot {
35    final UiDevice mDevice;
36    final Context mContext;
37    final int mTimeout;
38
39    BaseBot(UiDevice device, Context context, int timeout) {
40        mDevice = device;
41        mContext = context;
42        mTimeout = timeout;
43    }
44
45    /**
46     * Asserts that the specified view or one of its descendents has focus.
47     */
48    protected void assertHasFocus(String resourceName) {
49        UiObject2 candidate = mDevice.findObject(By.res(resourceName));
50        assertNotNull("Expected " + resourceName + " to have focus, but it didn't.",
51            candidate.findObject(By.focused(true)));
52    }
53
54    protected UiObject2 find(BySelector selector) {
55        mDevice.wait(Until.findObject(selector), mTimeout);
56        return mDevice.findObject(selector);
57    }
58
59    protected UiObject findObject(String resourceId) {
60        final UiSelector object = new UiSelector().resourceId(resourceId);
61        return mDevice.findObject(object);
62    }
63
64    protected UiObject findObject(String parentResourceId, String childResourceId) {
65        final UiSelector selector = new UiSelector()
66                .resourceId(parentResourceId)
67                .childSelector(new UiSelector().resourceId(childResourceId));
68        return mDevice.findObject(selector);
69    }
70
71    protected void waitForIdle() {
72        mDevice.waitForIdle(mTimeout);
73    }
74}
75