1 /*
2  *  The ManaPlus Client
3  *  Copyright (C) 2016-2019  The ManaPlus Developers
4  *  Copyright (C) 2019-2021  Andrei Karas
5  *
6  *  This file is part of The ManaPlus Client.
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 2 of the License, or
11  *  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 "unittests/unittests.h"
23 
24 #include "configuration.h"
25 #include "configmanager.h"
26 #include "dirs.h"
27 
28 #include "listeners/configlistener.h"
29 
30 #include "debug.h"
31 
32 namespace
33 {
34     int mCalled = 0;
35 
36     class TestConfigListener : public ConfigListener
37     {
38         public:
optionChanged(const std::string & name)39             void optionChanged(const std::string &name) override final
40             {
41                 if (name == "testkey123")
42                     mCalled ++;
43             }
44     } testListener;
45 }  // namespace
46 
47 TEST_CASE("configuration tests", "configuration")
48 {
49     Dirs::initRootDir();
50     Dirs::initHomeDir();
51 
52     ConfigManager::initConfiguration();
53 
54     SECTION("configuration undefined")
55     {
56         const char *const key = "nonsetvalue";
57         REQUIRE(config.getValue(key, "not set") == "not set");
58         REQUIRE(config.getValue(key, 12345) == 12345);
59         REQUIRE(config.getValue(key, 12345U) == 12345U);
60         REQUIRE(config.getValueInt(key, 12345) == 12345);
61         REQUIRE(config.getValueBool(key, false) == false);
62         REQUIRE(config.getValueBool(key, true) == true);
63         REQUIRE(config.getValue(key, -12345) == -12345);
64         REQUIRE(config.getValue(key, 12.345) > 12.3);
65     }
66 
67     SECTION("configuration getint default")
68     {
69         const char *const key = "sfxVolume";
70         REQUIRE(config.getIntValue(key) == 100);
71         REQUIRE(config.getFloatValue(key) >= 100.0F);
72         REQUIRE(config.getStringValue(key) == "100");
73         REQUIRE(config.getBoolValue(key) == true);
74     }
75 
76     SECTION("configuration getfloat default")
77     {
78         const char *const key = "guialpha";
79         REQUIRE(config.getIntValue(key) == 0);
80         REQUIRE(config.getFloatValue(key) >= 0.8F);
81         REQUIRE(config.getStringValue(key).substr(0, 3) == "0.8");
82         REQUIRE(config.getBoolValue(key) == false);
83     }
84 
85     SECTION("configuration getstring default")
86     {
87         const char *const key = "soundwhisper";
88         REQUIRE(config.getIntValue(key) == 0);
89         REQUIRE(config.getFloatValue(key) >= 0.0F);
90         REQUIRE(config.getStringValue(key) == "newmessage");
91         REQUIRE(config.getBoolValue(key) == true);
92     }
93 
94     SECTION("configuration getbool default1")
95     {
96         const char *const key = "showgender";
97         REQUIRE(config.getIntValue(key) == 1);
98         REQUIRE(config.getFloatValue(key) >= 1.0F);
99         REQUIRE(config.getStringValue(key) == "1");
100         REQUIRE(config.getBoolValue(key) == true);
101     }
102 
103     SECTION("configuration getbool default2")
104     {
105         const char *const key = "showlevel";
106         REQUIRE(config.getIntValue(key) == 0);
107         REQUIRE(config.getFloatValue(key) >= 0.0F);
108         REQUIRE(config.getStringValue(key) == "0");
109         REQUIRE(config.getBoolValue(key) == false);
110     }
111 
112     SECTION("configuration getint set")
113     {
114         const char *const key = "sfxVolume";
115         config.setValue(key, 50);
116         REQUIRE(config.getIntValue(key) == 50);
117         REQUIRE(config.getFloatValue(key) >= 50.0F);
118         REQUIRE(config.getStringValue(key) == "50");
119         REQUIRE(config.getBoolValue(key) == true);
120 
121         REQUIRE(config.getValue(key, "not set") == "50");
122         REQUIRE(config.getValue(key, 12345) == 50);
123         REQUIRE(config.getValue(key, 12345U) == 50U);
124         REQUIRE(config.getValueInt(key, 12345) == 50);
125         REQUIRE(config.getValueBool(key, false) == true);
126         REQUIRE(config.getValueBool(key, true) == true);
127         REQUIRE(config.getValue(key, -12345) == 50);
128         REQUIRE(config.getValue(key, 12.345) >= 50.0);
129     }
130 
131     SECTION("configuration getfloat set")
132     {
133         const char *const key = "guialpha";
134         config.setValue(key, 50.5);
135         REQUIRE(config.getIntValue(key) == 50);
136         REQUIRE(config.getFloatValue(key) >= 50.4F);
137         REQUIRE(config.getStringValue(key).substr(0, 2) == "50");
138         REQUIRE(config.getBoolValue(key) == true);
139 
140         REQUIRE(config.getValue(key, "not set").substr(0, 2) == "50");
141         REQUIRE(config.getValue(key, 12345) == 50);
142         REQUIRE(config.getValue(key, 12345U) == 50U);
143         REQUIRE(config.getValueInt(key, 12345) == 50);
144         REQUIRE(config.getValueBool(key, false) == true);
145         REQUIRE(config.getValueBool(key, true) == true);
146         REQUIRE(config.getValue(key, -12345) == 50);
147         REQUIRE(config.getValue(key, 12.345) >= 50.4);
148     }
149 
150     SECTION("configuration getstring set")
151     {
152         const char *const key = "soundwhisper";
153         config.setValue(key, "test line");
154         REQUIRE(config.getIntValue(key) == 0);
155         REQUIRE(config.getFloatValue(key) >= 0.0F);
156         REQUIRE(config.getStringValue(key) == "test line");
157         REQUIRE(config.getBoolValue(key) == false);
158 
159         REQUIRE(config.getValue(key, "not set") == "test line");
160         REQUIRE(config.getValue(key, 12345) == 0);
161         REQUIRE(config.getValue(key, 12345U) == 0U);
162         REQUIRE(config.getValueInt(key, 12345) == 0);
163         REQUIRE(config.getValueBool(key, false) == false);
164         REQUIRE(config.getValueBool(key, true) == false);
165         REQUIRE(config.getValue(key, -12345) == 0);
166         REQUIRE(config.getValue(key, 12.345) >= 0.0);
167     }
168 
169     SECTION("configuration getbool set1")
170     {
171         const char *const key = "showgender";
172         config.setValue(key, true);
173         REQUIRE(config.getIntValue(key) == 1);
174         REQUIRE(config.getFloatValue(key) >= 1.0F);
175         REQUIRE(config.getStringValue(key) == "1");
176         REQUIRE(config.getBoolValue(key) == true);
177 
178         REQUIRE(config.getValue(key, "not set") == "1");
179         REQUIRE(config.getValue(key, 12345) == 1);
180         REQUIRE(config.getValue(key, 12345U) == 1U);
181         REQUIRE(config.getValueInt(key, 12345) == 1);
182         REQUIRE(config.getValueBool(key, false) == true);
183         REQUIRE(config.getValueBool(key, true) == true);
184         REQUIRE(config.getValue(key, -12345) == 1);
185         REQUIRE(config.getValue(key, 12.345) >= 1.0);
186     }
187 
188     SECTION("configuration getbool set2")
189     {
190         const char *const key = "showgender";
191         config.setValue(key, false);
192         REQUIRE(config.getIntValue(key) == 0);
193         REQUIRE(config.getFloatValue(key) >= 0.0F);
194         REQUIRE(config.getStringValue(key) == "0");
195         REQUIRE(config.getBoolValue(key) == false);
196 
197         REQUIRE(config.getValue(key, "not set") == "0");
198         REQUIRE(config.getValue(key, 12345) == 0);
199         REQUIRE(config.getValue(key, 12345U) == 0U);
200         REQUIRE(config.getValueInt(key, 12345) == 0);
201         REQUIRE(config.getValueBool(key, false) == false);
202         REQUIRE(config.getValueBool(key, true) == false);
203         REQUIRE(config.getValue(key, -12345) == 0);
204         REQUIRE(config.getValue(key, 12.345) >= 0.0);
205     }
206 
207     SECTION("configuration deletekey")
208     {
209         const char *const key = "testkey123";
210         config.setValue(key, 123);
211         REQUIRE(config.getValueInt(key, 12345) == 123);
212         config.deleteKey(key);
213         REQUIRE(config.getValueInt(key, 12345) == 12345);
214     }
215 
216     SECTION("configuration addlistener")
217     {
218         const char *const key = "testkey123";
219         REQUIRE(mCalled == 0);
220         config.addListener(key, &testListener);
221         REQUIRE(mCalled == 0);
222         config.setValue(key, 123);
223         REQUIRE(mCalled == 1);
224         REQUIRE(config.getValueInt(key, 12345) == 123);
225         REQUIRE(mCalled == 1);
226         config.setValue(key, 123);
227         REQUIRE(mCalled == 2);
228         config.setSilent(key, true);
229         REQUIRE(mCalled == 2);
230         REQUIRE(config.getBoolValue(key) == true);
231         REQUIRE(mCalled == 2);
232         config.setSilent(key, false);
233         REQUIRE(mCalled == 2);
234         REQUIRE(config.getBoolValue(key) == false);
235         REQUIRE(mCalled == 2);
236         config.removeListener(key, &testListener);
237     }
238 
239     SECTION("configuration incvalue")
240     {
241         const char *const key = "testkey123";
242         config.setValue(key, 10);
243         REQUIRE(config.getValueInt(key, 12345) == 10);
244         config.incValue(key);
245         REQUIRE(config.getValueInt(key, 12345) == 11);
246     }
247 
248     SECTION("configuration resetintvalue")
249     {
250         const char *const key = "sfxVolume";
251         config.setValue(key, 20);
252         REQUIRE(config.getIntValue(key) == 20);
253         REQUIRE(config.getFloatValue(key) >= 20.0F);
254         REQUIRE(config.getStringValue(key) == "20");
255         REQUIRE(config.getBoolValue(key) == true);
256         config.resetIntValue(key);
257         REQUIRE(config.getIntValue(key) == 100);
258         REQUIRE(config.getFloatValue(key) >= 100.0F);
259         REQUIRE(config.getStringValue(key) == "100");
260         REQUIRE(config.getBoolValue(key) == true);
261     }
262 
263     SECTION("configuration resetboolvalue1")
264     {
265         const char *const key = "showgender";
266         config.setValue(key, false);
267         REQUIRE(config.getIntValue(key) == 0);
268         REQUIRE(config.getFloatValue(key) >= 0.0F);
269         REQUIRE(config.getStringValue(key) == "0");
270         REQUIRE(config.getBoolValue(key) == false);
271 
272         config.resetBoolValue(key);
273         REQUIRE(config.getIntValue(key) == 1);
274         REQUIRE(config.getFloatValue(key) >= 1.0F);
275         REQUIRE(config.getStringValue(key) == "1");
276         REQUIRE(config.getBoolValue(key) == true);
277     }
278 
279     SECTION("configuration resetboolvalue2")
280     {
281         const char *const key = "showlevel";
282         config.setValue(key, true);
283         REQUIRE(config.getIntValue(key) == 1);
284         REQUIRE(config.getFloatValue(key) >= 1.0F);
285         REQUIRE(config.getStringValue(key) == "1");
286         REQUIRE(config.getBoolValue(key) == true);
287 
288         config.resetBoolValue(key);
289         REQUIRE(config.getIntValue(key) == 0);
290         REQUIRE(config.getFloatValue(key) >= 0.0F);
291         REQUIRE(config.getStringValue(key) == "0");
292         REQUIRE(config.getBoolValue(key) == false);
293     }
294 }
295