-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonMatcher.java
executable file
·168 lines (141 loc) · 5.63 KB
/
ButtonMatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.eutechpro.matchers;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.v4.content.res.ResourcesCompat;
import android.view.View;
import android.widget.Button;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
/**
* Created by Kursulla on 14/01/16.
*/
public class ButtonMatcher {
private static final int POSITION_LEFT = 0;
private static final int POSITION_TOP = 1;
private static final int POSITION_RIGHT = 2;
private static final int POSITION_BOTTOM = 3;
/**
* Match does target {@link Button} has appropriate {@link Drawable} resource on place of Left compound background.
*
* @param resourceId
* @return Does it match or not.
*/
public static Matcher<View> buttonHasLeftCompoundBackground(@DrawableRes final int resourceId) {
return new TypeSafeMatcher<View>() {
Resources resources = null;
@Override
public boolean matchesSafely(View view) {
this.resources = view.getResources();
return match(view, resourceId, POSITION_LEFT);
}
@Override
public void describeTo(Description description) {
description.appendText("Button is missing compound image with id: " + getResourceName(resources, resourceId));
}
};
}
/**
* Match does target {@link Button} has appropriate {@link Drawable} resource on place of Top compound background.
*
* @param resourceId
* @return Does it match or not.
*/
public static Matcher<View> buttonHasTopCompoundBackground(@DrawableRes final int resourceId) {
return new TypeSafeMatcher<View>() {
Resources resources = null;
@Override
public boolean matchesSafely(View view) {
this.resources = view.getResources();
return match(view, resourceId, POSITION_TOP);
}
@Override
public void describeTo(Description description) {
description.appendText("Button is missing compound image with id: " + getResourceName(resources, resourceId));
}
};
}
/**
* Match does target {@link Button} has appropriate {@link Drawable} resource on place of Right compound background.
*
* @param resourceId
* @return Does it match or not.
*/
public static Matcher<View> buttonHasRightCompoundBackground(@DrawableRes final int resourceId) {
return new TypeSafeMatcher<View>() {
Resources resources = null;
@Override
public boolean matchesSafely(View view) {
this.resources = view.getResources();
return match(view, resourceId, POSITION_RIGHT);
}
@Override
public void describeTo(Description description) {
description.appendText("Button is missing compound image with id: " + getResourceName(resources, resourceId));
}
};
}
/**
* Match does target {@link Button} has appropriate {@link Drawable} resource on place of Bottom compound background.
*
* @param resourceId
* @return Does it match or not.
*/
public static Matcher<View> buttonHasBottomCompoundBackground(@DrawableRes final int resourceId) {
return new TypeSafeMatcher<View>() {
Resources resources = null;
@Override
public boolean matchesSafely(View view) {
this.resources = view.getResources();
return match(view, resourceId, POSITION_BOTTOM);
}
@Override
public void describeTo(Description description) {
description.appendText("Button is missing compound image with id: " + getResourceName(resources, resourceId));
}
};
}
public static Matcher<View> buttonIsEnabled(){
return new TypeSafeMatcher<View>() {
Resources resources = null;
int buttonId;
@Override
protected boolean matchesSafely(View view) {
this.resources = view.getResources();
this.buttonId = view.getId();
return view.isEnabled();
}
@Override
public void describeTo(Description description) {
description.appendText("Button with id "+buttonId+" should be enabled!");
}
};
}
private static boolean match(View view, int resourceId, int position) {
if (!(view instanceof Button)) {
return false;
}
Drawable buttonDrawable = ((Button) view).getCompoundDrawables()[position];
if(buttonDrawable == null){
return false;
}
Drawable drawableToCheck = ResourcesCompat.getDrawable(view.getResources(), resourceId, null);
if(drawableToCheck == null){
return false;
}
return ((BitmapDrawable) buttonDrawable).getBitmap().equals(((BitmapDrawable) drawableToCheck).getBitmap());
}
private static String getResourceName(Resources resources, int resourceId) {
String resourceName = Integer.toString(resourceId);
if (resources != null) {
try {
resourceName = resources.getResourceName(resourceId);
} catch (Resources.NotFoundException var4) {
resourceName = String.format("%d (resource name not found)", resourceId);
}
}
return resourceName;
}
}