File indexing completed on 2024-04-21 14:54:26

0001 /*
0002     This file is part of libkabc.
0003     SPDX-FileCopyrightText: 2015-2019 Laurent Montel <montel@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "impptest.h"
0009 #include "impp.h"
0010 #include "parametermap_p.h"
0011 #include "vcardtool_p.h"
0012 #include <QTest>
0013 
0014 ImppTest::ImppTest(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 ImppTest::~ImppTest()
0020 {
0021 }
0022 
0023 void ImppTest::shouldHaveDefaultValue()
0024 {
0025     KContacts::Impp impp;
0026     QVERIFY(!impp.isValid());
0027     QVERIFY(impp.address().isEmpty());
0028     QVERIFY(impp.serviceType().isEmpty());
0029     QVERIFY(impp.params().empty());
0030 }
0031 
0032 void ImppTest::shouldAssignValue()
0033 {
0034     const QUrl address(QStringLiteral("icq:address"));
0035     KContacts::ParameterMap params;
0036     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0037     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0038     KContacts::Impp impp;
0039     impp.setParams(params);
0040     impp.setAddress(address);
0041     QVERIFY(impp.isValid());
0042     QVERIFY(!impp.address().isEmpty());
0043     QCOMPARE(impp.address(), address);
0044     QCOMPARE(impp.serviceType(), QLatin1String("icq"));
0045     QCOMPARE(impp.serviceIcon(), QLatin1String("im-icq"));
0046     QVERIFY(!impp.params().empty());
0047     QCOMPARE(impp.params(), params);
0048 }
0049 
0050 void ImppTest::shouldSerialized()
0051 {
0052     const QUrl address(QStringLiteral("icq:address"));
0053     KContacts::ParameterMap params;
0054     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0055     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0056     KContacts::Impp impp;
0057     impp.setParams(params);
0058     impp.setAddress(address);
0059 
0060     QByteArray data;
0061     QDataStream s(&data, QIODevice::WriteOnly);
0062     s << impp;
0063 
0064     KContacts::Impp result;
0065     QDataStream t(&data, QIODevice::ReadOnly);
0066     t >> result;
0067 
0068     QVERIFY(impp == result);
0069 }
0070 
0071 void ImppTest::shouldEqualImpp()
0072 {
0073     const QUrl address(QStringLiteral("icq:address"));
0074     KContacts::ParameterMap params;
0075     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0076     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0077     KContacts::Impp impp;
0078     impp.setParams(params);
0079     impp.setAddress(address);
0080 
0081     KContacts::Impp result(impp);
0082     QVERIFY(impp == result);
0083 }
0084 
0085 void ImppTest::shouldParseWithoutImpp()
0086 {
0087     QByteArray vcarddata(
0088         "BEGIN:VCARD\n"
0089         "VERSION:3.0\n"
0090         "N:LastName;FirstName;;;\n"
0091         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0092         "LANG:fr"
0093         "REV:2015-03-14T09:24:45+00:00\n"
0094         "FN:FirstName LastName\n"
0095         "END:VCARD\n");
0096 
0097     KContacts::VCardTool vcard;
0098     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0099     QCOMPARE(lst.count(), 1);
0100     QCOMPARE(lst.at(0).imppList().count(), 0);
0101 }
0102 
0103 void ImppTest::shouldParseImpp()
0104 {
0105     QByteArray vcarddata(
0106         "BEGIN:VCARD\n"
0107         "VERSION:3.0\n"
0108         "N:LastName;FirstName;;;\n"
0109         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0110         "IMPP;X-SERVICE-TYPE=skype:skype:xxxxxxxx\n"
0111         "REV:2015-03-14T09:24:45+00:00\n"
0112         "FN:FirstName LastName\n"
0113         "END:VCARD\n");
0114 
0115     KContacts::VCardTool vcard;
0116     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0117     QCOMPARE(lst.count(), 1);
0118     QCOMPARE(lst.at(0).imppList().count(), 1);
0119     KContacts::Impp impp = lst.at(0).imppList().at(0);
0120     QCOMPARE(impp.address(), QUrl(QStringLiteral("skype:xxxxxxxx")));
0121     QCOMPARE(impp.serviceType(), QLatin1String("skype"));
0122 }
0123 
0124 void ImppTest::shouldParseImppVcard4()
0125 {
0126     QByteArray vcarddata(
0127         "BEGIN:VCARD\n"
0128         "VERSION:4.0\n"
0129         "N:LastName;FirstName;;;\n"
0130         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0131         "IMPP;PREF=1:skype:xxxxxxxx\n"
0132         "IMPP:skype:1234567890\n"
0133         "REV:2015-03-14T09:24:45+00:00\n"
0134         "FN:FirstName LastName\n"
0135         "END:VCARD\n");
0136 
0137     KContacts::VCardTool vcard;
0138     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0139     QCOMPARE(lst.count(), 1);
0140     QCOMPARE(lst.at(0).imppList().count(), 2);
0141     KContacts::Impp impp = lst.at(0).imppList().at(0);
0142     QCOMPARE(impp.address(), QUrl(QStringLiteral("skype:xxxxxxxx")));
0143     QCOMPARE(impp.serviceType(), QLatin1String("skype"));
0144     QVERIFY(impp.isPreferred());
0145     impp = lst.at(0).imppList().at(1);
0146     QCOMPARE(impp.address(), QUrl(QStringLiteral("skype:1234567890")));
0147     QCOMPARE(impp.serviceType(), QLatin1String("skype"));
0148     QVERIFY(!impp.isPreferred());
0149 }
0150 
0151 QByteArray createCard(const QString &type)
0152 {
0153     QByteArray expected(
0154         "BEGIN:VCARD\n"
0155         "VERSION:3.0\n"
0156         "EMAIL:foo@kde.org\n");
0157     if (!type.isEmpty()) {
0158         expected += "IMPP;X-SERVICE-TYPE=" + type.toLatin1() + ":" + type.toLatin1() + ":address\n";
0159     }
0160     expected += QByteArray(
0161         "N:;;;;\n"
0162         "UID:testuid\n"
0163         "END:VCARD\n\n");
0164     return expected;
0165 }
0166 
0167 void ImppTest::shouldParseServiceType_data()
0168 {
0169     QTest::addColumn<QString>("type");
0170     QTest::addColumn<bool>("hasImpp");
0171     QTest::newRow("withoutimpp") << QString() << false;
0172     for (const auto &type : KContacts::Impp::serviceTypes()) {
0173         QTest::newRow(type.toLatin1().constData()) << type << true;
0174     }
0175 }
0176 
0177 void ImppTest::shouldParseServiceType()
0178 {
0179     QFETCH(QString, type);
0180     QFETCH(bool, hasImpp);
0181 
0182     QByteArray vcarddata = createCard(type);
0183 
0184     KContacts::VCardTool vcard;
0185     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0186     QCOMPARE(lst.count(), 1);
0187 
0188     QCOMPARE(!lst.at(0).imppList().isEmpty(), hasImpp);
0189     if (hasImpp) {
0190         KContacts::Impp impp = lst.at(0).imppList().at(0);
0191         QCOMPARE(impp.address(), QUrl(QStringLiteral("%1:address").arg(type)));
0192         QCOMPARE(impp.serviceType(), type);
0193     }
0194 }
0195 
0196 QByteArray expectedVcard(const QString &type)
0197 {
0198     QByteArray expected(
0199         "BEGIN:VCARD\r\n"
0200         "VERSION:4.0\r\n"
0201         "EMAIL:foo@kde.org\r\n");
0202     if (!type.isEmpty()) {
0203         expected += "IMPP:" + type.toLatin1() + ":address\r\n";
0204     }
0205 
0206     expected +=
0207         ("N:;;;;\r\n"
0208          "UID:testuid\r\n"
0209          "END:VCARD\r\n\r\n");
0210     return expected;
0211 }
0212 
0213 void ImppTest::shouldExportEmptyType()
0214 {
0215     QByteArray expected(
0216         "BEGIN:VCARD\r\n"
0217         "VERSION:4.0\r\n"
0218         "EMAIL:foo@kde.org\r\n"
0219         "N:;;;;\r\n"
0220         "UID:testuid\r\n"
0221         "END:VCARD\r\n\r\n");
0222     KContacts::AddresseeList lst;
0223     KContacts::Addressee addr;
0224     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0225     addr.setUid(QStringLiteral("testuid"));
0226     lst << addr;
0227     KContacts::VCardTool vcard;
0228     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0229     QCOMPARE(ba, expected);
0230 }
0231 
0232 void ImppTest::shouldExportType_data()
0233 {
0234     QTest::addColumn<QString>("type");
0235     for (const auto &type : KContacts::Impp::serviceTypes()) {
0236         QTest::newRow(type.toLatin1().constData()) << type;
0237     }
0238 }
0239 
0240 void ImppTest::shouldExportType()
0241 {
0242     QFETCH(QString, type);
0243 
0244     QByteArray expected = expectedVcard(type);
0245     KContacts::AddresseeList lst;
0246     KContacts::Addressee addr;
0247     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0248     addr.setUid(QStringLiteral("testuid"));
0249     KContacts::Impp impp;
0250     impp.setAddress(QUrl(type + QStringLiteral(":address")));
0251     addr.insertImpp(impp);
0252     lst << addr;
0253     KContacts::VCardTool vcard;
0254     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0255     QCOMPARE(ba, expected);
0256 }
0257 
0258 void ImppTest::shouldExportWithParameters()
0259 {
0260     QByteArray expected(
0261         "BEGIN:VCARD\r\n"
0262         "VERSION:4.0\r\n"
0263         "EMAIL:foo@kde.org\r\n"
0264         "IMPP;FOO1=bla1,blo1;FOO2=bla2,blo2:skype:address\r\n"
0265         "N:;;;;\r\n"
0266         "UID:testuid\r\n"
0267         "END:VCARD\r\n\r\n");
0268     KContacts::AddresseeList lst;
0269     KContacts::Addressee addr;
0270     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0271     addr.setUid(QStringLiteral("testuid"));
0272     KContacts::ParameterMap params;
0273     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0274     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0275     KContacts::Impp impp;
0276     impp.setAddress(QUrl(QStringLiteral("skype:address")));
0277     impp.setParams(params);
0278     impp.setPreferred(false);
0279     addr.insertImpp(impp);
0280     lst << addr;
0281 
0282     KContacts::VCardTool vcard;
0283     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0284     QCOMPARE(ba, expected);
0285 }
0286 
0287 void ImppTest::shouldShouldNotExportTwiceServiceType()
0288 {
0289     QByteArray expected(
0290         "BEGIN:VCARD\r\n"
0291         "VERSION:4.0\r\n"
0292         "EMAIL:foo@kde.org\r\n"
0293         "IMPP;FOO1=bla1,blo1;FOO2=bla2,blo2;PREF=1:skype:address\r\n"
0294         "N:;;;;\r\n"
0295         "UID:testuid\r\n"
0296         "END:VCARD\r\n\r\n");
0297     KContacts::AddresseeList lst;
0298     KContacts::Addressee addr;
0299     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0300     addr.setUid(QStringLiteral("testuid"));
0301     KContacts::ParameterMap params;
0302     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0303     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0304     params.push_back({QStringLiteral("X-SERVICE-TYPE"), QStringList() << QStringLiteral("aim")});
0305     KContacts::Impp impp;
0306     impp.setAddress(QUrl(QStringLiteral("skype:address")));
0307     impp.setParams(params);
0308     impp.setPreferred(true);
0309     addr.insertImpp(impp);
0310     lst << addr;
0311 
0312     KContacts::VCardTool vcard;
0313     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0314     QCOMPARE(ba, expected);
0315 }
0316 
0317 void ImppTest::testProtocolInformation()
0318 {
0319     const auto types = KContacts::Impp::serviceTypes();
0320     QVERIFY(types.size() > 10);
0321     QVERIFY(types.contains(QLatin1String("xmpp")));
0322 
0323     for (const auto &type : types) {
0324         QVERIFY(!KContacts::Impp::serviceLabel(type).isEmpty());
0325     }
0326 
0327     QCOMPARE(KContacts::Impp::serviceIcon(QStringLiteral("xmpp")), QLatin1String("im-jabber"));
0328 }
0329 
0330 QTEST_MAIN(ImppTest)
0331 
0332 #include "moc_impptest.cpp"