File indexing completed on 2024-04-14 03:51:18

0001 /*
0002     This file is part of the KContacts framework.
0003     SPDX-FileCopyrightText: 2008 Kevin Krammer <kevin.krammer@gmx.at>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <QTest>
0009 
0010 #include "kcontacts/contactgroup.h"
0011 #include "kcontacts/contactgrouptool.h"
0012 
0013 #include <QBuffer>
0014 
0015 using namespace KContacts;
0016 
0017 class ContactGroupTest : public QObject
0018 {
0019     Q_OBJECT
0020 
0021 private Q_SLOTS:
0022     void contactGroupContactReference();
0023     void contactGroupContactGroupReference();
0024     void contactGroupData();
0025     void contactGroup();
0026     void testGroupRoundTrip();
0027     void testGroupListRoundTrip();
0028 };
0029 
0030 QTEST_MAIN(ContactGroupTest)
0031 
0032 void ContactGroupTest::contactGroupContactReference()
0033 {
0034     const QLatin1String uid("MyReferenceId");
0035     const QLatin1String gid("GID");
0036     const QLatin1String preferredEMail("tokoe@kde.org");
0037     const QLatin1String customKey("MyCustomKey");
0038     const QLatin1String customValue("MyCustomValue");
0039 
0040     // uid test
0041     {
0042         ContactGroup::ContactReference ref(uid);
0043         QCOMPARE(ref.uid(), uid);
0044     }
0045 
0046     // uid test
0047     {
0048         ContactGroup::ContactReference ref;
0049         ref.setUid(uid);
0050         QCOMPARE(ref.uid(), uid);
0051     }
0052 
0053     // preferredEmail test
0054     {
0055         ContactGroup::ContactReference ref(uid);
0056         ref.setPreferredEmail(preferredEMail);
0057         QCOMPARE(ref.preferredEmail(), preferredEMail);
0058     }
0059 
0060     // custom test
0061     {
0062         ContactGroup::ContactReference ref(uid);
0063         ref.insertCustom(customKey, customValue);
0064         QCOMPARE(ref.custom(customKey), customValue);
0065         ref.removeCustom(customKey);
0066         QCOMPARE(ref.custom(customKey), QString());
0067     }
0068 
0069     // assignment test
0070     {
0071         ContactGroup::ContactReference ref(uid);
0072         ref.setGid(gid);
0073         ref.setPreferredEmail(preferredEMail);
0074         ref.insertCustom(customKey, customValue);
0075 
0076         ContactGroup::ContactReference ref2(ref);
0077         QCOMPARE(ref.uid(), ref2.uid());
0078         QCOMPARE(ref.gid(), ref2.gid());
0079         QCOMPARE(ref.preferredEmail(), ref2.preferredEmail());
0080         QCOMPARE(ref.custom(customKey), ref2.custom(customKey));
0081 
0082         QVERIFY(ref == ref2);
0083     }
0084 
0085     // const test
0086     {
0087         ContactGroup::ContactReference ref(uid);
0088         ref.setPreferredEmail(preferredEMail);
0089         ref.insertCustom(customKey, customValue);
0090 
0091         const ContactGroup::ContactReference constRef(ref);
0092         QCOMPARE(constRef.uid(), uid);
0093         QCOMPARE(constRef.preferredEmail(), preferredEMail);
0094         QCOMPARE(constRef.custom(customKey), customValue);
0095         QVERIFY(constRef.gid().isEmpty());
0096     }
0097 }
0098 
0099 void ContactGroupTest::contactGroupContactGroupReference()
0100 {
0101     const QLatin1String uid("MyReferenceId");
0102     const QLatin1String customKey("MyCustomKey");
0103     const QLatin1String customValue("MyCustomValue");
0104 
0105     // uid test
0106     {
0107         ContactGroup::ContactGroupReference ref(uid);
0108         QCOMPARE(ref.uid(), uid);
0109     }
0110 
0111     // uid test
0112     {
0113         ContactGroup::ContactGroupReference ref;
0114         ref.setUid(uid);
0115         QCOMPARE(ref.uid(), uid);
0116     }
0117 
0118     // custom test
0119     {
0120         ContactGroup::ContactGroupReference ref(uid);
0121         ref.insertCustom(customKey, customValue);
0122         QCOMPARE(ref.custom(customKey), customValue);
0123         ref.removeCustom(customKey);
0124         QCOMPARE(ref.custom(customKey), QString());
0125     }
0126 
0127     // assignment test
0128     {
0129         ContactGroup::ContactGroupReference ref(uid);
0130         ref.insertCustom(customKey, customValue);
0131 
0132         ContactGroup::ContactGroupReference ref2(ref);
0133         QCOMPARE(ref.uid(), ref2.uid());
0134         QCOMPARE(ref.custom(customKey), ref2.custom(customKey));
0135 
0136         QVERIFY(ref == ref2);
0137     }
0138 
0139     // const test
0140     {
0141         ContactGroup::ContactGroupReference ref(uid);
0142         ref.insertCustom(customKey, customValue);
0143 
0144         const ContactGroup::ContactGroupReference constRef(ref);
0145         constRef.uid();
0146         constRef.custom(customKey);
0147     }
0148 }
0149 
0150 void ContactGroupTest::contactGroupData()
0151 {
0152     const QLatin1String name("Tobias Koenig");
0153     const QLatin1String email("tokoe@kde.org");
0154     const QLatin1String customKey("MyCustomKey");
0155     const QLatin1String customValue("MyCustomValue");
0156 
0157     // name/email test
0158     {
0159         ContactGroup::Data data(name, email);
0160         QCOMPARE(data.name(), name);
0161         QCOMPARE(data.email(), email);
0162     }
0163 
0164     // name test
0165     {
0166         ContactGroup::Data data;
0167         data.setName(name);
0168         QCOMPARE(data.name(), name);
0169     }
0170 
0171     // email test
0172     {
0173         ContactGroup::Data data;
0174         data.setEmail(email);
0175         QCOMPARE(data.email(), email);
0176     }
0177 
0178     // custom test
0179     {
0180         ContactGroup::Data data(name, email);
0181         data.insertCustom(customKey, customValue);
0182         QCOMPARE(data.custom(customKey), customValue);
0183         data.removeCustom(customKey);
0184         QCOMPARE(data.custom(customKey), QString());
0185     }
0186 
0187     // assignment test
0188     {
0189         ContactGroup::Data data(name, email);
0190         data.insertCustom(customKey, customValue);
0191 
0192         ContactGroup::Data data2(data);
0193         QCOMPARE(data.name(), data2.name());
0194         QCOMPARE(data.email(), data2.email());
0195         QCOMPARE(data.custom(customKey), data2.custom(customKey));
0196 
0197         QVERIFY(data == data2);
0198     }
0199 
0200     // const test
0201     {
0202         ContactGroup::Data data(name, email);
0203         data.insertCustom(customKey, customValue);
0204 
0205         const ContactGroup::Data constData(data);
0206         QCOMPARE(constData.name(), name);
0207         QCOMPARE(constData.email(), email);
0208         QCOMPARE(constData.custom(customKey), customValue);
0209     }
0210 }
0211 
0212 void ContactGroupTest::contactGroup()
0213 {
0214     const QLatin1String groupName("MyGroupName");
0215     const QLatin1String groupId("MyGroupID");
0216     const QLatin1String name("Tobias Koenig");
0217     const QLatin1String email("tokoe@kde.org");
0218     const QLatin1String uid("MyUid");
0219 
0220     // name test
0221     {
0222         ContactGroup group(groupName);
0223         QCOMPARE(group.name(), groupName);
0224     }
0225 
0226     // id test
0227     {
0228         ContactGroup group(groupName);
0229         group.setId(groupId);
0230         QCOMPARE(group.id(), groupId);
0231     }
0232 
0233     // contact reference test
0234     {
0235         ContactGroup group(groupName);
0236         QCOMPARE(group.contactReferenceCount(), (unsigned int)0);
0237 
0238         ContactGroup::ContactReference ref(uid);
0239         ref.setPreferredEmail(email);
0240 
0241         group.append(ref);
0242         QCOMPARE(group.contactReferenceCount(), (unsigned int)1);
0243 
0244         const ContactGroup::ContactReference ref2 = group.contactReference(0);
0245         QCOMPARE(ref, ref2);
0246 
0247         group.remove(ref);
0248         QCOMPARE(group.contactReferenceCount(), (unsigned int)0);
0249     }
0250 
0251     // contact group reference test
0252     {
0253         ContactGroup group(groupName);
0254         QCOMPARE(group.contactGroupReferenceCount(), (unsigned int)0);
0255 
0256         ContactGroup::ContactGroupReference ref(uid);
0257 
0258         group.append(ref);
0259         QCOMPARE(group.contactGroupReferenceCount(), (unsigned int)1);
0260 
0261         const ContactGroup::ContactGroupReference ref2 = group.contactGroupReference(0);
0262         QCOMPARE(ref, ref2);
0263 
0264         group.remove(ref);
0265         QCOMPARE(group.contactGroupReferenceCount(), (unsigned int)0);
0266     }
0267 
0268     // data test
0269     {
0270         ContactGroup group(groupName);
0271         QCOMPARE(group.dataCount(), (unsigned int)0);
0272 
0273         ContactGroup::Data data(name, email);
0274 
0275         group.append(data);
0276         QCOMPARE(group.dataCount(), (unsigned int)1);
0277 
0278         const ContactGroup::Data data2 = group.data(0);
0279         QCOMPARE(data, data2);
0280 
0281         group.remove(data);
0282         QCOMPARE(group.dataCount(), (unsigned int)0);
0283     }
0284 
0285     // mimetype test
0286     {
0287         ContactGroup group(groupName);
0288         QCOMPARE(group.mimeType(), QLatin1String("application/x-vnd.kde.contactgroup"));
0289     }
0290 }
0291 
0292 void ContactGroupTest::testGroupRoundTrip()
0293 {
0294     // TODO should also test empty group
0295 
0296     ContactGroup group(QLatin1String("TestGroup"));
0297     group.append(ContactGroup::ContactReference(QLatin1String("Xggdjetw")));
0298     ContactGroup::ContactReference gidReference;
0299     gidReference.setGid(QLatin1String("gid"));
0300     group.append(gidReference);
0301     group.append(ContactGroup::ContactGroupReference(QLatin1String("aaXggdjetw")));
0302     group.append(ContactGroup::Data(QLatin1String("Tobias Koenig"), QLatin1String("tokoe@kde.org")));
0303     group.append(ContactGroup::Data(QLatin1String("Kevin Krammer"), QLatin1String("kevin.krammer@gmx.at")));
0304 
0305     QBuffer buffer;
0306     buffer.open(QIODevice::WriteOnly);
0307 
0308     QString errorMessage;
0309     bool result = ContactGroupTool::convertToXml(group, &buffer, &errorMessage);
0310 
0311     QVERIFY(result);
0312     QVERIFY(errorMessage.isEmpty());
0313     buffer.close();
0314     QVERIFY(buffer.size() > 0);
0315     buffer.open(QIODevice::ReadOnly);
0316 
0317     ContactGroup group2;
0318     result = ContactGroupTool::convertFromXml(&buffer, group2, &errorMessage);
0319     QVERIFY(result);
0320     QVERIFY(errorMessage.isEmpty());
0321     QCOMPARE(group, group2);
0322 }
0323 
0324 void ContactGroupTest::testGroupListRoundTrip()
0325 {
0326     // TODO should also test empty list
0327 
0328     ContactGroup::List list;
0329 
0330     ContactGroup group1(QLatin1String("TestGroup1"));
0331     group1.append(ContactGroup::ContactReference(QLatin1String("Xggdjetw")));
0332     group1.append(ContactGroup::Data(QLatin1String("Tobias Koenig"), QLatin1String("tokoe@kde.org")));
0333     group1.append(ContactGroup::Data(QLatin1String("Kevin Krammer"), QLatin1String("kevin.krammer@gmx.at")));
0334 
0335     list.append(group1);
0336 
0337     ContactGroup group2(QLatin1String("TestGroup2"));
0338     group2.append(ContactGroup::ContactReference(QLatin1String("Xggdjetw")));
0339     group2.append(ContactGroup::Data(QLatin1String("Tobias Koenig"), QLatin1String("tokoe@kde.org")));
0340     group2.append(ContactGroup::Data(QLatin1String("Kevin Krammer"), QLatin1String("kevin.krammer@gmx.at")));
0341 
0342     list.append(group2);
0343 
0344     QBuffer buffer;
0345     buffer.open(QIODevice::WriteOnly);
0346 
0347     QString errorMessage;
0348     bool result = ContactGroupTool::convertToXml(list, &buffer, &errorMessage);
0349 
0350     QVERIFY(result);
0351     QVERIFY(errorMessage.isEmpty());
0352     buffer.close();
0353     QVERIFY(buffer.size() > 0);
0354 
0355     buffer.open(QIODevice::ReadOnly);
0356 
0357     ContactGroup::List list2;
0358     result = ContactGroupTool::convertFromXml(&buffer, list2, &errorMessage);
0359     QVERIFY(result);
0360     QVERIFY(errorMessage.isEmpty());
0361     QVERIFY(list2.size() == 2);
0362     QCOMPARE(list2[0], group1);
0363     QCOMPARE(list2[1], group2);
0364 }
0365 
0366 #include "contactgrouptest.moc"