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