GCC Code Coverage Report


Directory: ./
File: autotests/generator.cpp
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 162 165 98.2%
Branches: 108 208 51.9%

Line Branch Exec Source
1 /*************************************************************************
2 Copyright © 2020 Roman Gilg <subdiff@gmail.com>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 **************************************************************************/
18 #include <QtTest>
19
20 #include "backendmanager_p.h"
21 #include "generator.h"
22 #include "getconfigoperation.h"
23 #include "output.h"
24
25 using namespace Disman;
26
27 class TestGenerator : public QObject
28 {
29 Q_OBJECT
30
31 private Q_SLOTS:
32 void initTestCase();
33 void cleanupTestCase();
34
35 void single_output();
36 void multi_output_embedded();
37 void replicate_embedded();
38 void multi_output_pc();
39 void replicate_pc();
40
41 private:
42 Disman::ConfigPtr load_config(QByteArray const& file_name);
43 };
44
45 1 void TestGenerator::initTestCase()
46 {
47 1 qputenv("DISMAN_LOGGING", "false");
48 1 qputenv("DISMAN_BACKEND", "fake");
49 1 }
50
51 1 void TestGenerator::cleanupTestCase()
52 {
53 1 BackendManager::instance()->shutdown_backend();
54 1 }
55
56 5 Disman::ConfigPtr TestGenerator::load_config(const QByteArray& file_name)
57 {
58 5 qputenv("DISMAN_IN_PROCESS", "1");
59 5 auto path = "TEST_DATA=" TEST_DATA;
60 5 qputenv("DISMAN_BACKEND_ARGS", path + file_name);
61
62 5 auto op = new GetConfigOperation();
63
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if (!op->exec()) {
64 qWarning("ConfigOperation error: %s", qPrintable(op->error_string()));
65 BackendManager::instance()->shutdown_backend();
66 return ConfigPtr();
67 }
68
69 5 BackendManager::instance()->shutdown_backend();
70 5 return op->config();
71 }
72
73 1 void TestGenerator::single_output()
74 {
75 1 auto config = load_config("singleoutput.json");
76
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(config);
77 1 config->set_supported_features(Config::Feature::PrimaryDisplay);
78
79 // First make the received config worse.
80 1 config->set_primary_output(nullptr);
81
82 1 auto output = config->outputs().at(1);
83 1 output->set_enabled(false);
84 1 output->set_mode(output->modes().at("2"));
85
86
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(!config->primary_output());
87
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(output->enabled(), false);
88
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(output->auto_mode()->id(), "2");
89
90 // Now optimize the config.
91 1 Generator generator(config);
92
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(generator.optimize());
93
94 1 auto generated_config = generator.config();
95 1 output = generated_config->outputs().at(1);
96
97
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(generated_config->primary_output());
98
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(generated_config->primary_output()->id(), output->id());
99
100
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(output->auto_mode()->id(), "3");
101
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(output->enabled(), true);
102
1/2
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 QCOMPARE(output->position(), QPoint(0, 0));
103
4/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
1 }
104
105 1 void TestGenerator::multi_output_embedded()
106 {
107 1 auto config = load_config("multipleoutput.json");
108
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(config);
109
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QCOMPARE(config->outputs().size(), 2);
110 2 config->set_supported_features(Config::Feature::PerOutputScaling
111 1 | Config::Feature::PrimaryDisplay);
112
113 // First make the received config worse.
114 1 config->set_primary_output(nullptr);
115
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(!config->primary_output());
116
117
2/2
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 1 times.
3 for (auto const& [key, output] : config->outputs()) {
118 2 output->set_enabled(false);
119 2 output->set_mode(output->modes().at("3"));
120
121
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 QCOMPARE(output->enabled(), false);
122
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
2 QCOMPARE(output->auto_mode()->id(), "3");
123
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 }
124
125 1 Generator generator(config);
126
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(generator.optimize());
127
128 1 auto generated_config = generator.config();
129 1 auto output = generated_config->outputs().at(1);
130
131
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(generated_config->primary_output());
132
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(generated_config->primary_output()->id(), output->id());
133
134
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(generator.embedded(), output);
135 // Preferred mode is 2 on multipleoutput.json, so we expect 2
136 // When using auto mode.
137
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(output->auto_mode()->id(), "2");
138
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(output->enabled(), true);
139
1/2
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 QCOMPARE(output->position(), QPoint(0, 0));
140
141 1 output = generated_config->outputs().at(2);
142
143
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QCOMPARE(generator.biggest(), output);
144
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(output->auto_mode()->id(), "4");
145
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(output->enabled(), true);
146
1/2
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 QCOMPARE(output->position(), QPointF(1024, 0));
147
4/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
1 }
148
149 1 void TestGenerator::replicate_embedded()
150 {
151 1 auto config = load_config("multipleoutput.json");
152
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(config);
153
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QCOMPARE(config->outputs().size(), 2);
154 2 config->set_supported_features(Config::Feature::PerOutputScaling
155 1 | Config::Feature::OutputReplication
156 1 | Config::Feature::PrimaryDisplay);
157
158 // First make the received config worse.
159 1 config->set_primary_output(nullptr);
160
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(!config->primary_output());
161
162
2/2
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 1 times.
3 for (auto const& [key, output] : config->outputs()) {
163 2 output->set_enabled(false);
164 2 output->set_mode(output->modes().at("3"));
165
166
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 QCOMPARE(output->enabled(), false);
167
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
2 QCOMPARE(output->auto_mode()->id(), "3");
168
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 }
169
170 1 Generator generator(config);
171
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(generator.replicate());
172
173 1 auto generated_config = generator.config();
174 1 auto output = generated_config->outputs().at(1);
175
176
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(generated_config->primary_output());
177
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(generated_config->primary_output()->id(), output->id());
178
179
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(generator.embedded(), output);
180
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(output->auto_mode()->id(), "2");
181
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(output->enabled(), true);
182
1/2
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 QCOMPARE(output->position(), QPoint(0, 0));
183
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(output->replication_source(), 0);
184
185 1 output = generated_config->outputs().at(2);
186
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QCOMPARE(generator.biggest(), output);
187
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(output->auto_mode()->id(), "4");
188
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(output->enabled(), true);
189
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(output->replication_source(), 1);
190
4/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
1 }
191
192 1 void TestGenerator::multi_output_pc()
193 {
194 1 auto config = load_config("multipleoutput.json");
195
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(config);
196
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QCOMPARE(config->outputs().size(), 2);
197 2 config->set_supported_features(Config::Feature::PerOutputScaling
198 1 | Config::Feature::PrimaryDisplay);
199
200 // First make the received config worse.
201 1 config->set_primary_output(nullptr);
202
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(!config->primary_output());
203
204
2/2
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 1 times.
3 for (auto const& [key, output] : config->outputs()) {
205 2 output->set_enabled(false);
206 2 output->set_mode(output->modes().at("3"));
207 2 output->setType(Output::Type::HDMI);
208
209
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 QCOMPARE(output->enabled(), false);
210
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
2 QCOMPARE(output->auto_mode()->id(), "3");
211
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 }
212
213 1 Generator generator(config);
214
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(generator.optimize());
215
216 1 auto generated_config = generator.config();
217 1 auto output = generated_config->outputs().at(1);
218
219
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(output->auto_mode()->id(), "2");
220
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(output->enabled(), true);
221
1/2
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 QCOMPARE(output->position(), QPoint(1920, 0));
222
223 1 output = generated_config->outputs().at(2);
224
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(generated_config->primary_output());
225
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(generated_config->primary_output()->id(), output->id());
226
227
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QCOMPARE(generator.biggest(), output);
228
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(output->auto_mode()->id(), "4");
229
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(output->enabled(), true);
230
1/2
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 QCOMPARE(output->position(), QPointF(0, 0));
231
4/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
1 }
232
233 1 void TestGenerator::replicate_pc()
234 {
235 1 auto config = load_config("multipleoutput.json");
236
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(config);
237
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QCOMPARE(config->outputs().size(), 2);
238 2 config->set_supported_features(Config::Feature::PerOutputScaling
239 1 | Config::Feature::OutputReplication
240 1 | Config::Feature::PrimaryDisplay);
241
242 // First make the received config worse.
243 1 config->set_primary_output(nullptr);
244
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(!config->primary_output());
245
246
2/2
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 1 times.
3 for (auto const& [key, output] : config->outputs()) {
247 2 output->set_enabled(false);
248 2 output->set_mode(output->modes().at("3"));
249 2 output->setType(Output::Type::HDMI);
250
251
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 QCOMPARE(output->enabled(), false);
252
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
2 QCOMPARE(output->auto_mode()->id(), "3");
253
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 }
254
255 1 Generator generator(config);
256
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(generator.replicate());
257
258 1 auto generated_config = generator.config();
259 1 auto output = generated_config->outputs().at(1);
260
261
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(output->auto_mode()->id(), "2");
262
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(output->enabled(), true);
263
1/2
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 QCOMPARE(output->position(), QPoint(0, 0));
264
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(output->replication_source(), 2);
265
266 1 output = generated_config->outputs().at(2);
267
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(generated_config->primary_output());
268
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(generated_config->primary_output()->id(), output->id());
269
270
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QCOMPARE(generator.biggest(), output);
271
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(output->auto_mode()->id(), "4");
272
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(output->enabled(), true);
273
274 // Position is unchanged from initial config.
275
1/2
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 QCOMPARE(output->position(), QPoint(1280, 0));
276
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(output->replication_source(), 0);
277
4/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
1 }
278
279 1 QTEST_GUILESS_MAIN(TestGenerator)
280
281 #include "generator.moc"
282