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