File indexing completed on 2024-03-24 15:24:17

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Ivan Podkurkov <podkiva2@gmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "gattserviceremotetest.h"
0008 #include "autotests.h"
0009 #include "initmanagerjob.h"
0010 #include "pendingcall.h"
0011 
0012 #include <QSignalSpy>
0013 #include <QTest>
0014 
0015 namespace BluezQt
0016 {
0017 extern void bluezqt_initFakeBluezTestRun();
0018 }
0019 
0020 using namespace BluezQt;
0021 
0022 GattServiceRemoteTest::GattServiceRemoteTest()
0023     : m_manager(nullptr)
0024 {
0025     Autotests::registerMetatypes();
0026 }
0027 
0028 void GattServiceRemoteTest::initTestCase()
0029 {
0030     QDBusConnection connection = QDBusConnection::sessionBus();
0031     QString service = QStringLiteral("org.kde.bluezqt.fakebluez");
0032 
0033     bluezqt_initFakeBluezTestRun();
0034 
0035     FakeBluez::start();
0036     FakeBluez::runTest(QStringLiteral("bluez-standard"));
0037 
0038     // Create adapters
0039     QDBusObjectPath adapter1 = QDBusObjectPath(QStringLiteral("/org/bluez/hci0"));
0040     QVariantMap adapterProps;
0041     adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter1);
0042     adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E");
0043     adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter");
0044     FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps);
0045 
0046     QDBusObjectPath adapter2 = QDBusObjectPath(QStringLiteral("/org/bluez/hci1"));
0047     adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter2);
0048     adapterProps[QStringLiteral("Address")] = QStringLiteral("2E:3A:C3:BC:85:7C");
0049     adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter2");
0050     FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps);
0051 
0052     // Create devices
0053     QVariantMap deviceProps;
0054     deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci0/dev_40_79_6A_0C_39_75"));
0055     deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci0"));
0056     deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75");
0057     deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice");
0058     deviceProps[QStringLiteral("Alias")] = QStringLiteral("TestAlias");
0059     deviceProps[QStringLiteral("Icon")] = QStringLiteral("phone");
0060     deviceProps[QStringLiteral("Class")] = QVariant::fromValue(quint32(101));
0061     deviceProps[QStringLiteral("Appearance")] = QVariant::fromValue(quint16(25));
0062     deviceProps[QStringLiteral("UUIDs")] = QStringList();
0063     deviceProps[QStringLiteral("Paired")] = false;
0064     deviceProps[QStringLiteral("Connected")] = false;
0065     deviceProps[QStringLiteral("Trusted")] = false;
0066     deviceProps[QStringLiteral("Blocked")] = false;
0067     deviceProps[QStringLiteral("LegacyPairing")] = false;
0068     deviceProps[QStringLiteral("RSSI")] = QVariant::fromValue(qint16(20));
0069     deviceProps[QStringLiteral("Modalias")] = QStringLiteral("bluetooth:v001Dp1200d1236");
0070     FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps);
0071 
0072     deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci1/dev_50_79_6A_0C_39_75"));
0073     deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci1"));
0074     deviceProps[QStringLiteral("Address")] = QStringLiteral("50:79:6A:0C:39:75");
0075     deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice2");
0076     deviceProps[QStringLiteral("Alias")] = QStringLiteral("TestAlias2");
0077     deviceProps[QStringLiteral("Icon")] = QStringLiteral("joypad");
0078     deviceProps[QStringLiteral("Class")] = QVariant::fromValue(quint32(201));
0079     deviceProps[QStringLiteral("Appearance")] = QVariant::fromValue(quint16(32));
0080     deviceProps[QStringLiteral("UUIDs")] = QStringList();
0081     deviceProps[QStringLiteral("Paired")] = true;
0082     deviceProps[QStringLiteral("Connected")] = false;
0083     deviceProps[QStringLiteral("Trusted")] = true;
0084     deviceProps[QStringLiteral("Blocked")] = false;
0085     deviceProps[QStringLiteral("LegacyPairing")] = false;
0086     deviceProps[QStringLiteral("RSSI")] = QVariant::fromValue(qint16(-15));
0087     deviceProps[QStringLiteral("Modalias")] = QStringLiteral("bluetooth:v001Dp1100d1236");
0088     FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps);
0089 
0090     // Create services
0091     QVariantMap serviceProps;
0092     serviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci0/dev_40_79_6A_0C_39_75/service0"));
0093     serviceProps[QStringLiteral("UUID")] = QStringLiteral("04FA28C0-2D0C-11EC-8D3D-0242AC130003");
0094     serviceProps[QStringLiteral("Primary")] = true;
0095     serviceProps[QStringLiteral("Device")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci0/dev_40_79_6A_0C_39_75"));
0096     serviceProps[QStringLiteral("Includes")] = QVariant::fromValue(QList<QDBusObjectPath>());
0097     serviceProps[QStringLiteral("Handle")] = QVariant::fromValue(qint16(1));
0098     FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-gatt-service"), serviceProps);
0099 
0100     serviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci1/dev_50_79_6A_0C_39_75/service0"));
0101     serviceProps[QStringLiteral("UUID")] = QStringLiteral("0663A394-9A76-4361-9DE6-82577B82AC9A");
0102     serviceProps[QStringLiteral("Primary")] = true;
0103     serviceProps[QStringLiteral("Device")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci1/dev_50_79_6A_0C_39_75"));
0104     serviceProps[QStringLiteral("Includes")] = QVariant::fromValue(QList<QDBusObjectPath>());
0105     serviceProps[QStringLiteral("Handle")] = QVariant::fromValue(qint16(2));
0106     FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-gatt-service"), serviceProps);
0107 
0108     m_manager = new Manager();
0109     InitManagerJob *initJob = m_manager->init();
0110     initJob->exec();
0111     QVERIFY(!initJob->error());
0112 
0113     for (const AdapterPtr &adapter : m_manager->adapters()) {
0114         QVERIFY(!adapter->ubi().isEmpty());
0115 
0116         for (const DevicePtr &device : adapter->devices()) {
0117             QVERIFY(!device->ubi().isEmpty());
0118 
0119             for (const GattServiceRemotePtr &gattService : device->gattServices()) {
0120                 QVERIFY(!gattService->ubi().isEmpty());
0121 
0122                 GattServiceRemoteUnit u;
0123                 u.service = gattService;
0124                 u.dbusService = new org::bluez::GattService1(service, gattService->ubi(), connection, this);
0125                 u.dbusProperties = new org::freedesktop::DBus::Properties(service, gattService->ubi(), connection, this);
0126                 m_units.append(u);
0127             }
0128 
0129             QCOMPARE(device->gattServices().count(), 1);
0130         }
0131     }
0132 
0133     QCOMPARE(m_manager->adapters().count(), 2);
0134     QCOMPARE(m_manager->devices().count(), 2);
0135 }
0136 
0137 void GattServiceRemoteTest::cleanupTestCase()
0138 {
0139     for (const GattServiceRemoteUnit &unit : std::as_const(m_units)) {
0140         delete unit.dbusService;
0141         delete unit.dbusProperties;
0142     }
0143 
0144     delete m_manager;
0145 
0146     FakeBluez::stop();
0147 }
0148 
0149 void GattServiceRemoteTest::getPropertiesTest()
0150 {
0151     for (const GattServiceRemoteUnit &unit : std::as_const(m_units)) {
0152         QCOMPARE(unit.service->ubi(), unit.dbusService->path());
0153         QCOMPARE(unit.service->uuid(), unit.dbusService->uUID());
0154         QCOMPARE(unit.service->isPrimary(), unit.dbusService->primary());
0155         QCOMPARE(unit.service->device()->ubi(), unit.dbusService->device().path());
0156         QCOMPARE(unit.service->includes(), unit.dbusService->includes());
0157         QCOMPARE(unit.service->handle(), unit.dbusService->handle());
0158     }
0159 }
0160 
0161 void GattServiceRemoteTest::setHandleTest()
0162 {
0163     for (const GattServiceRemoteUnit &unit : std::as_const(m_units)) {
0164         QSignalSpy serviceSpy(unit.service.data(), SIGNAL(handleChanged(quint16)));
0165         QSignalSpy dbusSpy(unit.dbusProperties, SIGNAL(PropertiesChanged(QString, QVariantMap, QStringList)));
0166 
0167         quint16 value = unit.service->handle() + 3;
0168 
0169         unit.service->setHandle(value);
0170         QTRY_COMPARE(serviceSpy.count(), 1);
0171 
0172         QList<QVariant> arguments = serviceSpy.takeFirst();
0173         QCOMPARE(arguments.at(0).toUInt(), value);
0174         Autotests::verifyPropertiesChangedSignal(dbusSpy, QStringLiteral("Handle"), value);
0175 
0176         QCOMPARE(unit.service->handle(), value);
0177         QCOMPARE(unit.dbusService->handle(), value);
0178     }
0179 }
0180 
0181 void GattServiceRemoteTest::serviceRemovedTest()
0182 {
0183     for (const GattServiceRemoteUnit &unit : std::as_const(m_units)) {
0184         QSignalSpy deviceSpy(unit.service->device().data(), SIGNAL(gattServiceRemoved(GattServiceRemotePtr)));
0185 
0186         QVariantMap properties;
0187         properties[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(unit.service->ubi()));
0188         FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("remove-gatt-service"), properties);
0189 
0190         QTRY_COMPARE(deviceSpy.count(), 1);
0191 
0192         QCOMPARE(deviceSpy.at(0).at(0).value<GattServiceRemotePtr>(), unit.service);
0193     }
0194 }
0195 
0196 QTEST_MAIN(GattServiceRemoteTest)
0197 
0198 #include "moc_gattserviceremotetest.cpp"