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

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