File indexing completed on 2024-05-12 17:08:31
0001 /* 0002 * SPDX-FileCopyrightText: 2018-2019 Daniel Vrátil <dvratil@kde.org> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include <QObject> 0008 #include <QSignalSpy> 0009 #include <QTest> 0010 0011 #include "fakedevice.h" 0012 #include "fakemanager.h" 0013 #include "fakeserver.h" 0014 0015 #include "device.h" 0016 0017 #include "kded_bolt.h" 0018 0019 #include <memory> 0020 0021 class TestableKDEDBolt : public KDEDBolt 0022 { 0023 Q_OBJECT 0024 public: 0025 using KDEDBolt::KDEDBolt; 0026 0027 Q_SIGNALS: 0028 void deviceNotify(const QVector<QSharedPointer<Bolt::Device>> &device); 0029 0030 protected: 0031 void notify() override 0032 { 0033 Q_EMIT deviceNotify(sortDevices(mPendingDevices)); 0034 } 0035 }; 0036 0037 class KDEDTest : public QObject 0038 { 0039 Q_OBJECT 0040 public: 0041 KDEDTest() 0042 : QObject() 0043 { 0044 FakeServer::enableFakeEnv(); 0045 qRegisterMetaType<QSharedPointer<Bolt::Device>>(); 0046 } 0047 0048 private Q_SLOTS: 0049 void testShouldNotify() 0050 { 0051 std::unique_ptr<FakeServer> fakeServer; 0052 try { 0053 fakeServer = std::make_unique<FakeServer>(); 0054 } catch (const FakeServerException &e) { 0055 qWarning("Fake server exception: %s", e.what()); 0056 QFAIL("Caught server exception"); 0057 } 0058 0059 TestableKDEDBolt kded(nullptr, {}); 0060 QSignalSpy notifySpy(&kded, &TestableKDEDBolt::deviceNotify); 0061 QVERIFY(notifySpy.isValid()); 0062 0063 // Add unauthorized device 0064 auto fakeManager = fakeServer->manager(); 0065 try { 0066 auto fakeDevice = std::make_unique<FakeDevice>(QStringLiteral("Device1")); 0067 fakeDevice->setStatus(QStringLiteral("connected")); 0068 fakeDevice->setAuthFlags(QStringLiteral("none")); 0069 fakeManager->addDevice(std::move(fakeDevice)); 0070 0071 QTRY_COMPARE(notifySpy.size(), 1); 0072 const auto devices = notifySpy[0][0].value<QVector<QSharedPointer<Bolt::Device>>>(); 0073 QCOMPARE(devices.size(), 1); 0074 const auto device = devices.front(); 0075 QCOMPARE(device->uid(), QStringLiteral("Device1")); 0076 QCOMPARE(device->authFlags(), Bolt::Auth::None); 0077 QCOMPARE(device->status(), Bolt::Status::Connected); 0078 } catch (const FakeDeviceException &e) { 0079 qWarning("Fake device exception: %s", e.what()); 0080 QFAIL("Caught device exception"); 0081 } 0082 0083 // Add authorized device 0084 notifySpy.clear(); 0085 try { 0086 auto fakeDevice = std::make_unique<FakeDevice>(QStringLiteral("Device2")); 0087 fakeDevice->setStatus(QStringLiteral("authorized")); 0088 fakeDevice->setAuthFlags(QStringLiteral("nokey | boot")); 0089 fakeManager->addDevice(std::move(fakeDevice)); 0090 0091 QTest::qWait(200); 0092 QVERIFY(notifySpy.empty()); 0093 } catch (const FakeDeviceException &e) { 0094 qWarning("Fake device exception: %s", e.what()); 0095 QFAIL("Caught device exception"); 0096 } 0097 } 0098 }; 0099 0100 QTEST_MAIN(KDEDTest) 0101 0102 #include "kdedtest.moc"