File indexing completed on 2024-10-06 06:29:27

0001 /* GCompris - ApplicationSettingsTest.cpp
0002  *
0003  * SPDX-FileCopyrightText: 2018 Himanshu Vishwakarma <himvish997@gmail.com>
0004  *
0005  * Authors:
0006  *   Himanshu Vishwakarma <himvish997@gmail.com>
0007  *
0008  *   SPDX-License-Identifier: GPL-3.0-or-later
0009  */
0010 
0011 #include <QTest>
0012 #include <QObject>
0013 #include <QFile>
0014 #include <QSignalSpy>
0015 
0016 #include "src/core/ApplicationSettings.h"
0017 #include "ApplicationSettingsMock.h"
0018 
0019 #define APPLICATION_SETTINGS_TEST_ATTRIBUTE(attributeType, attributeName, accessorName, accessorNameChanged) \
0020 { \
0021     QFETCH(attributeType, attributeName); \
0022     QSignalSpy spy(&applicationSettingsMock, &ApplicationSettings::accessorNameChanged); \
0023     QVERIFY(spy.isValid()); \
0024     QVERIFY(spy.count() == 0); \
0025     applicationSettingsMock.accessorName(attributeName); \
0026     QVERIFY(spy.count() == 1); \
0027     QCOMPARE(applicationSettingsMock.attributeName(), attributeName); \
0028 }
0029 
0030 class CoreApplicationSettingsTest : public QObject
0031 {
0032     Q_OBJECT
0033 private Q_SLOTS:
0034     void cleanup();
0035     void ApplicationSettingsInitializationTest();
0036     void ApplicationSettingsTest();
0037     void ApplicationSettingsTest_data();
0038     void ActivitySettingsTest();
0039 };
0040 
0041 void CoreApplicationSettingsTest::ApplicationSettingsInitializationTest()
0042 {
0043     ApplicationSettingsMock applicationSettings;
0044     QCOMPARE(applicationSettings.baseFontSizeMin(), -7);
0045     QCOMPARE(applicationSettings.baseFontSizeMax(), 7);
0046     QCOMPARE(applicationSettings.fontLetterSpacingMin(), (qreal)0.0);
0047     QCOMPARE(applicationSettings.fontLetterSpacingMax(), (qreal)8.0);
0048 }
0049 
0050 void CoreApplicationSettingsTest::ApplicationSettingsTest_data()
0051 {
0052     QTest::addColumn<bool>("isAudioVoicesEnabled");
0053     QTest::addColumn<bool>("isAudioEffectsEnabled");
0054     QTest::addColumn<bool>("isBackgroundMusicEnabled");
0055     QTest::addColumn<quint32>("previousHeight");
0056     QTest::addColumn<quint32>("previousWidth");
0057     QTest::addColumn<bool>("isVirtualKeyboard");
0058     QTest::addColumn<QString>("locale");
0059     QTest::addColumn<QString>("font");
0060     QTest::addColumn<bool>("isEmbeddedFont");
0061     QTest::addColumn<quint32>("fontCapitalization");
0062     QTest::addColumn<qreal>("fontLetterSpacing");
0063     QTest::addColumn<bool>("isAutomaticDownloadsEnabled");
0064     QTest::addColumn<quint32>("filterLevelMin");
0065     QTest::addColumn<quint32>("filterLevelMax");
0066     QTest::addColumn<bool>("isKioskMode");
0067     QTest::addColumn<bool>("sectionVisible");
0068     QTest::addColumn<QString>("downloadServerUrl");
0069     QTest::addColumn<QString>("cachePath");
0070     QTest::addColumn<QString>("userDataPath");
0071     QTest::addColumn<quint32>("exeCount");
0072     QTest::addColumn<bool>("isBarHidden");
0073     QTest::addColumn<int>("baseFontSize");
0074     QTest::addColumn<int>("lastGCVersionRan");
0075     QTest::addColumn<bool>("isFullscreen");
0076     QTest::addColumn<QString>("renderer");
0077     QTest::addColumn<qreal>("backgroundMusicVolume");
0078     QTest::addColumn<qreal>("audioEffectsVolume");
0079     QTest::addColumn<QStringList>("filteredBackgroundMusic");
0080     
0081     QTest::newRow("dummySettings1") << true << true << true << (quint32)21 << (quint32)25 << true << "en_EN" << "font1" << true << (quint32)36 << (qreal)2.532 << true << (quint32)26 << (quint32)84 << true << true << "downloadServerUrl1" << "cachePath1" << "userDataPath1" << (quint32)48 << true << 7 << 52 << false << "softwareRenderer" << (qreal)0.5 << (qreal)0.7 << (QStringList() << "music1");
0082     QTest::newRow("dummySettings2") << false << false << false << (quint32)20 << (quint32)32 << false << "en_US" << "font2" << false << (quint32)34 <<(qreal)2.3 << false << (quint32)24 << (quint32)80 << false << false << "downloadServerUrl2" << "cachePath2" << "userDataPath2" << (quint32)44 << false << 5 << 64 << false << "openglRenderer" << (qreal)1.0 << (qreal)0.0 << (QStringList() << "music2" << "music3");
0083 }
0084 
0085 void CoreApplicationSettingsTest::ApplicationSettingsTest()
0086 {
0087     ApplicationSettingsMock applicationSettingsMock;
0088     ApplicationSettingsMock::getInstance();
0089 
0090     APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isAudioVoicesEnabled, setIsAudioVoicesEnabled, audioVoicesEnabledChanged);
0091     APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isAudioEffectsEnabled, setIsAudioEffectsEnabled, audioEffectsEnabledChanged);
0092     APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isBackgroundMusicEnabled, setIsBackgroundMusicEnabled, backgroundMusicEnabledChanged);
0093     APPLICATION_SETTINGS_TEST_ATTRIBUTE(quint32, previousHeight, setPreviousHeight, previousHeightChanged);
0094     APPLICATION_SETTINGS_TEST_ATTRIBUTE(quint32, previousWidth, setPreviousWidth, previousWidthChanged);
0095     APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isVirtualKeyboard, setVirtualKeyboard, virtualKeyboardChanged);
0096     APPLICATION_SETTINGS_TEST_ATTRIBUTE(QString, locale, setLocale, localeChanged);
0097     APPLICATION_SETTINGS_TEST_ATTRIBUTE(QString, font, setFont, fontChanged);
0098     APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isEmbeddedFont, setIsEmbeddedFont, embeddedFontChanged);
0099     APPLICATION_SETTINGS_TEST_ATTRIBUTE(quint32, fontCapitalization, setFontCapitalization, fontCapitalizationChanged);
0100     APPLICATION_SETTINGS_TEST_ATTRIBUTE(qreal, fontLetterSpacing, setFontLetterSpacing, fontLetterSpacingChanged);
0101     APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isAutomaticDownloadsEnabled, setIsAutomaticDownloadsEnabled, automaticDownloadsEnabledChanged);
0102     APPLICATION_SETTINGS_TEST_ATTRIBUTE(quint32, filterLevelMin, setFilterLevelMin, filterLevelMinChanged);
0103     APPLICATION_SETTINGS_TEST_ATTRIBUTE(quint32, filterLevelMax, setFilterLevelMax, filterLevelMaxChanged);
0104     APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isKioskMode, setKioskMode, kioskModeChanged);
0105     APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, sectionVisible, setSectionVisible, sectionVisibleChanged);
0106     APPLICATION_SETTINGS_TEST_ATTRIBUTE(QString, downloadServerUrl, setDownloadServerUrl, downloadServerUrlChanged);
0107     APPLICATION_SETTINGS_TEST_ATTRIBUTE(QString, cachePath, setCachePath, cachePathChanged);
0108     APPLICATION_SETTINGS_TEST_ATTRIBUTE(QString, userDataPath, setUserDataPath, userDataPathChanged);
0109     APPLICATION_SETTINGS_TEST_ATTRIBUTE(quint32, exeCount, setExeCount, exeCountChanged);
0110     APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isBarHidden, setBarHidden, barHiddenChanged);
0111     APPLICATION_SETTINGS_TEST_ATTRIBUTE(int, baseFontSize, setBaseFontSize, baseFontSizeChanged);
0112     APPLICATION_SETTINGS_TEST_ATTRIBUTE(int, lastGCVersionRan, setLastGCVersionRan, lastGCVersionRanChanged);
0113     APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isFullscreen, setFullscreen, fullscreenChanged);
0114     APPLICATION_SETTINGS_TEST_ATTRIBUTE(QString, renderer, setRenderer, rendererChanged);
0115     APPLICATION_SETTINGS_TEST_ATTRIBUTE(qreal, backgroundMusicVolume, setBackgroundMusicVolume, backgroundMusicVolumeChanged);
0116     APPLICATION_SETTINGS_TEST_ATTRIBUTE(qreal, audioEffectsVolume, setAudioEffectsVolume, audioEffectsVolumeChanged);
0117     APPLICATION_SETTINGS_TEST_ATTRIBUTE(QStringList, filteredBackgroundMusic, setFilteredBackgroundMusic, filteredBackgroundMusicChanged);
0118 
0119     delete ApplicationSettingsMock::getInstance();
0120 }
0121 
0122 void CoreApplicationSettingsTest::ActivitySettingsTest()
0123 {
0124     ApplicationSettingsMock applicationSettingsMock;
0125     ApplicationSettingsMock::getInstance();
0126     // Creating a dummyActivity
0127     QString dummyActivity = QStringLiteral("DummyActivity");
0128 
0129     // By Default the DummyActivity is not favorite
0130     QVERIFY(!applicationSettingsMock.isFavorite(dummyActivity));
0131     // Setting Up the DummyActivity as Favorite
0132     applicationSettingsMock.setFavorite(dummyActivity, true);
0133     QVERIFY(applicationSettingsMock.isFavorite(dummyActivity));
0134     // setting Up the DummyActivity as Not favorite
0135     applicationSettingsMock.setFavorite(dummyActivity, false);
0136     QVERIFY(!applicationSettingsMock.isFavorite(dummyActivity));
0137 
0138     // By Default the activity progress is zero
0139     QCOMPARE(applicationSettingsMock.loadActivityProgress(dummyActivity), 0);
0140     // Saving the Activity Progress
0141     applicationSettingsMock.saveActivityProgress(dummyActivity, 3);
0142     QCOMPARE(applicationSettingsMock.loadActivityProgress(dummyActivity), 3);
0143     applicationSettingsMock.saveActivityProgress(dummyActivity, 10);
0144     QCOMPARE(applicationSettingsMock.loadActivityProgress(dummyActivity), 10);
0145     applicationSettingsMock.saveActivityProgress(dummyActivity, 0);
0146     QCOMPARE(applicationSettingsMock.loadActivityProgress(dummyActivity), 0);
0147 
0148     // Test current level getter/setter
0149     QCOMPARE(applicationSettingsMock.currentLevels(dummyActivity), {});
0150     applicationSettingsMock.setCurrentLevels(dummyActivity, {"3"});
0151     QCOMPARE(applicationSettingsMock.currentLevels(dummyActivity), {"3"});
0152 
0153     // By Default the activity
0154     QVariantMap configuration;
0155     configuration.insert(QStringLiteral("DummyKey1"), QStringLiteral("DummyValue1"));
0156     configuration.insert(QStringLiteral("DummyKey2"), QStringLiteral("DummyValue2"));
0157     configuration.insert(QStringLiteral("DummyKey3"), QStringLiteral("DummyValue3"));
0158     configuration.insert(QStringLiteral("DummyKey4"), QStringLiteral("DummyValue4"));
0159 
0160     applicationSettingsMock.saveActivityConfiguration(dummyActivity, configuration);
0161     QVariantMap newConfiguration = applicationSettingsMock.loadActivityConfiguration(dummyActivity);
0162 
0163     QCOMPARE(newConfiguration.value(QStringLiteral("DummyKey1")), configuration.value(QStringLiteral("DummyKey1")));
0164     QCOMPARE(newConfiguration.value(QStringLiteral("DummyKey2")), configuration.value(QStringLiteral("DummyKey2")));
0165     QCOMPARE(newConfiguration.value(QStringLiteral("DummyKey3")), configuration.value(QStringLiteral("DummyKey3")));
0166     QCOMPARE(newConfiguration.value(QStringLiteral("DummyKey4")), configuration.value(QStringLiteral("DummyKey4")));
0167 
0168     delete ApplicationSettingsMock::getInstance();
0169 }
0170 
0171 void CoreApplicationSettingsTest::cleanup()
0172 {
0173     QFile::remove("./dummy_application_settings.conf");
0174 }
0175 
0176 QTEST_MAIN(CoreApplicationSettingsTest)
0177 #include "ApplicationSettingsTest.moc"