Move SMS App helper functions to their own file

This commit is contained in:
Simon Redman 2019-06-02 14:52:54 +00:00 committed by Nicolas Fella
parent 96cd55920e
commit e8a2e81421
7 changed files with 243 additions and 183 deletions

View file

@ -2,42 +2,39 @@ qt5_add_resources(KCSMS_SRCS resources.qrc)
find_package(KF5People) find_package(KF5People)
add_library(kdeconnectsms add_library(kdeconnectsmshelper
conversationmodel.cpp smshelper.cpp
conversationlistmodel.cpp
) )
set_target_properties(kdeconnectsms PROPERTIES set_target_properties(kdeconnectsmshelper PROPERTIES
VERSION ${KDECONNECT_VERSION} VERSION ${KDECONNECT_VERSION}
SOVERSION ${KDECONNECT_VERSION_MAJOR} SOVERSION ${KDECONNECT_VERSION_MAJOR}
) )
generate_export_header(kdeconnectsms EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/kdeconnectsms_export.h BASE_NAME KDEConnectSmsAppLib) generate_export_header(kdeconnectsmshelper EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/kdeconnectsms_export.h BASE_NAME KDEConnectSmsAppLib)
target_include_directories(kdeconnectsms PUBLIC ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) target_include_directories(kdeconnectsmshelper PUBLIC ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
# If ever this library is actually used by someone else, we should export these headers target_link_libraries(kdeconnectsmshelper
target_link_libraries(kdeconnectsms LINK_PUBLIC
LINK_PRIVATE Qt5::Core
Qt5::Quick
Qt5::Widgets
KF5::People
kdeconnectinterfaces
) )
set(libkdeconnectsms_HEADERS # If ever this library is actually used by someone else, we should export these headers
conversationmodel.h set(libkdeconnectsmshelper_HEADERS
conversationlistmodel.h smshelper.h
${CMAKE_CURRENT_BINARY_DIR}/kdeconnectsms_export.h ${CMAKE_CURRENT_BINARY_DIR}/kdeconnectsms_export.h
) )
add_executable(kdeconnect-sms add_executable(kdeconnect-sms
main.cpp main.cpp
conversationlistmodel.cpp
conversationmodel.cpp
${KCSMS_SRCS}) ${KCSMS_SRCS})
target_include_directories(kdeconnect-sms PUBLIC ${CMAKE_BINARY_DIR}) target_include_directories(kdeconnect-sms PUBLIC ${CMAKE_BINARY_DIR})
target_link_libraries(kdeconnect-sms target_link_libraries(kdeconnect-sms
kdeconnectsms kdeconnectsmshelper
kdeconnectinterfaces kdeconnectinterfaces
Qt5::Quick Qt5::Quick
Qt5::Widgets Qt5::Widgets
@ -48,5 +45,5 @@ target_link_libraries(kdeconnect-sms
) )
install(TARGETS kdeconnect-sms ${INSTALL_TARGETS_DEFAULT_ARGS}) install(TARGETS kdeconnect-sms ${INSTALL_TARGETS_DEFAULT_ARGS})
install(TARGETS kdeconnectsms ${INSTALL_TARGETS_DEFAULT_ARGS} LIBRARY NAMELINK_SKIP) install(TARGETS kdeconnectsmshelper ${INSTALL_TARGETS_DEFAULT_ARGS} LIBRARY NAMELINK_SKIP)
install(PROGRAMS org.kde.kdeconnect.sms.desktop DESTINATION ${KDE_INSTALL_APPDIR}) install(PROGRAMS org.kde.kdeconnect.sms.desktop DESTINATION ${KDE_INSTALL_APPDIR})

View file

@ -25,6 +25,7 @@
#include <QLoggingCategory> #include <QLoggingCategory>
#include "interfaces/conversationmessage.h" #include "interfaces/conversationmessage.h"
#include "interfaces/dbusinterfaces.h" #include "interfaces/dbusinterfaces.h"
#include "smshelper.h"
Q_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL, "kdeconnect.sms.conversations_list") Q_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL, "kdeconnect.sms.conversations_list")
@ -177,7 +178,7 @@ void ConversationListModel::createRowFromMessage(const QVariantMap& msg)
KPeople::PersonData* ConversationListModel::lookupPersonByAddress(const QString& address) KPeople::PersonData* ConversationListModel::lookupPersonByAddress(const QString& address)
{ {
const QString& canonicalAddress = canonicalizePhoneNumber(address); const QString& canonicalAddress = SmsHelper::canonicalizePhoneNumber(address);
int rowIndex = 0; int rowIndex = 0;
for (rowIndex = 0; rowIndex < m_people.rowCount(); rowIndex++) { for (rowIndex = 0; rowIndex < m_people.rowCount(); rowIndex++) {
const QString& uri = m_people.get(rowIndex, KPeople::PersonsModel::PersonUriRole).toString(); const QString& uri = m_people.get(rowIndex, KPeople::PersonsModel::PersonUriRole).toString();
@ -194,8 +195,8 @@ KPeople::PersonData* ConversationListModel::lookupPersonByAddress(const QString&
// TODO: Either upgrade KPeople with an allPhoneNumbers method // TODO: Either upgrade KPeople with an allPhoneNumbers method
const QVariantList allPhoneNumbers = person->contactCustomProperty(QStringLiteral("all-phoneNumber")).toList(); const QVariantList allPhoneNumbers = person->contactCustomProperty(QStringLiteral("all-phoneNumber")).toList();
for (const QVariant& rawPhoneNumber : allPhoneNumbers) { for (const QVariant& rawPhoneNumber : allPhoneNumbers) {
const QString& phoneNumber = canonicalizePhoneNumber(rawPhoneNumber.toString()); const QString& phoneNumber = SmsHelper::canonicalizePhoneNumber(rawPhoneNumber.toString());
bool matchingPhoneNumber = isPhoneNumberMatchCanonicalized(canonicalAddress, phoneNumber); bool matchingPhoneNumber = SmsHelper::isPhoneNumberMatchCanonicalized(canonicalAddress, phoneNumber);
if (matchingPhoneNumber) { if (matchingPhoneNumber) {
//qCDebug(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << "Matched" << address << "to" << person->name(); //qCDebug(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << "Matched" << address << "to" << person->name();
@ -208,87 +209,3 @@ KPeople::PersonData* ConversationListModel::lookupPersonByAddress(const QString&
return nullptr; return nullptr;
} }
bool ConversationListModel::isPhoneNumberMatchCanonicalized(const QString& canonicalPhone1, const QString& canonicalPhone2)
{
// To decide if a phone number matches:
// 1. Are they similar lengths? If two numbers are very different, probably one is junk data and should be ignored
// 2. Is one a superset of the other? Phone number digits get more specific the further towards the end of the string,
// so if one phone number ends with the other, it is probably just a more-complete version of the same thing
const QString& longerNumber = canonicalPhone1.length() >= canonicalPhone2.length() ? canonicalPhone1 : canonicalPhone2;
const QString& shorterNumber = canonicalPhone1.length() < canonicalPhone2.length() ? canonicalPhone1 : canonicalPhone2;
const CountryCode& country = determineCountryCode(longerNumber);
const bool shorterNumberIsShortCode = isShortCode(shorterNumber, country);
const bool longerNumberIsShortCode = isShortCode(longerNumber, country);
if ((shorterNumberIsShortCode && !longerNumberIsShortCode) || (!shorterNumberIsShortCode && longerNumberIsShortCode)) {
// If only one of the numbers is a short code, they clearly do not match
return false;
}
bool matchingPhoneNumber = longerNumber.endsWith(shorterNumber);
return matchingPhoneNumber;
}
bool ConversationListModel::isPhoneNumberMatch(const QString& phone1, const QString& phone2)
{
const QString& canonicalPhone1 = canonicalizePhoneNumber(phone1);
const QString& canonicalPhone2 = canonicalizePhoneNumber(phone2);
return ConversationListModel::isPhoneNumberMatchCanonicalized(canonicalPhone1, canonicalPhone2);
}
bool ConversationListModel::isShortCode(const QString& phoneNumber, const ConversationListModel::CountryCode& country)
{
// Regardless of which country this number belongs to, a number of length less than 6 is a "short code"
if (phoneNumber.length() <= 6) {
return true;
}
if (country == CountryCode::Australia && phoneNumber.length() == 8 && phoneNumber.startsWith("19")) {
return true;
}
if (country == CountryCode::CzechRepublic && phoneNumber.length() <= 9) {
// This entry of the Wikipedia article is fairly poorly written, so it is not clear whether a
// short code with length 7 should start with a 9. Leave it like this for now, upgrade as
// we get more information
return true;
}
return false;
}
ConversationListModel::CountryCode ConversationListModel::determineCountryCode(const QString& canonicalNumber)
{
// This is going to fall apart if someone has not entered a country code into their contact book
// or if Android decides it can't be bothered to report the country code, but probably we will
// be fine anyway
if (canonicalNumber.startsWith("41")) {
return CountryCode::Australia;
}
if (canonicalNumber.startsWith("420")) {
return CountryCode::CzechRepublic;
}
// The only countries I care about for the current implementation are Australia and CzechRepublic
// If we need to deal with further countries, we should probably find a library
return CountryCode::Other;
}
QString ConversationListModel::canonicalizePhoneNumber(const QString& phoneNumber)
{
QString toReturn(phoneNumber);
toReturn = toReturn.remove(' ');
toReturn = toReturn.remove('-');
toReturn = toReturn.remove('(');
toReturn = toReturn.remove(')');
toReturn = toReturn.remove('+');
toReturn = toReturn.remove(QRegularExpression("^0*")); // Strip leading zeroes
if (toReturn.length() == 0) {
// If we have stripped away everything, assume this is a special number (and already canonicalized)
return phoneNumber;
}
return toReturn;
}

View file

@ -88,42 +88,9 @@ public:
}; };
Q_ENUM(Roles) Q_ENUM(Roles)
enum CountryCode {
Australia,
CzechRepublic,
Other, // I only care about a few country codes
};
QString deviceId() const { return m_deviceId; } QString deviceId() const { return m_deviceId; }
void setDeviceId(const QString &/*deviceId*/); void setDeviceId(const QString &/*deviceId*/);
/**
* Return true to indicate the two phone numbers should be considered the same, false otherwise
*/
static bool isPhoneNumberMatch(const QString& phone1, const QString& phone2);
/**
* Return true to indicate the two phone numbers should be considered the same, false otherwise
* Requires canonicalized phone numbers as inputs
*/
static bool isPhoneNumberMatchCanonicalized(const QString& canonicalPhone1, const QString& canonicalPhone2);
/**
* See inline comments for how short codes are determined
* All information from https://en.wikipedia.org/wiki/Short_code
*/
static bool isShortCode(const QString& canonicalNumber, const CountryCode& country);
/**
* Try to guess the country code from the passed number
*/
static CountryCode determineCountryCode(const QString& canonicalNumber);
/**
* Simplify a phone number to a known form
*/
static QString canonicalizePhoneNumber(const QString& phoneNumber);
public Q_SLOTS: public Q_SLOTS:
void handleCreatedConversation(const QVariantMap& msg); void handleCreatedConversation(const QVariantMap& msg);
void handleConversationUpdated(const QVariantMap& msg); void handleConversationUpdated(const QVariantMap& msg);

111
smsapp/smshelper.cpp Normal file
View file

@ -0,0 +1,111 @@
/**
* Copyright (C) 2019 Simon Redman <simon@ergotech.com>
*
* This program 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 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "smshelper.h"
#include <QString>
#include <QRegularExpression>
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(KDECONNECT_SMS_SMSHELPER, "kdeconnect.sms.smshelper")
bool SmsHelper::isPhoneNumberMatchCanonicalized(const QString& canonicalPhone1, const QString& canonicalPhone2)
{
// To decide if a phone number matches:
// 1. Are they similar lengths? If two numbers are very different, probably one is junk data and should be ignored
// 2. Is one a superset of the other? Phone number digits get more specific the further towards the end of the string,
// so if one phone number ends with the other, it is probably just a more-complete version of the same thing
const QString& longerNumber = canonicalPhone1.length() >= canonicalPhone2.length() ? canonicalPhone1 : canonicalPhone2;
const QString& shorterNumber = canonicalPhone1.length() < canonicalPhone2.length() ? canonicalPhone1 : canonicalPhone2;
const CountryCode& country = determineCountryCode(longerNumber);
const bool shorterNumberIsShortCode = isShortCode(shorterNumber, country);
const bool longerNumberIsShortCode = isShortCode(longerNumber, country);
if ((shorterNumberIsShortCode && !longerNumberIsShortCode) || (!shorterNumberIsShortCode && longerNumberIsShortCode)) {
// If only one of the numbers is a short code, they clearly do not match
return false;
}
bool matchingPhoneNumber = longerNumber.endsWith(shorterNumber);
return matchingPhoneNumber;
}
bool SmsHelper::isPhoneNumberMatch(const QString& phone1, const QString& phone2)
{
const QString& canonicalPhone1 = canonicalizePhoneNumber(phone1);
const QString& canonicalPhone2 = canonicalizePhoneNumber(phone2);
return isPhoneNumberMatchCanonicalized(canonicalPhone1, canonicalPhone2);
}
bool SmsHelper::isShortCode(const QString& phoneNumber, const SmsHelper::CountryCode& country)
{
// Regardless of which country this number belongs to, a number of length less than 6 is a "short code"
if (phoneNumber.length() <= 6) {
return true;
}
if (country == CountryCode::Australia && phoneNumber.length() == 8 && phoneNumber.startsWith("19")) {
return true;
}
if (country == CountryCode::CzechRepublic && phoneNumber.length() <= 9) {
// This entry of the Wikipedia article is fairly poorly written, so it is not clear whether a
// short code with length 7 should start with a 9. Leave it like this for now, upgrade as
// we get more information
return true;
}
return false;
}
SmsHelper::CountryCode SmsHelper::determineCountryCode(const QString& canonicalNumber)
{
// This is going to fall apart if someone has not entered a country code into their contact book
// or if Android decides it can't be bothered to report the country code, but probably we will
// be fine anyway
if (canonicalNumber.startsWith("41")) {
return CountryCode::Australia;
}
if (canonicalNumber.startsWith("420")) {
return CountryCode::CzechRepublic;
}
// The only countries I care about for the current implementation are Australia and CzechRepublic
// If we need to deal with further countries, we should probably find a library
return CountryCode::Other;
}
QString SmsHelper::canonicalizePhoneNumber(const QString& phoneNumber)
{
QString toReturn(phoneNumber);
toReturn = toReturn.remove(' ');
toReturn = toReturn.remove('-');
toReturn = toReturn.remove('(');
toReturn = toReturn.remove(')');
toReturn = toReturn.remove('+');
toReturn = toReturn.remove(QRegularExpression("^0*")); // Strip leading zeroes
if (toReturn.length() == 0) {
// If we have stripped away everything, assume this is a special number (and already canonicalized)
return phoneNumber;
}
return toReturn;
}

71
smsapp/smshelper.h Normal file
View file

@ -0,0 +1,71 @@
/**
* Copyright (C) 2019 Simon Redman <simon@ergotech.com>
*
* This program 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 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef SMSHELPER_H
#define SMSHELPER_H
#include <QLoggingCategory>
#include "kdeconnectsms_export.h"
Q_DECLARE_LOGGING_CATEGORY(KDECONNECT_SMS_SMSHELPER)
class KDECONNECTSMSAPPLIB_EXPORT SmsHelper
{
public:
enum CountryCode {
Australia,
CzechRepublic,
Other, // I only care about a few country codes
};
/**
* Return true to indicate the two phone numbers should be considered the same, false otherwise
*/
static bool isPhoneNumberMatch(const QString& phone1, const QString& phone2);
/**
* Return true to indicate the two phone numbers should be considered the same, false otherwise
* Requires canonicalized phone numbers as inputs
*/
static bool isPhoneNumberMatchCanonicalized(const QString& canonicalPhone1, const QString& canonicalPhone2);
/**
* See inline comments for how short codes are determined
* All information from https://en.wikipedia.org/wiki/Short_code
*/
static bool isShortCode(const QString& canonicalNumber, const CountryCode& country);
/**
* Try to guess the country code from the passed number
*/
static CountryCode determineCountryCode(const QString& canonicalNumber);
/**
* Simplify a phone number to a known form
*/
static QString canonicalizePhoneNumber(const QString& phoneNumber);
private:
SmsHelper(){};
~SmsHelper(){};
};
#endif // SMSHELPER_H

View file

@ -19,11 +19,8 @@ set(kdeconnect_libraries
) )
set(kdeconnect_sms_libraries set(kdeconnect_sms_libraries
${kdeconnect_libraries} kdeconnectsmshelper
kdeconnectinterfaces Qt5::Test
kdeconnectsms
Qt5::Quick
KF5::People
) )
ecm_add_test(pluginloadtest.cpp LINK_LIBRARIES ${kdeconnect_libraries}) ecm_add_test(pluginloadtest.cpp LINK_LIBRARIES ${kdeconnect_libraries})
@ -41,5 +38,5 @@ ecm_add_test(testnotificationlistener.cpp
TEST_NAME testnotificationlistener TEST_NAME testnotificationlistener
LINK_LIBRARIES ${kdeconnect_libraries} Qt5::DBus KF5::Notifications KF5::IconThemes) LINK_LIBRARIES ${kdeconnect_libraries} Qt5::DBus KF5::Notifications KF5::IconThemes)
if(SMSAPP_ENABLED) if(SMSAPP_ENABLED)
ecm_add_test(testconversationlistmodel.cpp LINK_LIBRARIES ${kdeconnect_sms_libraries}) ecm_add_test(testsmshelper.cpp LINK_LIBRARIES ${kdeconnect_sms_libraries})
endif() endif()

View file

@ -18,14 +18,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "conversationlistmodel.h" #include "smshelper.h"
#include <QtTest> #include <QtTest>
/** /**
* This class tests the working of device class * This class tests the working of device class
*/ */
class ConversationListModelTest : public QObject class SmsHelperTest : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -53,23 +53,23 @@ private Q_SLOTS:
/** /**
* Two phone numbers which are exactly the same should match * Two phone numbers which are exactly the same should match
*/ */
void ConversationListModelTest::testSimplePhoneMatch() void SmsHelperTest::testSimplePhoneMatch()
{ {
const QString& phoneNumber = QLatin1String("+1 (222) 333-4444"); const QString& phoneNumber = QLatin1String("+1 (222) 333-4444");
QVERIFY2(ConversationListModel::isPhoneNumberMatch(phoneNumber, phoneNumber), QVERIFY2(SmsHelper::isPhoneNumberMatch(phoneNumber, phoneNumber),
"Failed to match a phone number with itself"); "Failed to match a phone number with itself");
} }
/** /**
* Two phone numbers which have been entered with different formatting should match * Two phone numbers which have been entered with different formatting should match
*/ */
void ConversationListModelTest::testMessyPhoneMatch() void SmsHelperTest::testMessyPhoneMatch()
{ {
const QString& phoneNumber = QLatin1String("12223334444"); const QString& phoneNumber = QLatin1String("12223334444");
const QString& messyPhoneNumber = QLatin1String("+1 (222) 333-4444"); const QString& messyPhoneNumber = QLatin1String("+1 (222) 333-4444");
QVERIFY2(ConversationListModel::isPhoneNumberMatch(phoneNumber, messyPhoneNumber), QVERIFY2(SmsHelper::isPhoneNumberMatch(phoneNumber, messyPhoneNumber),
"Failed to match same number with different formatting characters"); "Failed to match same number with different formatting characters");
} }
@ -77,12 +77,12 @@ void ConversationListModelTest::testMessyPhoneMatch()
* I don't think most people in the US even know what a country code *is*, and I know lots of people * 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. * who don't enter it into their contacts book here. Make sure that kind of match works.
*/ */
void ConversationListModelTest::testMissingCountryCode() void SmsHelperTest::testMissingCountryCode()
{ {
const QString& shortForm = QLatin1String("(222) 333-4444"); const QString& shortForm = QLatin1String("(222) 333-4444");
const QString& longForm = QLatin1String("+1 (222) 333-4444"); const QString& longForm = QLatin1String("+1 (222) 333-4444");
QVERIFY2(ConversationListModel::isPhoneNumberMatch(shortForm, longForm), QVERIFY2(SmsHelper::isPhoneNumberMatch(shortForm, longForm),
"Failed to match two same numbers with missing country code"); "Failed to match two same numbers with missing country code");
} }
@ -90,12 +90,12 @@ void ConversationListModelTest::testMissingCountryCode()
* I don't quite understand which cases this applies, but sometimes you see a message where the * 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. * country code has been prefixed with a few zeros. Make sure that works too.
*/ */
void ConversationListModelTest::testLeadingZeros() void SmsHelperTest::testLeadingZeros()
{ {
const QString& shortForm = QLatin1String("+1 (222) 333-4444"); const QString& shortForm = QLatin1String("+1 (222) 333-4444");
const QString& zeroForm = QLatin1String("001 (222) 333-4444"); const QString& zeroForm = QLatin1String("001 (222) 333-4444");
QVERIFY2(ConversationListModel::isPhoneNumberMatch(shortForm, zeroForm), QVERIFY2(SmsHelper::isPhoneNumberMatch(shortForm, zeroForm),
"Failed to match two same numbers with padded zeros"); "Failed to match two same numbers with padded zeros");
} }
@ -104,12 +104,12 @@ void ConversationListModelTest::testLeadingZeros()
* the phone call or text message go through. Some people's contacts books might be this way, so make * 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 * sure we support matching that too
*/ */
void ConversationListModelTest::testLongDistancePhoneNumber() void SmsHelperTest::testLongDistancePhoneNumber()
{ {
const QString& shortForm = QLatin1String("333-4444"); const QString& shortForm = QLatin1String("333-4444");
const QString& longForm = QLatin1String("+1 (222) 333-4444"); const QString& longForm = QLatin1String("+1 (222) 333-4444");
QVERIFY2(ConversationListModel::isPhoneNumberMatch(shortForm, longForm), QVERIFY2(SmsHelper::isPhoneNumberMatch(shortForm, longForm),
"Failed to suffix match two phone numbers"); "Failed to suffix match two phone numbers");
} }
@ -117,142 +117,142 @@ void ConversationListModelTest::testLongDistancePhoneNumber()
* Phone operators define short codes for a variety of reasons. Even if they have the same suffix, * 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 * they should not match a regular phone number
*/ */
void ConversationListModelTest::testShortCodePhoneNumberNonMatch() void SmsHelperTest::testShortCodePhoneNumberNonMatch()
{ {
const QString& shortCode = QLatin1String("44455"); const QString& shortCode = QLatin1String("44455");
const QString& normalNumber = QLatin1String("222-334-4455"); const QString& normalNumber = QLatin1String("222-334-4455");
QVERIFY2(!ConversationListModel::isPhoneNumberMatch(shortCode, normalNumber), QVERIFY2(!SmsHelper::isPhoneNumberMatch(shortCode, normalNumber),
"Short code matched with normal number"); "Short code matched with normal number");
} }
/** /**
* Two of the same short code should be able to match * Two of the same short code should be able to match
*/ */
void ConversationListModelTest::testShortCodeMatch() void SmsHelperTest::testShortCodeMatch()
{ {
const QString& shortCode = QLatin1String("44455"); const QString& shortCode = QLatin1String("44455");
QVERIFY2(ConversationListModel::isPhoneNumberMatch(shortCode, shortCode), QVERIFY2(SmsHelper::isPhoneNumberMatch(shortCode, shortCode),
"Did not match two of the same short code"); "Did not match two of the same short code");
} }
/** /**
* Two different short codes should not match * Two different short codes should not match
*/ */
void ConversationListModelTest::testShortCodeNonMatch() void SmsHelperTest::testShortCodeNonMatch()
{ {
const QString& shortCode1 = QLatin1String("44455"); const QString& shortCode1 = QLatin1String("44455");
const QString& shortCode2 = QLatin1String("66677"); const QString& shortCode2 = QLatin1String("66677");
QVERIFY2(!ConversationListModel::isPhoneNumberMatch(shortCode1, shortCode2), QVERIFY2(!SmsHelper::isPhoneNumberMatch(shortCode1, shortCode2),
"Incorrectly matched two different short codes"); "Incorrectly matched two different short codes");
} }
/** /**
* Even in the land down under, a short code phone number should not match a regular phone number * Even in the land down under, a short code phone number should not match a regular phone number
*/ */
void ConversationListModelTest::testAustralianShortCodeNumberNonMatch() void SmsHelperTest::testAustralianShortCodeNumberNonMatch()
{ {
const QString& australianShortCode = QLatin1String("19333444"); const QString& australianShortCode = QLatin1String("19333444");
// I'm not sure if this is even a valid Australian phone number, but whatever... // I'm not sure if this is even a valid Australian phone number, but whatever...
const QString& australianPhoneNumber = QLatin1String("+41 02 1233 3444"); const QString& australianPhoneNumber = QLatin1String("+41 02 1233 3444");
QVERIFY2(!ConversationListModel::isPhoneNumberMatch(australianShortCode, australianPhoneNumber), QVERIFY2(!SmsHelper::isPhoneNumberMatch(australianShortCode, australianPhoneNumber),
"Matched Australian short code with regular phone number"); "Matched Australian short code with regular phone number");
} }
/** /**
* Two of the same Australian short code numbers should be able to match * Two of the same Australian short code numbers should be able to match
*/ */
void ConversationListModelTest::testAustralianShortCodeMatch() void SmsHelperTest::testAustralianShortCodeMatch()
{ {
const QString& australianShortCode = QLatin1String("12333444"); const QString& australianShortCode = QLatin1String("12333444");
QVERIFY2(ConversationListModel::isPhoneNumberMatch(australianShortCode, australianShortCode), QVERIFY2(SmsHelper::isPhoneNumberMatch(australianShortCode, australianShortCode),
"Failed to match Australian short code number"); "Failed to match Australian short code number");
} }
/** /**
* Two different Australian short code numbers should be able to match * Two different Australian short code numbers should be able to match
*/ */
void ConversationListModelTest::testAustralianShortCodeNonMatch() void SmsHelperTest::testAustralianShortCodeNonMatch()
{ {
const QString& australianShortCode1 = QLatin1String("12333444"); const QString& australianShortCode1 = QLatin1String("12333444");
const QString& australianShortCode2 = QLatin1String("12555666"); const QString& australianShortCode2 = QLatin1String("12555666");
QVERIFY2(!ConversationListModel::isPhoneNumberMatch(australianShortCode1, australianShortCode2), QVERIFY2(!SmsHelper::isPhoneNumberMatch(australianShortCode1, australianShortCode2),
"Matched two different Australian short code numbers"); "Matched two different Australian short code numbers");
} }
/** /**
* A Czech Republic short code phone number should not match a regular phone number * A Czech Republic short code phone number should not match a regular phone number
*/ */
void ConversationListModelTest::testCzechRepublicShortCodeNumberNonMatch() void SmsHelperTest::testCzechRepublicShortCodeNumberNonMatch()
{ {
const QString& czechRepShortCode = QLatin1String("9090930"); const QString& czechRepShortCode = QLatin1String("9090930");
// I'm not sure if this is even a valid Czech Republic phone number, but whatever... // I'm not sure if this is even a valid Czech Republic phone number, but whatever...
const QString& czechRepPhoneNumber = QLatin1String("+420 809 090 930"); const QString& czechRepPhoneNumber = QLatin1String("+420 809 090 930");
QVERIFY2(!ConversationListModel::isPhoneNumberMatch(czechRepShortCode, czechRepPhoneNumber), QVERIFY2(!SmsHelper::isPhoneNumberMatch(czechRepShortCode, czechRepPhoneNumber),
"Matched Czech Republic short code with regular phone number"); "Matched Czech Republic short code with regular phone number");
} }
/** /**
* Two of the same Czech Republic short code numbers should be able to match * Two of the same Czech Republic short code numbers should be able to match
*/ */
void ConversationListModelTest::testCzechRepublicShortCodeMatch() void SmsHelperTest::testCzechRepublicShortCodeMatch()
{ {
const QString& czechRepShortCode = QLatin1String("9090930"); const QString& czechRepShortCode = QLatin1String("9090930");
QVERIFY2(ConversationListModel::isPhoneNumberMatch(czechRepShortCode, czechRepShortCode), QVERIFY2(SmsHelper::isPhoneNumberMatch(czechRepShortCode, czechRepShortCode),
"Failed to match Czech Republic short code number"); "Failed to match Czech Republic short code number");
} }
/** /**
* Two different Czech Republic short code numbers should be able to match * Two different Czech Republic short code numbers should be able to match
*/ */
void ConversationListModelTest::testCzechRepublicShortCodeNonMatch() void SmsHelperTest::testCzechRepublicShortCodeNonMatch()
{ {
const QString& czechRepShortCode1 = QLatin1String("9090930"); const QString& czechRepShortCode1 = QLatin1String("9090930");
const QString& czechRepShortCode2 = QLatin1String("9080990"); const QString& czechRepShortCode2 = QLatin1String("9080990");
QVERIFY2(!ConversationListModel::isPhoneNumberMatch(czechRepShortCode1, czechRepShortCode2), QVERIFY2(!SmsHelper::isPhoneNumberMatch(czechRepShortCode1, czechRepShortCode2),
"Matched two different Czech Republic short code numbers"); "Matched two different Czech Republic short code numbers");
} }
/** /**
* Two phone numbers which are different should not be reported as the same * Two phone numbers which are different should not be reported as the same
*/ */
void ConversationListModelTest::testDifferentPhoneNumbers1() void SmsHelperTest::testDifferentPhoneNumbers1()
{ {
const QString& phone1 = QLatin1String("+1 (222) 333-4444"); const QString& phone1 = QLatin1String("+1 (222) 333-4444");
const QString& phone2 = QLatin1String("+1 (333) 222-4444"); const QString& phone2 = QLatin1String("+1 (333) 222-4444");
QVERIFY2(!ConversationListModel::isPhoneNumberMatch(phone1, phone2), QVERIFY2(!SmsHelper::isPhoneNumberMatch(phone1, phone2),
"Incorrectly marked two different numbers as same"); "Incorrectly marked two different numbers as same");
} }
/** /**
* Two phone numbers which are different should not be reported as the same * Two phone numbers which are different should not be reported as the same
*/ */
void ConversationListModelTest::testDifferentPhoneNumbers2() void SmsHelperTest::testDifferentPhoneNumbers2()
{ {
const QString& phone1 = QLatin1String("+1 (222) 333-4444"); const QString& phone1 = QLatin1String("+1 (222) 333-4444");
const QString& phone2 = QLatin1String("122-2333"); const QString& phone2 = QLatin1String("122-2333");
QVERIFY2(!ConversationListModel::isPhoneNumberMatch(phone1, phone2), QVERIFY2(!SmsHelper::isPhoneNumberMatch(phone1, phone2),
"Incorrectly *prefix* matched two phone numbers"); "Incorrectly *prefix* matched two phone numbers");
} }
/** /**
* Some places allow a message with all zeros to be a short code. We should allow that too. * Some places allow a message with all zeros to be a short code. We should allow that too.
*/ */
void ConversationListModelTest::testAllZeros() void SmsHelperTest::testAllZeros()
{ {
const QString& zeros = QLatin1String("00000"); const QString& zeros = QLatin1String("00000");
const QString& canonicalized = ConversationListModel::canonicalizePhoneNumber(zeros); const QString& canonicalized = SmsHelper::canonicalizePhoneNumber(zeros);
QCOMPARE(canonicalized.length(), zeros.length()); QCOMPARE(canonicalized.length(), zeros.length());
} }
QTEST_MAIN(ConversationListModelTest); QTEST_MAIN(SmsHelperTest);
#include "testconversationlistmodel.moc" #include "testsmshelper.moc"