File indexing completed on 2024-04-14 03:49:54

0001 /*
0002  * SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@broulik.de>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "batterytest.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 BatteryTest::BatteryTest()
0022     : m_manager(nullptr)
0023 {
0024     Autotests::registerMetatypes();
0025 }
0026 
0027 void BatteryTest::initTestCase()
0028 {
0029     QDBusConnection connection = QDBusConnection::sessionBus();
0030     QString service = QStringLiteral("org.kde.bluezqt.fakebluez");
0031 
0032     bluezqt_initFakeBluezTestRun();
0033 
0034     FakeBluez::start();
0035     FakeBluez::runTest(QStringLiteral("bluez-standard"));
0036 
0037     // Create adapter
0038     QString adapter = QStringLiteral("/org/bluez/hci0");
0039     QVariantMap adapterProps;
0040     adapterProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(adapter));
0041     adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E");
0042     adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter");
0043     FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps);
0044 
0045     // Create devices
0046     QVariantMap deviceProps;
0047     QVariantMap batteryProps;
0048 
0049     QString device1 = adapter + QLatin1String("/dev_40_79_6A_0C_39_75");
0050     deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device1));
0051     deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter));
0052     deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75");
0053     deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice");
0054     deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB"));
0055     batteryProps[QStringLiteral("Percentage")] = uchar(42);
0056     deviceProps[QStringLiteral("Battery")] = batteryProps;
0057     FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps);
0058 
0059     QString device2 = adapter + QLatin1String("/dev_50_79_6A_0C_39_75");
0060     deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device2));
0061     deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter));
0062     deviceProps[QStringLiteral("Address")] = QStringLiteral("50:79:6A:0C:39:75");
0063     deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice2");
0064     deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB"));
0065     batteryProps[QStringLiteral("Percentage")] = uchar(0);
0066     deviceProps[QStringLiteral("Battery")] = batteryProps;
0067     FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps);
0068 
0069     QString device3 = adapter + QLatin1String("/dev_60_79_6B_0C_39_55");
0070     deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device3));
0071     deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter));
0072     deviceProps[QStringLiteral("Address")] = QStringLiteral("60:79:6B:0C:39:55");
0073     deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice3");
0074     deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB"));
0075     batteryProps[QStringLiteral("Percentage")] = uchar(99);
0076     deviceProps[QStringLiteral("Battery")] = batteryProps;
0077     FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps);
0078 
0079     QString device4 = adapter + QLatin1String("/dev_70_79_6B_0C_39_55");
0080     deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device4));
0081     deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter));
0082     deviceProps[QStringLiteral("Address")] = QStringLiteral("70:79:6B:0C:39:55");
0083     deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice4");
0084     deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB"));
0085     batteryProps[QStringLiteral("Percentage")] = uchar(100);
0086     deviceProps[QStringLiteral("Battery")] = batteryProps;
0087     FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps);
0088 
0089     m_manager = new Manager();
0090     InitManagerJob *initJob = m_manager->init();
0091     initJob->exec();
0092     QVERIFY(!initJob->error());
0093 
0094     for (DevicePtr device : m_manager->devices()) {
0095         QVERIFY(device->battery());
0096 
0097         BatteryUnit u;
0098         u.device = device;
0099         u.dbusBattery = new org::bluez::Battery1(service, device->ubi(), connection, this);
0100         m_units.append(u);
0101     }
0102 
0103     QCOMPARE(m_manager->adapters().count(), 1);
0104     QCOMPARE(m_manager->devices().count(), 4);
0105 }
0106 
0107 void BatteryTest::cleanupTestCase()
0108 {
0109     for (const BatteryUnit &unit : m_units) {
0110         delete unit.dbusBattery;
0111     }
0112 
0113     delete m_manager;
0114 
0115     FakeBluez::stop();
0116 }
0117 
0118 void BatteryTest::getPropertiesTest()
0119 {
0120     for (const BatteryUnit &unit : m_units) {
0121         QCOMPARE(unit.device->battery()->percentage(), unit.dbusBattery->percentage());
0122     }
0123 }
0124 
0125 QTEST_MAIN(BatteryTest)
0126 
0127 #include "moc_batterytest.cpp"