File indexing completed on 2024-04-21 15:02:45

0001 /*
0002     SPDX-FileCopyrightText: 2013 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "persondatatests.h"
0008 
0009 #include "fakecontactsource.h"
0010 
0011 // private includes
0012 #include "personmanager_p.h"
0013 
0014 // public kpeople includes
0015 #include <persondata.h>
0016 #include <personpluginmanager.h>
0017 
0018 #include <QSignalSpy>
0019 #include <QTest>
0020 
0021 QTEST_GUILESS_MAIN(PersonDataTests)
0022 
0023 using namespace KPeople;
0024 
0025 // this tests PersonData but also implicitly tests the private classes
0026 // - BasePersonsDataSource
0027 // - DefaultContactMonitor
0028 // - MetaContact
0029 
0030 void PersonDataTests::initTestCase()
0031 {
0032     QVERIFY(m_database.open());
0033 
0034     // Called before the first testfunction is executed
0035     PersonManager::instance(m_database.fileName());
0036     PersonManager::instance()->mergeContacts(QStringList() << QStringLiteral("fakesource://contact2") << QStringLiteral("fakesource://contact3"));
0037 
0038     m_source = new FakeContactSource(nullptr); // don't own. PersonPluginManager removes it on destruction
0039     QHash<QString, BasePersonsDataSource *> sources;
0040     sources[QStringLiteral("fakesource")] = m_source;
0041     PersonPluginManager::setDataSourcePlugins(sources);
0042 }
0043 
0044 void PersonDataTests::cleanupTestCase()
0045 {
0046     // Called after the last testfunction was executed
0047     m_database.close();
0048 }
0049 
0050 void PersonDataTests::init()
0051 {
0052     // Called before each testfunction is executed
0053 }
0054 
0055 void PersonDataTests::cleanup()
0056 {
0057     // Called after every testfunction
0058 }
0059 
0060 void PersonDataTests::loadContact()
0061 {
0062     QString personUri = QStringLiteral("fakesource://contact1");
0063     PersonData person(personUri);
0064     // in this case we know the datasource is synchronous, but we should extend the test to cope with it not being async.
0065 
0066     QCOMPARE(person.contactUris().size(), 1);
0067     QCOMPARE(person.name(), QStringLiteral("Contact 1"));
0068     QCOMPARE(person.allEmails(), QStringList(QStringLiteral("contact1@example.com")));
0069     QCOMPARE(person.personUri(), personUri);
0070 }
0071 
0072 void PersonDataTests::loadPerson()
0073 {
0074     // loading contact 2 which is already merged should return person1
0075     // which is both contact 2 and 3
0076     PersonData person(QStringLiteral("fakesource://contact2"));
0077 
0078     QCOMPARE(person.contactUris().size(), 2);
0079     QCOMPARE(person.name(), QStringLiteral("Contact 2"));
0080     QCOMPARE(person.allEmails().size(), 2);
0081     QCOMPARE(person.personUri(), QStringLiteral("kpeople://1"));
0082 
0083     // convert to set as order is not important
0084     const QStringList allEmails = person.allEmails();
0085     const QSet<QString> allEmailsSet(allEmails.begin(), allEmails.end());
0086     QCOMPARE(allEmailsSet, QSet<QString>() << QStringLiteral("contact2@example.com") << QStringLiteral("contact3@example.com"));
0087 }
0088 
0089 void PersonDataTests::contactChanged()
0090 {
0091     PersonData person(QStringLiteral("fakesource://contact1"));
0092 
0093     QCOMPARE(person.allEmails().at(0), QStringLiteral("contact1@example.com"));
0094 
0095     QSignalSpy spy(&person, SIGNAL(dataChanged()));
0096     m_source->changeProperty(AbstractContact::EmailProperty, QStringLiteral("newaddress@yahoo.com"));
0097     QCOMPARE(spy.count(), 1);
0098 
0099     QCOMPARE(person.allEmails().at(0), QStringLiteral("newaddress@yahoo.com"));
0100 }
0101 
0102 void PersonDataTests::nullPerson()
0103 {
0104     PersonData person(QStringLiteral("fakesource://unexisting"));
0105     QCOMPARE(QString(), person.name());
0106     QVERIFY(!person.isValid());
0107 
0108     PersonData invalidPerson(QStringLiteral());
0109     QVERIFY(!invalidPerson.isValid());
0110 }
0111 
0112 void PersonDataTests::removeContact()
0113 {
0114     PersonData person(QStringLiteral("fakesource://contact1"));
0115 
0116     QCOMPARE(person.allEmails().at(0), QStringLiteral("contact1@example.com"));
0117 
0118     QSignalSpy spy(&person, &PersonData::dataChanged);
0119     m_source->remove(person.personUri());
0120     QCOMPARE(spy.count(), 1);
0121 
0122     QCOMPARE(person.name(), QString());
0123 }
0124 
0125 #include "moc_persondatatests.cpp"