File indexing completed on 2024-03-24 15:26:04

0001 /*
0002     This file is part of the KContacts framework.
0003     SPDX-FileCopyrightText: 2007 KDE-PIM team <kde-pim@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "geotest.h"
0009 #include "kcontacts/geo.h"
0010 #include "vcardtool_p.h"
0011 #include <QTest>
0012 
0013 QTEST_MAIN(GeoTest)
0014 
0015 void GeoTest::constructor()
0016 {
0017     KContacts::Geo geo(1.2f, 3.4f);
0018 
0019     QVERIFY((float)geo.latitude() == (float)1.2);
0020     QVERIFY((float)geo.longitude() == (float)3.4);
0021 }
0022 
0023 void GeoTest::isValid()
0024 {
0025     KContacts::Geo geo;
0026 
0027     QVERIFY(!geo.isValid());
0028 
0029     geo.setLatitude(23);
0030 
0031     QVERIFY(!geo.isValid());
0032 
0033     geo.setLongitude(45);
0034 
0035     QVERIFY(geo.isValid());
0036 
0037     geo.clear();
0038 
0039     QVERIFY(!geo.isValid());
0040     QVERIFY(geo == KContacts::Geo());
0041 }
0042 
0043 void GeoTest::setData()
0044 {
0045     KContacts::Geo geo;
0046 
0047     geo.setLatitude(22.5f);
0048     geo.setLongitude(45.1f);
0049 
0050     QVERIFY((float)geo.latitude() == (float)22.5);
0051     QVERIFY((float)geo.longitude() == (float)45.1);
0052 }
0053 
0054 void GeoTest::equals()
0055 {
0056     KContacts::Geo geo1(22.5f, 33.7f);
0057     KContacts::Geo geo2(22.5f, 33.7f);
0058 
0059     QVERIFY(geo1 == geo2);
0060 }
0061 
0062 void GeoTest::differs()
0063 {
0064     KContacts::Geo geo1(22.5f, 33.7f);
0065     KContacts::Geo geo2(22.5f, 33.6f);
0066 
0067     QVERIFY(geo1 != geo2);
0068 }
0069 
0070 void GeoTest::serialization()
0071 {
0072     KContacts::Geo geo1(22.5f, 33.7f);
0073     QByteArray data;
0074 
0075     QDataStream s(&data, QIODevice::WriteOnly);
0076     s << geo1;
0077 
0078     KContacts::Geo geo2;
0079     QDataStream t(&data, QIODevice::ReadOnly);
0080     t >> geo2;
0081 
0082     QVERIFY(geo1 == geo2);
0083 }
0084 
0085 void GeoTest::shouldParseGeoVCard3()
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         "GEO:22.5;180.0\n"
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     QVERIFY(lst.at(0).geo().isValid());
0101     KContacts::Geo geo = lst.at(0).geo();
0102     QCOMPARE(geo.latitude(), 22.5);
0103     QCOMPARE(geo.longitude(), 180.0);
0104 }
0105 
0106 void GeoTest::shouldParseGeoVCard4()
0107 {
0108     QByteArray vcarddata(
0109         "BEGIN:VCARD\n"
0110         "VERSION:4.0\n"
0111         "N:LastName;FirstName;;;\n"
0112         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0113         "GEO:geo:22.5,180.0\n"
0114         "REV:2015-03-14T09:24:45+00:00\n"
0115         "FN:FirstName LastName\n"
0116         "END:VCARD\n");
0117 
0118     KContacts::VCardTool vcard;
0119     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0120     QCOMPARE(lst.count(), 1);
0121     QVERIFY(lst.at(0).geo().isValid());
0122     KContacts::Geo geo = lst.at(0).geo();
0123     QCOMPARE(geo.latitude(), 22.5);
0124     QCOMPARE(geo.longitude(), 180.0);
0125 }
0126 
0127 void GeoTest::shouldGenerateVCard3()
0128 {
0129     KContacts::AddresseeList lst;
0130     KContacts::Addressee addr;
0131     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0132     addr.setUid(QStringLiteral("testuid"));
0133 
0134     KContacts::Geo geo;
0135     geo.setLongitude(180.0);
0136     geo.setLatitude(22.5);
0137     addr.setGeo(geo);
0138 
0139     lst << addr;
0140     KContacts::VCardTool vcard;
0141     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
0142     QByteArray expected(
0143         "BEGIN:VCARD\r\n"
0144         "VERSION:3.0\r\n"
0145         "EMAIL:foo@kde.org\r\n"
0146         "GEO:22.500000;180.000000\r\n"
0147         "N:;;;;\r\n"
0148         "UID:testuid\r\n"
0149         "END:VCARD\r\n\r\n");
0150     QCOMPARE(ba, expected);
0151 }
0152 
0153 void GeoTest::shouldGenerateVCard4()
0154 {
0155     KContacts::AddresseeList lst;
0156     KContacts::Addressee addr;
0157     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0158     addr.setUid(QStringLiteral("testuid"));
0159 
0160     KContacts::Geo geo;
0161     geo.setLongitude(180.0);
0162     geo.setLatitude(22.5);
0163     addr.setGeo(geo);
0164 
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         "GEO:geo:22.500000,180.000000\r\n"
0173         "N:;;;;\r\n"
0174         "UID:testuid\r\n"
0175         "END:VCARD\r\n\r\n");
0176     QCOMPARE(ba, expected);
0177 }
0178 
0179 void GeoTest::shouldGenerateWithoutGeo()
0180 {
0181     KContacts::AddresseeList lst;
0182     KContacts::Addressee addr;
0183     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0184     addr.setUid(QStringLiteral("testuid"));
0185 
0186     lst << addr;
0187     KContacts::VCardTool vcard;
0188     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0189     QByteArray expected(
0190         "BEGIN:VCARD\r\n"
0191         "VERSION:4.0\r\n"
0192         "EMAIL:foo@kde.org\r\n"
0193         "N:;;;;\r\n"
0194         "UID:testuid\r\n"
0195         "END:VCARD\r\n\r\n");
0196     QCOMPARE(ba, expected);
0197 }
0198 
0199 #include "moc_geotest.cpp"