1 /*
2     This file is part of the KContacts framework.
3     SPDX-FileCopyrightText: 2008 Kevin Krammer <kevin.krammer@gmx.at>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include <QTest>
9 
10 #include "kcontacts/contactgroup.h"
11 #include "kcontacts/contactgrouptool.h"
12 
13 #include <QBuffer>
14 
15 using namespace KContacts;
16 
17 class ContactGroupTest : public QObject
18 {
19     Q_OBJECT
20 
21 private Q_SLOTS:
22     void contactGroupContactReference();
23     void contactGroupContactGroupReference();
24     void contactGroupData();
25     void contactGroup();
26     void testGroupRoundTrip();
27     void testGroupListRoundTrip();
28 };
29 
QTEST_MAIN(ContactGroupTest)30 QTEST_MAIN(ContactGroupTest)
31 
32 void ContactGroupTest::contactGroupContactReference()
33 {
34     const QLatin1String uid("MyReferenceId");
35     const QLatin1String gid("GID");
36     const QLatin1String preferredEMail("tokoe@kde.org");
37     const QLatin1String customKey("MyCustomKey");
38     const QLatin1String customValue("MyCustomValue");
39 
40     // uid test
41     {
42         ContactGroup::ContactReference ref(uid);
43         QCOMPARE(ref.uid(), uid);
44     }
45 
46     // uid test
47     {
48         ContactGroup::ContactReference ref;
49         ref.setUid(uid);
50         QCOMPARE(ref.uid(), uid);
51     }
52 
53     // preferredEmail test
54     {
55         ContactGroup::ContactReference ref(uid);
56         ref.setPreferredEmail(preferredEMail);
57         QCOMPARE(ref.preferredEmail(), preferredEMail);
58     }
59 
60     // custom test
61     {
62         ContactGroup::ContactReference ref(uid);
63         ref.insertCustom(customKey, customValue);
64         QCOMPARE(ref.custom(customKey), customValue);
65         ref.removeCustom(customKey);
66         QCOMPARE(ref.custom(customKey), QString());
67     }
68 
69     // assignment test
70     {
71         ContactGroup::ContactReference ref(uid);
72         ref.setGid(gid);
73         ref.setPreferredEmail(preferredEMail);
74         ref.insertCustom(customKey, customValue);
75 
76         ContactGroup::ContactReference ref2(ref);
77         QCOMPARE(ref.uid(), ref2.uid());
78         QCOMPARE(ref.gid(), ref2.gid());
79         QCOMPARE(ref.preferredEmail(), ref2.preferredEmail());
80         QCOMPARE(ref.custom(customKey), ref2.custom(customKey));
81 
82         QVERIFY(ref == ref2);
83     }
84 
85     // const test
86     {
87         ContactGroup::ContactReference ref(uid);
88         ref.setPreferredEmail(preferredEMail);
89         ref.insertCustom(customKey, customValue);
90 
91         const ContactGroup::ContactReference constRef(ref);
92         QCOMPARE(constRef.uid(), uid);
93         QCOMPARE(constRef.preferredEmail(), preferredEMail);
94         QCOMPARE(constRef.custom(customKey), customValue);
95         QVERIFY(constRef.gid().isEmpty());
96     }
97 }
98 
contactGroupContactGroupReference()99 void ContactGroupTest::contactGroupContactGroupReference()
100 {
101     const QLatin1String uid("MyReferenceId");
102     const QLatin1String customKey("MyCustomKey");
103     const QLatin1String customValue("MyCustomValue");
104 
105     // uid test
106     {
107         ContactGroup::ContactGroupReference ref(uid);
108         QCOMPARE(ref.uid(), uid);
109     }
110 
111     // uid test
112     {
113         ContactGroup::ContactGroupReference ref;
114         ref.setUid(uid);
115         QCOMPARE(ref.uid(), uid);
116     }
117 
118     // custom test
119     {
120         ContactGroup::ContactGroupReference ref(uid);
121         ref.insertCustom(customKey, customValue);
122         QCOMPARE(ref.custom(customKey), customValue);
123         ref.removeCustom(customKey);
124         QCOMPARE(ref.custom(customKey), QString());
125     }
126 
127     // assignment test
128     {
129         ContactGroup::ContactGroupReference ref(uid);
130         ref.insertCustom(customKey, customValue);
131 
132         ContactGroup::ContactGroupReference ref2(ref);
133         QCOMPARE(ref.uid(), ref2.uid());
134         QCOMPARE(ref.custom(customKey), ref2.custom(customKey));
135 
136         QVERIFY(ref == ref2);
137     }
138 
139     // const test
140     {
141         ContactGroup::ContactGroupReference ref(uid);
142         ref.insertCustom(customKey, customValue);
143 
144         const ContactGroup::ContactGroupReference constRef(ref);
145         constRef.uid();
146         constRef.custom(customKey);
147     }
148 }
149 
contactGroupData()150 void ContactGroupTest::contactGroupData()
151 {
152     const QLatin1String name("Tobias Koenig");
153     const QLatin1String email("tokoe@kde.org");
154     const QLatin1String customKey("MyCustomKey");
155     const QLatin1String customValue("MyCustomValue");
156 
157     // name/email test
158     {
159         ContactGroup::Data data(name, email);
160         QCOMPARE(data.name(), name);
161         QCOMPARE(data.email(), email);
162     }
163 
164     // name test
165     {
166         ContactGroup::Data data;
167         data.setName(name);
168         QCOMPARE(data.name(), name);
169     }
170 
171     // email test
172     {
173         ContactGroup::Data data;
174         data.setEmail(email);
175         QCOMPARE(data.email(), email);
176     }
177 
178     // custom test
179     {
180         ContactGroup::Data data(name, email);
181         data.insertCustom(customKey, customValue);
182         QCOMPARE(data.custom(customKey), customValue);
183         data.removeCustom(customKey);
184         QCOMPARE(data.custom(customKey), QString());
185     }
186 
187     // assignment test
188     {
189         ContactGroup::Data data(name, email);
190         data.insertCustom(customKey, customValue);
191 
192         ContactGroup::Data data2(data);
193         QCOMPARE(data.name(), data2.name());
194         QCOMPARE(data.email(), data2.email());
195         QCOMPARE(data.custom(customKey), data2.custom(customKey));
196 
197         QVERIFY(data == data2);
198     }
199 
200     // const test
201     {
202         ContactGroup::Data data(name, email);
203         data.insertCustom(customKey, customValue);
204 
205         const ContactGroup::Data constData(data);
206         QCOMPARE(constData.name(), name);
207         QCOMPARE(constData.email(), email);
208         QCOMPARE(constData.custom(customKey), customValue);
209     }
210 }
211 
contactGroup()212 void ContactGroupTest::contactGroup()
213 {
214     const QLatin1String groupName("MyGroupName");
215     const QLatin1String groupId("MyGroupID");
216     const QLatin1String name("Tobias Koenig");
217     const QLatin1String email("tokoe@kde.org");
218     const QLatin1String uid("MyUid");
219 
220     // name test
221     {
222         ContactGroup group(groupName);
223         QCOMPARE(group.name(), groupName);
224     }
225 
226     // id test
227     {
228         ContactGroup group(groupName);
229         group.setId(groupId);
230         QCOMPARE(group.id(), groupId);
231     }
232 
233     // contact reference test
234     {
235         ContactGroup group(groupName);
236         QCOMPARE(group.contactReferenceCount(), (unsigned int)0);
237 
238         ContactGroup::ContactReference ref(uid);
239         ref.setPreferredEmail(email);
240 
241         group.append(ref);
242         QCOMPARE(group.contactReferenceCount(), (unsigned int)1);
243 
244         const ContactGroup::ContactReference ref2 = group.contactReference(0);
245         QCOMPARE(ref, ref2);
246 
247         group.remove(ref);
248         QCOMPARE(group.contactReferenceCount(), (unsigned int)0);
249     }
250 
251     // contact group reference test
252     {
253         ContactGroup group(groupName);
254         QCOMPARE(group.contactGroupReferenceCount(), (unsigned int)0);
255 
256         ContactGroup::ContactGroupReference ref(uid);
257 
258         group.append(ref);
259         QCOMPARE(group.contactGroupReferenceCount(), (unsigned int)1);
260 
261         const ContactGroup::ContactGroupReference ref2 = group.contactGroupReference(0);
262         QCOMPARE(ref, ref2);
263 
264         group.remove(ref);
265         QCOMPARE(group.contactGroupReferenceCount(), (unsigned int)0);
266     }
267 
268     // data test
269     {
270         ContactGroup group(groupName);
271         QCOMPARE(group.dataCount(), (unsigned int)0);
272 
273         ContactGroup::Data data(name, email);
274 
275         group.append(data);
276         QCOMPARE(group.dataCount(), (unsigned int)1);
277 
278         const ContactGroup::Data data2 = group.data(0);
279         QCOMPARE(data, data2);
280 
281         group.remove(data);
282         QCOMPARE(group.dataCount(), (unsigned int)0);
283     }
284 
285     // mimetype test
286     {
287         ContactGroup group(groupName);
288         QCOMPARE(group.mimeType(), QLatin1String("application/x-vnd.kde.contactgroup"));
289     }
290 }
291 
testGroupRoundTrip()292 void ContactGroupTest::testGroupRoundTrip()
293 {
294     // TODO should also test empty group
295 
296     ContactGroup group(QLatin1String("TestGroup"));
297     group.append(ContactGroup::ContactReference(QLatin1String("Xggdjetw")));
298     ContactGroup::ContactReference gidReference;
299     gidReference.setGid(QLatin1String("gid"));
300     group.append(gidReference);
301     group.append(ContactGroup::ContactGroupReference(QLatin1String("aaXggdjetw")));
302     group.append(ContactGroup::Data(QLatin1String("Tobias Koenig"), QLatin1String("tokoe@kde.org")));
303     group.append(ContactGroup::Data(QLatin1String("Kevin Krammer"), QLatin1String("kevin.krammer@gmx.at")));
304 
305     QBuffer buffer;
306     buffer.open(QIODevice::WriteOnly);
307 
308     QString errorMessage;
309     bool result = ContactGroupTool::convertToXml(group, &buffer, &errorMessage);
310 
311     QVERIFY(result);
312     QVERIFY(errorMessage.isEmpty());
313     buffer.close();
314     QVERIFY(buffer.size() > 0);
315     buffer.open(QIODevice::ReadOnly);
316 
317     ContactGroup group2;
318     result = ContactGroupTool::convertFromXml(&buffer, group2, &errorMessage);
319     QVERIFY(result);
320     QVERIFY(errorMessage.isEmpty());
321     QCOMPARE(group, group2);
322 }
323 
testGroupListRoundTrip()324 void ContactGroupTest::testGroupListRoundTrip()
325 {
326     // TODO should also test empty list
327 
328     ContactGroup::List list;
329 
330     ContactGroup group1(QLatin1String("TestGroup1"));
331     group1.append(ContactGroup::ContactReference(QLatin1String("Xggdjetw")));
332     group1.append(ContactGroup::Data(QLatin1String("Tobias Koenig"), QLatin1String("tokoe@kde.org")));
333     group1.append(ContactGroup::Data(QLatin1String("Kevin Krammer"), QLatin1String("kevin.krammer@gmx.at")));
334 
335     list.append(group1);
336 
337     ContactGroup group2(QLatin1String("TestGroup2"));
338     group2.append(ContactGroup::ContactReference(QLatin1String("Xggdjetw")));
339     group2.append(ContactGroup::Data(QLatin1String("Tobias Koenig"), QLatin1String("tokoe@kde.org")));
340     group2.append(ContactGroup::Data(QLatin1String("Kevin Krammer"), QLatin1String("kevin.krammer@gmx.at")));
341 
342     list.append(group2);
343 
344     QBuffer buffer;
345     buffer.open(QIODevice::WriteOnly);
346 
347     QString errorMessage;
348     bool result = ContactGroupTool::convertToXml(list, &buffer, &errorMessage);
349 
350     QVERIFY(result);
351     QVERIFY(errorMessage.isEmpty());
352     buffer.close();
353     QVERIFY(buffer.size() > 0);
354 
355     buffer.open(QIODevice::ReadOnly);
356 
357     ContactGroup::List list2;
358     result = ContactGroupTool::convertFromXml(&buffer, list2, &errorMessage);
359     QVERIFY(result);
360     QVERIFY(errorMessage.isEmpty());
361     QVERIFY(list2.size() == 2);
362     QCOMPARE(list2[0], group1);
363     QCOMPARE(list2[1], group2);
364 }
365 
366 #include "contactgrouptest.moc"
367