File indexing completed on 2024-10-27 12:52:11

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 "nicknametest.h"
0009 #include "nickname.h"
0010 #include "parametermap_p.h"
0011 #include "vcardtool_p.h"
0012 #include <QTest>
0013 
0014 NickNameTest::NickNameTest(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 NickNameTest::~NickNameTest()
0020 {
0021 }
0022 
0023 void NickNameTest::shouldHaveDefaultValue()
0024 {
0025     KContacts::NickName nickname;
0026     QVERIFY(!nickname.isValid());
0027     QVERIFY(nickname.nickname().isEmpty());
0028     QVERIFY(nickname.params().empty());
0029 }
0030 
0031 void NickNameTest::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::NickName nickname(lang);
0038     nickname.setParams(params);
0039     QVERIFY(nickname.isValid());
0040     QVERIFY(!nickname.nickname().isEmpty());
0041     QCOMPARE(nickname.nickname(), lang);
0042     QVERIFY(!nickname.params().empty());
0043     QCOMPARE(nickname.params(), params);
0044 }
0045 
0046 void NickNameTest::shouldAssignExternal()
0047 {
0048     KContacts::NickName nickname;
0049     const QString lang(QStringLiteral("fr"));
0050     nickname.setNickName(lang);
0051     QVERIFY(nickname.isValid());
0052     QCOMPARE(nickname.nickname(), lang);
0053 }
0054 
0055 void NickNameTest::shouldSerialized()
0056 {
0057     KContacts::NickName nickname;
0058     KContacts::NickName result;
0059     const QString lang(QStringLiteral("fr"));
0060     nickname.setNickName(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     nickname.setParams(params);
0065 
0066     QByteArray data;
0067     QDataStream s(&data, QIODevice::WriteOnly);
0068     s << nickname;
0069 
0070     QDataStream t(&data, QIODevice::ReadOnly);
0071     t >> result;
0072 
0073     QVERIFY(nickname == result);
0074 }
0075 
0076 void NickNameTest::shouldEqualNickName()
0077 {
0078     KContacts::NickName nickname;
0079     KContacts::NickName result;
0080     const QString lang(QStringLiteral("fr"));
0081     nickname.setNickName(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     nickname.setParams(params);
0086 
0087     result = nickname;
0088     QVERIFY(nickname == result);
0089 }
0090 
0091 void NickNameTest::shouldParseNickName()
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         "NickName: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).extraNickNameList().count(), 1);
0107     QCOMPARE(lst.at(0).extraNickNameList().at(0).nickname(), QStringLiteral("boo"));
0108     QCOMPARE(lst.at(0).nickName(), QStringLiteral("boo"));
0109 }
0110 
0111 void NickNameTest::shouldParseWithoutNickName()
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).extraNickNameList().count(), 0);
0126     QCOMPARE(lst.at(0).nickName(), QString());
0127 }
0128 
0129 void NickNameTest::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::NickName::List lstNickName;
0136     KContacts::NickName title(QStringLiteral("blo"));
0137     lstNickName << title;
0138     addr.setExtraNickNameList(lstNickName);
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         "NICKNAME:blo\r\n"
0148         "UID:testuid\r\n"
0149         "END:VCARD\r\n\r\n");
0150 
0151     QCOMPARE(ba, expected);
0152 }
0153 
0154 void NickNameTest::shouldCreateVCardWithTwoNickName()
0155 {
0156     KContacts::AddresseeList lst;
0157     KContacts::Addressee addr;
0158     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0159     addr.setUid(QStringLiteral("testuid"));
0160     KContacts::NickName::List lstNickName;
0161     KContacts::NickName title(QStringLiteral("fr"));
0162     KContacts::NickName title2(QStringLiteral("fr2"));
0163     lstNickName << title << title2;
0164     addr.setExtraNickNameList(lstNickName);
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         "NICKNAME:fr\r\n"
0174         "NICKNAME:fr2\r\n"
0175         "UID:testuid\r\n"
0176         "END:VCARD\r\n\r\n");
0177 
0178     QCOMPARE(ba, expected);
0179 }
0180 
0181 void NickNameTest::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::NickName::List lstNickName;
0188     KContacts::NickName title(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     title.setParams(params);
0193     lstNickName << title;
0194     addr.setExtraNickNameList(lstNickName);
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         "NICKNAME;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 NickNameTest::shouldGenerateNickNameForVCard3()
0210 {
0211     KContacts::AddresseeList lst;
0212     KContacts::Addressee addr;
0213     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0214     addr.setUid(QStringLiteral("testuid"));
0215     KContacts::NickName::List lstNickName;
0216     KContacts::NickName title(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     title.setParams(params);
0221     lstNickName << title;
0222     addr.setExtraNickNameList(lstNickName);
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         "NICKNAME;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(NickNameTest)
0238 
0239 #include "moc_nicknametest.cpp"