@@ -182,14 +182,17 @@ public void testRestartFormatting() {
182182
183183 public void testTextChangedByOtherTextWatcher () {
184184 final TextWatcher cleanupTextWatcher = new TextWatcher () {
185+ @ Override
185186 public void afterTextChanged (Editable s ) {
186187 s .clear ();
187188 }
188189
190+ @ Override
189191 public void beforeTextChanged (CharSequence s , int start , int count ,
190192 int after ) {
191193 }
192194
195+ @ Override
193196 public void onTextChanged (CharSequence s , int start , int before ,
194197 int count ) {
195198 }
@@ -208,6 +211,81 @@ public void onTextChanged(CharSequence s, int start, int before,
208211 assertEquals (expected1 , number .toString ());
209212 }
210213
214+ /**
215+ * Test the case where some other component is auto-completing what the user is typing
216+ */
217+ public void testAutoCompleteWithFormattedNumber () {
218+ String init = "650-1" ;
219+ String expected = "+1-650-123-4567" ; // Different formatting than ours
220+ testReplacement (init , expected , expected );
221+ }
222+
223+ /**
224+ * Test the case where some other component is auto-completing what the user is typing
225+ */
226+ public void testAutoCompleteWithFormattedNameAndNumber () {
227+ String init = "650-1" ;
228+ String expected = "Test User <650-123-4567>" ;
229+ testReplacement (init , expected , expected );
230+ }
231+
232+ /**
233+ * Test the case where some other component is auto-completing what the user is typing
234+ */
235+ public void testAutoCompleteWithNumericNameAndNumber () {
236+ String init = "650" ;
237+ String expected = "2nd Test User <650-123-4567>" ;
238+ testReplacement (init , expected , expected );
239+ }
240+
241+ /**
242+ * Test the case where some other component is auto-completing what the user is typing
243+ */
244+ public void testAutoCompleteWithUnformattedNumber () {
245+ String init = "650-1" ;
246+ String expected = "6501234567" ;
247+ testReplacement (init , expected , expected );
248+ }
249+
250+ /**
251+ * Test the case where some other component is auto-completing what the user is typing, where
252+ * the deleted text doesn't have any formatting and neither does the replacement text: in this
253+ * case the replacement text should be formatted by the PhoneNumberFormattingTextWatcher.
254+ */
255+ public void testAutoCompleteUnformattedWithUnformattedNumber () {
256+ String init = "650" ;
257+ String replacement = "6501234567" ;
258+ String expected = "(650) 123-4567" ;
259+ testReplacement (init , replacement , expected );
260+
261+ String init2 = "650" ;
262+ String replacement2 = "16501234567" ;
263+ String expected2 = "1 650-123-4567" ;
264+ testReplacement (init2 , replacement2 , expected2 );
265+ }
266+
267+ /**
268+ * Helper method for testing replacing the entire string with another string
269+ * @param init The initial string
270+ * @param expected
271+ */
272+ private void testReplacement (String init , String replacement , String expected ) {
273+ TextWatcher textWatcher = getTextWatcher ();
274+
275+ SpannableStringBuilder number = new SpannableStringBuilder (init );
276+
277+ // Replace entire text with the given values
278+ textWatcher .beforeTextChanged (number , 0 , init .length (), replacement .length ());
279+ number .replace (0 , init .length (), replacement , 0 , replacement .length ());
280+ Selection .setSelection (number , replacement .length ()); // move the cursor to the end
281+ textWatcher .onTextChanged (number , 0 , init .length (), replacement .length ());
282+ textWatcher .afterTextChanged (number );
283+
284+ assertEquals (expected , number .toString ());
285+ // the cursor should be still at the end
286+ assertEquals (expected .length (), Selection .getSelectionEnd (number ));
287+ }
288+
211289 private TextWatcher getTextWatcher () {
212290 return new PhoneNumberFormattingTextWatcher ("US" );
213291 }
0 commit comments