File indexing completed on 2024-12-08 06:36:40
0001 /* 0002 This file is part of KDE. 0003 0004 SPDX-FileCopyrightText: 2018 Ralf Habacker <ralf.habacker@freenet.de> 0005 0006 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #include <QEventLoop> 0010 #include <QTest> 0011 #include <QTimer> 0012 0013 #include <QLoggingCategory> 0014 0015 #include "config.h" 0016 #include "content.h" 0017 #include "providermanager.h" 0018 0019 using namespace Attica; 0020 0021 class ProviderTest : public QObject 0022 { 0023 Q_OBJECT 0024 public: 0025 ProviderTest(); 0026 ~ProviderTest() override; 0027 0028 private: 0029 void initProvider(const QUrl &url); 0030 0031 private Q_SLOTS: 0032 void testFetchValidProvider(); 0033 void testFetchInvalidProvider(); 0034 void testSwitchSortOrder(); 0035 0036 protected Q_SLOTS: 0037 void providerAdded(Attica::Provider p); 0038 void slotDefaultProvidersLoaded(); 0039 void slotConfigResult(Attica::BaseJob *j); 0040 void slotListResult(Attica::BaseJob *j); 0041 void slotList2Result(Attica::BaseJob *j); 0042 void slotTimeout(); 0043 0044 private: 0045 Attica::ProviderManager *m_manager; 0046 QEventLoop *m_eventloop; 0047 QTimer m_timer; 0048 bool m_errorReceived = false; 0049 }; 0050 0051 ProviderTest::ProviderTest() 0052 : m_manager(nullptr) 0053 , m_eventloop(new QEventLoop) 0054 { 0055 QLoggingCategory::setFilterRules(QStringLiteral("kf.attica.debug=true")); 0056 } 0057 0058 ProviderTest::~ProviderTest() 0059 { 0060 delete m_manager; 0061 delete m_eventloop; 0062 } 0063 0064 void ProviderTest::slotDefaultProvidersLoaded() 0065 { 0066 qDebug() << "default providers loaded"; 0067 m_eventloop->exit(); 0068 } 0069 0070 void ProviderTest::providerAdded(Attica::Provider p) 0071 { 0072 qDebug() << "got provider" << p.name(); 0073 m_eventloop->exit(); 0074 } 0075 0076 void ProviderTest::initProvider(const QUrl &url) 0077 { 0078 m_errorReceived = false; 0079 delete m_manager; 0080 m_manager = new Attica::ProviderManager; 0081 m_manager->setAuthenticationSuppressed(true); 0082 connect(m_manager, &ProviderManager::defaultProvidersLoaded, this, &ProviderTest::slotDefaultProvidersLoaded); 0083 connect(m_manager, &ProviderManager::providerAdded, this, &ProviderTest::providerAdded); 0084 m_manager->addProviderFile(url); 0085 connect(m_manager, &Attica::ProviderManager::failedToLoad, this, [this]() { 0086 m_errorReceived = true; 0087 m_eventloop->quit(); 0088 }); 0089 m_timer.singleShot(30000, this, &ProviderTest::slotTimeout); 0090 0091 m_eventloop->exec(); 0092 } 0093 0094 void ProviderTest::testFetchValidProvider() 0095 { 0096 initProvider(QUrl(QLatin1String("https://autoconfig.kde.org/ocs/providers.xml"))); 0097 Attica::Provider provider = m_manager->providers().at(0); 0098 ItemJob<Config> *job = provider.requestConfig(); 0099 QVERIFY(job); 0100 connect(job, &BaseJob::finished, this, &ProviderTest::slotConfigResult); 0101 job->start(); 0102 m_eventloop->exec(); 0103 } 0104 0105 void ProviderTest::slotConfigResult(Attica::BaseJob *j) 0106 { 0107 if (j->metadata().error() == Metadata::NoError) { 0108 Attica::ItemJob<Config> *itemJob = static_cast<Attica::ItemJob<Config> *>(j); 0109 Attica::Config p = itemJob->result(); 0110 qDebug() << QLatin1String("Config loaded - Server has version") << p.version(); 0111 } else if (j->metadata().error() == Metadata::OcsError) { 0112 qDebug() << QString(QLatin1String("OCS Error: %1")).arg(j->metadata().message()); 0113 } else if (j->metadata().error() == Metadata::NetworkError) { 0114 qDebug() << QString(QLatin1String("Network Error: %1")).arg(j->metadata().message()); 0115 } else { 0116 qDebug() << QString(QLatin1String("Unknown Error: %1")).arg(j->metadata().message()); 0117 } 0118 m_eventloop->exit(); 0119 m_timer.stop(); 0120 QVERIFY(j->metadata().error() == Metadata::NoError); 0121 } 0122 0123 void ProviderTest::testSwitchSortOrder() 0124 { 0125 initProvider(QUrl(QLatin1String("https://autoconfig.kde.org/ocs/providers.xml"))); 0126 Attica::Provider provider = m_manager->providers().at(0); 0127 ListJob<Content> *job = provider.searchContents({}, {}); 0128 QVERIFY(job); 0129 connect(job, &BaseJob::finished, this, &ProviderTest::slotListResult); 0130 job->start(); 0131 m_eventloop->exec(); 0132 } 0133 0134 void ProviderTest::slotListResult(Attica::BaseJob *j) 0135 { 0136 if (j->metadata().error() == Metadata::NoError) { 0137 Attica::ListJob<Content> *contentJob = static_cast<Attica::ListJob<Content> *>(j); 0138 Content::List items = contentJob->itemList(); 0139 qDebug() << QLatin1String("First list of items loaded, we have the following amount:") << items.count(); 0140 } else if (j->metadata().error() == Metadata::OcsError) { 0141 qDebug() << QString(QLatin1String("OCS Error: %1")).arg(j->metadata().message()); 0142 } else if (j->metadata().error() == Metadata::NetworkError) { 0143 qDebug() << QString(QLatin1String("Network Error: %1")).arg(j->metadata().message()); 0144 } else { 0145 qDebug() << QString(QLatin1String("Unknown Error: %1")).arg(j->metadata().message()); 0146 } 0147 m_timer.stop(); 0148 QVERIFY(j->metadata().error() == Metadata::NoError); 0149 0150 // Now do the actual switch 0151 Attica::Provider provider = m_manager->providers().at(0); 0152 ListJob<Content> *job = provider.searchContents({}, {}, Provider::Downloads); 0153 QVERIFY(job); 0154 connect(job, &BaseJob::finished, this, &ProviderTest::slotList2Result); 0155 m_timer.singleShot(30000, this, &ProviderTest::slotTimeout); 0156 job->start(); 0157 } 0158 0159 void ProviderTest::slotList2Result(Attica::BaseJob *j) 0160 { 0161 if (j->metadata().error() == Metadata::NoError) { 0162 Attica::ListJob<Content> *contentJob = static_cast<Attica::ListJob<Content> *>(j); 0163 Content::List items = contentJob->itemList(); 0164 qDebug() << QLatin1String("Second list of items loaded, we have the following amount:") << items.count(); 0165 } else if (j->metadata().error() == Metadata::OcsError) { 0166 qDebug() << QString(QLatin1String("OCS Error: %1")).arg(j->metadata().message()); 0167 } else if (j->metadata().error() == Metadata::NetworkError) { 0168 qDebug() << QString(QLatin1String("Network Error: %1")).arg(j->metadata().message()); 0169 } else { 0170 qDebug() << QString(QLatin1String("Unknown Error: %1")).arg(j->metadata().message()); 0171 } 0172 m_eventloop->exit(); 0173 m_timer.stop(); 0174 QVERIFY(j->metadata().error() == Metadata::NoError); 0175 } 0176 0177 void ProviderTest::slotTimeout() 0178 { 0179 QVERIFY(m_eventloop->isRunning()); 0180 m_eventloop->exit(); 0181 QFAIL("Timeout fetching provider"); 0182 } 0183 0184 void ProviderTest::testFetchInvalidProvider() 0185 { 0186 initProvider(QUrl(QLatin1String("https://invalid-url.org/ocs/providers.xml"))); 0187 QVERIFY(m_manager->providers().size() == 0); 0188 QVERIFY(m_errorReceived); 0189 } 0190 0191 QTEST_GUILESS_MAIN(ProviderTest) 0192 0193 #include "providertest.moc"