summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qbluetoothdevicediscoveryagent/qbluetoothdevicediscoveryagent.pro4
-rw-r--r--tests/auto/qbluetoothdevicediscoveryagent/tst_qbluetoothdevicediscoveryagent.cpp217
-rw-r--r--tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp18
3 files changed, 227 insertions, 12 deletions
diff --git a/tests/auto/qbluetoothdevicediscoveryagent/qbluetoothdevicediscoveryagent.pro b/tests/auto/qbluetoothdevicediscoveryagent/qbluetoothdevicediscoveryagent.pro
index 36c88cdc..e012ae52 100644
--- a/tests/auto/qbluetoothdevicediscoveryagent/qbluetoothdevicediscoveryagent.pro
+++ b/tests/auto/qbluetoothdevicediscoveryagent/qbluetoothdevicediscoveryagent.pro
@@ -4,3 +4,7 @@ CONFIG += testcase
QT = core concurrent bluetooth testlib
osx:QT += widgets
+
+config_bluez:qtHaveModule(dbus) {
+ DEFINES += QT_BLUEZ_BLUETOOTH
+}
diff --git a/tests/auto/qbluetoothdevicediscoveryagent/tst_qbluetoothdevicediscoveryagent.cpp b/tests/auto/qbluetoothdevicediscoveryagent/tst_qbluetoothdevicediscoveryagent.cpp
index 192173c8..efc4d8a6 100644
--- a/tests/auto/qbluetoothdevicediscoveryagent/tst_qbluetoothdevicediscoveryagent.cpp
+++ b/tests/auto/qbluetoothdevicediscoveryagent/tst_qbluetoothdevicediscoveryagent.cpp
@@ -77,8 +77,13 @@ private slots:
void tst_deviceDiscovery_data();
void tst_deviceDiscovery();
+
+ void tst_discoveryTimeout();
+
+ void tst_discoveryMethods();
private:
int noOfLocalDevices;
+ bool isBluez5Runtime = false;
};
tst_QBluetoothDeviceDiscoveryAgent::tst_QBluetoothDeviceDiscoveryAgent()
@@ -91,12 +96,59 @@ tst_QBluetoothDeviceDiscoveryAgent::~tst_QBluetoothDeviceDiscoveryAgent()
{
}
+#ifdef QT_BLUEZ_BLUETOOTH
+// This section was adopted from tst_qloggingcategory.cpp
+QString logMessage;
+
+QByteArray qMyMessageFormatString(QtMsgType type, const QMessageLogContext &context,
+ const QString &str)
+{
+ QByteArray message;
+ message.append(context.category);
+ switch (type) {
+ case QtDebugMsg: message.append(".debug"); break;
+ case QtInfoMsg: message.append(".info"); break;
+ case QtWarningMsg: message.append(".warning"); break;
+ case QtCriticalMsg:message.append(".critical"); break;
+ case QtFatalMsg: message.append(".fatal"); break;
+ }
+ message.append(": ");
+ message.append(qPrintable(str));
+
+ return message.simplified();
+}
+
+static void myCustomMessageHandler(QtMsgType type,
+ const QMessageLogContext &context,
+ const QString &msg)
+{
+ logMessage = qMyMessageFormatString(type, context, msg);
+}
+#endif
+
+
+
void tst_QBluetoothDeviceDiscoveryAgent::initTestCase()
{
qRegisterMetaType<QBluetoothDeviceInfo>();
qRegisterMetaType<QBluetoothDeviceDiscoveryAgent::InquiryType>();
+#ifdef QT_BLUEZ_BLUETOOTH
+ // To distinguish Bluez 4 and 5 we peek into the debug output
+ // of first Bluetooth ctor. It executes a runtime test and prints the result
+ // as logging output. This avoids more complex runtime detection logic within this unit test.
+ QtMessageHandler oldMessageHandler;
+ oldMessageHandler = qInstallMessageHandler(myCustomMessageHandler);
+
+ noOfLocalDevices = QBluetoothLocalDevice::allDevices().count();
+ qInstallMessageHandler(oldMessageHandler);
+ isBluez5Runtime = logMessage.contains(QStringLiteral("Bluez 5"));
+ if (isBluez5Runtime)
+ qDebug() << "BlueZ 5 runtime detected.";
+#else
noOfLocalDevices = QBluetoothLocalDevice::allDevices().count();
+#endif
+
if (!noOfLocalDevices)
return;
@@ -419,10 +471,10 @@ void tst_QBluetoothDeviceDiscoveryAgent::tst_deviceDiscovery()
}
}
}
-#ifdef Q_OS_IOS
- //On iOS, we do not have access to the local device/adapter, numberOfAdapters is 0,
+#if defined(Q_OS_IOS) || defined(Q_OS_WINRT)
+ //On iOS/WinRT, we do not have access to the local device/adapter, numberOfAdapters is 0,
//so we skip this test at all.
- QSKIP("iOS: no local Bluetooth device available. Skipping remaining part of test.");
+ QSKIP("iOS/WinRT: no local Bluetooth device available. Skipping remaining part of test.");
#endif
//For multiple Bluetooth adapter do the check only for GeneralUnlimitedInquiry.
@@ -431,6 +483,165 @@ void tst_QBluetoothDeviceDiscoveryAgent::tst_deviceDiscovery()
}
}
+
+void tst_QBluetoothDeviceDiscoveryAgent::tst_discoveryTimeout()
+{
+ QBluetoothDeviceDiscoveryAgent agent;
+
+ // check default values
+#if defined(Q_OS_OSX) || defined(Q_OS_IOS) || defined(Q_OS_ANDROID) || defined(Q_OS_WINRT)
+ QCOMPARE(agent.lowEnergyDiscoveryTimeout(), 25000);
+ agent.setLowEnergyDiscoveryTimeout(-1); // negative ignored
+ QCOMPARE(agent.lowEnergyDiscoveryTimeout(), 25000);
+ agent.setLowEnergyDiscoveryTimeout(20000);
+ QCOMPARE(agent.lowEnergyDiscoveryTimeout(), 20000);
+#elif defined(QT_BLUEZ_BLUETOOTH)
+ if (isBluez5Runtime) {
+ QCOMPARE(agent.lowEnergyDiscoveryTimeout(), 20000);
+ agent.setLowEnergyDiscoveryTimeout(-1); // negative ignored
+ QCOMPARE(agent.lowEnergyDiscoveryTimeout(), 20000);
+ agent.setLowEnergyDiscoveryTimeout(25000);
+ QCOMPARE(agent.lowEnergyDiscoveryTimeout(), 25000);
+ } else {
+ QCOMPARE(agent.lowEnergyDiscoveryTimeout(), -1);
+ agent.setLowEnergyDiscoveryTimeout(20000); // feature not supported -> ignored
+ QCOMPARE(agent.lowEnergyDiscoveryTimeout(), -1);
+ }
+#else
+ QCOMPARE(agent.lowEnergyDiscoveryTimeout(), -1);
+ agent.setLowEnergyDiscoveryTimeout(20000); // feature not supported -> ignored
+ QCOMPARE(agent.lowEnergyDiscoveryTimeout(), -1);
+#endif
+}
+
+void tst_QBluetoothDeviceDiscoveryAgent::tst_discoveryMethods()
+{
+ const QBluetoothLocalDevice localDevice;
+ if (localDevice.allDevices().size() != 1) {
+ // On iOS it returns 0 but we still have working BT.
+#ifndef Q_OS_IOS
+ QSKIP("This test expects exactly one local device working");
+#endif
+ }
+
+ const QBluetoothDeviceDiscoveryAgent::DiscoveryMethods
+ supportedMethods = QBluetoothDeviceDiscoveryAgent::supportedDiscoveryMethods();
+
+ QVERIFY(supportedMethods != QBluetoothDeviceDiscoveryAgent::NoMethod);
+
+ QBluetoothDeviceDiscoveryAgent::DiscoveryMethod
+ unsupportedMethods = QBluetoothDeviceDiscoveryAgent::NoMethod;
+ QBluetoothDeviceInfo::CoreConfiguration
+ expectedConfiguration = QBluetoothDeviceInfo::BaseRateAndLowEnergyCoreConfiguration;
+
+ if (supportedMethods == QBluetoothDeviceDiscoveryAgent::ClassicMethod) {
+ unsupportedMethods = QBluetoothDeviceDiscoveryAgent::LowEnergyMethod;
+ expectedConfiguration = QBluetoothDeviceInfo::BaseRateCoreConfiguration;
+ } else if (supportedMethods == QBluetoothDeviceDiscoveryAgent::LowEnergyMethod) {
+ unsupportedMethods = QBluetoothDeviceDiscoveryAgent::ClassicMethod;
+ expectedConfiguration = QBluetoothDeviceInfo::LowEnergyCoreConfiguration;
+ }
+
+ QBluetoothDeviceDiscoveryAgent agent;
+ QSignalSpy finishedSpy(&agent, SIGNAL(finished()));
+ QSignalSpy errorSpy(&agent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)));
+ QSignalSpy discoveredSpy(&agent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)));
+
+ // NoMethod - should just immediately return:
+ agent.start(QBluetoothDeviceDiscoveryAgent::NoMethod);
+ QCOMPARE(agent.error(), QBluetoothDeviceDiscoveryAgent::NoError);
+ QVERIFY(!agent.isActive());
+ QCOMPARE(finishedSpy.size(), 0);
+ QCOMPARE(errorSpy.size(), 0);
+ QCOMPARE(discoveredSpy.size(), 0);
+
+ if (unsupportedMethods != QBluetoothDeviceDiscoveryAgent::NoMethod) {
+ agent.start(unsupportedMethods);
+ QCOMPARE(agent.error(), QBluetoothDeviceDiscoveryAgent::UnsupportedDiscoveryMethod);
+ QVERIFY(!agent.isActive());
+ QVERIFY(finishedSpy.isEmpty());
+ QCOMPARE(errorSpy.size(), 1);
+ errorSpy.clear();
+ QVERIFY(discoveredSpy.isEmpty());
+ }
+
+ // Start discovery, probably both Classic and LE methods:
+ agent.start(supportedMethods);
+ QVERIFY(agent.isActive());
+ QVERIFY(errorSpy.isEmpty());
+
+
+#define RUN_DISCOVERY(maxTimeout, step, condition) \
+ for (int scanTime = maxTimeout; (condition) && scanTime > 0; scanTime -= step) \
+ QTest::qWait(step);
+
+ // Wait for up to MaxScanTime for the scan to finish
+ const int timeStep = 15000;
+ RUN_DISCOVERY(MaxScanTime, timeStep, finishedSpy.isEmpty())
+
+ QVERIFY(!agent.isActive());
+ QVERIFY(errorSpy.size() <= 1);
+
+ if (errorSpy.size()) {
+ // For example, old iOS device could report it supports LE method,
+ // but it actually does not.
+ QVERIFY(supportedMethods == QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
+ QCOMPARE(agent.error(), QBluetoothDeviceDiscoveryAgent::UnsupportedDiscoveryMethod);
+ } else {
+ QVERIFY(finishedSpy.count() == 1);
+ QVERIFY(agent.error() == QBluetoothDeviceDiscoveryAgent::NoError);
+ QVERIFY(agent.errorString().isEmpty());
+
+ while (!discoveredSpy.isEmpty()) {
+ const QBluetoothDeviceInfo info =
+ qvariant_cast<QBluetoothDeviceInfo>(discoveredSpy.takeFirst().at(0));
+ QVERIFY(info.isValid());
+ QVERIFY(info.coreConfigurations() & expectedConfiguration);
+ }
+ }
+
+ if (unsupportedMethods != QBluetoothDeviceDiscoveryAgent::NoMethod)
+ return;
+
+ // Both methods were reported as supported. We already tested them
+ // above, now let's test first Classic then LE.
+ finishedSpy.clear();
+ errorSpy.clear();
+ discoveredSpy.clear();
+
+ agent.start(QBluetoothDeviceDiscoveryAgent::ClassicMethod);
+ QVERIFY(agent.isActive());
+ QVERIFY(errorSpy.isEmpty());
+ QCOMPARE(agent.error(), QBluetoothDeviceDiscoveryAgent::NoError);
+
+ RUN_DISCOVERY(MaxScanTime, timeStep, finishedSpy.isEmpty())
+
+ QVERIFY(!agent.isActive());
+ QVERIFY(errorSpy.isEmpty());
+ QCOMPARE(finishedSpy.size(), 1);
+
+ finishedSpy.clear();
+ discoveredSpy.clear();
+
+ agent.start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
+ QVERIFY(agent.isActive());
+ QVERIFY(errorSpy.isEmpty());
+
+ RUN_DISCOVERY(MaxScanTime, timeStep, finishedSpy.isEmpty() && errorSpy.isEmpty())
+
+ QVERIFY(!agent.isActive());
+ QVERIFY(errorSpy.size() <= 1);
+
+ if (errorSpy.size()) {
+ QCOMPARE(agent.error(), QBluetoothDeviceDiscoveryAgent::UnsupportedDiscoveryMethod);
+ qDebug() << "QBluetoothDeviceDiscoveryAgent::supportedDiscoveryMethods is inaccurate"
+ " on your platform/with your device, LowEnergyMethod is not supported";
+ } else {
+ QCOMPARE(agent.error(), QBluetoothDeviceDiscoveryAgent::NoError);
+ QCOMPARE(finishedSpy.size(), 1);
+ }
+}
+
QTEST_MAIN(tst_QBluetoothDeviceDiscoveryAgent)
#include "tst_qbluetoothdevicediscoveryagent.moc"
diff --git a/tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp b/tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp
index 773c673b..4acfe4d1 100644
--- a/tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp
+++ b/tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp
@@ -120,7 +120,7 @@ tst_QLowEnergyController::~tst_QLowEnergyController()
void tst_QLowEnergyController::initTestCase()
{
-#ifndef Q_OS_MAC
+#if !defined(Q_OS_MAC) && !defined(Q_OS_WINRT)
if (remoteDevice.isNull()
|| QBluetoothLocalDevice::allDevices().isEmpty()) {
qWarning("No remote device or local adapter found.");
@@ -244,7 +244,7 @@ void tst_QLowEnergyController::tst_connect()
{
QList<QBluetoothHostInfo> localAdapters = QBluetoothLocalDevice::allDevices();
-#if defined(Q_OS_IOS) || defined(Q_OS_TVOS)
+#if defined(Q_OS_IOS) || defined(Q_OS_TVOS) || defined(Q_OS_WINRT)
if (!remoteDeviceInfo.isValid())
#else
if (localAdapters.isEmpty() || !remoteDeviceInfo.isValid())
@@ -260,7 +260,7 @@ void tst_QLowEnergyController::tst_connect()
else
QCOMPARE(control.remoteName(), remoteDeviceInfo.name());
-#if !defined(Q_OS_IOS) && !defined(Q_OS_TVOS)
+#if !defined(Q_OS_IOS) && !defined(Q_OS_TVOS) && !defined(Q_OS_WINRT)
const QBluetoothAddress localAdapter = localAdapters.at(0).address();
QCOMPARE(control.localAddress(), localAdapter);
QVERIFY(!control.localAddress().isNull());
@@ -403,7 +403,7 @@ void tst_QLowEnergyController::tst_connect()
void tst_QLowEnergyController::tst_concurrentDiscovery()
{
-#ifndef Q_OS_MAC
+#if !defined(Q_OS_MAC) && !defined(Q_OS_WINRT)
QList<QBluetoothHostInfo> localAdapters = QBluetoothLocalDevice::allDevices();
if (localAdapters.isEmpty())
QSKIP("No local Bluetooth device found. Skipping test.");
@@ -440,7 +440,7 @@ void tst_QLowEnergyController::tst_concurrentDiscovery()
30000);
}
-#ifdef Q_OS_ANDROID
+#if defined(Q_OS_ANDROID) || defined(Q_OS_WINRT)
QCOMPARE(control.state(), QLowEnergyController::ConnectedState);
QCOMPARE(control2.state(), QLowEnergyController::ConnectedState);
control2.disconnectFromDevice();
@@ -1642,7 +1642,7 @@ void tst_QLowEnergyController::tst_defaultBehavior()
void tst_QLowEnergyController::tst_writeCharacteristic()
{
-#ifndef Q_OS_MAC
+#if !defined(Q_OS_MAC) && !defined(Q_OS_WINRT)
QList<QBluetoothHostInfo> localAdapters = QBluetoothLocalDevice::allDevices();
if (localAdapters.isEmpty())
QSKIP("No local Bluetooth device found. Skipping test.");
@@ -1816,7 +1816,7 @@ void tst_QLowEnergyController::tst_writeCharacteristic()
void tst_QLowEnergyController::tst_readWriteDescriptor()
{
-#ifndef Q_OS_MAC
+#if !defined(Q_OS_MAC) && !defined(Q_OS_WINRT)
QList<QBluetoothHostInfo> localAdapters = QBluetoothLocalDevice::allDevices();
if (localAdapters.isEmpty())
QSKIP("No local Bluetooth device found. Skipping test.");
@@ -2239,7 +2239,7 @@ void tst_QLowEnergyController::tst_customProgrammableDevice()
*/
void tst_QLowEnergyController::tst_errorCases()
{
-#ifndef Q_OS_MAC
+#if !defined(Q_OS_MAC) && !defined(Q_OS_WINRT)
QList<QBluetoothHostInfo> localAdapters = QBluetoothLocalDevice::allDevices();
if (localAdapters.isEmpty())
QSKIP("No local Bluetooth device found. Skipping test.");
@@ -2461,7 +2461,7 @@ void tst_QLowEnergyController::tst_errorCases()
*/
void tst_QLowEnergyController::tst_writeCharacteristicNoResponse()
{
-#ifndef Q_OS_MAC
+#if !defined(Q_OS_MAC) && !defined(Q_OS_WINRT)
QList<QBluetoothHostInfo> localAdapters = QBluetoothLocalDevice::allDevices();
if (localAdapters.isEmpty())
QSKIP("No local Bluetooth device found. Skipping test.");