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

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "resultduplicatetreewidgettest.h"
0008 #include "../searchduplicate/resultduplicatetreewidget.h"
0009 #include <KContacts/Addressee>
0010 #include <QTest>
0011 
0012 using namespace KContacts;
0013 
0014 ResultDuplicateTreeWidgetTest::ResultDuplicateTreeWidgetTest(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 ResultDuplicateTreeWidgetTest::~ResultDuplicateTreeWidgetTest() = default;
0020 
0021 void ResultDuplicateTreeWidgetTest::shouldHaveDefaultValue()
0022 {
0023     KABMergeContacts::ResultDuplicateTreeWidget w;
0024     QCOMPARE(w.topLevelItemCount(), 0);
0025 }
0026 
0027 void ResultDuplicateTreeWidgetTest::shouldFillList()
0028 {
0029     KABMergeContacts::ResultDuplicateTreeWidget w;
0030     Akonadi::Item::List lst;
0031     for (int i = 0; i < 5; ++i) {
0032         Akonadi::Item item(42 + i);
0033         Addressee address;
0034         address.setName(QStringLiteral("foo1"));
0035         item.setPayload<Addressee>(address);
0036         lst << item;
0037     }
0038     QList<Akonadi::Item::List> itemLst;
0039     itemLst << lst;
0040     w.setContacts(itemLst);
0041     QCOMPARE(w.topLevelItemCount(), 1);
0042 }
0043 
0044 void ResultDuplicateTreeWidgetTest::shouldClearList()
0045 {
0046     KABMergeContacts::ResultDuplicateTreeWidget w;
0047     Akonadi::Item::List lst;
0048     for (int i = 0; i < 5; ++i) {
0049         Akonadi::Item item(42 + i);
0050         Addressee address;
0051         address.setName(QStringLiteral("foo1"));
0052         item.setPayload<Addressee>(address);
0053         lst << item;
0054     }
0055     QList<Akonadi::Item::List> itemLst;
0056     itemLst << lst;
0057     w.setContacts(itemLst);
0058 
0059     Akonadi::Item item(45);
0060     Addressee address;
0061     address.setName(QStringLiteral("foo1"));
0062     item.setPayload<Addressee>(address);
0063     lst << item;
0064     itemLst.clear();
0065     itemLst << lst;
0066     w.setContacts(itemLst);
0067     QCOMPARE(w.topLevelItemCount(), 1);
0068 }
0069 
0070 void ResultDuplicateTreeWidgetTest::shouldEmptyListIfNotContactSelected()
0071 {
0072     KABMergeContacts::ResultDuplicateTreeWidget w;
0073     Akonadi::Item::List lst;
0074     for (int i = 0; i < 5; ++i) {
0075         Akonadi::Item item(42 + i);
0076         Addressee address;
0077         address.setName(QStringLiteral("foo1"));
0078         item.setPayload<Addressee>(address);
0079         lst << item;
0080     }
0081     QList<Akonadi::Item::List> itemLst;
0082     Akonadi::Item item(45);
0083     Addressee address;
0084     address.setName(QStringLiteral("foo1"));
0085     item.setPayload<Addressee>(address);
0086     lst << item;
0087     itemLst << lst;
0088     w.setContacts(itemLst);
0089     QVERIFY(w.selectedContactsToMerge().isEmpty());
0090 }
0091 
0092 void ResultDuplicateTreeWidgetTest::shouldReturnNotEmptyContactList()
0093 {
0094     KABMergeContacts::ResultDuplicateTreeWidget w;
0095     Akonadi::Item::List lst;
0096     for (int i = 0; i < 5; ++i) {
0097         Akonadi::Item item(42 + i);
0098         Addressee address;
0099         address.setName(QStringLiteral("foo1"));
0100         item.setPayload<Addressee>(address);
0101         lst << item;
0102     }
0103     QList<Akonadi::Item::List> itemLst;
0104     itemLst << lst;
0105     w.setContacts(itemLst);
0106 
0107     for (int i = 0; i < w.topLevelItemCount(); ++i) {
0108         QTreeWidgetItem *item = w.topLevelItem(i);
0109         const int childCount = item->childCount();
0110         if (childCount > 0) {
0111             for (int child = 0; child < childCount; ++child) {
0112                 auto childItem = static_cast<KABMergeContacts::ResultDuplicateTreeWidgetItem *>(item->child(child));
0113                 childItem->setCheckState(0, Qt::Checked);
0114             }
0115         }
0116     }
0117     QCOMPARE(w.selectedContactsToMerge().count(), 1);
0118 }
0119 
0120 void ResultDuplicateTreeWidgetTest::shouldNotReturnListWhenJustOneChildSelected()
0121 {
0122     KABMergeContacts::ResultDuplicateTreeWidget w;
0123     Akonadi::Item::List lst;
0124     for (int i = 0; i < 5; ++i) {
0125         Akonadi::Item item(42 + i);
0126         Addressee address;
0127         address.setName(QStringLiteral("foo1"));
0128         item.setPayload<Addressee>(address);
0129         lst << item;
0130     }
0131     QList<Akonadi::Item::List> itemLst;
0132     itemLst << lst;
0133     w.setContacts(itemLst);
0134 
0135     for (int i = 0; i < w.topLevelItemCount(); ++i) {
0136         QTreeWidgetItem *item = w.topLevelItem(i);
0137         const int childCount = item->childCount();
0138         if (childCount > 0) {
0139             for (int child = 0; child < childCount; ++child) {
0140                 auto childItem = static_cast<KABMergeContacts::ResultDuplicateTreeWidgetItem *>(item->child(child));
0141                 childItem->setCheckState(0, child == 0 ? Qt::Checked : Qt::Unchecked);
0142             }
0143         }
0144     }
0145     QCOMPARE(w.selectedContactsToMerge().count(), 0);
0146 }
0147 
0148 void ResultDuplicateTreeWidgetTest::shouldReturnTwoLists()
0149 {
0150     KABMergeContacts::ResultDuplicateTreeWidget w;
0151     Akonadi::Item::List lst;
0152     for (int i = 0; i < 5; ++i) {
0153         Akonadi::Item item(42 + i);
0154         Addressee address;
0155         address.setName(QStringLiteral("foo1"));
0156         item.setPayload<Addressee>(address);
0157         lst << item;
0158     }
0159     QList<Akonadi::Item::List> itemLst;
0160     itemLst << lst;
0161     itemLst << lst;
0162     w.setContacts(itemLst);
0163 
0164     for (int i = 0; i < w.topLevelItemCount(); ++i) {
0165         QTreeWidgetItem *item = w.topLevelItem(i);
0166         const int childCount = item->childCount();
0167         if (childCount > 0) {
0168             for (int child = 0; child < childCount; ++child) {
0169                 auto childItem = static_cast<KABMergeContacts::ResultDuplicateTreeWidgetItem *>(item->child(child));
0170                 childItem->setCheckState(0, Qt::Checked);
0171                 QVERIFY(childItem->item().isValid());
0172             }
0173         }
0174     }
0175     QCOMPARE(w.selectedContactsToMerge().count(), 2);
0176 }
0177 
0178 void ResultDuplicateTreeWidgetTest::shouldReturnJustOnList()
0179 {
0180     KABMergeContacts::ResultDuplicateTreeWidget w;
0181     Akonadi::Item::List lst;
0182     for (int i = 0; i < 5; ++i) {
0183         Akonadi::Item item(42 + i);
0184         Addressee address;
0185         address.setName(QStringLiteral("foo1"));
0186         item.setPayload<Addressee>(address);
0187         lst << item;
0188     }
0189     QList<Akonadi::Item::List> itemLst;
0190     itemLst << lst;
0191     itemLst << lst;
0192     w.setContacts(itemLst);
0193 
0194     bool firstList = true;
0195     for (int i = 0; i < w.topLevelItemCount(); ++i) {
0196         QTreeWidgetItem *item = w.topLevelItem(i);
0197         const int childCount = item->childCount();
0198         if (childCount > 0) {
0199             for (int child = 0; child < childCount; ++child) {
0200                 auto childItem = static_cast<KABMergeContacts::ResultDuplicateTreeWidgetItem *>(item->child(child));
0201                 childItem->setCheckState(0, firstList ? Qt::Checked : Qt::Unchecked);
0202             }
0203         }
0204         firstList = false;
0205     }
0206     QCOMPARE(w.selectedContactsToMerge().count(), 1);
0207 }
0208 
0209 QTEST_MAIN(ResultDuplicateTreeWidgetTest)
0210 
0211 #include "moc_resultduplicatetreewidgettest.cpp"