1 | /* |
2 | * SPDX-FileCopyrightText: 2014-2015 David Rosca <nowrep@gmail.com> |
3 | * |
4 | * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
5 | */ |
6 | |
7 | #include "inputtest.h" |
8 | #include "autotests.h" |
9 | #include "initmanagerjob.h" |
10 | #include "pendingcall.h" |
11 | |
12 | #include <QTest> |
13 | |
14 | namespace BluezQt |
15 | { |
16 | extern void bluezqt_initFakeBluezTestRun(); |
17 | } |
18 | |
19 | using namespace BluezQt; |
20 | |
21 | Q_DECLARE_METATYPE(Input::ReconnectMode) |
22 | |
23 | InputTest::InputTest() |
24 | : m_manager(nullptr) |
25 | { |
26 | Autotests::registerMetatypes(); |
27 | |
28 | qRegisterMetaType<Input::ReconnectMode>("ReconnectMode" ); |
29 | } |
30 | |
31 | void InputTest::initTestCase() |
32 | { |
33 | QDBusConnection connection = QDBusConnection::sessionBus(); |
34 | QString service = QStringLiteral("org.kde.bluezqt.fakebluez" ); |
35 | |
36 | bluezqt_initFakeBluezTestRun(); |
37 | |
38 | FakeBluez::start(); |
39 | FakeBluez::runTest(QStringLiteral("bluez-standard" )); |
40 | |
41 | // Create adapter |
42 | QString adapter = QStringLiteral("/org/bluez/hci0" ); |
43 | QVariantMap adapterProps; |
44 | adapterProps[QStringLiteral("Path" )] = QVariant::fromValue(QDBusObjectPath(adapter)); |
45 | adapterProps[QStringLiteral("Address" )] = QStringLiteral("1C:E5:C3:BC:94:7E" ); |
46 | adapterProps[QStringLiteral("Name" )] = QStringLiteral("TestAdapter" ); |
47 | FakeBluez::runAction(QStringLiteral("devicemanager" ), QStringLiteral("create-adapter" ), adapterProps); |
48 | |
49 | // Create devices |
50 | QVariantMap deviceProps; |
51 | QVariantMap inputProps; |
52 | |
53 | QString device1 = adapter + QLatin1String("/dev_40_79_6A_0C_39_75" ); |
54 | deviceProps[QStringLiteral("Path" )] = QVariant::fromValue(QDBusObjectPath(device1)); |
55 | deviceProps[QStringLiteral("Adapter" )] = QVariant::fromValue(QDBusObjectPath(adapter)); |
56 | deviceProps[QStringLiteral("Address" )] = QStringLiteral("40:79:6A:0C:39:75" ); |
57 | deviceProps[QStringLiteral("Name" )] = QStringLiteral("TestDevice" ); |
58 | deviceProps[QStringLiteral("UUIDs" )] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB" )); |
59 | inputProps[QStringLiteral("ReconnectMode" )] = QStringLiteral("none" ); |
60 | deviceProps[QStringLiteral("Input" )] = inputProps; |
61 | FakeBluez::runAction(QStringLiteral("devicemanager" ), QStringLiteral("create-device" ), deviceProps); |
62 | |
63 | QString device2 = adapter + QLatin1String("/dev_50_79_6A_0C_39_75" ); |
64 | deviceProps[QStringLiteral("Path" )] = QVariant::fromValue(QDBusObjectPath(device2)); |
65 | deviceProps[QStringLiteral("Adapter" )] = QVariant::fromValue(QDBusObjectPath(adapter)); |
66 | deviceProps[QStringLiteral("Address" )] = QStringLiteral("50:79:6A:0C:39:75" ); |
67 | deviceProps[QStringLiteral("Name" )] = QStringLiteral("TestDevice2" ); |
68 | deviceProps[QStringLiteral("UUIDs" )] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB" )); |
69 | inputProps[QStringLiteral("ReconnectMode" )] = QStringLiteral("host" ); |
70 | deviceProps[QStringLiteral("Input" )] = inputProps; |
71 | FakeBluez::runAction(QStringLiteral("devicemanager" ), QStringLiteral("create-device" ), deviceProps); |
72 | |
73 | QString device3 = adapter + QLatin1String("/dev_60_79_6B_0C_39_55" ); |
74 | deviceProps[QStringLiteral("Path" )] = QVariant::fromValue(QDBusObjectPath(device3)); |
75 | deviceProps[QStringLiteral("Adapter" )] = QVariant::fromValue(QDBusObjectPath(adapter)); |
76 | deviceProps[QStringLiteral("Address" )] = QStringLiteral("60:79:6B:0C:39:55" ); |
77 | deviceProps[QStringLiteral("Name" )] = QStringLiteral("TestDevice3" ); |
78 | deviceProps[QStringLiteral("UUIDs" )] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB" )); |
79 | inputProps[QStringLiteral("ReconnectMode" )] = QStringLiteral("device" ); |
80 | deviceProps[QStringLiteral("Input" )] = inputProps; |
81 | FakeBluez::runAction(QStringLiteral("devicemanager" ), QStringLiteral("create-device" ), deviceProps); |
82 | |
83 | QString device4 = adapter + QLatin1String("/dev_70_79_6B_0C_39_55" ); |
84 | deviceProps[QStringLiteral("Path" )] = QVariant::fromValue(QDBusObjectPath(device4)); |
85 | deviceProps[QStringLiteral("Adapter" )] = QVariant::fromValue(QDBusObjectPath(adapter)); |
86 | deviceProps[QStringLiteral("Address" )] = QStringLiteral("70:79:6B:0C:39:55" ); |
87 | deviceProps[QStringLiteral("Name" )] = QStringLiteral("TestDevice4" ); |
88 | deviceProps[QStringLiteral("UUIDs" )] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB" )); |
89 | inputProps[QStringLiteral("ReconnectMode" )] = QStringLiteral("any" ); |
90 | deviceProps[QStringLiteral("Input" )] = inputProps; |
91 | FakeBluez::runAction(QStringLiteral("devicemanager" ), QStringLiteral("create-device" ), deviceProps); |
92 | |
93 | m_manager = new Manager(); |
94 | InitManagerJob *initJob = m_manager->init(); |
95 | initJob->exec(); |
96 | QVERIFY(!initJob->error()); |
97 | |
98 | for (DevicePtr device : m_manager->devices()) { |
99 | QVERIFY(device->input()); |
100 | |
101 | InputUnit u; |
102 | u.device = device; |
103 | u.dbusInput = new org::bluez::Input1(service, device->ubi(), connection, this); |
104 | m_units.append(u); |
105 | } |
106 | |
107 | QCOMPARE(m_manager->adapters().count(), 1); |
108 | QCOMPARE(m_manager->devices().count(), 4); |
109 | } |
110 | |
111 | void InputTest::cleanupTestCase() |
112 | { |
113 | for (const InputUnit &unit : m_units) { |
114 | delete unit.dbusInput; |
115 | } |
116 | |
117 | delete m_manager; |
118 | |
119 | FakeBluez::stop(); |
120 | } |
121 | |
122 | void InputTest::getPropertiesTest() |
123 | { |
124 | for (const InputUnit &unit : m_units) { |
125 | QCOMPARE(reconnectModeString(unit.device->input()), unit.dbusInput->reconnectMode()); |
126 | } |
127 | } |
128 | |
129 | QString InputTest::reconnectModeString(const InputPtr &input) const |
130 | { |
131 | switch (input->reconnectMode()) { |
132 | case Input::NoReconnect: |
133 | return QStringLiteral("none" ); |
134 | case Input::HostReconnect: |
135 | return QStringLiteral("host" ); |
136 | case Input::DeviceReconnect: |
137 | return QStringLiteral("device" ); |
138 | default: |
139 | return QStringLiteral("any" ); |
140 | } |
141 | } |
142 | |
143 | QTEST_MAIN(InputTest) |
144 | |
145 | #include "moc_inputtest.cpp" |
146 | |