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 19 import com.android.inputmethod.latin.tests.R; 20 21 import android.content.res.AssetFileDescriptor; 22 import android.content.res.Configuration; 23 24 import java.util.Locale; 25 26 public class SuggestTests extends SuggestTestsBase { 27 private SuggestHelper mHelper; 28 29 @Override setUp()30 protected void setUp() throws Exception { 31 super.setUp(); 32 final AssetFileDescriptor dict = openTestRawResourceFd(R.raw.test); 33 final Locale locale = Locale.US; 34 mHelper = new SuggestHelper( 35 getContext(), mTestPackageFile, dict.getStartOffset(), dict.getLength(), 36 createKeyboardId(locale, Configuration.ORIENTATION_PORTRAIT), locale); 37 mHelper.setCorrectionMode(Suggest.CORRECTION_FULL_BIGRAM); 38 } 39 40 /************************** Tests ************************/ 41 42 /** 43 * Tests for simple completions of one character. 44 */ testCompletion1char()45 public void testCompletion1char() { 46 suggested("people", mHelper.getFirstSuggestion("peopl")); 47 suggested("about", mHelper.getFirstSuggestion("abou")); 48 suggested("their", mHelper.getFirstSuggestion("thei")); 49 } 50 51 /** 52 * Tests for simple completions of two characters. 53 */ testCompletion2char()54 public void testCompletion2char() { 55 suggested("people", mHelper.getFirstSuggestion("peop")); 56 suggested("calling", mHelper.getFirstSuggestion("calli")); 57 suggested("business", mHelper.getFirstSuggestion("busine")); 58 } 59 60 /** 61 * Tests for proximity errors. 62 */ testProximityPositive()63 public void testProximityPositive() { 64 suggested("typed peiple", "people", mHelper.getFirstSuggestion("peiple")); 65 suggested("typed peoole", "people", mHelper.getFirstSuggestion("peoole")); 66 suggested("typed pwpple", "people", mHelper.getFirstSuggestion("pwpple")); 67 } 68 69 /** 70 * Tests for proximity errors - negative, when the error key is not close. 71 */ testProximityNegative()72 public void testProximityNegative() { 73 notSuggested("about", mHelper.getFirstSuggestion("arout")); 74 notSuggested("are", mHelper.getFirstSuggestion("ire")); 75 } 76 77 /** 78 * Tests for checking if apostrophes are added automatically. 79 */ testApostropheInsertion()80 public void testApostropheInsertion() { 81 suggested("I'm", mHelper.getFirstSuggestion("im")); 82 suggested("don't", mHelper.getFirstSuggestion("dont")); 83 } 84 85 /** 86 * Test to make sure apostrophed word is not suggested for an apostrophed word. 87 */ testApostrophe()88 public void testApostrophe() { 89 notSuggested("don't", mHelper.getFirstSuggestion("don't")); 90 } 91 92 /** 93 * Tests for suggestion of capitalized version of a word. 94 */ testCapitalization()95 public void testCapitalization() { 96 suggested("I'm", mHelper.getFirstSuggestion("i'm")); 97 suggested("Sunday", mHelper.getFirstSuggestion("sunday")); 98 suggested("Sunday", mHelper.getFirstSuggestion("sundat")); 99 } 100 101 /** 102 * Tests to see if more than one completion is provided for certain prefixes. 103 */ testMultipleCompletions()104 public void testMultipleCompletions() { 105 isInSuggestions("com: come", mHelper.getSuggestIndex("com", "come")); 106 isInSuggestions("com: company", mHelper.getSuggestIndex("com", "company")); 107 isInSuggestions("th: the", mHelper.getSuggestIndex("th", "the")); 108 isInSuggestions("th: that", mHelper.getSuggestIndex("th", "that")); 109 isInSuggestions("th: this", mHelper.getSuggestIndex("th", "this")); 110 isInSuggestions("th: they", mHelper.getSuggestIndex("th", "they")); 111 } 112 113 /** 114 * Does the suggestion engine recognize zero frequency words as valid words. 115 */ testZeroFrequencyAccepted()116 public void testZeroFrequencyAccepted() { 117 assertTrue("valid word yikes", mHelper.isValidWord("yikes")); 118 assertFalse("non valid word yike", mHelper.isValidWord("yike")); 119 } 120 121 /** 122 * Tests to make sure that zero frequency words are not suggested as completions. 123 */ testZeroFrequencySuggestionsNegative()124 public void testZeroFrequencySuggestionsNegative() { 125 assertTrue(mHelper.getSuggestIndex("yike", "yikes") < 0); 126 assertTrue(mHelper.getSuggestIndex("what", "whatcha") < 0); 127 } 128 129 /** 130 * Tests to ensure that words with large edit distances are not suggested, in some cases. 131 * Also such word is not considered auto correction, in some cases. 132 */ 133 public void testTooLargeEditDistance() { 134 assertTrue(mHelper.getSuggestIndex("sniyr", "about") < 0); 135 // TODO: The following test fails. 136 // notSuggested("the", mHelper.getAutoCorrection("rjw")); 137 } 138 139 /** 140 * Make sure mHelper.isValidWord is case-sensitive. 141 */ 142 public void testValidityCaseSensitivity() { 143 assertTrue("valid word Sunday", mHelper.isValidWord("Sunday")); 144 assertFalse("non valid word sunday", mHelper.isValidWord("sunday")); 145 } 146 147 /** 148 * Are accented forms of words suggested as corrections? 149 */ 150 public void testAccents() { 151 // ni<LATIN SMALL LETTER N WITH TILDE>o 152 suggested("ni\u00F1o", mHelper.getAutoCorrection("nino")); 153 // ni<LATIN SMALL LETTER N WITH TILDE>o 154 suggested("ni\u00F1o", mHelper.getAutoCorrection("nimo")); 155 // Mar<LATIN SMALL LETTER I WITH ACUTE>a 156 suggested("Mar\u00EDa", mHelper.getAutoCorrection("maria")); 157 } 158 159 /** 160 * Make sure bigrams are showing when first character is typed 161 * and don't show any when there aren't any 162 */ 163 public void testBigramsAtFirstChar() { 164 suggested("bigram: about p[art]", 165 "part", mHelper.getBigramFirstSuggestion("about", "p")); 166 suggested("bigram: I'm a[bout]", 167 "about", mHelper.getBigramFirstSuggestion("I'm", "a")); 168 suggested("bigram: about b[usiness]", 169 "business", mHelper.getBigramFirstSuggestion("about", "b")); 170 isInSuggestions("bigram: about b[eing]", 171 mHelper.searchBigramSuggestion("about", "b", "being")); 172 notSuggested("bigram: about p", 173 "business", mHelper.getBigramFirstSuggestion("about", "p")); 174 } 175 176 /** 177 * Make sure bigrams score affects the original score 178 */ 179 public void testBigramsScoreEffect() { 180 suggested("single: page", 181 "page", mHelper.getAutoCorrection("pa")); 182 suggested("bigram: about pa[rt]", 183 "part", mHelper.getBigramAutoCorrection("about", "pa")); 184 // TODO: The following test fails. 185 // suggested("single: said", "said", mHelper.getAutoCorrection("sa")); 186 suggested("bigram: from sa[me]", 187 "same", mHelper.getBigramAutoCorrection("from", "sa")); 188 } 189 } 190