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 <QDebug>
0008 #include <QObject>
0009 #include <QSignalSpy>
0010 #include <QTest>
0011 
0012 #include "fakedevice.h"
0013 #include "fakemanager.h"
0014 #include "fakeserver.h"
0015 
0016 #include "device.h"
0017 #include "manager.h"
0018 
0019 #include <memory>
0020 
0021 Q_DECLARE_METATYPE(QSharedPointer<Bolt::Device>)
0022 
0023 class ManagerTest : public QObject
0024 {
0025     Q_OBJECT
0026 public:
0027     ManagerTest()
0028         : QObject()
0029     {
0030         FakeServer::enableFakeEnv();
0031         qRegisterMetaType<QSharedPointer<Bolt::Device>>();
0032     }
0033 
0034 private Q_SLOTS:
0035     void testDeviceAddedRemoved()
0036     {
0037         std::unique_ptr<FakeServer> server;
0038         try {
0039             server = std::make_unique<FakeServer>();
0040         } catch (const FakeServerException &e) {
0041             qWarning("Fake server exception: %s", e.what());
0042             QFAIL("Exception server caught");
0043         }
0044 
0045         auto fakeManager = server->manager();
0046 
0047         Bolt::Manager manager;
0048         QVERIFY(manager.isAvailable());
0049 
0050         QSignalSpy addSpy(&manager, &Bolt::Manager::deviceAdded);
0051         QVERIFY(addSpy.isValid());
0052 
0053         FakeDevice *fakeDevice = nullptr;
0054         try {
0055             fakeDevice = fakeManager->addDevice(std::make_unique<FakeDevice>(QStringLiteral("device1")));
0056         } catch (const FakeDeviceException &e) {
0057             qWarning("Fake device exception: %s", e.what());
0058             QFAIL("Caught device exception");
0059         }
0060         QTRY_COMPARE(addSpy.size(), 1);
0061         auto device = addSpy.first().first().value<QSharedPointer<Bolt::Device>>();
0062         QCOMPARE(device->uid(), fakeDevice->uid());
0063 
0064         QSignalSpy removeSpy(&manager, &Bolt::Manager::deviceRemoved);
0065         QVERIFY(removeSpy.isValid());
0066         fakeManager->removeDevice(fakeDevice->uid());
0067         QTRY_COMPARE(removeSpy.size(), 1);
0068         QCOMPARE(removeSpy.first().first().value<QSharedPointer<Bolt::Device>>(), device);
0069     }
0070 };
0071 
0072 QTEST_GUILESS_MAIN(ManagerTest)
0073 
0074 #include "managertest.moc"