Home | History | Annotate | Download | only in geocoding
      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.android.i18n.phonenumbers.geocoding;
     18 
     19 import com.android.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/android/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 BS_NUMBER1 =
     55       new PhoneNumber().setCountryCode(1).setNationalNumber(2423651234L);
     56   private static final PhoneNumber AU_NUMBER =
     57       new PhoneNumber().setCountryCode(61).setNationalNumber(236618300L);
     58   private static final PhoneNumber AR_MOBILE_NUMBER =
     59       new PhoneNumber().setCountryCode(54).setNationalNumber(92214000000L);
     60   private static final PhoneNumber NUMBER_WITH_INVALID_COUNTRY_CODE =
     61       new PhoneNumber().setCountryCode(999).setNationalNumber(2423651234L);
     62   private static final PhoneNumber INTERNATIONAL_TOLL_FREE =
     63       new PhoneNumber().setCountryCode(800).setNationalNumber(12345678L);
     64 
     65   public void testGetDescriptionForNumberWithNoDataFile() {
     66     // No data file containing mappings for US numbers is available in Chinese for the unittests. As
     67     // a result, the country name of United States in simplified Chinese is returned.
     68     assertEquals("\u7F8E\u56FD",
     69         geocoder.getDescriptionForNumber(US_NUMBER1, Locale.SIMPLIFIED_CHINESE));
     70     assertEquals("Bahamas",
     71         geocoder.getDescriptionForNumber(BS_NUMBER1, new Locale("en", "US")));
     72     assertEquals("Australia",
     73         geocoder.getDescriptionForNumber(AU_NUMBER, new Locale("en", "US")));
     74     assertEquals("", geocoder.getDescriptionForNumber(NUMBER_WITH_INVALID_COUNTRY_CODE,
     75                                                       new Locale("en", "US")));
     76     assertEquals("", geocoder.getDescriptionForNumber(INTERNATIONAL_TOLL_FREE,
     77                                                       new Locale("en", "US")));
     78   }
     79 
     80   public void testGetDescriptionForNumberWithMissingPrefix() {
     81     // Test that the name of the country is returned when the number passed in is valid but not
     82     // covered by the geocoding data file.
     83     assertEquals("United States",
     84         geocoder.getDescriptionForNumber(US_NUMBER4, new Locale("en", "US")));
     85   }
     86 
     87   public void testGetDescriptionForNumber_en_US() {
     88     assertEquals("CA",
     89         geocoder.getDescriptionForNumber(US_NUMBER1, new Locale("en", "US")));
     90     assertEquals("Mountain View, CA",
     91         geocoder.getDescriptionForNumber(US_NUMBER2, new Locale("en", "US")));
     92     assertEquals("New York, NY",
     93         geocoder.getDescriptionForNumber(US_NUMBER3, new Locale("en", "US")));
     94   }
     95 
     96   public void testGetDescriptionForKoreanNumber() {
     97     assertEquals("Seoul",
     98         geocoder.getDescriptionForNumber(KO_NUMBER1, Locale.ENGLISH));
     99     assertEquals("Incheon",
    100         geocoder.getDescriptionForNumber(KO_NUMBER2, Locale.ENGLISH));
    101     assertEquals("Jeju",
    102         geocoder.getDescriptionForNumber(KO_NUMBER3, Locale.ENGLISH));
    103     assertEquals("\uC11C\uC6B8",
    104         geocoder.getDescriptionForNumber(KO_NUMBER1, Locale.KOREAN));
    105     assertEquals("\uC778\uCC9C",
    106         geocoder.getDescriptionForNumber(KO_NUMBER2, Locale.KOREAN));
    107   }
    108 
    109   public void testGetDescriptionForArgentinianMobileNumber() {
    110     assertEquals("La Plata",
    111         geocoder.getDescriptionForNumber(AR_MOBILE_NUMBER, Locale.ENGLISH));
    112   }
    113 
    114   public void testGetDescriptionForFallBack() {
    115     // No fallback, as the location name for the given phone number is available in the requested
    116     // language.
    117     assertEquals("Kalifornien",
    118         geocoder.getDescriptionForNumber(US_NUMBER1, Locale.GERMAN));
    119     // German falls back to English.
    120     assertEquals("New York, NY",
    121         geocoder.getDescriptionForNumber(US_NUMBER3, Locale.GERMAN));
    122     // Italian falls back to English.
    123     assertEquals("CA",
    124         geocoder.getDescriptionForNumber(US_NUMBER1, Locale.ITALIAN));
    125     // Korean doesn't fall back to English.
    126     assertEquals("\uB300\uD55C\uBBFC\uAD6D",
    127         geocoder.getDescriptionForNumber(KO_NUMBER3, Locale.KOREAN));
    128   }
    129 
    130   public void testGetDescriptionForNumberWithUserRegion() {
    131     // User in Italy, American number. We should just show United States, in Spanish, and not more
    132     // detailed information.
    133     assertEquals("Estados Unidos",
    134         geocoder.getDescriptionForNumber(US_NUMBER1, new Locale("es", "ES"), "IT"));
    135     // Unknown region - should just show country name.
    136     assertEquals("Estados Unidos",
    137         geocoder.getDescriptionForNumber(US_NUMBER1, new Locale("es", "ES"), "ZZ"));
    138     // User in the States, language German, should show detailed data.
    139     assertEquals("Kalifornien",
    140         geocoder.getDescriptionForNumber(US_NUMBER1, Locale.GERMAN, "US"));
    141     // User in the States, language French, no data for French, so we fallback to English detailed
    142     // data.
    143     assertEquals("CA",
    144         geocoder.getDescriptionForNumber(US_NUMBER1, Locale.FRENCH, "US"));
    145     // Invalid number - return an empty string.
    146     assertEquals("", geocoder.getDescriptionForNumber(US_INVALID_NUMBER, Locale.ENGLISH,
    147                                                       "US"));
    148   }
    149 
    150   public void testGetDescriptionForInvalidNumber() {
    151     assertEquals("", geocoder.getDescriptionForNumber(KO_INVALID_NUMBER, Locale.ENGLISH));
    152     assertEquals("", geocoder.getDescriptionForNumber(US_INVALID_NUMBER, Locale.ENGLISH));
    153   }
    154 }
    155