1b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay/*
2b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * Copyright (C) 2015 The Android Open Source Project
3b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay *
4b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * Licensed under the Apache License, Version 2.0 (the "License");
5b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * you may not use this file except in compliance with the License.
6b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * You may obtain a copy of the License at
7b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay *
8b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay *      http://www.apache.org/licenses/LICENSE-2.0
9b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay *
10b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * Unless required by applicable law or agreed to in writing, software
11b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * distributed under the License is distributed on an "AS IS" BASIS,
12b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * See the License for the specific language governing permissions and
14b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * limitations under the License.
15b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay */
16b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay
17b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKaypackage com.android.documentsui.bots;
18b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay
19b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport static junit.framework.Assert.assertNotNull;
20b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay
21b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.content.Context;
22b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.support.test.uiautomator.By;
23b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.support.test.uiautomator.BySelector;
24b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.support.test.uiautomator.UiDevice;
25b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.support.test.uiautomator.UiObject;
26b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.support.test.uiautomator.UiObject2;
27b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.support.test.uiautomator.UiSelector;
28b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.support.test.uiautomator.Until;
29b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay
30b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay/**
31b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * A test helper class that provides support for controlling directory list
32b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * and making assertions against the state of it.
33b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay */
34b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayabstract class BaseBot {
35b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    final UiDevice mDevice;
36b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    final Context mContext;
37b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    final int mTimeout;
38b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay
39b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    BaseBot(UiDevice device, Context context, int timeout) {
40b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay        mDevice = device;
41b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay        mContext = context;
42b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay        mTimeout = timeout;
43b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    }
44b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay
45b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    /**
46b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay     * Asserts that the specified view or one of its descendents has focus.
47b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay     */
48b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    protected void assertHasFocus(String resourceName) {
49b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay        UiObject2 candidate = mDevice.findObject(By.res(resourceName));
50b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay        assertNotNull("Expected " + resourceName + " to have focus, but it didn't.",
51b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay            candidate.findObject(By.focused(true)));
52b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    }
53b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay
54b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    protected UiObject2 find(BySelector selector) {
55b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay        mDevice.wait(Until.findObject(selector), mTimeout);
56b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay        return mDevice.findObject(selector);
57b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    }
58b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay
59b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    protected UiObject findObject(String resourceId) {
60b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay        final UiSelector object = new UiSelector().resourceId(resourceId);
61b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay        return mDevice.findObject(object);
62b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    }
63b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay
64b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    protected UiObject findObject(String parentResourceId, String childResourceId) {
65b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay        final UiSelector selector = new UiSelector()
66b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay                .resourceId(parentResourceId)
67b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay                .childSelector(new UiSelector().resourceId(childResourceId));
68b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay        return mDevice.findObject(selector);
69b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    }
70b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay
71b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    protected void waitForIdle() {
72b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay        mDevice.waitForIdle(mTimeout);
73b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay    }
74b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay}
75