summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/browser/autocomplete/SuggestedTextControllerTest.java
blob: f162e3b08636918077e92e36df90009cec80c1d0 (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
 * Copyright (C) 2011 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.browser.autocomplete;

import com.android.browser.autocomplete.SuggestedTextController.TextOwner;

import android.graphics.Color;
import android.os.Parcelable;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.view.AbsSavedState;

/**
 * Test cases for {@link SuggestedTextController}.
 */
@SmallTest
public class SuggestedTextControllerTest extends AndroidTestCase {

    // these two must have a common prefix (but not be identical):
    private static final String RUBY_MURRAY = "ruby murray";
    private static final String RUBY_TUESDAY = "ruby tuesday";
    private static final String EXTRA_USER_TEXT = " curry";
    // no common prefix with the top two above:
    private static final String TOD_SLOAN = "tod sloan";

    private SuggestedTextController mController;
    private SpannableStringBuilder mString;

    private SuggestedTextController m2ndController;
    private SpannableStringBuilder m2ndString;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        mString = new SpannableStringBuilder();
        Selection.setSelection(mString, 0); // position cursor
        mController = new SuggestedTextController(new BufferTextOwner(mString), Color.GRAY);
        checkInvariant();
    }

    private void create2ndController() {
        m2ndString = new SpannableStringBuilder();
        Selection.setSelection(m2ndString, 0); // position cursor
        m2ndController = new SuggestedTextController(new BufferTextOwner(m2ndString), Color.GRAY);
        check2ndInvariant();
    }

    private int cursorPos(Spannable string) {
        int selStart = Selection.getSelectionStart(string);
        int selEnd = Selection.getSelectionEnd(string);
        assertEquals("Selection has non-zero length", selStart, selEnd);
        return selEnd;
    }

    private int cursorPos() {
        return cursorPos(mString);
    }

    private void insertAtCursor(String text) {
        mString.insert(cursorPos(), text);
        checkInvariant();
    }

    private void insertAtCursor(char ch) {
        insertAtCursor(Character.toString(ch));
    }

    private void insertAt2ndCursor(String text) {
        m2ndString.insert(cursorPos(m2ndString), text);
        check2ndInvariant();
    }

    private void insertAt2ndCursor(char ch) {
        insertAt2ndCursor(Character.toString(ch));
    }

    private void deleteBeforeCursor(int count) {
        int pos = cursorPos();
        count = Math.min(pos, count);
        mString.delete(pos - count, pos);
        checkInvariant();
    }

    private void replaceSelection(String withThis) {
        mString.replace(Selection.getSelectionStart(mString),
                Selection.getSelectionEnd(mString), withThis);
        checkInvariant();
    }

    private void setSuggested(String suggested) {
        mController.setSuggestedText(suggested);
        checkInvariant();
    }

    private void set2ndSuggested(String suggested) {
        m2ndController.setSuggestedText(suggested);
        check2ndInvariant();
    }

    private void checkInvariant() {
        mController.checkInvariant(mString);
    }

    private void check2ndInvariant() {
        m2ndController.checkInvariant(m2ndString);
    }

    private void assertUserEntered(String expected, SuggestedTextController controller) {
        assertEquals("User entered text not as expected", expected, controller.getUserText());
    }

    private void assertUserEntered(String expected) {
        assertUserEntered(expected, mController);
    }

    private void assertBuffer(String expected, Editable string) {
        assertEquals("Buffer contents not as expected", expected, string.toString());
    }

    private void assertBuffer(String expected) {
        assertBuffer(expected, mString);
    }

    private void assertCursorPos(int where, Spannable string) {
        assertEquals("Cursor not at expected position", where, cursorPos(string));
    }

    private void assertCursorPos(int where) {
        assertCursorPos(where, mString);
    }

    private static final String commonPrefix(String a, String b) {
        int pos = 0;
        while (a.charAt(pos) == b.charAt(pos)) {
            pos++;
        }
        assertTrue("No common prefix between '" + a + "' and '" + b + "'", pos > 0);
        return a.substring(0, pos);
    }

    public void testTypeNoSuggested() {
        for (int i = 0; i < RUBY_MURRAY.length(); ++i) {
            assertCursorPos(i);
            assertUserEntered(RUBY_MURRAY.substring(0, i));
            assertBuffer(RUBY_MURRAY.substring(0, i));
            insertAtCursor(RUBY_MURRAY.substring(i, i + 1));
        }
    }

    public void testTypeSuggested() {
        setSuggested(RUBY_MURRAY);
        assertCursorPos(0);
        assertBuffer(RUBY_MURRAY);
        for (int i = 0; i < RUBY_MURRAY.length(); ++i) {
            assertCursorPos(i);
            assertUserEntered(RUBY_MURRAY.substring(0, i));
            assertBuffer(RUBY_MURRAY);
            insertAtCursor(RUBY_MURRAY.charAt(i));
        }
    }

    public void testSetSuggestedAfterTextEntry() {
        final int count = RUBY_MURRAY.length() / 2;
        for (int i = 0; i < count; ++i) {
            assertCursorPos(i);
            assertUserEntered(RUBY_MURRAY.substring(0, i));
            insertAtCursor(RUBY_MURRAY.substring(i, i + 1));
        }
        setSuggested(RUBY_MURRAY);
        assertUserEntered(RUBY_MURRAY.substring(0, count));
        assertBuffer(RUBY_MURRAY);
    }

    public void testTypeSuggestedUpperCase() {
        setSuggested(RUBY_MURRAY);
        assertCursorPos(0);
        for (int i = 0; i < RUBY_MURRAY.length(); ++i) {
            assertCursorPos(i);
            assertUserEntered(RUBY_MURRAY.substring(0, i).toUpperCase());
            assertTrue("Buffer doesn't contain suggested text",
                    RUBY_MURRAY.equalsIgnoreCase(mString.toString()));
            insertAtCursor(Character.toUpperCase(RUBY_MURRAY.charAt(i)));
        }
    }

    public void testChangeSuggestedText() {
        String pref = commonPrefix(RUBY_MURRAY, RUBY_TUESDAY);
        setSuggested(RUBY_MURRAY);
        insertAtCursor(pref);
        assertBuffer(RUBY_MURRAY);
        assertUserEntered(pref);
        setSuggested(RUBY_TUESDAY);
        assertBuffer(RUBY_TUESDAY);
        assertUserEntered(pref);
    }

    public void testTypeNonSuggested() {
        setSuggested(RUBY_MURRAY);
        insertAtCursor(RUBY_MURRAY.charAt(0));
        assertBuffer(RUBY_MURRAY);
        insertAtCursor('x');
        assertBuffer("rx");
    }

    public void testTypeNonSuggestedThenNewSuggestion() {
        final String pref = commonPrefix(RUBY_MURRAY, RUBY_TUESDAY);
        setSuggested(RUBY_MURRAY);
        assertCursorPos(0);
        insertAtCursor(pref);
        assertCursorPos(pref.length());
        assertUserEntered(pref);
        insertAtCursor(RUBY_TUESDAY.charAt(pref.length()));
        assertBuffer(RUBY_TUESDAY.substring(0, pref.length() + 1));
        setSuggested(RUBY_TUESDAY);
        assertBuffer(RUBY_TUESDAY);
    }

    public void testChangeSuggestedToNonUserEntered() {
        final String half = RUBY_MURRAY.substring(0, RUBY_MURRAY.length() / 2);
        setSuggested(RUBY_MURRAY);
        insertAtCursor(half);
        setSuggested(TOD_SLOAN);
        assertUserEntered(half);
        assertBuffer(half);
    }

    public void testChangeSuggestedToUserEntered() {
        setSuggested(RUBY_MURRAY);
        insertAtCursor(TOD_SLOAN);
        setSuggested(TOD_SLOAN);
        assertUserEntered(TOD_SLOAN);
        assertBuffer(TOD_SLOAN);
    }

    public void testChangeSuggestedToEmpty() {
        final String half = RUBY_MURRAY.substring(0, RUBY_MURRAY.length() / 2);
        setSuggested(RUBY_MURRAY);
        insertAtCursor(half);
        setSuggested(null);
        assertUserEntered(half);
        assertBuffer(half);
    }

    public void testChangeSuggestedToEmptyFromUserEntered() {
        setSuggested(RUBY_MURRAY);
        insertAtCursor(RUBY_MURRAY);
        setSuggested(null);
        assertUserEntered(RUBY_MURRAY);
        assertBuffer(RUBY_MURRAY);
    }

    public void typeNonSuggestedThenDelete() {
        final String half = RUBY_MURRAY.substring(0, RUBY_MURRAY.length() / 2);
        assertCursorPos(0);
        insertAtCursor(half);
        assertCursorPos(half.length());
        setSuggested(RUBY_MURRAY);
        insertAtCursor('x');
        assertBuffer(half + "x");
        deleteBeforeCursor(1);
        assertUserEntered(half);
        assertBuffer(RUBY_MURRAY);
    }

    public void testDeleteMultipleFromSuggested() {
        final String twoThirds = RUBY_MURRAY.substring(0, (RUBY_MURRAY.length() * 2) / 3);
        setSuggested(RUBY_MURRAY);
        insertAtCursor(twoThirds);
        assertCursorPos(twoThirds.length());
        // select some of the text just entered:
        Selection.setSelection(mString, RUBY_MURRAY.length() / 3, twoThirds.length());
        // and delete it:
        replaceSelection("");
        assertCursorPos(RUBY_MURRAY.length() / 3);
        assertUserEntered(RUBY_MURRAY.substring(0, RUBY_MURRAY.length() / 3));
        assertBuffer(RUBY_MURRAY);
    }

    public void testDeleteMultipleToFormSuggested() {
        final String pref = commonPrefix(RUBY_TUESDAY, RUBY_MURRAY);
        final int extra = (RUBY_TUESDAY.length() - pref.length()) / 2;
        setSuggested(RUBY_MURRAY);
        insertAtCursor(RUBY_TUESDAY.substring(0, pref.length() + extra));
        assertCursorPos(pref.length() + extra);
        // select and delete extra characters, leaving just prefix
        Selection.setSelection(mString, pref.length(), pref.length() + extra);
        replaceSelection("");
        assertCursorPos(pref.length());
        assertBuffer(RUBY_MURRAY);
        assertUserEntered(pref);
    }

    public void testBackspaceWithinUserTextFromSuggested() {
        StringBuffer half = new StringBuffer(RUBY_MURRAY.substring(0, RUBY_MURRAY.length() / 2));
        insertAtCursor(half.toString());
        int backSpaceFrom = half.length() / 2;
        Selection.setSelection(mString, backSpaceFrom);
        deleteBeforeCursor(1);
        assertCursorPos(backSpaceFrom - 1);
        half.delete(backSpaceFrom - 1, backSpaceFrom);
        assertUserEntered(half.toString());
        assertBuffer(half.toString());
    }

    public void testInsertWithinUserTextToFormSuggested() {
        final String half = RUBY_MURRAY.substring(0, RUBY_MURRAY.length() / 2);
        StringBuffer initial = new StringBuffer(half);
        int pos = initial.length() / 2;
        char toInsert = initial.charAt(pos);
        initial.delete(pos, pos + 1);
        insertAtCursor(initial.toString());
        setSuggested(RUBY_MURRAY);
        assertUserEntered(initial.toString());
        assertBuffer(initial.toString());
        Selection.setSelection(mString, pos);
        insertAtCursor(toInsert);
        assertCursorPos(pos + 1);
        assertUserEntered(half);
        assertBuffer(RUBY_MURRAY);
    }

    public void testEnterTextBeyondSuggested() {
        setSuggested(RUBY_MURRAY);
        int i = RUBY_MURRAY.length() / 2;
        insertAtCursor(RUBY_MURRAY.substring(0, i));
        String query = RUBY_MURRAY + EXTRA_USER_TEXT;
        for (; i < query.length(); ++i) {
            assertUserEntered(query.substring(0, i));
            if (i <= RUBY_MURRAY.length()) {
                assertBuffer(RUBY_MURRAY);
            }
            insertAtCursor(query.charAt(i));
        }
        assertUserEntered(query);
    }

    public void testDeleteFromLongerThanSuggested() {
        setSuggested(RUBY_MURRAY);
        final String entered = RUBY_MURRAY + EXTRA_USER_TEXT;
        insertAtCursor(entered);
        for (int i = entered.length(); i > (RUBY_MURRAY.length() / 2); --i) {
            assertCursorPos(i);
            assertUserEntered(entered.substring(0, i));
            if (i <= RUBY_MURRAY.length()) {
                assertBuffer(RUBY_MURRAY);
            }
            deleteBeforeCursor(1);
        }
    }

    public void testReplaceWithShorterToFormSuggested() {
        final String pref = commonPrefix(RUBY_TUESDAY, RUBY_MURRAY);
        final int extra = (RUBY_TUESDAY.length() - pref.length()) / 2;
        setSuggested(RUBY_MURRAY);
        insertAtCursor(RUBY_TUESDAY.substring(0, pref.length() + extra));
        assertCursorPos(pref.length() + extra);
        // select and replace extra characters, to match suggested
        Selection.setSelection(mString, pref.length(), pref.length() + extra);
        replaceSelection(RUBY_MURRAY.substring(pref.length(), pref.length() + extra - 1));
        assertBuffer(RUBY_MURRAY);
        assertUserEntered(RUBY_MURRAY.substring(0, pref.length() + extra - 1));
    }

    public void testReplaceWithSameLengthToFormSuggested() {
        final String pref = commonPrefix(RUBY_TUESDAY, RUBY_MURRAY);
        final int extra = (RUBY_TUESDAY.length() - pref.length()) / 2;
        setSuggested(RUBY_MURRAY);
        insertAtCursor(RUBY_TUESDAY.substring(0, pref.length() + extra));
        assertCursorPos(pref.length() + extra);
        // select and replace extra characters, to match suggested
        Selection.setSelection(mString, pref.length(), pref.length() + extra);
        replaceSelection(RUBY_MURRAY.substring(pref.length(), pref.length() + extra));
        assertBuffer(RUBY_MURRAY);
        assertUserEntered(RUBY_MURRAY.substring(0, pref.length() + extra));
    }

    public void testReplaceWithLongerToFormSuggested() {
        final String pref = commonPrefix(RUBY_TUESDAY, RUBY_MURRAY);
        final int extra = (RUBY_TUESDAY.length() - pref.length()) / 2;
        setSuggested(RUBY_MURRAY);
        insertAtCursor(RUBY_TUESDAY.substring(0, pref.length() + extra));
        assertCursorPos(pref.length() + extra);
        // select and replace extra characters, to match suggested
        Selection.setSelection(mString, pref.length(), pref.length() + extra);
        replaceSelection(RUBY_MURRAY.substring(pref.length(), pref.length() + extra + 1));
        assertBuffer(RUBY_MURRAY);
        assertUserEntered(RUBY_MURRAY.substring(0, pref.length() + extra + 1));
    }

    public void testMoveCursorIntoSuggested() {
        final String half = RUBY_MURRAY.substring(0, RUBY_MURRAY.length() / 2);
        insertAtCursor(half);
        setSuggested(RUBY_MURRAY);
        assertCursorPos(half.length());
        Selection.setSelection(mString, half.length() + 1);
        checkInvariant();
        assertUserEntered(RUBY_MURRAY);
    }

    public void testMoveCursorWithinUserEntered() {
        final String half = RUBY_MURRAY.substring(0, RUBY_MURRAY.length() / 2);
        insertAtCursor(half);
        setSuggested(RUBY_MURRAY);
        assertCursorPos(half.length());
        Selection.setSelection(mString, half.length() - 1);
        checkInvariant();
        assertUserEntered(half);
    }

    public void testSelectWithinSuggested() {
        final String half = RUBY_MURRAY.substring(0, RUBY_MURRAY.length() / 2);
        insertAtCursor(half);
        setSuggested(RUBY_MURRAY);
        assertCursorPos(half.length());
        Selection.setSelection(mString, half.length() + 1, half.length() + 2);
        checkInvariant();
        assertUserEntered(RUBY_MURRAY);
    }

    public void testSelectStraddlingSuggested() {
        final String half = RUBY_MURRAY.substring(0, RUBY_MURRAY.length() / 2);
        insertAtCursor(half);
        setSuggested(RUBY_MURRAY);
        assertCursorPos(half.length());
        Selection.setSelection(mString, half.length() - 1, half.length() + 1);
        checkInvariant();
        assertUserEntered(RUBY_MURRAY);
    }

    public void testSaveAndRestoreNoText() {
        create2ndController();
        Parcelable state = mController.saveInstanceState(AbsSavedState.EMPTY_STATE);
        m2ndController.restoreInstanceState(state);
        check2ndInvariant();
        assertBuffer("", m2ndString);
    }

    public void testSaveAndRestoreWithSuggestedText() {
        create2ndController();
        setSuggested(TOD_SLOAN);
        Parcelable state = mController.saveInstanceState(AbsSavedState.EMPTY_STATE);
        m2ndController.restoreInstanceState(state);
        check2ndInvariant();
        assertBuffer(TOD_SLOAN, m2ndString);
        assertUserEntered("", m2ndController);
    }

    public void testSaveAndRestoreWithUserEnteredAndSuggestedText() {
        final String half = TOD_SLOAN.substring(0, TOD_SLOAN.length() / 2);
        create2ndController();
        setSuggested(TOD_SLOAN);
        insertAtCursor(half);
        Parcelable state = mController.saveInstanceState(AbsSavedState.EMPTY_STATE);
        m2ndController.restoreInstanceState(state);
        check2ndInvariant();
        assertBuffer(TOD_SLOAN, m2ndString);
        assertUserEntered(half, m2ndController);
        assertCursorPos(half.length(), m2ndString);
    }

    public void testSaveAndRestoreWithNonSuggested() {
        final String half = TOD_SLOAN.substring(0, TOD_SLOAN.length() / 2);
        create2ndController();
        setSuggested(RUBY_MURRAY);
        insertAtCursor(half);
        Parcelable state = mController.saveInstanceState(AbsSavedState.EMPTY_STATE);
        m2ndController.restoreInstanceState(state);
        check2ndInvariant();
        assertBuffer(half, m2ndString);
        assertUserEntered(half, m2ndController);
        assertCursorPos(half.length(), m2ndString);
    }

    public void testSaveAndRestoreThenTypeSuggested() {
        final String half = TOD_SLOAN.substring(0, TOD_SLOAN.length() / 2);
        create2ndController();
        set2ndSuggested(TOD_SLOAN);
        insertAt2ndCursor(half);
        insertAt2ndCursor('x');
        Parcelable state = m2ndController.saveInstanceState(AbsSavedState.EMPTY_STATE);
        mController.restoreInstanceState(state);
        assertCursorPos(half.length() + 1);
        // delete the x
        deleteBeforeCursor(1);
        assertCursorPos(half.length());
        assertBuffer(TOD_SLOAN);
        assertUserEntered(half);
    }

    public void testSuspendAndResumeCursorProcessing() {
        final String half = TOD_SLOAN.substring(0, TOD_SLOAN.length() / 2);
        setSuggested(TOD_SLOAN);
        insertAtCursor(half);
        mController.suspendCursorMovementHandling();
        Selection.setSelection(mString, TOD_SLOAN.length());
        Selection.setSelection(mString, half.length());
        mController.resumeCursorMovementHandlingAndApplyChanges();
        assertCursorPos(half.length());
        assertUserEntered(half);
        assertBuffer(TOD_SLOAN);
    }

    private static class BufferTextOwner implements TextOwner {

        private final Editable mBuffer;

        public BufferTextOwner(Editable buffer) {
            mBuffer = buffer;
        }

        public void addTextChangedListener(TextWatcher watcher) {
            mBuffer.setSpan(watcher , 0, mBuffer.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        }

        public void removeTextChangedListener(TextWatcher watcher) {
            mBuffer.removeSpan(watcher);
        }

        public Editable getText() {
            return mBuffer;
        }

        public void setText(String text) {
            mBuffer.replace(0, mBuffer.length(), text);
        }

    }

}