File indexing completed on 2024-04-14 14:18:57

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 "titletest.h"
0009 #include "parametermap_p.h"
0010 #include "title.h"
0011 #include "vcardtool_p.h"
0012 #include <QTest>
0013 
0014 TitleTest::TitleTest(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 TitleTest::~TitleTest()
0020 {
0021 }
0022 
0023 void TitleTest::shouldHaveDefaultValue()
0024 {
0025     KContacts::Title title;
0026     QVERIFY(!title.isValid());
0027     QVERIFY(title.title().isEmpty());
0028     QVERIFY(title.params().empty());
0029 }
0030 
0031 void TitleTest::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::Title title(lang);
0038     title.setParams(params);
0039     QVERIFY(title.isValid());
0040     QVERIFY(!title.title().isEmpty());
0041     QCOMPARE(title.title(), lang);
0042     QVERIFY(!title.params().empty());
0043     QCOMPARE(title.params(), params);
0044 }
0045 
0046 void TitleTest::shouldAssignExternal()
0047 {
0048     KContacts::Title title;
0049     const QString lang(QStringLiteral("fr"));
0050     title.setTitle(lang);
0051     QVERIFY(title.isValid());
0052     QCOMPARE(title.title(), lang);
0053 }
0054 
0055 void TitleTest::shouldSerialized()
0056 {
0057     KContacts::Title title;
0058     KContacts::Title result;
0059     const QString lang(QStringLiteral("fr"));
0060     title.setTitle(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     title.setParams(params);
0065 
0066     QByteArray data;
0067     QDataStream s(&data, QIODevice::WriteOnly);
0068     s << title;
0069 
0070     QDataStream t(&data, QIODevice::ReadOnly);
0071     t >> result;
0072 
0073     QVERIFY(title == result);
0074 }
0075 
0076 void TitleTest::shouldEqualTitle()
0077 {
0078     KContacts::Title title;
0079     KContacts::Title result;
0080     const QString lang(QStringLiteral("fr"));
0081     title.setTitle(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     title.setParams(params);
0086 
0087     result = title;
0088     QVERIFY(title == result);
0089 }
0090 
0091 void TitleTest::shouldParseTwoTitles()
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         "Title:boo\n"
0099         "Title:bla\n"
0100         "REV:2015-03-14T09:24:45+00:00\n"
0101         "FN:FirstName LastName\n"
0102         "END:VCARD\n");
0103 
0104     KContacts::VCardTool vcard;
0105     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0106     QCOMPARE(lst.count(), 1);
0107     QCOMPARE(lst.at(0).extraTitleList().count(), 2);
0108     QCOMPARE(lst.at(0).title(), QStringLiteral("boo"));
0109     QCOMPARE(lst.at(0).extraTitleList().at(0).title(), QStringLiteral("boo"));
0110     QCOMPARE(lst.at(0).extraTitleList().at(1).title(), QStringLiteral("bla"));
0111 }
0112 
0113 void TitleTest::shouldParseTitle()
0114 {
0115     QByteArray vcarddata(
0116         "BEGIN:VCARD\n"
0117         "VERSION:3.0\n"
0118         "N:LastName;FirstName;;;\n"
0119         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0120         "Title:boo\n"
0121         "REV:2015-03-14T09:24:45+00:00\n"
0122         "FN:FirstName LastName\n"
0123         "END:VCARD\n");
0124 
0125     KContacts::VCardTool vcard;
0126     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0127     QCOMPARE(lst.count(), 1);
0128     QCOMPARE(lst.at(0).extraTitleList().count(), 1);
0129     QCOMPARE(lst.at(0).title(), QStringLiteral("boo"));
0130     QCOMPARE(lst.at(0).extraTitleList().at(0).title(), QStringLiteral("boo"));
0131 }
0132 
0133 void TitleTest::shouldParseWithoutTitle()
0134 {
0135     QByteArray vcarddata(
0136         "BEGIN:VCARD\n"
0137         "VERSION:3.0\n"
0138         "N:LastName;FirstName;;;\n"
0139         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0140         "REV:2015-03-14T09:24:45+00:00\n"
0141         "FN:FirstName LastName\n"
0142         "END:VCARD\n");
0143 
0144     KContacts::VCardTool vcard;
0145     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0146     QCOMPARE(lst.count(), 1);
0147     QCOMPARE(lst.at(0).extraTitleList().count(), 0);
0148     QCOMPARE(lst.at(0).title(), QString());
0149 }
0150 
0151 void TitleTest::shouldCreateVCard()
0152 {
0153     KContacts::AddresseeList lst;
0154     KContacts::Addressee addr;
0155     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0156     addr.setUid(QStringLiteral("testuid"));
0157     KContacts::Title::List lstTitle;
0158     KContacts::Title title(QStringLiteral("fr"));
0159     lstTitle << title;
0160     addr.setExtraTitleList(lstTitle);
0161     lst << addr;
0162     KContacts::VCardTool vcard;
0163     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0164     QByteArray expected(
0165         "BEGIN:VCARD\r\n"
0166         "VERSION:4.0\r\n"
0167         "EMAIL:foo@kde.org\r\n"
0168         "N:;;;;\r\n"
0169         "TITLE:fr\r\n"
0170         "UID:testuid\r\n"
0171         "END:VCARD\r\n\r\n");
0172 
0173     QCOMPARE(ba, expected);
0174 }
0175 
0176 void TitleTest::shouldCreateVCardWithTwoTitle()
0177 {
0178     KContacts::AddresseeList lst;
0179     KContacts::Addressee addr;
0180     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0181     addr.setUid(QStringLiteral("testuid"));
0182     KContacts::Title::List lstTitle;
0183     KContacts::Title title(QStringLiteral("fr"));
0184     KContacts::Title title2(QStringLiteral("fr2"));
0185     lstTitle << title << title2;
0186     addr.setExtraTitleList(lstTitle);
0187     lst << addr;
0188     KContacts::VCardTool vcard;
0189     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0190     QByteArray expected(
0191         "BEGIN:VCARD\r\n"
0192         "VERSION:4.0\r\n"
0193         "EMAIL:foo@kde.org\r\n"
0194         "N:;;;;\r\n"
0195         "TITLE:fr\r\n"
0196         "TITLE:fr2\r\n"
0197         "UID:testuid\r\n"
0198         "END:VCARD\r\n\r\n");
0199 
0200     QCOMPARE(ba, expected);
0201 }
0202 
0203 void TitleTest::shouldCreateVCardWithParameters()
0204 {
0205     KContacts::AddresseeList lst;
0206     KContacts::Addressee addr;
0207     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0208     addr.setUid(QStringLiteral("testuid"));
0209     KContacts::Title::List lstTitle;
0210     KContacts::Title title(QStringLiteral("fr"));
0211     KContacts::ParameterMap params;
0212     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0213     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0214     title.setParams(params);
0215     lstTitle << title;
0216     addr.setExtraTitleList(lstTitle);
0217     lst << addr;
0218     KContacts::VCardTool vcard;
0219     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0220     QByteArray expected(
0221         "BEGIN:VCARD\r\n"
0222         "VERSION:4.0\r\n"
0223         "EMAIL:foo@kde.org\r\n"
0224         "N:;;;;\r\n"
0225         "TITLE;FOO1=bla1,blo1;FOO2=bla2,blo2:fr\r\n"
0226         "UID:testuid\r\n"
0227         "END:VCARD\r\n\r\n");
0228     QCOMPARE(ba, expected);
0229 }
0230 
0231 void TitleTest::shouldGenerateTitleForVCard3()
0232 {
0233     KContacts::AddresseeList lst;
0234     KContacts::Addressee addr;
0235     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0236     addr.setUid(QStringLiteral("testuid"));
0237     KContacts::Title::List lstTitle;
0238     KContacts::Title title(QStringLiteral("fr"));
0239     KContacts::ParameterMap params;
0240     params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
0241     params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
0242     title.setParams(params);
0243     lstTitle << title;
0244     addr.setExtraTitleList(lstTitle);
0245     lst << addr;
0246     KContacts::VCardTool vcard;
0247     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
0248     QByteArray expected(
0249         "BEGIN:VCARD\r\n"
0250         "VERSION:3.0\r\n"
0251         "EMAIL:foo@kde.org\r\n"
0252         "N:;;;;\r\n"
0253         "TITLE;FOO1=bla1,blo1;FOO2=bla2,blo2:fr\r\n"
0254         "UID:testuid\r\n"
0255         "END:VCARD\r\n\r\n");
0256     QCOMPARE(ba, expected);
0257 }
0258 
0259 QTEST_MAIN(TitleTest)
0260 
0261 #include "moc_titletest.cpp"