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 17 package com.android.documentsui.bots; 18 19 import static androidx.test.espresso.Espresso.onView; 20 import static androidx.test.espresso.action.ViewActions.swipeLeft; 21 import static androidx.test.espresso.action.ViewActions.swipeRight; 22 import static androidx.test.espresso.matcher.ViewMatchers.withId; 23 24 import android.content.Context; 25 import android.support.test.uiautomator.UiDevice; 26 import android.support.test.uiautomator.UiObject; 27 import android.support.test.uiautomator.UiObjectNotFoundException; 28 import android.support.test.uiautomator.UiScrollable; 29 import android.support.test.uiautomator.UiSelector; 30 import android.util.Log; 31 import android.view.View; 32 33 import com.android.documentsui.R; 34 35 import junit.framework.Assert; 36 37 import java.util.ArrayList; 38 import java.util.Arrays; 39 import java.util.List; 40 41 /** 42 * A test helper class that provides support for controlling and asserting against 43 * the roots list drawer. 44 */ 45 public class SidebarBot extends Bots.BaseBot { 46 private static final String TAG = "RootsListBot"; 47 48 private final String mRootListId; 49 SidebarBot(UiDevice device, Context context, int timeout)50 public SidebarBot(UiDevice device, Context context, int timeout) { 51 super(device, context, timeout); 52 mRootListId = mTargetPackage + ":id/roots_list"; 53 } 54 findRoot(String label)55 private UiObject findRoot(String label) throws UiObjectNotFoundException { 56 // We might need to expand drawer if not visible 57 openDrawer(); 58 59 final UiSelector rootsList = new UiSelector().resourceId( 60 mTargetPackage + ":id/container_roots").childSelector( 61 new UiSelector().resourceId(mRootListId)); 62 63 // Wait for the first list item to appear 64 new UiObject(rootsList.childSelector(new UiSelector())).waitForExists(mTimeout); 65 66 // Now scroll around to find our item 67 new UiScrollable(rootsList).scrollIntoView(new UiSelector().text(label)); 68 return new UiObject(rootsList.childSelector(new UiSelector().text(label))); 69 } 70 openRoot(String label)71 public void openRoot(String label) throws UiObjectNotFoundException { 72 findRoot(label).click(); 73 // Close the drawer in case we select a pre-selected root already 74 closeDrawer(); 75 } 76 openDrawer()77 public void openDrawer() throws UiObjectNotFoundException { 78 final UiSelector rootsList = new UiSelector().resourceId( 79 mTargetPackage + ":id/container_roots").childSelector( 80 new UiSelector().resourceId(mRootListId)); 81 82 // We might need to expand drawer if not visible 83 if (!new UiObject(rootsList).waitForExists(mTimeout)) { 84 Log.d(TAG, "Failed to find roots list; trying to expand"); 85 final UiSelector hamburger = new UiSelector().resourceId( 86 mTargetPackage + ":id/toolbar").childSelector( 87 new UiSelector().className("android.widget.ImageButton").clickable(true)); 88 new UiObject(hamburger).click(); 89 } 90 } 91 closeDrawer()92 public void closeDrawer() { 93 // Espresso will try to close the drawer if it's opened 94 // But if no drawer exists (Tablet devices), we will have to catch the exception 95 // and continue on the test 96 // Why can't we do something like .exist() first? 97 // http://stackoverflow.com/questions/20807131/espresso-return-boolean-if-view-exists 98 try { 99 if (mContext.getResources().getConfiguration() 100 .getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) { 101 onView(withId(R.id.drawer_layout)).perform(swipeRight()); 102 } else { 103 onView(withId(R.id.drawer_layout)).perform(swipeLeft()); 104 } 105 } catch (Exception e) { 106 } 107 } 108 assertRootsPresent(String... labels)109 public void assertRootsPresent(String... labels) throws UiObjectNotFoundException { 110 List<String> missing = new ArrayList<>(); 111 for (String label : labels) { 112 if (!findRoot(label).exists()) { 113 missing.add(label); 114 } 115 } 116 if (!missing.isEmpty()) { 117 Assert.fail( 118 "Expected roots " + Arrays.asList(labels) + ", but missing " + missing); 119 } 120 } 121 assertRootsAbsent(String... labels)122 public void assertRootsAbsent(String... labels) throws UiObjectNotFoundException { 123 List<String> unexpected = new ArrayList<>(); 124 for (String label : labels) { 125 if (findRoot(label).exists()) { 126 unexpected.add(label); 127 } 128 } 129 if (!unexpected.isEmpty()) { 130 Assert.fail("Unexpected roots " + unexpected); 131 } 132 } 133 assertHasFocus()134 public void assertHasFocus() { 135 assertHasFocus(mRootListId); 136 } 137 } 138