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