GCC Code Coverage Report


Directory: ./
File: backends/fake/fake.cpp
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 24 85 28.2%
Branches: 3 53 5.7%

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 "fake.h"
19 #include "parser.h"
20
21 #include "fake_logging.h"
22
23 #include "config.h"
24 #include <output.h>
25
26 #include <stdlib.h>
27
28 #include <QFile>
29 #include <QTimer>
30
31 #include <QJsonArray>
32 #include <QJsonDocument>
33 #include <QJsonObject>
34
35 #include <QDBusConnection>
36
37 #include "fakebackendadaptor.h"
38
39 using namespace Disman;
40
41 32 Fake::Fake()
42 32 : Disman::BackendImpl()
43 {
44 32 QLoggingCategory::setFilterRules(QStringLiteral("disman.fake.debug = true"));
45
46
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 32 times.
32 if (qgetenv("DISMAN_IN_PROCESS") != QByteArray("1")) {
47 QTimer::singleShot(0, this, &Fake::delayedInit);
48 }
49 32 }
50
51 32 void Fake::init(const QVariantMap& arguments)
52 {
53 32 mConfig.reset();
54
55 32 mConfigFile = arguments[QStringLiteral("TEST_DATA")].toString();
56
2/2
✓ Branch 8 taken 32 times.
✓ Branch 9 taken 32 times.
64 qCDebug(DISMAN_FAKE) << "Fake profile file:" << mConfigFile;
57 32 }
58
59 void Fake::delayedInit()
60 {
61 new FakebackendAdaptor(this);
62 QDBusConnection::sessionBus().registerObject(QStringLiteral("/fake"), this);
63 }
64
65 124 Fake::~Fake()
66 {
67 124 }
68
69 52 QString Fake::name() const
70 {
71 52 return QStringLiteral("Fake");
72 }
73
74 QString Fake::service_name() const
75 {
76 return QStringLiteral("org.kwinft.disman.fakebackend");
77 }
78
79 128 void Fake::update_config(ConfigPtr& config) const
80 {
81 128 Parser::fromJson(mConfigFile, config);
82 128 mConfig = config;
83 128 }
84
85 7 bool Fake::set_config_system(const ConfigPtr& config)
86 {
87 7 mConfig = config->clone();
88 7 emit config_changed(mConfig);
89 7 return true;
90 }
91
92 32 bool Fake::valid() const
93 {
94 32 return true;
95 }
96
97 QByteArray Fake::edid(int outputId) const
98 {
99 Q_UNUSED(outputId);
100 QFile file(mConfigFile);
101 file.open(QIODevice::ReadOnly);
102
103 const QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll());
104 const QJsonObject json = jsonDoc.object();
105
106 const QJsonArray outputs = json[QStringLiteral("outputs")].toArray();
107 Q_FOREACH (const QJsonValue& value, outputs) {
108 const QVariantMap output = value.toObject().toVariantMap();
109 if (output[QStringLiteral("id")].toInt() != outputId) {
110 continue;
111 }
112
113 return QByteArray::fromBase64(output[QStringLiteral("edid")].toByteArray());
114 }
115 return QByteArray();
116 }
117
118 void Fake::setEnabled(int outputId, bool enabled)
119 {
120 Disman::OutputPtr output = config()->output(outputId);
121 if (output->enabled() == enabled) {
122 return;
123 }
124
125 output->set_enabled(enabled);
126 Q_EMIT config_changed(mConfig);
127 }
128
129 void Fake::setPrimary(int outputId, bool primary)
130 {
131 auto output = config()->output(outputId);
132
133 if (primary) {
134 if (auto cur_prim = mConfig->primary_output()) {
135 if (output == cur_prim) {
136 return;
137 }
138 mConfig->set_primary_output(output);
139 }
140 } else {
141 if (auto cur_prim = mConfig->primary_output()) {
142 if (output != cur_prim) {
143 return;
144 }
145 mConfig->set_primary_output(nullptr);
146 }
147 }
148
149 Q_EMIT config_changed(mConfig);
150 }
151
152 void Fake::setCurrentModeId(int outputId, QString const& modeId)
153 {
154 std::string const& string_mode_id = modeId.toStdString();
155 auto output = config()->output(outputId);
156
157 if (auto mode = output->commanded_mode(); mode && mode->id() == string_mode_id) {
158 return;
159 }
160
161 output->set_mode(output->mode(string_mode_id));
162 Q_EMIT config_changed(mConfig);
163 }
164
165 void Fake::setRotation(int outputId, int rotation)
166 {
167 Disman::OutputPtr output = config()->output(outputId);
168 const Disman::Output::Rotation rot = static_cast<Disman::Output::Rotation>(rotation);
169 if (output->rotation() == rot) {
170 return;
171 }
172
173 output->set_rotation(rot);
174 Q_EMIT config_changed(mConfig);
175 }
176
177 void Fake::addOutput(int outputId, const QString& name)
178 {
179 Disman::OutputPtr output(new Disman::Output);
180 output->set_id(outputId);
181 output->set_name(name.toStdString());
182 output->set_description(name.toStdString());
183 output->set_hash(name.toStdString());
184 mConfig->add_output(output);
185 Q_EMIT config_changed(mConfig);
186 }
187
188 void Fake::removeOutput(int outputId)
189 {
190 mConfig->remove_output(outputId);
191 Q_EMIT config_changed(mConfig);
192 }
193