Changeset View
Changeset View
Standalone View
Standalone View
autotests/bolt/kded/kdedtest.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 <QObject> | ||||
22 | #include <QTest> | ||||
23 | #include <QSignalSpy> | ||||
24 | | ||||
25 | #include "fakeserver.h" | ||||
26 | #include "fakemanager.h" | ||||
27 | #include "fakedevice.h" | ||||
28 | | ||||
29 | #include "device.h" | ||||
30 | | ||||
31 | #include "kded_bolt.h" | ||||
32 | | ||||
33 | #include <memory> | ||||
34 | | ||||
35 | class TestableKDEDBolt : public KDEDBolt | ||||
36 | { | ||||
37 | Q_OBJECT | ||||
38 | public: | ||||
39 | using KDEDBolt::KDEDBolt; | ||||
40 | | ||||
41 | Q_SIGNALS: | ||||
42 | void deviceNotify(const QVector<QSharedPointer<Bolt::Device>> &device); | ||||
43 | | ||||
44 | protected: | ||||
45 | void notify() override | ||||
46 | { | ||||
47 | Q_EMIT deviceNotify(sortDevices(mPendingDevices)); | ||||
48 | } | ||||
49 | }; | ||||
50 | | ||||
51 | | ||||
52 | class KDEDTest : public QObject | ||||
53 | { | ||||
54 | Q_OBJECT | ||||
55 | public: | ||||
56 | KDEDTest() | ||||
57 | : QObject() | ||||
58 | { | ||||
59 | FakeServer::enableFakeEnv(); | ||||
60 | qRegisterMetaType<QSharedPointer<Bolt::Device>>(); | ||||
61 | } | ||||
62 | | ||||
63 | private Q_SLOTS: | ||||
64 | void testShouldNotify() | ||||
65 | { | ||||
66 | QScopedPointer<FakeServer> fakeServer; | ||||
67 | try { | ||||
68 | fakeServer.reset(new FakeServer); | ||||
69 | } catch (const FakeServerException &e) { | ||||
70 | qWarning("Fake server exception: %s", e.what()); | ||||
71 | QFAIL("Caught server exception"); | ||||
72 | } | ||||
73 | | ||||
74 | TestableKDEDBolt kded(nullptr, {}); | ||||
75 | QSignalSpy notifySpy(&kded, &TestableKDEDBolt::deviceNotify); | ||||
76 | QVERIFY(notifySpy.isValid()); | ||||
77 | | ||||
78 | // Add unauthorized device | ||||
79 | auto fakeManager = fakeServer->manager(); | ||||
80 | try { | ||||
81 | auto fakeDevice = std::make_unique<FakeDevice>(QStringLiteral("Device1")); | ||||
82 | fakeDevice->setStatus(QStringLiteral("connected")); | ||||
83 | fakeDevice->setAuthFlags(QStringLiteral("none")); | ||||
84 | fakeManager->addDevice(std::move(fakeDevice)); | ||||
85 | | ||||
86 | QTRY_COMPARE(notifySpy.size(), 1); | ||||
87 | const auto devices = notifySpy[0][0].value<QVector<QSharedPointer<Bolt::Device>>>(); | ||||
88 | QCOMPARE(devices.size(), 1); | ||||
89 | const auto device = devices.front(); | ||||
90 | QCOMPARE(device->uid(), QStringLiteral("Device1")); | ||||
91 | QCOMPARE(device->authFlags(), Bolt::Auth::None); | ||||
92 | QCOMPARE(device->status(), Bolt::Status::Connected); | ||||
93 | } catch (const FakeDeviceException &e) { | ||||
94 | qWarning("Fake device exception: %s", e.what()); | ||||
95 | QFAIL("Caught device exception"); | ||||
96 | } | ||||
97 | | ||||
98 | // Add authorized device | ||||
99 | notifySpy.clear(); | ||||
100 | try { | ||||
101 | auto fakeDevice = std::make_unique<FakeDevice>(QStringLiteral("Device2")); | ||||
102 | fakeDevice->setStatus(QStringLiteral("authorized")); | ||||
103 | fakeDevice->setAuthFlags(QStringLiteral("nokey | boot")); | ||||
104 | fakeManager->addDevice(std::move(fakeDevice)); | ||||
105 | | ||||
106 | QTest::qWait(200); | ||||
107 | QVERIFY(notifySpy.empty()); | ||||
108 | } catch (const FakeDeviceException &e) { | ||||
109 | qWarning("Fake device exception: %s", e.what()); | ||||
110 | QFAIL("Caught device exception"); | ||||
111 | } | ||||
112 | } | ||||
113 | }; | ||||
114 | | ||||
115 | QTEST_MAIN(KDEDTest) | ||||
116 | | ||||
117 | #include "kdedtest.moc" |