diff options
Diffstat (limited to 'tests')
3 files changed, 264 insertions, 35 deletions
diff --git a/tests/src/com/android/dialer/dialpad/SmartDialCacheTest.java b/tests/src/com/android/dialer/dialpad/SmartDialCacheTest.java new file mode 100644 index 000000000..8d96edafc --- /dev/null +++ b/tests/src/com/android/dialer/dialpad/SmartDialCacheTest.java @@ -0,0 +1,56 @@ +/* + * 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 08939b48e..3c481d0c7 100644 --- a/tests/src/com/android/dialer/dialpad/SmartDialNameMatcherTest.java +++ b/tests/src/com/android/dialer/dialpad/SmartDialNameMatcherTest.java @@ -91,6 +91,8 @@ public class SmartDialNameMatcherTest extends TestCase { checkMatches("William John-Smith", "5764", true, 8, 9, 13, 16); // make sure multiple spaces don't mess things up checkMatches("William John---Smith", "5764", true, 15, 16, 22, 25); + + checkMatches("Berkeley Hair-Studio", "788346", true, 14, 20); } public void testMatches_repeatedSeparators() { @@ -114,6 +116,7 @@ public class SmartDialNameMatcherTest extends TestCase { public void testMatches_umlaut() { checkMatches("ÄÖÜäöü", "268268", true, 0, 6); } + // TODO: Great if it was treated as "s" or "ss. Figure out if possible without prefix trie? @Suppress public void testMatches_germanSharpS() { @@ -135,6 +138,80 @@ public class SmartDialNameMatcherTest extends TestCase { fail("Cyrillic letters aren't supported yet."); } + + public void testMatches_NumberBasic() { + // Simple basic examples that start the match from the start of the number + checkMatchesNumber("5103337596", "510", true, 0, 3); + checkMatchesNumber("5103337596", "511", false, 0, 0); + checkMatchesNumber("5103337596", "5103337596", true, 0, 10); + checkMatchesNumber("123-456-789", "123456789", true, 0, 11); + checkMatchesNumber("123-456-789", "123456788", false, 0, 0); + checkMatchesNumber("09999999999", "099", true, 0, 3); + } + + public void testMatches_NumberWithCountryCode() { + // These matches should ignore the country prefix + // USA (+1) + checkMatchesNumber("+15103337596", "5103337596", true, 2, 12); + checkMatchesNumber("+15103337596", "15103337596", true, 0, 12); + + // Singapore (+65) + checkMatchesNumber("+6591776930", "6591", true, 0, 5); + checkMatchesNumber("+6591776930", "9177", true, 3, 7); + checkMatchesNumber("+6591776930", "5917", false, 3, 7); + + // Hungary (+36) + checkMatchesNumber("+3612345678", "361234", true, 0, 7); + checkMatchesNumber("+3612345678", "1234", true, 3, 7); + + // Hongkong (+852) + checkMatchesNumber("+852 2222 2222", "85222222222", true, 0, 14); + checkMatchesNumber("+852 2222 3333", "2222", true, 5, 9); + + // Invalid (+854) + checkMatchesNumber("+854 1111 2222", "8541111", true, 0, 9); + checkMatchesNumber("+854 1111 2222", "1111", false, 0, 0); + } + + public void testMatches_NumberNANP() { + // An 11 digit number prefixed with 1 should be matched by the 10 digit number, as well as + // the 7 digit number (without area code) + checkMatchesNumber("1-510-333-7596", "5103337596", true, true, 2, 14); + checkMatchesNumber("1-510-333-7596", "3337596", true, true, 6, 14); + + // Invalid NANP numbers should not be matched + checkMatchesNumber("1-510-333-759", "510333759", false, true, 0, 0); + checkMatchesNumber("510-333-759", "333759", false, true, 0, 0); + + // match should fail if NANP flag is switched off + checkMatchesNumber("1-510-333-7596", "3337596", false, false, 0, 0); + + // A 10 digit number without a 1 prefix should be matched by the 7 digit number + checkMatchesNumber("(650) 292 2323", "2922323", true, true, 6, 14); + checkMatchesNumber("(650) 292 2323", "6502922323", true, true, 0, 14); + // match should fail if NANP flag is switched off + checkMatchesNumber("(650) 292 2323", "2922323", false, false, 0, 0); + // But this should still match (since it is the full number) + checkMatchesNumber("(650) 292 2323", "6502922323", true, false, 0, 14); + } + + + private void checkMatchesNumber(String number, String query, boolean expectedMatches, + int matchStart, int matchEnd) { + checkMatchesNumber(number, query, expectedMatches, false, matchStart, matchEnd); + } + + private void checkMatchesNumber(String number, String query, boolean expectedMatches, + boolean matchNanp, int matchStart, int matchEnd) { + final SmartDialMatchPosition pos = SmartDialNameMatcher.matchesNumber(number, query, + matchNanp); + assertEquals(expectedMatches, pos != null); + if (expectedMatches) { + assertEquals("start", matchStart, pos.start); + assertEquals("end", matchEnd, pos.end); + } + } + private void checkMatches(String displayName, String query, boolean expectedMatches, int... expectedMatchPositions) { final SmartDialNameMatcher matcher = new SmartDialNameMatcher(query); diff --git a/tests/src/com/android/dialer/dialpad/SmartDialTrieTest.java b/tests/src/com/android/dialer/dialpad/SmartDialTrieTest.java index 0378555d0..7f55263e1 100644 --- a/tests/src/com/android/dialer/dialpad/SmartDialTrieTest.java +++ b/tests/src/com/android/dialer/dialpad/SmartDialTrieTest.java @@ -48,11 +48,11 @@ public class SmartDialTrieTest extends TestCase{ final ContactNumber jasonsmitt = new ContactNumber(1, "Jason Smitt", "0", "1", 2); trie.put(jasonsmith); trie.put(jasonsmitt); - assertEquals(true, trie.getAllWithPrefix("5276676484").contains(jasonsmith)); - assertEquals(false, trie.getAllWithPrefix("5276676484").contains(jasonsmitt)); + assertTrue(trie.getAllWithPrefix("5276676484").contains(jasonsmith)); + assertFalse(trie.getAllWithPrefix("5276676484").contains(jasonsmitt)); - assertEquals(false, trie.getAllWithPrefix("5276676488").contains(jasonsmith)); - assertEquals(true, trie.getAllWithPrefix("5276676488").contains(jasonsmitt)); + assertFalse(trie.getAllWithPrefix("5276676488").contains(jasonsmith)); + assertTrue(trie.getAllWithPrefix("5276676488").contains(jasonsmitt)); } @@ -66,18 +66,18 @@ public class SmartDialTrieTest extends TestCase{ trie.put(jasonsmitt); // 6279 corresponds to mary = "Mary Jane" but not "Jason Smitt" - assertEquals(true, checkContains(trie, maryjane, "6279")); - assertEquals(false, checkContains(trie, jasonsmitt, "6279")); + assertTrue(checkContains(trie, maryjane, "6279")); + assertFalse(checkContains(trie, jasonsmitt, "6279")); // 72 corresponds to sa = "Sarah Smith" but not "Jason Smitt" or "Mary Jane" - assertEquals(false, checkContains(trie, maryjane, "72")); - assertEquals(true, checkContains(trie, sarahsmith, "72")); - assertEquals(false, checkContains(trie, jasonsmitt, "72")); + 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" - assertEquals(false, checkContains(trie, maryjane, "76")); - assertEquals(true, checkContains(trie, sarahsmith, "76")); - assertEquals(true, checkContains(trie, jasonsmitt, "76")); + assertFalse(checkContains(trie, maryjane, "76")); + assertTrue(checkContains(trie, sarahsmith, "76")); + assertTrue(checkContains(trie, jasonsmitt, "76")); } public void testPutForNameTokens() { @@ -86,11 +86,11 @@ public class SmartDialTrieTest extends TestCase{ trie.put(jasonfwilliams); // 527 corresponds to jas = "Jason" - assertEquals(true, checkContains(trie, jasonfwilliams, "527")); + assertTrue(checkContains(trie, jasonfwilliams, "527")); // 945 corresponds to wil = "Wil" - assertEquals(true, checkContains(trie, jasonfwilliams, "945")); + assertTrue(checkContains(trie, jasonfwilliams, "945")); // 66 doesn't match - assertEquals(false, checkContains(trie, jasonfwilliams, "66")); + assertFalse(checkContains(trie, jasonfwilliams, "66")); } public void testPutForInitialMatches() { @@ -99,21 +99,21 @@ public class SmartDialTrieTest extends TestCase{ new ContactNumber(0, "Martin Jr Harry", "0", "0", 1); trie.put(martinjuniorharry); // 654 corresponds to mjh = "(M)artin (J)r (H)arry" - assertEquals(true, checkContains(trie, martinjuniorharry, "654")); + assertTrue(checkContains(trie, martinjuniorharry, "654")); // The reverse (456) does not match (for now) - assertEquals(false, checkContains(trie, martinjuniorharry, "456")); + assertFalse(checkContains(trie, martinjuniorharry, "456")); // 6542 corresponds to mjha = "(M)artin (J)r (Ha)rry" - assertEquals(true, checkContains(trie, martinjuniorharry, "6542")); + assertTrue(checkContains(trie, martinjuniorharry, "6542")); // 542 corresponds to jha = "Martin (J)r (Ha)rry" - assertEquals(true, checkContains(trie, martinjuniorharry, "542")); + assertTrue(checkContains(trie, martinjuniorharry, "542")); // 547 doesn't match - assertEquals(false, checkContains(trie, martinjuniorharry, "547")); + assertFalse(checkContains(trie, martinjuniorharry, "547")); // 655 doesn't match - assertEquals(false, checkContains(trie, martinjuniorharry, "655")); + assertFalse(checkContains(trie, martinjuniorharry, "655")); // 653 doesn't match - assertEquals(false, checkContains(trie, martinjuniorharry, "653")); + assertFalse(checkContains(trie, martinjuniorharry, "653")); // 6543 doesn't match - assertEquals(false, checkContains(trie, martinjuniorharry, "6543")); + assertFalse(checkContains(trie, martinjuniorharry, "6543")); } public void testSeparators() { @@ -124,9 +124,9 @@ public class SmartDialTrieTest extends TestCase{ for (int i = 0; i < name.length(); i++) { // separators at position 8 and 12 if (i == 8 || i == 14) { - assertEquals(true, bytes[i] == -1); + assertTrue(bytes[i] == -1); } else { - assertEquals(false, bytes[i] == -1); + assertFalse(bytes[i] == -1); } } } @@ -137,8 +137,8 @@ public class SmartDialTrieTest extends TestCase{ final ContactNumber bronte = new ContactNumber(2, "Brontë", "0", "1", 2); trie.put(reenee); trie.put(bronte); - assertEquals(true, checkContains(trie, reenee, "733633")); - assertEquals(true, checkContains(trie, bronte, "276683")); + assertTrue(checkContains(trie, reenee, "733633")); + assertTrue(checkContains(trie, bronte, "276683")); } public void testPutForNumbers() { @@ -150,15 +150,111 @@ public class SmartDialTrieTest extends TestCase{ final ContactNumber contactno3 = new ContactNumber(0, "James", "+13684976334", "0", 1); trie.put(contactno3); // all phone numbers belonging to the contact should correspond to it - assertEquals(true, checkContains(trie, contactno1, "510")); - assertEquals(false, checkContains(trie, contactno1, "511")); - assertEquals(true, checkContains(trie, contactno2, "77212862357")); - assertEquals(false, checkContains(trie, contactno2, "77212862356")); - assertEquals(true, checkContains(trie, contactno3, "1368")); - assertEquals(false, checkContains(trie, contactno3, "1367")); + 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")); + + // 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 @@ -185,8 +281,8 @@ public class SmartDialTrieTest extends TestCase{ 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); - assertEquals(true, n.getContents().contains(contact)); - assertEquals(false, n.getContents().contains(contactNotIn)); + assertTrue(n.getContents().contains(contact)); + assertFalse(n.getContents().contains(contactNotIn)); } private boolean checkContains(SmartDialTrie trie, ContactNumber contact, CharSequence prefix) { |