• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 junit.framework.Assert.assertEquals;
20 import static junit.framework.Assert.assertNotNull;
21 import static junit.framework.Assert.assertTrue;
22 
23 import android.content.Context;
24 import android.support.test.uiautomator.By;
25 import android.support.test.uiautomator.BySelector;
26 import android.support.test.uiautomator.Configurator;
27 import android.support.test.uiautomator.UiDevice;
28 import android.support.test.uiautomator.UiObject;
29 import android.support.test.uiautomator.UiObject2;
30 import android.support.test.uiautomator.UiObjectNotFoundException;
31 import android.support.test.uiautomator.UiSelector;
32 import android.support.test.uiautomator.Until;
33 import android.view.MotionEvent;
34 
35 import junit.framework.Assert;
36 
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.regex.Pattern;
41 
42 /**
43  * A test helper class that provides support for controlling directory list
44  * and making assertions against the state of it.
45  */
46 public class DirectoryListBot extends BaseBot {
47     private static final String DIR_LIST_ID = "com.android.documentsui:id/dir_list";
48 
49     private static final BySelector SNACK_DELETE =
50             By.desc(Pattern.compile("^Deleting [0-9]+ file.+"));
51 
DirectoryListBot(UiDevice device, Context context, int timeout)52     public DirectoryListBot(UiDevice device, Context context, int timeout) {
53         super(device, context, timeout);
54     }
55 
assertDocumentsCount(int count)56     public void assertDocumentsCount(int count) throws UiObjectNotFoundException {
57         UiObject docsList = findDocumentsList();
58         assertEquals(count, docsList.getChildCount());
59     }
60 
assertDocumentsPresent(String... labels)61     public void assertDocumentsPresent(String... labels) throws UiObjectNotFoundException {
62         List<String> absent = new ArrayList<>();
63         for (String label : labels) {
64             if (!findDocument(label).exists()) {
65                 absent.add(label);
66             }
67         }
68         if (!absent.isEmpty()) {
69             Assert.fail("Expected documents " + Arrays.asList(labels)
70                     + ", but missing " + absent);
71         }
72     }
73 
assertDocumentsAbsent(String... labels)74     public void assertDocumentsAbsent(String... labels) throws UiObjectNotFoundException {
75         List<String> found = new ArrayList<>();
76         for (String label : labels) {
77             if (findDocument(label).exists()) {
78                 found.add(label);
79             }
80         }
81         if (!found.isEmpty()) {
82             Assert.fail("Expected documents not present" + Arrays.asList(labels)
83                     + ", but present " + found);
84         }
85     }
86 
assertDocumentsCountOnList(boolean exists, int count)87     public void assertDocumentsCountOnList(boolean exists, int count) throws UiObjectNotFoundException {
88         UiObject docsList = findDocumentsList();
89         assertEquals(exists, docsList.exists());
90         if(docsList.exists()) {
91             assertEquals(count, docsList.getChildCount());
92         }
93     }
94 
assertMessageTextView(String message)95     public void assertMessageTextView(String message) throws UiObjectNotFoundException {
96         UiObject messageTextView = findMessageTextView();
97         assertTrue(messageTextView.exists());
98 
99         String msg = String.valueOf(message);
100         assertEquals(String.format(msg, "TEST_ROOT_0"), messageTextView.getText());
101 
102     }
103 
findMessageTextView()104     private UiObject findMessageTextView() {
105         return findObject(
106                 "com.android.documentsui:id/container_directory",
107                 "com.android.documentsui:id/message");
108     }
109 
assertSnackbar(int id)110     public void assertSnackbar(int id) {
111         assertNotNull(getSnackbar(mContext.getString(id)));
112     }
113 
openDocument(String label)114     public void openDocument(String label) throws UiObjectNotFoundException {
115         int toolType = Configurator.getInstance().getToolType();
116         Configurator.getInstance().setToolType(MotionEvent.TOOL_TYPE_FINGER);
117         UiObject doc = findDocument(label);
118         doc.click();
119         Configurator.getInstance().setToolType(toolType);
120     }
121 
clickDocument(String label)122     public void clickDocument(String label) throws UiObjectNotFoundException {
123         findDocument(label).click();
124     }
125 
selectDocument(String label)126     public UiObject selectDocument(String label) throws UiObjectNotFoundException {
127         UiObject doc = findDocument(label);
128         doc.longClick();
129         return doc;
130     }
131 
getSnackbar(String message)132     public UiObject2 getSnackbar(String message) {
133         return mDevice.wait(Until.findObject(By.text(message)), mTimeout);
134     }
135 
waitForDeleteSnackbar()136     public void waitForDeleteSnackbar() {
137         mDevice.wait(Until.findObject(SNACK_DELETE), mTimeout);
138     }
139 
waitForDeleteSnackbarGone()140     public void waitForDeleteSnackbarGone() {
141         // wait a little longer for snackbar to go away, as it disappears after a timeout.
142         mDevice.wait(Until.gone(SNACK_DELETE), mTimeout * 2);
143     }
144 
waitForDocument(String label)145     public void waitForDocument(String label) throws UiObjectNotFoundException {
146         findDocument(label).waitForExists(mTimeout);
147     }
148 
findDocument(String label)149     public UiObject findDocument(String label) throws UiObjectNotFoundException {
150         final UiSelector docList = new UiSelector().resourceId(
151                 "com.android.documentsui:id/container_directory").childSelector(
152                         new UiSelector().resourceId(DIR_LIST_ID));
153 
154         // Wait for the first list item to appear
155         new UiObject(docList.childSelector(new UiSelector())).waitForExists(mTimeout);
156 
157         // new UiScrollable(docList).scrollIntoView(new UiSelector().text(label));
158         return mDevice.findObject(docList.childSelector(new UiSelector().text(label)));
159     }
160 
hasDocuments(String... labels)161     public boolean hasDocuments(String... labels) throws UiObjectNotFoundException {
162         for (String label : labels) {
163             if (!findDocument(label).exists()) {
164                 return false;
165             }
166         }
167         return true;
168     }
169 
findDocumentsList()170     public UiObject findDocumentsList() {
171         return findObject(
172                 "com.android.documentsui:id/container_directory",
173                 DIR_LIST_ID);
174     }
175 
assertHasFocus()176     public void assertHasFocus() {
177         assertHasFocus(DIR_LIST_ID);
178     }
179 }
180