summaryrefslogtreecommitdiff
path: root/tests/src/com/android/inputmethod/keyboard/layout/expected/ExpectedKey.java
blob: 2674a6a693f5fab1275f021521dc1ca7582b65b3 (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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 * 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.layout.expected;

import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.keyboard.internal.MoreKeySpec;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;

/**
 * This class represents an expected key.
 */
public class ExpectedKey {
    static ExpectedKey EMPTY_KEY = newInstance("");

    // A key that has a string label and may have "more keys".
    static ExpectedKey newInstance(final String label, final ExpectedKey... moreKeys) {
        return newInstance(label, label, moreKeys);
    }

    // A key that has a string label and a different output text and may have "more keys".
    static ExpectedKey newInstance(final String label, final String outputText,
            final ExpectedKey... moreKeys) {
        return newInstance(ExpectedKeyVisual.newInstance(label),
                ExpectedKeyOutput.newInstance(outputText), moreKeys);
    }

    // A key that has a string label and a code point output and may have "more keys".
    static ExpectedKey newInstance(final String label, final int code,
            final ExpectedKey... moreKeys) {
        return newInstance(ExpectedKeyVisual.newInstance(label),
                ExpectedKeyOutput.newInstance(code), moreKeys);
    }

    // A key that has an icon and an output text and may have "more keys".
    static ExpectedKey newInstance(final int iconId, final String outputText,
            final ExpectedKey... moreKeys) {
        return newInstance(ExpectedKeyVisual.newInstance(iconId),
                ExpectedKeyOutput.newInstance(outputText), moreKeys);
    }

    // A key that has an icon and a code point output and may have "more keys".
    static ExpectedKey newInstance(final int iconId, final int code,
            final ExpectedKey... moreKeys) {
        return newInstance(ExpectedKeyVisual.newInstance(iconId),
                ExpectedKeyOutput.newInstance(code), moreKeys);
    }

    static ExpectedKey newInstance(final ExpectedKeyVisual visual, final ExpectedKeyOutput output,
            final ExpectedKey... moreKeys) {
        if (moreKeys.length == 0) {
            return new ExpectedKey(visual, output);
        }
        // The more keys are the extra keys that the main keyboard key may have in its long press
        // popup keyboard.
        // The additional more keys can be defined independently from other more keys.
        // The position of the additional more keys in the long press popup keyboard can be
        // controlled by specifying special marker "%" in the usual more keys definitions.
        final ArrayList<ExpectedKey> moreKeysList = new ArrayList<>();
        final ArrayList<ExpectedAdditionalMoreKey> additionalMoreKeys = new ArrayList<>();
        int firstAdditionalMoreKeyIndex = -1;
        for (int index = 0; index < moreKeys.length; index++) {
            final ExpectedKey moreKey = moreKeys[index];
            if (moreKey instanceof ExpectedAdditionalMoreKey) {
                additionalMoreKeys.add((ExpectedAdditionalMoreKey) moreKey);
                if (firstAdditionalMoreKeyIndex < 0) {
                    firstAdditionalMoreKeyIndex = index;
                }
            } else {
                moreKeysList.add(moreKey);
            }
        }
        if (additionalMoreKeys.isEmpty()) {
            return new ExpectedKeyWithMoreKeys(visual, output, moreKeys);
        }
        final ExpectedKey[] moreKeysArray = moreKeysList.toArray(
                new ExpectedKey[moreKeysList.size()]);
        final ExpectedAdditionalMoreKey[] additionalMoreKeysArray = additionalMoreKeys.toArray(
                new ExpectedAdditionalMoreKey[additionalMoreKeys.size()]);
        return new ExpectedKeyWithMoreKeysAndAdditionalMoreKeys(
                visual, output, moreKeysArray, firstAdditionalMoreKeyIndex,
                additionalMoreKeysArray);
    }

    private static final ExpectedKey[] EMPTY_KEYS = new ExpectedKey[0];

    // The expected visual outlook of this key.
    private final ExpectedKeyVisual mVisual;
    // The expected output of this key.
    private final ExpectedKeyOutput mOutput;

    public final ExpectedKeyVisual getVisual() {
        return mVisual;
    }

    public final ExpectedKeyOutput getOutput() {
        return mOutput;
    }

    public ExpectedKey[] getMoreKeys() {
        // This key has no "more keys".
        return EMPTY_KEYS;
    }

    public ExpectedKey setMoreKeys(final ExpectedKey... moreKeys) {
        return newInstance(mVisual, mOutput, moreKeys);
    }

    public ExpectedKey setAdditionalMoreKeys(
            final ExpectedAdditionalMoreKey... additionalMoreKeys) {
        if (additionalMoreKeys.length == 0) {
            return this;
        }
        return new ExpectedKeyWithMoreKeysAndAdditionalMoreKeys(
                mVisual, mOutput, EMPTY_KEYS, 0 /* additionalMoreKeysIndex */, additionalMoreKeys);
    }

    public ExpectedKey setAdditionalMoreKeysIndex(final int additionalMoreKeysIndex) {
        if (additionalMoreKeysIndex == 0) {
            return this;
        }
        return new ExpectedKeyWithMoreKeysAndAdditionalMoreKeys(
                mVisual, mOutput, EMPTY_KEYS, additionalMoreKeysIndex);
    }

    protected ExpectedKey(final ExpectedKeyVisual visual, final ExpectedKeyOutput output) {
        mVisual = visual;
        mOutput = output;
    }

    public ExpectedKey toUpperCase(Locale locale) {
        return newInstance(mVisual.toUpperCase(locale), mOutput.toUpperCase(locale));
    }

    public ExpectedKey preserveCase() {
        final ExpectedKey[] moreKeys = getMoreKeys();
        final ExpectedKey[] casePreservedMoreKeys = new ExpectedKey[moreKeys.length];
        for (int index = 0; index < moreKeys.length; index++) {
            final ExpectedKey moreKey = moreKeys[index];
            casePreservedMoreKeys[index] = newInstance(
                    moreKey.getVisual().preserveCase(), moreKey.getOutput().preserveCase());
        }
        return newInstance(
                getVisual().preserveCase(), getOutput().preserveCase(), casePreservedMoreKeys);
    }

    public boolean equalsTo(final Key key) {
        // This key has no "more keys".
        return mVisual.equalsTo(key) && mOutput.equalsTo(key) && key.getMoreKeys() == null;
    }

    public boolean equalsTo(final MoreKeySpec moreKeySpec) {
        return mVisual.equalsTo(moreKeySpec) && mOutput.equalsTo(moreKeySpec);
    }

    @Override
    public boolean equals(final Object object) {
        if (object instanceof ExpectedKey) {
            final ExpectedKey key = (ExpectedKey) object;
            return mVisual.equalsTo(key.mVisual) && mOutput.equalsTo(key.mOutput)
                    && Arrays.equals(getMoreKeys(), key.getMoreKeys());
        }
        return false;
    }

    private static int hashCode(final Object... objects) {
        return Arrays.hashCode(objects);
    }

    @Override
    public int hashCode() {
        return hashCode(mVisual, mOutput, getMoreKeys());
    }

    @Override
    public String toString() {
        if (mVisual.equalsTo(mOutput)) {
            return mVisual.toString();
        }
        return mVisual + "|" + mOutput;
    }

    /**
     * This class represents an expected "additional more key".
     *
     * The additional more keys can be defined independently from other more keys. The position of
     * the additional more keys in the long press popup keyboard can be controlled by specifying
     * special marker "%" in the usual more keys definitions.
     */
    public static class ExpectedAdditionalMoreKey extends ExpectedKey {
        public static ExpectedAdditionalMoreKey newInstance(final String label) {
            return new ExpectedAdditionalMoreKey(ExpectedKeyVisual.newInstance(label),
                    ExpectedKeyOutput.newInstance(label));
        }

        public static ExpectedAdditionalMoreKey newInstance(final ExpectedKey key) {
            return new ExpectedAdditionalMoreKey(key.getVisual(), key.getOutput());
        }

        ExpectedAdditionalMoreKey(final ExpectedKeyVisual visual, final ExpectedKeyOutput output) {
            super(visual, output);
        }

        @Override
        public ExpectedAdditionalMoreKey toUpperCase(final Locale locale) {
            final ExpectedKey upperCaseKey = super.toUpperCase(locale);
            return new ExpectedAdditionalMoreKey(
                    upperCaseKey.getVisual(), upperCaseKey.getOutput());
        }
    }

    /**
     * This class represents an expected key that has "more keys".
     */
    private static class ExpectedKeyWithMoreKeys extends ExpectedKey {
        private final ExpectedKey[] mMoreKeys;

        ExpectedKeyWithMoreKeys(final ExpectedKeyVisual visual, final ExpectedKeyOutput output,
                final ExpectedKey... moreKeys) {
            super(visual, output);
            mMoreKeys = moreKeys;
        }

        @Override
        public ExpectedKey toUpperCase(final Locale locale) {
            final ExpectedKey[] upperCaseMoreKeys = new ExpectedKey[mMoreKeys.length];
            for (int i = 0; i < mMoreKeys.length; i++) {
                upperCaseMoreKeys[i] = mMoreKeys[i].toUpperCase(locale);
            }
            return newInstance(getVisual().toUpperCase(locale), getOutput().toUpperCase(locale),
                    upperCaseMoreKeys);
        }

        @Override
        public ExpectedKey[] getMoreKeys() {
            return mMoreKeys;
        }

        @Override
        public ExpectedKey setAdditionalMoreKeys(
                final ExpectedAdditionalMoreKey... additionalMoreKeys) {
            if (additionalMoreKeys.length == 0) {
                return this;
            }
            return new ExpectedKeyWithMoreKeysAndAdditionalMoreKeys(
                    getVisual(), getOutput(), mMoreKeys, 0 /* additionalMoreKeysIndex */,
                    additionalMoreKeys);
        }

        @Override
        public ExpectedKey setAdditionalMoreKeysIndex(final int additionalMoreKeysIndex) {
            if (additionalMoreKeysIndex == 0) {
                return this;
            }
            return new ExpectedKeyWithMoreKeysAndAdditionalMoreKeys(
                    getVisual(), getOutput(), mMoreKeys, additionalMoreKeysIndex);
        }

        @Override
        public boolean equalsTo(final Key key) {
            if (getVisual().equalsTo(key) && getOutput().equalsTo(key)) {
                final MoreKeySpec[] moreKeySpecs = key.getMoreKeys();
                final ExpectedKey[] moreKeys = getMoreKeys();
                // This key should have at least one "more key".
                if (moreKeySpecs == null || moreKeySpecs.length != moreKeys.length) {
                    return false;
                }
                for (int index = 0; index < moreKeySpecs.length; index++) {
                    if (!moreKeys[index].equalsTo(moreKeySpecs[index])) {
                        return false;
                    }
                }
                return true;
            }
            return false;
        }

        @Override
        public boolean equalsTo(final MoreKeySpec moreKeySpec) {
            // MoreKeySpec has no "more keys".
            return false;
        }

        @Override
        public String toString() {
            return super.toString() + "^" + Arrays.toString(getMoreKeys());
        }
    }

    /**
     * This class represents an expected key that has "more keys" and "additional more keys".
     */
    private static final class ExpectedKeyWithMoreKeysAndAdditionalMoreKeys
            extends ExpectedKeyWithMoreKeys {
        private final ExpectedAdditionalMoreKey[] mAdditionalMoreKeys;
        private final int mAdditionalMoreKeysIndex;

        ExpectedKeyWithMoreKeysAndAdditionalMoreKeys(final ExpectedKeyVisual visual,
                final ExpectedKeyOutput output, final ExpectedKey[] moreKeys,
                final int additionalMoreKeysIndex,
                final ExpectedAdditionalMoreKey... additionalMoreKeys) {
            super(visual, output, moreKeys);
            mAdditionalMoreKeysIndex = additionalMoreKeysIndex;
            mAdditionalMoreKeys = additionalMoreKeys;
        }

        @Override
        public ExpectedKey setMoreKeys(final ExpectedKey... moreKeys) {
            return new ExpectedKeyWithMoreKeysAndAdditionalMoreKeys(
                    getVisual(), getOutput(), moreKeys, mAdditionalMoreKeysIndex,
                    mAdditionalMoreKeys);
        }

        @Override
        public ExpectedKey setAdditionalMoreKeys(
                final ExpectedAdditionalMoreKey... additionalMoreKeys) {
            return new ExpectedKeyWithMoreKeysAndAdditionalMoreKeys(
                    getVisual(), getOutput(), super.getMoreKeys(), mAdditionalMoreKeysIndex,
                    additionalMoreKeys);
        }

        @Override
        public ExpectedKey setAdditionalMoreKeysIndex(final int additionalMoreKeysIndex) {
            return new ExpectedKeyWithMoreKeysAndAdditionalMoreKeys(
                    getVisual(), getOutput(), super.getMoreKeys(), additionalMoreKeysIndex,
                    mAdditionalMoreKeys);
        }

        @Override
        public ExpectedKey toUpperCase(final Locale locale) {
            final ExpectedKey[] moreKeys = super.getMoreKeys();
            final ExpectedKey[] upperCaseMoreKeys = new ExpectedKey[moreKeys.length];
            for (int i = 0; i < moreKeys.length; i++) {
                upperCaseMoreKeys[i] = moreKeys[i].toUpperCase(locale);
            }
            final ExpectedAdditionalMoreKey[] upperCaseAdditionalMoreKeys =
                    new ExpectedAdditionalMoreKey[mAdditionalMoreKeys.length];
            for (int i = 0; i < mAdditionalMoreKeys.length; i++) {
                upperCaseAdditionalMoreKeys[i] = mAdditionalMoreKeys[i].toUpperCase(locale);
            }
            return new ExpectedKeyWithMoreKeysAndAdditionalMoreKeys(
                    getVisual().toUpperCase(locale), getOutput().toUpperCase(locale),
                    upperCaseMoreKeys, mAdditionalMoreKeysIndex, upperCaseAdditionalMoreKeys);
        }

        @Override
        public ExpectedKey[] getMoreKeys() {
            final ExpectedKey[] moreKeys = super.getMoreKeys();
            final ExpectedKey[] edittedMoreKeys = Arrays.copyOf(
                    moreKeys, moreKeys.length + mAdditionalMoreKeys.length);
            System.arraycopy(edittedMoreKeys, mAdditionalMoreKeysIndex,
                    edittedMoreKeys, mAdditionalMoreKeysIndex + mAdditionalMoreKeys.length,
                    moreKeys.length - mAdditionalMoreKeysIndex);
            System.arraycopy(mAdditionalMoreKeys, 0, edittedMoreKeys, mAdditionalMoreKeysIndex,
                    mAdditionalMoreKeys.length);
            return edittedMoreKeys;
        }
    }
}