SuggestedWordsTests.java revision 5b5ed3d6092ea539d8cfebd786c63ec0c784040b
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.latin;
18
19import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
20
21import android.test.AndroidTestCase;
22import android.test.suitebuilder.annotation.SmallTest;
23
24import com.android.inputmethod.latin.utils.CollectionUtils;
25
26import java.util.ArrayList;
27import java.util.Locale;
28import java.util.Random;
29
30@SmallTest
31public class SuggestedWordsTests extends AndroidTestCase {
32    public void testGetSuggestedWordsExcludingTypedWord() {
33        final String TYPED_WORD = "typed";
34        final int TYPED_WORD_FREQ = 5;
35        final int NUMBER_OF_ADDED_SUGGESTIONS = 5;
36        final ArrayList<SuggestedWordInfo> list = CollectionUtils.newArrayList();
37        list.add(new SuggestedWordInfo(TYPED_WORD, TYPED_WORD_FREQ,
38                SuggestedWordInfo.KIND_TYPED, null /* sourceDict */,
39                SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
40                SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */));
41        for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) {
42            list.add(new SuggestedWordInfo("" + i, 1, SuggestedWordInfo.KIND_CORRECTION,
43                    null /* sourceDict */,
44                    SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
45                    SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */));
46        }
47
48        final SuggestedWords words = new SuggestedWords(
49                list,
50                false /* typedWordValid */,
51                false /* willAutoCorrect */,
52                false /* isPunctuationSuggestions */,
53                false /* isObsoleteSuggestions */,
54                false /* isPrediction*/);
55        assertEquals(NUMBER_OF_ADDED_SUGGESTIONS + 1, words.size());
56        assertEquals("typed", words.getWord(0));
57        assertEquals(SuggestedWordInfo.KIND_TYPED, words.getInfo(0).mKind);
58        assertEquals("0", words.getWord(1));
59        assertEquals(SuggestedWordInfo.KIND_CORRECTION, words.getInfo(1).mKind);
60        assertEquals("4", words.getWord(5));
61        assertEquals(SuggestedWordInfo.KIND_CORRECTION, words.getInfo(5).mKind);
62
63        final SuggestedWords wordsWithoutTyped = words.getSuggestedWordsExcludingTypedWord();
64        assertEquals(words.size() - 1, wordsWithoutTyped.size());
65        assertEquals("0", wordsWithoutTyped.getWord(0));
66        assertEquals(SuggestedWordInfo.KIND_CORRECTION, wordsWithoutTyped.getInfo(0).mKind);
67    }
68
69    // Helper for testGetTransformedWordInfo
70    private SuggestedWordInfo createWordInfo(final String s) {
71        // Use 100 as the frequency because the numerical value does not matter as
72        // long as it's > 1 and < INT_MAX.
73        return new SuggestedWordInfo(s, 100,
74                SuggestedWordInfo.KIND_TYPED, null /* sourceDict */,
75                SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
76                new Random().nextInt(1000000) /* autoCommitFirstWordConfidence */);
77    }
78
79    // Helper for testGetTransformedWordInfo
80    private SuggestedWordInfo transformWordInfo(final String info,
81            final int trailingSingleQuotesCount) {
82        final SuggestedWordInfo suggestedWordInfo = createWordInfo(info);
83        final SuggestedWordInfo returnedWordInfo =
84                Suggest.getTransformedSuggestedWordInfo(suggestedWordInfo,
85                Locale.ENGLISH, false /* isAllUpperCase */, false /* isFirstCharCapitalized */,
86                trailingSingleQuotesCount);
87        assertEquals(suggestedWordInfo.mAutoCommitFirstWordConfidence,
88                returnedWordInfo.mAutoCommitFirstWordConfidence);
89        return returnedWordInfo;
90    }
91
92    public void testGetTransformedSuggestedWordInfo() {
93        SuggestedWordInfo result = transformWordInfo("word", 0);
94        assertEquals(result.mWord, "word");
95        result = transformWordInfo("word", 1);
96        assertEquals(result.mWord, "word'");
97        result = transformWordInfo("word", 3);
98        assertEquals(result.mWord, "word'''");
99        result = transformWordInfo("didn't", 0);
100        assertEquals(result.mWord, "didn't");
101        result = transformWordInfo("didn't", 1);
102        assertEquals(result.mWord, "didn't");
103        result = transformWordInfo("didn't", 3);
104        assertEquals(result.mWord, "didn't''");
105    }
106}
107