File indexing completed on 2024-05-12 15:34:29

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 "gendertest.h"
0009 #include "gender.h"
0010 #include "vcardtool_p.h"
0011 #include <QTest>
0012 
0013 GenderTest::GenderTest(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 GenderTest::~GenderTest()
0019 {
0020 }
0021 
0022 void GenderTest::shouldHaveDefaultValue()
0023 {
0024     KContacts::Gender gender;
0025     QVERIFY(!gender.isValid());
0026     QVERIFY(gender.gender().isEmpty());
0027     QVERIFY(gender.comment().isEmpty());
0028 }
0029 
0030 void GenderTest::shouldAssignValue()
0031 {
0032     const QString genderStr(QStringLiteral("F"));
0033     KContacts::Gender gender(genderStr);
0034     const QString commentStr(QStringLiteral("foo"));
0035     gender.setComment(commentStr);
0036     QVERIFY(gender.isValid());
0037     QVERIFY(!gender.gender().isEmpty());
0038     QCOMPARE(gender.gender(), genderStr);
0039     QVERIFY(!gender.comment().isEmpty());
0040     QCOMPARE(gender.comment(), commentStr);
0041 }
0042 
0043 void GenderTest::shouldAssignExternal()
0044 {
0045     KContacts::Gender gender;
0046     const QString genderStr(QStringLiteral("H"));
0047     gender.setGender(genderStr);
0048     QVERIFY(gender.isValid());
0049     QVERIFY(!gender.gender().isEmpty());
0050     QCOMPARE(gender.gender(), genderStr);
0051 }
0052 
0053 void GenderTest::shouldSerialized()
0054 {
0055     KContacts::Gender gender;
0056     KContacts::Gender result;
0057     const QString genderStr(QStringLiteral("H"));
0058     gender.setGender(genderStr);
0059     gender.setComment(QStringLiteral("foo"));
0060 
0061     QByteArray data;
0062     QDataStream s(&data, QIODevice::WriteOnly);
0063     s << gender;
0064 
0065     QDataStream t(&data, QIODevice::ReadOnly);
0066     t >> result;
0067 
0068     QVERIFY(gender == result);
0069 }
0070 
0071 void GenderTest::shouldEqualGender()
0072 {
0073     KContacts::Gender gender;
0074     KContacts::Gender result;
0075     const QString genderStr(QStringLiteral("H"));
0076     gender.setGender(genderStr);
0077     gender.setComment(QStringLiteral("foo"));
0078 
0079     result = gender;
0080     QVERIFY(gender == result);
0081 }
0082 
0083 void GenderTest::shouldParseGender_data()
0084 {
0085     QTest::addColumn<QByteArray>("vcarddata");
0086     QTest::addColumn<QString>("genre");
0087     QTest::addColumn<QString>("comment");
0088     QTest::addColumn<bool>("hasGender");
0089 
0090     QByteArray str(
0091         "BEGIN:VCARD\n"
0092         "VERSION:3.0\n"
0093         "N:LastName;FirstName;;;\n"
0094         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0095         "REV:2015-03-14T09:24:45+00:00\n"
0096         "FN:FirstName LastName\n"
0097         "END:VCARD\n");
0098     QTest::newRow("nogender") << str << QString() << QString() << false;
0099 
0100     str = QByteArray(
0101         "BEGIN:VCARD\n"
0102         "VERSION:3.0\n"
0103         "N:LastName;FirstName;;;\n"
0104         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0105         "REV:2015-03-14T09:24:45+00:00\n"
0106         "FN:FirstName LastName\n"
0107         "GENDER:H\n"
0108         "END:VCARD\n");
0109     QTest::newRow("hasgenderbutnocomment") << str << QStringLiteral("H") << QString() << true;
0110 
0111     str = QByteArray(
0112         "BEGIN:VCARD\n"
0113         "VERSION:3.0\n"
0114         "N:LastName;FirstName;;;\n"
0115         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0116         "REV:2015-03-14T09:24:45+00:00\n"
0117         "FN:FirstName LastName\n"
0118         "GENDER:;foo\n"
0119         "END:VCARD\n");
0120     QTest::newRow("hasgenderbutnotypebutcomment") << str << QString() << QStringLiteral("foo") << true;
0121 
0122     str = QByteArray(
0123         "BEGIN:VCARD\n"
0124         "VERSION:3.0\n"
0125         "N:LastName;FirstName;;;\n"
0126         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
0127         "REV:2015-03-14T09:24:45+00:00\n"
0128         "FN:FirstName LastName\n"
0129         "GENDER:H;foo\n"
0130         "END:VCARD\n");
0131     QTest::newRow("hasgendertypeandcomment") << str << QStringLiteral("H") << QStringLiteral("foo") << true;
0132 }
0133 
0134 void GenderTest::shouldParseGender()
0135 {
0136     QFETCH(QByteArray, vcarddata);
0137     QFETCH(QString, genre);
0138     QFETCH(QString, comment);
0139     QFETCH(bool, hasGender);
0140 
0141     KContacts::VCardTool vcard;
0142     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0143     QCOMPARE(lst.count(), 1);
0144     QCOMPARE(lst.at(0).gender().isValid(), hasGender);
0145     QCOMPARE(lst.at(0).gender().comment(), comment);
0146     QCOMPARE(lst.at(0).gender().gender(), genre);
0147 }
0148 
0149 QByteArray GenderTest::createCard(const QByteArray &gender)
0150 {
0151     QByteArray expected(
0152         "BEGIN:VCARD\r\n"
0153         "VERSION:4.0\r\n"
0154         "EMAIL:foo@kde.org\r\n");
0155     if (!gender.isEmpty()) {
0156         expected += gender + "\r\n";
0157     }
0158     expected += QByteArray(
0159         "N:;;;;\r\n"
0160         "UID:testuid\r\n"
0161         "END:VCARD\r\n\r\n");
0162     return expected;
0163 }
0164 
0165 void GenderTest::shouldExportEmptyGender()
0166 {
0167     KContacts::AddresseeList lst;
0168     KContacts::Addressee addr;
0169     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0170     addr.setUid(QStringLiteral("testuid"));
0171     lst << addr;
0172     KContacts::VCardTool vcard;
0173     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0174     QByteArray expected = createCard(QByteArray());
0175     QCOMPARE(ba, expected);
0176 }
0177 
0178 void GenderTest::shouldExportOnlyGenderWithoutCommentGender()
0179 {
0180     KContacts::AddresseeList lst;
0181     KContacts::Addressee addr;
0182     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0183     addr.setUid(QStringLiteral("testuid"));
0184     KContacts::Gender gender;
0185     gender.setGender(QStringLiteral("H"));
0186     addr.setGender(gender);
0187     lst << addr;
0188     KContacts::VCardTool vcard;
0189     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0190     QByteArray expected = createCard(QByteArray("GENDER:H"));
0191     QCOMPARE(ba, expected);
0192 }
0193 
0194 void GenderTest::shouldExportOnlyGenderWithCommentGender()
0195 {
0196     KContacts::AddresseeList lst;
0197     KContacts::Addressee addr;
0198     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0199     addr.setUid(QStringLiteral("testuid"));
0200     KContacts::Gender gender;
0201     gender.setGender(QStringLiteral("H"));
0202     gender.setComment(QStringLiteral("comment"));
0203     addr.setGender(gender);
0204     lst << addr;
0205     KContacts::VCardTool vcard;
0206     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0207     QByteArray expected = createCard(QByteArray("GENDER:H;comment"));
0208     QCOMPARE(ba, expected);
0209 }
0210 
0211 void GenderTest::shouldExportOnlyGenderWithoutTypeCommentGender()
0212 {
0213     KContacts::AddresseeList lst;
0214     KContacts::Addressee addr;
0215     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0216     addr.setUid(QStringLiteral("testuid"));
0217     KContacts::Gender gender;
0218     gender.setComment(QStringLiteral("comment"));
0219     addr.setGender(gender);
0220     lst << addr;
0221     KContacts::VCardTool vcard;
0222     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0223     QByteArray expected = createCard(QByteArray("GENDER:;comment"));
0224     QCOMPARE(ba, expected);
0225 }
0226 
0227 void GenderTest::shouldNotExportInVcard3()
0228 {
0229     KContacts::AddresseeList lst;
0230     KContacts::Addressee addr;
0231     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0232     addr.setUid(QStringLiteral("testuid"));
0233     KContacts::Gender gender;
0234     gender.setComment(QStringLiteral("comment"));
0235     addr.setGender(gender);
0236     lst << addr;
0237     KContacts::VCardTool vcard;
0238     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
0239     QByteArray expected(
0240         "BEGIN:VCARD\r\n"
0241         "VERSION:3.0\r\n"
0242         "EMAIL:foo@kde.org\r\n"
0243         "N:;;;;\r\n"
0244         "UID:testuid\r\n"
0245         "END:VCARD\r\n\r\n");
0246     QCOMPARE(ba, expected);
0247 }
0248 
0249 QTEST_MAIN(GenderTest)
0250 
0251 #include "moc_gendertest.cpp"