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
17package com.android.internal.telephony;
18
19import android.telephony.PhoneNumberFormattingTextWatcher;
20import android.test.suitebuilder.annotation.SmallTest;
21import android.text.Selection;
22import android.text.SpannableStringBuilder;
23import android.text.TextWatcher;
24
25import junit.framework.TestCase;
26
27public class PhoneNumberWatcherTest extends TestCase {
28    @SmallTest
29    public void testHyphenation() throws Exception {
30        SpannableStringBuilder number = new SpannableStringBuilder();
31        TextWatcher tw = new PhoneNumberFormattingTextWatcher();
32        number.append("555-1212");
33        // Move the cursor to the left edge
34        Selection.setSelection(number, 0);
35        tw.beforeTextChanged(number, 0, 0, 1);
36        // Insert an 8 at the beginning
37        number.insert(0, "8");
38        tw.afterTextChanged(number);
39        assertEquals("855-512-12", number.toString());
40    }
41
42    @SmallTest
43    public void testHyphenDeletion() throws Exception {
44        SpannableStringBuilder number = new SpannableStringBuilder();
45        TextWatcher tw = new PhoneNumberFormattingTextWatcher();
46        number.append("555-1212");
47        // Move the cursor to after the hyphen
48        Selection.setSelection(number, 4);
49        // Delete the hyphen
50        tw.beforeTextChanged(number, 3, 1, 0);
51        number.delete(3, 4);
52        tw.afterTextChanged(number);
53        // Make sure that it deleted the character before the hyphen
54        assertEquals("551-212", number.toString());
55
56        // Make sure it deals with left edge boundary case
57        number.insert(0, "-");
58        Selection.setSelection(number, 1);
59        tw.beforeTextChanged(number, 0, 1, 0);
60        number.delete(0, 1);
61        tw.afterTextChanged(number);
62        // Make sure that it deleted the character before the hyphen
63        assertEquals("551-212", number.toString());
64    }
65}
66