File indexing completed on 2024-12-01 03:28:59

0001 /* GCompris - DownloadManagerTest.cpp
0002  *
0003  * SPDX-FileCopyrightText: 2018 Alex Kovrigin <a.kovrigin0@gmail.com>
0004  * GCompris  (C) 2018 GCompris Developers <gcompris-devel@kde.org>
0005  *
0006  * Authors:
0007  *   Alex Kovrigin <a.kovrigin0@gmail.com>
0008  *
0009  *   SPDX-License-Identifier: GPL-3.0-or-later
0010  */
0011 
0012 #include <QObject>
0013 #include <QTest>
0014 #include <QString>
0015 
0016 #include "src/core/DownloadManager.h"
0017 #include "src/core/ApplicationInfo.h"
0018 #include "ApplicationSettingsMock.h"
0019 /**
0020  * @brief The DownloadManagerTest class Unit tests class for DownloadManager testing
0021  * @sa DownloadManager
0022  */
0023 class DownloadManagerTest : public QObject
0024 {
0025     Q_OBJECT
0026 
0027 private Q_SLOTS:
0028     /**
0029      * @brief initTestCase Case for basic functionality of DownloadManager
0030      */
0031     void initTestCase()
0032     {
0033         ApplicationSettingsMock::getInstance()->setIsAutomaticDownloadsEnabled(false);
0034         downloadManager = DownloadManager::getInstance();
0035     }
0036 
0037     void test_getVoicesResourceForLocale_data()
0038     {
0039         QTest::addColumn<QString>("locale");
0040         QTest::addColumn<QString>("language");
0041 
0042         QTest::newRow("en_US") << "en_US" << "en_US";
0043         QTest::newRow("en_UK") << "en_UK" << "en";
0044         QTest::newRow("ru_RU") << "ru_RU" << "ru";
0045         QTest::newRow("de_DE") << "de_DE" << "de";
0046         QTest::newRow("fr_FR") << "fr_FR" << "fr";
0047     }
0048     void test_getVoicesResourceForLocale()
0049     {
0050         QFETCH(QString, locale);
0051         QFETCH(QString, language);
0052 
0053         DownloadManager::DownloadJob job{GCompris::ResourceType::NONE};
0054         job.file.setFileName(QFINDTESTDATA("Contents.test"));
0055         downloadManager->parseContents(&job);
0056         QCOMPARE(downloadManager->getVoicesResourceForLocale(locale),
0057                  QString("data3/voices-%1/voices-%2-2023-10-07-17-29-00.rcc").arg(COMPRESSED_AUDIO, language));
0058     }
0059 
0060     void test_haveLocalResource_data()
0061     {
0062         QTest::addColumn<QString>("resource");
0063         QTest::addColumn<bool>("expected_success");
0064 
0065         QTest::newRow("invalid.invalid") << "invalid.invalid" << false;
0066         QTest::newRow("no.no") << "no.no" << false;
0067         QTest::newRow("money.rcc") << "money.rcc" << true;
0068         QTest::newRow("penalty.rcc") << "penalty.rcc" << true;
0069     }
0070     void test_haveLocalResource()
0071     {
0072         QFETCH(QString, resource);
0073         QFETCH(bool, expected_success);
0074 
0075         QVERIFY(expected_success == downloadManager->haveLocalResource(resource));
0076     }
0077 
0078     void test_downloadResource_data()
0079     {
0080         QTest::addColumn<GCompris::ResourceType>("resource");
0081 
0082         QTest::newRow("wordset") << GCompris::ResourceType::WORDSET;
0083         QTest::newRow("wordset") << GCompris::ResourceType::WORDSET;
0084     }
0085     void test_downloadResource()
0086     {
0087         QFETCH(GCompris::ResourceType, resource);
0088 
0089         QVERIFY(downloadManager->downloadResource(resource));
0090         QVERIFY(!downloadManager->downloadResource(resource));
0091         downloadManager->abortDownloads();
0092     }
0093 
0094     void test_updateResource_data()
0095     {
0096         QTest::addColumn<GCompris::ResourceType>("resource");
0097         QTest::addColumn<bool>("expected_success");
0098 
0099         QTest::newRow("wordset") << GCompris::ResourceType::WORDSET << false;
0100         QTest::newRow("none") << GCompris::ResourceType::NONE << false;
0101     }
0102     void test_updateResource()
0103     {
0104         QFETCH(GCompris::ResourceType, resource);
0105         QFETCH(bool, expected_success);
0106 
0107        QVERIFY(expected_success == downloadManager->updateResource(resource, {}));
0108     }
0109 
0110     void test_downloadIsRunning_data()
0111     {
0112         QTest::addColumn<GCompris::ResourceType>("resource");
0113 
0114         QTest::newRow("wordset") << GCompris::ResourceType::WORDSET;
0115     }
0116     void test_downloadIsRunning()
0117     {
0118         QFETCH(GCompris::ResourceType, resource);
0119 
0120         downloadManager->abortDownloads();
0121         QVERIFY(!downloadManager->downloadIsRunning());
0122         QVERIFY(downloadManager->downloadResource(resource));
0123         QVERIFY(downloadManager->downloadIsRunning());
0124         downloadManager->abortDownloads();
0125     }
0126 
0127     void test_registerResource_data()
0128     {
0129         QTest::addColumn<QString>("resource");
0130         QTest::addColumn<bool>("expected_success");
0131 
0132         QTest::newRow("invalid.rcc") << "invalid.rcc" << false;
0133         QTest::newRow("money.rcc") << "money.rcc" << true;
0134     }
0135     void test_registerResource()
0136     {
0137         QFETCH(QString, resource);
0138         QFETCH(bool, expected_success);
0139 
0140         QVERIFY(expected_success == downloadManager->registerResource(resource));
0141     }
0142 
0143     void test_isDataRegistered_data()
0144     {
0145         QTest::addColumn<QString>("resource");
0146         QTest::addColumn<QString>("register_mode");
0147 
0148         QTest::newRow("invalid.rcc") << "invalid.rcc" << "invalid";
0149         QTest::newRow("mosaic.rcc") << "mosaic.rcc" << "not_registered";
0150     }
0151     void test_isDataRegistered()
0152     {
0153         QFETCH(QString, resource);
0154         QFETCH(QString, register_mode);
0155 
0156         if(register_mode == "not_registered") {
0157             QVERIFY(!downloadManager->isDataRegistered(resource));
0158         }
0159         else if(register_mode == "invalid") {
0160             QVERIFY(!downloadManager->registerResource(resource));
0161             QVERIFY(!downloadManager->isDataRegistered(resource));
0162         }
0163         else {
0164             QVERIFY(false);
0165         }
0166     }
0167     
0168 private:
0169     /**
0170      * @brief downloadManager The DownloadManager object, that is the test downloadManager
0171      */
0172     DownloadManager *downloadManager;
0173 };
0174 
0175 QTEST_MAIN(DownloadManagerTest);
0176 #include "DownloadManagerTest.moc"