summaryrefslogtreecommitdiff
path: root/tests/src/com/android/inputmethod/keyboard/internal/MoreKeySpecStringReferenceTests.java
blob: 3655abbd54f378bc7692922eefbb289047409490 (plain)
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
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.inputmethod.keyboard.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import android.app.Instrumentation;
import android.content.Context;
import android.content.res.Resources;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.inputmethod.latin.tests.R;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Locale;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class MoreKeySpecStringReferenceTests {
    private static final Locale TEST_LOCALE = Locale.ENGLISH;
    private final KeyboardTextsSet mTextsSet = new KeyboardTextsSet();

    @Before
    public void setUp() throws Exception {
        final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
        final Context testContext = instrumentation.getContext();
        final Resources testRes = testContext.getResources();
        final String testPackageName = testRes.getResourcePackageName(R.string.empty_string);
        mTextsSet.setLocale(TEST_LOCALE, testRes, testPackageName);
    }

    private void assertTextArray(final String message, final String value,
            final String ... expectedArray) {
        final String resolvedActual = mTextsSet.resolveTextReference(value);
        final String[] actual = MoreKeySpec.splitKeySpecs(resolvedActual);
        final String[] expected = (expectedArray.length == 0) ? null : expectedArray;
        MoreKeySpecSplitTests.assertArrayEquals(message, expected, actual);
    }

    private void assertError(final String message, final String value, final String ... expected) {
        try {
            assertTextArray(message, value, expected);
            fail(message);
        } catch (Exception pcpe) {
            // success.
        }
    }

    @Test
    public void testResolveNullText() {
        assertEquals("resolve null",
                mTextsSet.resolveTextReference(null), null);
    }

    @Test
    public void testResolveEmptyText() {
        assertEquals("resolve empty text",
                mTextsSet.resolveTextReference("!string/empty_string"), null);
    }

    @Test
    public void testSplitSingleEscaped() {
        assertTextArray("Escaped !string", "\\!string",
                "\\!string");
        assertTextArray("Escaped !string/", "\\!string/",
                "\\!string/");
        assertTextArray("Escaped !STRING/", "\\!STRING/",
                "\\!STRING/");
        assertTextArray("Escaped !string/name", "\\!string/empty_string",
                "\\!string/empty_string");
        assertTextArray("Escaped !STRING/NAME", "\\!STRING/EMPTY_STRING",
                "\\!STRING/EMPTY_STRING");
    }

    @Test
    public void testSplitMultiEscaped() {
        assertTextArray("Multiple escaped !string", "\\!,\\!string/empty_string",
                "\\!", "\\!string/empty_string");
        assertTextArray("Multiple escaped !STRING", "\\!,\\!STRING/EMPTY_STRING",
                "\\!", "\\!STRING/EMPTY_STRING");
    }

    @Test
    public void testSplitStringReferenceError() {
        assertError("Incomplete resource name", "!string/", "!string/");
        assertError("Non existing resource", "!string/non_existing");
    }

    @Test
    public void testSplitEmptyStringReference() {
        assertTextArray("Empty string", "!string/empty_string");
    }

    @Test
    public void testSplitResourceSingle() {
        assertTextArray("Single char", "!string/single_char",
                "a");
        assertTextArray("Space", "!string/space",
                " ");
        assertTextArray("Single label", "!string/single_label",
                "abc");
        assertTextArray("Spaces", "!string/spaces",
                "   ");
        assertTextArray("Spaces in label", "!string/spaces_in_label",
                "a b c");
        assertTextArray("Spaces at beginning of label", "!string/spaces_at_beginning_of_label",
                " abc");
        assertTextArray("Spaces at end of label", "!string/spaces_at_end_of_label",
                "abc ");
        assertTextArray("label surrounded by spaces", "!string/label_surrounded_by_spaces",
                " abc ");
        assertTextArray("Escape and single char", "\\\\!string/single_char",
                "\\\\a");
    }

    @Test
    public void testSplitResourceSingleEscaped() {
        assertTextArray("Escaped char",
                "!string/escaped_char", "\\a");
        assertTextArray("Escaped comma",
                "!string/escaped_comma", "\\,");
        assertTextArray("Escaped comma escape",
                "!string/escaped_comma_escape", "a\\,\\");
        assertTextArray("Escaped escape",
                "!string/escaped_escape", "\\\\");
        assertTextArray("Escaped label",
                "!string/escaped_label", "a\\bc");
        assertTextArray("Escaped label at beginning",
                "!string/escaped_label_at_beginning", "\\abc");
        assertTextArray("Escaped label at end",
                "!string/escaped_label_at_end", "abc\\");
        assertTextArray("Escaped label with comma",
                "!string/escaped_label_with_comma", "a\\,c");
        assertTextArray("Escaped label with comma at beginning",
                "!string/escaped_label_with_comma_at_beginning", "\\,bc");
        assertTextArray("Escaped label with comma at end",
                "!string/escaped_label_with_comma_at_end", "ab\\,");
        assertTextArray("Escaped label with successive",
                "!string/escaped_label_with_successive", "\\,\\\\bc");
        assertTextArray("Escaped label with escape",
                "!string/escaped_label_with_escape", "a\\\\c");
    }

    @Test
    public void testSplitResourceMulti() {
        assertTextArray("Multiple chars",
                "!string/multiple_chars", "a", "b", "c");
        assertTextArray("Multiple chars surrounded by spaces",
                "!string/multiple_chars_surrounded_by_spaces",
                " a ", " b ", " c ");
        assertTextArray("Multiple labels",
                "!string/multiple_labels", "abc", "def", "ghi");
        assertTextArray("Multiple labels surrounded by spaces",
                "!string/multiple_labels_surrounded_by_spaces", " abc ", " def ", " ghi ");
    }

    @Test
    public void testSplitResourcetMultiEscaped() {
        assertTextArray("Multiple chars with comma",
                "!string/multiple_chars_with_comma",
                "a", "\\,", "c");
        assertTextArray("Multiple chars with comma surrounded by spaces",
                "!string/multiple_chars_with_comma_surrounded_by_spaces",
                " a ", " \\, ", " c ");
        assertTextArray("Multiple labels with escape",
                "!string/multiple_labels_with_escape",
                "\\abc", "d\\ef", "gh\\i");
        assertTextArray("Multiple labels with escape surrounded by spaces",
                "!string/multiple_labels_with_escape_surrounded_by_spaces",
                " \\abc ", " d\\ef ", " gh\\i ");
        assertTextArray("Multiple labels with comma and escape",
                "!string/multiple_labels_with_comma_and_escape",
                "ab\\\\", "d\\\\\\,", "g\\,i");
        assertTextArray("Multiple labels with comma and escape surrounded by spaces",
                "!string/multiple_labels_with_comma_and_escape_surrounded_by_spaces",
                " ab\\\\ ", " d\\\\\\, ", " g\\,i ");
    }

    @Test
    public void testSplitMultipleResources() {
        assertTextArray("Literals and resources",
                "1,!string/multiple_chars,z",
                "1", "a", "b", "c", "z");
        assertTextArray("Literals and resources and escape at end",
                "\\1,!string/multiple_chars,z\\",
                "\\1", "a", "b", "c", "z\\");
        assertTextArray("Multiple single resource chars and labels",
                "!string/single_char,!string/single_label,!string/escaped_comma",
                "a", "abc", "\\,");
        assertTextArray("Multiple single resource chars and labels 2",
                "!string/single_char,!string/single_label,!string/escaped_comma_escape",
                "a", "abc", "a\\,\\");
        assertTextArray("Multiple multiple resource chars and labels",
                "!string/multiple_chars,!string/multiple_labels,!string/multiple_chars_with_comma",
                "a", "b", "c", "abc", "def", "ghi", "a", "\\,", "c");
        assertTextArray("Concatenated resources",
                "!string/multiple_chars!string/multiple_labels!string/multiple_chars_with_comma",
                "a", "b", "cabc", "def", "ghia", "\\,", "c");
        assertTextArray("Concatenated resource and literal",
                "abc!string/multiple_labels",
                "abcabc", "def", "ghi");
    }

    @Test
    public void testSplitIndirectReference() {
        assertTextArray("Indirect",
                "!string/indirect_string", "a", "b", "c");
        assertTextArray("Indirect with literal",
                "1,!string/indirect_string_with_literal,2", "1", "x", "a", "b", "c", "y", "2");
        assertTextArray("Indirect2",
                "!string/indirect2_string", "a", "b", "c");
    }

    @Test
    public void testSplitInfiniteIndirectReference() {
        assertError("Infinite indirection",
                "1,!string/infinite_indirection,2", "1", "infinite", "<infinite>", "loop", "2");
    }

    @Test
    public void testLabelReferece() {
        assertTextArray("Indirect naviagte actions as more key",
                "!string/keyspec_indirect_navigate_actions",
                "!fixedColumnOrder!2",
                "!hasLabels!", "ActionPrevious|!code/key_action_previous",
                "!hasLabels!", "ActionNext|!code/key_action_next");
    }

    @Test
    public void testUselessUpperCaseSpecifier() {
        assertTextArray("EMPTY STRING",
                "!STRING/EMPTY_STRING", "!STRING/EMPTY_STRING");

        assertTextArray("SINGLE CHAR",
                "!STRING/SINGLE_CHAR", "!STRING/SINGLE_CHAR");
        assertTextArray("Escape and SINGLE CHAR",
                "\\\\!STRING/SINGLE_CHAR", "\\\\!STRING/SINGLE_CHAR");

        assertTextArray("MULTIPLE CHARS",
                "!STRING/MULTIPLE_CHARS", "!STRING/MULTIPLE_CHARS");

        assertTextArray("Literals and RESOURCES",
                "1,!STRING/MULTIPLE_CHARS,z", "1", "!STRING/MULTIPLE_CHARS", "z");
        assertTextArray("Multiple single RESOURCE chars and LABELS 2",
                "!STRING/SINGLE_CHAR,!STRING/SINGLE_LABEL,!STRING/ESCAPED_COMMA_ESCAPE",
                "!STRING/SINGLE_CHAR", "!STRING/SINGLE_LABEL", "!STRING/ESCAPED_COMMA_ESCAPE");

        assertTextArray("INDIRECT",
                "!STRING/INDIRECT_STRING", "!STRING/INDIRECT_STRING");
        assertTextArray("INDIRECT with literal",
                "1,!STRING/INDIRECT_STRING_WITH_LITERAL,2",
                "1", "!STRING/INDIRECT_STRING_WITH_LITERAL", "2");
        assertTextArray("INDIRECT2",
                "!STRING/INDIRECT2_STRING", "!STRING/INDIRECT2_STRING");

        assertTextArray("Upper indirect",
                "!string/upper_indirect_string", "!STRING/MULTIPLE_CHARS");
        assertTextArray("Upper indirect with literal",
                "1,!string/upper_indirect_string_with_literal,2",
                "1", "x", "!STRING/MULTIPLE_CHARS", "y", "2");
        assertTextArray("Upper indirect2",
                "!string/upper_indirect2_string", "!STRING/UPPER_INDIRECT_STRING");

        assertTextArray("UPPER INDIRECT",
                "!STRING/upper_INDIRECT_STRING", "!STRING/upper_INDIRECT_STRING");
        assertTextArray("Upper INDIRECT with literal",
                "1,!STRING/upper_INDIRECT_STRING_WITH_LITERAL,2",
                "1", "!STRING/upper_INDIRECT_STRING_WITH_LITERAL", "2");
        assertTextArray("Upper INDIRECT2",
                "!STRING/upper_INDIRECT2_STRING", "!STRING/upper_INDIRECT2_STRING");

        assertTextArray("INFINITE INDIRECTION",
                "1,!STRING/INFINITE_INDIRECTION,2", "1", "!STRING/INFINITE_INDIRECTION", "2");

        assertTextArray("Upper infinite indirection",
                "1,!string/upper_infinite_indirection,2",
                "1", "infinite", "!STRING/INFINITE_INDIRECTION", "loop", "2");
        assertTextArray("Upper INFINITE INDIRECTION",
                "1,!STRING/UPPER_INFINITE_INDIRECTION,2",
                "1", "!STRING/UPPER_INFINITE_INDIRECTION", "2");

        assertTextArray("INDIRECT NAVIGATE ACTIONS AS MORE KEY",
                "!STRING/INDIRECT_NAVIGATE_ACTIONS_AS_MORE_KEY",
                "!STRING/INDIRECT_NAVIGATE_ACTIONS_AS_MORE_KEY");
     }
}