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