Changeset View
Changeset View
Standalone View
Standalone View
autotests/integration/colorcorrect_nightcolor_test.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | KWin - the KDE window manager | ||||
3 | This file is part of the KDE project. | ||||
4 | | ||||
5 | Copyright 2017 Roman Gilg <subdiff@gmail.com> | ||||
6 | | ||||
7 | This program is free software; you can redistribute it and/or modify | ||||
8 | it under the terms of the GNU General Public License as published by | ||||
9 | the Free Software Foundation; either version 2 of the License, or | ||||
10 | (at your option) any later version. | ||||
11 | | ||||
12 | This program is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | GNU General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU General Public License | ||||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | *********************************************************************/ | ||||
20 | #include "kwin_wayland_test.h" | ||||
21 | | ||||
22 | #include "platform.h" | ||||
23 | #include "wayland_server.h" | ||||
24 | #include "colorcorrection/nightcolor.h" | ||||
25 | #include "colorcorrection/constants.h" | ||||
26 | | ||||
27 | #include <KConfigGroup> | ||||
28 | | ||||
29 | using namespace KWin; | ||||
30 | | ||||
31 | static const QString s_socketName = QStringLiteral("wayland_test_kwin_colorcorrect_nightcolor-0"); | ||||
32 | | ||||
33 | class ColorCorrectNightColorTest : public QObject | ||||
34 | { | ||||
35 | Q_OBJECT | ||||
36 | private Q_SLOTS: | ||||
37 | void initTestCase(); | ||||
38 | void init(); | ||||
39 | void cleanup(); | ||||
40 | | ||||
41 | void testConfigRead_data(); | ||||
42 | void testConfigRead(); | ||||
43 | void testChangeConfiguration_data(); | ||||
44 | void testChangeConfiguration(); | ||||
45 | void testAutoLocationUpdate(); | ||||
46 | }; | ||||
47 | | ||||
48 | void ColorCorrectNightColorTest::initTestCase() | ||||
49 | { | ||||
50 | QSignalSpy workspaceCreatedSpy(kwinApp(), &Application::workspaceCreated); | ||||
51 | QVERIFY(workspaceCreatedSpy.isValid()); | ||||
52 | QMetaObject::invokeMethod(kwinApp()->platform(), "setOutputCount", Qt::DirectConnection, Q_ARG(int, 2)); | ||||
53 | QVERIFY(waylandServer()->init(s_socketName.toLocal8Bit())); | ||||
54 | | ||||
55 | kwinApp()->setConfig(KSharedConfig::openConfig(QString(), KConfig::SimpleConfig)); | ||||
56 | | ||||
57 | kwinApp()->start(); | ||||
58 | QVERIFY(workspaceCreatedSpy.wait()); | ||||
59 | waylandServer()->initWorkspace(); | ||||
60 | } | ||||
61 | | ||||
62 | void ColorCorrectNightColorTest::init() | ||||
63 | { | ||||
64 | } | ||||
65 | | ||||
66 | void ColorCorrectNightColorTest::cleanup() | ||||
67 | { | ||||
68 | } | ||||
69 | | ||||
70 | void ColorCorrectNightColorTest::testConfigRead_data() | ||||
71 | { | ||||
72 | QTest::addColumn<QString>("active"); | ||||
73 | QTest::addColumn<int>("mode"); | ||||
74 | QTest::addColumn<int>("nightTemperature"); | ||||
75 | QTest::addColumn<double>("latitudeFixed"); | ||||
76 | QTest::addColumn<double>("longitudeFixed"); | ||||
77 | QTest::addColumn<QString>("morningBeginFixed"); | ||||
78 | QTest::addColumn<QString>("eveningBeginFixed"); | ||||
79 | QTest::addColumn<int>("transitionTime"); | ||||
80 | QTest::addColumn<bool>("success"); | ||||
81 | | ||||
82 | QTest::newRow("activeMode0") << "true" << 0 << 4500 << 45.5 << 35.1 << "0600" << "1800" << 30 << true; | ||||
83 | QTest::newRow("activeMode1") << "true" << 1 << 2500 << -10.5 << -8. << "0020" << "2000" << 60 << true; | ||||
84 | QTest::newRow("notActiveMode2") << "false" << 2 << 5000 << 90. << -180. << "0600" << "1800" << 1 << true; | ||||
85 | QTest::newRow("wrongData1") << "fa" << 3 << 7000 << 91. << -181. << "060" << "800" << 999999 << false; | ||||
86 | QTest::newRow("wrongData2") << "fa" << 3 << 7000 << 91. << -181. << "060" << "800" << -2 << false; | ||||
87 | } | ||||
88 | | ||||
89 | void ColorCorrectNightColorTest::testConfigRead() | ||||
90 | { | ||||
91 | QFETCH(QString, active); | ||||
92 | QFETCH(int, mode); | ||||
93 | QFETCH(int, nightTemperature); | ||||
94 | QFETCH(double, latitudeFixed); | ||||
95 | QFETCH(double, longitudeFixed); | ||||
96 | QFETCH(QString, morningBeginFixed); | ||||
97 | QFETCH(QString, eveningBeginFixed); | ||||
98 | QFETCH(int, transitionTime); | ||||
99 | QFETCH(bool, success); | ||||
100 | | ||||
101 | const bool activeDefault = true; | ||||
102 | const int modeDefault = 0; | ||||
103 | const int nightTemperatureUpperEnd = ColorCorrect::NEUTRAL_TEMPERATURE; | ||||
104 | const double latitudeFixedDefault = 0; | ||||
105 | const double longitudeFixedDefault = 0; | ||||
106 | const QTime morningBeginFixedDefault = QTime(6,0,0); | ||||
107 | const QTime eveningBeginFixedDefault = QTime(18,0,0); | ||||
108 | const int transitionTimeDefault = 30; | ||||
109 | | ||||
110 | KConfigGroup cfgGroup = kwinApp()->config()->group("NightColor"); | ||||
111 | | ||||
112 | cfgGroup.writeEntry("Active", activeDefault); | ||||
113 | cfgGroup.writeEntry("Mode", modeDefault); | ||||
114 | cfgGroup.writeEntry("NightTemperature", nightTemperatureUpperEnd); | ||||
115 | cfgGroup.writeEntry("LatitudeFixed", latitudeFixedDefault); | ||||
116 | cfgGroup.writeEntry("LongitudeFixed", longitudeFixedDefault); | ||||
117 | cfgGroup.writeEntry("MorningBeginFixed", morningBeginFixedDefault.toString("hhmm")); | ||||
118 | cfgGroup.writeEntry("EveningBeginFixed", eveningBeginFixedDefault.toString("hhmm")); | ||||
119 | cfgGroup.writeEntry("TransitionTime", transitionTimeDefault); | ||||
120 | | ||||
121 | ColorCorrect::Manager *manager = kwinApp()->platform()->nightColorManager(); | ||||
122 | manager->reparseConfigAndReset(); | ||||
123 | auto info = manager->info(); | ||||
124 | QVERIFY(!info.isEmpty()); | ||||
125 | | ||||
126 | QCOMPARE(info.value("Active").toBool(), activeDefault); | ||||
127 | QCOMPARE(info.value("Mode").toInt(), modeDefault); | ||||
128 | QCOMPARE(info.value("NightTemperature").toInt(), nightTemperatureUpperEnd); | ||||
129 | QCOMPARE(info.value("LatitudeFixed").toDouble(), latitudeFixedDefault); | ||||
130 | QCOMPARE(info.value("LongitudeFixed").toDouble(), longitudeFixedDefault); | ||||
131 | QCOMPARE(QTime::fromString(info.value("MorningBeginFixed").toString(), Qt::ISODate), morningBeginFixedDefault); | ||||
132 | QCOMPARE(QTime::fromString(info.value("EveningBeginFixed").toString(), Qt::ISODate), eveningBeginFixedDefault); | ||||
133 | QCOMPARE(info.value("TransitionTime").toInt(), transitionTimeDefault); | ||||
134 | | ||||
135 | cfgGroup.writeEntry("Active", active); | ||||
136 | cfgGroup.writeEntry("Mode", mode); | ||||
137 | cfgGroup.writeEntry("NightTemperature", nightTemperature); | ||||
138 | cfgGroup.writeEntry("LatitudeFixed", latitudeFixed); | ||||
139 | cfgGroup.writeEntry("LongitudeFixed", longitudeFixed); | ||||
140 | cfgGroup.writeEntry("MorningBeginFixed", morningBeginFixed); | ||||
141 | cfgGroup.writeEntry("EveningBeginFixed", eveningBeginFixed); | ||||
142 | cfgGroup.writeEntry("TransitionTime", transitionTime); | ||||
143 | | ||||
144 | manager->reparseConfigAndReset(); | ||||
145 | info = manager->info(); | ||||
146 | QVERIFY(!info.isEmpty()); | ||||
147 | | ||||
148 | if (success) { | ||||
149 | QCOMPARE(info.value("Active").toBool() ? QString("true") : QString("false"), active); | ||||
150 | QCOMPARE(info.value("Mode").toInt(), mode); | ||||
151 | QCOMPARE(info.value("NightTemperature").toInt(), nightTemperature); | ||||
152 | QCOMPARE(info.value("LatitudeFixed").toDouble(), latitudeFixed); | ||||
153 | QCOMPARE(info.value("LongitudeFixed").toDouble(), longitudeFixed); | ||||
154 | QCOMPARE(QTime::fromString(info.value("MorningBeginFixed").toString(), Qt::ISODate), QTime::fromString(morningBeginFixed, "hhmm")); | ||||
155 | QCOMPARE(QTime::fromString(info.value("EveningBeginFixed").toString(), Qt::ISODate), QTime::fromString(eveningBeginFixed, "hhmm")); | ||||
156 | QCOMPARE(info.value("TransitionTime").toInt(), transitionTime); | ||||
157 | } else { | ||||
158 | QCOMPARE(info.value("Active").toBool(), activeDefault); | ||||
159 | QCOMPARE(info.value("Mode").toInt(), modeDefault); | ||||
160 | QCOMPARE(info.value("NightTemperature").toInt(), nightTemperatureUpperEnd); | ||||
161 | QCOMPARE(info.value("LatitudeFixed").toDouble(), latitudeFixedDefault); | ||||
162 | QCOMPARE(info.value("LongitudeFixed").toDouble(), longitudeFixedDefault); | ||||
163 | QCOMPARE(QTime::fromString(info.value("MorningBeginFixed").toString(), Qt::ISODate), morningBeginFixedDefault); | ||||
164 | QCOMPARE(QTime::fromString(info.value("EveningBeginFixed").toString(), Qt::ISODate), eveningBeginFixedDefault); | ||||
165 | QCOMPARE(info.value("TransitionTime").toInt(), transitionTimeDefault); | ||||
166 | } | ||||
167 | } | ||||
168 | | ||||
169 | void ColorCorrectNightColorTest::testChangeConfiguration_data() | ||||
170 | { | ||||
171 | QTest::addColumn<bool>("activeReadIn"); | ||||
172 | QTest::addColumn<int>("modeReadIn"); | ||||
173 | QTest::addColumn<int>("nightTemperatureReadIn"); | ||||
174 | QTest::addColumn<double>("latitudeFixedReadIn"); | ||||
175 | QTest::addColumn<double>("longitudeFixedReadIn"); | ||||
176 | QTest::addColumn<QTime>("morBeginFixedReadIn"); | ||||
177 | QTest::addColumn<QTime>("eveBeginFixedReadIn"); | ||||
178 | QTest::addColumn<int>("transitionTimeReadIn"); | ||||
179 | QTest::addColumn<bool>("successReadIn"); | ||||
180 | | ||||
181 | QTest::newRow("data0") << true << 0 << 4500 << 45.5 << 35.1 << QTime(6,0,0) << QTime(18,0,0) << 30 << true; | ||||
182 | QTest::newRow("data1") << true << 1 << 2500 << -10.5 << -8. << QTime(0,2,0) << QTime(20,0,0) << 60 << true; | ||||
183 | QTest::newRow("data2") << false << 2 << 5000 << 90. << -180. << QTime(6,0,0) << QTime(19,1,1) << 1 << true; | ||||
184 | QTest::newRow("wrongData0") << true << 3 << 4500 << 0. << 0. << QTime(6,0,0) << QTime(18,0,0) << 30 << false; | ||||
185 | QTest::newRow("wrongData1") << true << 0 << 500 << 0. << 0. << QTime(6,0,0) << QTime(18,0,0) << 30 << false; | ||||
186 | QTest::newRow("wrongData2") << true << 0 << 7000 << 0. << 0. << QTime(6,0,0) << QTime(18,0,0) << 30 << false; | ||||
187 | QTest::newRow("wrongData3") << true << 0 << 4500 << 91. << -181. << QTime(6,0,0) << QTime(18,0,0) << 30 << false; | ||||
188 | QTest::newRow("wrongData4") << true << 0 << 4500 << 0. << 0. << QTime(18,0,0) << QTime(6,0,0) << 30 << false; | ||||
189 | QTest::newRow("wrongData5") << true << 0 << 4500 << 0. << 0. << QTime(6,0,0) << QTime(18,0,0) << 0 << false; | ||||
190 | QTest::newRow("wrongData6") << true << 0 << 4500 << 0. << 0. << QTime(6,0,0) << QTime(18,0,0) << -1 << false; | ||||
191 | QTest::newRow("wrongData7") << true << 0 << 4500 << 0. << 0. << QTime(12,0,0) << QTime(12,30,0) << 30 << false; | ||||
192 | QTest::newRow("wrongData8") << true << 0 << 4500 << 0. << 0. << QTime(1,0,0) << QTime(23,30,0) << 90 << false; | ||||
193 | } | ||||
194 | | ||||
195 | void ColorCorrectNightColorTest::testChangeConfiguration() | ||||
196 | { | ||||
197 | QFETCH(bool, activeReadIn); | ||||
198 | QFETCH(int, modeReadIn); | ||||
199 | QFETCH(int, nightTemperatureReadIn); | ||||
200 | QFETCH(double, latitudeFixedReadIn); | ||||
201 | QFETCH(double, longitudeFixedReadIn); | ||||
202 | QFETCH(QTime, morBeginFixedReadIn); | ||||
203 | QFETCH(QTime, eveBeginFixedReadIn); | ||||
204 | QFETCH(int, transitionTimeReadIn); | ||||
205 | QFETCH(bool, successReadIn); | ||||
206 | | ||||
207 | const bool activeDefault = true; | ||||
208 | const int modeDefault = 0; | ||||
209 | const int nightTemperatureDefault = ColorCorrect::DEFAULT_NIGHT_TEMPERATURE; | ||||
210 | const double latitudeFixedDefault = 0; | ||||
211 | const double longitudeFixedDefault = 0; | ||||
212 | const QTime morningBeginFixedDefault = QTime(6,0,0); | ||||
213 | const QTime eveningBeginFixedDefault = QTime(18,0,0); | ||||
214 | const int transitionTimeDefault = 30; | ||||
215 | | ||||
216 | // init with default values | ||||
217 | bool active = activeDefault; | ||||
218 | int mode = modeDefault; | ||||
219 | int nightTemperature = nightTemperatureDefault; | ||||
220 | double latitudeFixed = latitudeFixedDefault; | ||||
221 | double longitudeFixed = longitudeFixedDefault; | ||||
222 | QTime morningBeginFixed = morningBeginFixedDefault; | ||||
223 | QTime eveningBeginFixed = eveningBeginFixedDefault; | ||||
224 | int transitionTime = transitionTimeDefault; | ||||
225 | | ||||
226 | bool activeExpect = activeDefault; | ||||
227 | int modeExpect = modeDefault; | ||||
228 | int nightTemperatureExpect = nightTemperatureDefault; | ||||
229 | double latitudeFixedExpect = latitudeFixedDefault; | ||||
230 | double longitudeFixedExpect = longitudeFixedDefault; | ||||
231 | QTime morningBeginFixedExpect = morningBeginFixedDefault; | ||||
232 | QTime eveningBeginFixedExpect = eveningBeginFixedDefault; | ||||
233 | int transitionTimeExpect = transitionTimeDefault; | ||||
234 | | ||||
235 | QHash<QString, QVariant> data; | ||||
236 | | ||||
237 | auto setData = [&active, &mode, &nightTemperature, | ||||
238 | &latitudeFixed, &longitudeFixed, | ||||
239 | &morningBeginFixed, &eveningBeginFixed, &transitionTime, | ||||
240 | &data]() { | ||||
241 | data["Active"] = active; | ||||
242 | data["Mode"] = mode; | ||||
243 | data["NightTemperature"] = nightTemperature; | ||||
244 | | ||||
245 | data["LatitudeFixed"] = latitudeFixed; | ||||
246 | data["LongitudeFixed"] = longitudeFixed; | ||||
247 | | ||||
248 | data["MorningBeginFixed"] = morningBeginFixed.toString(Qt::ISODate); | ||||
249 | data["EveningBeginFixed"] = eveningBeginFixed.toString(Qt::ISODate); | ||||
250 | data["TransitionTime"] = transitionTime; | ||||
251 | }; | ||||
252 | | ||||
253 | auto compareValues = [&activeExpect, &modeExpect, &nightTemperatureExpect, | ||||
254 | &latitudeFixedExpect, &longitudeFixedExpect, | ||||
255 | &morningBeginFixedExpect, &eveningBeginFixedExpect, | ||||
256 | &transitionTimeExpect](QHash<QString, QVariant> info) { | ||||
257 | QCOMPARE(info.value("Active").toBool(), activeExpect); | ||||
258 | QCOMPARE(info.value("Mode").toInt(), modeExpect); | ||||
259 | QCOMPARE(info.value("NightTemperature").toInt(), nightTemperatureExpect); | ||||
260 | QCOMPARE(info.value("LatitudeFixed").toDouble(), latitudeFixedExpect); | ||||
261 | QCOMPARE(info.value("LongitudeFixed").toDouble(), longitudeFixedExpect); | ||||
262 | QCOMPARE(info.value("MorningBeginFixed").toString(), morningBeginFixedExpect.toString(Qt::ISODate)); | ||||
263 | QCOMPARE(info.value("EveningBeginFixed").toString(), eveningBeginFixedExpect.toString(Qt::ISODate)); | ||||
264 | QCOMPARE(info.value("TransitionTime").toInt(), transitionTimeExpect); | ||||
265 | }; | ||||
266 | | ||||
267 | ColorCorrect::Manager *manager = kwinApp()->platform()->nightColorManager(); | ||||
268 | | ||||
269 | // test with default values | ||||
270 | setData(); | ||||
271 | manager->changeConfiguration(data); | ||||
272 | compareValues(manager->info()); | ||||
273 | | ||||
274 | // set to test values | ||||
275 | active = activeReadIn; | ||||
276 | mode = modeReadIn; | ||||
277 | nightTemperature = nightTemperatureReadIn; | ||||
278 | latitudeFixed = latitudeFixedReadIn; | ||||
279 | longitudeFixed = longitudeFixedReadIn; | ||||
280 | morningBeginFixed = morBeginFixedReadIn; | ||||
281 | eveningBeginFixed = eveBeginFixedReadIn; | ||||
282 | transitionTime = transitionTimeReadIn; | ||||
283 | | ||||
284 | if (successReadIn) { | ||||
285 | activeExpect = activeReadIn; | ||||
286 | modeExpect = modeReadIn; | ||||
287 | nightTemperatureExpect = nightTemperatureReadIn; | ||||
288 | latitudeFixedExpect = latitudeFixedReadIn; | ||||
289 | longitudeFixedExpect = longitudeFixedReadIn; | ||||
290 | morningBeginFixedExpect = morBeginFixedReadIn; | ||||
291 | eveningBeginFixedExpect = eveBeginFixedReadIn; | ||||
292 | transitionTimeExpect = transitionTimeReadIn; | ||||
293 | } | ||||
294 | | ||||
295 | // test with test values | ||||
296 | setData(); | ||||
297 | QCOMPARE(manager->changeConfiguration(data), successReadIn); | ||||
298 | compareValues(manager->info()); | ||||
299 | } | ||||
300 | | ||||
301 | void ColorCorrectNightColorTest::testAutoLocationUpdate() | ||||
302 | { | ||||
303 | ColorCorrect::Manager *manager = kwinApp()->platform()->nightColorManager(); | ||||
304 | auto info = manager->info(); | ||||
305 | QCOMPARE(info.value("LatitudeAuto").toDouble(), 0.); | ||||
306 | QCOMPARE(info.value("LongitudeAuto").toDouble(), 0.); | ||||
307 | | ||||
308 | // wrong latitude value | ||||
309 | manager->autoLocationUpdate(91, 15); | ||||
310 | info = manager->info(); | ||||
311 | QCOMPARE(info.value("LatitudeAuto").toDouble(), 0.); | ||||
312 | QCOMPARE(info.value("LongitudeAuto").toDouble(), 0.); | ||||
313 | | ||||
314 | // wrong longitude value | ||||
315 | manager->autoLocationUpdate(50, -181); | ||||
316 | info = manager->info(); | ||||
317 | QCOMPARE(info.value("LatitudeAuto").toDouble(), 0.); | ||||
318 | QCOMPARE(info.value("LongitudeAuto").toDouble(), 0.); | ||||
319 | | ||||
320 | // change | ||||
321 | manager->autoLocationUpdate(50, -180); | ||||
322 | info = manager->info(); | ||||
323 | QCOMPARE(info.value("LatitudeAuto").toDouble(), 50.); | ||||
324 | QCOMPARE(info.value("LongitudeAuto").toDouble(), -180.); | ||||
325 | | ||||
326 | // small deviation only | ||||
327 | manager->autoLocationUpdate(51.5, -179.5); | ||||
328 | info = manager->info(); | ||||
329 | QCOMPARE(info.value("LongitudeAuto").toDouble(), -180.); | ||||
330 | QCOMPARE(info.value("LatitudeAuto").toDouble(), 50.); | ||||
331 | } | ||||
332 | | ||||
333 | WAYLANDTEST_MAIN(ColorCorrectNightColorTest) | ||||
334 | #include "colorcorrect_nightcolor_test.moc" |