1 /*
2  * Copyright (C) 2020 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.car.ui.matchers;
18 
19 import static androidx.test.espresso.assertion.ViewAssertions.matches;
20 import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
21 
22 import static org.hamcrest.Matchers.allOf;
23 import static org.hamcrest.Matchers.not;
24 
25 import android.content.Context;
26 import android.graphics.drawable.Drawable;
27 import android.view.View;
28 
29 import androidx.annotation.DrawableRes;
30 import androidx.annotation.NonNull;
31 import androidx.test.espresso.ViewAssertion;
32 
33 import com.android.car.ui.matchers.PaddingMatcher.Side;
34 
35 import org.hamcrest.Matcher;
36 
37 public class ViewMatchers {
withDrawable( @onNull Context context, @DrawableRes int drawableId)38     public static Matcher<View> withDrawable(
39             @NonNull Context context, @DrawableRes int drawableId) {
40         return new DrawableMatcher(context, drawableId);
41     }
42 
withDrawable( @onNull Drawable drawable)43     public static Matcher<View> withDrawable(
44             @NonNull Drawable drawable) {
45         return new DrawableMatcher(drawable);
46     }
47 
nthChildOfView(Matcher<View> parentMatcher, int n)48     public static Matcher<View> nthChildOfView(Matcher<View> parentMatcher, int n) {
49         return new NthChildMatcher(parentMatcher, n);
50     }
51 
withIndex(Matcher<View> matcher, int index)52     public static Matcher<View> withIndex(Matcher<View> matcher, int index) {
53         return new IndexMatcher(matcher, index);
54     }
55 
withPadding(Side side, int exactly)56     public static Matcher<View> withPadding(Side side, int exactly) {
57         return new PaddingMatcher(side, exactly, exactly);
58     }
59 
withPaddingAtLeast(Side side, int min)60     public static Matcher<View> withPaddingAtLeast(Side side, int min) {
61         return new PaddingMatcher(side, min, -1);
62     }
63 
isActivated()64     public static Matcher<View> isActivated() {
65         return new IsActivatedMatcher();
66     }
67 
doesNotExistOrIsNotDisplayed()68     public static ViewAssertion doesNotExistOrIsNotDisplayed() {
69         return (view, noViewFoundException) -> {
70             if (view != null) {
71                 matches(not(isDisplayed())).check(view, noViewFoundException);
72             }
73         };
74     }
75 
76     public static Matcher<View> isDisplayedAnd(Matcher<View> another) {
77         return allOf(isDisplayed(), another);
78     }
79 
80 
81     public static Matcher<View> hasIndeterminateProgress() {
82         return new ProgressBarIndeterminateMatcher();
83     }
84 }
85