Warning, file /plasma-mobile/plasma-dialer/kde-telephony-daemon/autotests/contact-mapper-test.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // SPDX-FileCopyrightText: 2021 Nicolas Fella <nicolas.fella@gmx.de>
0002 //
0003 // SPDX-License-Identifier: LicenseRef-KDE-Accepted-GPL
0004 
0005 #include <QTest>
0006 
0007 #include <QTemporaryFile>
0008 
0009 #include "contact-phone-number-mapper.h"
0010 #include "fake-contact-source.h"
0011 
0012 #include <KPeople/PersonPluginManager>
0013 #include <kpeopleprivate/personmanager_p.h>
0014 
0015 class ContactMapperTest : public QObject
0016 {
0017     Q_OBJECT
0018 private Q_SLOTS:
0019     void initTestCase()
0020     {
0021         QVERIFY(_database.open());
0022 
0023         // For this test we assume we are in Sweden and the implicit country code is +46
0024         QLocale::setDefault(QLocale::Swedish);
0025 
0026         // Called before the first testfunction is executed
0027         PersonManager::instance(_database.fileName());
0028         _source = new FakeContactSource(nullptr); // don't own. PersonPluginManager removes it on destruction
0029         QHash<QString, KPeople::BasePersonsDataSource *> sources;
0030         sources[QStringLiteral("fakesource")] = _source;
0031         KPeople::PersonPluginManager::setDataSourcePlugins(sources);
0032     }
0033 
0034     void testURI_data()
0035     {
0036         QTest::addColumn<QString>("number");
0037         QTest::addColumn<QString>("uri");
0038 
0039         QTest::newRow("nonexistant number") << QStringLiteral("+49123456") << QString();
0040 
0041         QTest::newRow("contact 2, foreign country code") << QStringLiteral("+1 234 567 890") << QStringLiteral("fakesource://contact2");
0042         QTest::newRow("contact 2, dashes") << QStringLiteral("+1-234-567-890") << QStringLiteral("fakesource://contact2");
0043 
0044         QTest::newRow("contact 3") << QStringLiteral("+46 666 777 999") << QStringLiteral("fakesource://contact3");
0045         QTest::newRow("contact 3, 00 instead of +") << QStringLiteral("0046 666 777 999") << QStringLiteral("fakesource://contact3");
0046         QTest::newRow("contact 3, no spaces") << QStringLiteral("+46666777999") << QStringLiteral("fakesource://contact3");
0047         QTest::newRow("contact 3, dashes") << QStringLiteral("+46-666-777-999") << QStringLiteral("fakesource://contact3");
0048         QTest::newRow("contact 5, first number") << QStringLiteral("+46111222333") << QStringLiteral("fakesource://contact5");
0049         QTest::newRow("contact 5, first number, no country code") << QStringLiteral("0111222333") << QStringLiteral("fakesource://contact5");
0050         QTest::newRow("contact 5, second number") << QStringLiteral("+46333222111") << QStringLiteral("fakesource://contact5");
0051         QTest::newRow("contact 5, second number, no country code") << QStringLiteral("0333222111") << QStringLiteral("fakesource://contact5");
0052         QTest::newRow("invalid number") << QStringLiteral("+49yyy123456") << QString();
0053     }
0054 
0055     void testURI()
0056     {
0057         auto &mapper = ContactPhoneNumberMapper::instance();
0058 
0059         QFETCH(QString, number);
0060         QFETCH(QString, uri);
0061 
0062         QCOMPARE(mapper.uriForNumber(number), uri);
0063     }
0064 
0065 private:
0066     FakeContactSource *_source;
0067     QTemporaryFile _database;
0068 };
0069 
0070 QTEST_GUILESS_MAIN(ContactMapperTest)
0071 
0072 #include "contact-mapper-test.moc"