Home | History | Annotate | Download | only in bots
      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     public final InspectorBot inspector;
     48 
     49     public Bots(UiDevice device, UiAutomation automation, Context context, int timeout) {
     50         main = new UiBot(device, context, TIMEOUT);
     51         breadcrumb = new BreadBot(device, context, TIMEOUT, main);
     52         roots = new SidebarBot(device, context, TIMEOUT);
     53         directory = new DirectoryListBot(device, automation, context, TIMEOUT);
     54         sortHeader = new SortHeaderBot(device, context, TIMEOUT);
     55         keyboard = new KeyboardBot(device, context, TIMEOUT);
     56         search = new SearchBot(device, context, TIMEOUT);
     57         gesture = new GestureBot(device, automation, context, TIMEOUT);
     58         menu = new MenuBot(device, context, TIMEOUT);
     59         inspector = new InspectorBot(device, context, TIMEOUT);
     60     }
     61 
     62     /**
     63      * A test helper class that provides support for controlling directory list
     64      * and making assertions against the state of it.
     65      */
     66     static abstract class BaseBot {
     67         public final UiDevice mDevice;
     68         final Context mContext;
     69         final int mTimeout;
     70 
     71         BaseBot(UiDevice device, Context context, int timeout) {
     72             mDevice = device;
     73             mContext = context;
     74             mTimeout = timeout;
     75         }
     76 
     77         /**
     78          * Asserts that the specified view or one of its descendents has focus.
     79          */
     80         protected void assertHasFocus(String resourceName) {
     81             UiObject2 candidate = mDevice.findObject(By.res(resourceName));
     82             assertNotNull("Expected " + resourceName + " to have focus, but it didn't.",
     83                 candidate.findObject(By.focused(true)));
     84         }
     85 
     86         protected UiObject2 find(BySelector selector) {
     87             mDevice.wait(Until.findObject(selector), mTimeout);
     88             return mDevice.findObject(selector);
     89         }
     90 
     91         protected UiObject findObject(String resourceId) {
     92             final UiSelector object = new UiSelector().resourceId(resourceId);
     93             return mDevice.findObject(object);
     94         }
     95 
     96         protected UiObject findObject(String parentResourceId, String childResourceId) {
     97             final UiSelector selector = new UiSelector()
     98                     .resourceId(parentResourceId)
     99                     .childSelector(new UiSelector().resourceId(childResourceId));
    100             return mDevice.findObject(selector);
    101         }
    102 
    103         protected void waitForIdle() {
    104             mDevice.waitForIdle(mTimeout);
    105         }
    106     }
    107 
    108 }
    109