18
18
19
19
package com .firebase .ui .auth .ui .phone ;
20
20
21
+ import android .content .Context ;
22
+ import android .telephony .TelephonyManager ;
23
+
21
24
import com .firebase .ui .auth .BuildConfig ;
22
25
import com .firebase .ui .auth .testhelpers .CustomRobolectricGradleTestRunner ;
23
26
24
27
import org .junit .Test ;
25
28
import org .junit .runner .RunWith ;
29
+ import org .robolectric .Robolectric ;
30
+ import org .robolectric .RuntimeEnvironment ;
26
31
import org .robolectric .annotation .Config ;
27
32
28
33
import java .util .Locale ;
29
34
35
+ import static com .firebase .ui .auth .ui .phone .PhoneNumberUtils .*;
30
36
import static com .firebase .ui .auth .ui .phone .PhoneTestConstants .RAW_PHONE ;
31
37
import static org .junit .Assert .assertEquals ;
38
+ import static org .junit .Assert .assertNull ;
39
+ import static org .mockito .Mockito .mock ;
40
+ import static org .mockito .Mockito .when ;
41
+ import static org .robolectric .Shadows .shadowOf ;
32
42
33
43
@ RunWith (CustomRobolectricGradleTestRunner .class )
34
44
@ Config (constants = BuildConfig .class , sdk = 21 )
35
45
public class PhoneNumberUtilsTest {
36
- private static final String INVENTED_ISO = "random" ;
37
-
38
46
@ Test
39
47
public void testGetPhoneNumber () throws Exception {
40
- final PhoneNumber number = PhoneNumberUtils . getPhoneNumber (RAW_PHONE );
48
+ final PhoneNumber number = getPhoneNumber (RAW_PHONE );
41
49
assertEquals (PhoneTestConstants .PHONE_NO_COUNTRY_CODE , number .getPhoneNumber ());
42
50
assertEquals (PhoneTestConstants .US_COUNTRY_CODE , number .getCountryCode ());
43
51
assertEquals (PhoneTestConstants .US_ISO2 , number .getCountryIso ());
44
52
}
45
53
46
54
@ Test
47
55
public void testGetPhoneNumber_withLongestCountryCode () throws Exception {
48
- final PhoneNumber phoneNumber = PhoneNumberUtils . getPhoneNumber (PhoneTestConstants
56
+ final PhoneNumber phoneNumber = getPhoneNumber (PhoneTestConstants
49
57
.YE_RAW_PHONE );
50
58
assertEquals (PhoneTestConstants .PHONE_NO_COUNTRY_CODE , phoneNumber .getPhoneNumber ());
51
59
assertEquals (PhoneTestConstants .YE_COUNTRY_CODE , phoneNumber .getCountryCode ());
@@ -54,15 +62,15 @@ public void testGetPhoneNumber_withLongestCountryCode() throws Exception {
54
62
55
63
@ Test
56
64
public void testGetPhoneNumber_withPhoneWithoutPlusSign () throws Exception {
57
- final PhoneNumber phoneNumber = PhoneNumberUtils . getPhoneNumber (PhoneTestConstants .PHONE );
65
+ final PhoneNumber phoneNumber = getPhoneNumber (PhoneTestConstants .PHONE );
58
66
assertEquals (PhoneTestConstants .PHONE , phoneNumber .getPhoneNumber ());
59
67
assertEquals (PhoneTestConstants .US_COUNTRY_CODE , phoneNumber .getCountryCode ());
60
68
assertEquals (PhoneTestConstants .US_ISO2 , phoneNumber .getCountryIso ());
61
69
}
62
70
63
71
@ Test
64
72
public void testGetPhoneNumber_noCountryCode () throws Exception {
65
- final PhoneNumber number = PhoneNumberUtils . getPhoneNumber ("0" + PhoneTestConstants
73
+ final PhoneNumber number = getPhoneNumber ("0" + PhoneTestConstants
66
74
.PHONE_NO_COUNTRY_CODE );
67
75
assertEquals ("0" + PhoneTestConstants .PHONE_NO_COUNTRY_CODE , number .getPhoneNumber ());
68
76
assertEquals (PhoneTestConstants .US_COUNTRY_CODE , number .getCountryCode ());
@@ -71,8 +79,94 @@ public void testGetPhoneNumber_noCountryCode() throws Exception {
71
79
72
80
@ Test
73
81
public void testGetCountryCode () throws Exception {
74
- assertEquals (86 , PhoneNumberUtils .getCountryCode (Locale .CHINA .getCountry ()));
75
- assertEquals (1 , PhoneNumberUtils .getCountryCode (null ));
76
- assertEquals (1 , PhoneNumberUtils .getCountryCode (new Locale ("" , "DJJZ" ).getCountry ()));
82
+ assertEquals (new Integer (86 ), getCountryCode (Locale .CHINA .getCountry ()));
83
+ assertEquals (null , getCountryCode (null ));
84
+ assertEquals (null , getCountryCode (new Locale ("" , "DJJZ" ).getCountry ()));
85
+ }
86
+
87
+ @ Test
88
+ @ Config (constants = BuildConfig .class , sdk = 21 )
89
+ public void testFormatNumberToE164_aboveApi21 () {
90
+ String validPhoneNumber = "+919994947354" ;
91
+ CountryInfo indiaCountryInfo = new CountryInfo (new Locale ("" , "IN" ), 91 );
92
+ //no leading plus
93
+ assertEquals (validPhoneNumber , formatPhoneNumber ("9994947354" , indiaCountryInfo ));
94
+ //no country code
95
+ assertEquals (validPhoneNumber , formatPhoneNumber ("919994947354" , indiaCountryInfo ));
96
+ //fully formatted
97
+ assertEquals (validPhoneNumber , formatPhoneNumber ("+919994947354" , indiaCountryInfo ));
98
+ //with hyphens
99
+ assertEquals (validPhoneNumber , formatPhoneNumber ("+91-(999)-(49)-(47354)" , indiaCountryInfo ));
100
+ //with spaces leading plus
101
+ assertEquals (validPhoneNumber , formatPhoneNumber ("+91 99949 47354" , indiaCountryInfo ));
102
+ // space formatting
103
+ assertEquals (validPhoneNumber , formatPhoneNumber ("91 99949 47354" , indiaCountryInfo ));
104
+ // parantheses and hyphens
105
+ assertEquals (validPhoneNumber , formatPhoneNumber ("(99949) 47-354" , indiaCountryInfo ));
106
+ // mismatched country
107
+ assertEquals (validPhoneNumber , formatPhoneNumber ("+919994947354" ,
108
+ new CountryInfo (
109
+ new Locale ("" , "US" ), 1 )));
110
+ // incorrect country with well formatted number
111
+ assertNull (formatPhoneNumber ("999474735" , indiaCountryInfo ));
112
+
113
+ // incorrect country with unformattednumber
114
+ assertNull (validPhoneNumber , formatPhoneNumber ("919994947354" ,
115
+ new CountryInfo (
116
+ new Locale ("" , "US" ), 1 )));
117
+ //incorrect country, incorrect phone number
118
+ assertNull (formatPhoneNumber ("+914349873457" , new CountryInfo (
119
+ new Locale ("" , "US" ), 1 )));
120
+ }
121
+
122
+ @ Test
123
+ @ Config (constants = BuildConfig .class , sdk = 16 )
124
+ public void testFormatNumberToE164_belowApi21 () {
125
+ String validPhoneNumber = "+919994947354" ;
126
+ CountryInfo indiaCountryInfo = new CountryInfo (new Locale ("" , "IN" ), 91 );
127
+ // no leading plus
128
+ assertEquals (validPhoneNumber , formatPhoneNumber ("9994947354" , indiaCountryInfo ));
129
+ // fully formatted
130
+ assertEquals (validPhoneNumber , formatPhoneNumber ("+919994947354" , indiaCountryInfo ));
131
+ // parantheses and hyphens
132
+ assertEquals (validPhoneNumber , formatPhoneNumber ("(99949) 47-354" , indiaCountryInfo ));
133
+
134
+ // The following cases would fail for lower api versions.
135
+ // Leaving tests in place to formally identify cases
136
+
137
+ // no leading +
138
+ // assertEquals(validPhoneNumber, formatPhoneNumber("919994947354", indiaCountryInfo));
139
+
140
+ // with hyphens
141
+ // assertEquals(validPhoneNumber, formatPhoneNumber("+91-(999)-(49)-(47354)",
142
+ // indiaCountryInfo));
143
+
144
+ // with spaces leading plus
145
+ // assertEquals(validPhoneNumber, formatPhoneNumber("+91 99949 47354", indiaCountryInfo));
146
+
147
+ // space formatting
148
+ // assertEquals(validPhoneNumber, formatPhoneNumber("91 99949 47354", indiaCountryInfo));
149
+
150
+ // invalid phone number
151
+ // assertNull(formatPhoneNumber("999474735", indiaCountryInfo));
152
+ }
153
+
154
+ @ Test
155
+ public void testGetCurrentCountryInfo_fromSim () {
156
+ Context context = mock (Context .class );
157
+ TelephonyManager telephonyManager = mock (TelephonyManager .class );
158
+
159
+ when (context .getSystemService (Context .TELEPHONY_SERVICE )).thenReturn (telephonyManager );
160
+ when (telephonyManager .getSimCountryIso ()).thenReturn ("IN" );
161
+ assertEquals (new CountryInfo (new Locale ("" , "IN" ), 91 ), getCurrentCountryInfo (context ));
162
+ }
163
+
164
+ @ Test
165
+ public void testGetCurrentCountryInfo_noTelephonyReturnsDefaultLocale () {
166
+ Context context = mock (Context .class );
167
+ assertEquals (new CountryInfo (
168
+ Locale .getDefault (),
169
+ PhoneNumberUtils .getCountryCode (Locale .getDefault ().getCountry ())),
170
+ getCurrentCountryInfo (context ));
77
171
}
78
172
}
0 commit comments