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 
17 package com.android.dialer.database;
18 
19 import static com.android.dialer.database.DatabaseTestUtils.*;
20 
21 import android.database.MatrixCursor;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.test.suitebuilder.annotation.SmallTest;
24 import android.test.AndroidTestCase;
25 
26 import com.android.dialer.database.DialerDatabaseHelper;
27 import com.android.dialer.database.DialerDatabaseHelper.ContactNumber;
28 import com.android.dialer.dialpad.SmartDialNameMatcher;
29 import com.android.dialer.dialpad.SmartDialPrefix;
30 
31 import java.lang.Exception;
32 import java.lang.Override;
33 import java.lang.String;
34 import java.util.ArrayList;
35 
36 /**
37  * To run this test, use the command:
38  * adb shell am instrument -w -e class com.android.dialer.database.SmartDialPrefixTest /
39  * com.android.dialer.tests/android.test.InstrumentationTestRunner
40  */
41 @SmallTest
42 public class SmartDialPrefixTest extends AndroidTestCase {
43 
44     private DialerDatabaseHelper mTestHelper;
45 
testIsCountryNanp_CaseInsensitive()46     public void testIsCountryNanp_CaseInsensitive() {
47         assertFalse(SmartDialPrefix.isCountryNanp(null));
48         assertFalse(SmartDialPrefix.isCountryNanp("CN"));
49         assertFalse(SmartDialPrefix.isCountryNanp("HK"));
50         assertFalse(SmartDialPrefix.isCountryNanp("uk"));
51         assertFalse(SmartDialPrefix.isCountryNanp("sg"));
52         assertTrue(SmartDialPrefix.isCountryNanp("US"));
53         assertTrue(SmartDialPrefix.isCountryNanp("CA"));
54         assertTrue(SmartDialPrefix.isCountryNanp("AS"));
55         assertTrue(SmartDialPrefix.isCountryNanp("AI"));
56         assertTrue(SmartDialPrefix.isCountryNanp("AG"));
57         assertTrue(SmartDialPrefix.isCountryNanp("BS"));
58         assertTrue(SmartDialPrefix.isCountryNanp("BB"));
59         assertTrue(SmartDialPrefix.isCountryNanp("bm"));
60         assertTrue(SmartDialPrefix.isCountryNanp("vg"));
61         assertTrue(SmartDialPrefix.isCountryNanp("ky"));
62         assertTrue(SmartDialPrefix.isCountryNanp("dm"));
63         assertTrue(SmartDialPrefix.isCountryNanp("do"));
64         assertTrue(SmartDialPrefix.isCountryNanp("gd"));
65         assertTrue(SmartDialPrefix.isCountryNanp("gu"));
66         assertTrue(SmartDialPrefix.isCountryNanp("jm"));
67         assertTrue(SmartDialPrefix.isCountryNanp("pr"));
68         assertTrue(SmartDialPrefix.isCountryNanp("ms"));
69         assertTrue(SmartDialPrefix.isCountryNanp("mp"));
70         assertTrue(SmartDialPrefix.isCountryNanp("kn"));
71         assertTrue(SmartDialPrefix.isCountryNanp("lc"));
72         assertTrue(SmartDialPrefix.isCountryNanp("vc"));
73         assertTrue(SmartDialPrefix.isCountryNanp("tt"));
74         assertTrue(SmartDialPrefix.isCountryNanp("tc"));
75         assertTrue(SmartDialPrefix.isCountryNanp("vi"));
76     }
77 
78     @Override
setUp()79     protected void setUp() {
80         mTestHelper = DialerDatabaseHelper.getNewInstanceForTest(getContext());
81     }
82 
83     @Override
tearDown()84     protected void tearDown() throws Exception {
85         final SQLiteDatabase db = mTestHelper.getWritableDatabase();
86         mTestHelper.removeAllContacts(db);
87         super.tearDown();
88     }
89 
getLooseMatchesFromDb(String query)90     private ArrayList<ContactNumber> getLooseMatchesFromDb(String query) {
91         final SmartDialNameMatcher nameMatcher = new SmartDialNameMatcher(query,
92                 SmartDialPrefix.getMap());
93         return mTestHelper.getLooseMatches(query, nameMatcher);
94     }
95 
testPutForFullName()96     public void testPutForFullName() {
97         final SQLiteDatabase db = mTestHelper.getWritableDatabase();
98 
99         final MatrixCursor nameCursor =  constructNewNameCursor();
100         final MatrixCursor contactCursor = constructNewContactCursor();
101         final ContactNumber jasonsmith = constructNewContactWithDummyIds(contactCursor, nameCursor,
102                 "", 0, "Jason Smith");
103         final ContactNumber jasonsmitt = constructNewContactWithDummyIds(contactCursor, nameCursor,
104                 "", 1, "Jason Smitt");
105         final ContactNumber alphabet = constructNewContactWithDummyIds(contactCursor, nameCursor,
106                 "12345678", 2, "abc def ghi jkl mno pqrs tuv wxyz");
107 
108         mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0));
109         mTestHelper.insertNamePrefixes(db, nameCursor);
110 
111         nameCursor.close();
112         contactCursor.close();
113 
114         final ArrayList<ContactNumber> result1 = getLooseMatchesFromDb("5276676484");
115         assertFalse(result1.contains(jasonsmitt));
116 
117         final ArrayList<ContactNumber> result2 = getLooseMatchesFromDb("5276676488");
118         assertFalse(result2.contains(jasonsmith));
119         assertTrue(result2.contains(jasonsmitt));
120 
121         assertTrue(getLooseMatchesFromDb("22233344455566677778889999").contains(alphabet));
122         assertTrue(getLooseMatchesFromDb("33344455566677778889999").contains(alphabet));
123         assertTrue(getLooseMatchesFromDb("44455566677778889999").contains(alphabet));
124         assertTrue(getLooseMatchesFromDb("55566677778889999").contains(alphabet));
125         assertTrue(getLooseMatchesFromDb("66677778889999").contains(alphabet));
126         assertTrue(getLooseMatchesFromDb("77778889999").contains(alphabet));
127         assertTrue(getLooseMatchesFromDb("8889999").contains(alphabet));
128         assertTrue(getLooseMatchesFromDb("9999").contains(alphabet));
129 
130         // Makes sure the phone number is correctly added.
131         assertTrue(getLooseMatchesFromDb("12345678").contains(alphabet));
132     }
133 
testPutForPartialName()134     public void testPutForPartialName() {
135         final SQLiteDatabase db = mTestHelper.getWritableDatabase();
136 
137         final MatrixCursor nameCursor =  constructNewNameCursor();
138         final MatrixCursor contactCursor = constructNewContactCursor();
139         final ContactNumber maryjane = constructNewContactWithDummyIds(contactCursor, nameCursor,
140                 "", 0, "Mary Jane");
141         final ContactNumber sarahsmith = constructNewContactWithDummyIds(contactCursor, nameCursor,
142                 "", 1, "Sarah Smith");
143         final ContactNumber jasonsmitt = constructNewContactWithDummyIds(contactCursor, nameCursor,
144                 "", 2, "Jason Smitt");
145 
146         mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0));
147         mTestHelper.insertNamePrefixes(db, nameCursor);
148 
149         nameCursor.close();
150         contactCursor.close();
151 
152         final ArrayList<ContactNumber> result1 = getLooseMatchesFromDb("6279");
153         assertTrue(result1.contains(maryjane));
154         assertFalse(result1.contains(jasonsmitt));
155 
156         // 72 corresponds to sa = "Sarah Smith" but not "Jason Smitt" or "Mary Jane"
157         final ArrayList<ContactNumber> result2 = getLooseMatchesFromDb("72");
158         assertFalse(result2.contains(maryjane));
159         assertTrue(result2.contains(sarahsmith));
160         assertFalse(result2.contains(jasonsmitt));
161 
162         // 76 corresponds to sm = "Sarah Smith" and "Jason Smitt" but not "Mary Jane"
163         final ArrayList<ContactNumber> result3 = getLooseMatchesFromDb("76");
164         assertFalse(result3.contains(maryjane));
165         assertTrue(result3.contains(sarahsmith));
166         assertTrue(result3.contains(jasonsmitt));
167     }
168 
testPutForNameTokens()169     public void testPutForNameTokens() {
170         final SQLiteDatabase db = mTestHelper.getWritableDatabase();
171 
172         final MatrixCursor nameCursor =  constructNewNameCursor();
173         final MatrixCursor contactCursor = constructNewContactCursor();
174         final ContactNumber jasonfwilliams = constructNewContactWithDummyIds(contactCursor,
175                 nameCursor, "", 0, "Jason F. Williams");
176 
177         mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0));
178         mTestHelper.insertNamePrefixes(db, nameCursor);
179 
180         nameCursor.close();
181         contactCursor.close();
182 
183         assertTrue(getLooseMatchesFromDb("527").contains(jasonfwilliams));
184         // 72 corresponds to sa = "Sarah Smith" but not "Jason Smitt" or "Mary Jane"
185         assertTrue(getLooseMatchesFromDb("945").contains(jasonfwilliams));
186         // 76 corresponds to sm = "Sarah Smith" and "Jason Smitt" but not "Mary Jane"
187         assertFalse(getLooseMatchesFromDb("66").contains(jasonfwilliams));
188     }
189 
testPutForInitialMatches()190     public void testPutForInitialMatches() {
191         final SQLiteDatabase db = mTestHelper.getWritableDatabase();
192 
193         final MatrixCursor nameCursor =  constructNewNameCursor();
194         final MatrixCursor contactCursor = constructNewContactCursor();
195         final ContactNumber martinjuniorharry = constructNewContactWithDummyIds(contactCursor,
196                 nameCursor, "", 0, "Martin Jr Harry");
197 
198         mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0));
199         mTestHelper.insertNamePrefixes(db, nameCursor);
200 
201         nameCursor.close();
202         contactCursor.close();
203 
204         // 654 corresponds to mjh = "(M)artin (J)r (H)arry"
205         assertTrue(getLooseMatchesFromDb("654").contains(martinjuniorharry));
206         // The reverse (456) does not match (for now)
207         assertFalse(getLooseMatchesFromDb("456").contains(martinjuniorharry));
208         // 6542 corresponds to mjha = "(M)artin (J)r (Ha)rry"
209         assertTrue(getLooseMatchesFromDb("6542").contains(martinjuniorharry));
210         // 542 corresponds to jha = "Martin (J)r (Ha)rry"
211         assertTrue(getLooseMatchesFromDb("542").contains(martinjuniorharry));
212         // 642 corresponds to mha = "(M)artin Jr (Ha)rry"
213         assertTrue(getLooseMatchesFromDb("642").contains(martinjuniorharry));
214         // 6542779 (M)artin (J)r (Harry)
215         assertTrue(getLooseMatchesFromDb("6542779").contains(martinjuniorharry));
216         // 65742779 (M)artin (Jr) (Harry)
217         assertTrue(getLooseMatchesFromDb("65742779").contains(martinjuniorharry));
218         // 542779 Martin (J)r (Harry)
219         assertTrue(getLooseMatchesFromDb("542779").contains(martinjuniorharry));
220         // 547 doesn't match
221         assertFalse(getLooseMatchesFromDb("547").contains(martinjuniorharry));
222         // 655 doesn't match
223         assertFalse(getLooseMatchesFromDb("655").contains(martinjuniorharry));
224         // 653 doesn't match
225         assertFalse(getLooseMatchesFromDb("653").contains(martinjuniorharry));
226         // 6543 doesn't match
227         assertFalse(getLooseMatchesFromDb("6543").contains(martinjuniorharry));
228 
229         // 7 actual rows, + 1 for the dummy number we added
230         assertEquals(8, mTestHelper.countPrefixTableRows(db));
231     }
232 
testPutForInitialMatchesForLongTokenNames()233     public void testPutForInitialMatchesForLongTokenNames() {
234 
235         final SQLiteDatabase db = mTestHelper.getWritableDatabase();
236 
237         final MatrixCursor nameCursor =  constructNewNameCursor();
238         final MatrixCursor contactCursor = constructNewContactCursor();
239         final ContactNumber alphabet = constructNewContactWithDummyIds(contactCursor, nameCursor,
240                 "12345678", 0, "abc def ghi jkl mno pqrs tuv wxyz");
241 
242         mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0));
243         mTestHelper.insertNamePrefixes(db, nameCursor);
244 
245         nameCursor.close();
246         contactCursor.close();
247 
248         // Makes sure only only the first two and last two token are considered for initials.
249         // The cut-off constant can be set in SmartDialPrefix.java
250         assertTrue(getLooseMatchesFromDb("2389999").contains(alphabet));
251         assertTrue(getLooseMatchesFromDb("239999").contains(alphabet));
252         assertTrue(getLooseMatchesFromDb("23888").contains(alphabet));
253         assertTrue(getLooseMatchesFromDb("2333").contains(alphabet));
254         assertTrue(getLooseMatchesFromDb("289999").contains(alphabet));
255         assertTrue(getLooseMatchesFromDb("2888").contains(alphabet));
256         assertTrue(getLooseMatchesFromDb("29999").contains(alphabet));
257         assertTrue(getLooseMatchesFromDb("3888").contains(alphabet));
258         assertTrue(getLooseMatchesFromDb("39999").contains(alphabet));
259         assertTrue(getLooseMatchesFromDb("389999").contains(alphabet));
260         assertTrue(getLooseMatchesFromDb("89999").contains(alphabet));
261     }
262 
testCheckLongToken()263     public void testCheckLongToken() {
264         final SQLiteDatabase db = mTestHelper.getWritableDatabase();
265 
266         final MatrixCursor nameCursor =  constructNewNameCursor();
267         final MatrixCursor contactCursor = constructNewContactCursor();
268         final ContactNumber alphabet = constructNewContactWithDummyIds(contactCursor, nameCursor,
269                 "1", 0,  " aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn" +
270                 " oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz");
271 
272         final ContactNumber alphabet2 = constructNewContactWithDummyIds(contactCursor, nameCursor,
273                 "1", 1, "aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnooooppppqqqqrrrr" +
274                 "ssssttttuuuuvvvvwwwwxxxxyyyyzzzz");
275 
276         mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0));
277         mTestHelper.insertNamePrefixes(db, nameCursor);
278 
279         nameCursor.close();
280         contactCursor.close();
281 
282         assertTrue(getLooseMatchesFromDb("2222").contains(alphabet));
283         assertEquals(40, mTestHelper.countPrefixTableRows(db));
284     }
285 
testAccentedCharacters()286     public void testAccentedCharacters() {
287         final SQLiteDatabase db = mTestHelper.getWritableDatabase();
288 
289         final MatrixCursor nameCursor =  constructNewNameCursor();
290         final MatrixCursor contactCursor = constructNewContactCursor();
291         final ContactNumber reene = constructNewContactWithDummyIds(contactCursor, nameCursor,
292                 "0", 0, "Reenée");
293         final ContactNumber bronte = constructNewContactWithDummyIds(contactCursor, nameCursor,
294                 "0", 1, "Brontë");
295 
296         mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0));
297         mTestHelper.insertNamePrefixes(db, nameCursor);
298 
299         nameCursor.close();
300         contactCursor.close();
301 
302         assertTrue(getLooseMatchesFromDb("733633").contains(reene));
303         assertTrue(getLooseMatchesFromDb("276683").contains(bronte));
304     }
305 
testNumbersInName()306     public void testNumbersInName() {
307         final SQLiteDatabase db = mTestHelper.getWritableDatabase();
308 
309         final MatrixCursor nameCursor =  constructNewNameCursor();
310         final MatrixCursor contactCursor = constructNewContactCursor();
311         final ContactNumber contact = constructNewContactWithDummyIds(contactCursor, nameCursor,
312                 "0", 0, "12345678");
313         final ContactNumber teacher = constructNewContactWithDummyIds(contactCursor, nameCursor,
314                 "0", 1, "1st Grade Teacher");
315 
316         mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0));
317         mTestHelper.insertNamePrefixes(db, nameCursor);
318 
319         nameCursor.close();
320         contactCursor.close();
321 
322         assertTrue(getLooseMatchesFromDb("12345678").contains(contact));
323         assertTrue(getLooseMatchesFromDb("17847233").contains(teacher));
324         assertTrue(getLooseMatchesFromDb("14832").contains(teacher));
325     }
326 
testPutForNumbers()327     public void testPutForNumbers() {
328         final SQLiteDatabase db = mTestHelper.getWritableDatabase();
329 
330         final MatrixCursor nameCursor =  constructNewNameCursor();
331         final MatrixCursor contactCursor = constructNewContactCursor();
332         final ContactNumber contactno1 = constructNewContactWithDummyIds(contactCursor, nameCursor,
333                 "510-527-2357", 0,  "James");
334         final ContactNumber contactno2 = constructNewContactWithDummyIds(contactCursor, nameCursor,
335                 "77212862357", 1, "James");
336         final ContactNumber contactno3 = constructNewContactWithDummyIds(contactCursor, nameCursor,
337                 "+13684976334", 2, "James");
338 
339         mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0));
340         mTestHelper.insertNamePrefixes(db, nameCursor);
341 
342         nameCursor.close();
343         contactCursor.close();
344 
345         assertTrue(getLooseMatchesFromDb("510").contains(contactno1));
346         assertFalse(getLooseMatchesFromDb("511").contains(contactno1));
347         assertTrue(getLooseMatchesFromDb("77212862357").contains(contactno2));
348         assertFalse(getLooseMatchesFromDb("77212862356").contains(contactno2));
349         assertTrue(getLooseMatchesFromDb("1368").contains(contactno3));
350         assertFalse(getLooseMatchesFromDb("1367").contains(contactno3));
351     }
352 
testPutNumbersCountryCode()353     public void testPutNumbersCountryCode() {
354         final SQLiteDatabase db = mTestHelper.getWritableDatabase();
355 
356         final MatrixCursor nameCursor =  constructNewNameCursor();
357         final MatrixCursor contactCursor = constructNewContactCursor();
358         final ContactNumber contactno1 = constructNewContactWithDummyIds(contactCursor, nameCursor,
359                 "+13684976334", 0, "James");
360         final ContactNumber contactno2 = constructNewContactWithDummyIds(contactCursor, nameCursor,
361                 "+65 9177-6930", 1, "Jason");
362         final ContactNumber contactno3 = constructNewContactWithDummyIds(contactCursor, nameCursor,
363                 "+85212345678", 2, "Mike");
364         final ContactNumber contactno4 = constructNewContactWithDummyIds(contactCursor, nameCursor,
365                 "+85112345678", 3, "Invalid");
366         final ContactNumber contactno5 = constructNewContactWithDummyIds(contactCursor, nameCursor,
367                 "+852", 4, "Invalid");
368 
369         mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0));
370         mTestHelper.insertNamePrefixes(db, nameCursor);
371 
372         nameCursor.close();
373         contactCursor.close();
374 
375         assertTrue(getLooseMatchesFromDb("1368").contains(contactno1));
376         assertTrue(getLooseMatchesFromDb("368497").contains(contactno1));
377         assertFalse(getLooseMatchesFromDb("2368497").contains(contactno1));
378 
379         assertTrue(getLooseMatchesFromDb("6591776930").contains(contactno2));
380         assertTrue(getLooseMatchesFromDb("91776930").contains(contactno2));
381         assertFalse(getLooseMatchesFromDb("591776930").contains(contactno2));
382 
383         assertTrue(getLooseMatchesFromDb("85212345678").contains(contactno3));
384         assertTrue(getLooseMatchesFromDb("12345678").contains(contactno3));
385         assertFalse(getLooseMatchesFromDb("5212345678").contains(contactno3));
386 
387         assertTrue(getLooseMatchesFromDb("85112345678").contains(contactno4));
388         assertFalse(getLooseMatchesFromDb("12345678").contains(contactno4));
389     }
390 
391     // Tests special case handling for NANP numbers
testPutNumbersNANP()392     public void testPutNumbersNANP() {
393         SmartDialPrefix.setUserInNanpRegion(true);
394         final SQLiteDatabase db = mTestHelper.getWritableDatabase();
395 
396         final MatrixCursor nameCursor =  constructNewNameCursor();
397         final MatrixCursor contactCursor = constructNewContactCursor();
398         final ContactNumber contactno1 = constructNewContactWithDummyIds(contactCursor, nameCursor,
399                 "16503337596", 0, "James");
400         final ContactNumber contactno2 = constructNewContactWithDummyIds(contactCursor, nameCursor,
401                 "5109921234", 1, "Michael");
402         final ContactNumber contactno3 = constructNewContactWithDummyIds(contactCursor, nameCursor,
403                 "(415)-123-4567", 2, "Jason");
404         final ContactNumber contactno4 = constructNewContactWithDummyIds(contactCursor, nameCursor,
405                 "1 510-284-9170", 3, "Mike");
406         final ContactNumber contactno5 = constructNewContactWithDummyIds(contactCursor, nameCursor,
407                 "1-415-123-123", 4, "Invalid");
408         final ContactNumber contactno6 = constructNewContactWithDummyIds(contactCursor, nameCursor,
409                 "415-123-123", 5, "Invalid2");
410         final ContactNumber contactno7 = constructNewContactWithDummyIds(contactCursor, nameCursor,
411                 "+1-510-284-9170", 6, "Mike");
412         final ContactNumber contactno8 = constructNewContactWithDummyIds(contactCursor, nameCursor,
413                 "+1-510-284-917", 7, "Invalid");
414         final ContactNumber contactno9 = constructNewContactWithDummyIds(contactCursor, nameCursor,
415                 "+857-510-284-9170", 8, "Inv");
416 
417         mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0));
418         mTestHelper.insertNamePrefixes(db, nameCursor);
419 
420         nameCursor.close();
421         contactCursor.close();
422 
423         assertTrue(getLooseMatchesFromDb("16503337596").contains(contactno1));
424         assertTrue(getLooseMatchesFromDb("6503337596").contains(contactno1));
425         assertTrue(getLooseMatchesFromDb("3337596").contains(contactno1));
426 
427         assertTrue(getLooseMatchesFromDb("5109921234").contains(contactno2));
428         assertTrue(getLooseMatchesFromDb("9921234").contains(contactno2));
429 
430         assertTrue(getLooseMatchesFromDb("4151234567").contains(contactno3));
431         assertTrue(getLooseMatchesFromDb("1234567").contains(contactno3));
432 
433         assertTrue(getLooseMatchesFromDb("15102849170").contains(contactno4));
434         assertTrue(getLooseMatchesFromDb("5102849170").contains(contactno4));
435         assertTrue(getLooseMatchesFromDb("2849170").contains(contactno4));
436 
437         assertTrue(getLooseMatchesFromDb("1415123123").contains(contactno5));
438         assertFalse(getLooseMatchesFromDb("415123123").contains(contactno5));
439         assertFalse(getLooseMatchesFromDb("123123").contains(contactno5));
440 
441         assertTrue(getLooseMatchesFromDb("415123123").contains(contactno6));
442         assertFalse(getLooseMatchesFromDb("123123").contains(contactno6));
443 
444         assertTrue(getLooseMatchesFromDb("15102849170").contains(contactno7));
445         assertTrue(getLooseMatchesFromDb("5102849170").contains(contactno7));
446         assertTrue(getLooseMatchesFromDb("2849170").contains(contactno7));
447         assertFalse(getLooseMatchesFromDb("849170").contains(contactno7));
448         assertFalse(getLooseMatchesFromDb("10849170").contains(contactno7));
449 
450         assertTrue(getLooseMatchesFromDb("1510284917").contains(contactno8));
451         assertTrue(getLooseMatchesFromDb("510284917").contains(contactno8));
452         assertFalse(getLooseMatchesFromDb("2849170").contains(contactno8));
453 
454         assertTrue(getLooseMatchesFromDb("8575102849170").contains(contactno9));
455         assertFalse(getLooseMatchesFromDb("5102849170").contains(contactno9));
456         assertFalse(getLooseMatchesFromDb("2849170").contains(contactno9));
457 
458         // TODO(klp) Adds test for non-NANP region number matchings.
459     }
460 
461     // Tests special case handling for non-NANP numbers
testPutNumbersNonNANP()462     public void testPutNumbersNonNANP() {
463         SmartDialPrefix.setUserInNanpRegion(false);
464         final SQLiteDatabase db = mTestHelper.getWritableDatabase();
465 
466         final MatrixCursor nameCursor =  constructNewNameCursor();
467         final MatrixCursor contactCursor = constructNewContactCursor();
468 
469         final ContactNumber contactno0 = constructNewContactWithDummyIds(contactCursor, nameCursor,
470                 "(415)-123-4567", 0, "Jason");
471         final ContactNumber contactno1 = constructNewContactWithDummyIds(contactCursor, nameCursor,
472                 "1 510-284-9170", 1, "Mike");
473 
474 
475         mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0));
476         mTestHelper.insertNamePrefixes(db, nameCursor);
477 
478         nameCursor.close();
479         contactCursor.close();
480 
481         assertTrue(getLooseMatchesFromDb("4151234567").contains(contactno0));
482         assertFalse(getLooseMatchesFromDb("1234567").contains(contactno0));
483 
484         assertTrue(getLooseMatchesFromDb("15102849170").contains(contactno1));
485         assertFalse(getLooseMatchesFromDb("5102849170").contains(contactno1));
486         assertFalse(getLooseMatchesFromDb("2849170").contains(contactno1));
487     }
488 
testParseInfo()489     public void testParseInfo() {
490         final String name = "Mcdonald Jamie-Cullum";
491         final ArrayList<String> info = SmartDialPrefix.parseToIndexTokens(name);
492         assertEquals(3, info.size());
493         assertEquals(8, info.get(0).length());
494         assertEquals(5, info.get(1).length());
495         assertEquals(6, info.get(2).length());
496 
497         final String name2 = "aaa bbb ccc ddd eee fff ggg hhh iii jjj kkk";
498         final ArrayList<String> info2 = SmartDialPrefix.parseToIndexTokens(name2);
499         assertEquals(11, info2.size());
500         assertEquals(3, info2.get(0).length());
501         assertEquals(3, info2.get(10).length());
502 
503         final String name3 = "this  is- a,test    name";
504         final ArrayList<String> info3 = SmartDialPrefix.parseToIndexTokens(name3);
505         assertEquals(5, info3.size());
506         assertEquals(2, info3.get(1).length());
507         assertEquals(1, info3.get(2).length());
508         assertEquals(4, info3.get(3).length());
509         assertEquals(4, info3.get(4).length());
510 
511         final String name4 = "M c-Donald James";
512         final ArrayList<String> info4 = SmartDialPrefix.parseToIndexTokens(name4);
513         assertEquals(4, info4.size());
514         assertEquals(1, info4.get(1).length());
515         assertEquals(6, info4.get(2).length());
516 
517         final String name5 = "   Aa'Bb    c    dddd  e'e";
518         final ArrayList<String> info5 = SmartDialPrefix.parseToIndexTokens(name5);
519         assertEquals(6, info5.size());
520         assertEquals(2, info5.get(0).length());
521         assertEquals(1, info5.get(5).length());
522     }
523 }
524