diff options
author | Alex Blasche <alexander.blasche@digia.com> | 2013-07-05 10:15:52 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-07-15 12:36:19 +0200 |
commit | 6c42b93e3f4e06e289af54920e0058baed9b4d33 (patch) | |
tree | deef7875d357204e1bb9958912979f8144ef82b3 /tests/auto/qbluetoothlocaldevice | |
parent | 1f0771e2782652b7256eb079e38d0dff6a730794 (diff) |
Improve and fix the qbluetoothlocaldevice test
The Bluez code was not passing the test.
Task-number: QTBUG-22017
Change-Id: I0bc2fca7ec9d352481eea24c75b902c21b4fc0da
Reviewed-by: Aaron McCarthy <mccarthy.aaron@gmail.com>
Diffstat (limited to 'tests/auto/qbluetoothlocaldevice')
-rw-r--r-- | tests/auto/qbluetoothlocaldevice/tst_qbluetoothlocaldevice.cpp | 176 |
1 files changed, 127 insertions, 49 deletions
diff --git a/tests/auto/qbluetoothlocaldevice/tst_qbluetoothlocaldevice.cpp b/tests/auto/qbluetoothlocaldevice/tst_qbluetoothlocaldevice.cpp index fbebc250..bd2272f1 100644 --- a/tests/auto/qbluetoothlocaldevice/tst_qbluetoothlocaldevice.cpp +++ b/tests/auto/qbluetoothlocaldevice/tst_qbluetoothlocaldevice.cpp @@ -49,6 +49,15 @@ QT_USE_NAMESPACE_BLUETOOTH +/* + * Running the manual tests requires another Bluetooth device in the vincinity. + * The remote device's address must be passed via the BT_TEST_DEVICE env variable. + * Every pairing request must be accepted within a 10s interval of appearing. + **/ +#define BT_TEST_DEVICE "98:D6:F7:D9:A5:3D" + + + class tst_QBluetoothLocalDevice : public QObject { Q_OBJECT @@ -71,52 +80,36 @@ private slots: void tst_pairingStatus(); void tst_pairDevice_data(); void tst_pairDevice(); + +private: + QBluetoothAddress remoteDevice; }; tst_QBluetoothLocalDevice::tst_QBluetoothLocalDevice() { + const QString remote = qgetenv("BT_TEST_DEVICE"); + if (remote == QStringLiteral("default")) { + remoteDevice = QBluetoothAddress(BT_TEST_DEVICE); + qWarning() << "Using default remote device for testing"; + } else if (!remote.isEmpty()) { + remoteDevice = QBluetoothAddress(remote); + qWarning() << "Using remote device " << remote << " for testing. Ensure that the device is discoverable for pairing requests"; + } else { + qWarning() << "Not using any remote device for testing. Set BT_TEST_DEVICE env to run manual tests involving a remote device"; + } + // start with host powered off QBluetoothLocalDevice *device = new QBluetoothLocalDevice(); device->setHostMode(QBluetoothLocalDevice::HostPoweredOff); delete device; // wait for the device to switch bluetooth mode. QTest::qWait(1000); - } tst_QBluetoothLocalDevice::~tst_QBluetoothLocalDevice() { } -void tst_QBluetoothLocalDevice::tst_hostModes_data() -{ - QTest::addColumn<QBluetoothLocalDevice::HostMode>("hostModeExpected"); - - QTest::newRow("HostDiscoverable") << QBluetoothLocalDevice::HostDiscoverable; - QTest::newRow("HostPoweredOff") << QBluetoothLocalDevice::HostPoweredOff; - QTest::newRow("HostConnectable") << QBluetoothLocalDevice::HostConnectable; - QTest::newRow("HostPoweredOff") << QBluetoothLocalDevice::HostPoweredOff; - QTest::newRow("HostDiscoverable") << QBluetoothLocalDevice::HostDiscoverable; - -} -void tst_QBluetoothLocalDevice::tst_pairDevice_data() -{ - QTest::addColumn<QBluetoothAddress>("deviceAddress"); - QTest::addColumn<QBluetoothLocalDevice::Pairing>("pairingExpected"); - - QTest::newRow("UnPaired Device: DUMMY") << QBluetoothAddress("11:00:00:00:00:00") - << QBluetoothLocalDevice::Unpaired; -} - -void tst_QBluetoothLocalDevice::tst_pairingStatus_data() -{ - QTest::addColumn<QBluetoothAddress>("deviceAddress"); - QTest::addColumn<QBluetoothLocalDevice::Pairing>("pairingExpected"); - - QTest::newRow("UnPaired Device: DUMMY") << QBluetoothAddress("11:00:00:00:00:00") - << QBluetoothLocalDevice::Unpaired; -} - void tst_QBluetoothLocalDevice::tst_powerOn() { QBluetoothLocalDevice localDevice; @@ -164,9 +157,28 @@ void tst_QBluetoothLocalDevice::tst_powerOff() } +void tst_QBluetoothLocalDevice::tst_hostModes_data() +{ + QTest::addColumn<QBluetoothLocalDevice::HostMode>("hostModeExpected"); + QTest::addColumn<bool>("expectSignal"); + + QTest::newRow("HostDiscoverable1") << QBluetoothLocalDevice::HostDiscoverable << true; + QTest::newRow("HostPoweredOff1") << QBluetoothLocalDevice::HostPoweredOff << true; + QTest::newRow("HostPoweredOff2") << QBluetoothLocalDevice::HostPoweredOff << false; + QTest::newRow("HostConnectable1") << QBluetoothLocalDevice::HostConnectable << true; + QTest::newRow("HostConnectable2") << QBluetoothLocalDevice::HostConnectable << false; + QTest::newRow("HostDiscoverable2") << QBluetoothLocalDevice::HostDiscoverable << true; + QTest::newRow("HostConnectable3") << QBluetoothLocalDevice::HostConnectable << true; + QTest::newRow("HostPoweredOff3") << QBluetoothLocalDevice::HostPoweredOff << true; + QTest::newRow("HostDiscoverable3") << QBluetoothLocalDevice::HostDiscoverable << true; + QTest::newRow("HostDiscoverable4") << QBluetoothLocalDevice::HostDiscoverable << false; + QTest::newRow("HostConnectable4") << QBluetoothLocalDevice::HostConnectable << true; +} + void tst_QBluetoothLocalDevice::tst_hostModes() { QFETCH(QBluetoothLocalDevice::HostMode, hostModeExpected); + QFETCH(bool, expectSignal); if (!QBluetoothLocalDevice::allDevices().count()) QSKIP("Skipping test due to missing Bluetooth device"); @@ -178,7 +190,6 @@ void tst_QBluetoothLocalDevice::tst_hostModes() QVERIFY(hostModeSpy.isEmpty()); QTest::qWait(1000); - localDevice.setHostMode(hostModeExpected); // wait for the device to switch bluetooth mode. QTest::qWait(1000); @@ -186,10 +197,16 @@ void tst_QBluetoothLocalDevice::tst_hostModes() QTRY_VERIFY(hostModeSpy.count() > 0); } // test the actual signal values. - QVERIFY(hostModeSpy.count() > 0); - QList<QVariant> arguments = hostModeSpy.takeFirst(); - QBluetoothLocalDevice::HostMode hostMode = qvariant_cast<QBluetoothLocalDevice::HostMode>(arguments.at(0)); - QCOMPARE(hostModeExpected, hostMode); + if (expectSignal) + QVERIFY(hostModeSpy.count() > 0); + else + QVERIFY(hostModeSpy.count() == 0); + + if (expectSignal) { + QList<QVariant> arguments = hostModeSpy.takeLast(); + QBluetoothLocalDevice::HostMode hostMode = qvariant_cast<QBluetoothLocalDevice::HostMode>(arguments.at(0)); + QCOMPARE(hostModeExpected, hostMode); + } // test actual QCOMPARE(hostModeExpected, localDevice.hostMode()); } @@ -201,6 +218,7 @@ void tst_QBluetoothLocalDevice::tst_address() QBluetoothLocalDevice localDevice; QVERIFY(!localDevice.address().toString().isEmpty()); + QVERIFY(!localDevice.address().isNull()); } void tst_QBluetoothLocalDevice::tst_name() { @@ -260,40 +278,103 @@ void tst_QBluetoothLocalDevice::tst_construction() } +void tst_QBluetoothLocalDevice::tst_pairDevice_data() +{ + QTest::addColumn<QBluetoothAddress>("deviceAddress"); + QTest::addColumn<QBluetoothLocalDevice::Pairing>("pairingExpected"); + //waiting time larger for manual tests -> requires device interaction + QTest::addColumn<int>("pairingWaitTime"); + QTest::addColumn<bool>("expectErrorSignal"); + + QTest::newRow("UnPaired Device: DUMMY->unpaired") << QBluetoothAddress("11:00:00:00:00:00") + << QBluetoothLocalDevice::Unpaired << 1000 << false; + QTest::newRow("UnPaired Device: DUMMY->paired") << QBluetoothAddress("11:00:00:00:00:00") + << QBluetoothLocalDevice::Paired << 1000 << true; + QTest::newRow("UnPaired Device: DUMMY") << QBluetoothAddress() + << QBluetoothLocalDevice::Unpaired << 1000 << true; + + if (!remoteDevice.isNull()) { + QTest::newRow("UnParing Test device 1") << QBluetoothAddress(remoteDevice) + << QBluetoothLocalDevice::Unpaired << 1000 << false; + QTest::newRow("Pairing Test Device") << QBluetoothAddress(remoteDevice) + << QBluetoothLocalDevice::Paired << 10000 << false; + QTest::newRow("Pairing upgrade for Authorization") << QBluetoothAddress(remoteDevice) + << QBluetoothLocalDevice::AuthorizedPaired << 1000 << false; + QTest::newRow("Unpairing Test device 2") << QBluetoothAddress(remoteDevice) + << QBluetoothLocalDevice::Unpaired << 1000 << false; + QTest::newRow("Authorized Pairing") << QBluetoothAddress(remoteDevice) + << QBluetoothLocalDevice::AuthorizedPaired << 10000 << false; + QTest::newRow("Pairing Test Device after Authorization Pairing") << QBluetoothAddress(remoteDevice) + << QBluetoothLocalDevice::Paired << 1000 << false; + QTest::newRow("Pairing Test Device after Authorization2") << QBluetoothAddress(remoteDevice) + << QBluetoothLocalDevice::Paired << 1000 << false; //same again + QTest::newRow("Unpairing Test device 3") << QBluetoothAddress(remoteDevice) + << QBluetoothLocalDevice::Unpaired << 1000 << false; + QTest::newRow("UnParing Test device 4") << QBluetoothAddress(remoteDevice) + << QBluetoothLocalDevice::Unpaired << 1000 << false; + } +} + void tst_QBluetoothLocalDevice::tst_pairDevice() { QFETCH(QBluetoothAddress, deviceAddress); QFETCH(QBluetoothLocalDevice::Pairing, pairingExpected); + QFETCH(int, pairingWaitTime); + QFETCH(bool, expectErrorSignal); if (!QBluetoothLocalDevice::allDevices().count()) QSKIP("Skipping test due to missing Bluetooth device"); - qDebug() << "tst_pairDevice(): address=" << deviceAddress.toString() << "pairingModeExpected=" - << static_cast<int>(pairingExpected); - QBluetoothLocalDevice localDevice; //powerOn if not already localDevice.powerOn(); + QVERIFY(localDevice.hostMode() != QBluetoothLocalDevice::HostPoweredOff); QSignalSpy pairingSpy(&localDevice, SIGNAL(pairingFinished(const QBluetoothAddress &,QBluetoothLocalDevice::Pairing)) ); + QSignalSpy errorSpy(&localDevice, SIGNAL(error(QBluetoothLocalDevice::Error))); // there should be no signals yet QVERIFY(pairingSpy.isValid()); QVERIFY(pairingSpy.isEmpty()); + QVERIFY(errorSpy.isValid()); + QVERIFY(errorSpy.isEmpty()); QVERIFY(localDevice.isValid()); + localDevice.requestPairing(deviceAddress, pairingExpected); // async, wait for it - QTRY_VERIFY(pairingSpy.count() > 0); + QTest::qWait(pairingWaitTime); + + if (expectErrorSignal) { + QTRY_VERIFY(!errorSpy.isEmpty()); + QVERIFY(pairingSpy.isEmpty()); + QList<QVariant> arguments = errorSpy.first(); + QBluetoothLocalDevice::Error e = qvariant_cast<QBluetoothLocalDevice::Error>(arguments.at(0)); + QCOMPARE(e, QBluetoothLocalDevice::PairingError); + } else { + QTRY_VERIFY(!pairingSpy.isEmpty()); + QVERIFY(errorSpy.isEmpty()); + + // test the actual signal values. + QList<QVariant> arguments = pairingSpy.takeFirst(); + QBluetoothAddress address = qvariant_cast<QBluetoothAddress>(arguments.at(0)); + QBluetoothLocalDevice::Pairing pairingResult = qvariant_cast<QBluetoothLocalDevice::Pairing>(arguments.at(1)); + QCOMPARE(deviceAddress, address); + QCOMPARE(pairingExpected, pairingResult); + } - // test the actual signal values. - QList<QVariant> arguments = pairingSpy.takeFirst(); - QBluetoothAddress address = qvariant_cast<QBluetoothAddress>(arguments.at(0)); - QBluetoothLocalDevice::Pairing pairingResult = qvariant_cast<QBluetoothLocalDevice::Pairing>(arguments.at(1)); - QCOMPARE(deviceAddress, address); - QCOMPARE(pairingExpected, pairingResult); + if (!expectErrorSignal) + QCOMPARE(pairingExpected, localDevice.pairingStatus(deviceAddress)); +} - QCOMPARE(pairingExpected, localDevice.pairingStatus(deviceAddress)); +void tst_QBluetoothLocalDevice::tst_pairingStatus_data() +{ + QTest::addColumn<QBluetoothAddress>("deviceAddress"); + QTest::addColumn<QBluetoothLocalDevice::Pairing>("pairingExpected"); + QTest::newRow("UnPaired Device: DUMMY") << QBluetoothAddress("11:00:00:00:00:00") + << QBluetoothLocalDevice::Unpaired; + QTest::newRow("Invalid device") << QBluetoothAddress() << QBluetoothLocalDevice::Unpaired; + //valid devices are already tested by tst_pairDevice() } void tst_QBluetoothLocalDevice::tst_pairingStatus() @@ -301,9 +382,6 @@ void tst_QBluetoothLocalDevice::tst_pairingStatus() QFETCH(QBluetoothAddress, deviceAddress); QFETCH(QBluetoothLocalDevice::Pairing, pairingExpected); - qDebug() << "tst_pairingStatus(): address=" << deviceAddress.toString() << "pairingModeExpected=" - << static_cast<int>(pairingExpected); - QBluetoothLocalDevice localDevice; QCOMPARE(pairingExpected, localDevice.pairingStatus(deviceAddress)); } |