From 5927138a29c09cf054ab85ac3dce4f96b91c37ec Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Tue, 19 Nov 2019 02:13:09 -0500 Subject: [PATCH] Fix phone number matching, add tests --- .../QKSMS/repository/PhoneNumberUtilsTest.kt | 72 +++++++++++++++++++ .../com/moez/QKSMS/util/PhoneNumberUtils.kt | 6 +- 2 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 data/src/androidTest/java/com/moez/QKSMS/repository/PhoneNumberUtilsTest.kt diff --git a/data/src/androidTest/java/com/moez/QKSMS/repository/PhoneNumberUtilsTest.kt b/data/src/androidTest/java/com/moez/QKSMS/repository/PhoneNumberUtilsTest.kt new file mode 100644 index 00000000..393bea4b --- /dev/null +++ b/data/src/androidTest/java/com/moez/QKSMS/repository/PhoneNumberUtilsTest.kt @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2019 Moez Bhatti + * + * This file is part of QKSMS. + * + * QKSMS is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * QKSMS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with QKSMS. If not, see . + */ +package com.moez.QKSMS.repository + +import androidx.test.InstrumentationRegistry +import androidx.test.runner.AndroidJUnit4 +import com.moez.QKSMS.util.PhoneNumberUtils +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import java.util.* + +@RunWith(AndroidJUnit4::class) +class PhoneNumberUtilsTest { + + private val context = InstrumentationRegistry.getInstrumentation().context + private val phoneNumberUtils = PhoneNumberUtils(context) + + @Before + fun setup() { + Locale.setDefault(Locale.US) + } + + @Test + fun compare_identicalNumbers_returnsTrue() { + assertTrue(phoneNumberUtils.compare("+1 123 456 7890", "+1 123 456 7890")) + } + + @Test + fun compare_IdenticalNsnsWithOneMissingCountryCode_returnsTrue() { + assertTrue(phoneNumberUtils.compare("+1 123 456 7890", "123 456 7890")) + } + + @Test + fun compare_IdenticalNsnsWithOnePoorlyFormattedCountryCode_returnsTrue() { + assertTrue(phoneNumberUtils.compare("+1 123 456 7890", "1 123 456 7890")) + } + + @Test + fun compare_IdenticalFullNationalAustralianNsnsWithOneMissingCountryCode_returnsTrue() { + assertTrue(phoneNumberUtils.compare("+61 4 1234 5678", "04 1234 5678")) + } + + @Test + fun compare_InvalidShortNsnMatch_returnsFalse() { + assertFalse(phoneNumberUtils.compare("+1 123 456 7890", "67890")) + } + + @Test + fun compare_unequalNumbers_returnsFalse() { + assertFalse(phoneNumberUtils.compare("123 456 7890", "234 567 8901")) + } + +} diff --git a/data/src/main/java/com/moez/QKSMS/util/PhoneNumberUtils.kt b/data/src/main/java/com/moez/QKSMS/util/PhoneNumberUtils.kt index 4f4ffa5e..9885fc7a 100644 --- a/data/src/main/java/com/moez/QKSMS/util/PhoneNumberUtils.kt +++ b/data/src/main/java/com/moez/QKSMS/util/PhoneNumberUtils.kt @@ -44,10 +44,8 @@ class PhoneNumberUtils @Inject constructor(context: Context) { } if (PhoneNumberUtils.compare(first, second)) { - val firstNumber = parse(first) ?: return false - val secondNumber = parse(second) ?: return false - val matchType = phoneNumberUtil.isNumberMatch(firstNumber, secondNumber) - if (matchType >= PhoneNumberUtil.MatchType.NSN_MATCH) { + val matchType = phoneNumberUtil.isNumberMatch(first, second) + if (matchType >= PhoneNumberUtil.MatchType.SHORT_NSN_MATCH) { return true } }