diff --git a/autotests/adaptertest.cpp b/autotests/adaptertest.cpp index 9fc485a..ff066ef 100644 --- a/autotests/adaptertest.cpp +++ b/autotests/adaptertest.cpp @@ -1,382 +1,382 @@ /* * Copyright (C) 2014-2015 David Rosca * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "adaptertest.h" #include "autotests.h" #include "device.h" #include "pendingcall.h" #include "initmanagerjob.h" -#include -#include +#include +#include namespace BluezQt { extern void bluezqt_initFakeBluezTestRun(); } using namespace BluezQt; AdapterTest::AdapterTest() : m_manager(nullptr) { Autotests::registerMetatypes(); } void AdapterTest::initTestCase() { QDBusConnection connection = QDBusConnection::sessionBus(); QString service = QStringLiteral("org.kde.bluezqt.fakebluez"); bluezqt_initFakeBluezTestRun(); FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-standard")); // Create adapters QDBusObjectPath adapter1path = QDBusObjectPath(QStringLiteral("/org/bluez/hci0")); QVariantMap adapterProps; adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter1path); adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E"); adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter"); adapterProps[QStringLiteral("Alias")] = QStringLiteral("TestAlias"); adapterProps[QStringLiteral("Class")] = QVariant::fromValue(quint32(101)); adapterProps[QStringLiteral("Powered")] = false; adapterProps[QStringLiteral("Discoverable")] = false; adapterProps[QStringLiteral("Pairable")] = false; adapterProps[QStringLiteral("PairableTimeout")] = QVariant::fromValue(quint32(0)); adapterProps[QStringLiteral("DiscoverableTimeout")] = QVariant::fromValue(quint32(0)); adapterProps[QStringLiteral("Discovering")] = false; adapterProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001200-0000-1000-8000-00805f9b34fb")); adapterProps[QStringLiteral("Modalias")] = QStringLiteral("usb:v2D6Bp1236d0215"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); QDBusObjectPath adapter2path = QDBusObjectPath(QStringLiteral("/org/bluez/hci1")); adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter2path); adapterProps[QStringLiteral("Address")] = QStringLiteral("2E:3A:C3:BC:85:7C"); adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter2"); adapterProps[QStringLiteral("Alias")] = QStringLiteral("TestAlias2"); adapterProps[QStringLiteral("Class")] = QVariant::fromValue(quint32(201)); adapterProps[QStringLiteral("Powered")] = true; adapterProps[QStringLiteral("Discoverable")] = true; adapterProps[QStringLiteral("Pairable")] = true; adapterProps[QStringLiteral("PairableTimeout")] = QVariant::fromValue(quint32(150)); adapterProps[QStringLiteral("DiscoverableTimeout")] = QVariant::fromValue(quint32(120)); adapterProps[QStringLiteral("Discovering")] = false; adapterProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("0000110c-0000-1000-8000-00805f9b34fb")); adapterProps[QStringLiteral("Modalias")] = QStringLiteral("usb:v1D3Bp1134d0214"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); // Create devices QVariantMap deviceProps; deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci0/dev_40_79_6A_0C_39_75")); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(adapter1path); deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci1/dev_50_79_6A_0C_39_75")); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(adapter2path); deviceProps[QStringLiteral("Address")] = QStringLiteral("50:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice2"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); m_manager = new Manager(); InitManagerJob *initJob = m_manager->init(); initJob->exec(); QVERIFY(!initJob->error()); Q_FOREACH (AdapterPtr adapter, m_manager->adapters()) { QVERIFY(!adapter->ubi().isEmpty()); AdapterUnit u; u.adapter = adapter; u.dbusAdapter = new org::bluez::Adapter1(service, adapter->ubi(), connection, this); u.dbusProperties = new org::freedesktop::DBus::Properties(service, adapter->ubi(), connection, this); m_units.append(u); } QCOMPARE(m_manager->adapters().count(), 2); } void AdapterTest::cleanupTestCase() { Q_FOREACH (const AdapterUnit &unit, m_units) { delete unit.dbusAdapter; delete unit.dbusProperties; } delete m_manager; FakeBluez::stop(); } static void compareUuids(const QStringList &actual, const QStringList &expected) { QCOMPARE(actual.size(), expected.size()); for (int i = 0; i < actual.size(); ++i) { QCOMPARE(actual.at(i).toUpper(), expected.at(i).toUpper()); } } void AdapterTest::getPropertiesTest() { Q_FOREACH (const AdapterUnit &unit, m_units) { QCOMPARE(unit.adapter->ubi(), unit.dbusAdapter->path()); QCOMPARE(unit.adapter->address(), unit.dbusAdapter->address()); QCOMPARE(unit.adapter->name(), unit.dbusAdapter->alias()); QCOMPARE(unit.adapter->systemName(), unit.dbusAdapter->name()); QCOMPARE(unit.adapter->adapterClass(), unit.dbusAdapter->adapterClass()); QCOMPARE(unit.adapter->isPowered(), unit.dbusAdapter->powered()); QCOMPARE(unit.adapter->isDiscoverable(), unit.dbusAdapter->discoverable()); QCOMPARE(unit.adapter->discoverableTimeout(), unit.dbusAdapter->discoverableTimeout()); QCOMPARE(unit.adapter->isPairable(), unit.dbusAdapter->pairable()); QCOMPARE(unit.adapter->pairableTimeout(), unit.dbusAdapter->pairableTimeout()); QCOMPARE(unit.adapter->isDiscovering(), unit.dbusAdapter->discovering()); QCOMPARE(unit.adapter->modalias(), unit.dbusAdapter->modalias()); compareUuids(unit.adapter->uuids(), unit.dbusAdapter->uUIDs()); } } void AdapterTest::setAliasTest() { Q_FOREACH (const AdapterUnit &unit, m_units) { QSignalSpy adapterSpy(unit.adapter.data(), SIGNAL(nameChanged(QString))); QSignalSpy dbusSpy(unit.dbusProperties, SIGNAL(PropertiesChanged(QString,QVariantMap,QStringList))); QString value = unit.adapter->name() + QLatin1String("_tst_alias"); unit.adapter->setName(value); QTRY_COMPARE(adapterSpy.count(), 1); QList adapterArguments = adapterSpy.takeFirst(); QCOMPARE(adapterArguments.at(0).toString(), value); Autotests::verifyPropertiesChangedSignal(dbusSpy, QStringLiteral("Alias"), value); QCOMPARE(unit.adapter->name(), value); QCOMPARE(unit.dbusAdapter->alias(), value); } } void AdapterTest::setPoweredTest() { Q_FOREACH (const AdapterUnit &unit, m_units) { QSignalSpy adapterSpy(unit.adapter.data(), SIGNAL(poweredChanged(bool))); QSignalSpy dbusSpy(unit.dbusProperties, SIGNAL(PropertiesChanged(QString,QVariantMap,QStringList))); bool value = !unit.adapter->isPowered(); unit.adapter->setPowered(value); QTRY_COMPARE(adapterSpy.count(), 1); QVERIFY(dbusSpy.count() >= 1); Autotests::verifyPropertiesChangedSignal(dbusSpy, QStringLiteral("Powered"), value); QList adapterArguments = adapterSpy.takeFirst(); QCOMPARE(adapterArguments.at(0).toBool(), value); QCOMPARE(unit.adapter->isPowered(), value); QCOMPARE(unit.dbusAdapter->powered(), value); } } void AdapterTest::setDiscoverableTest() { // Discoverable cannot be changed when Adapter is off Q_FOREACH (const AdapterUnit &unit, m_units) { unit.adapter->setPowered(true)->waitForFinished(); QSignalSpy adapterSpy(unit.adapter.data(), SIGNAL(discoverableChanged(bool))); QSignalSpy dbusSpy(unit.dbusProperties, SIGNAL(PropertiesChanged(QString,QVariantMap,QStringList))); bool value = !unit.adapter->isDiscoverable(); unit.adapter->setDiscoverable(value); QTRY_COMPARE(adapterSpy.count(), 1); QList adapterArguments = adapterSpy.takeFirst(); QCOMPARE(adapterArguments.at(0).toBool(), value); Autotests::verifyPropertiesChangedSignal(dbusSpy, QStringLiteral("Discoverable"), value); QCOMPARE(unit.adapter->isDiscoverable(), value); QCOMPARE(unit.dbusAdapter->discoverable(), value); } } void AdapterTest::setDiscoverableTimeoutTest() { Q_FOREACH (const AdapterUnit &unit, m_units) { QSignalSpy adapterSpy(unit.adapter.data(), SIGNAL(discoverableTimeoutChanged(quint32))); QSignalSpy dbusSpy(unit.dbusProperties, SIGNAL(PropertiesChanged(QString,QVariantMap,QStringList))); quint32 value = unit.adapter->discoverableTimeout() + 1; unit.adapter->setDiscoverableTimeout(value); QTRY_COMPARE(adapterSpy.count(), 1); QVERIFY(dbusSpy.count() >= 1); Autotests::verifyPropertiesChangedSignal(dbusSpy, QStringLiteral("DiscoverableTimeout"), value); QList adapterArguments = adapterSpy.takeFirst(); QCOMPARE(adapterArguments.at(0).toUInt(), value); QCOMPARE(unit.adapter->discoverableTimeout(), value); QCOMPARE(unit.dbusAdapter->discoverableTimeout(), value); } } void AdapterTest::setPairableTest() { Q_FOREACH (const AdapterUnit &unit, m_units) { QSignalSpy adapterSpy(unit.adapter.data(), SIGNAL(pairableChanged(bool))); QSignalSpy dbusSpy(unit.dbusProperties, SIGNAL(PropertiesChanged(QString,QVariantMap,QStringList))); bool value = !unit.adapter->isPairable(); unit.adapter->setPairable(value); QTRY_COMPARE(adapterSpy.count(), 1); QList adapterArguments = adapterSpy.takeFirst(); QCOMPARE(adapterArguments.at(0).toBool(), value); Autotests::verifyPropertiesChangedSignal(dbusSpy, QStringLiteral("Pairable"), value); QCOMPARE(unit.adapter->isPairable(), value); QCOMPARE(unit.dbusAdapter->pairable(), value); } } void AdapterTest::setPairableTimeoutTest() { Q_FOREACH (const AdapterUnit &unit, m_units) { QSignalSpy adapterSpy(unit.adapter.data(), SIGNAL(pairableTimeoutChanged(quint32))); QSignalSpy dbusSpy(unit.dbusProperties, SIGNAL(PropertiesChanged(QString,QVariantMap,QStringList))); quint32 value = unit.adapter->pairableTimeout() + 1; unit.adapter->setPairableTimeout(value); QTRY_COMPARE(adapterSpy.count(), 1); QVERIFY(dbusSpy.count() >= 1); Autotests::verifyPropertiesChangedSignal(dbusSpy, QStringLiteral("PairableTimeout"), value); QList adapterArguments = adapterSpy.takeFirst(); QCOMPARE(adapterArguments.at(0).toUInt(), value); QCOMPARE(unit.adapter->pairableTimeout(), value); QCOMPARE(unit.dbusAdapter->pairableTimeout(), value); } } void AdapterTest::discoveryTest() { // Discovery needs Adapter powered on Q_FOREACH (const AdapterUnit &unit, m_units) { // Make sure the Adapter is powered on and not discovering unit.adapter->setPowered(true)->waitForFinished(); if (unit.adapter->isDiscovering()) { unit.adapter->stopDiscovery()->waitForFinished(); } QSignalSpy adapterSpy(unit.adapter.data(), SIGNAL(discoveringChanged(bool))); QSignalSpy dbusSpy(unit.dbusProperties, SIGNAL(PropertiesChanged(QString,QVariantMap,QStringList))); // Start Discovery unit.adapter->startDiscovery(); QTRY_COMPARE(adapterSpy.count(), 1); QVERIFY(dbusSpy.count() >= 1); Autotests::verifyPropertiesChangedSignal(dbusSpy, QStringLiteral("Discovering"), true); QList adapterArguments = adapterSpy.takeFirst(); QCOMPARE(adapterArguments.at(0).toBool(), true); QCOMPARE(unit.adapter->isDiscovering(), true); QCOMPARE(unit.dbusAdapter->discovering(), true); adapterSpy.clear(); dbusSpy.clear(); // Stop Discovery unit.adapter->stopDiscovery(); QTRY_COMPARE(adapterSpy.count(), 1); QVERIFY(dbusSpy.count() >= 1); Autotests::verifyPropertiesChangedSignal(dbusSpy, QStringLiteral("Discovering"), false); adapterArguments = adapterSpy.takeFirst(); QCOMPARE(adapterArguments.at(0).toBool(), false); QCOMPARE(unit.adapter->isDiscovering(), false); QCOMPARE(unit.dbusAdapter->discovering(), false); } } void AdapterTest::removeDeviceTest() { Q_FOREACH (const AdapterUnit &unit, m_units) { while (!unit.adapter->devices().isEmpty()) { DevicePtr device = unit.adapter->devices().first(); QSignalSpy managerSpy(m_manager, SIGNAL(deviceRemoved(DevicePtr))); QSignalSpy adapterSpy(unit.adapter.data(), SIGNAL(deviceRemoved(DevicePtr))); QSignalSpy deviceSpy(device.data(), SIGNAL(deviceRemoved(DevicePtr))); unit.adapter->removeDevice(device); QTRY_COMPARE(managerSpy.count(), 1); QTRY_COMPARE(adapterSpy.count(), 1); QTRY_COMPARE(deviceSpy.count(), 1); QCOMPARE(managerSpy.at(0).at(0).value(), device); QCOMPARE(adapterSpy.at(0).at(0).value(), device); QCOMPARE(deviceSpy.at(0).at(0).value(), device); } } } void AdapterTest::adapterRemovedTest() { Q_FOREACH (const AdapterUnit &unit, m_units) { QSignalSpy managerSpy(m_manager, SIGNAL(adapterRemoved(AdapterPtr))); QSignalSpy adapterSpy(unit.adapter.data(), SIGNAL(adapterRemoved(AdapterPtr))); QVariantMap properties; properties[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(unit.adapter->ubi())); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("remove-adapter"), properties); QTRY_COMPARE(managerSpy.count(), 1); QTRY_COMPARE(adapterSpy.count(), 1); QCOMPARE(managerSpy.at(0).at(0).value(), unit.adapter); QCOMPARE(adapterSpy.at(0).at(0).value(), unit.adapter); } } QTEST_MAIN(AdapterTest) diff --git a/autotests/agentmanagertest.cpp b/autotests/agentmanagertest.cpp index fad6f53..95d2604 100644 --- a/autotests/agentmanagertest.cpp +++ b/autotests/agentmanagertest.cpp @@ -1,272 +1,272 @@ /* * Copyright (C) 2014-2015 David Rosca * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "agentmanagertest.h" #include "autotests.h" #include "manager.h" #include "initmanagerjob.h" #include "pendingcall.h" #include "services.h" -#include -#include +#include +#include #include namespace BluezQt { extern void bluezqt_initFakeBluezTestRun(); } using namespace BluezQt; // TestAgent TestAgent::TestAgent(QObject *parent) : Agent(parent) , m_pinRequested(false) , m_passkeyRequested(false) , m_authorizationRequested(false) , m_cancelCalled(false) , m_releaseCalled(false) { } QDBusObjectPath TestAgent::objectPath() const { return QDBusObjectPath(QStringLiteral("/testagent")); } void TestAgent::requestPinCode(DevicePtr device, const BluezQt::Request &request) { m_device = device; m_pinRequested = true; request.accept(QString()); } void TestAgent::displayPinCode(DevicePtr device, const QString &pinCode) { m_device = device; m_displayedPinCode = pinCode; } void TestAgent::requestPasskey(DevicePtr device, const BluezQt::Request &request) { m_device = device; m_passkeyRequested = true; request.accept(0); } void TestAgent::displayPasskey(DevicePtr device, const QString &passkey, const QString &entered) { m_device = device; m_displayedPasskey = passkey; m_enteredPasskey = entered; } void TestAgent::requestConfirmation(DevicePtr device, const QString &passkey, const BluezQt::Request<> &request) { m_device = device; m_requestedPasskey = passkey; request.accept(); } void TestAgent::requestAuthorization(DevicePtr device, const BluezQt::Request<> &request) { m_device = device; m_authorizationRequested = true; request.accept(); } void TestAgent::authorizeService(DevicePtr device, const QString &uuid, const BluezQt::Request<> &request) { m_device = device; m_authorizedUuid = uuid; request.accept(); } void TestAgent::cancel() { m_cancelCalled = true; } void TestAgent::release() { m_releaseCalled = true; } // AgentManagerTest void AgentManagerTest::initTestCase() { bluezqt_initFakeBluezTestRun(); FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-standard")); // Create adapter QVariantMap adapterProps; adapterProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(QStringLiteral("/org/bluez/hci0"))); adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E"); adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); // Create device m_device = QDBusObjectPath(QStringLiteral("/org/bluez/hci0/dev_40_79_6A_0C_39_75")); QVariantMap deviceProps; deviceProps[QStringLiteral("Path")] = QVariant::fromValue(m_device); deviceProps[QStringLiteral("Adapter")] = adapterProps.value(QStringLiteral("Path")); deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); Manager *manager = new Manager(this); InitManagerJob *job = manager->init(); job->exec(); QVERIFY(!job->error()); QCOMPARE(manager->adapters().count(), 1); QCOMPARE(manager->devices().count(), 1); m_agent = new TestAgent(this); manager->registerAgent(m_agent)->waitForFinished(); } void AgentManagerTest::cleanupTestCase() { FakeBluez::stop(); } void AgentManagerTest::requestPinCodeTest() { QCOMPARE(m_agent->m_pinRequested, false); QVariantMap props; props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device)); FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("request-pincode"), props); QTRY_COMPARE(m_agent->m_pinRequested, true); QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice")); } void AgentManagerTest::displayPinCodeTest() { QCOMPARE(m_agent->m_displayedPinCode, QString()); QVariantMap props; props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device)); props.insert(QStringLiteral("PinCode"), QStringLiteral("123456")); FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("display-pincode"), props); QTRY_COMPARE(m_agent->m_displayedPinCode, QStringLiteral("123456")); QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice")); } void AgentManagerTest::requestPasskeyTest() { QCOMPARE(m_agent->m_passkeyRequested, false); QVariantMap props; props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device)); FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("request-passkey"), props); QTRY_COMPARE(m_agent->m_passkeyRequested, true); QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice")); } void AgentManagerTest::displayPasskeyTest() { QCOMPARE(m_agent->m_displayedPasskey, QString()); QCOMPARE(m_agent->m_enteredPasskey, QString()); QVariantMap props; props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device)); props.insert(QStringLiteral("Passkey"), QVariant::fromValue(quint32(654321))); props.insert(QStringLiteral("EnteredPasskey"), QVariant::fromValue(quint16(43))); FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("display-passkey"), props); QTRY_COMPARE(m_agent->m_displayedPasskey, QStringLiteral("654321")); QTRY_COMPARE(m_agent->m_enteredPasskey, QStringLiteral("43")); QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice")); } void AgentManagerTest::requestConfirmationTest() { QCOMPARE(m_agent->m_requestedPasskey, QString()); QVariantMap props; props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device)); props.insert(QStringLiteral("Passkey"), QVariant::fromValue(quint32(12))); FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("request-confirmation"), props); QTRY_COMPARE(m_agent->m_requestedPasskey, QStringLiteral("000012")); QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice")); } void AgentManagerTest::requestAuthorizationTest() { QCOMPARE(m_agent->m_authorizationRequested, false); QVariantMap props; props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device)); FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("request-authorization"), props); QTRY_COMPARE(m_agent->m_authorizationRequested, true); QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice")); } void AgentManagerTest::authorizeServiceTest() { QCOMPARE(m_agent->m_authorizedUuid, QString()); QVariantMap props; props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device)); props.insert(QStringLiteral("UUID"), Services::ObexFileTransfer); FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("authorize-service"), props); QTRY_COMPARE(m_agent->m_authorizedUuid, Services::ObexFileTransfer); QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice")); } void AgentManagerTest::cancelTest() { QCOMPARE(m_agent->m_cancelCalled, false); FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("cancel")); QTRY_COMPARE(m_agent->m_cancelCalled, true); QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice")); } void AgentManagerTest::releaseTest() { QCOMPARE(m_agent->m_releaseCalled, false); FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("release")); QTRY_COMPARE(m_agent->m_releaseCalled, true); QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice")); } QTEST_MAIN(AgentManagerTest) diff --git a/autotests/autotests.h b/autotests/autotests.h index 9b1cc10..87bd83d 100644 --- a/autotests/autotests.h +++ b/autotests/autotests.h @@ -1,51 +1,51 @@ /* * Copyright (C) 2014-2015 David Rosca * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #ifndef AUTOTESTS_H #define AUTOTESTS_H -#include +#include #include #include #include #include class FakeBluez { public: static void start(); static void stop(); static bool isRunning(); static void runTest(const QString &testName); static void runAction(const QString &object, const QString &actionName, const QVariantMap &properties = QVariantMap()); static QProcess *s_process; }; namespace Autotests { void registerMetatypes(); void verifyPropertiesChangedSignal(const QSignalSpy &spy, const QString &propertyName, const QVariant &propertyValue); } #endif // AUTOTESTS_H diff --git a/autotests/devicetest.cpp b/autotests/devicetest.cpp index 05dce53..b1767fc 100644 --- a/autotests/devicetest.cpp +++ b/autotests/devicetest.cpp @@ -1,283 +1,283 @@ /* * Copyright (C) 2014-2015 David Rosca * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "devicetest.h" #include "autotests.h" #include "pendingcall.h" #include "initmanagerjob.h" -#include -#include +#include +#include namespace BluezQt { extern void bluezqt_initFakeBluezTestRun(); } using namespace BluezQt; DeviceTest::DeviceTest() : m_manager(nullptr) { Autotests::registerMetatypes(); } void DeviceTest::initTestCase() { QDBusConnection connection = QDBusConnection::sessionBus(); QString service = QStringLiteral("org.kde.bluezqt.fakebluez"); bluezqt_initFakeBluezTestRun(); FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-standard")); // Create adapters QDBusObjectPath adapter1 = QDBusObjectPath(QStringLiteral("/org/bluez/hci0")); QVariantMap adapterProps; adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter1); adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E"); adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); QDBusObjectPath adapter2 = QDBusObjectPath(QStringLiteral("/org/bluez/hci1")); adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter2); adapterProps[QStringLiteral("Address")] = QStringLiteral("2E:3A:C3:BC:85:7C"); adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter2"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); // Create devices QVariantMap deviceProps; deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci0/dev_40_79_6A_0C_39_75")); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci0")); deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice"); deviceProps[QStringLiteral("Alias")] = QStringLiteral("TestAlias"); deviceProps[QStringLiteral("Icon")] = QStringLiteral("phone"); deviceProps[QStringLiteral("Class")] = QVariant::fromValue(quint32(101)); deviceProps[QStringLiteral("Appearance")] = QVariant::fromValue(quint16(25)); deviceProps[QStringLiteral("UUIDs")] = QStringList(); deviceProps[QStringLiteral("Paired")] = false; deviceProps[QStringLiteral("Connected")] = false; deviceProps[QStringLiteral("Trusted")] = false; deviceProps[QStringLiteral("Blocked")] = false; deviceProps[QStringLiteral("LegacyPairing")] = false; deviceProps[QStringLiteral("RSSI")] = QVariant::fromValue(qint16(20)); deviceProps[QStringLiteral("Modalias")] = QStringLiteral("bluetooth:v001Dp1200d1236"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci1/dev_50_79_6A_0C_39_75")); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci1")); deviceProps[QStringLiteral("Address")] = QStringLiteral("50:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice2"); deviceProps[QStringLiteral("Alias")] = QStringLiteral("TestAlias2"); deviceProps[QStringLiteral("Icon")] = QStringLiteral("joypad"); deviceProps[QStringLiteral("Class")] = QVariant::fromValue(quint32(201)); deviceProps[QStringLiteral("Appearance")] = QVariant::fromValue(quint16(32)); deviceProps[QStringLiteral("UUIDs")] = QStringList(); deviceProps[QStringLiteral("Paired")] = true; deviceProps[QStringLiteral("Connected")] = false; deviceProps[QStringLiteral("Trusted")] = true; deviceProps[QStringLiteral("Blocked")] = false; deviceProps[QStringLiteral("LegacyPairing")] = false; deviceProps[QStringLiteral("RSSI")] = QVariant::fromValue(qint16(-15)); deviceProps[QStringLiteral("Modalias")] = QStringLiteral("bluetooth:v001Dp1100d1236"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); m_manager = new Manager(); InitManagerJob *initJob = m_manager->init(); initJob->exec(); QVERIFY(!initJob->error()); Q_FOREACH (const AdapterPtr &adapter, m_manager->adapters()) { QVERIFY(!adapter->ubi().isEmpty()); Q_FOREACH (const DevicePtr &device, adapter->devices()) { QVERIFY(!device->ubi().isEmpty()); DeviceUnit u; u.device = device; u.dbusDevice = new org::bluez::Device1(service, device->ubi(), connection, this); u.dbusProperties = new org::freedesktop::DBus::Properties(service, device->ubi(), connection, this); m_units.append(u); } } QCOMPARE(m_manager->adapters().count(), 2); QCOMPARE(m_manager->devices().count(), 2); } void DeviceTest::cleanupTestCase() { Q_FOREACH (const DeviceUnit &unit, m_units) { delete unit.dbusDevice; delete unit.dbusProperties; } delete m_manager; FakeBluez::stop(); } static void compareUuids(const QStringList &actual, const QStringList &expected) { QCOMPARE(actual.size(), expected.size()); for (int i = 0; i < actual.size(); ++i) { QCOMPARE(actual.at(i).toUpper(), expected.at(i).toUpper()); } } void DeviceTest::getPropertiesTest() { Q_FOREACH (const DeviceUnit &unit, m_units) { QCOMPARE(unit.device->ubi(), unit.dbusDevice->path()); QCOMPARE(unit.device->address(), unit.dbusDevice->address()); QCOMPARE(unit.device->name(), unit.dbusDevice->alias()); QCOMPARE(unit.device->remoteName(), unit.dbusDevice->name()); QCOMPARE(unit.device->deviceClass(), unit.dbusDevice->deviceClass()); QCOMPARE(unit.device->appearance(), unit.dbusDevice->appearance()); QCOMPARE(unit.device->icon(), deviceIcon(unit.dbusDevice)); QCOMPARE(unit.device->isPaired(), unit.dbusDevice->paired()); QCOMPARE(unit.device->isTrusted(), unit.dbusDevice->trusted()); QCOMPARE(unit.device->isBlocked(), unit.dbusDevice->blocked()); QCOMPARE(unit.device->hasLegacyPairing(), unit.dbusDevice->legacyPairing()); QCOMPARE(unit.device->rssi(), deviceRssi(unit.dbusDevice)); QCOMPARE(unit.device->isConnected(), unit.dbusDevice->connected()); QCOMPARE(unit.device->modalias(), unit.dbusDevice->modalias()); QCOMPARE(unit.device->adapter()->ubi(), unit.dbusDevice->adapter().path()); compareUuids(unit.device->uuids(), unit.dbusDevice->uUIDs()); } } void DeviceTest::setAliasTest() { Q_FOREACH (const DeviceUnit &unit, m_units) { QSignalSpy deviceSpy(unit.device.data(), SIGNAL(nameChanged(QString))); QSignalSpy dbusSpy(unit.dbusProperties, SIGNAL(PropertiesChanged(QString,QVariantMap,QStringList))); QString value = unit.device->name() + QLatin1String("_tst_alias"); unit.device->setName(value); QTRY_COMPARE(deviceSpy.count(), 1); QList arguments = deviceSpy.takeFirst(); QCOMPARE(arguments.at(0).toString(), value); Autotests::verifyPropertiesChangedSignal(dbusSpy, QStringLiteral("Alias"), value); QCOMPARE(unit.device->name(), value); QCOMPARE(unit.dbusDevice->alias(), value); } } void DeviceTest::setTrustedTest() { Q_FOREACH (const DeviceUnit &unit, m_units) { QSignalSpy deviceSpy(unit.device.data(), SIGNAL(trustedChanged(bool))); QSignalSpy dbusSpy(unit.dbusProperties, SIGNAL(PropertiesChanged(QString,QVariantMap,QStringList))); bool value = !unit.device->isTrusted(); unit.device->setTrusted(value); QTRY_COMPARE(deviceSpy.count(), 1); QList arguments = deviceSpy.takeFirst(); QCOMPARE(arguments.at(0).toBool(), value); Autotests::verifyPropertiesChangedSignal(dbusSpy, QStringLiteral("Trusted"), value); QCOMPARE(unit.device->isTrusted(), value); QCOMPARE(unit.dbusDevice->trusted(), value); } } void DeviceTest::setBlockedTest() { Q_FOREACH (const DeviceUnit &unit, m_units) { QSignalSpy deviceSpy(unit.device.data(), SIGNAL(blockedChanged(bool))); QSignalSpy dbusSpy(unit.dbusProperties, SIGNAL(PropertiesChanged(QString,QVariantMap,QStringList))); bool value = !unit.device->isBlocked(); unit.device->setBlocked(value); QTRY_COMPARE(deviceSpy.count(), 1); QList arguments = deviceSpy.takeFirst(); QCOMPARE(arguments.at(0).toBool(), value); Autotests::verifyPropertiesChangedSignal(dbusSpy, QStringLiteral("Blocked"), value); QCOMPARE(unit.device->isBlocked(), value); QCOMPARE(unit.dbusDevice->blocked(), value); } } void DeviceTest::deviceRemovedTest() { Q_FOREACH (const DeviceUnit &unit, m_units) { QSignalSpy managerSpy(m_manager, SIGNAL(deviceRemoved(DevicePtr))); QSignalSpy adapterSpy(unit.device->adapter().data(), SIGNAL(deviceRemoved(DevicePtr))); QSignalSpy deviceSpy(unit.device.data(), SIGNAL(deviceRemoved(DevicePtr))); QVariantMap properties; properties[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(unit.device->ubi())); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("remove-device"), properties); QTRY_COMPARE(managerSpy.count(), 1); QTRY_COMPARE(adapterSpy.count(), 1); QTRY_COMPARE(deviceSpy.count(), 1); QCOMPARE(managerSpy.at(0).at(0).value(), unit.device); QCOMPARE(adapterSpy.at(0).at(0).value(), unit.device); QCOMPARE(deviceSpy.at(0).at(0).value(), unit.device); } } QString DeviceTest::deviceIcon(org::bluez::Device1 *device) const { quint32 classNum = device->deviceClass(); switch ((classNum & 0x1f00) >> 8) { case 0x04: switch ((classNum & 0xfc) >> 2) { case 0x01: case 0x02: return QStringLiteral("audio-headset"); case 0x06: return QStringLiteral("audio-headphones"); } } return device->icon(); } qint16 DeviceTest::deviceRssi(org::bluez::Device1 *device) const { qint16 rssi = device->rSSI(); if (!rssi) { rssi = -32768; } return rssi; } QTEST_MAIN(DeviceTest) diff --git a/autotests/inputtest.cpp b/autotests/inputtest.cpp index 0723d3b..ba0918b 100644 --- a/autotests/inputtest.cpp +++ b/autotests/inputtest.cpp @@ -1,161 +1,161 @@ /* * Copyright (C) 2014-2015 David Rosca * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "inputtest.h" #include "autotests.h" #include "pendingcall.h" #include "initmanagerjob.h" -#include -#include +#include +#include namespace BluezQt { extern void bluezqt_initFakeBluezTestRun(); } using namespace BluezQt; Q_DECLARE_METATYPE(Input::ReconnectMode) InputTest::InputTest() : m_manager(nullptr) { Autotests::registerMetatypes(); qRegisterMetaType("ReconnectMode"); } void InputTest::initTestCase() { QDBusConnection connection = QDBusConnection::sessionBus(); QString service = QStringLiteral("org.kde.bluezqt.fakebluez"); bluezqt_initFakeBluezTestRun(); FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-standard")); // Create adapter QString adapter = QStringLiteral("/org/bluez/hci0"); QVariantMap adapterProps; adapterProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(adapter)); adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E"); adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); // Create devices QVariantMap deviceProps; QVariantMap inputProps; QString device1 = adapter + QLatin1String("/dev_40_79_6A_0C_39_75"); deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device1)); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter)); deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice"); deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB")); inputProps[QStringLiteral("ReconnectMode")] = QStringLiteral("none"); deviceProps[QStringLiteral("Input")] = inputProps; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); QString device2 = adapter + QLatin1String("/dev_50_79_6A_0C_39_75"); deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device2)); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter)); deviceProps[QStringLiteral("Address")] = QStringLiteral("50:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice2"); deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB")); inputProps[QStringLiteral("ReconnectMode")] = QStringLiteral("host"); deviceProps[QStringLiteral("Input")] = inputProps; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); QString device3 = adapter + QLatin1String("/dev_60_79_6B_0C_39_55"); deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device3)); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter)); deviceProps[QStringLiteral("Address")] = QStringLiteral("60:79:6B:0C:39:55"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice3"); deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB")); inputProps[QStringLiteral("ReconnectMode")] = QStringLiteral("device"); deviceProps[QStringLiteral("Input")] = inputProps; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); QString device4 = adapter + QLatin1String("/dev_70_79_6B_0C_39_55"); deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device4)); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter)); deviceProps[QStringLiteral("Address")] = QStringLiteral("70:79:6B:0C:39:55"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice4"); deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("00001124-0000-1000-8000-00805F9B34FB")); inputProps[QStringLiteral("ReconnectMode")] = QStringLiteral("any"); deviceProps[QStringLiteral("Input")] = inputProps; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); m_manager = new Manager(); InitManagerJob *initJob = m_manager->init(); initJob->exec(); QVERIFY(!initJob->error()); Q_FOREACH (DevicePtr device, m_manager->devices()) { QVERIFY(device->input()); InputUnit u; u.device = device; u.dbusInput = new org::bluez::Input1(service, device->ubi(), connection, this); m_units.append(u); } QCOMPARE(m_manager->adapters().count(), 1); QCOMPARE(m_manager->devices().count(), 4); } void InputTest::cleanupTestCase() { Q_FOREACH (const InputUnit &unit, m_units) { delete unit.dbusInput; } delete m_manager; FakeBluez::stop(); } void InputTest::getPropertiesTest() { Q_FOREACH (const InputUnit &unit, m_units) { QCOMPARE(reconnectModeString(unit.device->input()), unit.dbusInput->reconnectMode()); } } QString InputTest::reconnectModeString(const InputPtr &input) const { switch (input->reconnectMode()) { case Input::NoReconnect: return QStringLiteral("none"); case Input::HostReconnect: return QStringLiteral("host"); case Input::DeviceReconnect: return QStringLiteral("device"); default: return QStringLiteral("any"); } } QTEST_MAIN(InputTest) diff --git a/autotests/jobstest.cpp b/autotests/jobstest.cpp index 47739e9..56ff317 100644 --- a/autotests/jobstest.cpp +++ b/autotests/jobstest.cpp @@ -1,109 +1,109 @@ /* * Copyright (C) 2014-2015 David Rosca * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "jobstest.h" #include "manager.h" #include "initmanagerjob.h" #include "obexmanager.h" #include "initobexmanagerjob.h" #include "device.h" -#include -#include +#include +#include using namespace BluezQt; void JobsTest::deleteManagerTest() { Manager *manager = new Manager(); InitManagerJob *job = manager->init(); QSignalSpy jobSpy(job, SIGNAL(result(InitManagerJob*))); job->start(); delete manager; // Deleting manager while init job is in progress should emit error QTRY_COMPARE(jobSpy.count(), 1); } void JobsTest::deleteInitManagerJobTest() { Manager *manager = new Manager(); InitManagerJob *job = manager->init(); QSignalSpy jobSpy(job, SIGNAL(result(InitManagerJob*))); job->start(); delete job; // Deleting the job should emit error QTRY_COMPARE(jobSpy.count(), 1); delete manager; manager = new Manager(); job = manager->init(); QSignalSpy jobSpy2(job, SIGNAL(result(InitManagerJob*))); job->start(); job->kill(); // Killing the job should NOT emit error QTRY_COMPARE(jobSpy2.count(), 0); } void JobsTest::deleteObexManagerTest() { ObexManager *manager = new ObexManager(); InitObexManagerJob *job = manager->init(); QSignalSpy jobSpy(job, SIGNAL(result(InitObexManagerJob*))); job->start(); delete manager; // Deleting manager while init job is in progress should emit error QTRY_COMPARE(jobSpy.count(), 1); } void JobsTest::deleteInitObexManagerJobTest() { ObexManager *manager = new ObexManager(); InitObexManagerJob *job = manager->init(); QSignalSpy jobSpy(job, SIGNAL(result(InitObexManagerJob*))); job->start(); delete job; // Deleting the job should emit error QTRY_COMPARE(jobSpy.count(), 1); delete manager; manager = new ObexManager(); job = manager->init(); QSignalSpy jobSpy2(job, SIGNAL(result(InitObexManagerJob*))); job->start(); job->kill(); // Killing the job should NOT emit error QTRY_COMPARE(jobSpy2.count(), 0); } QTEST_MAIN(JobsTest) diff --git a/autotests/managertest.cpp b/autotests/managertest.cpp index d0602b1..760fcc5 100644 --- a/autotests/managertest.cpp +++ b/autotests/managertest.cpp @@ -1,461 +1,461 @@ /* * Copyright (C) 2014-2015 David Rosca * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "managertest.h" #include "autotests.h" #include "manager.h" #include "initmanagerjob.h" #include "adapter.h" #include "device.h" -#include -#include +#include +#include #include namespace BluezQt { extern void bluezqt_initFakeBluezTestRun(); } using namespace BluezQt; void ManagerTest::initTestCase() { bluezqt_initFakeBluezTestRun(); FakeBluez::start(); // to check that it works Autotests::registerMetatypes(); } void ManagerTest::cleanup() { FakeBluez::stop(); } void ManagerTest::bluezNotRunningTest() { // org.bluez is not running at all // expected: init successful Manager *manager = new Manager; InitManagerJob *job = manager->init(); job->exec(); QVERIFY(!job->error()); QVERIFY(manager->isInitialized()); QVERIFY(!manager->isOperational()); QVERIFY(!manager->isBluetoothOperational()); delete manager; } void ManagerTest::bluezNotExportingInterfacesTest() { // org.bluez is running, but it does not export any interfaces // expected: init error FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-not-exporting-interfaces")); Manager *manager = new Manager; InitManagerJob *job = manager->init(); job->exec(); QVERIFY(job->error()); QVERIFY(!manager->isInitialized()); QVERIFY(!manager->isOperational()); QVERIFY(!manager->isBluetoothOperational()); delete manager; } void ManagerTest::bluezEmptyManagedObjectsTest() { // org.bluez exports ObjectManager, but there is no AgentManager1 // expected: init error FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-empty-managed-objects")); Manager *manager = new Manager; InitManagerJob *job = manager->init(); job->exec(); QVERIFY(job->error()); QVERIFY(!manager->isInitialized()); QVERIFY(!manager->isOperational()); QVERIFY(!manager->isBluetoothOperational()); delete manager; } void ManagerTest::bluezNoAdaptersTest() { // org.bluez exports ObjectManager with AgentManager1, but there are no adapters // expected: init successful FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-no-adapters")); Manager *manager = new Manager; InitManagerJob *job = manager->init(); job->exec(); QVERIFY(!job->error()); QVERIFY(manager->isInitialized()); QVERIFY(manager->isOperational()); QVERIFY(!manager->isBluetoothOperational()); delete manager; } void ManagerTest::bluezShutdownTest() { // tests whether the adapter/device removed signals work correctly FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-standard")); // Create adapters QDBusObjectPath adapter1path = QDBusObjectPath(QStringLiteral("/org/bluez/hci0")); QVariantMap adapterProps; adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter1path); adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E"); adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter"); adapterProps[QStringLiteral("Powered")] = true; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); QDBusObjectPath adapter2path = QDBusObjectPath(QStringLiteral("/org/bluez/hci1")); adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter2path); adapterProps[QStringLiteral("Address")] = QStringLiteral("2E:3A:C3:BC:85:7C"); adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter2"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); // Create devices QVariantMap deviceProps; deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci0/dev_40_79_6A_0C_39_75")); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(adapter1path); deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci1/dev_50_79_6A_0C_39_75")); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(adapter2path); deviceProps[QStringLiteral("Address")] = QStringLiteral("50:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice2"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); Manager *manager = new Manager; QSignalSpy btOperationalChangedSpy(manager, SIGNAL(bluetoothOperationalChanged(bool))); InitManagerJob *job = manager->init(); job->exec(); QVERIFY(!job->error()); QVERIFY(manager->isInitialized()); QVERIFY(manager->isOperational()); QVERIFY(manager->isBluetoothOperational()); QCOMPARE(manager->adapters().count(), 2); QCOMPARE(manager->devices().count(), 2); QCOMPARE(btOperationalChangedSpy.count(), 1); QCOMPARE(btOperationalChangedSpy.first().first().toBool(), true); AdapterPtr adapter1 = manager->adapterForAddress(QStringLiteral("1C:E5:C3:BC:94:7E")); AdapterPtr adapter2 = manager->adapterForAddress(QStringLiteral("2E:3A:C3:BC:85:7C")); DevicePtr device1 = manager->deviceForAddress(QStringLiteral("40:79:6A:0C:39:75")); DevicePtr device2 = manager->deviceForAddress(QStringLiteral("50:79:6A:0C:39:75")); QVERIFY(adapter1); QVERIFY(adapter2); QVERIFY(device1); QVERIFY(device2); QSignalSpy allAdaptersRemovedSpy(manager, SIGNAL(allAdaptersRemoved())); QSignalSpy adapterRemovedSpy(manager, SIGNAL(adapterRemoved(AdapterPtr))); QSignalSpy device1RemovedSpy(adapter1.data(), SIGNAL(deviceRemoved(DevicePtr))); QSignalSpy device2RemovedSpy(adapter2.data(), SIGNAL(deviceRemoved(DevicePtr))); btOperationalChangedSpy.clear(); FakeBluez::stop(); // allAdaptersRemoved will be last signal QTRY_COMPARE(allAdaptersRemovedSpy.count(), 1); QCOMPARE(adapterRemovedSpy.count(), 2); QCOMPARE(device1RemovedSpy.count(), 1); QCOMPARE(device2RemovedSpy.count(), 1); QCOMPARE(btOperationalChangedSpy.count(), 1); QCOMPARE(btOperationalChangedSpy.first().first().toBool(), false); delete manager; } void ManagerTest::usableAdapterTest() { FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-standard")); // Create adapters QDBusObjectPath adapter1path = QDBusObjectPath(QStringLiteral("/org/bluez/hci0")); QVariantMap adapterProps; adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter1path); adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E"); adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter"); adapterProps[QStringLiteral("Powered")] = false; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); QDBusObjectPath adapter2path = QDBusObjectPath(QStringLiteral("/org/bluez/hci1")); adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter2path); adapterProps[QStringLiteral("Address")] = QStringLiteral("2E:3A:C3:BC:85:7C"); adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter2"); adapterProps[QStringLiteral("Powered")] = false; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); Manager *manager = new Manager; QSignalSpy usableAdapterChangedSpy(manager, SIGNAL(usableAdapterChanged(AdapterPtr))); InitManagerJob *job = manager->init(); job->exec(); QVERIFY(!job->error()); QVERIFY(manager->isInitialized()); QVERIFY(manager->isOperational()); QVERIFY(!manager->isBluetoothOperational()); QCOMPARE(usableAdapterChangedSpy.count(), 0); QCOMPARE(manager->adapters().count(), 2); QCOMPARE(manager->devices().count(), 0); QVariantMap properties; properties[QStringLiteral("Path")] = QVariant::fromValue(adapter1path); properties[QStringLiteral("Name")] = QStringLiteral("Powered"); properties[QStringLiteral("Value")] = true; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("change-adapter-property"), properties); QTRY_COMPARE(usableAdapterChangedSpy.count(), 1); QCOMPARE(manager->usableAdapter()->ubi(), adapter1path.path()); usableAdapterChangedSpy.clear(); properties[QStringLiteral("Value")] = false; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("change-adapter-property"), properties); QTRY_COMPARE(usableAdapterChangedSpy.count(), 1); QVERIFY(manager->usableAdapter().isNull()); usableAdapterChangedSpy.clear(); properties[QStringLiteral("Path")] = QVariant::fromValue(adapter2path); properties[QStringLiteral("Value")] = true; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("change-adapter-property"), properties); QTRY_COMPARE(usableAdapterChangedSpy.count(), 1); QCOMPARE(manager->usableAdapter()->ubi(), adapter2path.path()); delete manager; } void ManagerTest::deviceForAddressTest() { // tests whether the deviceForAddress correctly prefer powered adapters FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-standard")); // Create adapters QDBusObjectPath adapter1path = QDBusObjectPath(QStringLiteral("/org/bluez/hci0")); QVariantMap adapterProps; adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter1path); adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E"); adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter"); adapterProps[QStringLiteral("Powered")] = false; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); QDBusObjectPath adapter2path = QDBusObjectPath(QStringLiteral("/org/bluez/hci1")); adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter2path); adapterProps[QStringLiteral("Address")] = QStringLiteral("2E:3A:C3:BC:85:7C"); adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter2"); adapterProps[QStringLiteral("Powered")] = true; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); // Create devices QVariantMap deviceProps; deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci0/dev_40_79_6A_0C_39_75")); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(adapter1path); deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci1/dev_40_79_6A_0C_39_75")); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(adapter2path); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); Manager *manager = new Manager; InitManagerJob *job = manager->init(); job->exec(); QVERIFY(!job->error()); QString address(QStringLiteral("40:79:6A:0C:39:75")); AdapterPtr adapter1 = manager->adapterForUbi(QStringLiteral("/org/bluez/hci0")); AdapterPtr adapter2 = manager->adapterForUbi(QStringLiteral("/org/bluez/hci1")); QVERIFY(adapter1); QVERIFY(adapter2); QSignalSpy adapter1Spy(adapter1.data(), SIGNAL(poweredChanged(bool))); QSignalSpy adapter2Spy(adapter2.data(), SIGNAL(poweredChanged(bool))); QCOMPARE(manager->deviceForAddress(address)->adapter(), adapter2); QVariantMap properties; properties[QStringLiteral("Path")] = QVariant::fromValue(adapter1path); properties[QStringLiteral("Name")] = QStringLiteral("Powered"); properties[QStringLiteral("Value")] = true; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("change-adapter-property"), properties); properties[QStringLiteral("Path")] = QVariant::fromValue(adapter2path); properties[QStringLiteral("Value")] = false; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("change-adapter-property"), properties); QTRY_COMPARE(adapter1Spy.count(), 1); QTRY_COMPARE(adapter2Spy.count(), 1); QCOMPARE(manager->deviceForAddress(address)->adapter(), adapter1); properties[QStringLiteral("Path")] = QVariant::fromValue(adapter1path); properties[QStringLiteral("Value")] = false; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("change-adapter-property"), properties); QTRY_COMPARE(adapter1Spy.count(), 2); QVERIFY(manager->deviceForAddress(address)); delete manager; } void ManagerTest::adapterWithDevicesRemovedTest() { // tests whether the devices are always removed from the adapter before removing adapter FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-standard")); // Create adapters QDBusObjectPath adapter1path = QDBusObjectPath(QStringLiteral("/org/bluez/hci0")); QVariantMap adapterProps; adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter1path); adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E"); adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter"); adapterProps[QStringLiteral("Powered")] = false; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); // Create devices QVariantMap deviceProps; deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci0/dev_40_79_6A_0C_39_75")); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(adapter1path); deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci0/dev_50_79_6A_0C_39_75")); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(adapter1path); deviceProps[QStringLiteral("Address")] = QStringLiteral("50:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice2"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); Manager *manager = new Manager; InitManagerJob *job = manager->init(); job->exec(); QVERIFY(!job->error()); AdapterPtr adapter = manager->adapterForAddress(QStringLiteral("1C:E5:C3:BC:94:7E")); DevicePtr device1 = manager->deviceForAddress(QStringLiteral("40:79:6A:0C:39:75")); DevicePtr device2 = manager->deviceForAddress(QStringLiteral("50:79:6A:0C:39:75")); QVERIFY(adapter); QVERIFY(device1); QVERIFY(device2); QSignalSpy adapterRemovedSpy(manager, SIGNAL(adapterRemoved(AdapterPtr))); QSignalSpy deviceRemovedSpy(manager, SIGNAL(deviceRemoved(DevicePtr))); QVariantMap properties; properties[QStringLiteral("Path")] = QVariant::fromValue(adapter1path); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("remove-adapter"), properties); QTRY_COMPARE(adapterRemovedSpy.count(), 1); QTRY_COMPARE(deviceRemovedSpy.count(), 2); QCOMPARE(manager->adapters().count(), 0); QCOMPARE(manager->devices().count(), 0); QCOMPARE(adapter->devices().count(), 0); delete manager; } void ManagerTest::bug364416() { // Bug 364416: Crash when device is added with adapter that is unknown to Manager FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-standard")); Manager *manager = new Manager; InitManagerJob *job = manager->init(); job->exec(); QVERIFY(!job->error()); // Add device to invalid adapter QVariantMap deviceProps; deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath("/org/bluez/hci0/dev_40_79_6A_0C_39_75")); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(QStringLiteral("/org/bluez/hci0"))); deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); // Wait for Manager to receive the interfacesAdded signal QTest::qWait(100); delete manager; } void ManagerTest::bug377405() { // Bug 377405: Property changes immediately after adapter is added are lost FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-standard")); Manager *manager = new Manager; InitManagerJob *job = manager->init(); job->exec(); QVERIFY(!job->error()); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("bug377405")); // Adapter property Powered is changed to true immediately after being added QTRY_COMPARE(manager->isBluetoothOperational(), true); } QTEST_MAIN(ManagerTest) diff --git a/autotests/mediaplayertest.cpp b/autotests/mediaplayertest.cpp index 968e1d7..09ddbd1 100644 --- a/autotests/mediaplayertest.cpp +++ b/autotests/mediaplayertest.cpp @@ -1,409 +1,409 @@ /* * Copyright (C) 2014-2015 David Rosca * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "mediaplayertest.h" #include "autotests.h" #include "pendingcall.h" #include "initmanagerjob.h" #include "services.h" -#include -#include +#include +#include namespace BluezQt { extern void bluezqt_initFakeBluezTestRun(); } using namespace BluezQt; Q_DECLARE_METATYPE(MediaPlayer::Equalizer) Q_DECLARE_METATYPE(MediaPlayer::Repeat) Q_DECLARE_METATYPE(MediaPlayer::Shuffle) Q_DECLARE_METATYPE(MediaPlayer::Status) MediaPlayerTest::MediaPlayerTest() : m_manager(nullptr) { Autotests::registerMetatypes(); qRegisterMetaType("Equalizer"); qRegisterMetaType("Repeat"); qRegisterMetaType("Shuffle"); qRegisterMetaType("Status"); qRegisterMetaType("MediaPlayerTrack"); } void MediaPlayerTest::initTestCase() { bluezqt_initFakeBluezTestRun(); FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-standard")); // Create adapter QString adapter = QStringLiteral("/org/bluez/hci0"); QVariantMap adapterProps; adapterProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(adapter)); adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E"); adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter"); FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps); // Create devices QVariantMap deviceProps; QVariantMap mediaPlayerProps; QVariantMap trackProps; QString device1 = adapter + QLatin1String("/dev_40_79_6A_0C_39_75"); deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device1)); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter)); deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice"); deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("0000110E-0000-1000-8000-00805F9B34FB")); mediaPlayerProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device1 + QLatin1String("/player0"))); mediaPlayerProps[QStringLiteral("Name")] = QStringLiteral("Player1"); mediaPlayerProps[QStringLiteral("Equalizer")] = QStringLiteral("on"); mediaPlayerProps[QStringLiteral("Repeat")] = QStringLiteral("singletrack"); mediaPlayerProps[QStringLiteral("Shuffle")] = QStringLiteral("group"); mediaPlayerProps[QStringLiteral("Status")] = QStringLiteral("stopped"); mediaPlayerProps[QStringLiteral("Position")] = QVariant::fromValue(quint32(150)); mediaPlayerProps[QStringLiteral("Device")] = QVariant::fromValue(QDBusObjectPath(device1)); trackProps[QStringLiteral("Title")] = QStringLiteral("Song1"); trackProps[QStringLiteral("Artist")] = QStringLiteral("Band1"); trackProps[QStringLiteral("Album")] = QStringLiteral("First Album"); trackProps[QStringLiteral("Genre")] = QStringLiteral("Rock"); trackProps[QStringLiteral("NumberOfTracks")] = 24; trackProps[QStringLiteral("TrackNumber")] = 4; trackProps[QStringLiteral("Duration")] = 12403; mediaPlayerProps[QStringLiteral("Track")] = trackProps; deviceProps[QStringLiteral("MediaPlayer")] = mediaPlayerProps; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); QString device2 = adapter + QLatin1String("/dev_50_79_6A_0C_39_75"); deviceProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device2)); deviceProps[QStringLiteral("Adapter")] = QVariant::fromValue(QDBusObjectPath(adapter)); deviceProps[QStringLiteral("Address")] = QStringLiteral("50:79:6A:0C:39:75"); deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice2"); deviceProps[QStringLiteral("UUIDs")] = QStringList(QStringLiteral("0000110E-0000-1000-8000-00805F9B34FB")); mediaPlayerProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(device2 + QLatin1String("/player0"))); mediaPlayerProps[QStringLiteral("Name")] = QStringLiteral("Player2"); mediaPlayerProps[QStringLiteral("Equalizer")] = QStringLiteral("off"); mediaPlayerProps[QStringLiteral("Repeat")] = QStringLiteral("alltracks"); mediaPlayerProps[QStringLiteral("Shuffle")] = QStringLiteral("off"); mediaPlayerProps[QStringLiteral("Status")] = QStringLiteral("paused"); mediaPlayerProps[QStringLiteral("Position")] = QVariant::fromValue(quint32(2500)); mediaPlayerProps[QStringLiteral("Device")] = QVariant::fromValue(QDBusObjectPath(device2)); trackProps[QStringLiteral("Title")] = QStringLiteral("Song2"); trackProps[QStringLiteral("Artist")] = QStringLiteral("Band2"); trackProps[QStringLiteral("Album")] = QStringLiteral("Second Album"); trackProps[QStringLiteral("Genre")] = QStringLiteral("Heavy Metal"); trackProps[QStringLiteral("NumberOfTracks")] = 666; trackProps[QStringLiteral("TrackNumber")] = 22; trackProps[QStringLiteral("Duration")] = 22403; mediaPlayerProps[QStringLiteral("Track")] = trackProps; deviceProps[QStringLiteral("MediaPlayer")] = mediaPlayerProps; FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps); m_manager = new Manager(); InitManagerJob *initJob = m_manager->init(); initJob->exec(); QVERIFY(!initJob->error()); QCOMPARE(m_manager->adapters().count(), 1); QCOMPARE(m_manager->devices().count(), 2); } void MediaPlayerTest::cleanupTestCase() { Q_FOREACH (const MediaPlayerUnit &unit, m_units) { delete unit.dbusMediaPlayer; delete unit.dbusProperties; } delete m_manager; FakeBluez::stop(); } void MediaPlayerTest::connectTest() { Q_FOREACH (DevicePtr device, m_manager->devices()) { QVERIFY(!device->ubi().isEmpty()); QVERIFY(!device->mediaPlayer()); QSignalSpy deviceSpy(device.data(), SIGNAL(mediaPlayerChanged(MediaPlayerPtr))); device->connectToDevice(); QTRY_COMPARE(deviceSpy.count(), 1); QVERIFY(device->mediaPlayer()); } } void MediaPlayerTest::disconnectTest() { Q_FOREACH (DevicePtr device, m_manager->devices()) { QVERIFY(device->mediaPlayer()); QSignalSpy deviceSpy(device.data(), SIGNAL(mediaPlayerChanged(MediaPlayerPtr))); device->disconnectFromDevice(); QTRY_COMPARE(deviceSpy.count(), 1); QVERIFY(!device->mediaPlayer()); } } void MediaPlayerTest::connectProfileTest() { QDBusConnection connection = QDBusConnection::sessionBus(); QString service = QStringLiteral("org.kde.bluezqt.fakebluez"); Q_FOREACH (DevicePtr device, m_manager->devices()) { QVERIFY(!device->ubi().isEmpty()); QVERIFY(!device->mediaPlayer()); QSignalSpy deviceSpy(device.data(), SIGNAL(mediaPlayerChanged(MediaPlayerPtr))); device->connectProfile(Services::AudioVideoRemoteControl); QTRY_COMPARE(deviceSpy.count(), 1); QVERIFY(device->mediaPlayer()); QString path = device->ubi() + QLatin1String("/player0"); MediaPlayerUnit u; u.device = device; u.dbusMediaPlayer = new org::bluez::MediaPlayer1(service, path, connection, this); u.dbusProperties = new org::freedesktop::DBus::Properties(service, path, connection, this); m_units.append(u); } } void MediaPlayerTest::getPropertiesTest() { Q_FOREACH (const MediaPlayerUnit &unit, m_units) { QCOMPARE(unit.device->mediaPlayer()->name(), unit.dbusMediaPlayer->name()); QCOMPARE(equalizerString(unit.device->mediaPlayer()), unit.dbusMediaPlayer->equalizer()); QCOMPARE(repeatString(unit.device->mediaPlayer()), unit.dbusMediaPlayer->repeat()); QCOMPARE(shuffleString(unit.device->mediaPlayer()), unit.dbusMediaPlayer->shuffle()); QCOMPARE(statusString(unit.device->mediaPlayer()), unit.dbusMediaPlayer->status()); QCOMPARE(trackMap(unit.device->mediaPlayer()), unit.dbusMediaPlayer->track()); QCOMPARE(unit.device->mediaPlayer()->position(), unit.dbusMediaPlayer->position()); } } void MediaPlayerTest::setEqualizerTest() { Q_FOREACH (const MediaPlayerUnit &unit, m_units) { MediaPlayerPtr mediaPlayer = unit.device->mediaPlayer(); MediaPlayer::Equalizer value = mediaPlayer->equalizer() == MediaPlayer::EqualizerOff ? MediaPlayer::EqualizerOn : MediaPlayer::EqualizerOff; QSignalSpy equalizerSpy(mediaPlayer.data(), SIGNAL(equalizerChanged(Equalizer))); mediaPlayer->setEqualizer(value); QTRY_COMPARE(equalizerSpy.count(), 1); QCOMPARE(equalizerSpy.at(0).at(0).value(), value); QCOMPARE(mediaPlayer->equalizer(), value); } } void MediaPlayerTest::setRepeatTest() { Q_FOREACH (const MediaPlayerUnit &unit, m_units) { MediaPlayerPtr mediaPlayer = unit.device->mediaPlayer(); MediaPlayer::Repeat value = mediaPlayer->repeat() == MediaPlayer::RepeatGroup ? MediaPlayer::RepeatSingleTrack : MediaPlayer::RepeatGroup; QSignalSpy equalizerSpy(mediaPlayer.data(), SIGNAL(repeatChanged(Repeat))); mediaPlayer->setRepeat(value); QTRY_COMPARE(equalizerSpy.count(), 1); QCOMPARE(equalizerSpy.at(0).at(0).value(), value); QCOMPARE(mediaPlayer->repeat(), value); } } void MediaPlayerTest::setShuffleTest() { Q_FOREACH (const MediaPlayerUnit &unit, m_units) { MediaPlayerPtr mediaPlayer = unit.device->mediaPlayer(); MediaPlayer::Shuffle value = mediaPlayer->shuffle() == MediaPlayer::ShuffleAllTracks ? MediaPlayer::ShuffleOff : MediaPlayer::ShuffleAllTracks; QSignalSpy equalizerSpy(mediaPlayer.data(), SIGNAL(shuffleChanged(Shuffle))); mediaPlayer->setShuffle(value); QTRY_COMPARE(equalizerSpy.count(), 1); QCOMPARE(equalizerSpy.at(0).at(0).value(), value); QCOMPARE(mediaPlayer->shuffle(), value); } } void MediaPlayerTest::changeStatusTest() { Q_FOREACH (const MediaPlayerUnit &unit, m_units) { MediaPlayerPtr mediaPlayer = unit.device->mediaPlayer(); QSignalSpy statusSpy(mediaPlayer.data(), SIGNAL(statusChanged(Status))); if (mediaPlayer->status() != MediaPlayer::Stopped) { mediaPlayer->stop(); QTRY_COMPARE(statusSpy.count(), 1); QCOMPARE(statusSpy.at(0).at(0).value(), MediaPlayer::Stopped); QCOMPARE(mediaPlayer->status(), MediaPlayer::Stopped); } statusSpy.clear(); mediaPlayer->play(); QTRY_COMPARE(statusSpy.count(), 1); QCOMPARE(statusSpy.at(0).at(0).value(), MediaPlayer::Playing); QCOMPARE(mediaPlayer->status(), MediaPlayer::Playing); statusSpy.clear(); mediaPlayer->pause(); QTRY_COMPARE(statusSpy.count(), 1); QCOMPARE(statusSpy.at(0).at(0).value(), MediaPlayer::Paused); QCOMPARE(mediaPlayer->status(), MediaPlayer::Paused); statusSpy.clear(); mediaPlayer->fastForward(); QTRY_COMPARE(statusSpy.count(), 1); QCOMPARE(statusSpy.at(0).at(0).value(), MediaPlayer::ForwardSeek); QCOMPARE(mediaPlayer->status(), MediaPlayer::ForwardSeek); statusSpy.clear(); mediaPlayer->rewind(); QTRY_COMPARE(statusSpy.count(), 1); QCOMPARE(statusSpy.at(0).at(0).value(), MediaPlayer::ReverseSeek); QCOMPARE(mediaPlayer->status(), MediaPlayer::ReverseSeek); statusSpy.clear(); mediaPlayer->stop(); QTRY_COMPARE(statusSpy.count(), 1); QCOMPARE(statusSpy.at(0).at(0).value(), MediaPlayer::Stopped); QCOMPARE(mediaPlayer->status(), MediaPlayer::Stopped); } } void MediaPlayerTest::changeTrackTest() { Q_FOREACH (const MediaPlayerUnit &unit, m_units) { MediaPlayerPtr mediaPlayer = unit.device->mediaPlayer(); QSignalSpy trackSpy(mediaPlayer.data(), SIGNAL(trackChanged(MediaPlayerTrack))); trackSpy.clear(); mediaPlayer->next(); QTRY_COMPARE(trackSpy.count(), 1); QCOMPARE(mediaPlayer->track().isValid(), true); trackSpy.clear(); mediaPlayer->previous(); QTRY_COMPARE(trackSpy.count(), 1); QCOMPARE(mediaPlayer->track().isValid(), true); } } void MediaPlayerTest::disconnectProfileTest() { Q_FOREACH (const MediaPlayerUnit &unit, m_units) { QVERIFY(unit.device->mediaPlayer()); QSignalSpy deviceSpy(unit.device.data(), SIGNAL(mediaPlayerChanged(MediaPlayerPtr))); unit.device->disconnectProfile(Services::AudioVideoRemoteControl); QTRY_COMPARE(deviceSpy.count(), 1); QVERIFY(!unit.device->mediaPlayer()); } } QString MediaPlayerTest::equalizerString(const MediaPlayerPtr &mediaPlayer) const { switch (mediaPlayer->equalizer()) { case MediaPlayer::EqualizerOn: return QStringLiteral("on"); default: return QStringLiteral("off"); } } QString MediaPlayerTest::repeatString(const MediaPlayerPtr &mediaPlayer) const { switch (mediaPlayer->repeat()) { case MediaPlayer::RepeatSingleTrack: return QStringLiteral("singletrack"); case MediaPlayer::RepeatAllTracks: return QStringLiteral("alltracks"); case MediaPlayer::RepeatGroup: return QStringLiteral("group"); default: return QStringLiteral("off"); } } QString MediaPlayerTest::shuffleString(const MediaPlayerPtr &mediaPlayer) const { switch (mediaPlayer->shuffle()) { case MediaPlayer::ShuffleAllTracks: return QStringLiteral("alltracks"); case MediaPlayer::ShuffleGroup: return QStringLiteral("group"); default: return QStringLiteral("off"); } } QString MediaPlayerTest::statusString(const MediaPlayerPtr &mediaPlayer) const { switch (mediaPlayer->status()) { case MediaPlayer::Playing: return QStringLiteral("playing"); case MediaPlayer::Stopped: return QStringLiteral("stopped"); case MediaPlayer::Paused: return QStringLiteral("paused"); case MediaPlayer::ForwardSeek: return QStringLiteral("forward-seek"); case MediaPlayer::ReverseSeek: return QStringLiteral("reverse-seek"); default: return QStringLiteral("error"); } } QVariantMap MediaPlayerTest::trackMap(const MediaPlayerPtr &mediaPlayer) const { QVariantMap out; if (!mediaPlayer->track().isValid()) { return out; } out[QStringLiteral("Title")] = mediaPlayer->track().title(); out[QStringLiteral("Artist")] = mediaPlayer->track().artist(); out[QStringLiteral("Album")] = mediaPlayer->track().album(); out[QStringLiteral("Genre")] = mediaPlayer->track().genre(); out[QStringLiteral("NumberOfTracks")] = mediaPlayer->track().numberOfTracks(); out[QStringLiteral("TrackNumber")] = mediaPlayer->track().trackNumber(); out[QStringLiteral("Duration")] = mediaPlayer->track().duration(); return out; } QTEST_MAIN(MediaPlayerTest) diff --git a/autotests/mediatest.cpp b/autotests/mediatest.cpp index 8cb2d4d..19268ef 100644 --- a/autotests/mediatest.cpp +++ b/autotests/mediatest.cpp @@ -1,134 +1,134 @@ /* * Copyright (C) 2018 Manuel Weichselbaumer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "mediatest.h" #include "a2dp-codecs.h" #include "autotests.h" #include "initmanagerjob.h" #include "manager.h" #include "media.h" #include "mediaendpoint.h" #include "pendingcall.h" #include -#include +#include #include namespace BluezQt { extern void bluezqt_initFakeBluezTestRun(); } using namespace BluezQt; void TestEndpoint::release() { m_releaseCalled = true; } void MediaTest::initTestCase() { bluezqt_initFakeBluezTestRun(); FakeBluez::start(); FakeBluez::runTest(QStringLiteral("bluez-standard")); Manager *manager = new Manager(this); InitManagerJob *initJob = manager->init(); initJob->exec(); QVERIFY(!initJob->error()); QVERIFY(manager->media()); m_endpoint = new TestEndpoint({MediaEndpoint::Role::AudioSink, MediaEndpoint::Codec::Sbc}, this); manager->media()->registerEndpoint(m_endpoint)->waitForFinished(); } void MediaTest::cleanupTestCase() { FakeBluez::stop(); } void MediaTest::setConfigurationTest() { QSignalSpy endpointSpy(m_endpoint, SIGNAL(configurationSet(QString,QVariantMap))); QVariantMap props; props.insert(QStringLiteral("Key"), QVariant::fromValue(int(123))); QVariantMap params; params.insert(QStringLiteral("Transport"), QVariant::fromValue(m_endpoint->objectPath())); params.insert(QStringLiteral("Properties"), props); FakeBluez::runAction(QStringLiteral("media"), QStringLiteral("set-configuration"), params); endpointSpy.wait(); auto args = endpointSpy.takeFirst(); QCOMPARE(args.at(0).toString(), m_endpoint->objectPath().path()); QCOMPARE(args.at(1).value(), props); } void MediaTest::selectConfigurationTest() { QSignalSpy endpointSpy(m_endpoint, SIGNAL(configurationSelected(QByteArray,QByteArray))); a2dp_sbc_t sbcConfiguration; sbcConfiguration.frequency = SBC_SAMPLING_FREQ_44100; sbcConfiguration.channel_mode = SBC_CHANNEL_MODE_STEREO; sbcConfiguration.block_length = SBC_BLOCK_LENGTH_16; sbcConfiguration.subbands = SBC_SUBBANDS_8; sbcConfiguration.allocation_method = SBC_ALLOCATION_LOUDNESS; sbcConfiguration.min_bitpool = 2; sbcConfiguration.max_bitpool = 53; QVariantMap params; params.insert(QStringLiteral("Capabilities"), QByteArray(reinterpret_cast(&sbcCapabilities), sizeof(sbcCapabilities))); FakeBluez::runAction(QStringLiteral("media"), QStringLiteral("select-configuration"), params); endpointSpy.wait(); auto args = endpointSpy.takeFirst(); QCOMPARE(args.at(0).toByteArray(), QByteArray(reinterpret_cast(&sbcCapabilities), sizeof(sbcCapabilities))); QCOMPARE(args.at(1).toByteArray(), QByteArray(reinterpret_cast(&sbcConfiguration), sizeof(sbcConfiguration))); params.insert(QStringLiteral("Capabilities"), QByteArray()); FakeBluez::runAction(QStringLiteral("media"), QStringLiteral("select-configuration"), params); endpointSpy.wait(); args = endpointSpy.takeFirst(); QCOMPARE(args.at(0).toByteArray(), QByteArray()); QCOMPARE(args.at(1).toByteArray(), QByteArray()); } void MediaTest::clearConfigurationTest() { QSignalSpy endpointSpy(m_endpoint, SIGNAL(configurationCleared(QString))); QVariantMap params; params.insert(QStringLiteral("Transport"), QVariant::fromValue(m_endpoint->objectPath())); FakeBluez::runAction(QStringLiteral("media"), QStringLiteral("clear-configuration"), params); endpointSpy.wait(); auto args = endpointSpy.takeFirst(); QCOMPARE(args.at(0).toString(), m_endpoint->objectPath().path()); } void MediaTest::releaseTest() { QCOMPARE(m_endpoint->m_releaseCalled, false); FakeBluez::runAction(QStringLiteral("media"), QStringLiteral("release")); QTRY_COMPARE(m_endpoint->m_releaseCalled, true); } QTEST_MAIN(MediaTest) diff --git a/autotests/obexmanagertest.cpp b/autotests/obexmanagertest.cpp index 31ff50f..6517d4e 100644 --- a/autotests/obexmanagertest.cpp +++ b/autotests/obexmanagertest.cpp @@ -1,152 +1,152 @@ /* * Copyright (C) 2014-2015 David Rosca * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "obexmanagertest.h" #include "autotests.h" #include "obexmanager.h" #include "initobexmanagerjob.h" -#include -#include +#include +#include namespace BluezQt { extern void bluezqt_initFakeBluezTestRun(); } using namespace BluezQt; void ObexManagerTest::initTestCase() { bluezqt_initFakeBluezTestRun(); FakeBluez::start(); // to check that it works } void ObexManagerTest::cleanup() { FakeBluez::stop(); } void ObexManagerTest::obexNotRunningTest() { // org.bluez.obex is not running at all // expected: init successful ObexManager *manager = new ObexManager; InitObexManagerJob *job = manager->init(); job->exec(); QVERIFY(!job->error()); QVERIFY(manager->isInitialized()); QVERIFY(!manager->isOperational()); delete manager; } void ObexManagerTest::obexNotExportingInterfacesTest() { // org.bluez.obex is running, but it does not export any interfaces // expected: init error FakeBluez::start(); FakeBluez::runTest(QStringLiteral("obex-not-exporting-interfaces")); ObexManager *manager = new ObexManager; InitObexManagerJob *job = manager->init(); job->exec(); QVERIFY(job->error()); QVERIFY(!manager->isInitialized()); QVERIFY(!manager->isOperational()); delete manager; } void ObexManagerTest::obexEmptyManagedObjectsTest() { // org.bluez.obex exports ObjectManager, but there is no AgentManager1 nor Client1 // expected: init error FakeBluez::start(); FakeBluez::runTest(QStringLiteral("obex-empty-managed-objects")); ObexManager *manager = new ObexManager; InitObexManagerJob *job = manager->init(); job->exec(); QVERIFY(job->error()); QVERIFY(!manager->isInitialized()); QVERIFY(!manager->isOperational()); delete manager; } void ObexManagerTest::obexNoClientTest() { // org.bluez.obex exports ObjectManager and AgentManager1, but there is no Client1 // expected: init error FakeBluez::start(); FakeBluez::runTest(QStringLiteral("obex-no-client")); ObexManager *manager = new ObexManager; InitObexManagerJob *job = manager->init(); job->exec(); QVERIFY(job->error()); QVERIFY(!manager->isInitialized()); QVERIFY(!manager->isOperational()); delete manager; } void ObexManagerTest::obexNoAgentManagerTest() { // org.bluez.obex exports ObjectManager and Client1, but there is no AgentManager1 // expected: init error FakeBluez::start(); FakeBluez::runTest(QStringLiteral("obex-no-agentmanager")); ObexManager *manager = new ObexManager; InitObexManagerJob *job = manager->init(); job->exec(); QVERIFY(job->error()); QVERIFY(!manager->isInitialized()); QVERIFY(!manager->isOperational()); delete manager; } void ObexManagerTest::obexServiceOkTest() { // org.bluez.obex is running and exports all used interfaces // expected: init successful FakeBluez::start(); FakeBluez::runTest(QStringLiteral("obex-standard")); ObexManager *manager = new ObexManager; InitObexManagerJob *job = manager->init(); job->exec(); QVERIFY(!job->error()); QVERIFY(manager->isInitialized()); QVERIFY(manager->isOperational()); delete manager; } QTEST_MAIN(ObexManagerTest)