diff options
author | Christine Chen <christinech@google.com> | 2013-05-30 16:54:09 -0700 |
---|---|---|
committer | Christine Chen <christinech@google.com> | 2013-06-28 14:51:23 -0700 |
commit | 661834dd3b45b477165effe126a9c243750ae8d6 (patch) | |
tree | 18bdf376ca37f70c9409366a447783a692fa83fb /tests/src/com/android | |
parent | 68a5a26411d18bba457942bf1ae4c2623cc4da74 (diff) |
Add SmartDial database for the Dialer app.
- Creates a database helper to create a smartdial database for the Dialer app.
- Queries all rows in the Contact database and copies related columns to
the smart dial database.
- Create another prefix database to contain all prefixes of a contact.
- During keypad input, the prefix databse is queried to find contact
suggestions, and suggestions are ranked by the usage data and contact
status (starred, primary contact, etc.)
- Created unit test for the SmartDial database insertion and prefix
computing functions.
Change-Id: I4d7c3b3bcc52dd6efa4d6e69d3f1687c3abaeb69
Diffstat (limited to 'tests/src/com/android')
4 files changed, 600 insertions, 470 deletions
diff --git a/tests/src/com/android/dialer/database/SmartDialPrefixTest.java b/tests/src/com/android/dialer/database/SmartDialPrefixTest.java new file mode 100644 index 000000000..db0a0f38a --- /dev/null +++ b/tests/src/com/android/dialer/database/SmartDialPrefixTest.java @@ -0,0 +1,597 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dialer.database; + +import android.database.MatrixCursor; +import android.database.sqlite.SQLiteDatabase; +import android.provider.ContactsContract; +import android.provider.ContactsContract.CommonDataKinds.Phone; +import android.provider.ContactsContract.Contacts; +import android.provider.ContactsContract.Data; +import android.provider.ContactsContract.Directory; +import android.test.suitebuilder.annotation.SmallTest; +import android.test.suitebuilder.annotation.Suppress; +import android.test.AndroidTestCase; + +import com.android.dialer.database.DialerDatabaseHelper; +import com.android.dialer.database.DialerDatabaseHelper.ContactNumber; +import com.android.dialer.dialpad.SmartDialNameMatcher; +import com.android.dialer.dialpad.SmartDialPrefix; + +import junit.framework.TestCase; + +import java.lang.Exception; +import java.lang.FindBugsSuppressWarnings; +import java.lang.Override; +import java.lang.String; +import java.util.ArrayList; + +/** + * To run this test, use the command: + * adb shell am instrument -w -e class com.android.dialer.dialpad.SmartDialPrefixTest / + * com.android.dialer.tests/android.test.InstrumentationTestRunner + */ +@SmallTest +public class SmartDialPrefixTest extends AndroidTestCase { + + private DialerDatabaseHelper mTestHelper; + + public void testIsCountryNanp_CaseInsensitive() { + assertFalse(SmartDialPrefix.isCountryNanp(null)); + assertFalse(SmartDialPrefix.isCountryNanp("CN")); + assertFalse(SmartDialPrefix.isCountryNanp("HK")); + assertFalse(SmartDialPrefix.isCountryNanp("uk")); + assertFalse(SmartDialPrefix.isCountryNanp("sg")); + assertTrue(SmartDialPrefix.isCountryNanp("US")); + assertTrue(SmartDialPrefix.isCountryNanp("CA")); + assertTrue(SmartDialPrefix.isCountryNanp("AS")); + assertTrue(SmartDialPrefix.isCountryNanp("AI")); + assertTrue(SmartDialPrefix.isCountryNanp("AG")); + assertTrue(SmartDialPrefix.isCountryNanp("BS")); + assertTrue(SmartDialPrefix.isCountryNanp("BB")); + assertTrue(SmartDialPrefix.isCountryNanp("bm")); + assertTrue(SmartDialPrefix.isCountryNanp("vg")); + assertTrue(SmartDialPrefix.isCountryNanp("ky")); + assertTrue(SmartDialPrefix.isCountryNanp("dm")); + assertTrue(SmartDialPrefix.isCountryNanp("do")); + assertTrue(SmartDialPrefix.isCountryNanp("gd")); + assertTrue(SmartDialPrefix.isCountryNanp("gu")); + assertTrue(SmartDialPrefix.isCountryNanp("jm")); + assertTrue(SmartDialPrefix.isCountryNanp("pr")); + assertTrue(SmartDialPrefix.isCountryNanp("ms")); + assertTrue(SmartDialPrefix.isCountryNanp("mp")); + assertTrue(SmartDialPrefix.isCountryNanp("kn")); + assertTrue(SmartDialPrefix.isCountryNanp("lc")); + assertTrue(SmartDialPrefix.isCountryNanp("vc")); + assertTrue(SmartDialPrefix.isCountryNanp("tt")); + assertTrue(SmartDialPrefix.isCountryNanp("tc")); + assertTrue(SmartDialPrefix.isCountryNanp("vi")); + } + + protected void setUp() { + mTestHelper = DialerDatabaseHelper.getNewInstanceForTest(getContext()); + } + + @Override + protected void tearDown() throws Exception { + final SQLiteDatabase db = mTestHelper.getWritableDatabase(); + mTestHelper.removeAllContacts(db); + super.tearDown(); + } + + @Suppress + public void testForNewContacts() { + } + + @Suppress + public void testForUpdatedContacts() { + } + + @Suppress + public void testForDeletedContacts() { + } + + @Suppress + public void testSize() { + } + + + private MatrixCursor constructNewNameCursor() { + final MatrixCursor cursor = new MatrixCursor(new String[]{ + DialerDatabaseHelper.SmartDialDbColumns.DISPLAY_NAME_PRIMARY, + DialerDatabaseHelper.SmartDialDbColumns.CONTACT_ID}); + return cursor; + } + + private MatrixCursor constructNewContactCursor() { + final MatrixCursor cursor = new MatrixCursor(new String[]{ + Phone._ID, + Phone.TYPE, + Phone.LABEL, + Phone.NUMBER, + Phone.CONTACT_ID, + Phone.LOOKUP_KEY, + Phone.DISPLAY_NAME_PRIMARY, + Data.LAST_TIME_USED, + Data.TIMES_USED, + Contacts.STARRED, + Data.IS_SUPER_PRIMARY, + Contacts.IN_VISIBLE_GROUP, + Data.IS_PRIMARY}); + + return cursor; + } + + private ContactNumber constructNewContact(MatrixCursor contactCursor, MatrixCursor nameCursor, + int id, String number, int contactId, String lookupKey, String displayName, + int lastTimeUsed, int timesUsed, int starred, int isSuperPrimary, int inVisibleGroup, + int isPrimary) { + assertNotNull(contactCursor); + assertNotNull(nameCursor); + + contactCursor.addRow(new Object[]{id, "", "", number, contactId, lookupKey, displayName, + lastTimeUsed, timesUsed, starred, isSuperPrimary, inVisibleGroup, isPrimary}); + nameCursor.addRow(new Object[]{displayName, contactId}); + + return new ContactNumber(contactId, displayName, number, lookupKey); + } + + private ArrayList<ContactNumber> getLooseMatchesFromDb(String query) { + final SmartDialNameMatcher nameMatcher = new SmartDialNameMatcher(query, + SmartDialPrefix.getMap()); + return mTestHelper.getLooseMatches(query, nameMatcher); + } + + public void testPutForFullName() { + final SQLiteDatabase db = mTestHelper.getWritableDatabase(); + + final MatrixCursor nameCursor = constructNewNameCursor(); + final MatrixCursor contactCursor = constructNewContactCursor(); + final ContactNumber jasonsmith = constructNewContact(contactCursor, nameCursor, + 0, "", 0, "", "Jason Smith", 0, 0, 0, 0, 0, 0); + final ContactNumber jasonsmitt = constructNewContact(contactCursor, nameCursor, + 1, "", 1, "", "Jason Smitt", 0, 0, 0, 0, 0, 0); + final ContactNumber alphabet = constructNewContact(contactCursor, nameCursor, + 0, "12345678", 0, "", "abc def ghi jkl mno pqrs tuv wxyz", 0, 0, 0, 0, 0, 0); + + mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0)); + mTestHelper.insertNamePrefixes(db, nameCursor); + + nameCursor.close(); + contactCursor.close(); + + final ArrayList<ContactNumber> result1 = getLooseMatchesFromDb("5276676484"); + assertFalse(result1.contains(jasonsmitt)); + + final ArrayList<ContactNumber> result2 = getLooseMatchesFromDb("5276676488"); + assertFalse(result2.contains(jasonsmith)); + assertTrue(result2.contains(jasonsmitt)); + + assertTrue(getLooseMatchesFromDb("22233344455566677778889999").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("33344455566677778889999").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("44455566677778889999").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("55566677778889999").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("66677778889999").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("77778889999").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("8889999").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("9999").contains(alphabet)); + + // Makes sure the phone number is correctly added. + assertTrue(getLooseMatchesFromDb("12345678").contains(alphabet)); + } + + public void testPutForPartialName() { + final SQLiteDatabase db = mTestHelper.getWritableDatabase(); + + final MatrixCursor nameCursor = constructNewNameCursor(); + final MatrixCursor contactCursor = constructNewContactCursor(); + final ContactNumber maryjane = constructNewContact(contactCursor, nameCursor, + 0, "", 0, "", "Mary Jane", 0, 0, 0, 0, 0, 0); + final ContactNumber sarahsmith = constructNewContact(contactCursor, nameCursor, + 0, "", 1, "", "Sarah Smith", 0, 0, 0, 0, 0, 0); + final ContactNumber jasonsmitt = constructNewContact(contactCursor, nameCursor, + 0, "", 2, "", "Jason Smitt", 0, 0, 0, 0, 0, 0); + + mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0)); + mTestHelper.insertNamePrefixes(db, nameCursor); + + nameCursor.close(); + contactCursor.close(); + + final ArrayList<ContactNumber> result1 = getLooseMatchesFromDb("6279"); + assertTrue(result1.contains(maryjane)); + assertFalse(result1.contains(jasonsmitt)); + + // 72 corresponds to sa = "Sarah Smith" but not "Jason Smitt" or "Mary Jane" + final ArrayList<ContactNumber> result2 = getLooseMatchesFromDb("72"); + assertFalse(result2.contains(maryjane)); + assertTrue(result2.contains(sarahsmith)); + assertFalse(result2.contains(jasonsmitt)); + + // 76 corresponds to sm = "Sarah Smith" and "Jason Smitt" but not "Mary Jane" + final ArrayList<ContactNumber> result3 = getLooseMatchesFromDb("76"); + assertFalse(result3.contains(maryjane)); + assertTrue(result3.contains(sarahsmith)); + assertTrue(result3.contains(jasonsmitt)); + } + + public void testPutForNameTokens() { + final SQLiteDatabase db = mTestHelper.getWritableDatabase(); + + final MatrixCursor nameCursor = constructNewNameCursor(); + final MatrixCursor contactCursor = constructNewContactCursor(); + final ContactNumber jasonfwilliams = constructNewContact(contactCursor, nameCursor, + 0, "", 0, "", "Jason F. Williams", 0, 0, 0, 0, 0, 0); + + mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0)); + mTestHelper.insertNamePrefixes(db, nameCursor); + + nameCursor.close(); + contactCursor.close(); + + assertTrue(getLooseMatchesFromDb("527").contains(jasonfwilliams)); + // 72 corresponds to sa = "Sarah Smith" but not "Jason Smitt" or "Mary Jane" + assertTrue(getLooseMatchesFromDb("945").contains(jasonfwilliams)); + // 76 corresponds to sm = "Sarah Smith" and "Jason Smitt" but not "Mary Jane" + assertFalse(getLooseMatchesFromDb("66").contains(jasonfwilliams)); + } + + public void testPutForInitialMatches() { + final SQLiteDatabase db = mTestHelper.getWritableDatabase(); + + final MatrixCursor nameCursor = constructNewNameCursor(); + final MatrixCursor contactCursor = constructNewContactCursor(); + final ContactNumber martinjuniorharry = constructNewContact(contactCursor, nameCursor, + 0, "", 0, "", "Martin Jr Harry", 0, 0, 0, 0, 0, 0); + + mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0)); + mTestHelper.insertNamePrefixes(db, nameCursor); + + nameCursor.close(); + contactCursor.close(); + + // 654 corresponds to mjh = "(M)artin (J)r (H)arry" + assertTrue(getLooseMatchesFromDb("654").contains(martinjuniorharry)); + // The reverse (456) does not match (for now) + assertFalse(getLooseMatchesFromDb("456").contains(martinjuniorharry)); + // 6542 corresponds to mjha = "(M)artin (J)r (Ha)rry" + assertTrue(getLooseMatchesFromDb("6542").contains(martinjuniorharry)); + // 542 corresponds to jha = "Martin (J)r (Ha)rry" + assertTrue(getLooseMatchesFromDb("542").contains(martinjuniorharry)); + // 642 corresponds to mha = "(M)artin Jr (Ha)rry" + assertTrue(getLooseMatchesFromDb("642").contains(martinjuniorharry)); + // 6542779 (M)artin (J)r (Harry) + assertTrue(getLooseMatchesFromDb("6542779").contains(martinjuniorharry)); + // 65742779 (M)artin (Jr) (Harry) + assertTrue(getLooseMatchesFromDb("65742779").contains(martinjuniorharry)); + // 542779 Martin (J)r (Harry) + assertTrue(getLooseMatchesFromDb("542779").contains(martinjuniorharry)); + // 547 doesn't match + assertFalse(getLooseMatchesFromDb("547").contains(martinjuniorharry)); + // 655 doesn't match + assertFalse(getLooseMatchesFromDb("655").contains(martinjuniorharry)); + // 653 doesn't match + assertFalse(getLooseMatchesFromDb("653").contains(martinjuniorharry)); + // 6543 doesn't match + assertFalse(getLooseMatchesFromDb("6543").contains(martinjuniorharry)); + + assertEquals(7, mTestHelper.countPrefixTableRows(db)); + } + + public void testPutForInitialMatchesForLongTokenNames() { + + final SQLiteDatabase db = mTestHelper.getWritableDatabase(); + + final MatrixCursor nameCursor = constructNewNameCursor(); + final MatrixCursor contactCursor = constructNewContactCursor(); + final ContactNumber alphabet = constructNewContact(contactCursor, nameCursor, + 0, "12345678", 0, "", "abc def ghi jkl mno pqrs tuv wxyz", 0, 0, 0, 0, 0, 0); + + mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0)); + mTestHelper.insertNamePrefixes(db, nameCursor); + + nameCursor.close(); + contactCursor.close(); + + // Makes sure only only the first two and last two token are considered for initials. + // The cut-off constant can be set in SmartDialPrefix.java + assertTrue(getLooseMatchesFromDb("2389999").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("239999").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("23888").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("2333").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("289999").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("2888").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("29999").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("3888").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("39999").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("389999").contains(alphabet)); + assertTrue(getLooseMatchesFromDb("89999").contains(alphabet)); + } + + public void testCheckLongToken() { + final SQLiteDatabase db = mTestHelper.getWritableDatabase(); + + final MatrixCursor nameCursor = constructNewNameCursor(); + final MatrixCursor contactCursor = constructNewContactCursor(); + final ContactNumber alphabet = constructNewContact(contactCursor, nameCursor, + 0, "1", 0, "", " aaaa bbbb cccc dddd eeee ffff gggg" + + " hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv " + + " wwww xxxx yyyy zzzz", 0, 0, 0, 0, 0, 0); + + final ContactNumber alphabet2 = constructNewContact(contactCursor, nameCursor, + 0, "1", 1, "", "aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnoooopppp" + + "qqqqrrrrssssttttuuuuvvvvwwwwxxxxyyyyzzzz", 0, 0, 0, 0, 0, 0); + + mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0)); + mTestHelper.insertNamePrefixes(db, nameCursor); + + nameCursor.close(); + contactCursor.close(); + + assertTrue(getLooseMatchesFromDb("2222").contains(alphabet)); + assertEquals(40, mTestHelper.countPrefixTableRows(db)); + } + + public void testParseInfo() { + final String name = "Mcdonald Jamie-Cullum"; + final ArrayList<String> info = SmartDialPrefix.parseToIndexTokens(name); + assertEquals(3, info.size()); + assertEquals(8, info.get(0).length()); + assertEquals(5, info.get(1).length()); + assertEquals(6, info.get(2).length()); + + final String name2 = "aaa bbb ccc ddd eee fff ggg hhh iii jjj kkk"; + final ArrayList<String> info2 = SmartDialPrefix.parseToIndexTokens(name2); + assertEquals(11, info2.size()); + assertEquals(3, info2.get(0).length()); + assertEquals(3, info2.get(10).length()); + + final String name3 = "this is- a,test name"; + final ArrayList<String> info3 = SmartDialPrefix.parseToIndexTokens(name3); + assertEquals(5, info3.size()); + assertEquals(2, info3.get(1).length()); + assertEquals(1, info3.get(2).length()); + assertEquals(4, info3.get(3).length()); + assertEquals(4, info3.get(4).length()); + + final String name4 = "M c-Donald James"; + final ArrayList<String> info4 = SmartDialPrefix.parseToIndexTokens(name4); + assertEquals(4, info4.size()); + assertEquals(1, info4.get(1).length()); + assertEquals(6, info4.get(2).length()); + + final String name5 = " Aa'Bb c dddd e'e"; + final ArrayList<String> info5 = SmartDialPrefix.parseToIndexTokens(name5); + assertEquals(6, info5.size()); + assertEquals(2, info5.get(0).length()); + assertEquals(1, info5.get(5).length()); + } + + public void testAccentedCharacters() { + final SQLiteDatabase db = mTestHelper.getWritableDatabase(); + + final MatrixCursor nameCursor = constructNewNameCursor(); + final MatrixCursor contactCursor = constructNewContactCursor(); + final ContactNumber reene = constructNewContact(contactCursor, nameCursor, + 0, "0", 0, "", "Reenée", 0, 0, 0, 0, 0, 0); + final ContactNumber bronte = constructNewContact(contactCursor, nameCursor, + 0, "0", 1, "", "Brontë", 0, 0, 0, 0, 0, 0); + + mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0)); + mTestHelper.insertNamePrefixes(db, nameCursor); + + nameCursor.close(); + contactCursor.close(); + + assertTrue(getLooseMatchesFromDb("733633").contains(reene)); + assertTrue(getLooseMatchesFromDb("276683").contains(bronte)); + } + + public void testNumbersInName() { + final SQLiteDatabase db = mTestHelper.getWritableDatabase(); + + final MatrixCursor nameCursor = constructNewNameCursor(); + final MatrixCursor contactCursor = constructNewContactCursor(); + final ContactNumber contact = constructNewContact(contactCursor, nameCursor, + 0, "0", 0, "", "12345678", 0, 0, 0, 0, 0, 0); + final ContactNumber teacher = constructNewContact(contactCursor, nameCursor, + 0, "0", 1, "", "1st Grade Teacher", 0, 0, 0, 0, 0, 0); + + mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0)); + mTestHelper.insertNamePrefixes(db, nameCursor); + + nameCursor.close(); + contactCursor.close(); + + assertTrue(getLooseMatchesFromDb("12345678").contains(contact)); + assertTrue(getLooseMatchesFromDb("17847233").contains(teacher)); + assertTrue(getLooseMatchesFromDb("14832").contains(teacher)); + } + + public void testPutForNumbers() { + final SQLiteDatabase db = mTestHelper.getWritableDatabase(); + + final MatrixCursor nameCursor = constructNewNameCursor(); + final MatrixCursor contactCursor = constructNewContactCursor(); + final ContactNumber contactno1 = constructNewContact(contactCursor, nameCursor, + 0, "510-527-2357", 0, "", "James", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno2 = constructNewContact(contactCursor, nameCursor, + 0, "77212862357", 1, "", "James", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno3 = constructNewContact(contactCursor, nameCursor, + 0, "+13684976334", 2, "", "James", 0, 0, 0, 0, 0, 0); + + mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0)); + mTestHelper.insertNamePrefixes(db, nameCursor); + + nameCursor.close(); + contactCursor.close(); + + assertTrue(getLooseMatchesFromDb("510").contains(contactno1)); + assertFalse(getLooseMatchesFromDb("511").contains(contactno1)); + assertTrue(getLooseMatchesFromDb("77212862357").contains(contactno2)); + assertFalse(getLooseMatchesFromDb("77212862356").contains(contactno2)); + assertTrue(getLooseMatchesFromDb("1368").contains(contactno3)); + assertFalse(getLooseMatchesFromDb("1367").contains(contactno3)); + } + + public void testPutNumbersCountryCode() { + final SQLiteDatabase db = mTestHelper.getWritableDatabase(); + + final MatrixCursor nameCursor = constructNewNameCursor(); + final MatrixCursor contactCursor = constructNewContactCursor(); + final ContactNumber contactno1 = constructNewContact(contactCursor, nameCursor, + 0, "+13684976334", 0, "", "James", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno2 = constructNewContact(contactCursor, nameCursor, + 0, "+65 9177-6930", 1, "", "Jason", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno3 = constructNewContact(contactCursor, nameCursor, + 0, "+85212345678", 2, "", "Mike", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno4 = constructNewContact(contactCursor, nameCursor, + 0, "+85112345678", 3, "", "Invalid", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno5 = constructNewContact(contactCursor, nameCursor, + 0, "+852", 4, "", "Invalid", 0, 0, 0, 0, 0, 0); + + mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0)); + mTestHelper.insertNamePrefixes(db, nameCursor); + + nameCursor.close(); + contactCursor.close(); + + assertTrue(getLooseMatchesFromDb("1368").contains(contactno1)); + assertTrue(getLooseMatchesFromDb("368497").contains(contactno1)); + assertFalse(getLooseMatchesFromDb("2368497").contains(contactno1)); + + assertTrue(getLooseMatchesFromDb("6591776930").contains(contactno2)); + assertTrue(getLooseMatchesFromDb("91776930").contains(contactno2)); + assertFalse(getLooseMatchesFromDb("591776930").contains(contactno2)); + + assertTrue(getLooseMatchesFromDb("85212345678").contains(contactno3)); + assertTrue(getLooseMatchesFromDb("12345678").contains(contactno3)); + assertFalse(getLooseMatchesFromDb("5212345678").contains(contactno3)); + + assertTrue(getLooseMatchesFromDb("85112345678").contains(contactno4)); + assertFalse(getLooseMatchesFromDb("12345678").contains(contactno4)); + } + + // Tests special case handling for NANP numbers + public void testPutNumbersNANP() { + SmartDialPrefix.setUserInNanpRegion(true); + final SQLiteDatabase db = mTestHelper.getWritableDatabase(); + + final MatrixCursor nameCursor = constructNewNameCursor(); + final MatrixCursor contactCursor = constructNewContactCursor(); + final ContactNumber contactno1 = constructNewContact(contactCursor, nameCursor, + 0, "16503337596", 0, "", "James", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno2 = constructNewContact(contactCursor, nameCursor, + 0, "5109921234", 1, "", "Michael", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno3 = constructNewContact(contactCursor, nameCursor, + 0, "(415)-123-4567", 2, "", "Jason", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno4 = constructNewContact(contactCursor, nameCursor, + 0, "1 510-284-9170", 3, "", "Mike", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno5 = constructNewContact(contactCursor, nameCursor, + 0, "1-415-123-123", 4, "", "Invalid", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno6 = constructNewContact(contactCursor, nameCursor, + 0, "415-123-123", 5, "", "Invalid2", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno7 = constructNewContact(contactCursor, nameCursor, + 0, "+1-510-284-9170", 6, "", "Mike", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno8 = constructNewContact(contactCursor, nameCursor, + 0, "+1-510-284-917", 7, "", "Invalid", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno9 = constructNewContact(contactCursor, nameCursor, + 0, "+857-510-284-9170", 8, "", "Inv", 0, 0, 0, 0, 0, 0); + + mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0)); + mTestHelper.insertNamePrefixes(db, nameCursor); + + nameCursor.close(); + contactCursor.close(); + + assertTrue(getLooseMatchesFromDb("16503337596").contains(contactno1)); + assertTrue(getLooseMatchesFromDb("6503337596").contains(contactno1)); + assertTrue(getLooseMatchesFromDb("3337596").contains(contactno1)); + + assertTrue(getLooseMatchesFromDb("5109921234").contains(contactno2)); + assertTrue(getLooseMatchesFromDb("9921234").contains(contactno2)); + + assertTrue(getLooseMatchesFromDb("4151234567").contains(contactno3)); + assertTrue(getLooseMatchesFromDb("1234567").contains(contactno3)); + + assertTrue(getLooseMatchesFromDb("15102849170").contains(contactno4)); + assertTrue(getLooseMatchesFromDb("5102849170").contains(contactno4)); + assertTrue(getLooseMatchesFromDb("2849170").contains(contactno4)); + + assertTrue(getLooseMatchesFromDb("1415123123").contains(contactno5)); + assertFalse(getLooseMatchesFromDb("415123123").contains(contactno5)); + assertFalse(getLooseMatchesFromDb("123123").contains(contactno5)); + + assertTrue(getLooseMatchesFromDb("415123123").contains(contactno6)); + assertFalse(getLooseMatchesFromDb("123123").contains(contactno6)); + + assertTrue(getLooseMatchesFromDb("15102849170").contains(contactno7)); + assertTrue(getLooseMatchesFromDb("5102849170").contains(contactno7)); + assertTrue(getLooseMatchesFromDb("2849170").contains(contactno7)); + assertFalse(getLooseMatchesFromDb("849170").contains(contactno7)); + assertFalse(getLooseMatchesFromDb("10849170").contains(contactno7)); + + assertTrue(getLooseMatchesFromDb("1510284917").contains(contactno8)); + assertTrue(getLooseMatchesFromDb("510284917").contains(contactno8)); + assertFalse(getLooseMatchesFromDb("2849170").contains(contactno8)); + + assertTrue(getLooseMatchesFromDb("8575102849170").contains(contactno9)); + assertFalse(getLooseMatchesFromDb("5102849170").contains(contactno9)); + assertFalse(getLooseMatchesFromDb("2849170").contains(contactno9)); + +// // If user's region is determined to be not in North America, then the NANP number +// // workarounds should not be applied +// final SmartDialTrie trieNonNANP = new SmartDialTrie(); +// +// trieNonNANP.put(contactno3); +// assertTrue(checkContains(trieNonNANP, contactno3, "4151234567")); +// assertFalse(checkContains(trieNonNANP, contactno3, "1234567")); +// +// trieNonNANP.put(contactno4); +// assertTrue(checkContains(trieNonNANP, contactno4, "15102849170")); +// assertFalse(checkContains(trieNonNANP, contactno4, "5102849170")); +// assertFalse(checkContains(trieNonNANP, contactno4, "2849170")); + } + + // Tests special case handling for non-NANP numbers + public void testPutNumbersNonNANP() { + SmartDialPrefix.setUserInNanpRegion(false); + final SQLiteDatabase db = mTestHelper.getWritableDatabase(); + + final MatrixCursor nameCursor = constructNewNameCursor(); + final MatrixCursor contactCursor = constructNewContactCursor(); + + final ContactNumber contactno0 = constructNewContact(contactCursor, nameCursor, + 0, "(415)-123-4567", 0, "", "Jason", 0, 0, 0, 0, 0, 0); + final ContactNumber contactno1 = constructNewContact(contactCursor, nameCursor, + 0, "1 510-284-9170", 1, "", "Mike", 0, 0, 0, 0, 0, 0); + + + mTestHelper.insertUpdatedContactsAndNumberPrefix(db, contactCursor, Long.valueOf(0)); + mTestHelper.insertNamePrefixes(db, nameCursor); + + nameCursor.close(); + contactCursor.close(); + + assertTrue(getLooseMatchesFromDb("4151234567").contains(contactno0)); + assertFalse(getLooseMatchesFromDb("1234567").contains(contactno0)); + + assertTrue(getLooseMatchesFromDb("15102849170").contains(contactno1)); + assertFalse(getLooseMatchesFromDb("5102849170").contains(contactno1)); + assertFalse(getLooseMatchesFromDb("2849170").contains(contactno1)); + } +} diff --git a/tests/src/com/android/dialer/dialpad/SmartDialCacheTest.java b/tests/src/com/android/dialer/dialpad/SmartDialCacheTest.java deleted file mode 100644 index 8d96edafc..000000000 --- a/tests/src/com/android/dialer/dialpad/SmartDialCacheTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.dialer.dialpad; - -import android.test.suitebuilder.annotation.SmallTest; - -import junit.framework.TestCase; - -@SmallTest -public class SmartDialCacheTest extends TestCase { - public void testIsCountryNanp_CaseInsensitive() { - assertFalse(SmartDialCache.isCountryNanp(null)); - assertFalse(SmartDialCache.isCountryNanp("CN")); - assertFalse(SmartDialCache.isCountryNanp("HK")); - assertFalse(SmartDialCache.isCountryNanp("uk")); - assertFalse(SmartDialCache.isCountryNanp("sg")); - assertTrue(SmartDialCache.isCountryNanp("US")); - assertTrue(SmartDialCache.isCountryNanp("CA")); - assertTrue(SmartDialCache.isCountryNanp("AS")); - assertTrue(SmartDialCache.isCountryNanp("AI")); - assertTrue(SmartDialCache.isCountryNanp("AG")); - assertTrue(SmartDialCache.isCountryNanp("BS")); - assertTrue(SmartDialCache.isCountryNanp("BB")); - assertTrue(SmartDialCache.isCountryNanp("bm")); - assertTrue(SmartDialCache.isCountryNanp("vg")); - assertTrue(SmartDialCache.isCountryNanp("ky")); - assertTrue(SmartDialCache.isCountryNanp("dm")); - assertTrue(SmartDialCache.isCountryNanp("do")); - assertTrue(SmartDialCache.isCountryNanp("gd")); - assertTrue(SmartDialCache.isCountryNanp("gu")); - assertTrue(SmartDialCache.isCountryNanp("jm")); - assertTrue(SmartDialCache.isCountryNanp("pr")); - assertTrue(SmartDialCache.isCountryNanp("ms")); - assertTrue(SmartDialCache.isCountryNanp("mp")); - assertTrue(SmartDialCache.isCountryNanp("kn")); - assertTrue(SmartDialCache.isCountryNanp("lc")); - assertTrue(SmartDialCache.isCountryNanp("vc")); - assertTrue(SmartDialCache.isCountryNanp("tt")); - assertTrue(SmartDialCache.isCountryNanp("tc")); - assertTrue(SmartDialCache.isCountryNanp("vi")); - } -} diff --git a/tests/src/com/android/dialer/dialpad/SmartDialNameMatcherTest.java b/tests/src/com/android/dialer/dialpad/SmartDialNameMatcherTest.java index 47edaf371..1e578eee7 100644 --- a/tests/src/com/android/dialer/dialpad/SmartDialNameMatcherTest.java +++ b/tests/src/com/android/dialer/dialpad/SmartDialNameMatcherTest.java @@ -19,8 +19,10 @@ package com.android.dialer.dialpad; import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.Suppress; import android.util.Log; +import android.test.AndroidTestCase; import com.android.dialer.dialpad.SmartDialNameMatcher; +import com.android.dialer.dialpad.SmartDialPrefix; import java.text.Normalizer; import java.util.ArrayList; @@ -237,8 +239,7 @@ public class SmartDialNameMatcherTest extends TestCase { private void checkMatchesNumber(String number, String query, boolean expectedMatches, boolean matchNanp, int matchStart, int matchEnd) { final SmartDialNameMatcher matcher = new SmartDialNameMatcher(query); - final SmartDialMatchPosition pos = matcher.matchesNumber(number, query, - matchNanp); + final SmartDialMatchPosition pos = matcher.matchesNumber(number, query, matchNanp); assertEquals(expectedMatches, pos != null); if (expectedMatches) { assertEquals("start", matchStart, pos.start); diff --git a/tests/src/com/android/dialer/dialpad/SmartDialTrieTest.java b/tests/src/com/android/dialer/dialpad/SmartDialTrieTest.java deleted file mode 100644 index f0c4cbb46..000000000 --- a/tests/src/com/android/dialer/dialpad/SmartDialTrieTest.java +++ /dev/null @@ -1,412 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.dialer.dialpad; - -import static com.android.dialer.dialpad.SmartDialCache.ContactNumber; - -import com.android.dialer.dialpad.SmartDialTrie.Node; -import com.android.dialer.dialpad.SmartDialTrie.ParseInfo; - -import android.test.suitebuilder.annotation.SmallTest; - -import junit.framework.TestCase; - -/** - * To run this test, use the command: - * adb shell am instrument -w -e class com.android.dialer.dialpad.SmartDialTrieTest / - * com.android.dialer.tests/android.test.InstrumentationTestRunner - */ -@SmallTest -public class SmartDialTrieTest extends TestCase{ - - public void testSize() { - final SmartDialTrie trie = new SmartDialTrie(); - trie.put(new ContactNumber(0, "Jason", "0", "0", 1)); - assertEquals(1, trie.size()); - trie.put(new ContactNumber(1, "Mary", "0", "1", 2)); - assertEquals(2, trie.size()); - trie.put(new ContactNumber(2, "John", "0", "2", 3)); - assertEquals(3, trie.size()); - } - - public void testPutForFullName() { - final SmartDialTrie trie = new SmartDialTrie(); - final ContactNumber jasonsmith = new ContactNumber(0, "Jason Smith", "0", "0", 1); - final ContactNumber jasonsmitt = new ContactNumber(1, "Jason Smitt", "0", "1", 2); - trie.put(jasonsmith); - trie.put(jasonsmitt); - assertTrue(trie.getAllWithPrefix("5276676484").contains(jasonsmith)); - assertFalse(trie.getAllWithPrefix("5276676484").contains(jasonsmitt)); - - assertFalse(trie.getAllWithPrefix("5276676488").contains(jasonsmith)); - assertTrue(trie.getAllWithPrefix("5276676488").contains(jasonsmitt)); - - } - - public void testPutForPartialName() { - final SmartDialTrie trie = new SmartDialTrie(); - final ContactNumber maryjane = new ContactNumber(0, "Mary Jane", "0", "0", 1); - final ContactNumber sarahsmith = new ContactNumber(1, "Sarah Smith", "0", "1", 2); - final ContactNumber jasonsmitt = new ContactNumber(2, "Jason Smitt", "0", "2", 3); - trie.put(maryjane); - trie.put(sarahsmith); - trie.put(jasonsmitt); - - // 6279 corresponds to mary = "Mary Jane" but not "Jason Smitt" - assertTrue(checkContains(trie, maryjane, "6279")); - assertFalse(checkContains(trie, jasonsmitt, "6279")); - - // 72 corresponds to sa = "Sarah Smith" but not "Jason Smitt" or "Mary Jane" - assertFalse(checkContains(trie, maryjane, "72")); - assertTrue(checkContains(trie, sarahsmith, "72")); - assertFalse(checkContains(trie, jasonsmitt, "72")); - - // 76 corresponds to sm = "Sarah Smith" and "Jason Smitt" but not "Mary Jane" - assertFalse(checkContains(trie, maryjane, "76")); - assertTrue(checkContains(trie, sarahsmith, "76")); - assertTrue(checkContains(trie, jasonsmitt, "76")); - } - - public void testPutForNameTokens() { - final SmartDialTrie trie = new SmartDialTrie(); - final ContactNumber jasonfwilliams = new ContactNumber(0, "Jason F. Williams", "0", "0", 1); - trie.put(jasonfwilliams); - - // 527 corresponds to jas = "Jason" - assertTrue(checkContains(trie, jasonfwilliams, "527")); - // 945 corresponds to wil = "Wil" - assertTrue(checkContains(trie, jasonfwilliams, "945")); - // 66 doesn't match - assertFalse(checkContains(trie, jasonfwilliams, "66")); - } - - public void testPutForInitialMatches() { - final SmartDialTrie trie = new SmartDialTrie(); - final ContactNumber martinjuniorharry = - new ContactNumber(0, "Martin Jr Harry", "0", "0", 1); - trie.put(martinjuniorharry); - // 654 corresponds to mjh = "(M)artin (J)r (H)arry" - assertTrue(checkContains(trie, martinjuniorharry, "654")); - // The reverse (456) does not match (for now) - assertFalse(checkContains(trie, martinjuniorharry, "456")); - // 6542 corresponds to mjha = "(M)artin (J)r (Ha)rry" - assertTrue(checkContains(trie, martinjuniorharry, "6542")); - // 542 corresponds to jha = "Martin (J)r (Ha)rry" - assertTrue(checkContains(trie, martinjuniorharry, "542")); - // 642 corresponds to mha = "(M)artin Jr (Ha)rry" - assertTrue(checkContains(trie, martinjuniorharry, "642")); - // 6542779 (M)artin (J)r (Harry) - assertTrue(checkContains(trie, martinjuniorharry, "6542779")); - // 65742779 (M)artin (Jr) (Harry) - assertTrue(checkContains(trie, martinjuniorharry, "65742779")); - // 542779 Martin (J)r (Harry) - assertTrue(checkContains(trie, martinjuniorharry, "542779")); - // 547 doesn't match - assertFalse(checkContains(trie, martinjuniorharry, "547")); - // 655 doesn't match - assertFalse(checkContains(trie, martinjuniorharry, "655")); - // 653 doesn't match - assertFalse(checkContains(trie, martinjuniorharry, "653")); - // 6543 doesn't match - assertFalse(checkContains(trie, martinjuniorharry, "6543")); - // 7(2^3 -1) entries for the name, and 1 for the number - assertEquals(8, trie.numEntries()); - } - - public void testPutForInitialMatchesCombinations() { - final SmartDialTrie trie = new SmartDialTrie(); - final ContactNumber alphabet = new ContactNumber(0, "abc def ghi jkl mno pqrs tuv wxyz", - "12345678", "1", 2); - trie.put(alphabet); - assertEquals(20, trie.numEntries()); - // 8 name entries (abcdefghi..., defghi..., ...) - assertTrue(checkContains(trie, alphabet, "22233344455566677778889999")); - assertTrue(checkContains(trie, alphabet, "33344455566677778889999")); - assertTrue(checkContains(trie, alphabet, "44455566677778889999")); - assertTrue(checkContains(trie, alphabet, "55566677778889999")); - assertTrue(checkContains(trie, alphabet, "66677778889999")); - assertTrue(checkContains(trie, alphabet, "77778889999")); - assertTrue(checkContains(trie, alphabet, "8889999")); - assertTrue(checkContains(trie, alphabet, "9999")); - // 1 number entry - assertTrue(checkContains(trie, alphabet, "12345678")); - // 11 initial entries (adtw, adw, adt, ad, atw, at, aw, dt, dw, dtw, tw) - // 4c2(6) + 4c3(4) + 4c4(1) - assertTrue(checkContains(trie, alphabet, "2389999")); - assertTrue(checkContains(trie, alphabet, "239999")); - assertTrue(checkContains(trie, alphabet, "23888")); - assertTrue(checkContains(trie, alphabet, "2333")); - assertTrue(checkContains(trie, alphabet, "289999")); - assertTrue(checkContains(trie, alphabet, "2888")); - assertTrue(checkContains(trie, alphabet, "29999")); - assertTrue(checkContains(trie, alphabet, "3888")); - assertTrue(checkContains(trie, alphabet, "39999")); - assertTrue(checkContains(trie, alphabet, "389999")); - assertTrue(checkContains(trie, alphabet, "89999")); - } - - public void testCheckLongToken() { - final SmartDialTrie trie = new SmartDialTrie(); - final ContactNumber alphabet = new ContactNumber(0, " aaaa bbbb cccc dddd eeee ffff gggg" + - " hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv " + - " wwww xxxx yyyy zzzz", "1", "1", 2); - // Make sure the check to prevent overly long tokens from causing an OOM kicks in - trie.put(alphabet); - assertTrue(checkContains(trie, alphabet, "2222")); - // 26 name entries (aaaabbbbcccc...., bbbbccccdddd...., ccccdddd...) - // 1 number entry - // 11 initial entries 4c2(6) + 4c3(4) + 4c4(1) - assertEquals(38, trie.numEntries()); - - final ContactNumber alphabet2 = new ContactNumber(0, "aaaabbbbccccddddeeeeffffgggg" + - "hhhhiiiijjjjkkkkllllmmmmnnnnooooppppqqqqrrrrssssttttuuuuvvvvwwwwxxxxyyyyzzzz", - "1", "1", 2); - trie.put(alphabet2); - // added one name, and one number entry - assertEquals(40, trie.numEntries()); - } - - public void testParseInfo() { - final SmartDialTrie trie = new SmartDialTrie(); - final String name = "Mcdonald Jamie-Cullum"; - final ParseInfo info = trie.parseToIndexes(name, 2, 2); - // Make sure the dash is correctly converted to a separator character - for (int i = 0; i < name.length(); i++) { - // separators at position 8 and 12 - if (i == 8 || i == 14) { - assertTrue(info.indexes[i] == -1); - } else { - assertFalse(info.indexes[i] == -1); - } - } - assertEquals(14, info.nthFirstTokenPos); - assertEquals(8, info.nthLastTokenPos); - - final String name2 = "aaa bbb ccc ddd eee fff ggg hhh iii jjj kkk"; - final ParseInfo info2 = trie.parseToIndexes(name2, 2, 2); - assertEquals(7, info2.nthFirstTokenPos); - assertEquals(35, info2.nthLastTokenPos); - - final String name3 = "this is- a,test name"; - final ParseInfo info3 = trie.parseToIndexes(name3, 3, 3); - assertEquals(11, info3.nthFirstTokenPos); - assertEquals(8, info3.nthLastTokenPos); - - final String name4 = "M c-Donald James"; - final ParseInfo info4 = trie.parseToIndexes(name4, 2, 3); - assertEquals(3, info4.nthFirstTokenPos); - assertEquals(1, info4.nthLastTokenPos); - - final String name5 = " Aa'Bb c dddd e'e"; - final ParseInfo info5 = trie.parseToIndexes(name5, 4, 4); - assertEquals(21, info5.nthFirstTokenPos); - assertEquals(8, info5.nthLastTokenPos); - } - - public void testAccentedCharacters() { - final SmartDialTrie trie = new SmartDialTrie(); - final ContactNumber reenee = new ContactNumber(0, "Reenée", "0", "0", 1); - final ContactNumber bronte = new ContactNumber(2, "Brontë", "0", "1", 2); - trie.put(reenee); - trie.put(bronte); - assertTrue(checkContains(trie, reenee, "733633")); - assertTrue(checkContains(trie, bronte, "276683")); - } - - public void testNumbersInName() { - final SmartDialTrie trie = new SmartDialTrie(); - final ContactNumber contact = new ContactNumber(0, "12345678", "0", "0", 1); - final ContactNumber teacher = new ContactNumber(1, "1st Grade Teacher", "0", "1", 2); - trie.put(contact); - trie.put(teacher); - assertTrue(checkContains(trie, contact, "12345678")); - // (1st Grade) Teacher - assertTrue(checkContains(trie, teacher, "17847233")); - // (1)st (G)rade (Tea)cher - assertTrue(checkContains(trie, teacher, "14832")); - } - - public void testPutForNumbers() { - final SmartDialTrie trie = new SmartDialTrie(); - final ContactNumber contactno1 = new ContactNumber(0, "James", "510-527-2357", "0", 1); - trie.put(contactno1); - final ContactNumber contactno2 = new ContactNumber(0, "James", "77212862357", "0", 1); - trie.put(contactno2); - final ContactNumber contactno3 = new ContactNumber(0, "James", "+13684976334", "0", 1); - trie.put(contactno3); - // all phone numbers belonging to the contact should correspond to it - assertTrue(checkContains(trie, contactno1, "510")); - assertFalse(checkContains(trie, contactno1, "511")); - assertTrue(checkContains(trie, contactno2, "77212862357")); - assertFalse(checkContains(trie, contactno2, "77212862356")); - assertTrue(checkContains(trie, contactno3, "1368")); - assertFalse(checkContains(trie, contactno3, "1367")); - - } - - public void testPutNumbersCountryCode() { - final SmartDialTrie trie = new SmartDialTrie(); - final ContactNumber contactno1 = new ContactNumber(0, "James", "+13684976334", "0", 1); - trie.put(contactno1); - - // all phone numbers belonging to the contact should correspond to it - assertTrue(checkContains(trie, contactno1, "1368")); - assertTrue(checkContains(trie, contactno1, "368497")); - assertFalse(checkContains(trie, contactno1, "2368497")); - - final ContactNumber contactno2 = new ContactNumber(0, "Jason", "+65 9177-6930", "0", 1); - trie.put(contactno2); - - assertTrue(checkContains(trie, contactno2, "6591776930")); - assertTrue(checkContains(trie, contactno2, "91776930")); - assertFalse(checkContains(trie, contactno2, "591776930")); - - final ContactNumber contactno3 = new ContactNumber(0, "Mike", "+85212345678", "0", 1); - trie.put(contactno3); - assertTrue(checkContains(trie, contactno3, "85212345678")); - assertTrue(checkContains(trie, contactno3, "12345678")); - assertFalse(checkContains(trie, contactno2, "5212345678")); - - // Invalid country code, don't try to parse it - final ContactNumber contactno4 = new ContactNumber(0, "Invalid", "+85112345678", "0", 1); - trie.put(contactno4); - assertTrue(checkContains(trie, contactno4, "85112345678")); - assertFalse(checkContains(trie, contactno4, "12345678")); - - final ContactNumber contactno5 = new ContactNumber(0, "Invalid", "+852", "0", 1); - // Shouldn't crash - trie.put(contactno5); - } - - // Tests special case handling for NANP numbers - public void testPutNumbersNANP() { - final SmartDialTrie trie = new SmartDialTrie(true /* formatNanp */); - // Unformatted number with 1 prefix - final ContactNumber contactno1 = new ContactNumber(0, "James", "16503337596", "0", 1); - trie.put(contactno1); - - assertTrue(checkContains(trie, contactno1, "16503337596")); - assertTrue(checkContains(trie, contactno1, "6503337596")); - assertTrue(checkContains(trie, contactno1, "3337596")); - - // Number with seperators - final ContactNumber contactno2 = new ContactNumber(0, "Michael", "5109921234", "0", 1); - trie.put(contactno2); - assertTrue(checkContains(trie, contactno2, "5109921234")); - assertTrue(checkContains(trie, contactno2, "9921234")); - - // Number with area code only + separators - final ContactNumber contactno3 = new ContactNumber(0, "Jason", "(415)-123-4567", "0", 1); - trie.put(contactno3); - assertTrue(checkContains(trie, contactno3, "4151234567")); - assertTrue(checkContains(trie, contactno3, "1234567")); - - // Number without +1 prefix but is a NANP number - final ContactNumber contactno4 = new ContactNumber(0, "Mike", "1 510-284-9170", "0", 1); - trie.put(contactno4); - assertTrue(checkContains(trie, contactno4, "15102849170")); - assertTrue(checkContains(trie, contactno4, "5102849170")); - assertTrue(checkContains(trie, contactno4, "2849170")); - - // Invalid number(has 1 prefix, but is only 10 characters long) - final ContactNumber contactno5 = new ContactNumber(0, "Invalid", "1-415-123-123", "0", 1); - trie.put(contactno5); - // It should still be inserted as is - assertTrue(checkContains(trie, contactno5, "1415123123")); - // But the NANP special case handling should not work - assertFalse(checkContains(trie, contactno5, "415123123")); - assertFalse(checkContains(trie, contactno5, "123123")); - - // Invalid number(only 9 characters long) - final ContactNumber contactno6 = new ContactNumber(0, "Invalid2", "415-123-123", "0", 1); - trie.put(contactno6); - // It should still be inserted as is - assertTrue(checkContains(trie, contactno6, "415123123")); - // But the NANP special case handling should not work - assertFalse(checkContains(trie, contactno6, "123123")); - - // Number with +1 prefix and is a NANP number - final ContactNumber contactno7 = new ContactNumber(0, "Mike", "+1-510-284-9170", "0", 1); - trie.put(contactno7); - assertTrue(checkContains(trie, contactno7, "15102849170")); - assertTrue(checkContains(trie, contactno7, "5102849170")); - assertTrue(checkContains(trie, contactno7, "2849170")); - assertFalse(checkContains(trie, contactno7, "849170")); - assertFalse(checkContains(trie, contactno7, "10849170")); - - // Number with +1 prefix but is an invalid NANP number - final ContactNumber contactno8 = new ContactNumber(0, "Invalid", "+1-510-284-917", "0", 1); - trie.put(contactno8); - assertTrue(checkContains(trie, contactno8, "1510284917")); - assertTrue(checkContains(trie, contactno8, "510284917")); - assertFalse(checkContains(trie, contactno8, "2849170")); - - // Number with invalid country code prefix - final ContactNumber contactno9 = new ContactNumber(0, "Inv", "+857-510-284-9170", "0", 1); - trie.put(contactno9); - assertTrue(checkContains(trie, contactno9, "8575102849170")); - assertFalse(checkContains(trie, contactno9, "5102849170")); - assertFalse(checkContains(trie, contactno9, "2849170")); - - // If user's region is determined to be not in North America, then the NANP number - // workarounds should not be applied - final SmartDialTrie trieNonNANP = new SmartDialTrie(); - - trieNonNANP.put(contactno3); - assertTrue(checkContains(trieNonNANP, contactno3, "4151234567")); - assertFalse(checkContains(trieNonNANP, contactno3, "1234567")); - - trieNonNANP.put(contactno4); - assertTrue(checkContains(trieNonNANP, contactno4, "15102849170")); - assertFalse(checkContains(trieNonNANP, contactno4, "5102849170")); - assertFalse(checkContains(trieNonNANP, contactno4, "2849170")); - } - - public void testNodeConstructor() { - final Node n = new Node(); - // Node member variables should not be initialized by default at construction to reduce - // unnecessary memory usage - assertEquals(-1, n.getChildrenSize()); - assertNull(n.getChild(5, false)); - assertNull(n.getChild(0, false)); - } - - public void testNodeGetChild() { - final Node n = new Node(); - // A node shouldn't contain children until getChild(index, true) is called - assertEquals(-1, n.getChildrenSize()); - final Node child = n.getChild(1, true); - // A node should always have 10 children once the child array is created - assertEquals(10, n.getChildrenSize()); - // getChild(index, true) should never return null - assertNotNull(child); - } - - public void testNodeAddContact() { - final Node n = new Node(); - assertNull(n.getContents()); - final ContactNumber contact = new ContactNumber(0, "James", "510-527-2357", "0", 1); - final ContactNumber contactNotIn = new ContactNumber(2, "Jason Smitt", "0", "2", 3); - n.add(contact); - assertTrue(n.getContents().contains(contact)); - assertFalse(n.getContents().contains(contactNotIn)); - } - - private boolean checkContains(SmartDialTrie trie, ContactNumber contact, CharSequence prefix) { - return trie.getAllWithPrefix(prefix).contains(contact); - } -} |