forked from ab-rohman/Open-Hack2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElementTest.java
345 lines (292 loc) · 14.3 KB
/
ElementTest.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package org.aplas.colorgamex;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TextView;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowTypeface;
import java.util.List;
public class ElementTest extends ViewTest {
private View component;
private String compName;
private String msgHeader;
private String className;
//private ArrayList<HashMap> testList = new ArrayList<>();
public ElementTest(View comp) {
component =comp;
compName = component.getContext().getResources().getResourceEntryName(component.getId());
className = component.getClass().getSimpleName();
msgHeader = "\n( "+compName+" - "+className+" ) ";
}
public void testIdName(String expected) {
String msg = msgHeader + "Element Id Name is not suitable\n";
testItem(expected,compName,msg,1);
}
public void testWidth(int expected) {
String msg = msgHeader + "Element width is not suitable\n";
testItem(expected,component.getLayoutParams().width,msg,1);
}
public void testHeight(int expected) {
String msg = msgHeader + "Element height is not suitable\n";
testItem(expected,component.getLayoutParams().height,msg,1);
}
public void testWeight(float expected) {
String msg = msgHeader + "Element height is not suitable\n";
testItem(expected,((LinearLayout.LayoutParams)component.getLayoutParams()).weight,msg,1);
}
public void testOrientation(int expected) {
String msg = msgHeader + "Element orientation is not suitable\n";
testItem(expected,((LinearLayout)component).getOrientation(),msg,1);
}
public void testTopMargin(int expected) {
String msg = msgHeader + "Element top margin is not suitable\n";
if (component.getLayoutParams().getClass().equals(RelativeLayout.LayoutParams.class)) {
testItem(expected,((RelativeLayout.LayoutParams)component.getLayoutParams()).topMargin,msg,1);
} else {
testItem(expected, ((LinearLayout.LayoutParams) component.getLayoutParams()).topMargin, msg, 1);
}
}
public void testBottomMargin(int expected) {
String msg = msgHeader + "Element top margin is not suitable\n";
if (component.getLayoutParams().getClass().equals(RelativeLayout.LayoutParams.class)) {
testItem(expected,((RelativeLayout.LayoutParams)component.getLayoutParams()).bottomMargin,msg,1);
} else {
testItem(expected, ((LinearLayout.LayoutParams) component.getLayoutParams()).bottomMargin, msg, 1);
}
}
public void testPaddingLeft(int expected) {
String msg = msgHeader + "Element left padding is not suitable\n";
testItem(expected,component.getPaddingLeft(),msg,1);
}
public void testPaddingRight(int expected) {
String msg = msgHeader + "Element right padding is not suitable\n";
testItem(expected,component.getPaddingRight(),msg,1);
}
public void testPaddingBottom(int expected) {
String msg = msgHeader + "Element right padding is not suitable\n";
testItem(expected,component.getPaddingBottom(),msg,1);
}
public void testPadding(int expected) {
String msg = msgHeader + "Element padding is not suitable\n";
testItem(expected,component.getPaddingTop(),msg,1);
testItem(expected,component.getPaddingBottom(),msg,1);
testItem(expected,component.getPaddingLeft(),msg,1);
testItem(expected,component.getPaddingRight(),msg,1);
}
public void testBgColor(int expected) {
String msg = msgHeader + "Element Background color is not suitable\n";
testItem(expected,((ColorDrawable) component.getBackground()).getColor(),msg,1);
}
public void testBgGradientColor(int[] expected) {
String msg = msgHeader + "Element Background gradient color is not suitable\n";
GradientDrawable draw = (GradientDrawable) component.getBackground();
//draw.getColors()
testItem(expected[0],draw.getColors()[0],msg,1);
testItem(expected[1],draw.getColors()[1],msg,1);
testItem(expected[2],draw.getColors()[2],msg,1);
}
public void testTextString(String expected) {
String msg = msgHeader + "Element text string is not suitable\n";
testItem(expected,((TextView)component).getText().toString(),msg,1);
}
public void testTextFont(String expected) {
String msg = msgHeader + "Element text font is not suitable\n";
//testItem(expected,((TextView)component).getPaint().getTypeface(),msg,1);
ShadowTypeface shadow = Shadows.shadowOf(((TextView)component).getPaint().getTypeface());
testItem(expected,shadow.getFontDescription().familyName,msg,1);
}
public void testTextStyle(int expected) {
String msg = msgHeader + "Element text style (bold/italic/normal) is not suitable\n";
//testItem(expected,((TextView)component).getPaint().getTypeface().getStyle(),msg,1);
ShadowTypeface shadow = Shadows.shadowOf(((TextView)component).getPaint().getTypeface());
testItem(expected,shadow.getFontDescription().style,msg,1);
}
public void testTextColor(int expected) {
String msg = msgHeader + "Element text color is not suitable\n";
testItem(expected,((TextView)component).getTextColors().getDefaultColor(),msg,1);
}
public void testTextSize(float expected) {
String msg = msgHeader + "Element text size is not suitable\n";
/*if (className.equals("AppCompatSpinner")) {
testItem(expected,((Spinner)component).get,msg,1);
}*/
testItem(expected,((TextView)component).getTextSize(),msg,1);
}
public void testGravity(int expected) {
String msg = msgHeader + "Element gravity orientation is not suitable\n";
if (className.equals("AppCompatSpinner")) {
testItem(expected,((Spinner)component).getGravity(),msg,1);
} else if (className.equals("AppCompatTextView")) {
testItem(expected, ((TextView) component).getGravity(), msg, 1);
} else if (className.equals("TableLayout")) {
testItem(expected, ((LinearLayout)component).getGravity(), msg, 1);
}
}
public void testAllCaps(boolean expected) {
String msg = msgHeader + "Element all caps is not suitable\n";
testItem(expected,((TextView)component).isAllCaps(),msg,1);
}
public void testHintText(String expected) {
String msg = msgHeader + "Element hint text is not suitable\n";
testItem(expected,((TextView)component).getHint().toString(),msg,1);
}
public void testInputType(int expected) {
String msg = msgHeader + "Element input type is not suitable\n";
testItem(expected,((TextView)component).getInputType(),msg,1);
}
public void testIsEnabled(boolean expected) {
String msg = msgHeader + "Element enabled value is not suitable\n";
testItem(expected,component.isEnabled(),msg,1);
}
public void testPromptText(String expected) {
String msg = msgHeader + "Element prompt text is not suitable\n";
testItem(expected,((Spinner)component).getPrompt().toString(),msg,1);
}
public void testSpinnerMode(int expected) {
String msg = msgHeader + "Element mode is not suitable\n";
if (expected==0) {
testItem(expected,((Spinner)component).getDropDownVerticalOffset(),msg,1);
} else {
testItem(expected,((Spinner)component).getDropDownVerticalOffset()>0,msg,3);
}
}
public void testSpinnerContent(List expected) {
String msg = msgHeader + "Element spinner content is not suitable\n";
Adapter list = ((Spinner)component).getAdapter();
String res = "";
for (int i=0; i<list.getCount(); i++){
res += list.getItem(i)+";";
/*
if (!expected.get(i).equals(list.getItem(i))) {
isValid = false;
}*/
}
testItem(listToString(expected),res,msg,1);
}
public void testSpinnerContent(String[] expected) {
/*
String msg = msgHeader + "Element spinner content is not suitable\n";
//testItem(expected,((Spinner)component).getGravity(),msg,1);
Adapter list = ((Spinner)component).getAdapter();
boolean isValid = true;
for (int i=0; i<list.getCount(); i++){
if (!expected[i].equals(list.getItem(i))) {
isValid = false;
}
}
testItem(true,isValid,msg,1);
*/
String msg = msgHeader + "Element spinner content is not suitable\n";
Adapter list = ((Spinner)component).getAdapter();
String res = "";
for (int i=0; i<list.getCount(); i++){
res += list.getItem(i)+";";
}
testItem(arrayToString(expected),res,msg,1);
}
public void testSpinnerContent(String expected) {
testSpinnerContent(expected.split("\\,"));
}
public void testSelected(boolean expected) {
String msg = msgHeader + "Element checked/selected value is not suitable\n";
if (component.getClass().equals(CheckBox.class)) {
testItem(expected,((CheckBox)component).isChecked(),msg,1);
} else {
testItem(expected,((RadioButton)component).isChecked(),msg,1);
}
}
public void testImageSrc(String expected) {
String msg = msgHeader + "Element image source is not suitable\n";
//int resId = R.font.cambria;
int resId = component.getContext().getResources().getIdentifier(expected, "drawable",component.getContext().getPackageName());
testItem(expected,component.getResources().getResourceEntryName(resId),msg,1);
//int resId = (Integer) component.gett
// testItem(0,resId,msg,1);
//testItem(expected,component.getResources().getResourceEntryName(resId),msg,1);
}
public void testLayoutBelow(int expected) {
String msg = msgHeader + "Element layout below is not suitable\n";
RelativeLayout.LayoutParams para = (RelativeLayout.LayoutParams)component.getLayoutParams();
testItem(expected, para.getRule(3), msg, 1); //check android:layout_below
//para.get
}
public void testLinearHorizontal(boolean expected) {
String msg = msgHeader + "Element layout center horizontal is not suitable\n";
RelativeLayout.LayoutParams para = (RelativeLayout.LayoutParams)component.getLayoutParams();
int val = (expected)?-1:0;
/*int val=0;
int idx=0;
for (int i=0; i<para.getRules().length; i++){
if (para.getRule(i)!=0) {
val=para.getRule(i);
idx=i;
}
}*/
testItem(val, para.getRule(14), msg, 1); //check android:layout_below
}
public void testStrectchColumn(boolean expected) {
String msg = msgHeader + "Element column strecth is not suitable\n";
testItem(expected,((TableLayout)component).isStretchAllColumns(),msg,1);
}
public void testVisibility(int visibility) {
String msg = msgHeader + "Element visibility is not suitable\n";
testItem(visibility,component.getVisibility(),msg,1);
}
public void testOnClickMethod(String expected) {
String msg = msgHeader + "Element onClick is not suitable\n";
testItem(expected,((Button)component).hasOnClickListeners(),msg,3);
}
public void testTextAlignment(int expected) {
String msg = msgHeader + "Element text alignment is not suitable\n";
testItem(expected,component.getTextAlignment(),msg,1);
}
public void testCenterInParent() {
String msg = msgHeader + "Element centerInParent is not suitable\n";
testItem(-1,((RelativeLayout.LayoutParams)component.getLayoutParams()).getRules()[13],msg,1);
}
public void testAlignParentEnd() {
String msg = msgHeader + "Element alignParentEnd is not suitable\n";
/*
String x="";
for (int i=0; i<22; i++) {
x += ((RelativeLayout.LayoutParams)component.getLayoutParams()).getRules()[i]+"-";
}
testItem("",x,msg,1);
*/
testItem(-1,((RelativeLayout.LayoutParams)component.getLayoutParams()).getRules()[11],msg,1);
}
public void testTextAppearance() {
//((TextView)component).getTextColors().
}
public void testProgressProgressBar(int expected) {
String msg = msgHeader + "Element progress in ProgressBar is not suitable\n";
testItem(expected,((ProgressBar)component).getProgress(),msg,1);
}
public void testMaxProgressBar(int expected) {
String msg = msgHeader + "Element progress in ProgressBar is not suitable\n";
testItem(expected,((ProgressBar)component).getMax(),msg,1);
}
public void testBgDrawable(String name, Drawable expected) {
String msg = msgHeader + "Element background drawable should be "+name+"\n";
GradientDrawable gExpected = (GradientDrawable)expected;
GradientDrawable gActual = (GradientDrawable)component.getBackground();
String eColors = gExpected.getColors()[0]+"-"+gExpected.getColors()[1]+"-"+gExpected.getColors()[2];
String aColors = gActual.getColors()[0]+"-"+gActual.getColors()[1]+"-"+gActual.getColors()[2];
testItem(eColors,aColors,msg,1);
testItem(gExpected.getCornerRadius(),gActual.getCornerRadius(),msg,1);
testItem(gExpected.getGradientRadius(),gActual.getGradientRadius(),msg,1);
testItem(gExpected.getGradientType(),gActual.getGradientType(),msg,1);
testItem(gExpected.getGradientCenterX(),gActual.getGradientCenterX(),msg,1);
testItem(gExpected.getGradientCenterY(),gActual.getGradientCenterY(),msg,1);
}
}