• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.content.Context;
20 
21 import androidx.test.uiautomator.By;
22 import androidx.test.uiautomator.UiDevice;
23 import androidx.test.uiautomator.UiObject2;
24 
25 import junit.framework.Assert;
26 
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.function.Predicate;
31 
32 /**
33  * A test helper class that provides support for controlling the UI Breadcrumb
34  * programmatically, and making assertions against the state of the UI.
35  * <p>
36  * Support for working directly with Roots and Directory view can be found in the respective bots.
37  */
38 public class BreadBot extends Bots.BaseBot {
39 
40     private final String mBreadCrumbId;
41 
BreadBot(UiDevice device, Context context, int timeout)42     public BreadBot(UiDevice device, Context context, int timeout) {
43         super(device, context, timeout);
44         mBreadCrumbId = mTargetPackage + ":id/horizontal_breadcrumb";
45     }
46 
clickItem(String label)47     public void clickItem(String label) {
48         findHorizontalEntry(label).click();
49     }
50 
assertItemsPresent(String... items)51     public void assertItemsPresent(String... items) {
52         Predicate<String> checker = this::hasHorizontalEntry;
53         assertItemsPresent(items, checker);
54     }
55 
assertItemsPresent(String[] items, Predicate<String> predicate)56     private void assertItemsPresent(String[] items, Predicate<String> predicate) {
57         List<String> absent = new ArrayList<>();
58         for (String item : items) {
59             if (!predicate.test(item)) {
60                 absent.add(item);
61             }
62         }
63         if (!absent.isEmpty()) {
64             Assert.fail("Expected iteams " + Arrays.asList(items)
65                     + ", but missing " + absent);
66         }
67     }
68 
hasHorizontalEntry(String label)69     private boolean hasHorizontalEntry(String label) {
70         return findHorizontalEntry(label) != null;
71     }
72 
73     @SuppressWarnings("unchecked")
findHorizontalEntry(String label)74     private UiObject2 findHorizontalEntry(String label) {
75         UiObject2 breadcrumb = mDevice.findObject(By.res(mBreadCrumbId));
76         return breadcrumb.findObject(By.text(label));
77     }
78 }
79