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