Warning, file /frameworks/kpeople/autotests/personsmodeltest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2013 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "personsmodeltest.h"
0008 
0009 #include "fakecontactsource.h"
0010 
0011 // private includes
0012 #include "personmanager_p.h"
0013 
0014 // public kpeople includes
0015 #include <personpluginmanager.h>
0016 #include <personsmodel.h>
0017 
0018 #include <QSignalSpy>
0019 #include <QTest>
0020 #include <QVariant>
0021 
0022 QTEST_GUILESS_MAIN(PersonsModelTest)
0023 
0024 using namespace KPeople;
0025 
0026 void PersonsModelTest::initTestCase()
0027 {
0028     QVERIFY(m_database.open());
0029 
0030     // Called before the first testfunction is executed
0031     PersonManager::instance(m_database.fileName());
0032     m_source = new FakeContactSource(nullptr); // don't own. PersonPluginManager removes it on destruction
0033     QHash<QString, BasePersonsDataSource *> sources;
0034     sources[QStringLiteral("fakesource")] = m_source;
0035     PersonPluginManager::setDataSourcePlugins(sources);
0036 
0037     m_model = new KPeople::PersonsModel(this);
0038     QSignalSpy modelInit(m_model, SIGNAL(modelInitialized(bool)));
0039 
0040     QTRY_COMPARE(modelInit.count(), 1);
0041     QCOMPARE(modelInit.first().at(0).toBool(), true);
0042 }
0043 
0044 void PersonsModelTest::cleanupTestCase()
0045 {
0046     // Called after the last testfunction was executed
0047     m_database.close();
0048 }
0049 
0050 void PersonsModelTest::loadModel()
0051 {
0052     QCOMPARE(m_model->rowCount(), 4);
0053     QCOMPARE(m_model->data(m_model->index(0)).toString(), QStringLiteral("Contact 1"));
0054     QCOMPARE(m_model->data(m_model->index(1)).toString(), QStringLiteral("Contact 2"));
0055     QCOMPARE(m_model->data(m_model->index(2)).toString(), QStringLiteral("Contact 3"));
0056     QCOMPARE(m_model->data(m_model->index(3)).toString(), QStringLiteral("Contact 4"));
0057 
0058     m_source->changeProperty(AbstractContact::NameProperty, QStringLiteral("Contact A"));
0059 
0060     QCOMPARE(m_model->rowCount(), 4);
0061     QCOMPARE(m_model->data(m_model->index(0)).toString(), QStringLiteral("Contact A"));
0062     QCOMPARE(m_model->data(m_model->index(1)).toString(), QStringLiteral("Contact 2"));
0063     QCOMPARE(m_model->data(m_model->index(2)).toString(), QStringLiteral("Contact 3"));
0064     QCOMPARE(m_model->data(m_model->index(3)).toString(), QStringLiteral("Contact 4"));
0065 }
0066 
0067 void PersonsModelTest::mergeContacts()
0068 {
0069     QStringList uris{QStringLiteral("fakesource://contact1"), QStringLiteral("fakesource://contact2")};
0070     QSignalSpy modelRowsInsert(m_model, SIGNAL(rowsInserted(QModelIndex, int, int)));
0071 
0072     QCOMPARE(m_model->rowCount(), 4);
0073     QString newUri = KPeople::mergeContacts(uris);
0074     QCOMPARE(newUri, QStringLiteral("kpeople://1"));
0075 
0076     // TODO: replace with actual model signals spying
0077     QTRY_COMPARE(m_model->rowCount(), 3);
0078     QCOMPARE(m_model->rowCount(m_model->indexForPersonUri(newUri)), 2);
0079 
0080     // There needs to be 2 rows inserted - one for the new Person
0081     // and one for the new contact added to it (the other contact
0082     // is already a child of the person; merging just takes all
0083     // contacts from one person and adds them to the other)
0084     QCOMPARE(modelRowsInsert.count(), 2);
0085     // The first inserted Person must have invalid parent index
0086     QCOMPARE(modelRowsInsert.first().at(0).toModelIndex(), QModelIndex());
0087     // Second inserted row, the Contact, must have the Person index as parent
0088     QCOMPARE(modelRowsInsert.at(1).at(0).toModelIndex(), m_model->indexForPersonUri(newUri));
0089 
0090     modelRowsInsert.clear();
0091 
0092     QStringList uris2{QStringLiteral("fakesource://contact3"), newUri};
0093     QString newUri2 = KPeople::mergeContacts(uris2);
0094     QCOMPARE(newUri2, QStringLiteral("kpeople://1"));
0095 
0096     QTest::qWait(2000);
0097 
0098     QCOMPARE(m_model->rowCount(), 2);
0099     QCOMPARE(m_model->rowCount(m_model->indexForPersonUri(newUri2)), 3);
0100     QCOMPARE(modelRowsInsert.count(), 1);
0101     QCOMPARE(modelRowsInsert.first().at(0).toModelIndex(), m_model->indexForPersonUri(newUri));
0102 }
0103 
0104 void PersonsModelTest::gettersTests()
0105 {
0106     // Find the index for "kpeople://1" using the QAIModel method
0107     QModelIndexList indexList =
0108         m_model->match(m_model->index(0, 0, QModelIndex()), KPeople::PersonsModel::PersonUriRole, QVariant(QStringLiteral("kpeople://1")), 1);
0109     QModelIndex personIndex = indexList.first();
0110 
0111     // Now get the index using our method
0112     QModelIndex indexForPerson = m_model->indexForPersonUri(QStringLiteral("kpeople://1"));
0113 
0114     // Now compare
0115     QCOMPARE(personIndex, indexForPerson);
0116 
0117     // TODO: also test the get() method?
0118 }
0119 
0120 void PersonsModelTest::unmergeContacts()
0121 {
0122     QModelIndex personIndex = m_model->indexForPersonUri(QStringLiteral("kpeople://1"));
0123     QSignalSpy modelRowsInsert(m_model, SIGNAL(rowsInserted(QModelIndex, int, int)));
0124     QSignalSpy modelRowsRemove(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)));
0125 
0126     QCOMPARE(m_model->rowCount(), 2);
0127     QCOMPARE(m_model->rowCount(personIndex), 3);
0128 
0129     KPeople::unmergeContact(QStringLiteral("fakesource://contact3"));
0130     QTest::qWait(2000);
0131 
0132     QCOMPARE(m_model->rowCount(), 3);
0133     QCOMPARE(m_model->rowCount(personIndex), 2);
0134 
0135     // The unmerged Contact is turned into new Person (the fake Person where Person == Contact)
0136     // There must be 1 insertion and the parent must be invalid index
0137     QCOMPARE(modelRowsInsert.count(), 1);
0138     QCOMPARE(modelRowsInsert.first().at(0).toModelIndex(), QModelIndex());
0139 
0140     // Similarly, there must be one row removed and the parent must be
0141     // the old Person index
0142     QCOMPARE(modelRowsRemove.count(), 1);
0143     QCOMPARE(modelRowsRemove.first().at(0).toModelIndex(), personIndex);
0144 
0145     modelRowsInsert.clear();
0146     modelRowsRemove.clear();
0147 
0148     KPeople::unmergeContact(QStringLiteral("kpeople://1"));
0149     QTest::qWait(2000);
0150     QCOMPARE(m_model->rowCount(), 4);
0151     // Check that the person is gone from the model
0152     QCOMPARE(m_model->indexForPersonUri(QStringLiteral("kpeople://1")), QModelIndex());
0153 
0154     QCOMPARE(modelRowsInsert.count(), 2);
0155     QCOMPARE(modelRowsInsert.first().at(0).toModelIndex(), QModelIndex());
0156     QCOMPARE(modelRowsInsert.at(1).at(0).toModelIndex(), QModelIndex());
0157 
0158     // There must be exactly 3 rows removed when unmerging a Person
0159     // with 2 contacts - first the subcontacts are removed and then
0160     // the parent Person itself
0161     QCOMPARE(modelRowsRemove.count(), 3);
0162     // The first two Contacts must have parent the Person index
0163     // and both should have 0 0 as the "first last" args of removeRows
0164     QCOMPARE(modelRowsRemove.first().at(0).toModelIndex(), personIndex);
0165     QCOMPARE(modelRowsRemove.first().at(1).toInt(), 0);
0166     QCOMPARE(modelRowsRemove.first().at(2).toInt(), 0);
0167 
0168     QCOMPARE(modelRowsRemove.at(1).at(0).toModelIndex(), personIndex);
0169     QCOMPARE(modelRowsRemove.at(1).at(1).toInt(), 0);
0170     QCOMPARE(modelRowsRemove.at(1).at(2).toInt(), 0);
0171 
0172     // The parent Person should have just invalid index as parent
0173     // (and we don't care about the position)
0174     QCOMPARE(modelRowsRemove.at(2).at(0).toModelIndex(), QModelIndex());
0175 }
0176 
0177 #include "moc_personsmodeltest.cpp"