File indexing completed on 2024-10-06 12:23:27

0001 /*
0002     This file is part of KNewStuff2.
0003     SPDX-FileCopyrightText: 2007 Josef Spillner <spillner@kde.org>
0004     SPDX-FileCopyrightText: 2018 Dan Leinir Turthra Jensen <admin@leinir.dk>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-or-later
0007 */
0008 
0009 #include "khotnewstuff_test.h"
0010 
0011 #include "../src/staticxml/staticxmlprovider_p.h"
0012 #include <KNSCore/Engine>
0013 
0014 #include <KLocalizedString>
0015 
0016 #include <QApplication>
0017 #include <QCommandLineOption>
0018 #include <QCommandLineParser>
0019 #include <QDebug>
0020 #include <QQmlApplicationEngine>
0021 #include <QQmlContext>
0022 #include <QStandardPaths>
0023 
0024 #include <QFile>
0025 #include <QXmlStreamReader>
0026 
0027 KNewStuff2Test::KNewStuff2Test(const QString &configFile)
0028     : QObject()
0029 {
0030     m_messages = new QStandardItemModel(this);
0031     m_configFile = configFile;
0032     m_engine = nullptr;
0033     m_testall = false;
0034 }
0035 
0036 void KNewStuff2Test::setTestAll(bool testall)
0037 {
0038     m_testall = testall;
0039     Q_EMIT testAllChanged();
0040 }
0041 
0042 bool KNewStuff2Test::testAll() const
0043 {
0044     return m_testall;
0045 }
0046 
0047 void KNewStuff2Test::entryTest()
0048 {
0049     addMessage(QStringLiteral("-- test kns2 entry class"), QStringLiteral("msg_info"));
0050 
0051     QFile f(QStringLiteral("%1/testdata/entry.xml").arg(QStringLiteral(KNSSRCDIR)));
0052     if (!f.open(QIODevice::ReadOnly)) {
0053         addMessage(QStringLiteral("Error loading entry file: %1").arg(f.fileName()), QStringLiteral("msg_error"));
0054         return;
0055     }
0056 
0057     QXmlStreamReader reader(&f);
0058     KNSCore::EntryInternal e;
0059     reader.readNextStartElement(); // Skip the first (the external OCS container)
0060     bool xmlResult = reader.readNextStartElement() && e.setEntryXML(reader);
0061     e.setProviderId(QStringLiteral("test-provider"));
0062 
0063     f.close();
0064     if (!xmlResult) {
0065         addMessage(QStringLiteral("Error parsing entry file."), QStringLiteral("msg_error"));
0066         return;
0067     }
0068 
0069     addMessage(QStringLiteral("-- entry->xml test result: %1").arg(e.isValid()), e.isValid() ? QStringLiteral("msg_info") : QStringLiteral("msg_error"));
0070     if (!e.isValid()) {
0071         return;
0072     } else {
0073         QTextStream out(stdout);
0074         out << e.entryXML();
0075     }
0076 }
0077 
0078 void KNewStuff2Test::providerTest()
0079 {
0080     addMessage(QStringLiteral("-- test kns2 provider class"), QStringLiteral("msg_info"));
0081 
0082     QDomDocument doc;
0083     QFile f(QStringLiteral("%1/testdata/provider.xml").arg(QStringLiteral(KNSSRCDIR)));
0084     if (!f.open(QIODevice::ReadOnly)) {
0085         addMessage(QStringLiteral("Error loading provider file: %1").arg(f.fileName()), QStringLiteral("msg_error"));
0086         return;
0087     }
0088     if (!doc.setContent(&f)) {
0089         addMessage(QStringLiteral("Error parsing provider file: %1").arg(f.fileName()), QStringLiteral("msg_error"));
0090         f.close();
0091         return;
0092     }
0093     f.close();
0094 
0095     KNSCore::StaticXmlProvider p;
0096     p.setProviderXML(doc.documentElement());
0097 
0098     addMessage(QStringLiteral("-- xml->provider test result: %1").arg(p.isInitialized()),
0099                p.isInitialized() ? QStringLiteral("msg_info") : QStringLiteral("msg_error"));
0100 }
0101 
0102 void KNewStuff2Test::engineTest()
0103 {
0104     addMessage(QStringLiteral("-- test kns2 engine"), QStringLiteral("msg_info"));
0105 
0106     m_engine = new KNSCore::Engine(this);
0107 
0108     connect(m_engine, &KNSCore::Engine::signalErrorCode, this, &KNewStuff2Test::slotEngineError);
0109     connect(m_engine, &KNSCore::Engine::signalProvidersLoaded, this, &KNewStuff2Test::slotProvidersLoaded);
0110     connect(m_engine, &KNSCore::Engine::signalEntriesLoaded, this, &KNewStuff2Test::slotEntriesLoaded);
0111     connect(m_engine, &KNSCore::Engine::signalEntryEvent, this, &KNewStuff2Test::slotInstallationFinished);
0112 
0113     bool ret = m_engine->init(m_configFile);
0114 
0115     addMessage(QStringLiteral("-- engine test result: %1").arg(ret), ret ? QStringLiteral("msg_info") : QStringLiteral("msg_error"));
0116 
0117     if (!ret) {
0118         addMessage(
0119             QStringLiteral(
0120                 "ACHTUNG: you probably need to 'make install' the knsrc file first. Although this is not required anymore, so something went really wrong."),
0121             QStringLiteral("msg_warning"));
0122     }
0123     addMessage(QStringLiteral("-- initial engine test completed"), QStringLiteral("msg_info"));
0124 }
0125 
0126 void KNewStuff2Test::slotProvidersLoaded()
0127 {
0128     addMessage(QStringLiteral("SLOT: slotProvidersLoaded"), QStringLiteral("msg_info"));
0129     //     qDebug() << "-- provider: " << provider->name().representation();
0130 
0131     m_engine->reloadEntries();
0132 }
0133 
0134 void KNewStuff2Test::slotEntriesLoaded(const KNSCore::EntryInternal::List &entries)
0135 {
0136     addMessage(QStringLiteral("SLOT: slotEntriesLoaded. Number of entries %1").arg(entries.count()), QStringLiteral("msg_info"));
0137 
0138     if (m_testall) {
0139         addMessage(QStringLiteral("-- now, download the entries' previews and payload files"), QStringLiteral("msg_info"));
0140 
0141         for (const KNSCore::EntryInternal &entry : entries) {
0142             addMessage(QStringLiteral("-- entry: %1").arg(entry.name()), QStringLiteral("msg_info"));
0143             if (!entry.previewUrl(KNSCore::EntryInternal::PreviewSmall1).isEmpty()) {
0144                 m_engine->loadPreview(entry, KNSCore::EntryInternal::PreviewSmall1);
0145             }
0146             if (!entry.payload().isEmpty()) {
0147                 m_engine->install(entry);
0148             }
0149         }
0150     }
0151 }
0152 
0153 void KNewStuff2Test::slotInstallationFinished()
0154 {
0155     addMessage(QStringLiteral("SLOT: slotInstallationFinished"));
0156 }
0157 
0158 void KNewStuff2Test::slotEngineError(const KNSCore::ErrorCode &, const QString &message, const QVariant &)
0159 {
0160     addMessage(QStringLiteral("SLOT: slotEngineError %1").arg(message), QStringLiteral("msg_error"));
0161 }
0162 
0163 QObject *KNewStuff2Test::messages()
0164 {
0165     return m_messages;
0166 }
0167 
0168 void KNewStuff2Test::addMessage(const QString &message, const QString &iconName)
0169 {
0170     QStandardItem *item = new QStandardItem(message);
0171     item->setData(iconName, Qt::WhatsThisRole);
0172     m_messages->appendRow(item);
0173 }
0174 
0175 KNewStuff2Test *test = nullptr;
0176 static const QtMessageHandler QT_DEFAULT_MESSAGE_HANDLER = qInstallMessageHandler(nullptr);
0177 void debugOutputHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
0178 {
0179     if (test) {
0180         test->addMessage(msg, QStringLiteral("msg_info"));
0181     }
0182     // Call the default handler.
0183     (*QT_DEFAULT_MESSAGE_HANDLER)(type, context, msg);
0184 }
0185 
0186 int main(int argc, char **argv)
0187 {
0188     QApplication app(argc, argv);
0189 
0190     QCommandLineParser *parser = new QCommandLineParser;
0191     parser->addHelpOption();
0192     parser->addOption(QCommandLineOption(QStringLiteral("testall"), i18n("Downloads all previews and payloads")));
0193     parser->addPositionalArgument(
0194         QStringLiteral("knsrcfile"),
0195         i18n("The KNSRC file you want to use for testing. If none is passed, we will use khotnewstuff_test.knsrc, which must be installed."));
0196     parser->process(app);
0197 
0198     if (parser->positionalArguments().count() > 0) {
0199         test = new KNewStuff2Test(parser->positionalArguments().first());
0200     } else {
0201         test = new KNewStuff2Test(QStringLiteral("%1/khotnewstuff_test.knsrc").arg(QStringLiteral(KNSBUILDDIR)));
0202     }
0203     test->setTestAll(parser->isSet(QStringLiteral("testall")));
0204 
0205     QQmlApplicationEngine *appengine = new QQmlApplicationEngine();
0206     appengine->rootContext()->setContextProperty(QStringLiteral("testObject"), test);
0207     appengine->load(QUrl::fromLocalFile(QStringLiteral("%1/khotnewstuff_test-ui/main.qml").arg(QStringLiteral(KNSSRCDIR))));
0208 
0209     // Don't really want to add messages until the tester
0210     // begins to actually request stuff in the UI,
0211     // so let's just install it here
0212     qInstallMessageHandler(debugOutputHandler);
0213 
0214     return app.exec();
0215 }
0216 
0217 #include "moc_khotnewstuff_test.cpp"