This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
AxeConf.java
293 lines (241 loc) · 7.74 KB
/
AxeConf.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
package com.deque.axe.android;
import static com.deque.axe.android.constants.AxeImpact.CRITICAL;
import static com.deque.axe.android.constants.AxeImpact.MODERATE;
import static com.deque.axe.android.constants.AxeImpact.SERIOUS;
import com.deque.axe.android.constants.AxeStandard;
import com.deque.axe.android.constants.Constants;
import com.deque.axe.android.rules.hierarchy.ActiveViewName;
import com.deque.axe.android.rules.hierarchy.CheckBoxName;
import com.deque.axe.android.rules.hierarchy.ColorContrast;
import com.deque.axe.android.rules.hierarchy.EditTextName;
import com.deque.axe.android.rules.hierarchy.EditTextValue;
import com.deque.axe.android.rules.hierarchy.ImageViewName;
import com.deque.axe.android.rules.hierarchy.SwitchName;
import com.deque.axe.android.rules.hierarchy.TouchSizeWcag;
import com.deque.axe.android.rules.stateful.DontMoveAccessibilityFocus;
import com.deque.axe.android.utils.JsonSerializable;
import com.deque.axe.android.utils.SuppressFBWarnings;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class AxeConf {
@SuppressFBWarnings(
value = "URF_UNREAD_FIELD",
justification = "This is an object that isn't used for"
+ " anything but serialization."
)
static class IssueFilterConf implements JsonSerializable {
boolean onlyShowResultsVisibleToUser = false;
}
static class RuleConf implements JsonSerializable {
final int impact;
final String standard;
final String summary;
boolean ignored = false;
RuleConf(
final int impact,
final String standard,
final String summary
) {
this.impact = impact;
this.standard = standard;
this.summary = summary;
}
}
/**
* The default standards to apply to every new Axe Instance.
*/
private static final @AxeStandard Set<String> DEFAULT_STANDARDS = new HashSet<>();
static {
DEFAULT_STANDARDS.add(AxeStandard.BEST_PRACTICE);
DEFAULT_STANDARDS.add(AxeStandard.PLATFORM);
DEFAULT_STANDARDS.add(AxeStandard.WCAG_20);
DEFAULT_STANDARDS.add(AxeStandard.WCAG_21);
}
/**
* AxeConf Constructor to instantiate the legacy rules.
*/
public AxeConf() {
rules.put(
ActiveViewName.class.getSimpleName(),
new RuleConf(
CRITICAL.getValue(),
AxeStandard.WCAG_20,
"Views that users can interact with must have a Name."
)
);
rules.put(
CheckBoxName.class.getSimpleName(),
new RuleConf(
MODERATE.getValue(),
AxeStandard.BEST_PRACTICE,
"Views that have modifiable Values should get their name from a nearby Label."
)
);
rules.put(
ColorContrast.class.getSimpleName(),
new RuleConf(
MODERATE.getValue(),
AxeStandard.WCAG_20,
"Text adequately contrasts with its background."
)
);
rules.put(
EditTextName.class.getSimpleName(),
new RuleConf(
MODERATE.getValue(),
AxeStandard.BEST_PRACTICE,
"Views that have modifiable Values should get their name from a nearby Label."
)
);
rules.put(
EditTextValue.class.getSimpleName(),
new RuleConf(
CRITICAL.getValue(),
AxeStandard.WCAG_20,
"Editable Views must not override the Value spoken by TalkBack."
)
);
rules.put(
ImageViewName.class.getSimpleName(),
new RuleConf(
CRITICAL.getValue(),
AxeStandard.WCAG_20,
"Focusable Informative Views must have Text or a ContentDescription."
)
);
rules.put(
SwitchName.class.getSimpleName(),
new RuleConf(
MODERATE.getValue(),
AxeStandard.BEST_PRACTICE,
"Views that have modifiable Values should get their name from a nearby Label."
)
);
rules.put(
TouchSizeWcag.class.getSimpleName(),
new RuleConf(
MODERATE.getValue(),
AxeStandard.WCAG_21,
"Active views adhere to WCAG Touch Target Size requirements."
)
);
rules.put(
DontMoveAccessibilityFocus.class.getSimpleName(),
new RuleConf(
SERIOUS.getValue(),
AxeStandard.BEST_PRACTICE,
"Applications should not forcibly move focus around."
)
);
}
/**
* A Set of AxeTypes to include in this AxeRun.
*/
final @AxeStandard Set<String> standards = DEFAULT_STANDARDS;
/**
* A set of AxeRules that will be included Regardless of any other setting.
*/
@Deprecated
final Set<String> ruleIds = new HashSet<>();
final Map<String, RuleConf> rules = new HashMap<>();
final IssueFilterConf issueFilterConf = new IssueFilterConf();
public final transient Set<Class<? extends AxeRuleViewHierarchy>> customRules = new HashSet<>();
/**
* A set of AxeRules that will be excluded from result.
* @param ruleIds List of ruleIds to ignore.
* @param ignore boolean to ignore or not.
* @return the AxeConf instance.
*/
public AxeConf ignore(final List<String> ruleIds, boolean ignore) {
for (String s : ruleIds) {
ignore(s, ignore);
}
return this;
}
/**
* An AxeRule that will be excluded from result.
* @param ruleId ruleId to ignore.
* @param ignore boolean to ignore or not.
* @return the AxeConf instance.
*/
public AxeConf ignore(final String ruleId, final boolean ignore) {
rules.get(ruleId).ignored = ignore;
if (ignore) {
ruleIds.remove(ruleId);
} else {
ruleIds.add(ruleId);
}
return this;
}
/**
* Deprecated: add rule to rules map.
*/
@Deprecated
public AxeConf addRule(final Class<? extends AxeRuleViewHierarchy> rule) {
final AxeRule axeRule;
try {
axeRule = rule.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
ruleIds.add(rule.getSimpleName());
rules.put(rule.getSimpleName(), new RuleConf(
axeRule.impact,
axeRule.standard,
axeRule.summary
));
return this;
}
@Deprecated
public AxeConf removeStandard(@AxeStandard String standard) {
standards.remove(standard);
return this;
}
private void normalize() {
// Make sure that all RulesIDs were trying to run are classes we have access to.
for (String ruleId : ruleIds) {
if (!rules.containsKey(ruleId)) {
throw new RuntimeException("Tried to run a RuleID that doesn't exist.");
}
}
// Make sure that the RuleIDs list matches the rules data structure.
ruleIds.clear();
for (Map.Entry<String, RuleConf> entry : rules.entrySet()) {
String s = entry.getKey();
RuleConf ruleConf = entry.getValue();
if (!ruleConf.ignored) {
ruleIds.add(s);
}
}
}
Set<AxeRule> ruleInstances() {
if (ruleIds.size() == 0) {
normalize();
}
final Set<AxeRule> ruleInstances = new HashSet<>();
try {
for (Class<? extends AxeRule> axeRuleClass : Constants.AXE_RULE_CLASSES) {
final AxeRule axeRule = axeRuleClass.newInstance();
if (standards.contains(axeRule.standard)
|| ruleIds.contains(axeRule.getClass().getSimpleName())) {
ruleInstances.add(axeRule);
ruleIds.add(axeRule.getClass().getSimpleName());
}
}
for (Class<? extends AxeRuleViewHierarchy> axeRuleClass : customRules) {
ruleInstances.add(axeRuleClass.newInstance());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return ruleInstances;
}
@Deprecated
public AxeConf includeBestPractices() {
standards.add(AxeStandard.BEST_PRACTICE);
return this;
}
}