1/* 2 * Copyright (C) 2015 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 17package com.android.documentsui.bots; 18 19import android.content.Context; 20import android.support.test.uiautomator.UiDevice; 21import android.support.test.uiautomator.UiObject; 22import android.support.test.uiautomator.UiObjectNotFoundException; 23import android.support.test.uiautomator.UiScrollable; 24import android.support.test.uiautomator.UiSelector; 25import android.util.Log; 26 27import junit.framework.Assert; 28 29import java.util.ArrayList; 30import java.util.Arrays; 31import java.util.List; 32 33/** 34 * A test helper class that provides support for controlling and asserting against 35 * the roots list drawer. 36 */ 37public class RootsListBot extends BaseBot { 38 private static final String ROOTS_LIST_ID = "com.android.documentsui:id/roots_list"; 39 private static final String TAG = "RootsListBot"; 40 41 public RootsListBot(UiDevice device, Context context, int timeout) { 42 super(device, context, timeout); 43 } 44 45 private UiObject findRoot(String label) throws UiObjectNotFoundException { 46 final UiSelector rootsList = new UiSelector().resourceId( 47 "com.android.documentsui:id/container_roots").childSelector( 48 new UiSelector().resourceId(ROOTS_LIST_ID)); 49 50 // We might need to expand drawer if not visible 51 if (!new UiObject(rootsList).waitForExists(mTimeout)) { 52 Log.d(TAG, "Failed to find roots list; trying to expand"); 53 final UiSelector hamburger = new UiSelector().resourceId( 54 "com.android.documentsui:id/toolbar").childSelector( 55 new UiSelector().className("android.widget.ImageButton").clickable(true)); 56 new UiObject(hamburger).click(); 57 } 58 59 // Wait for the first list item to appear 60 new UiObject(rootsList.childSelector(new UiSelector())).waitForExists(mTimeout); 61 62 // Now scroll around to find our item 63 new UiScrollable(rootsList).scrollIntoView(new UiSelector().text(label)); 64 return new UiObject(rootsList.childSelector(new UiSelector().text(label))); 65 } 66 67 public void openRoot(String label) throws UiObjectNotFoundException { 68 findRoot(label).click(); 69 mDevice.waitForIdle(); 70 } 71 72 public void assertRootsPresent(String... labels) throws UiObjectNotFoundException { 73 List<String> missing = new ArrayList<>(); 74 for (String label : labels) { 75 if (!findRoot(label).exists()) { 76 missing.add(label); 77 } 78 } 79 if (!missing.isEmpty()) { 80 Assert.fail( 81 "Expected roots " + Arrays.asList(labels) + ", but missing " + missing); 82 } 83 } 84 85 public void assertRootsAbsent(String... labels) throws UiObjectNotFoundException { 86 List<String> unexpected = new ArrayList<>(); 87 for (String label : labels) { 88 if (findRoot(label).exists()) { 89 unexpected.add(label); 90 } 91 } 92 if (!unexpected.isEmpty()) { 93 Assert.fail("Unexpected roots " + unexpected); 94 } 95 } 96 97 public void assertHasFocus() { 98 assertHasFocus(ROOTS_LIST_ID); 99 } 100} 101