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