1/*
2 * SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@broulik.de>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7#include "batterytest.h"
8#include "autotests.h"
9#include "initmanagerjob.h"
10#include "pendingcall.h"
11
12#include <QTest>
13
14namespace BluezQt
15{
16extern void bluezqt_initFakeBluezTestRun();
17}
18
19using namespace BluezQt;
20
21BatteryTest::BatteryTest()
22 : m_manager(nullptr)
23{
24 Autotests::registerMetatypes();
25}
26
27void BatteryTest::initTestCase()
28{
29 QDBusConnection connection = QDBusConnection::sessionBus();
30 QString service = QStringLiteral("org.kde.bluezqt.fakebluez");
31
32 bluezqt_initFakeBluezTestRun();
33
34 FakeBluez::start();
35 FakeBluez::runTest(QStringLiteral("bluez-standard"));
36
37 // Create adapter
38 QString adapter = QStringLiteral("/org/bluez/hci0");
39 QVariantMap adapterProps;
40 adapterProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(adapter));
41 adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E");
42 adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter");
43 FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps);
44
45 // Create devices
46 QVariantMap deviceProps;
47 QVariantMap batteryProps;
48
49 QString device1 = adapter + QLatin1String("/dev_40_79_6A_0C_39_75");
50 deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device1));
51 deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter));
52 deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75");
53 deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice");
54 deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB"));
55 batteryProps[QStringLiteral("Percentage")] = uchar(42);
56 deviceProps[QStringLiteral("Battery")] = batteryProps;
57 FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps);
58
59 QString device2 = adapter + QLatin1String("/dev_50_79_6A_0C_39_75");
60 deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device2));
61 deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter));
62 deviceProps[QStringLiteral("Address")] = QStringLiteral("50:79:6A:0C:39:75");
63 deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice2");
64 deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB"));
65 batteryProps[QStringLiteral("Percentage")] = uchar(0);
66 deviceProps[QStringLiteral("Battery")] = batteryProps;
67 FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps);
68
69 QString device3 = adapter + QLatin1String("/dev_60_79_6B_0C_39_55");
70 deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device3));
71 deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter));
72 deviceProps[QStringLiteral("Address")] = QStringLiteral("60:79:6B:0C:39:55");
73 deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice3");
74 deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB"));
75 batteryProps[QStringLiteral("Percentage")] = uchar(99);
76 deviceProps[QStringLiteral("Battery")] = batteryProps;
77 FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps);
78
79 QString device4 = adapter + QLatin1String("/dev_70_79_6B_0C_39_55");
80 deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device4));
81 deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter));
82 deviceProps[QStringLiteral("Address")] = QStringLiteral("70:79:6B:0C:39:55");
83 deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice4");
84 deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB"));
85 batteryProps[QStringLiteral("Percentage")] = uchar(100);
86 deviceProps[QStringLiteral("Battery")] = batteryProps;
87 FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps);
88
89 m_manager = new Manager();
90 InitManagerJob *initJob = m_manager->init();
91 initJob->exec();
92 QVERIFY(!initJob->error());
93
94 for (DevicePtr device : m_manager->devices()) {
95 QVERIFY(device->battery());
96
97 BatteryUnit u;
98 u.device = device;
99 u.dbusBattery = new org::bluez::Battery1(service, device->ubi(), connection, this);
100 m_units.append(u);
101 }
102
103 QCOMPARE(m_manager->adapters().count(), 1);
104 QCOMPARE(m_manager->devices().count(), 4);
105}
106
107void BatteryTest::cleanupTestCase()
108{
109 for (const BatteryUnit &unit : m_units) {
110 delete unit.dbusBattery;
111 }
112
113 delete m_manager;
114
115 FakeBluez::stop();
116}
117
118void BatteryTest::getPropertiesTest()
119{
120 for (const BatteryUnit &unit : m_units) {
121 QCOMPARE(unit.device->battery()->percentage(), unit.dbusBattery->percentage());
122 }
123}
124
125QTEST_MAIN(BatteryTest)
126
127#include "moc_batterytest.cpp"
128

source code of bluez-qt/autotests/batterytest.cpp