Changeset View
Changeset View
Standalone View
Standalone View
autotests/bolt/fakeserver/fakeserver.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2018 - 2019 Daniel Vrátil <dvratil@kde.org> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
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 | | ||||
21 | #include "fakeserver.h" | ||||
22 | #include "fakemanager.h" | ||||
23 | | ||||
24 | #include <QJsonDocument> | ||||
25 | #include <QJsonObject> | ||||
26 | #include <QDBusConnection> | ||||
27 | #include <QDBusConnectionInterface> | ||||
28 | #include <QDBusError> | ||||
29 | #include <QDebug> | ||||
30 | #include <QTest> | ||||
31 | #include <QEventLoop> | ||||
32 | #include <QTimer> | ||||
33 | | ||||
34 | namespace { | ||||
35 | | ||||
36 | static const QString OrgKdeFakebolt = QStringLiteral("org.kde.fakebolt"); | ||||
37 | | ||||
38 | } | ||||
39 | | ||||
40 | FakeServer::FakeServer(const QString &filename) | ||||
41 | { | ||||
42 | QFile jsonFile(filename); | ||||
43 | if (!jsonFile.open(QIODevice::ReadOnly)) { | ||||
44 | qCritical("Failed to open file %s: %s", qUtf8Printable(filename), qUtf8Printable(jsonFile.errorString())); | ||||
45 | throw FakeServerException( | ||||
46 | QStringLiteral("Failed to open file %1: %2").arg(filename, jsonFile.errorString())); | ||||
47 | } | ||||
48 | | ||||
49 | const auto doc = QJsonDocument::fromJson(jsonFile.readAll()); | ||||
50 | | ||||
51 | if (!QDBusConnection::sessionBus().registerService(OrgKdeFakebolt)) { | ||||
52 | throw FakeServerException( | ||||
53 | QStringLiteral("Failed to register org.kde.fakebolt service: %1") | ||||
54 | .arg(QDBusConnection::sessionBus().lastError().message())); | ||||
55 | } | ||||
56 | | ||||
57 | try { | ||||
58 | mManager.reset(new FakeManager(doc.object())); | ||||
59 | } catch (const FakeManagerException &e) { | ||||
60 | throw FakeServerException(e.what()); | ||||
61 | } | ||||
62 | } | ||||
63 | | ||||
64 | FakeServer::FakeServer() | ||||
65 | { | ||||
66 | if (!QDBusConnection::sessionBus().registerService(OrgKdeFakebolt)) { | ||||
67 | throw FakeServerException( | ||||
68 | QStringLiteral("Failed to register org.kde.fakebolt service: %1") | ||||
69 | .arg(QDBusConnection::sessionBus().lastError().message())); | ||||
70 | } | ||||
71 | | ||||
72 | try { | ||||
73 | mManager.reset(new FakeManager()); | ||||
74 | } catch (FakeManagerException &e) { | ||||
75 | throw FakeServerException(e.what()); | ||||
76 | } | ||||
77 | } | ||||
78 | | ||||
79 | FakeServer::~FakeServer() | ||||
80 | { | ||||
81 | } | ||||
82 | | ||||
83 | void FakeServer::enableFakeEnv() | ||||
84 | { | ||||
85 | qputenv("KBOLT_FAKE", "1"); | ||||
86 | } | ||||
87 | | ||||
88 | FakeManager *FakeServer::manager() const | ||||
89 | { | ||||
90 | return mManager.get(); | ||||
91 | } |