2019-05-30 20:18:32 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2019 Simon Redman <simon@ergotech.com>
|
2019-05-30 20:18:32 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2019-05-30 20:18:32 +01:00
|
|
|
*/
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
#include "smshelper.h"
|
2019-05-30 20:18:32 +01:00
|
|
|
|
|
|
|
#include <QtTest>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class tests the working of device class
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
class SmsHelperTest : public QObject
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
|
|
|
|
void testSimplePhoneMatch();
|
|
|
|
void testMessyPhoneMatch();
|
|
|
|
void testMissingCountryCode();
|
|
|
|
void testLeadingZeros();
|
|
|
|
void testLongDistancePhoneNumber();
|
|
|
|
void testShortCodePhoneNumberNonMatch();
|
|
|
|
void testShortCodeMatch();
|
|
|
|
void testShortCodeNonMatch();
|
|
|
|
void testAustralianShortCodeNumberNonMatch();
|
|
|
|
void testAustralianShortCodeMatch();
|
|
|
|
void testAustralianShortCodeNonMatch();
|
|
|
|
void testCzechRepublicShortCodeNumberNonMatch();
|
|
|
|
void testCzechRepublicShortCodeMatch();
|
|
|
|
void testCzechRepublicShortCodeNonMatch();
|
|
|
|
void testDifferentPhoneNumbers1();
|
|
|
|
void testDifferentPhoneNumbers2();
|
2019-05-31 01:48:14 +01:00
|
|
|
void testAllZeros();
|
2019-06-07 23:39:05 +01:00
|
|
|
void testEmptyInput();
|
2019-05-30 20:18:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Two phone numbers which are exactly the same should match
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testSimplePhoneMatch()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& phoneNumber = QLatin1String("+1 (222) 333-4444");
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(SmsHelper::isPhoneNumberMatch(phoneNumber, phoneNumber),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Failed to match a phone number with itself");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Two phone numbers which have been entered with different formatting should match
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testMessyPhoneMatch()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& phoneNumber = QLatin1String("12223334444");
|
|
|
|
const QString& messyPhoneNumber = QLatin1String("+1 (222) 333-4444");
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(SmsHelper::isPhoneNumberMatch(phoneNumber, messyPhoneNumber),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Failed to match same number with different formatting characters");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* I don't think most people in the US even know what a country code *is*, and I know lots of people
|
|
|
|
* who don't enter it into their contacts book here. Make sure that kind of match works.
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testMissingCountryCode()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& shortForm = QLatin1String("(222) 333-4444");
|
|
|
|
const QString& longForm = QLatin1String("+1 (222) 333-4444");
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(SmsHelper::isPhoneNumberMatch(shortForm, longForm),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Failed to match two same numbers with missing country code");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* I don't quite understand which cases this applies, but sometimes you see a message where the
|
|
|
|
* country code has been prefixed with a few zeros. Make sure that works too.
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testLeadingZeros()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& shortForm = QLatin1String("+1 (222) 333-4444");
|
|
|
|
const QString& zeroForm = QLatin1String("001 (222) 333-4444");
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(SmsHelper::isPhoneNumberMatch(shortForm, zeroForm),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Failed to match two same numbers with padded zeros");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* At least on my phone, it is possible to leave the area code and country code off but still have
|
|
|
|
* the phone call or text message go through. Some people's contacts books might be this way, so make
|
|
|
|
* sure we support matching that too
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testLongDistancePhoneNumber()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& shortForm = QLatin1String("333-4444");
|
|
|
|
const QString& longForm = QLatin1String("+1 (222) 333-4444");
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(SmsHelper::isPhoneNumberMatch(shortForm, longForm),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Failed to suffix match two phone numbers");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Phone operators define short codes for a variety of reasons. Even if they have the same suffix,
|
|
|
|
* they should not match a regular phone number
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testShortCodePhoneNumberNonMatch()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& shortCode = QLatin1String("44455");
|
|
|
|
const QString& normalNumber = QLatin1String("222-334-4455");
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(!SmsHelper::isPhoneNumberMatch(shortCode, normalNumber),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Short code matched with normal number");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Two of the same short code should be able to match
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testShortCodeMatch()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& shortCode = QLatin1String("44455");
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(SmsHelper::isPhoneNumberMatch(shortCode, shortCode),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Did not match two of the same short code");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Two different short codes should not match
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testShortCodeNonMatch()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& shortCode1 = QLatin1String("44455");
|
|
|
|
const QString& shortCode2 = QLatin1String("66677");
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(!SmsHelper::isPhoneNumberMatch(shortCode1, shortCode2),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Incorrectly matched two different short codes");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Even in the land down under, a short code phone number should not match a regular phone number
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testAustralianShortCodeNumberNonMatch()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& australianShortCode = QLatin1String("19333444");
|
|
|
|
// I'm not sure if this is even a valid Australian phone number, but whatever...
|
|
|
|
const QString& australianPhoneNumber = QLatin1String("+41 02 1233 3444");
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(!SmsHelper::isPhoneNumberMatch(australianShortCode, australianPhoneNumber),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Matched Australian short code with regular phone number");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Two of the same Australian short code numbers should be able to match
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testAustralianShortCodeMatch()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& australianShortCode = QLatin1String("12333444");
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(SmsHelper::isPhoneNumberMatch(australianShortCode, australianShortCode),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Failed to match Australian short code number");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Two different Australian short code numbers should be able to match
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testAustralianShortCodeNonMatch()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& australianShortCode1 = QLatin1String("12333444");
|
|
|
|
const QString& australianShortCode2 = QLatin1String("12555666");
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(!SmsHelper::isPhoneNumberMatch(australianShortCode1, australianShortCode2),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Matched two different Australian short code numbers");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Czech Republic short code phone number should not match a regular phone number
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testCzechRepublicShortCodeNumberNonMatch()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& czechRepShortCode = QLatin1String("9090930");
|
|
|
|
// I'm not sure if this is even a valid Czech Republic phone number, but whatever...
|
|
|
|
const QString& czechRepPhoneNumber = QLatin1String("+420 809 090 930");
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(!SmsHelper::isPhoneNumberMatch(czechRepShortCode, czechRepPhoneNumber),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Matched Czech Republic short code with regular phone number");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Two of the same Czech Republic short code numbers should be able to match
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testCzechRepublicShortCodeMatch()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& czechRepShortCode = QLatin1String("9090930");
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(SmsHelper::isPhoneNumberMatch(czechRepShortCode, czechRepShortCode),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Failed to match Czech Republic short code number");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Two different Czech Republic short code numbers should be able to match
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testCzechRepublicShortCodeNonMatch()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& czechRepShortCode1 = QLatin1String("9090930");
|
|
|
|
const QString& czechRepShortCode2 = QLatin1String("9080990");
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(!SmsHelper::isPhoneNumberMatch(czechRepShortCode1, czechRepShortCode2),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Matched two different Czech Republic short code numbers");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Two phone numbers which are different should not be reported as the same
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testDifferentPhoneNumbers1()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& phone1 = QLatin1String("+1 (222) 333-4444");
|
|
|
|
const QString& phone2 = QLatin1String("+1 (333) 222-4444");
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(!SmsHelper::isPhoneNumberMatch(phone1, phone2),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Incorrectly marked two different numbers as same");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Two phone numbers which are different should not be reported as the same
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testDifferentPhoneNumbers2()
|
2019-05-30 20:18:32 +01:00
|
|
|
{
|
|
|
|
const QString& phone1 = QLatin1String("+1 (222) 333-4444");
|
|
|
|
const QString& phone2 = QLatin1String("122-2333");
|
|
|
|
|
2019-06-02 15:52:54 +01:00
|
|
|
QVERIFY2(!SmsHelper::isPhoneNumberMatch(phone1, phone2),
|
2019-05-30 20:18:32 +01:00
|
|
|
"Incorrectly *prefix* matched two phone numbers");
|
|
|
|
}
|
|
|
|
|
2019-05-31 01:48:14 +01:00
|
|
|
/**
|
|
|
|
* Some places allow a message with all zeros to be a short code. We should allow that too.
|
|
|
|
*/
|
2019-06-02 15:52:54 +01:00
|
|
|
void SmsHelperTest::testAllZeros()
|
2019-05-31 01:48:14 +01:00
|
|
|
{
|
|
|
|
const QString& zeros = QLatin1String("00000");
|
2019-06-02 15:52:54 +01:00
|
|
|
const QString& canonicalized = SmsHelper::canonicalizePhoneNumber(zeros);
|
2019-05-31 01:48:14 +01:00
|
|
|
|
|
|
|
QCOMPARE(canonicalized.length(), zeros.length());
|
|
|
|
}
|
|
|
|
|
2019-06-07 23:39:05 +01:00
|
|
|
/**
|
|
|
|
* An empty string is not a valid phone number and should not match anything
|
|
|
|
*/
|
|
|
|
void SmsHelperTest::testEmptyInput()
|
|
|
|
{
|
|
|
|
const QString& empty = QLatin1String("");
|
|
|
|
const QString& shortCode = QLatin1String("44455");
|
|
|
|
const QString& realNumber = QLatin1String("12223334444");
|
|
|
|
|
|
|
|
QVERIFY2(!SmsHelper::isPhoneNumberMatch(empty, shortCode), "The empty string matched a shortcode phone number");
|
|
|
|
QVERIFY2(!SmsHelper::isPhoneNumberMatch(empty, realNumber), "The empty string matched a real phone number");
|
|
|
|
}
|
|
|
|
|
2019-08-01 21:32:41 +01:00
|
|
|
QTEST_GUILESS_MAIN(SmsHelperTest);
|
2019-06-02 15:52:54 +01:00
|
|
|
#include "testsmshelper.moc"
|