1 /* 2 * Copyright (C) 2016 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 17 package com.android.documentsui.bots; 18 19 import static junit.framework.Assert.assertNotNull; 20 21 import android.app.UiAutomation; 22 import android.content.Context; 23 import android.support.test.uiautomator.By; 24 import android.support.test.uiautomator.BySelector; 25 import android.support.test.uiautomator.UiDevice; 26 import android.support.test.uiautomator.UiObject; 27 import android.support.test.uiautomator.UiObject2; 28 import android.support.test.uiautomator.UiSelector; 29 import android.support.test.uiautomator.Until; 30 31 /** 32 * Handy collection of bots for working with Files app. 33 */ 34 public final class Bots { 35 36 private static final int TIMEOUT = 5000; 37 38 public final BreadBot breadcrumb; 39 public final DirectoryListBot directory; 40 public final SortHeaderBot sortHeader; 41 public final KeyboardBot keyboard; 42 public final SidebarBot roots; 43 public final SearchBot search; 44 public final GestureBot gesture; 45 public final MenuBot menu; 46 public final UiBot main; 47 48 public Bots(UiDevice device, UiAutomation automation, Context context, int timeout) { 49 main = new UiBot(device, context, TIMEOUT); 50 breadcrumb = new BreadBot(device, context, TIMEOUT, main); 51 roots = new SidebarBot(device, context, TIMEOUT); 52 directory = new DirectoryListBot(device, automation, context, TIMEOUT); 53 sortHeader = new SortHeaderBot(device, context, TIMEOUT); 54 keyboard = new KeyboardBot(device, context, TIMEOUT); 55 search = new SearchBot(device, context, TIMEOUT); 56 gesture = new GestureBot(device, automation, context, TIMEOUT); 57 menu = new MenuBot(device, context, TIMEOUT); 58 } 59 60 /** 61 * A test helper class that provides support for controlling directory list 62 * and making assertions against the state of it. 63 */ 64 static abstract class BaseBot { 65 public final UiDevice mDevice; 66 final Context mContext; 67 final int mTimeout; 68 69 BaseBot(UiDevice device, Context context, int timeout) { 70 mDevice = device; 71 mContext = context; 72 mTimeout = timeout; 73 } 74 75 /** 76 * Asserts that the specified view or one of its descendents has focus. 77 */ 78 protected void assertHasFocus(String resourceName) { 79 UiObject2 candidate = mDevice.findObject(By.res(resourceName)); 80 assertNotNull("Expected " + resourceName + " to have focus, but it didn't.", 81 candidate.findObject(By.focused(true))); 82 } 83 84 protected UiObject2 find(BySelector selector) { 85 mDevice.wait(Until.findObject(selector), mTimeout); 86 return mDevice.findObject(selector); 87 } 88 89 protected UiObject findObject(String resourceId) { 90 final UiSelector object = new UiSelector().resourceId(resourceId); 91 return mDevice.findObject(object); 92 } 93 94 protected UiObject findObject(String parentResourceId, String childResourceId) { 95 final UiSelector selector = new UiSelector() 96 .resourceId(parentResourceId) 97 .childSelector(new UiSelector().resourceId(childResourceId)); 98 return mDevice.findObject(selector); 99 } 100 101 protected void waitForIdle() { 102 mDevice.waitForIdle(mTimeout); 103 } 104 } 105 106 } 107