• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.view.View;
20 
21 import org.hamcrest.Description;
22 import org.hamcrest.TypeSafeMatcher;
23 
24 public class PaddingMatcher extends TypeSafeMatcher<View> {
25 
26     public enum Side {
27         TOP,
28         BOTTOM,
29         LEFT,
30         RIGHT,
31         START,
32         END
33     }
34 
35     private Side mSide;
36     private int mMin;
37     private int mMax;
38 
PaddingMatcher(Side side, int min, int max)39     public PaddingMatcher(Side side, int min, int max) {
40         mSide = side;
41         mMin = min;
42         mMax = max;
43     }
44 
45     @Override
matchesSafely(View item)46     protected boolean matchesSafely(View item) {
47         int padding = 0;
48         switch (mSide) {
49             case TOP:
50                 padding = item.getPaddingTop();
51                 break;
52             case BOTTOM:
53                 padding = item.getPaddingBottom();
54                 break;
55             case LEFT:
56                 padding = item.getPaddingLeft();
57                 break;
58             case RIGHT:
59                 padding = item.getPaddingRight();
60                 break;
61             case START:
62                 padding = item.getPaddingStart();
63                 break;
64             case END:
65                 padding = item.getPaddingEnd();
66                 break;
67         }
68 
69         if (mMin >= 0 && padding < mMin) {
70             return false;
71         }
72 
73         return mMax < 0 || padding <= mMax;
74     }
75 
76     @Override
describeTo(Description description)77     public void describeTo(Description description) {
78         description
79             .appendText("with " + mSide.toString() + " padding between " + mMin + " and " + mMax);
80     }
81 }
82