1 /* 2 * Copyright (C) 2008 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 package com.android.internal.telephony; 17 18 import android.telephony.PhoneNumberFormattingTextWatcher; 19 import android.test.AndroidTestCase; 20 import android.test.suitebuilder.annotation.SmallTest; 21 import android.text.Editable; 22 import android.text.Selection; 23 import android.text.SpannableStringBuilder; 24 import android.text.TextWatcher; 25 26 import org.junit.Test; 27 28 public class PhoneNumberWatcherTest extends AndroidTestCase { 29 @Test @SmallTest 30 public void testAppendChars() { 31 final String multiChars = "65012345"; 32 final String formatted1 = "(650) 123-45"; 33 TextWatcher textWatcher = getTextWatcher(); 34 SpannableStringBuilder number = new SpannableStringBuilder(); 35 // Append more than one chars 36 textWatcher.beforeTextChanged(number, 0, 0, multiChars.length()); 37 number.append(multiChars); 38 Selection.setSelection(number, number.length()); 39 textWatcher.onTextChanged(number, 0, 0, number.length()); 40 textWatcher.afterTextChanged(number); 41 assertEquals(formatted1, number.toString()); 42 assertEquals(formatted1.length(), Selection.getSelectionEnd(number)); 43 // Append one chars 44 final char appendChar = '6'; 45 final String formatted2 = "(650) 123-456"; 46 int len = number.length(); 47 textWatcher.beforeTextChanged(number, number.length(), 0, 1); 48 number.append(appendChar); 49 Selection.setSelection(number, number.length()); 50 textWatcher.onTextChanged(number, len, 0, 1); 51 textWatcher.afterTextChanged(number); 52 assertEquals(formatted2, number.toString()); 53 assertEquals(formatted2.length(), Selection.getSelectionEnd(number)); 54 } 55 56 @Test @SmallTest 57 public void testRemoveLastChars() { 58 final String init = "65012345678"; 59 final String result1 = "(650) 123-4567"; 60 TextWatcher textWatcher = getTextWatcher(); 61 // Remove the last char. 62 SpannableStringBuilder number = new SpannableStringBuilder(init); 63 int len = number.length(); 64 textWatcher.beforeTextChanged(number, len - 1, 1, 0); 65 number.delete(len - 1, len); 66 Selection.setSelection(number, number.length()); 67 textWatcher.onTextChanged(number, number.length() - 1, 1, 0); 68 textWatcher.afterTextChanged(number); 69 assertEquals(result1, number.toString()); 70 assertEquals(result1.length(), Selection.getSelectionEnd(number)); 71 // Remove last 5 chars 72 final String result2 = "650-123"; 73 textWatcher.beforeTextChanged(number, number.length() - 4, 4, 0); 74 number.delete(number.length() - 5, number.length()); 75 Selection.setSelection(number, number.length()); 76 textWatcher.onTextChanged(number, number.length(), 4, 0); 77 textWatcher.afterTextChanged(number); 78 assertEquals(result2, number.toString()); 79 assertEquals(result2.length(), Selection.getSelectionEnd(number)); 80 } 81 82 @Test @SmallTest 83 public void testInsertChars() { 84 final String init = "650-23"; 85 final String expected1 = "650-123"; 86 TextWatcher textWatcher = getTextWatcher(); 87 88 // Insert one char 89 SpannableStringBuilder number = new SpannableStringBuilder(init); 90 textWatcher.beforeTextChanged(number, 3, 0, 1); 91 number.insert(3, "1"); // 6501-23 92 Selection.setSelection(number, 4); // make the cursor at right of 1 93 textWatcher.onTextChanged(number, 3, 0, 1); 94 textWatcher.afterTextChanged(number); 95 assertEquals(expected1, number.toString()); 96 // the cursor should still at the right of '1' 97 assertEquals(5, Selection.getSelectionEnd(number)); 98 99 // Insert multiple chars 100 final String expected2 = "(650) 145-6723"; 101 textWatcher.beforeTextChanged(number, 5, 0, 4); 102 number.insert(5, "4567"); // change to 650-1456723 103 Selection.setSelection(number, 9); // the cursor is at the right of '7'. 104 textWatcher.onTextChanged(number, 7, 0, 4); 105 textWatcher.afterTextChanged(number); 106 assertEquals(expected2, number.toString()); 107 // the cursor should be still at the right of '7' 108 assertEquals(12, Selection.getSelectionEnd(number)); 109 } 110 111 @Test @SmallTest 112 public void testStopFormatting() { 113 final String init = "(650) 123"; 114 final String expected1 = "(650) 123 4"; 115 TextWatcher textWatcher = getTextWatcher(); 116 117 // Append space 118 SpannableStringBuilder number = new SpannableStringBuilder(init); 119 textWatcher.beforeTextChanged(number, 9, 0, 2); 120 number.insert(9, " 4"); // (6501) 23 4 121 Selection.setSelection(number, number.length()); // make the cursor at right of 4 122 textWatcher.onTextChanged(number, 9, 0, 2); 123 textWatcher.afterTextChanged(number); 124 assertEquals(expected1, number.toString()); 125 // the cursor should still at the right of '1' 126 assertEquals(expected1.length(), Selection.getSelectionEnd(number)); 127 128 // Delete a ')' 129 final String expected2 ="(650 123"; 130 textWatcher = getTextWatcher(); 131 number = new SpannableStringBuilder(init); 132 textWatcher.beforeTextChanged(number, 4, 1, 0); 133 number.delete(4, 5); // (6501 23 4 134 Selection.setSelection(number, 5); // make the cursor at right of 1 135 textWatcher.onTextChanged(number, 4, 1, 0); 136 textWatcher.afterTextChanged(number); 137 assertEquals(expected2, number.toString()); 138 // the cursor should still at the right of '1' 139 assertEquals(5, Selection.getSelectionEnd(number)); 140 141 // Insert a hyphen 142 final String expected3 ="(650) 12-3"; 143 textWatcher = getTextWatcher(); 144 number = new SpannableStringBuilder(init); 145 textWatcher.beforeTextChanged(number, 8, 0, 1); 146 number.insert(8, "-"); // (650) 12-3 147 Selection.setSelection(number, 9); // make the cursor at right of - 148 textWatcher.onTextChanged(number, 8, 0, 1); 149 textWatcher.afterTextChanged(number); 150 assertEquals(expected3, number.toString()); 151 // the cursor should still at the right of '-' 152 assertEquals(9, Selection.getSelectionEnd(number)); 153 } 154 155 @Test @SmallTest 156 public void testRestartFormatting() { 157 final String init = "(650) 123"; 158 final String expected1 = "(650) 123 4"; 159 TextWatcher textWatcher = getTextWatcher(); 160 161 // Append space 162 SpannableStringBuilder number = new SpannableStringBuilder(init); 163 textWatcher.beforeTextChanged(number, 9, 0, 2); 164 number.insert(9, " 4"); // (650) 123 4 165 Selection.setSelection(number, number.length()); // make the cursor at right of 4 166 textWatcher.onTextChanged(number, 9, 0, 2); 167 textWatcher.afterTextChanged(number); 168 assertEquals(expected1, number.toString()); 169 // the cursor should still at the right of '4' 170 assertEquals(expected1.length(), Selection.getSelectionEnd(number)); 171 172 // Clear the current string, and start formatting again. 173 int len = number.length(); 174 textWatcher.beforeTextChanged(number, 0, len, 0); 175 number.delete(0, len); 176 textWatcher.onTextChanged(number, 0, len, 0); 177 textWatcher.afterTextChanged(number); 178 179 final String expected2 = "650-1234"; 180 number = new SpannableStringBuilder(init); 181 textWatcher.beforeTextChanged(number, 9, 0, 1); 182 number.insert(9, "4"); // (650) 1234 183 Selection.setSelection(number, number.length()); // make the cursor at right of 4 184 textWatcher.onTextChanged(number, 9, 0, 1); 185 textWatcher.afterTextChanged(number); 186 assertEquals(expected2, number.toString()); 187 // the cursor should still at the right of '4' 188 assertEquals(expected2.length(), Selection.getSelectionEnd(number)); 189 } 190 191 @Test @SmallTest 192 public void testTextChangedByOtherTextWatcher() { 193 final TextWatcher cleanupTextWatcher = new TextWatcher() { 194 @Override 195 public void afterTextChanged(Editable s) { 196 s.clear(); 197 } 198 199 @Override 200 public void beforeTextChanged(CharSequence s, int start, int count, 201 int after) { 202 } 203 204 @Override 205 public void onTextChanged(CharSequence s, int start, int before, 206 int count) { 207 } 208 }; 209 final String init = "(650) 123"; 210 final String expected1 = ""; 211 TextWatcher textWatcher = getTextWatcher(); 212 213 SpannableStringBuilder number = new SpannableStringBuilder(init); 214 textWatcher.beforeTextChanged(number, 5, 0, 1); 215 number.insert(5, "4"); // (6504) 123 216 Selection.setSelection(number, 5); // make the cursor at right of 4 217 textWatcher.onTextChanged(number, 5, 0, 1); 218 number.setSpan(cleanupTextWatcher, 0, number.length(), 0); 219 textWatcher.afterTextChanged(number); 220 assertEquals(expected1, number.toString()); 221 } 222 223 /** 224 * Test the case where some other component is auto-completing what the user is typing 225 */ 226 @Test @SmallTest 227 public void testAutoCompleteWithFormattedNumber() { 228 String init = "650-1"; 229 String expected = "+1-650-123-4567"; // Different formatting than ours 230 testReplacement(init, expected, expected); 231 } 232 233 /** 234 * Test the case where some other component is auto-completing what the user is typing 235 */ 236 @Test @SmallTest 237 public void testAutoCompleteWithFormattedNameAndNumber() { 238 String init = "650-1"; 239 String expected = "Test User <650-123-4567>"; 240 testReplacement(init, expected, expected); 241 } 242 243 /** 244 * Test the case where some other component is auto-completing what the user is typing 245 */ 246 @Test @SmallTest 247 public void testAutoCompleteWithNumericNameAndNumber() { 248 String init = "650"; 249 String expected = "2nd Test User <650-123-4567>"; 250 testReplacement(init, expected, expected); 251 } 252 253 /** 254 * Test the case where some other component is auto-completing what the user is typing 255 */ 256 @Test @SmallTest 257 public void testAutoCompleteWithUnformattedNumber() { 258 String init = "650-1"; 259 String expected = "6501234567"; 260 testReplacement(init, expected, expected); 261 } 262 263 /** 264 * Test the case where some other component is auto-completing what the user is typing, where 265 * the deleted text doesn't have any formatting and neither does the replacement text: in this 266 * case the replacement text should be formatted by the PhoneNumberFormattingTextWatcher. 267 */ 268 @Test @SmallTest 269 public void testAutoCompleteUnformattedWithUnformattedNumber() { 270 String init = "650"; 271 String replacement = "6501234567"; 272 String expected = "(650) 123-4567"; 273 testReplacement(init, replacement, expected); 274 275 String init2 = "650"; 276 String replacement2 = "16501234567"; 277 String expected2 = "1 650-123-4567"; 278 testReplacement(init2, replacement2, expected2); 279 } 280 281 /** 282 * Helper method for testing replacing the entire string with another string 283 * @param init The initial string 284 * @param expected 285 */ 286 private void testReplacement(String init, String replacement, String expected) { 287 TextWatcher textWatcher = getTextWatcher(); 288 289 SpannableStringBuilder number = new SpannableStringBuilder(init); 290 291 // Replace entire text with the given values 292 textWatcher.beforeTextChanged(number, 0, init.length(), replacement.length()); 293 number.replace(0, init.length(), replacement, 0, replacement.length()); 294 Selection.setSelection(number, replacement.length()); // move the cursor to the end 295 textWatcher.onTextChanged(number, 0, init.length(), replacement.length()); 296 textWatcher.afterTextChanged(number); 297 298 assertEquals(expected, number.toString()); 299 // the cursor should be still at the end 300 assertEquals(expected.length(), Selection.getSelectionEnd(number)); 301 } 302 303 @Test @SmallTest 304 private TextWatcher getTextWatcher() { 305 return new PhoneNumberFormattingTextWatcher("US"); 306 } 307 } 308