• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2012 The Libphonenumber Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Author: Patrick Mezard
16 
17 #include "phonenumbers/geocoding/phonenumber_offline_geocoder.h"
18 
19 #include <gtest/gtest.h>
20 #include <unicode/locid.h>
21 
22 #include "phonenumbers/geocoding/geocoding_test_data.h"
23 #include "phonenumbers/phonenumber.h"
24 #include "phonenumbers/phonenumber.pb.h"
25 
26 namespace i18n {
27 namespace phonenumbers {
28 
29 using icu::Locale;
30 
31 namespace {
32 
MakeNumber(int32 country_code,uint64 national_number)33 PhoneNumber MakeNumber(int32 country_code, uint64 national_number) {
34   PhoneNumber n;
35   n.set_country_code(country_code);
36   n.set_national_number(national_number);
37   return n;
38 }
39 
40 const Locale kEnglishLocale = Locale("en", "GB");
41 const Locale kFrenchLocale = Locale("fr", "FR");
42 const Locale kGermanLocale = Locale("de", "DE");
43 const Locale kItalianLocale = Locale("it", "IT");
44 const Locale kKoreanLocale = Locale("ko", "KR");
45 const Locale kSimplifiedChineseLocale = Locale("zh", "CN");
46 
47 }  // namespace
48 
49 class PhoneNumberOfflineGeocoderTest : public testing::Test {
50  protected:
PhoneNumberOfflineGeocoderTest()51   PhoneNumberOfflineGeocoderTest() :
52     KO_NUMBER1(MakeNumber(82, 22123456UL)),
53     KO_NUMBER2(MakeNumber(82, 322123456UL)),
54     KO_NUMBER3(MakeNumber(82, uint64{6421234567})),
55     KO_INVALID_NUMBER(MakeNumber(82, 1234UL)),
56     KO_MOBILE(MakeNumber(82, uint64{101234567})),
57     US_NUMBER1(MakeNumber(1, uint64{6502530000})),
58     US_NUMBER2(MakeNumber(1, uint64{6509600000})),
59     US_NUMBER3(MakeNumber(1, 2128120000UL)),
60     US_NUMBER4(MakeNumber(1, uint64{6174240000})),
61     US_INVALID_NUMBER(MakeNumber(1, 123456789UL)),
62     BS_NUMBER1(MakeNumber(1, 2423651234UL)),
63     AU_NUMBER(MakeNumber(61, 236618300UL)),
64     NUMBER_WITH_INVALID_COUNTRY_CODE(MakeNumber(999, 2423651234UL)),
65     INTERNATIONAL_TOLL_FREE(MakeNumber(800, 12345678UL)) {
66   }
67 
SetUp()68   virtual void SetUp() {
69     geocoder_.reset(
70         new PhoneNumberOfflineGeocoder(
71             get_test_country_calling_codes(),
72             get_test_country_calling_codes_size(),
73             get_test_country_languages,
74             get_test_prefix_language_code_pairs(),
75             get_test_prefix_language_code_pairs_size(),
76             get_test_prefix_descriptions));
77   }
78 
79  protected:
80   scoped_ptr<PhoneNumberOfflineGeocoder> geocoder_;
81 
82   const PhoneNumber KO_NUMBER1;
83   const PhoneNumber KO_NUMBER2;
84   const PhoneNumber KO_NUMBER3;
85   const PhoneNumber KO_INVALID_NUMBER;
86   const PhoneNumber KO_MOBILE;
87 
88   const PhoneNumber US_NUMBER1;
89   const PhoneNumber US_NUMBER2;
90   const PhoneNumber US_NUMBER3;
91   const PhoneNumber US_NUMBER4;
92   const PhoneNumber US_INVALID_NUMBER;
93 
94   const PhoneNumber BS_NUMBER1;
95   const PhoneNumber AU_NUMBER;
96   const PhoneNumber NUMBER_WITH_INVALID_COUNTRY_CODE;
97   const PhoneNumber INTERNATIONAL_TOLL_FREE;
98 };
99 
TEST_F(PhoneNumberOfflineGeocoderTest,TestGetDescriptionForNumberWithNoDataFile)100 TEST_F(PhoneNumberOfflineGeocoderTest,
101        TestGetDescriptionForNumberWithNoDataFile) {
102   // No data file containing mappings for US numbers is available in Chinese for
103   // the unittests. As a result, the country name of United States in simplified
104   // Chinese is returned.
105 
106   // "\u7F8E\u56FD" (unicode escape sequences are not always supported)
107   EXPECT_EQ("\xe7""\xbe""\x8e""\xe5""\x9b""\xbd",
108             geocoder_->GetDescriptionForNumber(US_NUMBER1,
109                                                kSimplifiedChineseLocale));
110   EXPECT_EQ("Bahamas",
111             geocoder_->GetDescriptionForNumber(BS_NUMBER1, Locale("en", "US")));
112   EXPECT_EQ("Australia",
113             geocoder_->GetDescriptionForNumber(AU_NUMBER, Locale("en", "US")));
114   EXPECT_EQ("",
115             geocoder_->GetDescriptionForNumber(NUMBER_WITH_INVALID_COUNTRY_CODE,
116                                                Locale("en", "US")));
117   EXPECT_EQ("",
118             geocoder_->GetDescriptionForNumber(INTERNATIONAL_TOLL_FREE,
119                                                Locale("en", "US")));
120 }
121 
TEST_F(PhoneNumberOfflineGeocoderTest,TestGetDescriptionForNumberWithMissingPrefix)122 TEST_F(PhoneNumberOfflineGeocoderTest,
123        TestGetDescriptionForNumberWithMissingPrefix) {
124   // Test that the name of the country is returned when the number passed in is
125   // valid but not covered by the geocoding data file.
126   EXPECT_EQ("United States",
127             geocoder_->GetDescriptionForNumber(US_NUMBER4, Locale("en", "US")));
128 }
129 
TEST_F(PhoneNumberOfflineGeocoderTest,TestGetDescriptionForNumber_en_US)130 TEST_F(PhoneNumberOfflineGeocoderTest, TestGetDescriptionForNumber_en_US) {
131   EXPECT_EQ("CA",
132             geocoder_->GetDescriptionForNumber(US_NUMBER1, Locale("en", "US")));
133   EXPECT_EQ("Mountain View, CA",
134             geocoder_->GetDescriptionForNumber(US_NUMBER2, Locale("en", "US")));
135   EXPECT_EQ("New York, NY",
136             geocoder_->GetDescriptionForNumber(US_NUMBER3, Locale("en", "US")));
137 }
138 
TEST_F(PhoneNumberOfflineGeocoderTest,TestGetDescriptionForKoreanNumber)139 TEST_F(PhoneNumberOfflineGeocoderTest, TestGetDescriptionForKoreanNumber) {
140   EXPECT_EQ("Seoul",
141             geocoder_->GetDescriptionForNumber(KO_NUMBER1, kEnglishLocale));
142   EXPECT_EQ("Incheon",
143             geocoder_->GetDescriptionForNumber(KO_NUMBER2, kEnglishLocale));
144   EXPECT_EQ("Jeju",
145             geocoder_->GetDescriptionForNumber(KO_NUMBER3, kEnglishLocale));
146   // "\uC11C\uC6B8"
147   EXPECT_EQ("\xec""\x84""\x9c""\xec""\x9a""\xb8",
148             geocoder_->GetDescriptionForNumber(KO_NUMBER1, kKoreanLocale));
149   // "\uC778\uCC9C"
150   EXPECT_EQ("\xec""\x9d""\xb8""\xec""\xb2""\x9c",
151             geocoder_->GetDescriptionForNumber(KO_NUMBER2, kKoreanLocale));
152 }
153 
TEST_F(PhoneNumberOfflineGeocoderTest,TestGetDescriptionForFallBack)154 TEST_F(PhoneNumberOfflineGeocoderTest, TestGetDescriptionForFallBack) {
155   // No fallback, as the location name for the given phone number is available
156   // in the requested language.
157   EXPECT_EQ("Kalifornien",
158             geocoder_->GetDescriptionForNumber(US_NUMBER1, kGermanLocale));
159   // German falls back to English.
160   EXPECT_EQ("New York, NY",
161             geocoder_->GetDescriptionForNumber(US_NUMBER3, kGermanLocale));
162   // Italian falls back to English.
163   EXPECT_EQ("CA",
164             geocoder_->GetDescriptionForNumber(US_NUMBER1, kItalianLocale));
165   // Korean doesn't fall back to English.
166   // "\uB300\uD55C\uBBFC\uAD6D"
167   EXPECT_EQ("\xeb""\x8c""\x80""\xed""\x95""\x9c""\xeb""\xaf""\xbc""\xea""\xb5"
168             "\xad",
169             geocoder_->GetDescriptionForNumber(KO_NUMBER3, kKoreanLocale));
170 }
171 
TEST_F(PhoneNumberOfflineGeocoderTest,TestGetDescriptionForNumberWithUserRegion)172 TEST_F(PhoneNumberOfflineGeocoderTest,
173        TestGetDescriptionForNumberWithUserRegion) {
174   // User in Italy, American number. We should just show United States, in
175   // Spanish, and not more detailed information.
176   EXPECT_EQ("Estados Unidos",
177             geocoder_->GetDescriptionForNumber(US_NUMBER1, Locale("es", "ES"),
178                                                "IT"));
179   // Unknown region - should just show country name.
180   EXPECT_EQ("Estados Unidos",
181             geocoder_->GetDescriptionForNumber(US_NUMBER1, Locale("es", "ES"),
182                                                "ZZ"));
183   // User in the States, language German, should show detailed data.
184   EXPECT_EQ("Kalifornien",
185             geocoder_->GetDescriptionForNumber(US_NUMBER1, kGermanLocale,
186                                                "US"));
187   // User in the States, language French, no data for French, so we fallback to
188   // English detailed data.
189   EXPECT_EQ("CA",
190             geocoder_->GetDescriptionForNumber(US_NUMBER1, kFrenchLocale,
191                                                "US"));
192   // Invalid number - return an empty string.
193   EXPECT_EQ("",
194             geocoder_->GetDescriptionForNumber(US_INVALID_NUMBER,
195                                                kEnglishLocale,
196                                                "US"));
197 }
198 
TEST_F(PhoneNumberOfflineGeocoderTest,TestGetDescriptionForInvalidNumber)199 TEST_F(PhoneNumberOfflineGeocoderTest, TestGetDescriptionForInvalidNumber) {
200   EXPECT_EQ("", geocoder_->GetDescriptionForNumber(KO_INVALID_NUMBER,
201                                                    kEnglishLocale));
202   EXPECT_EQ("", geocoder_->GetDescriptionForNumber(US_INVALID_NUMBER,
203                                                    kEnglishLocale));
204 }
205 
TEST_F(PhoneNumberOfflineGeocoderTest,TestGetDescriptionForNonGeographicalNumberWithGeocodingPrefix)206 TEST_F(PhoneNumberOfflineGeocoderTest,
207        TestGetDescriptionForNonGeographicalNumberWithGeocodingPrefix) {
208   // We have a geocoding prefix, but we shouldn't use it since this is not
209   // geographical.
210   EXPECT_EQ("South Korea",
211             geocoder_->GetDescriptionForNumber(KO_MOBILE, kEnglishLocale));
212 }
213 }  // namespace phonenumbers
214 }  // namespace i18n
215