File indexing completed on 2023-10-01 07:47:23
0001 /* 0002 * SPDX-FileCopyrightText: 2014-2015 David Rosca <nowrep@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 "inputtest.h" 0008 #include "autotests.h" 0009 #include "initmanagerjob.h" 0010 #include "pendingcall.h" 0011 0012 #include <QTest> 0013 0014 namespace BluezQt 0015 { 0016 extern void bluezqt_initFakeBluezTestRun(); 0017 } 0018 0019 using namespace BluezQt; 0020 0021 Q_DECLARE_METATYPE(Input::ReconnectMode) 0022 0023 InputTest::InputTest() 0024 : m_manager(nullptr) 0025 { 0026 Autotests::registerMetatypes(); 0027 0028 qRegisterMetaType<Input::ReconnectMode>("ReconnectMode"); 0029 } 0030 0031 void InputTest::initTestCase() 0032 { 0033 QDBusConnection connection = QDBusConnection::sessionBus(); 0034 QString service = QStringLiteral("org.kde.bluezqt.fakebluez"); 0035 0036 bluezqt_initFakeBluezTestRun(); 0037 0038 FakeBluez::start(); 0039 FakeBluez::runTest(QStringLiteral("bluez-standard")); 0040 0041 // Create adapter 0042 QString adapter = QStringLiteral("/org/bluez/hci0"); 0043 QVariantMap adapterProps; 0044 adapterProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(adapter)); 0045 adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E"); 0046 adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter"); 0047 FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); 0048 0049 // Create devices 0050 QVariantMap deviceProps; 0051 QVariantMap inputProps; 0052 0053 QString device1 = adapter + QLatin1String("/dev_40_79_6A_0C_39_75"); 0054 deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device1)); 0055 deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter)); 0056 deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75"); 0057 deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice"); 0058 deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB")); 0059 inputProps[QStringLiteral("ReconnectMode")] = QStringLiteral("none"); 0060 deviceProps[QStringLiteral("Input")] = inputProps; 0061 FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); 0062 0063 QString device2 = adapter + QLatin1String("/dev_50_79_6A_0C_39_75"); 0064 deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device2)); 0065 deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter)); 0066 deviceProps[QStringLiteral("Address")] = QStringLiteral("50:79:6A:0C:39:75"); 0067 deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice2"); 0068 deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB")); 0069 inputProps[QStringLiteral("ReconnectMode")] = QStringLiteral("host"); 0070 deviceProps[QStringLiteral("Input")] = inputProps; 0071 FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); 0072 0073 QString device3 = adapter + QLatin1String("/dev_60_79_6B_0C_39_55"); 0074 deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device3)); 0075 deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter)); 0076 deviceProps[QStringLiteral("Address")] = QStringLiteral("60:79:6B:0C:39:55"); 0077 deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice3"); 0078 deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB")); 0079 inputProps[QStringLiteral("ReconnectMode")] = QStringLiteral("device"); 0080 deviceProps[QStringLiteral("Input")] = inputProps; 0081 FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); 0082 0083 QString device4 = adapter + QLatin1String("/dev_70_79_6B_0C_39_55"); 0084 deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device4)); 0085 deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter)); 0086 deviceProps[QStringLiteral("Address")] = QStringLiteral("70:79:6B:0C:39:55"); 0087 deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice4"); 0088 deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB")); 0089 inputProps[QStringLiteral("ReconnectMode")] = QStringLiteral("any"); 0090 deviceProps[QStringLiteral("Input")] = inputProps; 0091 FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); 0092 0093 m_manager = new Manager(); 0094 InitManagerJob *initJob = m_manager->init(); 0095 initJob->exec(); 0096 QVERIFY(!initJob->error()); 0097 0098 for (DevicePtr device : m_manager->devices()) { 0099 QVERIFY(device->input()); 0100 0101 InputUnit u; 0102 u.device = device; 0103 u.dbusInput = new org::bluez::Input1(service, device->ubi(), connection, this); 0104 m_units.append(u); 0105 } 0106 0107 QCOMPARE(m_manager->adapters().count(), 1); 0108 QCOMPARE(m_manager->devices().count(), 4); 0109 } 0110 0111 void InputTest::cleanupTestCase() 0112 { 0113 for (const InputUnit &unit : m_units) { 0114 delete unit.dbusInput; 0115 } 0116 0117 delete m_manager; 0118 0119 FakeBluez::stop(); 0120 } 0121 0122 void InputTest::getPropertiesTest() 0123 { 0124 for (const InputUnit &unit : m_units) { 0125 QCOMPARE(reconnectModeString(unit.device->input()), unit.dbusInput->reconnectMode()); 0126 } 0127 } 0128 0129 QString InputTest::reconnectModeString(const InputPtr &input) const 0130 { 0131 switch (input->reconnectMode()) { 0132 case Input::NoReconnect: 0133 return QStringLiteral("none"); 0134 case Input::HostReconnect: 0135 return QStringLiteral("host"); 0136 case Input::DeviceReconnect: 0137 return QStringLiteral("device"); 0138 default: 0139 return QStringLiteral("any"); 0140 } 0141 } 0142 0143 QTEST_MAIN(InputTest) 0144 0145 #include "moc_inputtest.cpp"