1 /*
2  * Copyright (C) 2013 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 package com.google.i18n.phonenumbers;
17 
18 import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
19 import junit.framework.TestCase;
20 
21 import java.util.Locale;
22 
23 /**
24  * Unit tests for PhoneNumberToCarrierMapper.java
25  *
26  * @author Cecilia Roes
27  */
28 public class PhoneNumberToCarrierMapperTest extends TestCase {
29   private final PhoneNumberToCarrierMapper carrierMapper =
30       new PhoneNumberToCarrierMapper(TEST_MAPPING_DATA_DIRECTORY);
31   private static final String TEST_MAPPING_DATA_DIRECTORY =
32       "/com/google/i18n/phonenumbers/carrier/testing_data/";
33 
34   // Set up some test numbers to re-use.
35   private static final PhoneNumber AO_MOBILE1 =
36       new PhoneNumber().setCountryCode(244).setNationalNumber(917654321L);
37   private static final PhoneNumber AO_MOBILE2 =
38       new PhoneNumber().setCountryCode(244).setNationalNumber(927654321L);
39   private static final PhoneNumber AO_FIXED1 =
40       new PhoneNumber().setCountryCode(244).setNationalNumber(22254321L);
41   private static final PhoneNumber AO_FIXED2 =
42       new PhoneNumber().setCountryCode(244).setNationalNumber(26254321L);
43   private static final PhoneNumber AO_INVALID_NUMBER =
44       new PhoneNumber().setCountryCode(244).setNationalNumber(101234L);
45   private static final PhoneNumber UK_MOBILE1 =
46       new PhoneNumber().setCountryCode(44).setNationalNumber(7387654321L);
47   private static final PhoneNumber UK_MOBILE2 =
48       new PhoneNumber().setCountryCode(44).setNationalNumber(7487654321L);
49   private static final PhoneNumber UK_FIXED1 =
50       new PhoneNumber().setCountryCode(44).setNationalNumber(1123456789L);
51   private static final PhoneNumber UK_FIXED2 =
52       new PhoneNumber().setCountryCode(44).setNationalNumber(2987654321L);
53   private static final PhoneNumber UK_INVALID_NUMBER =
54       new PhoneNumber().setCountryCode(44).setNationalNumber(7301234L);
55   private static final PhoneNumber UK_PAGER =
56       new PhoneNumber().setCountryCode(44).setNationalNumber(7601234567L);
57   private static final PhoneNumber US_FIXED_OR_MOBILE =
58       new PhoneNumber().setCountryCode(1).setNationalNumber(6502123456L);
59   private static final PhoneNumber NUMBER_WITH_INVALID_COUNTRY_CODE =
60       new PhoneNumber().setCountryCode(999).setNationalNumber(2423651234L);
61   private static final PhoneNumber INTERNATIONAL_TOLL_FREE =
62       new PhoneNumber().setCountryCode(800).setNationalNumber(12345678L);
63 
testGetNameForMobilePortableRegion()64   public void testGetNameForMobilePortableRegion() {
65     assertEquals("British carrier",
66                  carrierMapper.getNameForNumber(UK_MOBILE1, Locale.ENGLISH));
67     assertEquals("Brittisk operat\u00F6r",
68                  carrierMapper.getNameForNumber(UK_MOBILE1, new Locale("sv", "SE")));
69     assertEquals("British carrier",
70                  carrierMapper.getNameForNumber(UK_MOBILE1, Locale.FRENCH));
71     // Returns an empty string because the UK implements mobile number portability.
72     assertEquals("", carrierMapper.getSafeDisplayName(UK_MOBILE1, Locale.ENGLISH));
73   }
74 
testGetNameForNonMobilePortableRegion()75   public void testGetNameForNonMobilePortableRegion() {
76     assertEquals("Angolan carrier",
77                  carrierMapper.getNameForNumber(AO_MOBILE1, Locale.ENGLISH));
78     assertEquals("Angolan carrier",
79                  carrierMapper.getSafeDisplayName(AO_MOBILE1, Locale.ENGLISH));
80   }
81 
testGetNameForFixedLineNumber()82   public void testGetNameForFixedLineNumber() {
83     assertEquals("", carrierMapper.getNameForNumber(AO_FIXED1, Locale.ENGLISH));
84     assertEquals("", carrierMapper.getNameForNumber(UK_FIXED1, Locale.ENGLISH));
85     // If the carrier information is present in the files and the method that assumes a valid
86     // number is used, a carrier is returned.
87     assertEquals("Angolan fixed line carrier",
88                  carrierMapper.getNameForValidNumber(AO_FIXED2, Locale.ENGLISH));
89     assertEquals("", carrierMapper.getNameForValidNumber(UK_FIXED2, Locale.ENGLISH));
90   }
91 
testGetNameForFixedOrMobileNumber()92   public void testGetNameForFixedOrMobileNumber() {
93     assertEquals("US carrier", carrierMapper.getNameForNumber(US_FIXED_OR_MOBILE,
94                                                                      Locale.ENGLISH));
95   }
96 
testGetNameForPagerNumber()97   public void testGetNameForPagerNumber() {
98     assertEquals("British pager", carrierMapper.getNameForNumber(UK_PAGER, Locale.ENGLISH));
99   }
100 
testGetNameForNumberWithNoDataFile()101   public void testGetNameForNumberWithNoDataFile() {
102     assertEquals("", carrierMapper.getNameForNumber(NUMBER_WITH_INVALID_COUNTRY_CODE,
103                                                            Locale.ENGLISH));
104     assertEquals("", carrierMapper.getNameForNumber(INTERNATIONAL_TOLL_FREE,
105                                                            Locale.ENGLISH));
106     assertEquals("", carrierMapper.getNameForValidNumber(NUMBER_WITH_INVALID_COUNTRY_CODE,
107                                                                 Locale.ENGLISH));
108     assertEquals("", carrierMapper.getNameForValidNumber(INTERNATIONAL_TOLL_FREE,
109                                                                 Locale.ENGLISH));
110   }
111 
testGetNameForNumberWithMissingPrefix()112   public void testGetNameForNumberWithMissingPrefix() {
113     assertEquals("", carrierMapper.getNameForNumber(UK_MOBILE2, Locale.ENGLISH));
114     assertEquals("", carrierMapper.getNameForNumber(AO_MOBILE2, Locale.ENGLISH));
115   }
116 
testGetNameForInvalidNumber()117   public void testGetNameForInvalidNumber() {
118     assertEquals("", carrierMapper.getNameForNumber(UK_INVALID_NUMBER, Locale.ENGLISH));
119     assertEquals("", carrierMapper.getNameForNumber(AO_INVALID_NUMBER, Locale.ENGLISH));
120   }
121 }
122