File indexing completed on 2024-04-21 05:41:33

0001 #include <QObject>
0002 #include <QDebug>
0003 #include <QJsonDocument>
0004 #include <QJsonArray>
0005 
0006 #include <QTest>
0007 #include <QSignalSpy>
0008 
0009 #include "filedataprovider.h"
0010 #include "listhandler.h"
0011 #include "downloadhandler.h"
0012 
0013 class TestFileData : public QObject
0014 {
0015     Q_OBJECT
0016 
0017 private Q_SLOTS:
0018     void fileDataProviderShouldProvideData();
0019     void listCommandShouldOutputList();
0020     void invalidDownloadCommandShouldError();
0021     void downloadCommandWithNoSourceShouldError();
0022     void downloadCommandShouldStartDownload();
0023 
0024 private:
0025     void fetchData(QJsonDocument *doc);
0026 };
0027 
0028 void TestFileData::fetchData(QJsonDocument *doc)
0029 {
0030     DataProvider::Ptr provider(new FileDataProvider(QFINDTESTDATA("inqlude-all.json")));
0031     provider->ensureDataAvailable();
0032     QSignalSpy spyError(provider.data(), SIGNAL(error()));
0033     QSignalSpy spyDataAvailable(provider.data(), SIGNAL(dataAvailable(QJsonDocument)));
0034 
0035     QVERIFY(spyDataAvailable.wait(1000));
0036     QVERIFY(spyError.isEmpty());
0037     *doc = spyDataAvailable.at(0).at(0).value<QJsonDocument> ();
0038     QCOMPARE(doc->array().size(), 3);
0039 }
0040 
0041 void TestFileData::fileDataProviderShouldProvideData()
0042 {
0043     QJsonDocument doc;
0044     fetchData(&doc);
0045 }
0046 
0047 void TestFileData::listCommandShouldOutputList()
0048 {
0049     QJsonDocument doc;
0050     fetchData(&doc);
0051 
0052     QString output;
0053     QTextStream stream(&output);
0054     ListHandler handler(stream);
0055     handler.list(doc);
0056     const QStringList expected = QStringList()
0057        << "karchive   Reading, creating, and manipulating file archives"
0058        << "kauth      Execute actions as privileged user"
0059        << "kbookmarks Web browser bookmark management"
0060        << "";
0061     QCOMPARE(output.split('\n'), expected);
0062 }
0063 
0064 void TestFileData::invalidDownloadCommandShouldError()
0065 {
0066     QJsonDocument doc;
0067     fetchData(&doc);
0068 
0069     QString output;
0070     QTextStream stream(&output);
0071     DownloadHandler handler(stream, "doesnotexist");
0072     handler.download(doc);
0073     const QString expected("Library doesnotexist not found\n");
0074     QCOMPARE(output, expected);
0075 }
0076 
0077 void TestFileData::downloadCommandWithNoSourceShouldError()
0078 {
0079     QJsonDocument doc;
0080     fetchData(&doc);
0081 
0082     QString output;
0083     QTextStream stream(&output);
0084     DownloadHandler handler(stream, "kbookmarks");
0085     handler.download(doc);
0086     const QString expected("Library kbookmarks has no source package\nSuggestion: see https://projects.kde.org/projects/frameworks/kbookmarks/repository for access to the sources\n");
0087     QCOMPARE(output, expected);
0088 }
0089 
0090 void TestFileData::downloadCommandShouldStartDownload()
0091 {
0092     QJsonDocument doc;
0093     fetchData(&doc);
0094 
0095     QString output;
0096     QTextStream stream(&output);
0097     DownloadHandler handler(stream, "kauth");
0098     handler.download(doc);
0099     const QString expected("Downloading http://download.kde.org/stable/frameworks/5.1.0/kauth-5.1.0.tar.xz...\n");
0100     QCOMPARE(output, expected);
0101 }
0102 
0103 QTEST_MAIN(TestFileData)
0104 
0105 #include "filedatatest.moc"