1b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay/* 2b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * Copyright (C) 2015 The Android Open Source Project 3b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * 4b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * Licensed under the Apache License, Version 2.0 (the "License"); 5b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * you may not use this file except in compliance with the License. 6b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * You may obtain a copy of the License at 7b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * 8b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * http://www.apache.org/licenses/LICENSE-2.0 9b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * 10b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * Unless required by applicable law or agreed to in writing, software 11b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * distributed under the License is distributed on an "AS IS" BASIS, 12b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * See the License for the specific language governing permissions and 14b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * limitations under the License. 15b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay */ 16b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay 17b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKaypackage com.android.documentsui.bots; 18b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay 19b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.content.Context; 20b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.support.test.uiautomator.UiDevice; 21b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.support.test.uiautomator.UiObject; 22b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.support.test.uiautomator.UiObjectNotFoundException; 23b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.support.test.uiautomator.UiScrollable; 24b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.support.test.uiautomator.UiSelector; 25b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport android.util.Log; 26b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay 27b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport junit.framework.Assert; 28b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay 29b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport java.util.ArrayList; 30b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport java.util.Arrays; 31b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKayimport java.util.List; 32b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay 33b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay/** 34b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * A test helper class that provides support for controlling and asserting against 35b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay * the roots list drawer. 36b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay */ 37b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKaypublic class RootsListBot extends BaseBot { 38b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay private static final String ROOTS_LIST_ID = "com.android.documentsui:id/roots_list"; 39b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay private static final String TAG = "RootsListBot"; 40b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay 41b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay public RootsListBot(UiDevice device, Context context, int timeout) { 42b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay super(device, context, timeout); 43b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay } 44b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay 45b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay private UiObject findRoot(String label) throws UiObjectNotFoundException { 46b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay final UiSelector rootsList = new UiSelector().resourceId( 47b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay "com.android.documentsui:id/container_roots").childSelector( 48b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay new UiSelector().resourceId(ROOTS_LIST_ID)); 49b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay 50b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay // We might need to expand drawer if not visible 51b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay if (!new UiObject(rootsList).waitForExists(mTimeout)) { 52b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay Log.d(TAG, "Failed to find roots list; trying to expand"); 53b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay final UiSelector hamburger = new UiSelector().resourceId( 54b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay "com.android.documentsui:id/toolbar").childSelector( 55b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay new UiSelector().className("android.widget.ImageButton").clickable(true)); 56b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay new UiObject(hamburger).click(); 57b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay } 58b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay 59b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay // Wait for the first list item to appear 60b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay new UiObject(rootsList.childSelector(new UiSelector())).waitForExists(mTimeout); 61b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay 62b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay // Now scroll around to find our item 63b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay new UiScrollable(rootsList).scrollIntoView(new UiSelector().text(label)); 64b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay return new UiObject(rootsList.childSelector(new UiSelector().text(label))); 65b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay } 66b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay 67b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay public void openRoot(String label) throws UiObjectNotFoundException { 68b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay findRoot(label).click(); 69b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay mDevice.waitForIdle(); 70b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay } 71b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay 72b4129d785c1a14d0499fa45291b14ea356d59ee7Aga Wronska public void assertRootsPresent(String... labels) throws UiObjectNotFoundException { 73b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay List<String> missing = new ArrayList<>(); 74b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay for (String label : labels) { 75b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay if (!findRoot(label).exists()) { 76b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay missing.add(label); 77b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay } 78b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay } 79b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay if (!missing.isEmpty()) { 80b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay Assert.fail( 81b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay "Expected roots " + Arrays.asList(labels) + ", but missing " + missing); 82b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay } 83b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay } 84b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay 85b4129d785c1a14d0499fa45291b14ea356d59ee7Aga Wronska public void assertRootsAbsent(String... labels) throws UiObjectNotFoundException { 86b4129d785c1a14d0499fa45291b14ea356d59ee7Aga Wronska List<String> unexpected = new ArrayList<>(); 87b4129d785c1a14d0499fa45291b14ea356d59ee7Aga Wronska for (String label : labels) { 88b4129d785c1a14d0499fa45291b14ea356d59ee7Aga Wronska if (findRoot(label).exists()) { 89b4129d785c1a14d0499fa45291b14ea356d59ee7Aga Wronska unexpected.add(label); 90b4129d785c1a14d0499fa45291b14ea356d59ee7Aga Wronska } 91b4129d785c1a14d0499fa45291b14ea356d59ee7Aga Wronska } 92b4129d785c1a14d0499fa45291b14ea356d59ee7Aga Wronska if (!unexpected.isEmpty()) { 93b4129d785c1a14d0499fa45291b14ea356d59ee7Aga Wronska Assert.fail("Unexpected roots " + unexpected); 94b4129d785c1a14d0499fa45291b14ea356d59ee7Aga Wronska } 95b4129d785c1a14d0499fa45291b14ea356d59ee7Aga Wronska } 96b4129d785c1a14d0499fa45291b14ea356d59ee7Aga Wronska 97b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay public void assertHasFocus() { 98b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay assertHasFocus(ROOTS_LIST_ID); 99b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay } 100b9a20d10c948cf111b70981e08233ea97323fe6cSteve McKay} 101