1 /* 2 * Copyright (C) 2013 The Libphonenumber Authors 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.i18n.phonenumbers; 18 19 import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber; 20 21 /** 22 * Unit tests for ShortNumberInfo.java 23 * 24 * @author Shaopeng Jia 25 */ 26 public class ShortNumberInfoTest extends TestMetadataTestCase { 27 private ShortNumberInfo shortInfo; 28 29 public ShortNumberInfoTest() { 30 shortInfo = new ShortNumberInfo(phoneUtil); 31 } 32 33 public void testIsPossibleShortNumber() { 34 PhoneNumber possibleNumber = new PhoneNumber(); 35 possibleNumber.setCountryCode(33).setNationalNumber(123456L); 36 assertTrue(shortInfo.isPossibleShortNumber(possibleNumber)); 37 assertTrue(shortInfo.isPossibleShortNumberForRegion("123456", RegionCode.FR)); 38 39 PhoneNumber impossibleNumber = new PhoneNumber(); 40 impossibleNumber.setCountryCode(33).setNationalNumber(9L); 41 assertFalse(shortInfo.isPossibleShortNumber(impossibleNumber)); 42 assertFalse(shortInfo.isPossibleShortNumberForRegion("9", RegionCode.FR)); 43 44 // Note that GB and GG share the country calling code 44, and that this number is possible but 45 // not valid. 46 assertTrue(shortInfo.isPossibleShortNumber( 47 new PhoneNumber().setCountryCode(44).setNationalNumber(11001L))); 48 } 49 50 public void testIsValidShortNumber() { 51 assertTrue(shortInfo.isValidShortNumber( 52 new PhoneNumber().setCountryCode(33).setNationalNumber(1010L))); 53 assertTrue(shortInfo.isValidShortNumberForRegion("1010", RegionCode.FR)); 54 assertFalse(shortInfo.isValidShortNumber( 55 new PhoneNumber().setCountryCode(33).setNationalNumber(123456L))); 56 assertFalse(shortInfo.isValidShortNumberForRegion("123456", RegionCode.FR)); 57 58 // Note that GB and GG share the country calling code 44. 59 assertTrue(shortInfo.isValidShortNumber( 60 new PhoneNumber().setCountryCode(44).setNationalNumber(18001L))); 61 } 62 63 public void testGetExpectedCost() { 64 String premiumRateExample = shortInfo.getExampleShortNumberForCost( 65 RegionCode.FR, ShortNumberInfo.ShortNumberCost.PREMIUM_RATE); 66 assertEquals(ShortNumberInfo.ShortNumberCost.PREMIUM_RATE, 67 shortInfo.getExpectedCostForRegion(premiumRateExample, RegionCode.FR)); 68 PhoneNumber premiumRateNumber = new PhoneNumber(); 69 premiumRateNumber.setCountryCode(33).setNationalNumber(Integer.parseInt(premiumRateExample)); 70 assertEquals(ShortNumberInfo.ShortNumberCost.PREMIUM_RATE, 71 shortInfo.getExpectedCost(premiumRateNumber)); 72 73 String standardRateExample = shortInfo.getExampleShortNumberForCost( 74 RegionCode.FR, ShortNumberInfo.ShortNumberCost.STANDARD_RATE); 75 assertEquals(ShortNumberInfo.ShortNumberCost.STANDARD_RATE, 76 shortInfo.getExpectedCostForRegion(standardRateExample, RegionCode.FR)); 77 PhoneNumber standardRateNumber = new PhoneNumber(); 78 standardRateNumber.setCountryCode(33).setNationalNumber(Integer.parseInt(standardRateExample)); 79 assertEquals(ShortNumberInfo.ShortNumberCost.STANDARD_RATE, 80 shortInfo.getExpectedCost(standardRateNumber)); 81 82 String tollFreeExample = shortInfo.getExampleShortNumberForCost( 83 RegionCode.FR, ShortNumberInfo.ShortNumberCost.TOLL_FREE); 84 assertEquals(ShortNumberInfo.ShortNumberCost.TOLL_FREE, 85 shortInfo.getExpectedCostForRegion(tollFreeExample, RegionCode.FR)); 86 PhoneNumber tollFreeNumber = new PhoneNumber(); 87 tollFreeNumber.setCountryCode(33).setNationalNumber(Integer.parseInt(tollFreeExample)); 88 assertEquals(ShortNumberInfo.ShortNumberCost.TOLL_FREE, 89 shortInfo.getExpectedCost(tollFreeNumber)); 90 91 assertEquals(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST, 92 shortInfo.getExpectedCostForRegion("12345", RegionCode.FR)); 93 PhoneNumber unknownCostNumber = new PhoneNumber(); 94 unknownCostNumber.setCountryCode(33).setNationalNumber(12345L); 95 assertEquals(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST, 96 shortInfo.getExpectedCost(unknownCostNumber)); 97 98 // Test that an invalid number may nevertheless have a cost other than UNKNOWN_COST. 99 assertFalse(shortInfo.isValidShortNumberForRegion("116123", RegionCode.FR)); 100 assertEquals(ShortNumberInfo.ShortNumberCost.TOLL_FREE, 101 shortInfo.getExpectedCostForRegion("116123", RegionCode.FR)); 102 PhoneNumber invalidNumber = new PhoneNumber(); 103 invalidNumber.setCountryCode(33).setNationalNumber(116123L); 104 assertFalse(shortInfo.isValidShortNumber(invalidNumber)); 105 assertEquals(ShortNumberInfo.ShortNumberCost.TOLL_FREE, 106 shortInfo.getExpectedCost(invalidNumber)); 107 108 // Test a nonexistent country code. 109 assertEquals(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST, 110 shortInfo.getExpectedCostForRegion("911", RegionCode.ZZ)); 111 unknownCostNumber.clear(); 112 unknownCostNumber.setCountryCode(123).setNationalNumber(911L); 113 assertEquals(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST, 114 shortInfo.getExpectedCost(unknownCostNumber)); 115 } 116 117 public void testGetExpectedCostForSharedCountryCallingCode() { 118 // Test some numbers which have different costs in countries sharing the same country calling 119 // code. In Australia, 1234 is premium-rate, 1194 is standard-rate, and 733 is toll-free. These 120 // are not known to be valid numbers in the Christmas Islands. 121 String ambiguousPremiumRateString = "1234"; 122 PhoneNumber ambiguousPremiumRateNumber = new PhoneNumber().setCountryCode(61) 123 .setNationalNumber(1234L); 124 String ambiguousStandardRateString = "1194"; 125 PhoneNumber ambiguousStandardRateNumber = new PhoneNumber().setCountryCode(61) 126 .setNationalNumber(1194L); 127 String ambiguousTollFreeString = "733"; 128 PhoneNumber ambiguousTollFreeNumber = new PhoneNumber().setCountryCode(61) 129 .setNationalNumber(733L); 130 131 assertTrue(shortInfo.isValidShortNumber(ambiguousPremiumRateNumber)); 132 assertTrue(shortInfo.isValidShortNumber(ambiguousStandardRateNumber)); 133 assertTrue(shortInfo.isValidShortNumber(ambiguousTollFreeNumber)); 134 135 assertTrue(shortInfo.isValidShortNumberForRegion(ambiguousPremiumRateString, RegionCode.AU)); 136 assertEquals(ShortNumberInfo.ShortNumberCost.PREMIUM_RATE, 137 shortInfo.getExpectedCostForRegion(ambiguousPremiumRateString, RegionCode.AU)); 138 assertFalse(shortInfo.isValidShortNumberForRegion(ambiguousPremiumRateString, RegionCode.CX)); 139 assertEquals(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST, 140 shortInfo.getExpectedCostForRegion(ambiguousPremiumRateString, RegionCode.CX)); 141 // PREMIUM_RATE takes precedence over UNKNOWN_COST. 142 assertEquals(ShortNumberInfo.ShortNumberCost.PREMIUM_RATE, 143 shortInfo.getExpectedCost(ambiguousPremiumRateNumber)); 144 145 assertTrue(shortInfo.isValidShortNumberForRegion(ambiguousStandardRateString, RegionCode.AU)); 146 assertEquals(ShortNumberInfo.ShortNumberCost.STANDARD_RATE, 147 shortInfo.getExpectedCostForRegion(ambiguousStandardRateString, RegionCode.AU)); 148 assertFalse(shortInfo.isValidShortNumberForRegion(ambiguousStandardRateString, RegionCode.CX)); 149 assertEquals(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST, 150 shortInfo.getExpectedCostForRegion(ambiguousStandardRateString, RegionCode.CX)); 151 assertEquals(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST, 152 shortInfo.getExpectedCost(ambiguousStandardRateNumber)); 153 154 assertTrue(shortInfo.isValidShortNumberForRegion(ambiguousTollFreeString, RegionCode.AU)); 155 assertEquals(ShortNumberInfo.ShortNumberCost.TOLL_FREE, 156 shortInfo.getExpectedCostForRegion(ambiguousTollFreeString, RegionCode.AU)); 157 assertFalse(shortInfo.isValidShortNumberForRegion(ambiguousTollFreeString, RegionCode.CX)); 158 assertEquals(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST, 159 shortInfo.getExpectedCostForRegion(ambiguousTollFreeString, RegionCode.CX)); 160 assertEquals(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST, 161 shortInfo.getExpectedCost(ambiguousTollFreeNumber)); 162 } 163 164 public void testGetExampleShortNumber() { 165 assertEquals("8711", shortInfo.getExampleShortNumber(RegionCode.AM)); 166 assertEquals("1010", shortInfo.getExampleShortNumber(RegionCode.FR)); 167 assertEquals("", shortInfo.getExampleShortNumber(RegionCode.UN001)); 168 assertEquals("", shortInfo.getExampleShortNumber(null)); 169 } 170 171 public void testGetExampleShortNumberForCost() { 172 assertEquals("3010", shortInfo.getExampleShortNumberForCost(RegionCode.FR, 173 ShortNumberInfo.ShortNumberCost.TOLL_FREE)); 174 assertEquals("1023", shortInfo.getExampleShortNumberForCost(RegionCode.FR, 175 ShortNumberInfo.ShortNumberCost.STANDARD_RATE)); 176 assertEquals("42000", shortInfo.getExampleShortNumberForCost(RegionCode.FR, 177 ShortNumberInfo.ShortNumberCost.PREMIUM_RATE)); 178 assertEquals("", shortInfo.getExampleShortNumberForCost(RegionCode.FR, 179 ShortNumberInfo.ShortNumberCost.UNKNOWN_COST)); 180 } 181 182 public void testConnectsToEmergencyNumber_US() { 183 assertTrue(shortInfo.connectsToEmergencyNumber("911", RegionCode.US)); 184 assertTrue(shortInfo.connectsToEmergencyNumber("112", RegionCode.US)); 185 assertFalse(shortInfo.connectsToEmergencyNumber("999", RegionCode.US)); 186 } 187 188 public void testConnectsToEmergencyNumberLongNumber_US() { 189 assertTrue(shortInfo.connectsToEmergencyNumber("9116666666", RegionCode.US)); 190 assertTrue(shortInfo.connectsToEmergencyNumber("1126666666", RegionCode.US)); 191 assertFalse(shortInfo.connectsToEmergencyNumber("9996666666", RegionCode.US)); 192 } 193 194 public void testConnectsToEmergencyNumberWithFormatting_US() { 195 assertTrue(shortInfo.connectsToEmergencyNumber("9-1-1", RegionCode.US)); 196 assertTrue(shortInfo.connectsToEmergencyNumber("1-1-2", RegionCode.US)); 197 assertFalse(shortInfo.connectsToEmergencyNumber("9-9-9", RegionCode.US)); 198 } 199 200 public void testConnectsToEmergencyNumberWithPlusSign_US() { 201 assertFalse(shortInfo.connectsToEmergencyNumber("+911", RegionCode.US)); 202 assertFalse(shortInfo.connectsToEmergencyNumber("\uFF0B911", RegionCode.US)); 203 assertFalse(shortInfo.connectsToEmergencyNumber(" +911", RegionCode.US)); 204 assertFalse(shortInfo.connectsToEmergencyNumber("+112", RegionCode.US)); 205 assertFalse(shortInfo.connectsToEmergencyNumber("+999", RegionCode.US)); 206 } 207 208 public void testConnectsToEmergencyNumber_BR() { 209 assertTrue(shortInfo.connectsToEmergencyNumber("911", RegionCode.BR)); 210 assertTrue(shortInfo.connectsToEmergencyNumber("190", RegionCode.BR)); 211 assertFalse(shortInfo.connectsToEmergencyNumber("999", RegionCode.BR)); 212 } 213 214 public void testConnectsToEmergencyNumberLongNumber_BR() { 215 // Brazilian emergency numbers don't work when additional digits are appended. 216 assertFalse(shortInfo.connectsToEmergencyNumber("9111", RegionCode.BR)); 217 assertFalse(shortInfo.connectsToEmergencyNumber("1900", RegionCode.BR)); 218 assertFalse(shortInfo.connectsToEmergencyNumber("9996", RegionCode.BR)); 219 } 220 221 public void testConnectsToEmergencyNumber_CL() { 222 assertTrue(shortInfo.connectsToEmergencyNumber("131", RegionCode.CL)); 223 assertTrue(shortInfo.connectsToEmergencyNumber("133", RegionCode.CL)); 224 } 225 226 public void testConnectsToEmergencyNumberLongNumber_CL() { 227 // Chilean emergency numbers don't work when additional digits are appended. 228 assertFalse(shortInfo.connectsToEmergencyNumber("1313", RegionCode.CL)); 229 assertFalse(shortInfo.connectsToEmergencyNumber("1330", RegionCode.CL)); 230 } 231 232 public void testConnectsToEmergencyNumber_AO() { 233 // Angola doesn't have any metadata for emergency numbers in the test metadata. 234 assertFalse(shortInfo.connectsToEmergencyNumber("911", RegionCode.AO)); 235 assertFalse(shortInfo.connectsToEmergencyNumber("222123456", RegionCode.AO)); 236 assertFalse(shortInfo.connectsToEmergencyNumber("923123456", RegionCode.AO)); 237 } 238 239 public void testConnectsToEmergencyNumber_ZW() { 240 // Zimbabwe doesn't have any metadata in the test metadata. 241 assertFalse(shortInfo.connectsToEmergencyNumber("911", RegionCode.ZW)); 242 assertFalse(shortInfo.connectsToEmergencyNumber("01312345", RegionCode.ZW)); 243 assertFalse(shortInfo.connectsToEmergencyNumber("0711234567", RegionCode.ZW)); 244 } 245 246 public void testIsEmergencyNumber_US() { 247 assertTrue(shortInfo.isEmergencyNumber("911", RegionCode.US)); 248 assertTrue(shortInfo.isEmergencyNumber("112", RegionCode.US)); 249 assertFalse(shortInfo.isEmergencyNumber("999", RegionCode.US)); 250 } 251 252 public void testIsEmergencyNumberLongNumber_US() { 253 assertFalse(shortInfo.isEmergencyNumber("9116666666", RegionCode.US)); 254 assertFalse(shortInfo.isEmergencyNumber("1126666666", RegionCode.US)); 255 assertFalse(shortInfo.isEmergencyNumber("9996666666", RegionCode.US)); 256 } 257 258 public void testIsEmergencyNumberWithFormatting_US() { 259 assertTrue(shortInfo.isEmergencyNumber("9-1-1", RegionCode.US)); 260 assertTrue(shortInfo.isEmergencyNumber("*911", RegionCode.US)); 261 assertTrue(shortInfo.isEmergencyNumber("1-1-2", RegionCode.US)); 262 assertTrue(shortInfo.isEmergencyNumber("*112", RegionCode.US)); 263 assertFalse(shortInfo.isEmergencyNumber("9-9-9", RegionCode.US)); 264 assertFalse(shortInfo.isEmergencyNumber("*999", RegionCode.US)); 265 } 266 267 public void testIsEmergencyNumberWithPlusSign_US() { 268 assertFalse(shortInfo.isEmergencyNumber("+911", RegionCode.US)); 269 assertFalse(shortInfo.isEmergencyNumber("\uFF0B911", RegionCode.US)); 270 assertFalse(shortInfo.isEmergencyNumber(" +911", RegionCode.US)); 271 assertFalse(shortInfo.isEmergencyNumber("+112", RegionCode.US)); 272 assertFalse(shortInfo.isEmergencyNumber("+999", RegionCode.US)); 273 } 274 275 public void testIsEmergencyNumber_BR() { 276 assertTrue(shortInfo.isEmergencyNumber("911", RegionCode.BR)); 277 assertTrue(shortInfo.isEmergencyNumber("190", RegionCode.BR)); 278 assertFalse(shortInfo.isEmergencyNumber("999", RegionCode.BR)); 279 } 280 281 public void testIsEmergencyNumberLongNumber_BR() { 282 assertFalse(shortInfo.isEmergencyNumber("9111", RegionCode.BR)); 283 assertFalse(shortInfo.isEmergencyNumber("1900", RegionCode.BR)); 284 assertFalse(shortInfo.isEmergencyNumber("9996", RegionCode.BR)); 285 } 286 287 public void testIsEmergencyNumber_AO() { 288 // Angola doesn't have any metadata for emergency numbers in the test metadata. 289 assertFalse(shortInfo.isEmergencyNumber("911", RegionCode.AO)); 290 assertFalse(shortInfo.isEmergencyNumber("222123456", RegionCode.AO)); 291 assertFalse(shortInfo.isEmergencyNumber("923123456", RegionCode.AO)); 292 } 293 294 public void testIsEmergencyNumber_ZW() { 295 // Zimbabwe doesn't have any metadata in the test metadata. 296 assertFalse(shortInfo.isEmergencyNumber("911", RegionCode.ZW)); 297 assertFalse(shortInfo.isEmergencyNumber("01312345", RegionCode.ZW)); 298 assertFalse(shortInfo.isEmergencyNumber("0711234567", RegionCode.ZW)); 299 } 300 301 public void testEmergencyNumberForSharedCountryCallingCode() { 302 // Test the emergency number 112, which is valid in both Australia and the Christmas Islands. 303 assertTrue(shortInfo.isEmergencyNumber("112", RegionCode.AU)); 304 assertTrue(shortInfo.isValidShortNumberForRegion("112", RegionCode.AU)); 305 assertEquals(ShortNumberInfo.ShortNumberCost.TOLL_FREE, 306 shortInfo.getExpectedCostForRegion("112", RegionCode.AU)); 307 assertTrue(shortInfo.isEmergencyNumber("112", RegionCode.CX)); 308 assertTrue(shortInfo.isValidShortNumberForRegion("112", RegionCode.CX)); 309 assertEquals(ShortNumberInfo.ShortNumberCost.TOLL_FREE, 310 shortInfo.getExpectedCostForRegion("112", RegionCode.CX)); 311 PhoneNumber sharedEmergencyNumber = 312 new PhoneNumber().setCountryCode(61).setNationalNumber(112L); 313 assertTrue(shortInfo.isValidShortNumber(sharedEmergencyNumber)); 314 assertEquals(ShortNumberInfo.ShortNumberCost.TOLL_FREE, 315 shortInfo.getExpectedCost(sharedEmergencyNumber)); 316 } 317 318 public void testOverlappingNANPANumber() { 319 // 211 is an emergency number in Barbados, while it is a toll-free information line in Canada 320 // and the USA. 321 assertTrue(shortInfo.isEmergencyNumber("211", RegionCode.BB)); 322 assertEquals(ShortNumberInfo.ShortNumberCost.TOLL_FREE, 323 shortInfo.getExpectedCostForRegion("211", RegionCode.BB)); 324 assertFalse(shortInfo.isEmergencyNumber("211", RegionCode.US)); 325 assertEquals(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST, 326 shortInfo.getExpectedCostForRegion("211", RegionCode.US)); 327 assertFalse(shortInfo.isEmergencyNumber("211", RegionCode.CA)); 328 assertEquals(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST, 329 shortInfo.getExpectedCostForRegion("211", RegionCode.CA)); 330 } 331 } 332