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;
28
29@SmallTest
30public class SuggestedWordsTests extends AndroidTestCase {
31    public void testGetSuggestedWordsExcludingTypedWord() {
32        final String TYPED_WORD = "typed";
33        final int TYPED_WORD_FREQ = 5;
34        final int NUMBER_OF_ADDED_SUGGESTIONS = 5;
35        final ArrayList<SuggestedWordInfo> list = CollectionUtils.newArrayList();
36        list.add(new SuggestedWordInfo(TYPED_WORD, TYPED_WORD_FREQ,
37                SuggestedWordInfo.KIND_TYPED, null /* sourceDict */,
38                SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
39                SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */));
40        for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) {
41            list.add(new SuggestedWordInfo("" + i, 1, SuggestedWordInfo.KIND_CORRECTION,
42                    null /* sourceDict */,
43                    SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
44                    SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */));
45        }
46
47        final SuggestedWords words = new SuggestedWords(
48                list,
49                false /* typedWordValid */,
50                false /* willAutoCorrect */,
51                false /* isPunctuationSuggestions */,
52                false /* isObsoleteSuggestions */,
53                false /* isPrediction*/);
54        assertEquals(NUMBER_OF_ADDED_SUGGESTIONS + 1, words.size());
55        assertEquals("typed", words.getWord(0));
56        assertEquals(SuggestedWordInfo.KIND_TYPED, words.getInfo(0).mKind);
57        assertEquals("0", words.getWord(1));
58        assertEquals(SuggestedWordInfo.KIND_CORRECTION, words.getInfo(1).mKind);
59        assertEquals("4", words.getWord(5));
60        assertEquals(SuggestedWordInfo.KIND_CORRECTION, words.getInfo(5).mKind);
61
62        final SuggestedWords wordsWithoutTyped = words.getSuggestedWordsExcludingTypedWord();
63        assertEquals(words.size() - 1, wordsWithoutTyped.size());
64        assertEquals("0", wordsWithoutTyped.getWord(0));
65        assertEquals(SuggestedWordInfo.KIND_CORRECTION, wordsWithoutTyped.getInfo(0).mKind);
66    }
67
68    // Helper for testGetTransformedWordInfo
69    private SuggestedWordInfo createWordInfo(final String s) {
70        // Use 100 as the frequency because the numerical value does not matter as
71        // long as it's > 1 and < INT_MAX.
72        return new SuggestedWordInfo(s, 100,
73                SuggestedWordInfo.KIND_TYPED, null /* sourceDict */,
74                SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
75                SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */);
76    }
77
78    // Helper for testGetTransformedWordInfo
79    private SuggestedWordInfo transformWordInfo(final String info,
80            final int trailingSingleQuotesCount) {
81        return Suggest.getTransformedSuggestedWordInfo(createWordInfo(info),
82                Locale.ENGLISH, false /* isAllUpperCase */, false /* isFirstCharCapitalized */,
83                trailingSingleQuotesCount);
84    }
85
86    public void testGetTransformedSuggestedWordInfo() {
87        SuggestedWordInfo result = transformWordInfo("word", 0);
88        assertEquals(result.mWord, "word");
89        result = transformWordInfo("word", 1);
90        assertEquals(result.mWord, "word'");
91        result = transformWordInfo("word", 3);
92        assertEquals(result.mWord, "word'''");
93        result = transformWordInfo("didn't", 0);
94        assertEquals(result.mWord, "didn't");
95        result = transformWordInfo("didn't", 1);
96        assertEquals(result.mWord, "didn't");
97        result = transformWordInfo("didn't", 3);
98        assertEquals(result.mWord, "didn't''");
99    }
100}
101