Skip to content

Commit 0bbc74b

Browse files
committed
injects the phonenumberutil and adds testcases around the parsing
1 parent 27b1bc9 commit 0bbc74b

File tree

4 files changed

+108
-4
lines changed

4 files changed

+108
-4
lines changed

vector/src/main/java/im/vector/app/core/di/SingletonModule.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import android.content.Context
2121
import android.content.Context.MODE_PRIVATE
2222
import android.content.SharedPreferences
2323
import android.content.res.Resources
24+
import com.google.i18n.phonenumbers.PhoneNumberUtil
2425
import dagger.Binds
2526
import dagger.Module
2627
import dagger.Provides
@@ -193,6 +194,9 @@ object VectorStaticModule {
193194
return analyticsConfig
194195
}
195196

197+
@Provides
198+
fun providesPhoneNumberUtil(): PhoneNumberUtil = PhoneNumberUtil.getInstance()
199+
196200
@Provides
197201
@Singleton
198202
fun providesBuildMeta() = BuildMeta()

vector/src/main/java/im/vector/app/features/onboarding/ftueauth/PhoneNumberParser.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import com.google.i18n.phonenumbers.NumberParseException
2020
import com.google.i18n.phonenumbers.PhoneNumberUtil
2121
import javax.inject.Inject
2222

23-
class PhoneNumberParser @Inject constructor() {
23+
class PhoneNumberParser @Inject constructor(
24+
private val phoneNumberUtil: PhoneNumberUtil
25+
) {
2426

2527
fun parseInternationalNumber(rawPhoneNumber: String): Result {
2628
return when {
@@ -30,9 +32,8 @@ class PhoneNumberParser @Inject constructor() {
3032
}
3133

3234
private fun parseNumber(rawPhoneNumber: String) = try {
33-
val instance = PhoneNumberUtil.getInstance()
34-
val phoneNumber = instance.parse(rawPhoneNumber, null)
35-
Result.Success(instance.getRegionCodeForCountryCode(phoneNumber.countryCode), rawPhoneNumber)
35+
val phoneNumber = phoneNumberUtil.parse(rawPhoneNumber, null)
36+
Result.Success(phoneNumberUtil.getRegionCodeForCountryCode(phoneNumber.countryCode), rawPhoneNumber)
3637
} catch (e: NumberParseException) {
3738
Result.ErrorInvalidNumber
3839
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2022 New Vector Ltd
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 im.vector.app.features.onboarding.ftueauth
18+
19+
import im.vector.app.test.fakes.FakePhoneNumberUtil
20+
import org.amshove.kluent.shouldBeEqualTo
21+
import org.junit.Test
22+
23+
private const val AN_INTERNATIONAL_PHONE_NUMBER = "+4411111111111"
24+
private const val A_NON_INTERNATIONAL_PHONE_NUMBER = "111111111111"
25+
private const val AN_INVALID_INTERNATIONAL_NUMBER = "+abc"
26+
private const val A_COUNTRY_CODE = "GB"
27+
private const val A_COUNTRY_CALLING_CODE = 44
28+
29+
class PhoneNumberParserTest {
30+
31+
private val fakePhoneNumberUtil = FakePhoneNumberUtil()
32+
private val phoneNumberParser = PhoneNumberParser(fakePhoneNumberUtil.instance)
33+
34+
@Test
35+
fun `given a calling code and country code are successfully read when parsing, then returns success with country code`() {
36+
fakePhoneNumberUtil.givenCountryCallingCodeFor(AN_INTERNATIONAL_PHONE_NUMBER, callingCode = A_COUNTRY_CALLING_CODE)
37+
fakePhoneNumberUtil.givenRegionCodeFor(A_COUNTRY_CALLING_CODE, countryCode = A_COUNTRY_CODE)
38+
39+
val result = phoneNumberParser.parseInternationalNumber(AN_INTERNATIONAL_PHONE_NUMBER)
40+
41+
result shouldBeEqualTo PhoneNumberParser.Result.Success(A_COUNTRY_CODE, AN_INTERNATIONAL_PHONE_NUMBER)
42+
}
43+
44+
@Test
45+
fun `given a non internation phone number, when parsing, then returns MissingInternationCode error`() {
46+
val result = phoneNumberParser.parseInternationalNumber(A_NON_INTERNATIONAL_PHONE_NUMBER)
47+
48+
result shouldBeEqualTo PhoneNumberParser.Result.ErrorMissingInternationalCode
49+
}
50+
51+
@Test
52+
fun `given an invalid phone number, when parsing, then returns ErrorInvalidNumber error`() {
53+
fakePhoneNumberUtil.givenFailsToParse(AN_INVALID_INTERNATIONAL_NUMBER)
54+
55+
val result = phoneNumberParser.parseInternationalNumber(AN_INVALID_INTERNATIONAL_NUMBER)
56+
57+
result shouldBeEqualTo PhoneNumberParser.Result.ErrorInvalidNumber
58+
}
59+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2022 New Vector Ltd
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 im.vector.app.test.fakes
18+
19+
import com.google.i18n.phonenumbers.NumberParseException
20+
import com.google.i18n.phonenumbers.PhoneNumberUtil
21+
import com.google.i18n.phonenumbers.Phonenumber
22+
import io.mockk.every
23+
import io.mockk.mockk
24+
25+
class FakePhoneNumberUtil {
26+
27+
val instance = mockk<PhoneNumberUtil>()
28+
29+
fun givenCountryCallingCodeFor(phoneNumber: String, callingCode: Int) {
30+
every { instance.parse(phoneNumber, null) } returns Phonenumber.PhoneNumber().setCountryCode(callingCode)
31+
}
32+
33+
fun givenRegionCodeFor(callingCode: Int, countryCode: String) {
34+
every { instance.getRegionCodeForCountryCode(callingCode) } returns countryCode
35+
}
36+
37+
fun givenFailsToParse(phoneNumber: String) {
38+
every { instance.parse(phoneNumber, null) } throws NumberParseException(NumberParseException.ErrorType.NOT_A_NUMBER, "")
39+
}
40+
}

0 commit comments

Comments
 (0)