1 /*
2     This file is part of the KContacts framework.
3     SPDX-FileCopyrightText: 2012 Kevin Krammer <krammer@kde.org>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "addressee.h"
9 
10 #include "converter/vcardconverter.h"
11 
12 #include <QDebug>
13 #include <QDir>
14 #include <QObject>
15 #include <QTest>
16 
17 using namespace KContacts;
18 
19 class RoundtripTest : public QObject
20 {
21     Q_OBJECT
22 
23 private:
24     QDir mInputDir;
25     QDir mOutput2_1Dir;
26     QDir mOutput3_0Dir;
27     QDir mOutput4_0Dir;
28 
29     QStringList mInputFiles;
30 
31 private Q_SLOTS:
32     void initTestCase();
33     void testVCardRoundtrip_data();
34     void testVCardRoundtrip();
35 };
36 
37 // check the validity of our test data set
initTestCase()38 void RoundtripTest::initTestCase()
39 {
40     // check that all resource prefixes exist
41 
42     mInputDir = QDir(QStringLiteral(":/input"));
43     QVERIFY(mInputDir.exists());
44     QVERIFY(mInputDir.cd(QStringLiteral("data")));
45 
46     mOutput2_1Dir = QDir(QStringLiteral(":/output2.1"));
47     QVERIFY(mOutput2_1Dir.exists());
48     QVERIFY(mOutput2_1Dir.cd(QStringLiteral("data")));
49 
50     mOutput3_0Dir = QDir(QStringLiteral(":/output3.0"));
51     QVERIFY(mOutput3_0Dir.exists());
52     QVERIFY(mOutput3_0Dir.cd(QStringLiteral("data")));
53 
54     mOutput4_0Dir = QDir(QStringLiteral(":/output4.0"));
55     QVERIFY(mOutput4_0Dir.exists());
56     QVERIFY(mOutput4_0Dir.cd(QStringLiteral("data")));
57 
58     // check that there are input files
59 
60     mInputFiles = mInputDir.entryList();
61 
62     QVERIFY(!mInputFiles.isEmpty());
63 }
64 
testVCardRoundtrip_data()65 void RoundtripTest::testVCardRoundtrip_data()
66 {
67     QTest::addColumn<QString>("inputFile");
68     QTest::addColumn<QString>("output2_1File");
69     QTest::addColumn<QString>("output3_0File");
70     QTest::addColumn<QString>("output4_0File");
71 
72     QString outFile21Pattern = QLatin1String("%1.2_1ref");
73     QString outFile4Pattern = QLatin1String("v4_0.%1.ref");
74     QString outFilePattern = QLatin1String("%1.ref");
75     for (const QString &inputFile : std::as_const(mInputFiles)) {
76         const QString outFile = outFilePattern.arg(inputFile);
77         const QString outFileV2_1 = outFile21Pattern.arg(inputFile);
78         const QString outFileV4 = outFile4Pattern.arg(inputFile);
79         QTest::newRow(QFile::encodeName(inputFile).constData())
80             << inputFile << (mOutput2_1Dir.exists(outFileV2_1) ? outFileV2_1 : QString()) << (mOutput3_0Dir.exists(outFile) ? outFile : QString())
81             << (mOutput4_0Dir.exists(outFileV4) ? outFileV4 : QString());
82     }
83 }
84 
compareBuffers(const char * version,const QByteArray & outputData,const QByteArray & outputRefData)85 static void compareBuffers(const char *version, const QByteArray &outputData, const QByteArray &outputRefData)
86 {
87     if (outputData != outputRefData) {
88         qDebug() << " outputData " << outputData;
89         qDebug() << " outputRefData " << outputRefData;
90     }
91     const QList<QByteArray> outputLines = outputData.split('\n');
92     const QList<QByteArray> outputRefLines = outputRefData.split('\n');
93     for (int i = 0; i < qMin(outputLines.count(), outputRefLines.count()); ++i) {
94         const QByteArray &actual = outputLines.at(i);
95         const QByteArray &expect = outputRefLines.at(i);
96         if (actual != expect) {
97             qCritical() << "Mismatch in" << version << "output line" << (i + 1);
98             QCOMPARE(actual, expect);
99             QCOMPARE(actual.count(), expect.count());
100         }
101     }
102     QCOMPARE(outputLines.count(), outputRefLines.count());
103     QCOMPARE(outputData.size(), outputRefData.size());
104 }
105 
testVCardRoundtrip()106 void RoundtripTest::testVCardRoundtrip()
107 {
108     QFETCH(QString, inputFile);
109     QFETCH(QString, output2_1File);
110     QFETCH(QString, output3_0File);
111     QFETCH(QString, output4_0File);
112 
113     QVERIFY2(!output2_1File.isEmpty() || !output3_0File.isEmpty() || !output4_0File.isEmpty(), "No reference output file for either format version");
114 
115     QFile input(QFileInfo(mInputDir, inputFile).absoluteFilePath());
116     QVERIFY(input.open(QIODevice::ReadOnly));
117 
118     const QByteArray inputData = input.readAll();
119     QVERIFY(!inputData.isEmpty());
120 
121     VCardConverter converter;
122     const Addressee::List list = converter.parseVCards(inputData);
123     QVERIFY(!list.isEmpty());
124 
125     if (!output2_1File.isEmpty()) {
126         const QByteArray outputData = converter.createVCards(list, VCardConverter::v2_1);
127 
128         QFile outputFile(QFileInfo(mOutput2_1Dir, output2_1File).absoluteFilePath());
129         QVERIFY(outputFile.open(QIODevice::ReadOnly));
130 
131         const QByteArray outputRefData = outputFile.readAll();
132         compareBuffers("v2.1", outputData, outputRefData);
133     }
134 
135     if (!output3_0File.isEmpty()) {
136         const QByteArray outputData = converter.createVCards(list, VCardConverter::v3_0);
137 
138         QFile outputFile(QFileInfo(mOutput3_0Dir, output3_0File).absoluteFilePath());
139         QVERIFY(outputFile.open(QIODevice::ReadOnly));
140 
141         const QByteArray outputRefData = outputFile.readAll();
142         compareBuffers("v3.0", outputData, outputRefData);
143     }
144 #if 0
145     if (!output4_0File.isEmpty()) {
146         const QByteArray outputData = converter.createVCards(list, VCardConverter::v4_0);
147 
148         QFile outputFile(QFileInfo(mOutput4_0Dir, output4_0File).absoluteFilePath());
149         QVERIFY(outputFile.open(QIODevice::ReadOnly));
150 
151         const QByteArray outputRefData = outputFile.readAll();
152         compareBuffers("v4.0", outputData, outputRefData);
153     }
154 #endif
155 }
156 
157 QTEST_MAIN(RoundtripTest)
158 
159 #include "testroundtrip.moc"
160