File indexing completed on 2024-11-24 04:43:03

0001 /*
0002    SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "searchpotentialduplicatecontactjobtest.h"
0008 #include "../job/searchpotentialduplicatecontactjob.h"
0009 
0010 #include <Akonadi/Item>
0011 #include <KContacts/Addressee>
0012 #include <QSignalSpy>
0013 #include <QTest>
0014 
0015 using namespace KABMergeContacts;
0016 SearchPotentialDuplicateContactJobTest::SearchPotentialDuplicateContactJobTest()
0017 {
0018     qRegisterMetaType<QList<Akonadi::Item>>();
0019     qRegisterMetaType<QList<Akonadi::Item::List>>();
0020 }
0021 
0022 void SearchPotentialDuplicateContactJobTest::shouldReturnEmptyListWhenNoItem()
0023 {
0024     Akonadi::Item::List lst;
0025     SearchPotentialDuplicateContactJob job(lst);
0026     QSignalSpy spy(&job, &SearchPotentialDuplicateContactJob::finished);
0027     job.start();
0028     QCOMPARE(spy.count(), 1);
0029     auto lstResult = spy.at(0).at(0).value<QList<Akonadi::Item::List>>();
0030     QCOMPARE(lstResult.count(), 0);
0031 }
0032 
0033 void SearchPotentialDuplicateContactJobTest::shouldReturnEmptyListWhenOneItem()
0034 {
0035     Akonadi::Item::List lst;
0036     lst << Akonadi::Item(42);
0037     SearchPotentialDuplicateContactJob job(lst);
0038     QSignalSpy spy(&job, &SearchPotentialDuplicateContactJob::finished);
0039     job.start();
0040     QCOMPARE(spy.count(), 1);
0041     auto lstResult = spy.at(0).at(0).value<QList<Akonadi::Item::List>>();
0042     QCOMPARE(lstResult.count(), 0);
0043 }
0044 
0045 void SearchPotentialDuplicateContactJobTest::shouldReturnListWhenTwoItemsAreDuplicated()
0046 {
0047     Akonadi::Item::List lst;
0048     Akonadi::Item itemA;
0049     KContacts::Addressee address;
0050     address.setName(QStringLiteral("foo1"));
0051     itemA.setPayload<KContacts::Addressee>(address);
0052     itemA.setMimeType(KContacts::Addressee::mimeType());
0053 
0054     lst << itemA << itemA;
0055 
0056     SearchPotentialDuplicateContactJob job(lst);
0057     QSignalSpy spy(&job, &SearchPotentialDuplicateContactJob::finished);
0058     job.start();
0059     QCOMPARE(spy.count(), 1);
0060     auto lstResult = spy.at(0).at(0).value<QList<Akonadi::Item::List>>();
0061     QCOMPARE(lstResult.count(), 1);
0062 }
0063 
0064 void SearchPotentialDuplicateContactJobTest::shouldReturnListWhenThreeItemsAreDuplicated()
0065 {
0066     Akonadi::Item::List lst;
0067     Akonadi::Item itemA;
0068     KContacts::Addressee address;
0069     address.setName(QStringLiteral("foo1"));
0070     itemA.setPayload<KContacts::Addressee>(address);
0071     itemA.setMimeType(KContacts::Addressee::mimeType());
0072 
0073     lst << itemA << itemA << itemA;
0074 
0075     SearchPotentialDuplicateContactJob job(lst);
0076     QSignalSpy spy(&job, &SearchPotentialDuplicateContactJob::finished);
0077     job.start();
0078     QCOMPARE(spy.count(), 1);
0079     auto lstResult = spy.at(0).at(0).value<QList<Akonadi::Item::List>>();
0080     QCOMPARE(lstResult.count(), 1);
0081 }
0082 
0083 void SearchPotentialDuplicateContactJobTest::shouldReturnTwoList()
0084 {
0085     Akonadi::Item::List lst;
0086     Akonadi::Item item;
0087     KContacts::Addressee addressA;
0088     addressA.setName(QStringLiteral("foo1"));
0089     item.setPayload<KContacts::Addressee>(addressA);
0090     item.setMimeType(KContacts::Addressee::mimeType());
0091 
0092     lst << item << item << item;
0093 
0094     KContacts::Addressee addressB;
0095     addressB.setName(QStringLiteral("foo2"));
0096     item.setPayload<KContacts::Addressee>(addressB);
0097     item.setMimeType(KContacts::Addressee::mimeType());
0098 
0099     lst << item << item << item;
0100 
0101     SearchPotentialDuplicateContactJob job(lst);
0102     QSignalSpy spy(&job, &SearchPotentialDuplicateContactJob::finished);
0103     job.start();
0104     QCOMPARE(spy.count(), 1);
0105     auto lstResult = spy.at(0).at(0).value<QList<Akonadi::Item::List>>();
0106     QCOMPARE(lstResult.count(), 2);
0107 }
0108 
0109 void SearchPotentialDuplicateContactJobTest::shouldReturnList_data()
0110 {
0111     QTest::addColumn<Akonadi::Item::List>("listItem");
0112     QTest::addColumn<int>("numberOfList");
0113     QTest::newRow("noList") << Akonadi::Item::List() << 0;
0114     Akonadi::Item itemA;
0115     KContacts::Addressee addressA;
0116     addressA.setName(QStringLiteral("foo1"));
0117     itemA.setPayload<KContacts::Addressee>(addressA);
0118     itemA.setMimeType(KContacts::Addressee::mimeType());
0119 
0120     Akonadi::Item itemB;
0121 
0122     KContacts::Addressee addressB;
0123     addressB.setName(QStringLiteral("foo2"));
0124     itemB.setPayload<KContacts::Addressee>(addressB);
0125     itemB.setMimeType(KContacts::Addressee::mimeType());
0126 
0127     Akonadi::Item::List lst;
0128     lst << itemA;
0129     QTest::newRow("oneItem") << lst << 0;
0130 
0131     lst.clear();
0132     lst << itemA << itemA;
0133     QTest::newRow("oneDuplicate") << lst << 1;
0134 
0135     lst.clear();
0136     lst << itemB << itemA;
0137     QTest::newRow("twoDifferentItem") << lst << 0;
0138 
0139     lst.clear();
0140     lst << itemB << itemA << itemA << itemA;
0141     QTest::newRow("onDuplicate") << lst << 1;
0142 
0143     lst.clear();
0144     lst << itemB << itemA << itemA << itemB;
0145     QTest::newRow("twoDuplicate") << lst << 2;
0146 
0147     Akonadi::Item itemC;
0148 
0149     KContacts::Addressee addressC;
0150     addressC.setName(QStringLiteral("foo3"));
0151     itemC.setPayload<KContacts::Addressee>(addressC);
0152     itemC.setMimeType(KContacts::Addressee::mimeType());
0153 
0154     lst.clear();
0155     lst << itemB << itemC << itemA << itemA << itemB << itemC;
0156     QTest::newRow("threeDuplicate") << lst << 3;
0157 
0158     lst.clear();
0159     lst << itemB << itemC << itemA;
0160     QTest::newRow("threeDifferent") << lst << 0;
0161 }
0162 
0163 void SearchPotentialDuplicateContactJobTest::shouldReturnList()
0164 {
0165     QFETCH(Akonadi::Item::List, listItem);
0166     QFETCH(int, numberOfList);
0167 
0168     SearchPotentialDuplicateContactJob job(listItem);
0169     QSignalSpy spy(&job, &SearchPotentialDuplicateContactJob::finished);
0170     job.start();
0171     QCOMPARE(spy.count(), 1);
0172     auto lstResult = spy.at(0).at(0).value<QList<Akonadi::Item::List>>();
0173     QCOMPARE(lstResult.count(), numberOfList);
0174 }
0175 
0176 QTEST_MAIN(SearchPotentialDuplicateContactJobTest)
0177 
0178 #include "moc_searchpotentialduplicatecontactjobtest.cpp"