File indexing completed on 2024-04-28 03:54:48
0001 /* 0002 0003 SPDX-FileCopyrightText: 2023 Nicolas Fella <nicolas.fella@gmx.de> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include <KIO/TransferJob> 0009 0010 #include <QSignalSpy> 0011 #include <QTest> 0012 0013 class GetTest : public QObject 0014 { 0015 Q_OBJECT 0016 private Q_SLOTS: 0017 void testGet(); 0018 void testGet_data(); 0019 }; 0020 0021 void GetTest::testGet_data() 0022 { 0023 QTest::addColumn<QString>("url"); 0024 QTest::addColumn<QString>("expectedMimeType"); 0025 QTest::addColumn<QByteArray>("expectedData"); 0026 0027 QTest::addRow("html") << "http://localhost:5000/get/html" 0028 << "text/html" << QByteArray("<p>Hello, World!</p>"); 0029 QTest::addRow("calendar") << "http://localhost:5000/get/calendar" 0030 << "text/calendar" << QByteArray("Some data\nthat\nhas\nnew\nlines\n"); 0031 } 0032 0033 void GetTest::testGet() 0034 { 0035 QFETCH(QString, url); 0036 QFETCH(QString, expectedMimeType); 0037 QFETCH(QByteArray, expectedData); 0038 0039 auto *job = KIO::get(QUrl(url)); 0040 0041 QSignalSpy mimeTypeFoundSpy(job, &KIO::TransferJob::mimeTypeFound); 0042 mimeTypeFoundSpy.wait(); 0043 QCOMPARE(mimeTypeFoundSpy.count(), 1); 0044 0045 auto args = mimeTypeFoundSpy.first(); 0046 QCOMPARE(args[1], expectedMimeType); 0047 0048 QSignalSpy dataSpy(job, &KIO::TransferJob::data); 0049 QSignalSpy spy(job, &KJob::finished); 0050 spy.wait(); 0051 QVERIFY(spy.size()); 0052 QCOMPARE(job->mimetype(), expectedMimeType); 0053 0054 QVERIFY(dataSpy.count()); 0055 const QByteArray actualData = dataSpy.first().at(1).toByteArray(); 0056 QCOMPARE(actualData, expectedData); 0057 0058 QCOMPARE(job->error(), KJob::NoError); 0059 } 0060 0061 QTEST_GUILESS_MAIN(GetTest) 0062 0063 #include "gettest.moc"