1 /*
2 SPDX-FileCopyrightText: 2015-2021 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "resultduplicatetreewidgettest.h"
8 #include "../searchduplicate/resultduplicatetreewidget.h"
9 #include <KContacts/Addressee>
10 #include <QTest>
11
12 using namespace KContacts;
13
ResultDuplicateTreeWidgetTest(QObject * parent)14 ResultDuplicateTreeWidgetTest::ResultDuplicateTreeWidgetTest(QObject *parent)
15 : QObject(parent)
16 {
17 }
18
~ResultDuplicateTreeWidgetTest()19 ResultDuplicateTreeWidgetTest::~ResultDuplicateTreeWidgetTest()
20 {
21 }
22
shouldHaveDefaultValue()23 void ResultDuplicateTreeWidgetTest::shouldHaveDefaultValue()
24 {
25 KABMergeContacts::ResultDuplicateTreeWidget w;
26 QCOMPARE(w.topLevelItemCount(), 0);
27 }
28
shouldFillList()29 void ResultDuplicateTreeWidgetTest::shouldFillList()
30 {
31 KABMergeContacts::ResultDuplicateTreeWidget w;
32 Akonadi::Item::List lst;
33 for (int i = 0; i < 5; ++i) {
34 Akonadi::Item item(42 + i);
35 Addressee address;
36 address.setName(QStringLiteral("foo1"));
37 item.setPayload<Addressee>(address);
38 lst << item;
39 }
40 QVector<Akonadi::Item::List> itemLst;
41 itemLst << lst;
42 w.setContacts(itemLst);
43 QCOMPARE(w.topLevelItemCount(), 1);
44 }
45
shouldClearList()46 void ResultDuplicateTreeWidgetTest::shouldClearList()
47 {
48 KABMergeContacts::ResultDuplicateTreeWidget w;
49 Akonadi::Item::List lst;
50 for (int i = 0; i < 5; ++i) {
51 Akonadi::Item item(42 + i);
52 Addressee address;
53 address.setName(QStringLiteral("foo1"));
54 item.setPayload<Addressee>(address);
55 lst << item;
56 }
57 QVector<Akonadi::Item::List> itemLst;
58 itemLst << lst;
59 w.setContacts(itemLst);
60
61 Akonadi::Item item(45);
62 Addressee address;
63 address.setName(QStringLiteral("foo1"));
64 item.setPayload<Addressee>(address);
65 lst << item;
66 itemLst.clear();
67 itemLst << lst;
68 w.setContacts(itemLst);
69 QCOMPARE(w.topLevelItemCount(), 1);
70 }
71
shouldEmptyListIfNotContactSelected()72 void ResultDuplicateTreeWidgetTest::shouldEmptyListIfNotContactSelected()
73 {
74 KABMergeContacts::ResultDuplicateTreeWidget w;
75 Akonadi::Item::List lst;
76 for (int i = 0; i < 5; ++i) {
77 Akonadi::Item item(42 + i);
78 Addressee address;
79 address.setName(QStringLiteral("foo1"));
80 item.setPayload<Addressee>(address);
81 lst << item;
82 }
83 QVector<Akonadi::Item::List> itemLst;
84 Akonadi::Item item(45);
85 Addressee address;
86 address.setName(QStringLiteral("foo1"));
87 item.setPayload<Addressee>(address);
88 lst << item;
89 itemLst << lst;
90 w.setContacts(itemLst);
91 QVERIFY(w.selectedContactsToMerge().isEmpty());
92 }
93
shouldReturnNotEmptyContactList()94 void ResultDuplicateTreeWidgetTest::shouldReturnNotEmptyContactList()
95 {
96 KABMergeContacts::ResultDuplicateTreeWidget w;
97 Akonadi::Item::List lst;
98 for (int i = 0; i < 5; ++i) {
99 Akonadi::Item item(42 + i);
100 Addressee address;
101 address.setName(QStringLiteral("foo1"));
102 item.setPayload<Addressee>(address);
103 lst << item;
104 }
105 QVector<Akonadi::Item::List> itemLst;
106 itemLst << lst;
107 w.setContacts(itemLst);
108
109 for (int i = 0; i < w.topLevelItemCount(); ++i) {
110 QTreeWidgetItem *item = w.topLevelItem(i);
111 const int childCount = item->childCount();
112 if (childCount > 0) {
113 for (int child = 0; child < childCount; ++child) {
114 auto childItem = static_cast<KABMergeContacts::ResultDuplicateTreeWidgetItem *>(item->child(child));
115 childItem->setCheckState(0, Qt::Checked);
116 }
117 }
118 }
119 QCOMPARE(w.selectedContactsToMerge().count(), 1);
120 }
121
shouldNotReturnListWhenJustOneChildSelected()122 void ResultDuplicateTreeWidgetTest::shouldNotReturnListWhenJustOneChildSelected()
123 {
124 KABMergeContacts::ResultDuplicateTreeWidget w;
125 Akonadi::Item::List lst;
126 for (int i = 0; i < 5; ++i) {
127 Akonadi::Item item(42 + i);
128 Addressee address;
129 address.setName(QStringLiteral("foo1"));
130 item.setPayload<Addressee>(address);
131 lst << item;
132 }
133 QVector<Akonadi::Item::List> itemLst;
134 itemLst << lst;
135 w.setContacts(itemLst);
136
137 for (int i = 0; i < w.topLevelItemCount(); ++i) {
138 QTreeWidgetItem *item = w.topLevelItem(i);
139 const int childCount = item->childCount();
140 if (childCount > 0) {
141 for (int child = 0; child < childCount; ++child) {
142 auto childItem = static_cast<KABMergeContacts::ResultDuplicateTreeWidgetItem *>(item->child(child));
143 childItem->setCheckState(0, child == 0 ? Qt::Checked : Qt::Unchecked);
144 }
145 }
146 }
147 QCOMPARE(w.selectedContactsToMerge().count(), 0);
148 }
149
shouldReturnTwoLists()150 void ResultDuplicateTreeWidgetTest::shouldReturnTwoLists()
151 {
152 KABMergeContacts::ResultDuplicateTreeWidget w;
153 Akonadi::Item::List lst;
154 for (int i = 0; i < 5; ++i) {
155 Akonadi::Item item(42 + i);
156 Addressee address;
157 address.setName(QStringLiteral("foo1"));
158 item.setPayload<Addressee>(address);
159 lst << item;
160 }
161 QVector<Akonadi::Item::List> itemLst;
162 itemLst << lst;
163 itemLst << lst;
164 w.setContacts(itemLst);
165
166 for (int i = 0; i < w.topLevelItemCount(); ++i) {
167 QTreeWidgetItem *item = w.topLevelItem(i);
168 const int childCount = item->childCount();
169 if (childCount > 0) {
170 for (int child = 0; child < childCount; ++child) {
171 auto childItem = static_cast<KABMergeContacts::ResultDuplicateTreeWidgetItem *>(item->child(child));
172 childItem->setCheckState(0, Qt::Checked);
173 QVERIFY(childItem->item().isValid());
174 }
175 }
176 }
177 QCOMPARE(w.selectedContactsToMerge().count(), 2);
178 }
179
shouldReturnJustOnList()180 void ResultDuplicateTreeWidgetTest::shouldReturnJustOnList()
181 {
182 KABMergeContacts::ResultDuplicateTreeWidget w;
183 Akonadi::Item::List lst;
184 for (int i = 0; i < 5; ++i) {
185 Akonadi::Item item(42 + i);
186 Addressee address;
187 address.setName(QStringLiteral("foo1"));
188 item.setPayload<Addressee>(address);
189 lst << item;
190 }
191 QVector<Akonadi::Item::List> itemLst;
192 itemLst << lst;
193 itemLst << lst;
194 w.setContacts(itemLst);
195
196 bool firstList = true;
197 for (int i = 0; i < w.topLevelItemCount(); ++i) {
198 QTreeWidgetItem *item = w.topLevelItem(i);
199 const int childCount = item->childCount();
200 if (childCount > 0) {
201 for (int child = 0; child < childCount; ++child) {
202 auto childItem = static_cast<KABMergeContacts::ResultDuplicateTreeWidgetItem *>(item->child(child));
203 childItem->setCheckState(0, firstList ? Qt::Checked : Qt::Unchecked);
204 }
205 }
206 firstList = false;
207 }
208 QCOMPARE(w.selectedContactsToMerge().count(), 1);
209 }
210
211 QTEST_MAIN(ResultDuplicateTreeWidgetTest)
212