diff options
| author | Bai Tao <michaelbai@google.com> | 2010-06-07 10:25:53 +0800 |
|---|---|---|
| committer | Bai Tao <michaelbai@google.com> | 2010-06-10 11:27:54 +0800 |
| commit | ef4fd8e12e1928b1e9cdc03378bb1185d9e05cbe (patch) | |
| tree | 348f33d3427b6754c66cb9581b41045fbf471ee2 /telephony/tests | |
| parent | 81d5dad156ce89abd3037bb27582e4da283e2a71 (diff) | |
| download | frameworks_base-ef4fd8e12e1928b1e9cdc03378bb1185d9e05cbe.zip frameworks_base-ef4fd8e12e1928b1e9cdc03378bb1185d9e05cbe.tar.gz frameworks_base-ef4fd8e12e1928b1e9cdc03378bb1185d9e05cbe.tar.bz2 | |
Reimplement the PhoneNumberFormattingTextWatcher
a. Built the external/libphonenumberutil into the ext.jar. The file size increased 50K, the phone number meta file is 90K before the compression.
b. Used the external/libphonenumberutil to format the phone number for about 200 countries.
c. Beside the phone number formatting, the external/libphonenumberutil will also be used for phonenumber match and international dialing.
Change-Id: Ie5165dc60d66e1eddab7134725a8d1d1c826434a
Diffstat (limited to 'telephony/tests')
| -rw-r--r-- | telephony/tests/telephonytests/src/com/android/internal/telephony/PhoneNumberWatcherTest.java | 227 |
1 files changed, 188 insertions, 39 deletions
diff --git a/telephony/tests/telephonytests/src/com/android/internal/telephony/PhoneNumberWatcherTest.java b/telephony/tests/telephonytests/src/com/android/internal/telephony/PhoneNumberWatcherTest.java index 88eaecd..d2e573c 100644 --- a/telephony/tests/telephonytests/src/com/android/internal/telephony/PhoneNumberWatcherTest.java +++ b/telephony/tests/telephonytests/src/com/android/internal/telephony/PhoneNumberWatcherTest.java @@ -13,53 +13,202 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.android.internal.telephony; import android.telephony.PhoneNumberFormattingTextWatcher; -import android.test.suitebuilder.annotation.SmallTest; +import android.test.AndroidTestCase; +import android.text.Editable; import android.text.Selection; import android.text.SpannableStringBuilder; import android.text.TextWatcher; -import junit.framework.TestCase; - -public class PhoneNumberWatcherTest extends TestCase { - @SmallTest - public void testHyphenation() throws Exception { +public class PhoneNumberWatcherTest extends AndroidTestCase { + public void testAppendChars() { + final String multiChars = "65012345"; + final String formatted1 = "(650) 123-45"; + TextWatcher textWatcher = getTextWatcher(); SpannableStringBuilder number = new SpannableStringBuilder(); - TextWatcher tw = new PhoneNumberFormattingTextWatcher(); - number.append("555-1212"); - // Move the cursor to the left edge - Selection.setSelection(number, 0); - tw.beforeTextChanged(number, 0, 0, 1); - // Insert an 8 at the beginning - number.insert(0, "8"); - tw.afterTextChanged(number); - assertEquals("855-512-12", number.toString()); + // Append more than one chars + textWatcher.beforeTextChanged(number, 0, 0, multiChars.length()); + number.append(multiChars); + Selection.setSelection(number, number.length()); + textWatcher.onTextChanged(number, 0, 0, number.length()); + textWatcher.afterTextChanged(number); + assertEquals(formatted1, number.toString()); + assertEquals(formatted1.length(), Selection.getSelectionEnd(number)); + // Append one chars + final char appendChar = '6'; + final String formatted2 = "(650) 123-456"; + int len = number.length(); + textWatcher.beforeTextChanged(number, number.length(), 0, 1); + number.append(appendChar); + Selection.setSelection(number, number.length()); + textWatcher.onTextChanged(number, len, 0, 1); + textWatcher.afterTextChanged(number); + assertEquals(formatted2, number.toString()); + assertEquals(formatted2.length(), Selection.getSelectionEnd(number)); } - - @SmallTest - public void testHyphenDeletion() throws Exception { - SpannableStringBuilder number = new SpannableStringBuilder(); - TextWatcher tw = new PhoneNumberFormattingTextWatcher(); - number.append("555-1212"); - // Move the cursor to after the hyphen - Selection.setSelection(number, 4); - // Delete the hyphen - tw.beforeTextChanged(number, 3, 1, 0); - number.delete(3, 4); - tw.afterTextChanged(number); - // Make sure that it deleted the character before the hyphen - assertEquals("551-212", number.toString()); - - // Make sure it deals with left edge boundary case - number.insert(0, "-"); - Selection.setSelection(number, 1); - tw.beforeTextChanged(number, 0, 1, 0); - number.delete(0, 1); - tw.afterTextChanged(number); - // Make sure that it deleted the character before the hyphen - assertEquals("551-212", number.toString()); + + public void testRemoveLastChars() { + final String init = "65012345678"; + final String result1 = "(650) 123-4567"; + TextWatcher textWatcher = getTextWatcher(); + // Remove the last char. + SpannableStringBuilder number = new SpannableStringBuilder(init); + int len = number.length(); + textWatcher.beforeTextChanged(number, len - 1, 1, 0); + number.delete(len - 1, len); + Selection.setSelection(number, number.length()); + textWatcher.onTextChanged(number, number.length() - 1, 1, 0); + textWatcher.afterTextChanged(number); + assertEquals(result1, number.toString()); + assertEquals(result1.length(), Selection.getSelectionEnd(number)); + // Remove last 5 chars + final String result2 = "(650) 123"; + textWatcher.beforeTextChanged(number, number.length() - 4, 4, 0); + number.delete(number.length() - 5, number.length()); + Selection.setSelection(number, number.length()); + textWatcher.onTextChanged(number, number.length(), 4, 0); + textWatcher.afterTextChanged(number); + assertEquals(result2, number.toString()); + assertEquals(result2.length(), Selection.getSelectionEnd(number)); + } + + public void testInsertChars() { + final String init = "(650) 23"; + final String expected1 = "(650) 123"; + TextWatcher textWatcher = getTextWatcher(); + + // Insert one char + SpannableStringBuilder number = new SpannableStringBuilder(init); + textWatcher.beforeTextChanged(number, 4, 0, 1); + number.insert(4, "1"); // (6501) 23 + Selection.setSelection(number, 5); // make the cursor at right of 1 + textWatcher.onTextChanged(number, 4, 0, 1); + textWatcher.afterTextChanged(number); + assertEquals(expected1, number.toString()); + // the cursor should still at the right of '1' + assertEquals(7, Selection.getSelectionEnd(number)); + + // Insert multiple chars + final String expected2 = "(650) 145-6723"; + textWatcher.beforeTextChanged(number, 7, 0, 4); + number.insert(7, "4567"); // change to (650) 1456723 + Selection.setSelection(number, 11); // the cursor is at the right of '7'. + textWatcher.onTextChanged(number, 7, 0, 4); + textWatcher.afterTextChanged(number); + assertEquals(expected2, number.toString()); + // the cursor should be still at the right of '7' + assertEquals(12, Selection.getSelectionEnd(number)); + } + + public void testStopFormatting() { + final String init = "(650) 123"; + final String expected1 = "(650) 123 4"; + TextWatcher textWatcher = getTextWatcher(); + + // Append space + SpannableStringBuilder number = new SpannableStringBuilder(init); + textWatcher.beforeTextChanged(number, 9, 0, 2); + number.insert(9, " 4"); // (6501) 23 4 + Selection.setSelection(number, number.length()); // make the cursor at right of 4 + textWatcher.onTextChanged(number, 9, 0, 2); + textWatcher.afterTextChanged(number); + assertEquals(expected1, number.toString()); + // the cursor should still at the right of '1' + assertEquals(expected1.length(), Selection.getSelectionEnd(number)); + + // Delete a ')' + final String expected2 ="(650 123"; + textWatcher = getTextWatcher(); + number = new SpannableStringBuilder(init); + textWatcher.beforeTextChanged(number, 4, 1, 0); + number.delete(4, 5); // (6501 23 4 + Selection.setSelection(number, 5); // make the cursor at right of 1 + textWatcher.onTextChanged(number, 4, 1, 0); + textWatcher.afterTextChanged(number); + assertEquals(expected2, number.toString()); + // the cursor should still at the right of '1' + assertEquals(5, Selection.getSelectionEnd(number)); + + // Insert a hyphen + final String expected3 ="(650) 12-3"; + textWatcher = getTextWatcher(); + number = new SpannableStringBuilder(init); + textWatcher.beforeTextChanged(number, 8, 0, 1); + number.insert(8, "-"); // (650) 12-3 + Selection.setSelection(number, 9); // make the cursor at right of - + textWatcher.onTextChanged(number, 8, 0, 1); + textWatcher.afterTextChanged(number); + assertEquals(expected3, number.toString()); + // the cursor should still at the right of '-' + assertEquals(9, Selection.getSelectionEnd(number)); + } + + public void testRestartFormatting() { + final String init = "(650) 123"; + final String expected1 = "(650) 123 4"; + TextWatcher textWatcher = getTextWatcher(); + + // Append space + SpannableStringBuilder number = new SpannableStringBuilder(init); + textWatcher.beforeTextChanged(number, 9, 0, 2); + number.insert(9, " 4"); // (650) 123 4 + Selection.setSelection(number, number.length()); // make the cursor at right of 4 + textWatcher.onTextChanged(number, 9, 0, 2); + textWatcher.afterTextChanged(number); + assertEquals(expected1, number.toString()); + // the cursor should still at the right of '4' + assertEquals(expected1.length(), Selection.getSelectionEnd(number)); + + // Clear the current string, and start formatting again. + int len = number.length(); + textWatcher.beforeTextChanged(number, 0, len, 0); + number.delete(0, len); + textWatcher.onTextChanged(number, 0, len, 0); + textWatcher.afterTextChanged(number); + + final String expected2 = "(650) 123-4"; + number = new SpannableStringBuilder(init); + textWatcher.beforeTextChanged(number, 9, 0, 1); + number.insert(9, "4"); // (650) 1234 + Selection.setSelection(number, number.length()); // make the cursor at right of 4 + textWatcher.onTextChanged(number, 9, 0, 1); + textWatcher.afterTextChanged(number); + assertEquals(expected2, number.toString()); + // the cursor should still at the right of '4' + assertEquals(expected2.length(), Selection.getSelectionEnd(number)); + } + + public void testTextChangedByOtherTextWatcher() { + final TextWatcher cleanupTextWatcher = new TextWatcher() { + public void afterTextChanged(Editable s) { + s.clear(); + } + + public void beforeTextChanged(CharSequence s, int start, int count, + int after) { + } + + public void onTextChanged(CharSequence s, int start, int before, + int count) { + } + }; + final String init = "(650) 123"; + final String expected1 = ""; + TextWatcher textWatcher = getTextWatcher(); + + SpannableStringBuilder number = new SpannableStringBuilder(init); + textWatcher.beforeTextChanged(number, 5, 0, 1); + number.insert(5, "4"); // (6504) 123 + Selection.setSelection(number, 5); // make the cursor at right of 4 + textWatcher.onTextChanged(number, 5, 0, 1); + number.setSpan(cleanupTextWatcher, 0, number.length(), 0); + textWatcher.afterTextChanged(number); + assertEquals(expected1, number.toString()); + } + + private TextWatcher getTextWatcher() { + return new PhoneNumberFormattingTextWatcher("US"); } } |
