Changeset View
Standalone View
tests/core/ApplicationSettingsTest.cpp
- This file was added.
1 | /* GCompris - ApplicationSettingTest.cpp | ||||
---|---|---|---|---|---|
2 | * | ||||
jjazeix: ApplicationSettingsTest.cpp | |||||
3 | * Copyright (C) 2018 Himanshu Vishwakarma <himvish997@gmail.com> | ||||
4 | * | ||||
5 | * Authors: | ||||
6 | * Himanshu Vishwakarma <himvish997@gmail.com> | ||||
7 | * | ||||
8 | * This program is free software; you can redistribute it and/or modify | ||||
9 | * it under the terms of the GNU General Public License as published by | ||||
10 | * the Free Software Foundation; either version 3 of the License, or | ||||
11 | * (at your option) any later version. | ||||
12 | * | ||||
13 | * This program is distributed in the hope that it will be useful, | ||||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
16 | * GNU General Public License for more details. | ||||
17 | * | ||||
18 | * You should have received a copy of the GNU General Public License | ||||
19 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | ||||
20 | */ | ||||
21 | | ||||
22 | #include <QtTest> | ||||
23 | #include <QObject> | ||||
24 | | ||||
25 | #include "src/core/ApplicationSettings.h" | ||||
26 | | ||||
27 | #define APPLICATION_SETTINGS_TEST_ATTRIBUTE(attributeType, attributeName, accessorName, accessorNameChanged) \ | ||||
28 | { \ | ||||
29 | QFETCH(attributeType, attributeName); \ | ||||
30 | QSignalSpy spy(applicationsettings, &ApplicationSettings::accessorNameChanged); \ | ||||
31 | QVERIFY(spy.count() == 0); \ | ||||
32 | applicationsettings->accessorName(attributeName); \ | ||||
33 | QVERIFY(spy.count() == 1); \ | ||||
34 | QCOMPARE(applicationsettings->attributeName(), attributeName); \ | ||||
35 | } | ||||
36 | | ||||
37 | class CoreApplicationSettingsTest : public QObject | ||||
38 | { | ||||
can you create new files (cpp, h) for this mock (ApplicationSettingsMock), it can be used on other tests? jjazeix: can you create new files (cpp, h) for this mock (ApplicationSettingsMock), it can be used on… | |||||
39 | Q_OBJECT | ||||
40 | private slots: | ||||
41 | void ApplicationSettingsInitializationTest(); | ||||
42 | void ApplicationSettingsTest(); | ||||
try to make a separate config file for the test which does not affect the application settings. But not get success. Any help or suggestion ?? himanshuvishwakarma: try to make a separate config file for the test which does not affect the application settings. | |||||
I see 2 potential solutions: I let you investigate a bit more to look for them and choose :). jjazeix: I see 2 potential solutions:
either inheritance and protected
or
friend keyword.
I let you… | |||||
Yaa, I tried both methods but both of them compelled to change in class ApplicationSettings. which we have to test. So I find a nice approach by this our default configuration is not going to change. In this method we are first saving the default data(which is already present in the config file ), then test the unit test with our data and again reset the default data. In this approach, we don't need to make the dummy class for the testing and not to change in the ApplicationSettings class. now everything is working fine. himanshuvishwakarma: Yaa, I tried both methods but both of them compelled to change in class ApplicationSettings. | |||||
What happens if for some reason the test crashes while there is a change done? jjazeix: What happens if for some reason the test crashes while there is a change done?
Also, you are… | |||||
43 | void ApplicationSettingsTest_data(); | ||||
44 | void ActivitySettingsTest(); | ||||
45 | private: | ||||
jjazeix: indentation should be 4 spaces on c++ files. | |||||
46 | ApplicationSettings* applicationsettings = ApplicationSettings::getInstance(); | ||||
47 | }; | ||||
48 | | ||||
49 | void CoreApplicationSettingsTest::ApplicationSettingsInitializationTest() | ||||
50 | { | ||||
51 | QCOMPARE(applicationsettings->baseFontSizeMin(), -7); | ||||
you don't have to redefine a new attribute with the same name, it won't do what you expect and it does not correspond to the one in the parent class jjazeix: you don't have to redefine a new attribute with the same name, it won't do what you expect and… | |||||
Okay, next time this will not happen and now NO need of this. himanshuvishwakarma: Okay, next time this will not happen and now NO need of this. | |||||
52 | QCOMPARE(applicationsettings->baseFontSizeMax(), 7); | ||||
53 | QCOMPARE(applicationsettings->fontLetterSpacingMin(), (qreal)0.0); | ||||
54 | QCOMPARE(applicationsettings->fontLetterSpacingMax(), (qreal)8.0); | ||||
55 | } | ||||
56 | | ||||
57 | void CoreApplicationSettingsTest::ApplicationSettingsTest_data() | ||||
58 | { | ||||
59 | QTest::addColumn<bool>("showLockedActivities"); | ||||
60 | QTest::addColumn<bool>("isAudioVoicesEnabled"); | ||||
61 | QTest::addColumn<bool>("isAudioEffectsEnabled"); | ||||
62 | QTest::addColumn<bool>("isFullscreen"); | ||||
63 | QTest::addColumn<qint32>("previousHeight"); | ||||
this one is never freed so it leaks, don't use pointer when you don't need them :) jjazeix: this one is never freed so it leaks, don't use pointer when you don't need them :)
And don't… | |||||
himanshuvishwakarma: here also, I remember it for the next time. | |||||
64 | QTest::addColumn<qint32>("previousWidth"); | ||||
65 | QTest::addColumn<bool>("isVirtualKeyboard"); | ||||
66 | QTest::addColumn<QString>("locale"); | ||||
67 | QTest::addColumn<QString>("font"); | ||||
68 | QTest::addColumn<bool>("isEmbeddedFont"); | ||||
69 | QTest::addColumn<quint32>("fontCapitalization"); | ||||
70 | QTest::addColumn<qreal>("fontLetterSpacing"); | ||||
71 | QTest::addColumn<bool>("isAutomaticDownloadsEnabled"); | ||||
72 | QTest::addColumn<quint32>("filterLevelMin"); | ||||
73 | QTest::addColumn<quint32>("filterLevelMax"); | ||||
74 | QTest::addColumn<bool>("isDemoMode"); | ||||
75 | QTest::addColumn<QString>("codeKey"); | ||||
76 | QTest::addColumn<bool>("isKioskMode"); | ||||
77 | QTest::addColumn<bool>("sectionVisible"); | ||||
78 | QTest::addColumn<QString>("wordset"); | ||||
79 | QTest::addColumn<QString>("downloadServerUrl"); | ||||
80 | QTest::addColumn<QString>("cachePath"); | ||||
81 | QTest::addColumn<quint32>("exeCount"); | ||||
82 | QTest::addColumn<bool>("isBarHidden"); | ||||
83 | QTest::addColumn<int>("baseFontSize"); | ||||
84 | QTest::addColumn<int>("lastGCVersionRan"); | ||||
85 | | ||||
86 | QTest::newRow("dummySettings1") << true << true << true << true << (qint32)21 << (qint32)25 << true << "locale1" << "font1" << true << (quint32)36 << (qreal)2.532 << true << (quint32)26 << (quint32)84 << true << "codeKey1" << true << true << "wordset1" << "downloadServerUrl1" << "cachePath1" << (quint32)48 << true << 7 << 52 ; | ||||
87 | QTest::newRow("dummySettings2") << false << false << false << true << (qint32)20 << (qint32)32 << false << "locale2" << "font2" << false << (quint32)34 <<(qreal)2.3 << false << (quint32)24 << (quint32)80 << false << "codekey2" << false << false << "wordset2" << "downloadServerUrl2" << "cachePath2" << (quint32)44 << false << 5 << 64 ; | ||||
88 | } | ||||
89 | | ||||
90 | void CoreApplicationSettingsTest::ApplicationSettingsTest() | ||||
91 | { | ||||
92 | ApplicationSettings* applicationsettings = new ApplicationSettings(nullptr); | ||||
jjazeix: applicationSettings | |||||
93 | // Setting Up null is required as it may already be present in memory because of unsuccessful test/ interruption | ||||
It would be better to override the class with a constructor that initialize these fields. It would be nice to also test the content of the configuration file at the end of the function to be sure all the content has been written as expected. jjazeix: It would be better to override the class with a constructor that initialize these fields.
The… | |||||
Yes, it changed the settings of the application. So As per your recommendation, I have created a DummyApplicationSettings class in the unit test and inherited from ApplicationSettings class and in the constructor of the class I override and initialized these fields with NULL, please give me any solution, link to tutorial or blog regarding the same. One another thing we may introduce in our GCompris is RestoreDefaultSettings(A member function of class ApplicationSettings) in the settings section by which the user can restore all default settings. himanshuvishwakarma: Yes, it changed the settings of the application. So As per your recommendation, I have created… | |||||
94 | applicationsettings->setFullscreen(NULL); | ||||
95 | applicationsettings->setPreviousHeight(NULL); | ||||
96 | applicationsettings->setPreviousWidth(NULL); | ||||
97 | applicationsettings->setBaseFontSize(NULL); | ||||
98 | | ||||
99 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, showLockedActivities, setShowLockedActivities, showLockedActivitiesChanged); | ||||
100 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isAudioVoicesEnabled, setIsAudioVoicesEnabled, audioVoicesEnabledChanged); | ||||
101 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isAudioEffectsEnabled, setIsAudioEffectsEnabled, audioEffectsEnabledChanged); | ||||
102 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isFullscreen, setFullscreen, fullscreenChanged); | ||||
103 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(qint32, previousHeight, setPreviousHeight, previousHeightChanged); | ||||
104 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(qint32, previousWidth, setPreviousWidth, previousWidthChanged); | ||||
105 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isVirtualKeyboard, setVirtualKeyboard, virtualKeyboardChanged); | ||||
106 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(QString, locale, setLocale, localeChanged); | ||||
107 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(QString, font, setFont, fontChanged); | ||||
108 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isEmbeddedFont, setIsEmbeddedFont, embeddedFontChanged); | ||||
109 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(quint32, fontCapitalization, setFontCapitalization, fontCapitalizationChanged); | ||||
110 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(qreal, fontLetterSpacing, setFontLetterSpacing, fontLetterSpacingChanged); | ||||
111 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isAutomaticDownloadsEnabled, setIsAutomaticDownloadsEnabled, automaticDownloadsEnabledChanged); | ||||
112 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(quint32, filterLevelMin, setFilterLevelMin, filterLevelMinChanged); | ||||
113 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(quint32, filterLevelMax, setFilterLevelMax, filterLevelMaxChanged); | ||||
114 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isDemoMode, setDemoMode, demoModeChanged); | ||||
115 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(QString, codeKey, setCodeKey, codeKeyChanged); | ||||
116 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isKioskMode, setKioskMode, kioskModeChanged); | ||||
117 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, sectionVisible, setSectionVisible, sectionVisibleChanged); | ||||
118 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(QString, wordset, setWordset, wordsetChanged); | ||||
119 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(QString, downloadServerUrl, setDownloadServerUrl, downloadServerUrlChanged); | ||||
120 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(QString, cachePath, setCachePath, cachePathChanged); | ||||
121 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(quint32, exeCount, setExeCount, exeCountChanged); | ||||
122 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(bool, isBarHidden, setBarHidden, barHiddenChanged); | ||||
123 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(int, baseFontSize, setBaseFontSize, baseFontSizeChanged); | ||||
124 | APPLICATION_SETTINGS_TEST_ATTRIBUTE(int, lastGCVersionRan, setLastGCVersionRan, lastGCVersionRanChanged); | ||||
125 | | ||||
126 | delete applicationsettings; | ||||
127 | } | ||||
128 | | ||||
129 | void CoreApplicationSettingsTest::ActivitySettingsTest() | ||||
130 | { | ||||
131 | // Creating a dummyActivity | ||||
132 | QString DummyActivity = QStringLiteral("DummyActivity"); | ||||
133 | // Setting Up null is required as it may already be present in memory because of unsuccessful test/ interruption | ||||
134 | applicationsettings->setFavorite(DummyActivity, NULL); | ||||
135 | applicationsettings->saveActivityProgress(DummyActivity, NULL); | ||||
136 | applicationsettings->saveActivityConfiguration(DummyActivity, QVariantMap()); | ||||
137 | | ||||
138 | // By Default the DummyActivity is not favorite | ||||
139 | QVERIFY(!applicationsettings->isFavorite(DummyActivity)); | ||||
140 | // Setting Up the DummyActivity as Favorite | ||||
141 | applicationsettings->setFavorite(DummyActivity, true); | ||||
142 | QVERIFY(applicationsettings->isFavorite(DummyActivity)); | ||||
143 | // setting Up the DummyActivity as Not favorite | ||||
144 | applicationsettings->setFavorite(DummyActivity, false); | ||||
jjazeix: variable names should start with a lowercase letter | |||||
himanshuvishwakarma: here also, I remember it for the next time. | |||||
145 | QVERIFY(!applicationsettings->isFavorite(DummyActivity)); | ||||
146 | | ||||
147 | // By Default the activity progress is zero | ||||
148 | QCOMPARE(applicationsettings->loadActivityProgress(DummyActivity), 0); | ||||
149 | // Saving the Activity Progress | ||||
150 | applicationsettings->saveActivityProgress(DummyActivity, 3); | ||||
151 | QCOMPARE(applicationsettings->loadActivityProgress(DummyActivity), 3); | ||||
152 | applicationsettings->saveActivityProgress(DummyActivity, 10); | ||||
153 | QCOMPARE(applicationsettings->loadActivityProgress(DummyActivity), 10); | ||||
154 | applicationsettings->saveActivityProgress(DummyActivity, 0); | ||||
155 | QCOMPARE(applicationsettings->loadActivityProgress(DummyActivity), 0); | ||||
156 | | ||||
157 | // By Default the activity | ||||
158 | QVariantMap configuration; | ||||
159 | configuration.insert(QStringLiteral("DummyKey1"), QStringLiteral("DummyValue1")); | ||||
160 | configuration.insert(QStringLiteral("DummyKey2"), QStringLiteral("DummyValue2")); | ||||
161 | configuration.insert(QStringLiteral("DummyKey3"), QStringLiteral("DummyValue3")); | ||||
162 | configuration.insert(QStringLiteral("DummyKey4"), QStringLiteral("DummyValue4")); | ||||
163 | | ||||
164 | applicationsettings->saveActivityConfiguration(DummyActivity, configuration); | ||||
165 | QVariantMap newconfiguration = applicationsettings->loadActivityConfiguration(DummyActivity); | ||||
166 | | ||||
167 | QCOMPARE(newconfiguration.value(QStringLiteral("DummyKey1")), configuration.value(QStringLiteral("DummyKey1"))); | ||||
168 | QCOMPARE(newconfiguration.value(QStringLiteral("DummyKey2")), configuration.value(QStringLiteral("DummyKey2"))); | ||||
169 | QCOMPARE(newconfiguration.value(QStringLiteral("DummyKey3")), configuration.value(QStringLiteral("DummyKey3"))); | ||||
170 | QCOMPARE(newconfiguration.value(QStringLiteral("DummyKey4")), configuration.value(QStringLiteral("DummyKey4"))); | ||||
171 | } | ||||
172 | | ||||
173 | QTEST_MAIN(CoreApplicationSettingsTest) | ||||
174 | #include "ApplicationSettingsTest.moc" | ||||
175 | |
ApplicationSettingsTest.cpp