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