1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.latin;
18
19import android.test.AndroidTestCase;
20import com.android.inputmethod.latin.tests.R;
21import java.util.Locale;
22
23public class UserBigramTests extends AndroidTestCase {
24    private static final String TAG = "UserBigramTests";
25
26    private static final int SUGGESTION_STARTS = 6;
27    private static final int MAX_DATA = 20;
28    private static final int DELETE_DATA = 10;
29
30    private SuggestHelper sh;
31
32    @Override
33    protected void setUp() {
34        int[] resId = new int[] { R.raw.test };
35        sh = new SuggestHelper(TAG, getTestContext(), resId, MAX_DATA, DELETE_DATA);
36    }
37
38    /************************** Tests ************************/
39
40    /**
41     * Test suggestion started at right time
42     */
43    public void testUserBigram() {
44        for (int i = 0; i < SUGGESTION_STARTS; i++) sh.addToUserBigram(pair1);
45        for (int i = 0; i < (SUGGESTION_STARTS - 1); i++) sh.addToUserBigram(pair2);
46
47        assertTrue(sh.isUserBigramSuggestion("user", 'b', "bigram"));
48        assertFalse(sh.isUserBigramSuggestion("android", 'p', "platform"));
49    }
50
51    /**
52     * Test loading correct (locale) bigrams
53     */
54    public void testOpenAndClose() {
55        for (int i = 0; i < SUGGESTION_STARTS; i++) sh.addToUserBigram(pair1);
56        assertTrue(sh.isUserBigramSuggestion("user", 'b', "bigram"));
57
58        // change to fr_FR
59        sh.changeUserBigramLocale(getTestContext(), Locale.FRANCE);
60        for (int i = 0; i < SUGGESTION_STARTS; i++) sh.addToUserBigram(pair3);
61        assertTrue(sh.isUserBigramSuggestion("locale", 'f', "france"));
62        assertFalse(sh.isUserBigramSuggestion("user", 'b', "bigram"));
63
64        // change back to en_US
65        sh.changeUserBigramLocale(getTestContext(), Locale.US);
66        assertFalse(sh.isUserBigramSuggestion("locale", 'f', "france"));
67        assertTrue(sh.isUserBigramSuggestion("user", 'b', "bigram"));
68    }
69
70    /**
71     * Test data gets pruned when it is over maximum
72     */
73    public void testPruningData() {
74        for (int i = 0; i < SUGGESTION_STARTS; i++) sh.addToUserBigram(sentence0);
75        sh.flushUserBigrams();
76        assertTrue(sh.isUserBigramSuggestion("Hello", 'w', "world"));
77
78        sh.addToUserBigram(sentence1);
79        sh.addToUserBigram(sentence2);
80        assertTrue(sh.isUserBigramSuggestion("Hello", 'w', "world"));
81
82        // pruning should happen
83        sh.addToUserBigram(sentence3);
84        sh.addToUserBigram(sentence4);
85
86        // trying to reopen database to check pruning happened in database
87        sh.changeUserBigramLocale(getTestContext(), Locale.US);
88        assertFalse(sh.isUserBigramSuggestion("Hello", 'w', "world"));
89    }
90
91    final String[] pair1 = new String[] {"user", "bigram"};
92    final String[] pair2 = new String[] {"android","platform"};
93    final String[] pair3 = new String[] {"locale", "france"};
94    final String sentence0 = "Hello world";
95    final String sentence1 = "This is a test for user input based bigram";
96    final String sentence2 = "It learns phrases that contain both dictionary and nondictionary "
97            + "words";
98    final String sentence3 = "This should give better suggestions than the previous version";
99    final String sentence4 = "Android stock keyboard is improving";
100}
101