File indexing completed on 2023-10-03 06:52:06

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 <QtTest>
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 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         QCOMPARE(downloadManager->getVoicesResourceForLocale(locale),
0054                  QString("data2/voices-%1/voices-%2.rcc").arg(COMPRESSED_AUDIO, language));
0055     }
0056 
0057     void test_haveLocalResource_data()
0058     {
0059         QTest::addColumn<QString>("resource");
0060         QTest::addColumn<bool>("expected_success");
0061 
0062         QTest::newRow("invalid.invalid") << "invalid.invalid" << false;
0063         QTest::newRow("no.no") << "no.no" << false;
0064         QTest::newRow("money.rcc") << "money.rcc" << true;
0065         QTest::newRow("penalty.rcc") << "penalty.rcc" << true;
0066     }
0067     void test_haveLocalResource()
0068     {
0069         QFETCH(QString, resource);
0070         QFETCH(bool, expected_success);
0071 
0072         QVERIFY(expected_success == downloadManager->haveLocalResource(resource));
0073     }
0074 
0075     void test_downloadResource_data()
0076     {
0077         QTest::addColumn<QString>("resource");
0078 
0079         QTest::newRow("invalid.blabla") << "invalid.blabla";
0080         QTest::newRow("algorithm.rcc") << "algorithm.rcc";
0081     }
0082     void test_downloadResource()
0083     {
0084         QFETCH(QString, resource);
0085 
0086         QVERIFY(downloadManager->downloadResource(resource));
0087         QVERIFY(!downloadManager->downloadResource(resource));
0088         downloadManager->abortDownloads();
0089     }
0090 
0091     void test_updateResource_data()
0092     {
0093         QTest::addColumn<QString>("resource");
0094         QTest::addColumn<bool>("expected_success");
0095 
0096         QTest::newRow("invalid.haha") << "invalid.haha" << false;
0097         QTest::newRow("money.rcc") << "money.rcc" << true;
0098     }
0099     void test_updateResource()
0100     {
0101         QFETCH(QString, resource);
0102         QFETCH(bool, expected_success);
0103 
0104         QVERIFY(expected_success == downloadManager->updateResource(resource));
0105     }
0106 
0107     void test_downloadIsRunning_data()
0108     {
0109         QTest::addColumn<QString>("resource");
0110 
0111         QTest::newRow("colors.rcc") << "colors.rcc";
0112     }
0113     void test_downloadIsRunning()
0114     {
0115         QFETCH(QString, resource);
0116 
0117         downloadManager->abortDownloads();
0118         QVERIFY(!downloadManager->downloadIsRunning());
0119         QVERIFY(downloadManager->downloadResource(resource));
0120         QVERIFY(downloadManager->downloadIsRunning());
0121         downloadManager->abortDownloads();
0122     }
0123 
0124     void test_registerResource_data()
0125     {
0126         QTest::addColumn<QString>("resource");
0127         QTest::addColumn<bool>("expected_success");
0128 
0129         QTest::newRow("invalid.rcc") << "invalid.rcc" << false;
0130         QTest::newRow("money.rcc") << "money.rcc" << true;
0131     }
0132     void test_registerResource()
0133     {
0134         QFETCH(QString, resource);
0135         QFETCH(bool, expected_success);
0136 
0137         QVERIFY(expected_success == downloadManager->registerResource(resource));
0138     }
0139 
0140     void test_isDataRegistered_data()
0141     {
0142         QTest::addColumn<QString>("resource");
0143         QTest::addColumn<QString>("register_mode");
0144 
0145         QTest::newRow("invalid.rcc") << "invalid.rcc" << "invalid";
0146         QTest::newRow("mosaic.rcc") << "mosaic.rcc" << "not_registered";
0147     }
0148     void test_isDataRegistered()
0149     {
0150         QFETCH(QString, resource);
0151         QFETCH(QString, register_mode);
0152 
0153         if(register_mode == "not_registered") {
0154             QVERIFY(!downloadManager->isDataRegistered(resource));
0155         }
0156         else if(register_mode == "invalid") {
0157             QVERIFY(!downloadManager->registerResource(resource));
0158             QVERIFY(!downloadManager->isDataRegistered(resource));
0159         }
0160         else {
0161             QVERIFY(false);
0162         }
0163     }
0164     
0165 private:
0166     /**
0167      * @brief downloadManager The DownloadManager object, that is the test downloadManager
0168      */
0169     DownloadManager *downloadManager;
0170 };
0171 
0172 QTEST_MAIN(DownloadManagerTest);
0173 #include "DownloadManagerTest.moc"