GCC Code Coverage Report


Directory: ./
File: autotests/testconfigserializer.cpp
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 113 113 100.0%
Branches: 70 134 52.2%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2014 Daniel Vratil <dvratil@redhat.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 Street, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 */
19 #include <QObject>
20 #include <QtTest>
21
22 #include "configserializer_p.h"
23 #include "mode.h"
24 #include "output.h"
25 #include "screen.h"
26 #include "types.h"
27
28 class TestConfigSerializer : public QObject
29 {
30 Q_OBJECT
31
32 public:
33 1 TestConfigSerializer()
34 1 {
35 1 }
36
37 private Q_SLOTS:
38 1 void testSerializePoint()
39 {
40 1 const QPoint point(42, 24);
41
42 1 const QJsonObject obj = Disman::ConfigSerializer::serialize_point(point);
43
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(!obj.isEmpty());
44
45
1/2
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
1 QCOMPARE(obj[QLatin1String("x")].toInt(), point.x());
46
1/2
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
1 QCOMPARE(obj[QLatin1String("y")].toInt(), point.y());
47
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 }
48
49 1 void testSerializeSize()
50 {
51 1 const QSize size(800, 600);
52
53 1 const QJsonObject obj = Disman::ConfigSerializer::serialize_size(size);
54
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(!obj.isEmpty());
55
56
1/2
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
1 QCOMPARE(obj[QLatin1String("width")].toInt(), size.width());
57
1/2
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
1 QCOMPARE(obj[QLatin1String("height")].toInt(), size.height());
58
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 }
59
60 1 void testSerializeList()
61 {
62 1 QStringList stringList;
63 3 stringList << QStringLiteral("Item 1") << QStringLiteral("Item 2")
64 2 << QStringLiteral("Item 3") << QStringLiteral("Item 4");
65
66 1 QJsonArray arr = Disman::ConfigSerializer::serialize_list<QString>(stringList);
67
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(arr.size(), stringList.size());
68
69
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
5 for (int i = 0; i < arr.size(); ++i) {
70
1/2
✗ Branch 6 not taken.
✓ Branch 7 taken 4 times.
4 QCOMPARE(arr.at(i).toString(), stringList.at(i));
71 }
72
73 1 QList<int> intList;
74 1 intList << 4 << 3 << 2 << 1;
75
76 1 arr = Disman::ConfigSerializer::serialize_list<int>(intList);
77
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(arr.size(), intList.size());
78
79
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
5 for (int i = 0; i < arr.size(); ++i) {
80
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
4 QCOMPARE(arr.at(i).toInt(), intList[i]);
81 }
82
3/6
✓ 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.
1 }
83
84 1 void testSerializeScreen()
85 {
86 1 Disman::ScreenPtr screen(new Disman::Screen);
87 1 screen->set_id(12);
88 1 screen->set_min_size(QSize(360, 360));
89 1 screen->set_max_size(QSize(8192, 8192));
90 1 screen->set_current_size(QSize(3600, 1280));
91 1 screen->set_max_outputs_count(3);
92
93 1 const QJsonObject obj = Disman::ConfigSerializer::serialize_screen(screen);
94
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(!obj.isEmpty());
95
96
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(obj[QLatin1String("id")].toInt(), screen->id());
97
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(obj[QLatin1String("max_outputs_count")].toInt(), screen->max_outputs_count());
98 1 const QJsonObject min_size = obj[QLatin1String("min_size")].toObject();
99
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(min_size[QLatin1String("width")].toInt(), screen->min_size().width());
100
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(min_size[QLatin1String("height")].toInt(), screen->min_size().height());
101 1 const QJsonObject max_size = obj[QLatin1String("max_size")].toObject();
102
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(max_size[QLatin1String("width")].toInt(), screen->max_size().width());
103
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(max_size[QLatin1String("height")].toInt(), screen->max_size().height());
104 1 const QJsonObject currSize = obj[QLatin1String("current_size")].toObject();
105
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(currSize[QLatin1String("width")].toInt(), screen->current_size().width());
106
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(currSize[QLatin1String("height")].toInt(), screen->current_size().height());
107
5/10
✓ 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.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
1 }
108
109 1 void testSerializeMode()
110 {
111 1 Disman::ModePtr mode(new Disman::Mode);
112 1 mode->set_id("755");
113 1 mode->set_name("1280x1024");
114 1 mode->set_refresh(50.666);
115 1 mode->set_size(QSize(1280, 1024));
116
117 1 const QJsonObject obj = Disman::ConfigSerializer::serialize_mode(mode);
118
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(!obj.isEmpty());
119
120
1/2
✗ Branch 11 not taken.
✓ Branch 12 taken 1 times.
1 QCOMPARE(obj[QLatin1String("id")].toString().toStdString(), mode->id());
121
1/2
✗ Branch 11 not taken.
✓ Branch 12 taken 1 times.
1 QCOMPARE(obj[QLatin1String("name")].toString().toStdString(), mode->name());
122
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(obj[QLatin1String("refresh")].toDouble(), mode->refresh());
123 1 const QJsonObject size = obj[QLatin1String("size")].toObject();
124
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(size[QLatin1String("width")].toInt(), mode->size().width());
125
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(size[QLatin1String("height")].toInt(), mode->size().height());
126
3/6
✓ 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.
1 }
127
128 1 void testSerializeOutput()
129 {
130 1 Disman::ModeMap modes;
131 1 Disman::ModePtr mode(new Disman::Mode);
132 1 mode->set_id("1");
133 1 mode->set_name("800x600");
134 1 mode->set_size(QSize(800, 600));
135 1 mode->set_refresh(50.4);
136 1 modes.insert({mode->id(), mode});
137
138 1 Disman::OutputPtr output(new Disman::Output);
139 1 output->set_id(60);
140 1 output->set_name("LVDS-0");
141 1 output->setType(Disman::Output::Panel);
142 1 output->set_modes(modes);
143 1 output->set_position(QPoint(1280, 0));
144 1 output->set_rotation(Disman::Output::None);
145
2/2
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 1 times.
2 output->set_preferred_modes({"1"});
146 1 output->set_enabled(true);
147 1 output->set_physical_size(QSize(310, 250));
148
149 1 const QJsonObject obj = Disman::ConfigSerializer::serialize_output(output);
150
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(!obj.isEmpty());
151
152
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(obj[QLatin1String("id")].toInt(), output->id());
153
1/2
✗ Branch 11 not taken.
✓ Branch 12 taken 1 times.
1 QCOMPARE(obj[QLatin1String("name")].toString(), QString::fromStdString(output->name()));
154
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(static_cast<Disman::Output::Type>(obj[QLatin1String("type")].toInt()),
155 output->type());
156 1 const QJsonArray arr = obj[QLatin1String("modes")].toArray();
157
1/2
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
1 QCOMPARE(arr.size(), output->modes().size());
158
159 1 QJsonObject pos = obj[QLatin1String("position")].toObject();
160
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(pos[QLatin1String("x")].toInt(), output->position().x());
161
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(pos[QLatin1String("y")].toInt(), output->position().y());
162
163
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(static_cast<Disman::Output::Rotation>(obj[QLatin1String("rotation")].toInt()),
164 output->rotation());
165
166 1 auto res = obj[QStringLiteral("resolution")].toObject();
167
1/2
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
2 QCOMPARE(res[QStringLiteral("width")].toInt(), output->auto_mode()->size().width());
168
1/2
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
2 QCOMPARE(res[QStringLiteral("height")].toInt(), output->auto_mode()->size().height());
169
170
1/2
✗ Branch 13 not taken.
✓ Branch 14 taken 1 times.
2 QCOMPARE(obj[QStringLiteral("refresh")].toDouble(), output->auto_mode()->refresh());
171
172
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
1 QCOMPARE(obj[QLatin1String("enabled")].toBool(), output->enabled());
173 1 const QJsonObject physical_size = obj[QLatin1String("physical_size")].toObject();
174
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(physical_size[QLatin1String("width")].toInt(), output->physical_size().width());
175
1/2
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QCOMPARE(physical_size[QLatin1String("height")].toInt(), output->physical_size().height());
176
177 1 output->set_mode(mode);
178
179 1 auto const obj2 = Disman::ConfigSerializer::serialize_output(output);
180
181 1 res = obj2[QStringLiteral("resolution")].toObject();
182
1/2
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
2 QCOMPARE(res[QStringLiteral("width")].toInt(), output->commanded_mode()->size().width());
183
1/2
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
2 QCOMPARE(res[QStringLiteral("height")].toInt(), output->commanded_mode()->size().height());
184
185
1/2
✗ Branch 13 not taken.
✓ Branch 14 taken 1 times.
2 QCOMPARE(obj2[QStringLiteral("refresh")].toDouble(), output->commanded_mode()->refresh());
186
9/18
✓ 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.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 1 times.
✗ Branch 26 not taken.
1 }
187 };
188
189 1 QTEST_MAIN(TestConfigSerializer)
190
191 #include "testconfigserializer.moc"
192