• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 android.telephony.cts;
17 
18 import static androidx.test.InstrumentationRegistry.getContext;
19 
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertTrue;
24 
25 import android.content.ContentResolver;
26 import android.content.ContentValues;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.net.Uri;
30 import android.os.RemoteException;
31 import android.provider.Contacts;
32 import android.provider.Contacts.People;
33 import android.telephony.PhoneNumberUtils;
34 import android.telephony.TelephonyManager;
35 import android.text.Editable;
36 import android.text.Spannable;
37 import android.text.SpannableString;
38 import android.text.SpannableStringBuilder;
39 import android.text.style.TtsSpan;
40 
41 import java.util.Locale;
42 
43 import org.junit.Test;
44 
45 public class PhoneNumberUtilsTest {
46     // mPhoneNumber ~ "+17005550020", length == 7.
47     private byte[] mPhoneNumber = { (byte) 0x91, (byte) 0x71, (byte) 0x00, (byte) 0x55,
48             (byte) 0x05, (byte) 0x20, (byte) 0xF0 };
49 
50     @Test
testExtractMethods()51     public void testExtractMethods() {
52 
53         // Test extractNetworkPortion
54         assertNull(PhoneNumberUtils.extractNetworkPortion(null));
55         assertEquals("+17005554141", PhoneNumberUtils.extractNetworkPortion("+17005554141"));
56         assertEquals("+17005554141*#N",
57                 PhoneNumberUtils.extractNetworkPortion("+1 (700).555-4141*#N"));
58         assertEquals("170055541", PhoneNumberUtils.extractNetworkPortion(
59                 String.format("1 (700).555-41%c1234", PhoneNumberUtils.PAUSE)));
60         assertEquals("**21**17005554141#",
61                 PhoneNumberUtils.extractNetworkPortion("**21**+17005554141#"));
62 
63         // Test extractPostDialPortion
64         assertNull(PhoneNumberUtils.extractPostDialPortion(null));
65         assertEquals("", PhoneNumberUtils.extractPostDialPortion("+17005554141"));
66         assertEquals(String.format("%c1234", PhoneNumberUtils.PAUSE),
67                 PhoneNumberUtils.extractPostDialPortion(
68                 String.format("+1 (700).555-41NN%c1234", PhoneNumberUtils.PAUSE)));
69         assertEquals(String.format("%c1234", PhoneNumberUtils.WAIT),
70                 PhoneNumberUtils.extractPostDialPortion(
71                 String.format("+1 (700).555-41NN%c1234", PhoneNumberUtils.WAIT)));
72         assertEquals(String.format("%c1234%c%cN", PhoneNumberUtils.WAIT, PhoneNumberUtils.PAUSE,
73                 PhoneNumberUtils.WAIT), PhoneNumberUtils
74                 .extractPostDialPortion(
75                         String.format("+1 (700).555-41NN%c1-2.34 %c%cN", PhoneNumberUtils.WAIT,
76                                 PhoneNumberUtils.PAUSE,
77                                 PhoneNumberUtils.WAIT)));
78     }
79 
80     @Test
testCallMethods()81     public void testCallMethods() {
82         // Test calledPartyBCDToString
83         assertEquals("+17005550020", PhoneNumberUtils.calledPartyBCDToString(mPhoneNumber, 0, 7));
84 
85         // Test toCallerIDMinMatch
86         assertNull(PhoneNumberUtils.toCallerIDMinMatch(null));
87 //        assertEquals("1414555", PhoneNumberUtils.toCallerIDMinMatch("17005554141"));
88 //        assertEquals("1414555", PhoneNumberUtils.toCallerIDMinMatch("1-700-555-4141"));
89 //        assertEquals("1414555", PhoneNumberUtils.toCallerIDMinMatch("1-700-555-4141,1234"));
90 //        assertEquals("1414555", PhoneNumberUtils.toCallerIDMinMatch("1-700-555-4141;1234"));
91 //        assertEquals("NN14555", PhoneNumberUtils.toCallerIDMinMatch("1-700-555-41NN"));
92         assertEquals("", PhoneNumberUtils.toCallerIDMinMatch(""));
93         assertEquals("0032", PhoneNumberUtils.toCallerIDMinMatch("2300"));
94         assertEquals("0032+", PhoneNumberUtils.toCallerIDMinMatch("+2300"));
95         assertEquals("#130#*", PhoneNumberUtils.toCallerIDMinMatch("*#031#"));
96 
97         // Test networkPortionToCalledPartyBCD, calledPartyBCDToString
98         byte[] bRet = PhoneNumberUtils.networkPortionToCalledPartyBCD("+17005550020");
99         assertEquals(mPhoneNumber.length, bRet.length);
100         for (int i = 0; i < mPhoneNumber.length; i++) {
101             assertEquals(mPhoneNumber[i], bRet[i]);
102         }
103         bRet = PhoneNumberUtils.networkPortionToCalledPartyBCD("7005550020");
104         assertEquals("7005550020", PhoneNumberUtils.calledPartyBCDToString(bRet, 0, bRet.length));
105 
106         // Test calledPartyBCDFragmentToString
107         assertEquals("1917005550020", PhoneNumberUtils.calledPartyBCDFragmentToString(mPhoneNumber,
108                 0, 7));
109 
110         // Test networkPortionToCalledPartyBCDWithLength
111         bRet = PhoneNumberUtils.networkPortionToCalledPartyBCDWithLength("+17005550020");
112         assertEquals(mPhoneNumber.length + 1, bRet.length);
113         for (int i = 0; i < mPhoneNumber.length; i++) {
114             assertEquals(mPhoneNumber[i], bRet[i + 1]);
115         }
116 
117         // Test numberToCalledPartyBCD
118         bRet = PhoneNumberUtils.numberToCalledPartyBCD("+17005550020");
119         assertEquals(mPhoneNumber.length, bRet.length);
120         for (int i = 0; i < mPhoneNumber.length; i++) {
121             assertEquals(mPhoneNumber[i], bRet[i]);
122         }
123     }
124 
125     @Test
testGetMethods()126     public void testGetMethods() throws RemoteException {
127         // Test getStrippedReversed
128         assertNull(PhoneNumberUtils.getStrippedReversed(null));
129         assertEquals("14145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-4141"));
130         assertEquals("14145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-4141,1234"));
131         assertEquals("14145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-4141;1234"));
132         assertEquals("NN145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-41NN"));
133         assertEquals("", PhoneNumberUtils.getStrippedReversed(""));
134         assertEquals("#130#*+", PhoneNumberUtils.getStrippedReversed("+*#031#"));
135 
136         // Test getFormatTypeForLocale
137         int formatType = PhoneNumberUtils.getFormatTypeForLocale(Locale.CHINA);
138         assertEquals(PhoneNumberUtils.FORMAT_UNKNOWN, formatType);
139         formatType = PhoneNumberUtils.getFormatTypeForLocale(Locale.US);
140         assertEquals(PhoneNumberUtils.FORMAT_NANP, formatType);
141         formatType = PhoneNumberUtils.getFormatTypeForLocale(Locale.JAPAN);
142         assertEquals(PhoneNumberUtils.FORMAT_JAPAN, formatType);
143 
144         // Test getNumberFromIntent, query nothing, return null.
145         Intent intent = new Intent();
146         intent.setData(Contacts.People.CONTENT_URI);
147         Context context = getContext();
148         assertNull(PhoneNumberUtils.getNumberFromIntent(intent, context));
149 
150         intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:+18005555555"));
151         assertEquals("+18005555555", PhoneNumberUtils.getNumberFromIntent(intent, getContext()));
152 
153         ContentResolver cr = getContext().getContentResolver();
154         Uri personRecord = null;
155         Uri phoneRecord = null;
156         try {
157             // insert a contact with phone number
158             ContentValues values = new ContentValues();
159             values.put(People.NAME, "CTS test contact");
160             personRecord = cr.insert(People.CONTENT_URI, values);
161             Uri phoneUri = Uri.withAppendedPath(personRecord, People.Phones.CONTENT_DIRECTORY);
162             values.clear();
163             values.put(People.Phones.TYPE, People.Phones.TYPE_HOME);
164             values.put(People.Phones.NUMBER, "+18005552871");
165             phoneRecord = cr.insert(phoneUri, values);
166 
167             intent = new Intent(Intent.ACTION_DIAL, phoneRecord);
168             assertEquals("+18005552871",
169                     PhoneNumberUtils.getNumberFromIntent(intent, getContext()));
170         } finally {
171             if (personRecord != null) {
172                 cr.delete(personRecord, null, null);
173             }
174             if (phoneRecord != null) {
175                 cr.delete(phoneRecord, null, null);
176             }
177         }
178     }
179 
180     @Test
testFormatMethods()181     public void testFormatMethods() {
182         // Test formatNanpNumber
183         SpannableStringBuilder builderNumber = new SpannableStringBuilder();
184         builderNumber.append("8005551212");
185         PhoneNumberUtils.formatNanpNumber(builderNumber);
186         assertEquals("800-555-1212", builderNumber.toString());
187         builderNumber.clear();
188         builderNumber.append("800555121");
189         PhoneNumberUtils.formatNanpNumber(builderNumber);
190         assertEquals("800-555-121", builderNumber.toString());
191         builderNumber.clear();
192         builderNumber.append("555-1212");
193         PhoneNumberUtils.formatNanpNumber(builderNumber);
194         assertEquals("555-1212", builderNumber.toString());
195         builderNumber.clear();
196         builderNumber.append("180055512");
197         PhoneNumberUtils.formatNanpNumber(builderNumber);
198         assertEquals("1-800-555-12", builderNumber.toString());
199         builderNumber.clear();
200         builderNumber.append("+180055512");
201         PhoneNumberUtils.formatNanpNumber(builderNumber);
202         assertEquals("+1-800-555-12", builderNumber.toString());
203 
204         // Test convertKeypadLettersToDigits
205         assertEquals("1-800-4664-411", PhoneNumberUtils
206                 .convertKeypadLettersToDigits("1-800-GOOG-411"));
207         assertEquals("1-800-466-4411", PhoneNumberUtils
208                 .convertKeypadLettersToDigits("1-800-466-4411"));
209         assertEquals("222-333-444-555-666-7777-888-9999", PhoneNumberUtils
210                 .convertKeypadLettersToDigits("ABC-DEF-GHI-JKL-MNO-PQRS-TUV-WXYZ"));
211         assertEquals("222-333-444-555-666-7777-888-9999", PhoneNumberUtils
212                 .convertKeypadLettersToDigits("abc-def-ghi-jkl-mno-pqrs-tuv-wxyz"));
213         assertEquals("(800) 222-3334", PhoneNumberUtils
214                 .convertKeypadLettersToDigits("(800) ABC-DEFG"));
215 
216         // Test stringFromStringAndTOA
217         assertNull(PhoneNumberUtils.stringFromStringAndTOA(null, 1));
218         assertEquals("+888888888", PhoneNumberUtils.stringFromStringAndTOA("888888888",
219                 PhoneNumberUtils.TOA_International));
220 
221         // Test formatJapaneseNumber
222         Editable jpEditNumber = Editable.Factory.getInstance().newEditable("0377777777");
223         PhoneNumberUtils.formatJapaneseNumber(jpEditNumber);
224         assertEquals("03-7777-7777", jpEditNumber.toString());
225         jpEditNumber = Editable.Factory.getInstance().newEditable("09077777777");
226         PhoneNumberUtils.formatJapaneseNumber(jpEditNumber);
227         assertEquals("090-7777-7777", jpEditNumber.toString());
228         jpEditNumber = Editable.Factory.getInstance().newEditable("0120777777");
229         PhoneNumberUtils.formatJapaneseNumber(jpEditNumber);
230         assertEquals("0120-777-777", jpEditNumber.toString());
231         jpEditNumber = Editable.Factory.getInstance().newEditable("+81377777777");
232         PhoneNumberUtils.formatJapaneseNumber(jpEditNumber);
233         assertEquals("+81-3-7777-7777", jpEditNumber.toString());
234         jpEditNumber = Editable.Factory.getInstance().newEditable("+819077777777");
235         PhoneNumberUtils.formatJapaneseNumber(jpEditNumber);
236         assertEquals("+81-90-7777-7777", jpEditNumber.toString());
237 
238         // Test formatNumber(String). Only numbers begin with +1 or +81 can be formatted.
239         assertEquals("+1-888-888-888", PhoneNumberUtils.formatNumber("+1888888888"));
240         // Test formatNumber(Editable, int)
241         Editable editNumber = Editable.Factory.getInstance().newEditable("0377777777");
242         PhoneNumberUtils.formatNumber(editNumber, PhoneNumberUtils.FORMAT_UNKNOWN);
243         assertEquals("0377777777", editNumber.toString());
244         editNumber = Editable.Factory.getInstance().newEditable("+177777777");
245         PhoneNumberUtils.formatNumber(editNumber, PhoneNumberUtils.FORMAT_UNKNOWN);
246         assertEquals("+1-777-777-77", editNumber.toString());
247         editNumber = Editable.Factory.getInstance().newEditable("+8177777777");
248         PhoneNumberUtils.formatNumber(editNumber, PhoneNumberUtils.FORMAT_UNKNOWN);
249         assertEquals("+81-77-777-777", editNumber.toString());
250 
251         // Test stripSeparators
252         assertEquals("+188888888", PhoneNumberUtils.stripSeparators("+188-888-888"));
253 
254         // Test toaFromString
255         assertEquals(PhoneNumberUtils.TOA_International, PhoneNumberUtils
256                 .toaFromString("+88888888"));
257         assertEquals(PhoneNumberUtils.TOA_Unknown, PhoneNumberUtils.toaFromString("88888888"));
258     }
259 
260     @Test
testJudgeMethods()261     public void testJudgeMethods() {
262         // Test is12Key, isDialable, isISODigit, isReallyDialable, isStartsPostDial
263         for (char c = '0'; c <= '9'; c++) {
264             assertTrue(PhoneNumberUtils.is12Key(c));
265             assertTrue(PhoneNumberUtils.isDialable(c));
266             assertTrue(PhoneNumberUtils.isISODigit(c));
267             assertTrue(PhoneNumberUtils.isNonSeparator(c));
268             assertTrue(PhoneNumberUtils.isReallyDialable(c));
269         }
270         char c = '*';
271         assertTrue(PhoneNumberUtils.is12Key(c));
272         assertTrue(PhoneNumberUtils.isDialable(c));
273         assertTrue(PhoneNumberUtils.isNonSeparator(c));
274         assertTrue(PhoneNumberUtils.isReallyDialable(c));
275         c = '#';
276         assertTrue(PhoneNumberUtils.is12Key(c));
277         assertTrue(PhoneNumberUtils.isDialable(c));
278         assertTrue(PhoneNumberUtils.isNonSeparator(c));
279         assertTrue(PhoneNumberUtils.isReallyDialable(c));
280         c = '$';
281         assertFalse(PhoneNumberUtils.is12Key(c));
282         assertFalse(PhoneNumberUtils.isDialable(c));
283         assertFalse(PhoneNumberUtils.isDialable(c));
284         c = '+';
285         assertTrue(PhoneNumberUtils.isDialable(c));
286         assertFalse(PhoneNumberUtils.isISODigit(c));
287         assertTrue(PhoneNumberUtils.isNonSeparator(c));
288         assertTrue(PhoneNumberUtils.isReallyDialable(c));
289         c = PhoneNumberUtils.WILD;
290         assertTrue(PhoneNumberUtils.isDialable(c));
291         assertTrue(PhoneNumberUtils.isNonSeparator(c));
292         assertFalse(PhoneNumberUtils.isReallyDialable(c));
293         c = PhoneNumberUtils.WAIT;
294         assertTrue(PhoneNumberUtils.isNonSeparator(c));
295         assertTrue(PhoneNumberUtils.isStartsPostDial(c));
296         c = PhoneNumberUtils.PAUSE;
297         assertTrue(PhoneNumberUtils.isNonSeparator(c));
298         assertTrue(PhoneNumberUtils.isStartsPostDial(c));
299         c = '8';
300         assertFalse(PhoneNumberUtils.isStartsPostDial(c));
301 
302         // Test isEmergencyNumber, now only know US emergency number
303         TelephonyManager tm = (TelephonyManager)getContext().getSystemService(
304                  Context.TELEPHONY_SERVICE);
305         // Test isEmergencyNumber, now only know US emergency number
306         if ("US".equals(tm.getSimCountryIso())) {
307             assertTrue(PhoneNumberUtils.isEmergencyNumber("911"));
308             assertFalse(PhoneNumberUtils.isEmergencyNumber("119"));
309         }
310 
311         // Test isGlobalPhoneNumber
312         assertTrue(PhoneNumberUtils.isGlobalPhoneNumber("+17005554141"));
313         assertFalse(PhoneNumberUtils.isGlobalPhoneNumber("android"));
314 
315         // Test isWellFormedSmsAddress
316         assertTrue(PhoneNumberUtils.isWellFormedSmsAddress("+17005554141"));
317         assertFalse(PhoneNumberUtils.isWellFormedSmsAddress("android"));
318     }
319 
320     @Test
testGetPhoneTtsSpan()321     public void testGetPhoneTtsSpan() {
322         // Setup: phone number without a country code. Lets keep coverage minimal to avoid
323         // exercising the underlying PhoneNumberUtil or constraining localization changes.
324         String phoneNumber = "6512223333";
325         // Execute
326         TtsSpan ttsSpan = PhoneNumberUtils.createTtsSpan(phoneNumber);
327         // Verify: the created TtsSpan contains the phone number.
328         assertEquals("6512223333", ttsSpan.getArgs().get(TtsSpan.ARG_NUMBER_PARTS));
329     }
330 
331     @Test
testAddPhoneTtsSpan()332     public void testAddPhoneTtsSpan() {
333         // Setup: phone number without a country code. Lets keep coverage minimal to avoid
334         // exercising the underlying PhoneNumberUtil or constraining localization changes.
335         Spannable spannable = new SpannableString("Hello 6502223333");
336         // Execute
337         PhoneNumberUtils.addTtsSpan(spannable, 5, spannable.length());
338         // Verify: the Spannable is annotated with a TtsSpan in the correct location.
339         TtsSpan[] ttsSpans = spannable.getSpans(5, spannable.length() - 1, TtsSpan.class);
340         assertEquals(1, ttsSpans.length);
341         assertEquals("6502223333", ttsSpans[0].getArgs().get(TtsSpan.ARG_NUMBER_PARTS));
342     }
343 
344     @Test
testGetPhoneTtsSpannable()345     public void testGetPhoneTtsSpannable() {
346         // Setup: phone number without a country code. Lets keep coverage minimal to avoid
347         // exercising the underlying PhoneNumberUtil or constraining localization changes.
348         CharSequence phoneNumber = "6512223333";
349         // Execute
350         Spannable spannable = (Spannable) PhoneNumberUtils.createTtsSpannable(phoneNumber);
351         // Verify: returned char sequence contains a TtsSpan with the phone number in it
352         TtsSpan[] ttsSpans = spannable.getSpans(0, spannable.length() - 1, TtsSpan.class);
353         assertEquals(1, ttsSpans.length);
354         assertEquals("6512223333", ttsSpans[0].getArgs().get(TtsSpan.ARG_NUMBER_PARTS));
355     }
356 
357     @Test
testFormatNumberToE164()358     public void testFormatNumberToE164() {
359         assertNull(PhoneNumberUtils.formatNumber("invalid#", "US"));
360         assertNull(PhoneNumberUtils.formatNumberToE164("1234567", "US"));
361 
362         assertEquals("+18004664114", PhoneNumberUtils.formatNumberToE164("800-GOOG-114", "US"));
363         assertEquals("+16502910000", PhoneNumberUtils.formatNumberToE164("650 2910000", "US"));
364         assertEquals("+12023458246", PhoneNumberUtils.formatNumberToE164("(202)345-8246", "US"));
365         assertEquals("+812023458246", PhoneNumberUtils.formatNumberToE164("202-345-8246", "JP"));
366     }
367 }
368