1+ package com .anysoftkeyboard .api ;
2+
3+ import android .content .res .Resources ;
4+
5+ import org .junit .Assert ;
6+ import org .junit .Test ;
7+ import org .junit .runner .RunWith ;
8+ import org .robolectric .RobolectricTestRunner ;
9+ import org .robolectric .RuntimeEnvironment ;
10+
11+ import java .lang .reflect .Field ;
12+ import java .lang .reflect .Modifier ;
13+ import java .util .HashSet ;
14+ import java .util .function .BinaryOperator ;
15+ import java .util .function .Function ;
16+
17+ @ RunWith (RobolectricTestRunner .class )
18+ public class KeyCodesTest {
19+
20+ @ Test
21+ public void testVerifyKeyCodesHasUniques () throws Exception {
22+ HashSet <Integer > seenValues = new HashSet <>();
23+
24+ for (Field field : KeyCodes .class .getFields ()) {
25+ final int intValue = (int ) field .get (null /*This is a static field*/ );
26+ Assert .assertTrue ("Field " + field , seenValues .add (intValue ));
27+ }
28+
29+ //verifying that the R integers match
30+ testVerifyKeyCodesResourcesHasUniques (seenValues );
31+ }
32+
33+ private void testVerifyKeyCodesResourcesHasUniques (HashSet <Integer > seenValues ) throws Exception {
34+ Resources resources = RuntimeEnvironment .application .getResources ();
35+ for (Field field : R .integer .class .getFields ()) {
36+ if (field .getName ().startsWith ("key_code_" )) {
37+ final int idValue = (int ) field .get (null /*This is a static field*/ );
38+ final int intValue = resources .getInteger (idValue );
39+
40+ Assert .assertTrue ("Field " + field , seenValues .remove (intValue ));
41+ }
42+ }
43+
44+
45+ Assert .assertEquals (
46+ seenValues .stream ().map (new Function <Integer , String >() {
47+ @ Override
48+ public String apply (Integer integer ) {
49+ return integer .toString ();
50+ }
51+ }).reduce (new BinaryOperator <String >() {
52+ @ Override
53+ public String apply (String s , String s2 ) {
54+ return s + ", " + s2 ;
55+ }
56+ }).orElse ("EMPTY" ),
57+ 0 , seenValues .size ());
58+ }
59+
60+ @ Test
61+ public void testAllFieldsArePublicStaticFinalInt () {
62+ for (Field field : KeyCodes .class .getFields ()) {
63+ Assert .assertEquals ("Field " + field , Modifier .PUBLIC , field .getModifiers () & Modifier .PUBLIC );
64+ Assert .assertEquals ("Field " + field , Modifier .STATIC , field .getModifiers () & Modifier .STATIC );
65+ Assert .assertEquals ("Field " + field , Modifier .FINAL , field .getModifiers () & Modifier .FINAL );
66+ Assert .assertEquals ("Field " + field , int .class , field .getType ());
67+ }
68+ }
69+ }
0 commit comments