Home | History | Annotate | Download | only in latin
      1 /*
      2  * Copyright (C) 2010,2011 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 
     17 package com.android.inputmethod.latin;
     18 import android.content.res.AssetFileDescriptor;
     19 import android.content.res.Configuration;
     20 
     21 import com.android.inputmethod.latin.tests.R;
     22 
     23 import java.util.Locale;
     24 
     25 public class UserBigramSuggestTests extends SuggestTestsBase {
     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 UserBigramSuggestHelper mHelper;
     31 
     32     @Override
     33     protected void setUp() throws Exception {
     34         super.setUp();
     35         final AssetFileDescriptor dict = openTestRawResourceFd(R.raw.test);
     36         final Locale locale = Locale.US;
     37         mHelper = new UserBigramSuggestHelper(
     38                 getContext(), mTestPackageFile, dict.getStartOffset(), dict.getLength(),
     39                 MAX_DATA, DELETE_DATA,
     40                 createKeyboardId(locale, Configuration.ORIENTATION_PORTRAIT), locale);
     41     }
     42 
     43     /************************** Tests ************************/
     44 
     45     /**
     46      * Test suggestion started at right time
     47      */
     48     public void testUserBigram() {
     49         for (int i = 0; i < SUGGESTION_STARTS; i++) mHelper.addToUserBigram(pair1);
     50         for (int i = 0; i < (SUGGESTION_STARTS - 1); i++) mHelper.addToUserBigram(pair2);
     51 
     52         isInSuggestions("bigram", mHelper.searchUserBigramSuggestion("user", 'b', "bigram"));
     53         isNotInSuggestions("platform",
     54                 mHelper.searchUserBigramSuggestion("android", 'p', "platform"));
     55     }
     56 
     57     /**
     58      * Test loading correct (locale) bigrams
     59      */
     60     public void testOpenAndClose() {
     61         for (int i = 0; i < SUGGESTION_STARTS; i++) mHelper.addToUserBigram(pair1);
     62         isInSuggestions("bigram in default locale",
     63                 mHelper.searchUserBigramSuggestion("user", 'b', "bigram"));
     64 
     65         // change to fr_FR
     66         mHelper.changeUserBigramLocale(Locale.FRANCE);
     67         for (int i = 0; i < SUGGESTION_STARTS; i++) mHelper.addToUserBigram(pair3);
     68         isInSuggestions("france in fr_FR",
     69                 mHelper.searchUserBigramSuggestion("locale", 'f', "france"));
     70         isNotInSuggestions("bigram in fr_FR",
     71                 mHelper.searchUserBigramSuggestion("user", 'b', "bigram"));
     72 
     73         // change back to en_US
     74         mHelper.changeUserBigramLocale(Locale.US);
     75         isNotInSuggestions("france in en_US",
     76                 mHelper.searchUserBigramSuggestion("locale", 'f', "france"));
     77         isInSuggestions("bigram in en_US",
     78                 mHelper.searchUserBigramSuggestion("user", 'b', "bigram"));
     79     }
     80 
     81     /**
     82      * Test data gets pruned when it is over maximum
     83      */
     84     public void testPruningData() {
     85         for (int i = 0; i < SUGGESTION_STARTS; i++) mHelper.addToUserBigram(sentence0);
     86         mHelper.flushUserBigrams();
     87         isInSuggestions("world after several sentence 0",
     88                 mHelper.searchUserBigramSuggestion("Hello", 'w', "world"));
     89 
     90         mHelper.addToUserBigram(sentence1);
     91         mHelper.addToUserBigram(sentence2);
     92         isInSuggestions("world after sentence 1 and 2",
     93                 mHelper.searchUserBigramSuggestion("Hello", 'w', "world"));
     94 
     95         // pruning should happen
     96         mHelper.addToUserBigram(sentence3);
     97         mHelper.addToUserBigram(sentence4);
     98 
     99         // trying to reopen database to check pruning happened in database
    100         mHelper.changeUserBigramLocale(Locale.US);
    101         isNotInSuggestions("world after sentence 3 and 4",
    102                 mHelper.searchUserBigramSuggestion("Hello", 'w', "world"));
    103     }
    104 
    105     private static final String[] pair1 = {"user", "bigram"};
    106     private static final String[] pair2 = {"android","platform"};
    107     private static final String[] pair3 = {"locale", "france"};
    108     private static final String sentence0 = "Hello world";
    109     private static final String sentence1 = "This is a test for user input based bigram";
    110     private static final String sentence2 = "It learns phrases that contain both dictionary and "
    111         + "nondictionary words";
    112     private static final String sentence3 = "This should give better suggestions than the previous "
    113         + "version";
    114     private static final String sentence4 = "Android stock keyboard is improving";
    115 }
    116