25fe690b3f7808ed02404b13c9638c16391cac13
[android/platform/frameworks/opt/telephony.git] / tests / telephonytests / src / com / android / internal / telephony / PhoneNumberUtilsTest.java
1 /*
2  * Copyright (C) 2006 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
17 package com.android.internal.telephony;
18
19 import android.net.Uri;
20 import android.platform.test.annotations.Postsubmit;
21 import android.support.test.filters.FlakyTest;
22 import android.telephony.PhoneNumberUtils;
23 import android.test.AndroidTestCase;
24 import android.test.suitebuilder.annotation.SmallTest;
25 import android.text.SpannableStringBuilder;
26
27 public class PhoneNumberUtilsTest extends AndroidTestCase {
28
29     @SmallTest
30     public void testExtractNetworkPortion() throws Exception {
31         assertEquals(
32                 "+17005554141",
33                 PhoneNumberUtils.extractNetworkPortion("+17005554141")
34         );
35
36         assertEquals(
37                 "+17005554141",
38                 PhoneNumberUtils.extractNetworkPortion("+1 (700).555-4141")
39         );
40
41         assertEquals(
42                 "17005554141",
43                 PhoneNumberUtils.extractNetworkPortion("1 (700).555-4141")
44         );
45
46         // This may seem wrong, but it's probably ok
47         assertEquals(
48                 "17005554141*#",
49                 PhoneNumberUtils.extractNetworkPortion("1 (700).555-4141*#")
50         );
51
52         assertEquals(
53                 "170055541NN",
54                 PhoneNumberUtils.extractNetworkPortion("1 (700).555-41NN")
55         );
56
57         assertEquals(
58                 "170055541NN",
59                 PhoneNumberUtils.extractNetworkPortion("1 (700).555-41NN,1234")
60         );
61
62         assertEquals(
63                 "170055541NN",
64                 PhoneNumberUtils.extractNetworkPortion("1 (700).555-41NN;1234")
65         );
66
67         // An MMI string is unperterbed, even though it contains a
68         // (valid in this case) embedded +
69         assertEquals(
70                 "**21**17005554141#",
71                 PhoneNumberUtils.extractNetworkPortion("**21**+17005554141#")
72                 //TODO this is the correct result, although the above
73                 //result has been returned since change 31776
74                 //"**21**+17005554141#"
75         );
76
77         assertEquals("", PhoneNumberUtils.extractNetworkPortion(""));
78
79         assertEquals("", PhoneNumberUtils.extractNetworkPortion(",1234"));
80
81         byte [] b = new byte[20];
82         b[0] = (byte) 0x81; b[1] = (byte) 0x71; b[2] = (byte) 0x00; b[3] = (byte) 0x55;
83         b[4] = (byte) 0x05; b[5] = (byte) 0x20; b[6] = (byte) 0xF0;
84         assertEquals("17005550020",
85             PhoneNumberUtils.calledPartyBCDToString(b, 0, 7));
86
87         b[0] = (byte) 0x80; b[1] = (byte) 0x71; b[2] = (byte) 0x00; b[3] = (byte) 0x55;
88         b[4] = (byte) 0x05; b[5] = (byte) 0x20; b[6] = (byte) 0xF0;
89         assertEquals("17005550020",
90             PhoneNumberUtils.calledPartyBCDToString(b, 0, 7));
91
92         b[0] = (byte) 0x90; b[1] = (byte) 0x71; b[2] = (byte) 0x00; b[3] = (byte) 0x55;
93         b[4] = (byte) 0x05; b[5] = (byte) 0x20; b[6] = (byte) 0xF0;
94         assertEquals("+17005550020",
95             PhoneNumberUtils.calledPartyBCDToString(b, 0, 7));
96
97         b[0] = (byte) 0x91; b[1] = (byte) 0x71; b[2] = (byte) 0x00; b[3] = (byte) 0x55;
98         b[4] = (byte) 0x05; b[5] = (byte) 0x20; b[6] = (byte) 0xF0;
99         assertEquals("+17005550020",
100                 PhoneNumberUtils.calledPartyBCDToString(b, 0, 7));
101
102         byte[] bRet = PhoneNumberUtils.networkPortionToCalledPartyBCD("+17005550020");
103         assertEquals(7, bRet.length);
104         for (int i = 0; i < 7; i++) {
105             assertEquals(b[i], bRet[i]);
106         }
107
108         bRet = PhoneNumberUtils.networkPortionToCalledPartyBCDWithLength("+17005550020");
109         assertEquals(8, bRet.length);
110         assertEquals(bRet[0], 7);
111         for (int i = 1; i < 8; i++) {
112             assertEquals(b[i - 1], bRet[i]);
113         }
114
115         bRet = PhoneNumberUtils.networkPortionToCalledPartyBCD("7005550020");
116         assertEquals("7005550020",
117             PhoneNumberUtils.calledPartyBCDToString(bRet, 0, bRet.length));
118
119         b[0] = (byte) 0x81; b[1] = (byte) 0x71; b[2] = (byte) 0x00; b[3] = (byte) 0x55;
120         b[4] = (byte) 0x05; b[5] = (byte) 0x20; b[6] = (byte) 0xB0;
121         assertEquals("17005550020#",
122             PhoneNumberUtils.calledPartyBCDToString(b, 0, 7));
123
124         b[0] = (byte) 0x91; b[1] = (byte) 0x71; b[2] = (byte) 0x00; b[3] = (byte) 0x55;
125         b[4] = (byte) 0x05; b[5] = (byte) 0x20; b[6] = (byte) 0xB0;
126         assertEquals("+17005550020#",
127             PhoneNumberUtils.calledPartyBCDToString(b, 0, 7));
128
129         b[0] = (byte) 0x81; b[1] = (byte) 0x2A; b[2] = (byte) 0xB1;
130         assertEquals("*21#",
131             PhoneNumberUtils.calledPartyBCDToString(b, 0, 3));
132
133         b[0] = (byte) 0x81; b[1] = (byte) 0x2B; b[2] = (byte) 0xB1;
134         assertEquals("#21#",
135             PhoneNumberUtils.calledPartyBCDToString(b, 0, 3));
136
137         b[0] = (byte) 0x91; b[1] = (byte) 0x2A; b[2] = (byte) 0xB1;
138         assertEquals("*21#+",
139             PhoneNumberUtils.calledPartyBCDToString(b, 0, 3));
140
141         b[0] = (byte) 0x81; b[1] = (byte) 0xAA; b[2] = (byte) 0x12; b[3] = (byte) 0xFB;
142         assertEquals("**21#",
143             PhoneNumberUtils.calledPartyBCDToString(b, 0, 4));
144
145         b[0] = (byte) 0x91; b[1] = (byte) 0xAA; b[2] = (byte) 0x12; b[3] = (byte) 0xFB;
146         assertEquals("**21#+",
147             PhoneNumberUtils.calledPartyBCDToString(b, 0, 4));
148
149         b[0] = (byte) 0x81; b[1] = (byte) 0x9A; b[2] = (byte) 0xA9; b[3] = (byte) 0x71;
150         b[4] = (byte) 0x00; b[5] = (byte) 0x55; b[6] = (byte) 0x05; b[7] = (byte) 0x20;
151         b[8] = (byte) 0xB0;
152         assertEquals("*99*17005550020#",
153             PhoneNumberUtils.calledPartyBCDToString(b, 0, 9));
154
155         b[0] = (byte) 0x91; b[1] = (byte) 0x9A; b[2] = (byte) 0xA9; b[3] = (byte) 0x71;
156         b[4] = (byte) 0x00; b[5] = (byte) 0x55; b[6] = (byte) 0x05; b[7] = (byte) 0x20;
157         b[8] = (byte) 0xB0;
158         assertEquals("*99*+17005550020#",
159             PhoneNumberUtils.calledPartyBCDToString(b, 0, 9));
160
161         b[0] = (byte) 0x81; b[1] = (byte) 0xAA; b[2] = (byte) 0x12; b[3] = (byte) 0x1A;
162         b[4] = (byte) 0x07; b[5] = (byte) 0x50; b[6] = (byte) 0x55; b[7] = (byte) 0x00;
163         b[8] = (byte) 0x02; b[9] = (byte) 0xFB;
164         assertEquals("**21*17005550020#",
165             PhoneNumberUtils.calledPartyBCDToString(b, 0, 10));
166
167         b[0] = (byte) 0x91; b[1] = (byte) 0xAA; b[2] = (byte) 0x12; b[3] = (byte) 0x1A;
168         b[4] = (byte) 0x07; b[5] = (byte) 0x50; b[6] = (byte) 0x55; b[7] = (byte) 0x00;
169         b[8] = (byte) 0x02; b[9] = (byte) 0xFB;
170         assertEquals("**21*+17005550020#",
171             PhoneNumberUtils.calledPartyBCDToString(b, 0, 10));
172
173         b[0] = (byte) 0x81; b[1] = (byte) 0x2A; b[2] = (byte) 0xA1; b[3] = (byte) 0x71;
174         b[4] = (byte) 0x00; b[5] = (byte) 0x55; b[6] = (byte) 0x05; b[7] = (byte) 0x20;
175         b[8] = (byte) 0xF0;
176         assertEquals("*21*17005550020",
177             PhoneNumberUtils.calledPartyBCDToString(b, 0, 9));
178
179         b[0] = (byte) 0x91; b[1] = (byte) 0x2A; b[2] = (byte) 0xB1; b[3] = (byte) 0x71;
180         b[4] = (byte) 0x00; b[5] = (byte) 0x55; b[6] = (byte) 0x05; b[7] = (byte) 0x20;
181         b[8] = (byte) 0xF0;
182         assertEquals("*21#+17005550020",
183             PhoneNumberUtils.calledPartyBCDToString(b, 0, 9));
184
185         assertNull(PhoneNumberUtils.extractNetworkPortion(null));
186         assertNull(PhoneNumberUtils.extractPostDialPortion(null));
187         assertTrue(PhoneNumberUtils.compare(null, null));
188         assertFalse(PhoneNumberUtils.compare(null, "123"));
189         assertFalse(PhoneNumberUtils.compare("123", null));
190         assertNull(PhoneNumberUtils.toCallerIDMinMatch(null));
191         assertNull(PhoneNumberUtils.getStrippedReversed(null));
192         assertNull(PhoneNumberUtils.stringFromStringAndTOA(null, 1));
193     }
194
195     @SmallTest
196     public void testExtractNetworkPortionAlt() throws Exception {
197         assertEquals(
198                 "+17005554141",
199                 PhoneNumberUtils.extractNetworkPortionAlt("+17005554141")
200         );
201
202         assertEquals(
203                 "+17005554141",
204                 PhoneNumberUtils.extractNetworkPortionAlt("+1 (700).555-4141")
205         );
206
207         assertEquals(
208                 "17005554141",
209                 PhoneNumberUtils.extractNetworkPortionAlt("1 (700).555-4141")
210         );
211
212         // This may seem wrong, but it's probably ok
213         assertEquals(
214                 "17005554141*#",
215                 PhoneNumberUtils.extractNetworkPortionAlt("1 (700).555-4141*#")
216         );
217
218         assertEquals(
219                 "170055541NN",
220                 PhoneNumberUtils.extractNetworkPortionAlt("1 (700).555-41NN")
221         );
222
223         assertEquals(
224                 "170055541NN",
225                 PhoneNumberUtils.extractNetworkPortionAlt("1 (700).555-41NN,1234")
226         );
227
228         assertEquals(
229                 "170055541NN",
230                 PhoneNumberUtils.extractNetworkPortionAlt("1 (700).555-41NN;1234")
231         );
232
233         // An MMI string is unperterbed, even though it contains a
234         // (valid in this case) embedded +
235         assertEquals(
236                 "**21**+17005554141#",
237                 PhoneNumberUtils.extractNetworkPortionAlt("**21**+17005554141#")
238         );
239
240         assertEquals(
241                 "*31#+447966164208",
242                 PhoneNumberUtils.extractNetworkPortionAlt("*31#+447966164208")
243         );
244
245         assertEquals(
246                 "*31#+447966164208",
247                 PhoneNumberUtils.extractNetworkPortionAlt("*31# (+44) 79 6616 4208")
248         );
249
250         assertEquals("", PhoneNumberUtils.extractNetworkPortionAlt(""));
251
252         assertEquals("", PhoneNumberUtils.extractNetworkPortionAlt(",1234"));
253
254         assertNull(PhoneNumberUtils.extractNetworkPortionAlt(null));
255     }
256
257     @SmallTest
258     public void testB() throws Exception {
259         assertEquals("", PhoneNumberUtils.extractPostDialPortion("+17005554141"));
260         assertEquals("", PhoneNumberUtils.extractPostDialPortion("+1 (700).555-4141"));
261         assertEquals("", PhoneNumberUtils.extractPostDialPortion("+1 (700).555-41NN"));
262         assertEquals(",1234", PhoneNumberUtils.extractPostDialPortion("+1 (700).555-41NN,1234"));
263         assertEquals(";1234", PhoneNumberUtils.extractPostDialPortion("+1 (700).555-41NN;1234"));
264         assertEquals(";1234,;N",
265                 PhoneNumberUtils.extractPostDialPortion("+1 (700).555-41NN;1-2.34 ,;N"));
266     }
267
268     @SmallTest
269     public void testCompare() throws Exception {
270         // this is odd
271         assertFalse(PhoneNumberUtils.compare("", ""));
272
273         assertTrue(PhoneNumberUtils.compare("911", "911"));
274         assertFalse(PhoneNumberUtils.compare("911", "18005550911"));
275         assertTrue(PhoneNumberUtils.compare("5555", "5555"));
276         assertFalse(PhoneNumberUtils.compare("5555", "180055555555"));
277
278         assertTrue(PhoneNumberUtils.compare("+17005554141", "+17005554141"));
279         assertTrue(PhoneNumberUtils.compare("+17005554141", "+1 (700).555-4141"));
280         assertTrue(PhoneNumberUtils.compare("+17005554141", "+1 (700).555-4141,1234"));
281         assertTrue(PhoneNumberUtils.compare("+17005554141", "17005554141"));
282         assertTrue(PhoneNumberUtils.compare("+17005554141", "7005554141"));
283         assertTrue(PhoneNumberUtils.compare("+17005554141", "5554141"));
284         assertTrue(PhoneNumberUtils.compare("17005554141", "5554141"));
285         assertTrue(PhoneNumberUtils.compare("+17005554141", "01117005554141"));
286         assertTrue(PhoneNumberUtils.compare("+17005554141", "0017005554141"));
287         assertTrue(PhoneNumberUtils.compare("17005554141", "0017005554141"));
288
289
290         assertTrue(PhoneNumberUtils.compare("+17005554141", "**31#+17005554141"));
291
292         assertFalse(PhoneNumberUtils.compare("+1 999 7005554141", "+1 7005554141"));
293         assertTrue(PhoneNumberUtils.compare("011 1 7005554141", "7005554141"));
294
295         assertFalse(PhoneNumberUtils.compare("011 11 7005554141", "+17005554141"));
296
297         assertFalse(PhoneNumberUtils.compare("+17005554141", "7085882300"));
298
299         assertTrue(PhoneNumberUtils.compare("+44 207 792 3490", "0 207 792 3490"));
300
301         assertFalse(PhoneNumberUtils.compare("+44 207 792 3490", "00 207 792 3490"));
302         assertFalse(PhoneNumberUtils.compare("+44 207 792 3490", "011 207 792 3490"));
303
304         /***** FIXME's ******/
305         //
306         // MMI header should be ignored
307         assertFalse(PhoneNumberUtils.compare("+17005554141", "**31#17005554141"));
308
309         // It's too bad this is false
310         // +44 (0) 207 792 3490 is not a dialable number
311         // but it is commonly how European phone numbers are written
312         assertFalse(PhoneNumberUtils.compare("+44 207 792 3490", "+44 (0) 207 792 3490"));
313
314         // The japanese international prefix, for example, messes us up
315         // But who uses a GSM phone in Japan?
316         assertFalse(PhoneNumberUtils.compare("+44 207 792 3490", "010 44 207 792 3490"));
317
318         // The Australian one messes us up too
319         assertFalse(PhoneNumberUtils.compare("+44 207 792 3490", "0011 44 207 792 3490"));
320
321         // The Russian trunk prefix messes us up, as does current
322         // Russian area codes (which bein with 0)
323
324         assertFalse(PhoneNumberUtils.compare("+7(095)9100766", "8(095)9100766"));
325
326         // 444 is not a valid country code, but
327         // matchIntlPrefixAndCC doesnt know this
328         assertTrue(PhoneNumberUtils.compare("+444 207 792 3490", "0 207 792 3490"));
329
330         // compare SMS short code
331         assertTrue(PhoneNumberUtils.compare("404-04", "40404"));
332     }
333
334
335     @SmallTest
336     public void testToCallerIDIndexable() throws Exception {
337         assertEquals("1414555", PhoneNumberUtils.toCallerIDMinMatch("17005554141"));
338         assertEquals("1414555", PhoneNumberUtils.toCallerIDMinMatch("1-700-555-4141"));
339         assertEquals("1414555", PhoneNumberUtils.toCallerIDMinMatch("1-700-555-4141,1234"));
340         assertEquals("1414555", PhoneNumberUtils.toCallerIDMinMatch("1-700-555-4141;1234"));
341
342         //this seems wrong, or at least useless
343         assertEquals("NN14555", PhoneNumberUtils.toCallerIDMinMatch("1-700-555-41NN"));
344
345         //<shrug> -- these are all not useful, but not terribly wrong
346         assertEquals("", PhoneNumberUtils.toCallerIDMinMatch(""));
347         assertEquals("0032", PhoneNumberUtils.toCallerIDMinMatch("2300"));
348         assertEquals("0032+", PhoneNumberUtils.toCallerIDMinMatch("+2300"));
349         assertEquals("#130#*", PhoneNumberUtils.toCallerIDMinMatch("*#031#"));
350     }
351
352     @SmallTest
353     public void testGetIndexable() throws Exception {
354         assertEquals("14145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-4141"));
355         assertEquals("14145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-4141,1234"));
356         assertEquals("14145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-4141;1234"));
357
358         //this seems wrong, or at least useless
359         assertEquals("NN145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-41NN"));
360
361         //<shrug> -- these are all not useful, but not terribly wrong
362         assertEquals("", PhoneNumberUtils.getStrippedReversed(""));
363         assertEquals("0032", PhoneNumberUtils.getStrippedReversed("2300"));
364         assertEquals("0032+", PhoneNumberUtils.getStrippedReversed("+2300"));
365         assertEquals("#130#*", PhoneNumberUtils.getStrippedReversed("*#031#"));
366     }
367
368     @SmallTest
369     public void testNanpFormatting() {
370         SpannableStringBuilder number = new SpannableStringBuilder();
371         number.append("8005551212");
372         PhoneNumberUtils.formatNanpNumber(number);
373         assertEquals("800-555-1212", number.toString());
374
375         number.clear();
376         number.append("800555121");
377         PhoneNumberUtils.formatNanpNumber(number);
378         assertEquals("800-555-121", number.toString());
379
380         number.clear();
381         number.append("555-1212");
382         PhoneNumberUtils.formatNanpNumber(number);
383         assertEquals("555-1212", number.toString());
384
385         number.clear();
386         number.append("800-55512");
387         PhoneNumberUtils.formatNanpNumber(number);
388         assertEquals("800-555-12", number.toString());
389
390         number.clear();
391         number.append("46645");
392         PhoneNumberUtils.formatNanpNumber(number);
393         assertEquals("46645", number.toString());
394     }
395
396     @SmallTest
397     public void testConvertKeypadLettersToDigits() {
398         assertEquals("1-800-4664-411",
399                      PhoneNumberUtils.convertKeypadLettersToDigits("1-800-GOOG-411"));
400         assertEquals("18004664411",
401                      PhoneNumberUtils.convertKeypadLettersToDigits("1800GOOG411"));
402         assertEquals("1-800-466-4411",
403                      PhoneNumberUtils.convertKeypadLettersToDigits("1-800-466-4411"));
404         assertEquals("18004664411",
405                      PhoneNumberUtils.convertKeypadLettersToDigits("18004664411"));
406         assertEquals("222-333-444-555-666-7777-888-9999",
407                      PhoneNumberUtils.convertKeypadLettersToDigits(
408                              "ABC-DEF-GHI-JKL-MNO-PQRS-TUV-WXYZ"));
409         assertEquals("222-333-444-555-666-7777-888-9999",
410                      PhoneNumberUtils.convertKeypadLettersToDigits(
411                              "abc-def-ghi-jkl-mno-pqrs-tuv-wxyz"));
412         assertEquals("(800) 222-3334",
413                      PhoneNumberUtils.convertKeypadLettersToDigits("(800) ABC-DEFG"));
414     }
415
416     // To run this test, the device has to be registered with network
417     @FlakyTest
418     @Postsubmit
419     public void testCheckAndProcessPlusCode() {
420         assertEquals("0118475797000",
421                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+8475797000"));
422         assertEquals("18475797000",
423                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+18475797000"));
424         assertEquals("0111234567",
425                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+1234567"));
426         assertEquals("01123456700000",
427                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+23456700000"));
428         assertEquals("01111875767800",
429                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+11875767800"));
430         assertEquals("8475797000,18475231753",
431                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,+18475231753"));
432         assertEquals("0118475797000,18475231753",
433                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+8475797000,+18475231753"));
434         assertEquals("8475797000;0118469312345",
435                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000;+8469312345"));
436         assertEquals("8475797000,0111234567",
437                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,+1234567"));
438         assertEquals("847597000;01111875767000",
439                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("847597000;+11875767000"));
440         assertEquals("8475797000,,0118469312345",
441                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,,+8469312345"));
442         assertEquals("8475797000;,0118469312345",
443                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000;,+8469312345"));
444         assertEquals("8475797000,;18475231753",
445                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,;+18475231753"));
446         assertEquals("8475797000;,01111875767000",
447                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000;,+11875767000"));
448         assertEquals("8475797000,;01111875767000",
449                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,;+11875767000"));
450         assertEquals("8475797000,,,01111875767000",
451                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,,,+11875767000"));
452         assertEquals("8475797000;,,01111875767000",
453                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000;,,+11875767000"));
454         assertEquals("+;,8475797000",
455                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+;,8475797000"));
456         assertEquals("8475797000,",
457                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,"));
458         assertEquals("847+579-7000",
459                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("847+579-7000"));
460         assertEquals(",8475797000",
461                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode(",8475797000"));
462         assertEquals(";;8475797000,,",
463                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode(";;8475797000,,"));
464         assertEquals("+this+is$weird;,+",
465                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+this+is$weird;,+"));
466         assertEquals("",
467                 PhoneNumberUtils.cdmaCheckAndProcessPlusCode(""));
468         assertNull(PhoneNumberUtils.cdmaCheckAndProcessPlusCode(null));
469     }
470
471     @SmallTest
472     public void testCheckAndProcessPlusCodeByNumberFormat() {
473         assertEquals("18475797000",
474                 PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+18475797000",
475                 PhoneNumberUtils.FORMAT_NANP,PhoneNumberUtils.FORMAT_NANP));
476     }
477
478     /**
479      * Basic checks for the VoiceMail number.
480      */
481     @SmallTest
482     public void testWithNumberNotEqualToVoiceMail() throws Exception {
483         assertFalse(PhoneNumberUtils.isVoiceMailNumber("911"));
484         assertFalse(PhoneNumberUtils.isVoiceMailNumber("tel:911"));
485         assertFalse(PhoneNumberUtils.isVoiceMailNumber("+18001234567"));
486         assertFalse(PhoneNumberUtils.isVoiceMailNumber(""));
487         assertFalse(PhoneNumberUtils.isVoiceMailNumber(null));
488         // This test fails on a device without a sim card
489         /*TelephonyManager mTelephonyManager =
490             (TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE);
491         String mVoiceMailNumber = mTelephonyManager.getDefault().getVoiceMailNumber();
492         assertTrue(PhoneNumberUtils.isVoiceMailNumber(mVoiceMailNumber));
493         */
494     }
495
496     @SmallTest
497     public void testFormatNumberToE164() {
498         // Note: ISO 3166-1 only allows upper case country codes.
499         assertEquals("+16502910000", PhoneNumberUtils.formatNumberToE164("650 2910000", "US"));
500         assertNull(PhoneNumberUtils.formatNumberToE164("1234567", "US"));
501         assertEquals("+18004664114", PhoneNumberUtils.formatNumberToE164("800-GOOG-114", "US"));
502     }
503
504     @SmallTest
505     public void testFormatNumber() {
506         assertEquals("(650) 291-0000", PhoneNumberUtils.formatNumber("650 2910000", "US"));
507         assertEquals("223-4567", PhoneNumberUtils.formatNumber("2234567", "US"));
508         assertEquals("011 86 10 8888 0000",
509                      PhoneNumberUtils.formatNumber("011861088880000", "US"));
510         assertEquals("010 8888 0000", PhoneNumberUtils.formatNumber("01088880000", "CN"));
511         // formatNumber doesn't format alpha numbers, but keep them as they are.
512         assertEquals("800-GOOG-114", PhoneNumberUtils.formatNumber("800-GOOG-114", "US"));
513     }
514
515     /**
516      * Tests ability to format phone numbers from Japan using the international format when the
517      * current country is not Japan.
518      */
519     @SmallTest
520     public void testFormatJapanInternational() {
521         assertEquals("+81 90-6657-1180", PhoneNumberUtils.formatNumber("+819066571180", "US"));
522     }
523
524     /**
525      * Tests ability to format phone numbers from Japan using the national format when the current
526      * country is Japan.
527      */
528     @SmallTest
529     public void testFormatJapanNational() {
530         assertEquals("090-6657-0660", PhoneNumberUtils.formatNumber("09066570660", "JP"));
531         assertEquals("090-6657-1180", PhoneNumberUtils.formatNumber("+819066571180", "JP"));
532
533         // US number should still be internationally formatted
534         assertEquals("+1 650-555-1212", PhoneNumberUtils.formatNumber("+16505551212", "JP"));
535     }
536
537     @SmallTest
538     public void testFormatNumber_LeadingStarAndHash() {
539         // Numbers with a leading '*' or '#' should be left unchanged.
540         assertEquals("*650 2910000", PhoneNumberUtils.formatNumber("*650 2910000", "US"));
541         assertEquals("#650 2910000", PhoneNumberUtils.formatNumber("#650 2910000", "US"));
542         assertEquals("*#650 2910000", PhoneNumberUtils.formatNumber("*#650 2910000", "US"));
543         assertEquals("#*650 2910000", PhoneNumberUtils.formatNumber("#*650 2910000", "US"));
544         assertEquals("#650*2910000", PhoneNumberUtils.formatNumber("#650*2910000", "US"));
545         assertEquals("#650*2910000", PhoneNumberUtils.formatNumber("#650*2910000", "US"));
546         assertEquals("##650 2910000", PhoneNumberUtils.formatNumber("##650 2910000", "US"));
547         assertEquals("**650 2910000", PhoneNumberUtils.formatNumber("**650 2910000", "US"));
548     }
549
550     @SmallTest
551     public void testNormalizeNumber() {
552         assertEquals("6502910000", PhoneNumberUtils.normalizeNumber("650 2910000"));
553         assertEquals("1234567", PhoneNumberUtils.normalizeNumber("12,3#4*567"));
554         assertEquals("8004664114", PhoneNumberUtils.normalizeNumber("800-GOOG-114"));
555         assertEquals("+16502910000", PhoneNumberUtils.normalizeNumber("+1 650 2910000"));
556     }
557
558     @SmallTest
559     public void testFormatDailabeNumber() {
560         // Using the phoneNumberE164's country code
561         assertEquals("(650) 291-0000",
562                 PhoneNumberUtils.formatNumber("6502910000", "+16502910000", "CN"));
563         // Using the default country code for a phone number containing the IDD
564         assertEquals("011 86 10 8888 0000",
565                 PhoneNumberUtils.formatNumber("011861088880000", "+861088880000", "US"));
566         assertEquals("00 86 10 8888 0000",
567                 PhoneNumberUtils.formatNumber("00861088880000", "+861088880000", "GB"));
568         assertEquals("+86 10 8888 0000",
569                 PhoneNumberUtils.formatNumber("+861088880000", "+861088880000", "GB"));
570         // Wrong default country, so no formatting is done
571         assertEquals("011861088880000",
572                 PhoneNumberUtils.formatNumber("011861088880000", "+861088880000", "GB"));
573         // The phoneNumberE164 is null
574         assertEquals("(650) 291-0000", PhoneNumberUtils.formatNumber("6502910000", null, "US"));
575         // The given number has a country code.
576         assertEquals("+1 650-291-0000", PhoneNumberUtils.formatNumber("+16502910000", null, "CN"));
577         // The given number was formatted.
578         assertEquals("650-291-0000", PhoneNumberUtils.formatNumber("650-291-0000", null, "US"));
579         // A valid Polish number should be formatted.
580         assertEquals("506 128 687", PhoneNumberUtils.formatNumber("506128687", null, "PL"));
581         // An invalid Polish number should be left as it is. Note Poland doesn't use '0' as a
582         // national prefix; therefore, the leading '0' makes the number invalid.
583         assertEquals("0506128687", PhoneNumberUtils.formatNumber("0506128687", null, "PL"));
584         // Wrong default country, so no formatting is done
585         assertEquals("011861088880000",
586                 PhoneNumberUtils.formatNumber("011861088880000", "", "GB"));
587     }
588
589     @Postsubmit
590     @SmallTest
591     public void testIsEmergencyNumber() {
592         // There are two parallel sets of tests here: one for the
593         // regular isEmergencyNumber() method, and the other for
594         // isPotentialEmergencyNumber().
595         //
596         // (The difference is that isEmergencyNumber() will return true
597         // only if the specified number exactly matches an actual
598         // emergency number, but isPotentialEmergencyNumber() will
599         // return true if the specified number simply starts with the
600         // same digits as any actual emergency number.)
601
602         // Tests for isEmergencyNumber():
603         assertTrue(PhoneNumberUtils.isEmergencyNumber("911", "US"));
604         assertTrue(PhoneNumberUtils.isEmergencyNumber("112", "US"));
605         // The next two numbers are not valid phone numbers in the US,
606         // so do not count as emergency numbers (but they *are* "potential"
607         // emergency numbers; see below.)
608         assertFalse(PhoneNumberUtils.isEmergencyNumber("91112345", "US"));
609         assertFalse(PhoneNumberUtils.isEmergencyNumber("11212345", "US"));
610         // A valid mobile phone number from Singapore shouldn't be classified as an emergency number
611         // in Singapore, as 911 is not an emergency number there.
612         assertFalse(PhoneNumberUtils.isEmergencyNumber("91121234", "SG"));
613         // A valid fixed-line phone number from Brazil shouldn't be classified as an emergency number
614         // in Brazil, as 112 is not an emergency number there.
615         assertFalse(PhoneNumberUtils.isEmergencyNumber("1121234567", "BR"));
616         // A valid local phone number from Brazil shouldn't be classified as an emergency number in
617         // Brazil.
618         assertFalse(PhoneNumberUtils.isEmergencyNumber("91112345", "BR"));
619
620         // Tests for isPotentialEmergencyNumber():
621         // These first two are obviously emergency numbers:
622         assertTrue(PhoneNumberUtils.isPotentialEmergencyNumber("911", "US"));
623         assertTrue(PhoneNumberUtils.isPotentialEmergencyNumber("112", "US"));
624         // The next two numbers are not valid phone numbers in the US, but can be used to trick the
625         // system to dial 911 and 112, which are emergency numbers in the US. For the purpose of
626         // addressing that, they are also classified as "potential" emergency numbers in the US.
627         assertTrue(PhoneNumberUtils.isPotentialEmergencyNumber("91112345", "US"));
628         assertTrue(PhoneNumberUtils.isPotentialEmergencyNumber("11212345", "US"));
629
630         // A valid mobile phone number from Singapore shouldn't be classified as an emergency number
631         // in Singapore, as 911 is not an emergency number there.
632         // This test fails on devices that have ecclist property preloaded with 911.
633         // assertFalse(PhoneNumberUtils.isPotentialEmergencyNumber("91121234", "SG"));
634
635         // A valid fixed-line phone number from Brazil shouldn't be classified as an emergency number
636         // in Brazil, as 112 is not an emergency number there.
637         assertFalse(PhoneNumberUtils.isPotentialEmergencyNumber("1121234567", "BR"));
638         // A valid local phone number from Brazil shouldn't be classified as an emergency number in
639         // Brazil.
640         assertFalse(PhoneNumberUtils.isPotentialEmergencyNumber("91112345", "BR"));
641     }
642
643     @SmallTest
644     public void testStripSeparators() {
645         // Smoke tests which should never fail.
646         assertEquals("1234567890", PhoneNumberUtils.stripSeparators("1234567890"));
647         assertEquals("911", PhoneNumberUtils.stripSeparators("911"));
648         assertEquals("112", PhoneNumberUtils.stripSeparators("112"));
649
650         // Separators should be removed, while '+' or any other digits should not.
651         assertEquals("+16502910000", PhoneNumberUtils.stripSeparators("+1 (650) 291-0000"));
652
653         // WAIT, PAUSE should *not* be stripped
654         assertEquals("+16502910000,300;",
655                 PhoneNumberUtils.stripSeparators("+1 (650) 291-0000, 300;"));
656     }
657
658     @SmallTest
659     public void testConvertAndStrip() {
660         // Smoke tests which should never fail.
661         assertEquals("1234567890", PhoneNumberUtils.convertAndStrip("1234567890"));
662         assertEquals("911", PhoneNumberUtils.convertAndStrip("911"));
663         assertEquals("112", PhoneNumberUtils.convertAndStrip("112"));
664
665         // It should convert keypad characters into digits, and strip separators
666         assertEquals("22233344455566677778889999",
667                 PhoneNumberUtils.convertAndStrip("ABC DEF GHI JKL MNO PQR STUV WXYZ"));
668
669         // Test real cases.
670         assertEquals("18004664411", PhoneNumberUtils.convertAndStrip("1-800-GOOG-411"));
671         assertEquals("8002223334", PhoneNumberUtils.convertAndStrip("(800) ABC-DEFG"));
672     }
673
674     @SmallTest
675     public void testConvertSipUriToTelUri1() {
676         // Nominal case, a tel Uri came in, so we expect one out.
677         Uri source = Uri.fromParts("tel", "+16505551212", null);
678         Uri expected = Uri.fromParts("tel", "+16505551212", null);
679         Uri converted = PhoneNumberUtils.convertSipUriToTelUri(source);
680         assertEquals(expected, converted);
681
682         // Valid cases
683         source = Uri.fromParts("sip", "+16505551212@sipinator.com", null);
684         converted = PhoneNumberUtils.convertSipUriToTelUri(source);
685         assertEquals(expected, converted);
686
687         source = Uri.fromParts("sip", "+16505551212;phone-context=blah.com@host.com", null);
688         converted = PhoneNumberUtils.convertSipUriToTelUri(source);
689         assertEquals(expected, converted);
690
691         source = Uri.fromParts("sip", "+16505551212@something.com;user=phone", null);
692         converted = PhoneNumberUtils.convertSipUriToTelUri(source);
693         assertEquals(expected, converted);
694     }
695
696     @SmallTest
697     public void testIsInternational() {
698         assertFalse(PhoneNumberUtils.isInternationalNumber("+16505551212", "US"));
699         assertTrue(PhoneNumberUtils.isInternationalNumber("+16505551212", "UK"));
700         assertTrue(PhoneNumberUtils.isInternationalNumber("+16505551212", "JP"));
701         assertTrue(PhoneNumberUtils.isInternationalNumber("+86 10 8888 0000", "US"));
702         assertTrue(PhoneNumberUtils.isInternationalNumber("001-541-754-3010", "DE"));
703         assertFalse(PhoneNumberUtils.isInternationalNumber("001-541-754-3010", "US"));
704         assertTrue(PhoneNumberUtils.isInternationalNumber("01161396694916", "US"));
705         assertTrue(PhoneNumberUtils.isInternationalNumber("011-613-966-94916", "US"));
706         assertFalse(PhoneNumberUtils.isInternationalNumber("011-613-966-94916", "AU"));
707     }
708 }