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