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 import androidx.test.InstrumentationRegistry; 32 33 /** 34 * Handy collection of bots for working with Files app. 35 */ 36 public final class Bots { 37 38 private static final int TIMEOUT = 15000; 39 40 public final BreadBot breadcrumb; 41 public final DirectoryListBot directory; 42 public final SortBot sort; 43 public final KeyboardBot keyboard; 44 public final SidebarBot roots; 45 public final SearchBot search; 46 public final GestureBot gesture; 47 public final MenuBot menu; 48 public final UiBot main; 49 public final InspectorBot inspector; 50 public final NotificationsBot notifications; 51 Bots(UiDevice device, UiAutomation automation, Context context, int timeout)52 public Bots(UiDevice device, UiAutomation automation, Context context, int timeout) { 53 main = new UiBot(device, context, TIMEOUT); 54 breadcrumb = new BreadBot(device, context, TIMEOUT); 55 roots = new SidebarBot(device, context, TIMEOUT); 56 directory = new DirectoryListBot(device, automation, context, TIMEOUT); 57 sort = new SortBot(device, context, TIMEOUT, main); 58 keyboard = new KeyboardBot(device, context, TIMEOUT); 59 search = new SearchBot(device, context, TIMEOUT); 60 gesture = new GestureBot(device, automation, context, TIMEOUT); 61 menu = new MenuBot(device, context, TIMEOUT); 62 inspector = new InspectorBot(device, context, TIMEOUT); 63 notifications = new NotificationsBot(device, context, TIMEOUT); 64 } 65 66 /** 67 * A test helper class that provides support for controlling directory list 68 * and making assertions against the state of it. 69 */ 70 static abstract class BaseBot { 71 public final UiDevice mDevice; 72 final Context mContext; 73 final int mTimeout; 74 public final String mTargetPackage; 75 BaseBot(UiDevice device, Context context, int timeout)76 BaseBot(UiDevice device, Context context, int timeout) { 77 mDevice = device; 78 mContext = context; 79 mTimeout = timeout; 80 mTargetPackage = 81 InstrumentationRegistry.getInstrumentation() 82 .getTargetContext().getPackageName(); 83 } 84 85 /** 86 * Asserts that the specified view or one of its descendents has focus. 87 */ assertHasFocus(String resourceName)88 protected void assertHasFocus(String resourceName) { 89 UiObject2 candidate = mDevice.findObject(By.res(resourceName)); 90 assertNotNull("Expected " + resourceName + " to have focus, but it didn't.", 91 candidate.findObject(By.focused(true))); 92 } 93 find(BySelector selector)94 protected UiObject2 find(BySelector selector) { 95 mDevice.wait(Until.findObject(selector), mTimeout); 96 return mDevice.findObject(selector); 97 } 98 findObject(String resourceId)99 protected UiObject findObject(String resourceId) { 100 final UiSelector object = new UiSelector().resourceId(resourceId); 101 return mDevice.findObject(object); 102 } 103 findObject(String parentResourceId, String childResourceId)104 protected UiObject findObject(String parentResourceId, String childResourceId) { 105 final UiSelector selector = new UiSelector() 106 .resourceId(parentResourceId) 107 .childSelector(new UiSelector().resourceId(childResourceId)); 108 return mDevice.findObject(selector); 109 } 110 waitForIdle()111 protected void waitForIdle() { 112 mDevice.waitForIdle(mTimeout); 113 } 114 } 115 116 } 117