blob: 8af027c024d0c5739a409da19f7925a87af69369 [file] [log] [blame]
Tadashi G. Takaoka65046752017-08-01 13:02:59 +09001package com.android.settingslib.inputmethod;
2
3import static junit.framework.Assert.assertEquals;
4import static junit.framework.Assert.assertTrue;
5
6import android.support.test.InstrumentationRegistry;
7import android.support.test.filters.SmallTest;
8import android.support.test.runner.AndroidJUnit4;
9
10import org.junit.Test;
11import org.junit.runner.RunWith;
12
13import java.text.Collator;
14import java.util.ArrayList;
15import java.util.Arrays;
16import java.util.Collection;
17import java.util.List;
18import java.util.Locale;
19import java.util.function.BiConsumer;
20import java.util.function.Function;
21
22@SmallTest
23@RunWith(AndroidJUnit4.class)
24public class InputMethodSubtypePreferenceTest {
25
26 private static final List<InputMethodSubtypePreference> ITEMS_IN_ASCENDING = Arrays.asList(
27 // Subtypes that has the same locale of the system's.
28 createPreference("", "en_US", Locale.US),
29 createPreference("E", "en_US", Locale.US),
30 createPreference("Z", "en_US", Locale.US),
31 // Subtypes that has the same language of the system's.
32 createPreference("", "en", Locale.US),
33 createPreference("E", "en", Locale.US),
34 createPreference("Z", "en", Locale.US),
35 // Subtypes that has different language than the system's.
36 createPreference("", "ja", Locale.US),
37 createPreference("A", "hi_IN", Locale.US),
38 createPreference("B", "", Locale.US),
39 createPreference("E", "ja", Locale.US),
40 createPreference("Z", "ja", Locale.US)
41 );
42 private static final List<InputMethodSubtypePreference> SAME_ITEMS = Arrays.asList(
43 // Subtypes that has different language than the system's.
44 createPreference("A", "ja_JP", Locale.US),
45 createPreference("A", "hi_IN", Locale.US),
46 // Subtypes that has an empty subtype locale string.
47 createPreference("A", "", Locale.US)
48 );
49 private static final Collator COLLATOR = Collator.getInstance(Locale.US);
50
51 @Test
52 public void testComparableOrdering() throws Exception {
53 onAllAdjacentItems(ITEMS_IN_ASCENDING,
54 (x, y) -> assertTrue(
55 x.getKey() + " is less than " + y.getKey(),
56 x.compareTo(y, COLLATOR) < 0)
57 );
58 }
59
60 @Test
61 public void testComparableEquality() {
62 onAllAdjacentItems(SAME_ITEMS,
63 (x, y) -> assertTrue(
64 x.getKey() + " is equal to " + y.getKey(),
65 x.compareTo(y, COLLATOR) == 0)
66 );
67 }
68
69 @Test
70 public void testComparableContracts() {
71 final Collection<InputMethodSubtypePreference> items = new ArrayList<>();
72 items.addAll(ITEMS_IN_ASCENDING);
73 items.addAll(SAME_ITEMS);
74 items.add(createPreference("", "", Locale.US));
75 items.add(createPreference("A", "en", Locale.US));
76 items.add(createPreference("A", "en_US", Locale.US));
77 items.add(createPreference("E", "hi_IN", Locale.US));
78 items.add(createPreference("E", "en", Locale.US));
79 items.add(createPreference("Z", "en_US", Locale.US));
80
81 assertComparableContracts(
82 items,
83 (x, y) -> x.compareTo(y, COLLATOR),
84 InputMethodSubtypePreference::getKey);
85 }
86
87 private static InputMethodSubtypePreference createPreference(
88 final String subtypeName,
89 final String subtypeLocaleString,
90 final Locale systemLocale) {
91 return new InputMethodSubtypePreference(
92 InstrumentationRegistry.getTargetContext(),
93 subtypeName + "-" + subtypeLocaleString + "-" + systemLocale,
94 subtypeName,
95 subtypeLocaleString,
96 systemLocale);
97 }
98
99 private static <T> void onAllAdjacentItems(final List<T> items, final BiConsumer<T, T> check) {
100 for (int i = 0; i < items.size() - 1; i++) {
101 check.accept(items.get(i), items.get(i + 1));
102 }
103 }
104
105 @FunctionalInterface
106 interface CompareTo<T> {
107 int apply(T t, T u);
108 }
109
110 private static <T> void assertComparableContracts(final Collection<T> items,
111 final CompareTo<T> compareTo, final Function<T, String> name) {
112 for (final T x : items) {
113 final String nameX = name.apply(x);
114 assertTrue("Reflective: " + nameX + " is equal to itself",
115 compareTo.apply(x, x) == 0);
116 for (final T y : items) {
117 final String nameY = name.apply(y);
118 assertEquals("Asymmetric: " + nameX + " and " + nameY,
119 Integer.signum(compareTo.apply(x, y)),
120 -Integer.signum(compareTo.apply(y, x)));
121 for (final T z : items) {
122 final String nameZ = name.apply(z);
123 if (compareTo.apply(x, y) > 0 && compareTo.apply(y, z) > 0) {
124 assertTrue("Transitive: " + nameX + " is greater than " + nameY
125 + " and " + nameY + " is greater than " + nameZ
126 + " then " + nameX + " is greater than " + nameZ,
127 compareTo.apply(x, z) > 0);
128 }
129 if (compareTo.apply(x, y) == 0) {
130 assertEquals("Transitive: " + nameX + " and " + nameY + " is same "
131 + " then both return the same result for " + nameZ,
132 Integer.signum(compareTo.apply(x, z)),
133 Integer.signum(compareTo.apply(y, z)));
134 }
135 }
136 }
137 }
138 }
139}