GCC Code Coverage Report


Directory: ./
File: backends/fake/parser.cpp
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 96 133 72.2%
Branches: 33 76 43.4%

Line Branch Exec Source
1 /*************************************************************************************
2 * Copyright (C) 2012 by Alejandro Fiestas Olivares <afiestas@kde.org> *
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 "parser.h"
19 #include "fake.h"
20
21 #include "fake_logging.h"
22
23 #include "config.h"
24 #include "output.h"
25
26 #include <QFile>
27 #include <QJsonArray>
28 #include <QJsonDocument>
29 #include <QJsonObject>
30 #include <QLoggingCategory>
31 #include <QMetaObject>
32 #include <QMetaProperty>
33
34 using namespace Disman;
35
36 128 void Parser::fromJson(const QByteArray& data, ConfigPtr& config)
37 {
38 128 const QJsonObject json = QJsonDocument::fromJson(data).object();
39
40 ScreenPtr screen
41 128 = Parser::screenFromJson(json[QStringLiteral("screen")].toObject().toVariantMap());
42 128 config->setScreen(screen);
43
44 128 const QVariantList outputs = json[QStringLiteral("outputs")].toArray().toVariantList();
45
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 128 times.
128 if (outputs.isEmpty()) {
46 return;
47 }
48
49 128 OutputMap outputList;
50 128 OutputPtr primary;
51
2/2
✓ Branch 5 taken 220 times.
✓ Branch 6 taken 128 times.
348 for (auto const& value : outputs) {
52 220 bool is_primary = false;
53 220 const OutputPtr output = Parser::outputFromJson(value.toMap(), is_primary);
54 220 outputList.insert({output->id(), output});
55
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 92 times.
220 if (is_primary) {
56 128 primary = output;
57 }
58 220 }
59
60 128 config->set_outputs(outputList);
61 128 config->set_primary_output(primary);
62
3/6
✓ Branch 3 taken 128 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 128 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 128 times.
✗ Branch 10 not taken.
128 }
63
64 128 void Parser::fromJson(const QString& path, Disman::ConfigPtr& config)
65 {
66 128 QFile file(path);
67
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 128 times.
128 if (!file.open(QIODevice::ReadOnly)) {
68 qWarning() << file.errorString();
69 qWarning() << "File: " << path;
70 return;
71 }
72
73 128 Parser::fromJson(file.readAll(), config);
74
1/2
✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
128 }
75
76 128 ScreenPtr Parser::screenFromJson(const QVariantMap& data)
77 {
78 128 ScreenPtr screen(new Screen);
79 256 screen->set_id(data[QStringLiteral("id")].toInt());
80 256 screen->set_min_size(Parser::sizeFromJson(data[QStringLiteral("minSize")].toMap()));
81 256 screen->set_max_size(Parser::sizeFromJson(data[QStringLiteral("maxSize")].toMap()));
82 256 screen->set_current_size(Parser::sizeFromJson(data[QStringLiteral("currentSize")].toMap()));
83 256 screen->set_max_outputs_count(data[QStringLiteral("maxActiveOutputsCount")].toInt());
84
85 128 return screen;
86 }
87
88 220 OutputPtr Parser::outputFromJson(QMap<QString, QVariant> map, bool& primary)
89 {
90 220 OutputPtr output(new Output);
91 440 output->set_id(map[QStringLiteral("id")].toInt());
92 440 output->set_name(map[QStringLiteral("name")].toString().toStdString());
93 440 output->set_description(map[QStringLiteral("description")].toString().toStdString());
94 440 output->set_enabled(map[QStringLiteral("enabled")].toBool());
95 440 output->set_rotation((Output::Rotation)map[QStringLiteral("rotation")].toInt());
96
97 220 primary = map[QStringLiteral("primary")].toBool();
98
99 220 std::vector<std::string> preferredModes;
100 220 const QVariantList prefModes = map[QStringLiteral("preferredModes")].toList();
101
2/2
✓ Branch 4 taken 216 times.
✓ Branch 5 taken 220 times.
436 for (auto const& mode : prefModes) {
102 216 preferredModes.push_back(mode.toString().toStdString());
103 }
104 220 output->set_preferred_modes(preferredModes);
105
106 220 ModeMap modelist;
107 220 const QVariantList modes = map[QStringLiteral("modes")].toList();
108
2/2
✓ Branch 5 taken 716 times.
✓ Branch 6 taken 220 times.
936 for (auto const& modeValue : modes) {
109 716 const ModePtr mode = Parser::modeFromJson(modeValue);
110 716 modelist.insert({mode->id(), mode});
111 716 }
112 220 output->set_modes(modelist);
113
114
2/2
✓ Branch 7 taken 216 times.
✓ Branch 8 taken 4 times.
220 if (!map[QStringLiteral("currentModeId")].toString().isEmpty()) {
115 432 output->set_mode(
116 648 output->mode(map[QStringLiteral("currentModeId")].toString().toStdString()));
117 }
118
119 220 const QByteArray type = map[QStringLiteral("type")].toByteArray().toUpper();
120
2/4
✓ Branch 4 taken 92 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 92 times.
✗ Branch 9 not taken.
312 if (type.contains("LVDS") || type.contains("EDP") || type.contains("IDP")
121
5/6
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 128 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 92 times.
✓ Branch 6 taken 128 times.
✓ Branch 7 taken 92 times.
312 || type.contains("7")) {
122 128 output->setType(Output::Panel);
123
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 92 times.
92 } else if (type.contains("VGA")) {
124 output->setType(Output::VGA);
125
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 92 times.
92 } else if (type.contains("DVI")) {
126 output->setType(Output::DVI);
127
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 92 times.
92 } else if (type.contains("DVI-I")) {
128 output->setType(Output::DVII);
129
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 92 times.
92 } else if (type.contains("DVI-A")) {
130 output->setType(Output::DVIA);
131
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 92 times.
92 } else if (type.contains("DVI-D")) {
132 output->setType(Output::DVID);
133
2/6
✗ Branch 2 not taken.
✓ Branch 3 taken 92 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 92 times.
✗ Branch 9 not taken.
92 } else if (type.contains("HDMI") || type.contains("6")) {
134 92 output->setType(Output::HDMI);
135 } else if (type.contains("Panel")) {
136 output->setType(Output::Panel);
137 } else if (type.contains("TV")) {
138 output->setType(Output::TV);
139 } else if (type.contains("TV-Composite")) {
140 output->setType(Output::TVComposite);
141 } else if (type.contains("TV-SVideo")) {
142 output->setType(Output::TVSVideo);
143 } else if (type.contains("TV-Component")) {
144 output->setType(Output::TVComponent);
145 } else if (type.contains("TV-SCART")) {
146 output->setType(Output::TVSCART);
147 } else if (type.contains("TV-C4")) {
148 output->setType(Output::TVC4);
149 } else if (type.contains("DisplayPort") || type.contains("14")) {
150 output->setType(Output::DisplayPort);
151 } else if (type.contains("Unknown")) {
152 output->setType(Output::Unknown);
153 } else {
154 qCWarning(DISMAN_FAKE) << "Output Type not translated:" << type;
155 }
156
157
1/2
✓ Branch 4 taken 220 times.
✗ Branch 5 not taken.
220 if (map.contains(QStringLiteral("pos"))) {
158 440 output->set_position(Parser::pointFromJson(map[QStringLiteral("pos")].toMap()));
159 }
160
161 220 auto scale = QStringLiteral("scale");
162
2/2
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 136 times.
220 if (map.contains(scale)) {
163 84 qDebug() << "Scale found:" << map[scale].toReal();
164 84 output->set_scale(map[scale].toReal());
165 }
166
167 220 return output;
168 220 }
169
170 716 ModePtr Parser::modeFromJson(const QVariant& data)
171 {
172 716 const QVariantMap map = data.toMap();
173 716 ModePtr mode(new Mode);
174
175 1432 mode->set_id(map[QStringLiteral("id")].toString().toStdString());
176 1432 mode->set_name(map[QStringLiteral("name")].toString().toStdString());
177 1432 mode->set_refresh(map[QStringLiteral("refresh")].toDouble() * 1000);
178 1432 mode->set_size(Parser::sizeFromJson(map[QStringLiteral("size")].toMap()));
179
180 716 return mode;
181 716 }
182
183 1100 QSize Parser::sizeFromJson(const QVariant& data)
184 {
185 1100 const QVariantMap map = data.toMap();
186
187 1100 QSize size;
188 1100 size.setWidth(map[QStringLiteral("width")].toInt());
189 1100 size.setHeight(map[QStringLiteral("height")].toInt());
190
191 2200 return size;
192 1100 }
193
194 220 QPoint Parser::pointFromJson(const QVariant& data)
195 {
196 220 const QVariantMap map = data.toMap();
197
198 220 QPoint point;
199 220 point.setX(map[QStringLiteral("x")].toInt());
200 220 point.setY(map[QStringLiteral("y")].toInt());
201
202 440 return point;
203 220 }
204
205 QRect Parser::rectFromJson(const QVariant& data)
206 {
207 QRect rect;
208 rect.setSize(Parser::sizeFromJson(data));
209 rect.setBottomLeft(Parser::pointFromJson(data));
210
211 return rect;
212 }
213
214 bool Parser::validate(const QByteArray& data)
215 {
216 Q_UNUSED(data);
217 return true;
218 }
219
220 bool Parser::validate(const QString& data)
221 {
222 Q_UNUSED(data);
223 return true;
224 }
225