1 /* 2 * Copyright (C) 2011 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.google.i18n.phonenumbers.geocoding; 18 19 import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; 20 import junit.framework.TestCase; 21 22 import java.util.Locale; 23 24 /** 25 * Unit tests for PhoneNumberOfflineGeocoder.java 26 * 27 * @author Shaopeng Jia 28 */ 29 public class PhoneNumberOfflineGeocoderTest extends TestCase { 30 private final PhoneNumberOfflineGeocoder geocoder = 31 new PhoneNumberOfflineGeocoder(TEST_MAPPING_DATA_DIRECTORY); 32 private static final String TEST_MAPPING_DATA_DIRECTORY = 33 "/com/google/i18n/phonenumbers/geocoding/testing_data/"; 34 35 // Set up some test numbers to re-use. 36 private static final PhoneNumber KO_NUMBER1 = 37 new PhoneNumber().setCountryCode(82).setNationalNumber(22123456L); 38 private static final PhoneNumber KO_NUMBER2 = 39 new PhoneNumber().setCountryCode(82).setNationalNumber(322123456L); 40 private static final PhoneNumber KO_NUMBER3 = 41 new PhoneNumber().setCountryCode(82).setNationalNumber(6421234567L); 42 private static final PhoneNumber KO_INVALID_NUMBER = 43 new PhoneNumber().setCountryCode(82).setNationalNumber(1234L); 44 private static final PhoneNumber US_NUMBER1 = 45 new PhoneNumber().setCountryCode(1).setNationalNumber(6502530000L); 46 private static final PhoneNumber US_NUMBER2 = 47 new PhoneNumber().setCountryCode(1).setNationalNumber(6509600000L); 48 private static final PhoneNumber US_NUMBER3 = 49 new PhoneNumber().setCountryCode(1).setNationalNumber(2128120000L); 50 private static final PhoneNumber US_NUMBER4 = 51 new PhoneNumber().setCountryCode(1).setNationalNumber(6174240000L); 52 private static final PhoneNumber US_INVALID_NUMBER = 53 new PhoneNumber().setCountryCode(1).setNationalNumber(123456789L); 54 private static final PhoneNumber NANPA_TOLL_FREE = 55 new PhoneNumber().setCountryCode(1).setNationalNumber(8002431234L); 56 private static final PhoneNumber BS_NUMBER1 = 57 new PhoneNumber().setCountryCode(1).setNationalNumber(2423651234L); 58 private static final PhoneNumber AU_NUMBER = 59 new PhoneNumber().setCountryCode(61).setNationalNumber(236618300L); 60 private static final PhoneNumber AR_MOBILE_NUMBER = 61 new PhoneNumber().setCountryCode(54).setNationalNumber(92214000000L); 62 private static final PhoneNumber NUMBER_WITH_INVALID_COUNTRY_CODE = 63 new PhoneNumber().setCountryCode(999).setNationalNumber(2423651234L); 64 private static final PhoneNumber INTERNATIONAL_TOLL_FREE = 65 new PhoneNumber().setCountryCode(800).setNationalNumber(12345678L); 66 testGetDescriptionForNumberWithNoDataFile()67 public void testGetDescriptionForNumberWithNoDataFile() { 68 // No data file containing mappings for US numbers is available in Chinese for the unittests. As 69 // a result, the country name of United States in simplified Chinese is returned. 70 assertEquals("\u7F8E\u56FD", 71 geocoder.getDescriptionForNumber(US_NUMBER1, Locale.SIMPLIFIED_CHINESE)); 72 assertEquals("Bahamas", 73 geocoder.getDescriptionForNumber(BS_NUMBER1, new Locale("en", "US"))); 74 assertEquals("Australia", 75 geocoder.getDescriptionForNumber(AU_NUMBER, new Locale("en", "US"))); 76 assertEquals("", geocoder.getDescriptionForNumber(NUMBER_WITH_INVALID_COUNTRY_CODE, 77 new Locale("en", "US"))); 78 assertEquals("", geocoder.getDescriptionForNumber(INTERNATIONAL_TOLL_FREE, 79 new Locale("en", "US"))); 80 } 81 testGetDescriptionForNumberWithMissingPrefix()82 public void testGetDescriptionForNumberWithMissingPrefix() { 83 // Test that the name of the country is returned when the number passed in is valid but not 84 // covered by the geocoding data file. 85 assertEquals("United States", 86 geocoder.getDescriptionForNumber(US_NUMBER4, new Locale("en", "US"))); 87 } 88 testGetDescriptionForNumberBelongingToMultipleCountriesIsEmpty()89 public void testGetDescriptionForNumberBelongingToMultipleCountriesIsEmpty() { 90 // Test that nothing is returned when the number passed in is valid but not 91 // covered by the geocoding data file and belongs to multiple countries 92 assertEquals("", 93 geocoder.getDescriptionForNumber(NANPA_TOLL_FREE, new Locale("en", "US"))); 94 } 95 testGetDescriptionForNumber_en_US()96 public void testGetDescriptionForNumber_en_US() { 97 assertEquals("CA", 98 geocoder.getDescriptionForNumber(US_NUMBER1, new Locale("en", "US"))); 99 assertEquals("Mountain View, CA", 100 geocoder.getDescriptionForNumber(US_NUMBER2, new Locale("en", "US"))); 101 assertEquals("New York, NY", 102 geocoder.getDescriptionForNumber(US_NUMBER3, new Locale("en", "US"))); 103 } 104 testGetDescriptionForKoreanNumber()105 public void testGetDescriptionForKoreanNumber() { 106 assertEquals("Seoul", 107 geocoder.getDescriptionForNumber(KO_NUMBER1, Locale.ENGLISH)); 108 assertEquals("Incheon", 109 geocoder.getDescriptionForNumber(KO_NUMBER2, Locale.ENGLISH)); 110 assertEquals("Jeju", 111 geocoder.getDescriptionForNumber(KO_NUMBER3, Locale.ENGLISH)); 112 assertEquals("\uC11C\uC6B8", 113 geocoder.getDescriptionForNumber(KO_NUMBER1, Locale.KOREAN)); 114 assertEquals("\uC778\uCC9C", 115 geocoder.getDescriptionForNumber(KO_NUMBER2, Locale.KOREAN)); 116 } 117 testGetDescriptionForArgentinianMobileNumber()118 public void testGetDescriptionForArgentinianMobileNumber() { 119 assertEquals("La Plata", 120 geocoder.getDescriptionForNumber(AR_MOBILE_NUMBER, Locale.ENGLISH)); 121 } 122 testGetDescriptionForFallBack()123 public void testGetDescriptionForFallBack() { 124 // No fallback, as the location name for the given phone number is available in the requested 125 // language. 126 assertEquals("Kalifornien", 127 geocoder.getDescriptionForNumber(US_NUMBER1, Locale.GERMAN)); 128 // German falls back to English. 129 assertEquals("New York, NY", 130 geocoder.getDescriptionForNumber(US_NUMBER3, Locale.GERMAN)); 131 // Italian falls back to English. 132 assertEquals("CA", 133 geocoder.getDescriptionForNumber(US_NUMBER1, Locale.ITALIAN)); 134 // Korean doesn't fall back to English. 135 assertEquals("\uB300\uD55C\uBBFC\uAD6D", 136 geocoder.getDescriptionForNumber(KO_NUMBER3, Locale.KOREAN)); 137 } 138 testGetDescriptionForNumberWithUserRegion()139 public void testGetDescriptionForNumberWithUserRegion() { 140 // User in Italy, American number. We should just show United States, in Spanish, and not more 141 // detailed information. 142 assertEquals("Estados Unidos", 143 geocoder.getDescriptionForNumber(US_NUMBER1, new Locale("es", "ES"), "IT")); 144 // Unknown region - should just show country name. 145 assertEquals("Estados Unidos", 146 geocoder.getDescriptionForNumber(US_NUMBER1, new Locale("es", "ES"), "ZZ")); 147 // User in the States, language German, should show detailed data. 148 assertEquals("Kalifornien", 149 geocoder.getDescriptionForNumber(US_NUMBER1, Locale.GERMAN, "US")); 150 // User in the States, language French, no data for French, so we fallback to English detailed 151 // data. 152 assertEquals("CA", 153 geocoder.getDescriptionForNumber(US_NUMBER1, Locale.FRENCH, "US")); 154 // Invalid number - return an empty string. 155 assertEquals("", geocoder.getDescriptionForNumber(US_INVALID_NUMBER, Locale.ENGLISH, 156 "US")); 157 } 158 testGetDescriptionForInvalidNumber()159 public void testGetDescriptionForInvalidNumber() { 160 assertEquals("", geocoder.getDescriptionForNumber(KO_INVALID_NUMBER, Locale.ENGLISH)); 161 assertEquals("", geocoder.getDescriptionForNumber(US_INVALID_NUMBER, Locale.ENGLISH)); 162 } 163 } 164