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

0001 /*
0002     This file is part of the KContacts framework.
0003     SPDX-FileCopyrightText: 2016-2019 Laurent Montel <montel@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "roletest.h"
0009 #include "parametermap_p.h"
0010 #include "role.h"
0011 #include "vcardtool_p.h"
0012 #include <QTest>
0013 
0014 RoleTest::RoleTest(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 RoleTest::~RoleTest()
0020 {
0021 }
0022 
0023 void RoleTest::shouldHaveDefaultValue()
0024 {
0025     KContacts::Role role;
0026     QVERIFY(!role.isValid());
0027     QVERIFY(role.role().isEmpty());
0028     QVERIFY(role.params().empty());
0029 }
0030 
0031 void RoleTest::shouldAssignValue()
0032 {
0033     const QString lang(QStringLiteral("fr"));
0034     KContacts::ParameterMap params;
0035     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0036     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0037     KContacts::Role role(lang);
0038     role.setParams(params);
0039     QVERIFY(role.isValid());
0040     QVERIFY(!role.role().isEmpty());
0041     QCOMPARE(role.role(), lang);
0042     QVERIFY(!role.params().empty());
0043     QCOMPARE(role.params(), params);
0044 }
0045 
0046 void RoleTest::shouldAssignExternal()
0047 {
0048     KContacts::Role role;
0049     const QString lang(QStringLiteral("fr"));
0050     role.setRole(lang);
0051     QVERIFY(role.isValid());
0052     QCOMPARE(role.role(), lang);
0053 }
0054 
0055 void RoleTest::shouldSerialized()
0056 {
0057     KContacts::Role role;
0058     KContacts::Role result;
0059     const QString lang(QStringLiteral("fr"));
0060     role.setRole(lang);
0061     KContacts::ParameterMap params;
0062     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0063     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0064     role.setParams(params);
0065 
0066     QByteArray data;
0067     QDataStream s(&data, QIODevice::WriteOnly);
0068     s << role;
0069 
0070     QDataStream t(&data, QIODevice::ReadOnly);
0071     t >> result;
0072 
0073     QVERIFY(role == result);
0074 }
0075 
0076 void RoleTest::shouldEqualRole()
0077 {
0078     KContacts::Role role;
0079     KContacts::Role result;
0080     const QString lang(QStringLiteral("fr"));
0081     role.setRole(lang);
0082     KContacts::ParameterMap params;
0083     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0084     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0085     role.setParams(params);
0086 
0087     result = role;
0088     QVERIFY(role == result);
0089 }
0090 
0091 void RoleTest::shouldParseRole()
0092 {
0093     QByteArray vcarddata(
0094         "BEGIN:VCARD\n"
0095         "VERSION:3.0\n"
0096         "N:LastName;FirstName;;;\n"
0097         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0098         "Role:boo\n"
0099         "REV:2015-03-14T09:24:45+00:00\n"
0100         "FN:FirstName LastName\n"
0101         "END:VCARD\n");
0102 
0103     KContacts::VCardTool vcard;
0104     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0105     QCOMPARE(lst.count(), 1);
0106     QCOMPARE(lst.at(0).extraRoleList().count(), 1);
0107     QCOMPARE(lst.at(0).role(), QStringLiteral("boo"));
0108     QCOMPARE(lst.at(0).extraRoleList().at(0).role(), QStringLiteral("boo"));
0109 }
0110 
0111 void RoleTest::shouldParseWithoutRole()
0112 {
0113     QByteArray vcarddata(
0114         "BEGIN:VCARD\n"
0115         "VERSION:3.0\n"
0116         "N:LastName;FirstName;;;\n"
0117         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0118         "REV:2015-03-14T09:24:45+00:00\n"
0119         "FN:FirstName LastName\n"
0120         "END:VCARD\n");
0121 
0122     KContacts::VCardTool vcard;
0123     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0124     QCOMPARE(lst.count(), 1);
0125     QCOMPARE(lst.at(0).extraRoleList().count(), 0);
0126     QCOMPARE(lst.at(0).role(), QString());
0127 }
0128 
0129 void RoleTest::shouldCreateVCard()
0130 {
0131     KContacts::AddresseeList lst;
0132     KContacts::Addressee addr;
0133     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0134     addr.setUid(QStringLiteral("testuid"));
0135     KContacts::Role::List lstRole;
0136     KContacts::Role role(QStringLiteral("fr"));
0137     lstRole << role;
0138     addr.setExtraRoleList(lstRole);
0139     lst << addr;
0140     KContacts::VCardTool vcard;
0141     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0142     QByteArray expected(
0143         "BEGIN:VCARD\r\n"
0144         "VERSION:4.0\r\n"
0145         "EMAIL:foo@kde.org\r\n"
0146         "N:;;;;\r\n"
0147         "ROLE:fr\r\n"
0148         "UID:testuid\r\n"
0149         "END:VCARD\r\n\r\n");
0150 
0151     QCOMPARE(ba, expected);
0152 }
0153 
0154 void RoleTest::shouldCreateVCardWithTwoRole()
0155 {
0156     KContacts::AddresseeList lst;
0157     KContacts::Addressee addr;
0158     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0159     addr.setUid(QStringLiteral("testuid"));
0160     KContacts::Role::List lstRole;
0161     KContacts::Role role(QStringLiteral("fr"));
0162     KContacts::Role role2(QStringLiteral("fr2"));
0163     lstRole << role << role2;
0164     addr.setExtraRoleList(lstRole);
0165     lst << addr;
0166     KContacts::VCardTool vcard;
0167     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0168     QByteArray expected(
0169         "BEGIN:VCARD\r\n"
0170         "VERSION:4.0\r\n"
0171         "EMAIL:foo@kde.org\r\n"
0172         "N:;;;;\r\n"
0173         "ROLE:fr\r\n"
0174         "ROLE:fr2\r\n"
0175         "UID:testuid\r\n"
0176         "END:VCARD\r\n\r\n");
0177 
0178     QCOMPARE(ba, expected);
0179 }
0180 
0181 void RoleTest::shouldCreateVCardWithParameters()
0182 {
0183     KContacts::AddresseeList lst;
0184     KContacts::Addressee addr;
0185     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0186     addr.setUid(QStringLiteral("testuid"));
0187     KContacts::Role::List lstRole;
0188     KContacts::Role role(QStringLiteral("fr"));
0189     KContacts::ParameterMap params;
0190     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0191     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0192     role.setParams(params);
0193     lstRole << role;
0194     addr.setExtraRoleList(lstRole);
0195     lst << addr;
0196     KContacts::VCardTool vcard;
0197     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0198     QByteArray expected(
0199         "BEGIN:VCARD\r\n"
0200         "VERSION:4.0\r\n"
0201         "EMAIL:foo@kde.org\r\n"
0202         "N:;;;;\r\n"
0203         "ROLE;FOO1=bla1,blo1;FOO2=bla2,blo2:fr\r\n"
0204         "UID:testuid\r\n"
0205         "END:VCARD\r\n\r\n");
0206     QCOMPARE(ba, expected);
0207 }
0208 
0209 void RoleTest::shouldGenerateRoleForVCard3()
0210 {
0211     KContacts::AddresseeList lst;
0212     KContacts::Addressee addr;
0213     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0214     addr.setUid(QStringLiteral("testuid"));
0215     KContacts::Role::List lstRole;
0216     KContacts::Role role(QStringLiteral("fr"));
0217     KContacts::ParameterMap params;
0218     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0219     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0220     role.setParams(params);
0221     lstRole << role;
0222     addr.setExtraRoleList(lstRole);
0223     lst << addr;
0224     KContacts::VCardTool vcard;
0225     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
0226     QByteArray expected(
0227         "BEGIN:VCARD\r\n"
0228         "VERSION:3.0\r\n"
0229         "EMAIL:foo@kde.org\r\n"
0230         "N:;;;;\r\n"
0231         "ROLE;FOO1=bla1,blo1;FOO2=bla2,blo2:fr\r\n"
0232         "UID:testuid\r\n"
0233         "END:VCARD\r\n\r\n");
0234     QCOMPARE(ba, expected);
0235 }
0236 
0237 QTEST_MAIN(RoleTest)
0238 
0239 #include "moc_roletest.cpp"