diff options
author | Alex Blasche <alexander.blasche@digia.com> | 2014-03-14 11:16:27 +0100 |
---|---|---|
committer | Alex Blasche <alexander.blasche@digia.com> | 2014-03-14 11:16:27 +0100 |
commit | 83cd348bde8e856d2508c2514cd33a90e593461d (patch) | |
tree | 9dbd933129ef4a9e514218fc6177d775aa94c9db /tests/auto | |
parent | 4439541b05ae88a30159a5597d40f569328783ca (diff) | |
parent | 40ac14e13b0aa07124c6ae9ef8eacfe6d8550684 (diff) |
Merge branch 'dev' into btle
Conflicts:
src/bluetooth/qbluetoothuuid.cpp
tests/bttestui/btlocaldevice.cpp
Change-Id: Id16ab93ec61361950669da7eb232769c5dc4644f
Diffstat (limited to 'tests/auto')
20 files changed, 407 insertions, 1575 deletions
diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 10798a1b..5a703944 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -16,8 +16,7 @@ qtHaveModule(bluetooth) { qbluetoothtransfermanager \ qbluetoothtransferrequest \ qbluetoothuuid \ - ql2capserver \ - qrfcommserver + qbluetoothserver } qtHaveModule(nfc) { diff --git a/tests/auto/qbluetoothdevicediscoveryagent/tst_qbluetoothdevicediscoveryagent.cpp b/tests/auto/qbluetoothdevicediscoveryagent/tst_qbluetoothdevicediscoveryagent.cpp index d20458fd..2da1a8a9 100644 --- a/tests/auto/qbluetoothdevicediscoveryagent/tst_qbluetoothdevicediscoveryagent.cpp +++ b/tests/auto/qbluetoothdevicediscoveryagent/tst_qbluetoothdevicediscoveryagent.cpp @@ -347,10 +347,10 @@ void tst_QBluetoothDeviceDiscoveryAgent::tst_deviceDiscovery() QSignalSpy finishedSpy(&discoveryAgent, SIGNAL(finished())); QSignalSpy errorSpy(&discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error))); - QSignalSpy discoveredSpy(&discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&))); + QSignalSpy discoveredSpy(&discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo))); // connect(&discoveryAgent, SIGNAL(finished()), this, SLOT(finished())); -// connect(&discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)), -// this, SLOT(deviceDiscoveryDebug(const QBluetoothDeviceInfo&))); +// connect(&discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), +// this, SLOT(deviceDiscoveryDebug(QBluetoothDeviceInfo))); discoveryAgent.setInquiryType(inquiryType); discoveryAgent.start(); diff --git a/tests/auto/qbluetoothhostinfo/tst_qbluetoothhostinfo.cpp b/tests/auto/qbluetoothhostinfo/tst_qbluetoothhostinfo.cpp index aa2448a8..be2cadbf 100644 --- a/tests/auto/qbluetoothhostinfo/tst_qbluetoothhostinfo.cpp +++ b/tests/auto/qbluetoothhostinfo/tst_qbluetoothhostinfo.cpp @@ -65,6 +65,8 @@ private slots: void tst_construction_data(); void tst_construction(); + + void tst_copy(); }; tst_QBluetoothHostInfo::tst_QBluetoothHostInfo() @@ -171,6 +173,22 @@ void tst_QBluetoothHostInfo::tst_construction() QCOMPARE(setter.address().isNull(), !validBtAddress); } +void tst_QBluetoothHostInfo::tst_copy() +{ + QBluetoothHostInfo original; + original.setAddress(QBluetoothAddress("11:22:33:44:55:66")); + original.setName(QStringLiteral("FunkyName")); + + QBluetoothHostInfo assignConstructor(original); + QCOMPARE(assignConstructor.name(), original.name()); + QCOMPARE(assignConstructor.address(), original.address()); + + QBluetoothHostInfo assignOperator; + assignOperator = original; + QCOMPARE(assignOperator.name(), original.name()); + QCOMPARE(assignOperator.address(), original.address()); +} + QTEST_MAIN(tst_QBluetoothHostInfo) #include "tst_qbluetoothhostinfo.moc" diff --git a/tests/auto/qbluetoothlocaldevice/tst_qbluetoothlocaldevice.cpp b/tests/auto/qbluetoothlocaldevice/tst_qbluetoothlocaldevice.cpp index abe19c0b..3285592e 100644 --- a/tests/auto/qbluetoothlocaldevice/tst_qbluetoothlocaldevice.cpp +++ b/tests/auto/qbluetoothlocaldevice/tst_qbluetoothlocaldevice.cpp @@ -337,7 +337,7 @@ void tst_QBluetoothLocalDevice::tst_pairDevice() localDevice.powerOn(); QVERIFY(localDevice.hostMode() != QBluetoothLocalDevice::HostPoweredOff); - QSignalSpy pairingSpy(&localDevice, SIGNAL(pairingFinished(const QBluetoothAddress &,QBluetoothLocalDevice::Pairing)) ); + QSignalSpy pairingSpy(&localDevice, SIGNAL(pairingFinished(QBluetoothAddress,QBluetoothLocalDevice::Pairing)) ); QSignalSpy errorSpy(&localDevice, SIGNAL(error(QBluetoothLocalDevice::Error))); // there should be no signals yet QVERIFY(pairingSpy.isValid()); diff --git a/tests/auto/qrfcommserver/qrfcommserver.pro b/tests/auto/qbluetoothserver/qbluetoothserver.pro index b6472ca4..4b5e5d46 100644 --- a/tests/auto/qrfcommserver/qrfcommserver.pro +++ b/tests/auto/qbluetoothserver/qbluetoothserver.pro @@ -1,5 +1,5 @@ -SOURCES += tst_qrfcommserver.cpp -TARGET = tst_qrfcommserver +SOURCES += tst_qbluetoothserver.cpp +TARGET = tst_qbluetoothserver CONFIG += testcase QT = core concurrent bluetooth testlib @@ -7,5 +7,4 @@ QT = core concurrent bluetooth testlib OTHER_FILES += \ README.txt -CONFIG += insignificant_test # QTBUG-22017 DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 diff --git a/tests/auto/qbluetoothserver/tst_qbluetoothserver.cpp b/tests/auto/qbluetoothserver/tst_qbluetoothserver.cpp new file mode 100644 index 00000000..347cddce --- /dev/null +++ b/tests/auto/qbluetoothserver/tst_qbluetoothserver.cpp @@ -0,0 +1,240 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> + +#include <QDebug> + +#include <qbluetoothserver.h> +#include <qbluetoothsocket.h> +#include <qbluetoothlocaldevice.h> + +QT_USE_NAMESPACE + +//same uuid as tests/bttestui +#define TEST_SERVICE_UUID "e8e10f95-1a70-4b27-9ccf-02010264e9c8" + +Q_DECLARE_METATYPE(QBluetooth::SecurityFlags) +Q_DECLARE_METATYPE(QBluetoothServer::Error) + +// Max time to wait for connection +static const int MaxConnectTime = 60 * 1000; // 1 minute in ms + +class tst_QBluetoothServer : public QObject +{ + Q_OBJECT + +public: + tst_QBluetoothServer(); + ~tst_QBluetoothServer(); + +private slots: + void initTestCase(); + void cleanupTestCase(); + + void tst_construction(); + + void tst_receive_data(); + void tst_receive(); + + void setHostMode(const QBluetoothAddress &localAdapter, QBluetoothLocalDevice::HostMode newHostMode); + +private: + QBluetoothLocalDevice localDevice; + QBluetoothLocalDevice::HostMode initialHostMode; +}; + +tst_QBluetoothServer::tst_QBluetoothServer() +{ +} + +tst_QBluetoothServer::~tst_QBluetoothServer() +{ +} + +void tst_QBluetoothServer::setHostMode(const QBluetoothAddress &localAdapter, + QBluetoothLocalDevice::HostMode newHostMode) +{ + QBluetoothLocalDevice device(localAdapter); + QSignalSpy hostModeSpy(&device, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode))); + + if (!device.isValid()) + return; + + if (device.hostMode() == newHostMode) + return; + + //We distinguish powerOn() and setHostMode(HostConnectable) because + //Android uses different permission levels, we want to avoid interaction with user + //which implies usage of powerOn -> unfortunately powerOn() is not equivalent to HostConnectable + //Unfortunately the discoverable mode always requires a user confirmation + switch (newHostMode) { + case QBluetoothLocalDevice::HostPoweredOff: + case QBluetoothLocalDevice::HostDiscoverable: + case QBluetoothLocalDevice::HostDiscoverableLimitedInquiry: + device.setHostMode(newHostMode); + break; + case QBluetoothLocalDevice::HostConnectable: + device.powerOn(); + break; + } + + int connectTime = 5000; // ms + while (hostModeSpy.count() < 1 && connectTime > 0) { + QTest::qWait(500); + connectTime -= 500; + } +} + +void tst_QBluetoothServer::initTestCase() +{ + qRegisterMetaType<QBluetooth::SecurityFlags>("QBluetooth::SecurityFlags"); + qRegisterMetaType<QBluetoothServer::Error>("QBluetoothServer::Error"); + + QBluetoothLocalDevice device; + if (!device.isValid()) + return; + + initialHostMode = device.hostMode(); + + setHostMode(device.address(), QBluetoothLocalDevice::HostConnectable); + + QBluetoothLocalDevice::HostMode hostMode= localDevice.hostMode(); + + QVERIFY(hostMode != QBluetoothLocalDevice::HostPoweredOff); +} + +void tst_QBluetoothServer::cleanupTestCase() +{ + QBluetoothLocalDevice device; + setHostMode(device.address(), initialHostMode); +} + +void tst_QBluetoothServer::tst_construction() +{ + { + QBluetoothServer server(QBluetoothServiceInfo::RfcommProtocol); + + QVERIFY(!server.isListening()); + QCOMPARE(server.maxPendingConnections(), 1); + QVERIFY(!server.hasPendingConnections()); + QVERIFY(server.nextPendingConnection() == 0); + QCOMPARE(server.error(), QBluetoothServer::NoError); + QCOMPARE(server.serverType(), QBluetoothServiceInfo::RfcommProtocol); + } + + { + QBluetoothServer server(QBluetoothServiceInfo::L2capProtocol); + + QVERIFY(!server.isListening()); + QCOMPARE(server.maxPendingConnections(), 1); + QVERIFY(!server.hasPendingConnections()); + QVERIFY(server.nextPendingConnection() == 0); + QCOMPARE(server.error(), QBluetoothServer::NoError); + QCOMPARE(server.serverType(), QBluetoothServiceInfo::L2capProtocol); + } +} + +void tst_QBluetoothServer::tst_receive_data() +{ + QTest::addColumn<QBluetoothLocalDevice::HostMode>("hostmode"); + QTest::newRow("offline mode") << QBluetoothLocalDevice::HostPoweredOff; + QTest::newRow("online mode") << QBluetoothLocalDevice::HostConnectable; +} + +void tst_QBluetoothServer::tst_receive() +{ + QFETCH(QBluetoothLocalDevice::HostMode, hostmode); + + QBluetoothLocalDevice localDev; + const QBluetoothAddress address = localDev.address(); + + bool localDeviceAvailable = localDev.isValid(); + + if (localDeviceAvailable) { + setHostMode(address, hostmode); + + if (hostmode == QBluetoothLocalDevice::HostPoweredOff) + QCOMPARE(localDevice.hostMode(), hostmode); + else + QVERIFY(localDevice.hostMode() != QBluetoothLocalDevice::HostPoweredOff); + } + QBluetoothServer server(QBluetoothServiceInfo::RfcommProtocol); + QSignalSpy errorSpy(&server, SIGNAL(error(QBluetoothServer::Error))); + + bool result = server.listen(address, 20); // port == 20 + QTest::qWait(1000); + + if (!result) { + QCOMPARE(server.serverAddress(), QBluetoothAddress()); + QCOMPARE(server.serverPort(), quint16(0)); + QVERIFY(errorSpy.count() > 0); + QVERIFY(!server.isListening()); + if (!localDeviceAvailable) { + QVERIFY(server.error() != QBluetoothServer::NoError); + } else { + //local device but poweredOff + QCOMPARE(server.error(), QBluetoothServer::PoweredOffError); + } + return; + } + + QVERIFY(result); + + QVERIFY(QBluetoothLocalDevice::allDevices().count()); + QCOMPARE(server.error(), QBluetoothServer::NoError); + QCOMPARE(server.serverAddress(), address); + QCOMPARE(server.serverPort(), quint16(20)); + QVERIFY(server.isListening()); + QVERIFY(!server.hasPendingConnections()); + + server.close(); + QCOMPARE(server.error(), QBluetoothServer::NoError); + QVERIFY(server.serverAddress() == address || server.serverAddress() == QBluetoothAddress()); + QVERIFY(server.serverPort() == 0); + QVERIFY(!server.isListening()); + QVERIFY(!server.hasPendingConnections()); +} + + +QTEST_MAIN(tst_QBluetoothServer) + +#include "tst_qbluetoothserver.moc" diff --git a/tests/auto/qbluetoothservicediscoveryagent/tst_qbluetoothservicediscoveryagent.cpp b/tests/auto/qbluetoothservicediscoveryagent/tst_qbluetoothservicediscoveryagent.cpp index 2355f4d5..d7a6a6ef 100644 --- a/tests/auto/qbluetoothservicediscoveryagent/tst_qbluetoothservicediscoveryagent.cpp +++ b/tests/auto/qbluetoothservicediscoveryagent/tst_qbluetoothservicediscoveryagent.cpp @@ -134,9 +134,9 @@ void tst_QBluetoothServiceDiscoveryAgent::initTestCase() QSignalSpy finishedSpy(&discoveryAgent, SIGNAL(finished())); QSignalSpy errorSpy(&discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error))); - QSignalSpy discoveredSpy(&discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&))); - // connect(&discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)), - // this, SLOT(deviceDiscoveryDebug(const QBluetoothDeviceInfo&))); + QSignalSpy discoveredSpy(&discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo))); + // connect(&discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), + // this, SLOT(deviceDiscoveryDebug(QBluetoothDeviceInfo))); discoveryAgent.start(); diff --git a/tests/auto/qbluetoothsocket/README.txt b/tests/auto/qbluetoothsocket/README.txt index 1e92712d..d04324d2 100644 --- a/tests/auto/qbluetoothsocket/README.txt +++ b/tests/auto/qbluetoothsocket/README.txt @@ -1,14 +1,7 @@ +This test requires bttestui running on a remote machine, and for it to +be discoverable and connectable to the device under test. The bttestui +should run an rfcomm server (see UI option SListenUuid). -This test requires btclient to running on a linux machine, and for it to -be discoverable and connectable to the device under test. - -btclient is available in tests/btclient. It requires a linux machine -with bluez 4 installed. It does not depend on Qt. - -The unit test attempts to use service discovery to locate btclient. For quick -testing this can be time consuming. It will also check the environment variable -TESTSERVER for the mac address bluetooth adaptor of the system running -btclient. You can use hciconfig on the linux system to find it's mac address. -If TESTSERVER is set and btclient is not running the test will fail. - +bttestui is available in tests/bttestui. +The unit test attempts to use service discovery to locate bttestui. diff --git a/tests/auto/qbluetoothsocket/qbluetoothsocket.pro b/tests/auto/qbluetoothsocket/qbluetoothsocket.pro index 6498c97a..34f6e527 100644 --- a/tests/auto/qbluetoothsocket/qbluetoothsocket.pro +++ b/tests/auto/qbluetoothsocket/qbluetoothsocket.pro @@ -5,10 +5,7 @@ testcase.timeout = 250 # this test is slow QT = core concurrent network bluetooth testlib -INCLUDEPATH += ../../../tests/btclient - OTHER_FILES += \ README.txt -CONFIG += insignificant_test # QTBUG-22017 DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 diff --git a/tests/auto/qbluetoothsocket/tst_qbluetoothsocket.cpp b/tests/auto/qbluetoothsocket/tst_qbluetoothsocket.cpp index e9ad44c1..766bbb48 100644 --- a/tests/auto/qbluetoothsocket/tst_qbluetoothsocket.cpp +++ b/tests/auto/qbluetoothsocket/tst_qbluetoothsocket.cpp @@ -43,23 +43,20 @@ #include <QDebug> -#include <QProcessEnvironment> - #include <qbluetoothsocket.h> #include <qbluetoothdeviceinfo.h> #include <qbluetoothserviceinfo.h> #include <qbluetoothservicediscoveryagent.h> #include <qbluetoothlocaldevice.h> -#include <btclient.h> - QT_USE_NAMESPACE Q_DECLARE_METATYPE(QBluetoothSocket::SocketState) +Q_DECLARE_METATYPE(QBluetoothSocket::SocketError) Q_DECLARE_METATYPE(QBluetoothServiceInfo::Protocol) -//#define BTADDRESS "00:1A:9F:92:9E:5A" -char BTADDRESS[] = "00:00:00:00:00:00"; +//same uuid as tests/bttestui +#define TEST_SERVICE_UUID "e8e10f95-1a70-4b27-9ccf-02010264e9c8" // Max time to wait for connection @@ -87,18 +84,11 @@ private slots: void tst_construction_data(); void tst_construction(); - void tst_clientConnection_data(); - void tst_clientConnection(); - - void tst_serviceConnection_data(); void tst_serviceConnection(); void tst_clientCommunication_data(); void tst_clientCommunication(); - void tst_localPeer_data(); - void tst_localPeer(); - void tst_error(); public slots: @@ -107,6 +97,10 @@ public slots: void error(QBluetoothServiceDiscoveryAgent::Error error); private: bool done_discovery; + bool localDeviceFound; + QBluetoothDeviceInfo remoteDevice; + QBluetoothHostInfo localDevice; + QBluetoothServiceInfo remoteServiceInfo; }; @@ -116,6 +110,12 @@ tst_QBluetoothSocket::tst_QBluetoothSocket() { qRegisterMetaType<QBluetoothSocket::SocketState>("QBluetoothSocket::SocketState"); qRegisterMetaType<QBluetoothSocket::SocketError>("QBluetoothSocket::SocketError"); + + localDeviceFound = false; // true if we have a local adapter + done_discovery = false; //true if we found remote device + + //Enable logging to facilitate debugging in case of error + QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")); } tst_QBluetoothSocket::~tst_QBluetoothSocket() @@ -124,54 +124,56 @@ tst_QBluetoothSocket::~tst_QBluetoothSocket() void tst_QBluetoothSocket::initTestCase() { - // start Bluetooth if not started - if (!QBluetoothLocalDevice::allDevices().count()) - QSKIP("Skipping test due to missing Bluetooth device"); - - QBluetoothLocalDevice *device = new QBluetoothLocalDevice(); - device->powerOn(); - delete device; - - QProcessEnvironment pe = QProcessEnvironment::systemEnvironment(); - QLatin1String t("TESTSERVER"); - if (pe.contains(t)){ - qDebug() << pe.value(t); - strcpy(BTADDRESS, pe.value(t).toLatin1()); + // setup Bluetooth env + const QList<QBluetoothHostInfo> foundDevices = QBluetoothLocalDevice::allDevices(); + if (!foundDevices.count()) { + qWarning() << "Missing local device"; + return; + } else { + localDevice = foundDevices.at(0); + qDebug() << "Local device" << localDevice.name() << localDevice.address().toString(); } - if (QBluetoothAddress(BTADDRESS).isNull()){ - // Go find an echo server for BTADDRESS - QBluetoothServiceDiscoveryAgent *sda = new QBluetoothServiceDiscoveryAgent(this); - connect(sda, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), this, SLOT(serviceDiscovered(QBluetoothServiceInfo))); - connect(sda, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)), this, SLOT(error(QBluetoothServiceDiscoveryAgent::Error))); - connect(sda, SIGNAL(finished()), this, SLOT(finished())); + localDeviceFound = true; + //start the device + QBluetoothLocalDevice device(localDevice.address()); + device.powerOn(); - qDebug() << "Starting discovery"; - done_discovery = false; - memset(BTADDRESS, 0, 18); - sda->setUuidFilter(QBluetoothUuid(QString(ECHO_SERVICE_UUID))); - sda->start(QBluetoothServiceDiscoveryAgent::MinimalDiscovery); + //find the remote test server + //the remote test server is tests/bttestui - for (int connectTime = MaxConnectTime; !done_discovery && connectTime > 0; connectTime -= 1000) - QTest::qWait(1000); + // Go find the server + QBluetoothServiceDiscoveryAgent *sda = new QBluetoothServiceDiscoveryAgent(this); + connect(sda, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), this, SLOT(serviceDiscovered(QBluetoothServiceInfo))); + connect(sda, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)), this, SLOT(error(QBluetoothServiceDiscoveryAgent::Error))); + connect(sda, SIGNAL(finished()), this, SLOT(finished())); - sda->stop(); + qDebug() << "Starting discovery"; - if (QBluetoothAddress(BTADDRESS).isNull()){ - QFAIL("Unable to find test service"); - } - delete sda; - sda = 0x0; + sda->setUuidFilter(QBluetoothUuid(QString(TEST_SERVICE_UUID))); + sda->start(QBluetoothServiceDiscoveryAgent::MinimalDiscovery); + + for (int connectTime = MaxConnectTime; !done_discovery && connectTime > 0; connectTime -= 1000) + QTest::qWait(1000); + + sda->stop(); + + if (!remoteDevice.isValid()) { + qWarning() << "#########################"; + qWarning() << "Unable to find test service"; + qWarning() << "Remote device may have to be changed into Discoverable mode"; + qWarning() << "#########################"; } + delete sda; } void tst_QBluetoothSocket::error(QBluetoothServiceDiscoveryAgent::Error error) { qDebug() << "Received error" << error; -// done_discovery = true; + done_discovery = true; } void tst_QBluetoothSocket::finished() @@ -182,8 +184,9 @@ void tst_QBluetoothSocket::finished() void tst_QBluetoothSocket::serviceDiscovered(const QBluetoothServiceInfo &info) { - qDebug() << "Found: " << info.device().name() << info.serviceUuid(); - strcpy(BTADDRESS, info.device().address().toString().toLatin1()); + qDebug() << "Found test service on:" << info.device().name() << info.device().address().toString(); + remoteDevice = info.device(); + remoteServiceInfo = info; done_discovery = true; } @@ -204,234 +207,68 @@ void tst_QBluetoothSocket::tst_construction() QBluetoothSocket socket; QCOMPARE(socket.socketType(), QBluetoothServiceInfo::UnknownProtocol); + QCOMPARE(socket.error(), QBluetoothSocket::NoSocketError); + QCOMPARE(socket.errorString(), QString()); + QCOMPARE(socket.peerAddress(), QBluetoothAddress()); + QCOMPARE(socket.peerName(), QString()); + QCOMPARE(socket.peerPort(), quint16(0)); + QCOMPARE(socket.state(), QBluetoothSocket::UnconnectedState); + QCOMPARE(socket.socketDescriptor(), -1); + QCOMPARE(socket.bytesAvailable(), 0); + QCOMPARE(socket.bytesToWrite(), 0); + QCOMPARE(socket.canReadLine(), false); + QCOMPARE(socket.isSequential(), true); + QCOMPARE(socket.atEnd(), true); + QCOMPARE(socket.pos(), 0); + QCOMPARE(socket.size(), 0); + + QSignalSpy spy(&socket, SIGNAL(error(QBluetoothSocket::SocketError))); + QCOMPARE(spy.count(), 0); + + QByteArray array = socket.readAll(); + QVERIFY(array.isEmpty()); + QCOMPARE(socket.error(), QBluetoothSocket::OperationError); + QCOMPARE(spy.count(), 1); + + char buffer[10]; + int returnValue = socket.read((char*)&buffer, 10); + QCOMPARE(returnValue, -1); + QCOMPARE(socket.error(), QBluetoothSocket::OperationError); + QVERIFY(spy.count() >= 2); + while (spy.count()) { + QBluetoothSocket::SocketError er = spy.takeFirst().at(0).value<QBluetoothSocket::SocketError>(); + QVERIFY(er == QBluetoothSocket::OperationError); + } } { QBluetoothSocket socket(socketType); - QCOMPARE(socket.socketType(), socketType); } } -void tst_QBluetoothSocket::tst_clientConnection_data() -{ - QTest::addColumn<QBluetoothServiceInfo::Protocol>("socketprotocol"); - QTest::addColumn<ClientConnectionShutdown>("shutdown"); - QTest::addColumn<QBluetoothAddress>("address"); - QTest::addColumn<quint16>("port"); - QTest::addColumn<QByteArray>("data"); - - //QBluetoothAddress address("00:1E:3A:81:BA:69"); - QBluetoothAddress address(BTADDRESS); - quint16 port = 10; - - QTest::newRow("unavailable, error") << QBluetoothServiceInfo::RfcommProtocol - << Error << QBluetoothAddress("112233445566") << quint16(10) << QByteArray(); - - QTest::newRow("available, disconnect") << QBluetoothServiceInfo::RfcommProtocol - << Disconnect << address << port << QByteArray(); - QTest::newRow("available, disconnect with data") << QBluetoothServiceInfo::RfcommProtocol - << Disconnect << address << port << QByteArray("Test message\n"); - QTest::newRow("available, close") << QBluetoothServiceInfo::RfcommProtocol - << Close << address << port << QByteArray(); - QTest::newRow("available, abort") << QBluetoothServiceInfo::RfcommProtocol - << Abort << address << port << QByteArray(); - QTest::newRow("available, abort with data") << QBluetoothServiceInfo::RfcommProtocol - << Abort << address << port << QByteArray("Test message\n"); - - - port = 0x1011; - QTest::newRow("unavailable, error") << QBluetoothServiceInfo::L2capProtocol - << Error << QBluetoothAddress("112233445566") << quint16(10) << QByteArray(); - - QTest::newRow("available, disconnect") << QBluetoothServiceInfo::L2capProtocol - << Disconnect << address << port << QByteArray(); - QTest::newRow("available, disconnect with data") << QBluetoothServiceInfo::L2capProtocol - << Disconnect << address << port << QByteArray("Test message\n"); - QTest::newRow("available, close") << QBluetoothServiceInfo::L2capProtocol - << Close << address << port << QByteArray(); - QTest::newRow("available, abort") << QBluetoothServiceInfo::L2capProtocol - << Abort << address << port << QByteArray(); - QTest::newRow("available, abort with data") << QBluetoothServiceInfo::L2capProtocol - << Abort << address << port << QByteArray("Test message\n"); - -} - -void tst_QBluetoothSocket::tst_clientConnection() -{ - QFETCH(QBluetoothServiceInfo::Protocol, socketprotocol); - QFETCH(ClientConnectionShutdown, shutdown); - QFETCH(QBluetoothAddress, address); - QFETCH(quint16, port); - QFETCH(QByteArray, data); - int loop = 5; - - tryagain: - /* Construction */ - QBluetoothSocket *socket = new QBluetoothSocket(socketprotocol); - - QSignalSpy stateSpy(socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState))); - - QCOMPARE(socket->socketType(), socketprotocol); - QCOMPARE(socket->state(), QBluetoothSocket::UnconnectedState); - - /* Connection */ - QSignalSpy connectedSpy(socket, SIGNAL(connected())); - QSignalSpy errorSpy(socket, SIGNAL(error(QBluetoothSocket::SocketError))); - - socket->connectToService(address, port); - - QCOMPARE(stateSpy.count(), 1); - QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::ConnectingState); - QCOMPARE(socket->state(), QBluetoothSocket::ConnectingState); - - stateSpy.clear(); - - int connectTime = MaxConnectTime; - while (connectedSpy.count() == 0 && errorSpy.count() == 0 && connectTime > 0) { - QTest::qWait(1000); - connectTime -= 1000; - } - - if (shutdown == Error) { - QCOMPARE(connectedSpy.count(), 0); - QCOMPARE(errorSpy.count(), 1); - QCOMPARE(stateSpy.count(), 1); - QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::UnconnectedState); - QCOMPARE(socket->state(), QBluetoothSocket::UnconnectedState); - // The remote service needs time to close the connection and resume listening - QTest::qSleep(100); - return; - } else { - if (errorSpy.count() != 0) { - qDebug() << errorSpy.takeFirst().at(0).toInt(); - if (loop--) - goto tryagain; - QSKIP("Connection error"); - } - QCOMPARE(connectedSpy.count(), 1); - QCOMPARE(stateSpy.count(), 1); - QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::ConnectedState); - QCOMPARE(socket->state(), QBluetoothSocket::ConnectedState); - } - QVERIFY(shutdown != Error); - - stateSpy.clear(); - - /* Read / Write */ - QSignalSpy bytesWrittenSpy(socket, SIGNAL(bytesWritten(qint64))); - - if (!data.isEmpty()) { - // Write data but don't flush. - socket->write(data); - } - - /* Disconnection */ - QSignalSpy disconnectedSpy(socket, SIGNAL(disconnected())); - - if (shutdown == Abort) { - socket->abort(); - -// TODO: no buffereing, all data is sent on write - if (!data.isEmpty()) { - // Check that pending write did not complete. -// QEXPECT_FAIL("", "TODO: need to implement write buffering", Continue); - QCOMPARE(bytesWrittenSpy.count(), 0); - } - - QCOMPARE(disconnectedSpy.count(), 1); - QCOMPARE(stateSpy.count(), 1); - QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::UnconnectedState); - QCOMPARE(socket->state(), QBluetoothSocket::UnconnectedState); - } else { - if (shutdown == Disconnect) - socket->disconnectFromService(); - else if (shutdown == Close) - socket->close(); - - if (socket->state() == QBluetoothSocket::UnconnectedState){ - // Linux for example on close goes through closing and unconnected without stopping - QCOMPARE(stateSpy.count(), 2); // closing + unconnected - QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::ClosingState); - QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::UnconnectedState); - QCOMPARE(socket->state(), QBluetoothSocket::UnconnectedState); - - } - else { - QCOMPARE(stateSpy.count(), 1); - QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::ClosingState); - QCOMPARE(socket->state(), QBluetoothSocket::ClosingState); - - int disconnectTime = MaxConnectTime; - while (disconnectedSpy.count() == 0 && disconnectTime > 0) { - QTest::qWait(1000); - disconnectTime -= 1000; - } - QCOMPARE(disconnectedSpy.count(), 1); - QCOMPARE(stateSpy.count(), 1); - QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::UnconnectedState); - } - - if (!data.isEmpty()) { - // Check that pending write completed. - QCOMPARE(bytesWrittenSpy.count(), 1); - QCOMPARE(bytesWrittenSpy.at(0).at(0).toLongLong(), qint64(data.length())); - } - - } - - delete socket; - - // The remote service needs time to close the connection and resume listening - QTest::qSleep(100); -} - -void tst_QBluetoothSocket::tst_serviceConnection_data() -{ - QTest::addColumn<QBluetoothServiceInfo>("serviceInfo"); - - QBluetoothServiceInfo serviceInfo; - - //serviceInfo.setDevice(QBluetoothDeviceInfo(QBluetoothAddress("001167602023"), QString(), 0)); - QBluetoothAddress address(BTADDRESS); - serviceInfo.setDevice(QBluetoothDeviceInfo(address, QString(), 0)); - - QBluetoothServiceInfo::Sequence protocolDescriptorList; - QBluetoothServiceInfo::Sequence protocol; - - protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)); - protocolDescriptorList.append(QVariant::fromValue(protocol)); - - protocol.clear(); - protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) << QVariant::fromValue(quint8(10)); - protocolDescriptorList.append(QVariant::fromValue(protocol)); - - serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); - - QTest::newRow("service connection") << serviceInfo; -} - void tst_QBluetoothSocket::tst_serviceConnection() { - QFETCH(QBluetoothServiceInfo, serviceInfo); + if (!remoteServiceInfo.isValid()) + QSKIP("Remote service not found"); /* Construction */ - QBluetoothSocket *socket = new QBluetoothSocket; + QBluetoothSocket socket; - QSignalSpy stateSpy(socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState))); + QSignalSpy stateSpy(&socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState))); - QCOMPARE(socket->socketType(), QBluetoothServiceInfo::UnknownProtocol); - QCOMPARE(socket->state(), QBluetoothSocket::UnconnectedState); + QCOMPARE(socket.socketType(), QBluetoothServiceInfo::UnknownProtocol); + QCOMPARE(socket.state(), QBluetoothSocket::UnconnectedState); /* Connection */ - QSignalSpy connectedSpy(socket, SIGNAL(connected())); - QSignalSpy errorSpy(socket, SIGNAL(error(QBluetoothSocket::SocketError))); + QSignalSpy connectedSpy(&socket, SIGNAL(connected())); + QSignalSpy errorSpy(&socket, SIGNAL(error(QBluetoothSocket::SocketError))); - socket->connectToService(serviceInfo); + socket.connectToService(remoteServiceInfo); QCOMPARE(stateSpy.count(), 1); QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::ConnectingState); - QCOMPARE(socket->state(), QBluetoothSocket::ConnectingState); + QCOMPARE(socket.state(), QBluetoothSocket::ConnectingState); stateSpy.clear(); @@ -448,14 +285,20 @@ void tst_QBluetoothSocket::tst_serviceConnection() QCOMPARE(connectedSpy.count(), 1); QCOMPARE(stateSpy.count(), 1); QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::ConnectedState); - QCOMPARE(socket->state(), QBluetoothSocket::ConnectedState); + QCOMPARE(socket.state(), QBluetoothSocket::ConnectedState); stateSpy.clear(); + //check the peer & local info + QCOMPARE(socket.localAddress(), localDevice.address()); + QCOMPARE(socket.localName(), localDevice.name()); + QCOMPARE(socket.peerName(), remoteDevice.name()); + QCOMPARE(socket.peerAddress(), remoteDevice.address()); + /* Disconnection */ - QSignalSpy disconnectedSpy(socket, SIGNAL(disconnected())); + QSignalSpy disconnectedSpy(&socket, SIGNAL(disconnected())); - socket->disconnectFromService(); + socket.disconnectFromService(); QVERIFY(stateSpy.count() >= 1); QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::ClosingState); @@ -470,8 +313,6 @@ void tst_QBluetoothSocket::tst_serviceConnection() QCOMPARE(stateSpy.count(), 1); QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::UnconnectedState); - delete socket; - // The remote service needs time to close the connection and resume listening QTest::qSleep(100); } @@ -482,8 +323,8 @@ void tst_QBluetoothSocket::tst_clientCommunication_data() { QStringList data; - data << QLatin1String("Test line one.\n"); - data << QLatin1String("Test line two, with longer data.\n"); + data << QLatin1String("Echo: Test line one.\n"); + data << QLatin1String("Echo: Test line two, with longer data.\n"); QTest::newRow("two line test") << data; } @@ -491,24 +332,26 @@ void tst_QBluetoothSocket::tst_clientCommunication_data() void tst_QBluetoothSocket::tst_clientCommunication() { + if (!remoteServiceInfo.isValid()) + QSKIP("Remote service not found"); QFETCH(QStringList, data); /* Construction */ - QBluetoothSocket *socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); + QBluetoothSocket socket(QBluetoothServiceInfo::RfcommProtocol); - QSignalSpy stateSpy(socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState))); + QSignalSpy stateSpy(&socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState))); - QCOMPARE(socket->socketType(), QBluetoothServiceInfo::RfcommProtocol); - QCOMPARE(socket->state(), QBluetoothSocket::UnconnectedState); + QCOMPARE(socket.socketType(), QBluetoothServiceInfo::RfcommProtocol); + QCOMPARE(socket.state(), QBluetoothSocket::UnconnectedState); /* Connection */ - QSignalSpy connectedSpy(socket, SIGNAL(connected())); + QSignalSpy connectedSpy(&socket, SIGNAL(connected())); - socket->connectToService(QBluetoothAddress(BTADDRESS), 10); // echo service running on device 00:11:67:60:20:23 + socket.connectToService(remoteServiceInfo); QCOMPARE(stateSpy.count(), 1); QCOMPARE(qvariant_cast<QBluetoothSocket::SocketState>(stateSpy.takeFirst().at(0)), QBluetoothSocket::ConnectingState); - QCOMPARE(socket->state(), QBluetoothSocket::ConnectingState); + QCOMPARE(socket.state(), QBluetoothSocket::ConnectingState); stateSpy.clear(); @@ -521,24 +364,24 @@ void tst_QBluetoothSocket::tst_clientCommunication() QCOMPARE(connectedSpy.count(), 1); QCOMPARE(stateSpy.count(), 1); QCOMPARE(qvariant_cast<QBluetoothSocket::SocketState>(stateSpy.takeFirst().at(0)), QBluetoothSocket::ConnectedState); - QCOMPARE(socket->state(), QBluetoothSocket::ConnectedState); + QCOMPARE(socket.state(), QBluetoothSocket::ConnectedState); stateSpy.clear(); /* Read / Write */ - QCOMPARE(socket->bytesToWrite(), qint64(0)); - QCOMPARE(socket->bytesAvailable(), qint64(0)); + QCOMPARE(socket.bytesToWrite(), qint64(0)); + QCOMPARE(socket.bytesAvailable(), qint64(0)); { /* Send line by line with event loop */ foreach (const QString &line, data) { - QSignalSpy readyReadSpy(socket, SIGNAL(readyRead())); - QSignalSpy bytesWrittenSpy(socket, SIGNAL(bytesWritten(qint64))); + QSignalSpy readyReadSpy(&socket, SIGNAL(readyRead())); + QSignalSpy bytesWrittenSpy(&socket, SIGNAL(bytesWritten(qint64))); - socket->write(line.toUtf8()); + socket.write(line.toUtf8()); // QEXPECT_FAIL("", "TODO: need to implement write buffering", Continue); - QCOMPARE(socket->bytesToWrite(), qint64(line.length())); + QCOMPARE(socket.bytesToWrite(), qint64(line.length())); int readWriteTime = MaxReadWriteTime; while ((bytesWrittenSpy.count() == 0 || readyReadSpy.count() == 0) && readWriteTime > 0) { @@ -557,59 +400,26 @@ void tst_QBluetoothSocket::tst_clientCommunication() QCOMPARE(readyReadSpy.count(), 1); - QCOMPARE(socket->bytesAvailable(), qint64(line.length())); + QCOMPARE(socket.bytesAvailable(), qint64(line.length())); - QVERIFY(socket->canReadLine()); + QVERIFY(socket.canReadLine()); - QByteArray echoed = socket->readAll(); + QByteArray echoed = socket.readAll(); QCOMPARE(line.toUtf8(), echoed); } } -#if 0 - { - /* Send line by line without event loop */ - QSignalSpy readyReadSpy(socket, SIGNAL(readyRead())); - QSignalSpy bytesWrittenSpy(socket, SIGNAL(bytesWritten(qint64))); - - qint64 bytesToWrite = 0; - foreach (const QString &line, data) { - socket->write(line.toUtf8()); - bytesToWrite += line.toUtf8().length(); - QCOMPARE(socket->bytesToWrite(), bytesToWrite); - } - - int readWriteTime = MaxReadWriteTime; - while (socket->bytesToWrite() != 0 && readyReadSpy.count() == 0 && readWriteTime > 0) { - QTest::qWait(1000); - readWriteTime -= 1000; - } - - QCOMPARE(bytesWrittenSpy.count(), 1); - QCOMPARE(bytesWrittenSpy.at(0).at(0).toLongLong(), bytesToWrite); - QCOMPARE(readyReadSpy.count(), 1); - - QCOMPARE(socket->bytesAvailable(), bytesToWrite); - - QVERIFY(socket->canReadLine()); - - QByteArray echoed = socket->readAll(); - - QCOMPARE(data.join(QString()).toUtf8(), echoed); - } -#endif - { /* Send all at once */ - QSignalSpy readyReadSpy(socket, SIGNAL(readyRead())); - QSignalSpy bytesWrittenSpy(socket, SIGNAL(bytesWritten(qint64))); + QSignalSpy readyReadSpy(&socket, SIGNAL(readyRead())); + QSignalSpy bytesWrittenSpy(&socket, SIGNAL(bytesWritten(qint64))); QString joined = data.join(QString()); - socket->write(joined.toUtf8()); + socket.write(joined.toUtf8()); // QEXPECT_FAIL("", "TODO: need to implement write buffering", Continue); - QCOMPARE(socket->bytesToWrite(), qint64(joined.length())); + QCOMPARE(socket.bytesToWrite(), qint64(joined.length())); int readWriteTime = MaxReadWriteTime; while ((bytesWrittenSpy.count() == 0 || readyReadSpy.count() == 0) && readWriteTime > 0) { @@ -619,21 +429,21 @@ void tst_QBluetoothSocket::tst_clientCommunication() QCOMPARE(bytesWrittenSpy.count(), 1); QCOMPARE(bytesWrittenSpy.at(0).at(0).toLongLong(), qint64(joined.length())); - QCOMPARE(readyReadSpy.count(), 1); + QVERIFY(readyReadSpy.count() > 0); - QCOMPARE(socket->bytesAvailable(), qint64(joined.length())); + QCOMPARE(socket.bytesAvailable(), qint64(joined.length())); - QVERIFY(socket->canReadLine()); + QVERIFY(socket.canReadLine()); - QByteArray echoed = socket->readAll(); + QByteArray echoed = socket.readAll(); QCOMPARE(joined.toUtf8(), echoed); } /* Disconnection */ - QSignalSpy disconnectedSpy(socket, SIGNAL(disconnected())); + QSignalSpy disconnectedSpy(&socket, SIGNAL(disconnected())); - socket->disconnectFromService(); + socket.disconnectFromService(); int disconnectTime = MaxConnectTime; while (disconnectedSpy.count() == 0 && disconnectTime > 0) { @@ -646,106 +456,8 @@ void tst_QBluetoothSocket::tst_clientCommunication() QCOMPARE(qvariant_cast<QBluetoothSocket::SocketState>(stateSpy.takeFirst().at(0)), QBluetoothSocket::ClosingState); QCOMPARE(qvariant_cast<QBluetoothSocket::SocketState>(stateSpy.takeFirst().at(0)), QBluetoothSocket::UnconnectedState); - delete socket; - // The remote service needs time to close the connection and resume listening QTest::qSleep(100); - -} - -void tst_QBluetoothSocket::tst_localPeer_data() -{ -// QTest::addColumn<QString>("localName"); -// QTest::addColumn<QBluetoothAddress>("localAddress"); -// QTest::addColumn<quint16>("localPort"); - QTest::addColumn<QString>("peerName"); - QTest::addColumn<QBluetoothAddress>("peerAddress"); - QTest::addColumn<quint16>("peerPort"); - - QTest::newRow("test") << QString(BTADDRESS) << QBluetoothAddress(BTADDRESS) << quint16(10); -} - -void tst_QBluetoothSocket::tst_localPeer() -{ -// QFETCH(QString, localName); -// QFETCH(QBluetoothAddress, localAddress); -// QFETCH(quint16, localPort); - QFETCH(QString, peerName); - QFETCH(QBluetoothAddress, peerAddress); - QFETCH(quint16, peerPort); - Q_UNUSED(peerPort) - - QStringList args; - args << "name" << peerAddress.toString(); - QProcess *hcitool = new QProcess(); - hcitool->start("hcitool", args); - hcitool->waitForReadyRead(); - QString peerNameHCI = hcitool->readLine().trimmed(); - hcitool->close(); - delete hcitool; - - - /* Construction */ - QBluetoothSocket *socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); - - QSignalSpy stateSpy(socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState))); - - QCOMPARE(socket->socketType(), QBluetoothServiceInfo::RfcommProtocol); - QCOMPARE(socket->state(), QBluetoothSocket::UnconnectedState); - - /* Connection */ - QSignalSpy connectedSpy(socket, SIGNAL(connected())); - - socket->connectToService(QBluetoothAddress(BTADDRESS), 11); - - QCOMPARE(stateSpy.count(), 1); - QCOMPARE(qvariant_cast<QBluetoothSocket::SocketState>(stateSpy.takeFirst().at(0)), QBluetoothSocket::ConnectingState); - QCOMPARE(socket->state(), QBluetoothSocket::ConnectingState); - - stateSpy.clear(); - - int connectTime = MaxConnectTime; - while (connectedSpy.count() == 0 && connectTime > 0) { - QTest::qWait(1000); - connectTime -= 1000; - } - - QCOMPARE(connectedSpy.count(), 1); - QCOMPARE(stateSpy.count(), 1); - QCOMPARE(qvariant_cast<QBluetoothSocket::SocketState>(stateSpy.takeFirst().at(0)), QBluetoothSocket::ConnectedState); - QCOMPARE(socket->state(), QBluetoothSocket::ConnectedState); - - QByteArray echoed = socket->readAll(); - QString data(echoed); - QStringList list = data.split(QChar(' '), QString::SkipEmptyParts); - // list is: - // [0]local mac, [1]local port, [2]local name, [3]peer mac, [4]peer port - - QCOMPARE(socket->peerAddress(), QBluetoothAddress(list[0])); - QCOMPARE(socket->peerPort(), list[1].toUShort()); - - QCOMPARE(socket->localName(), list[2]); - QCOMPARE(socket->localAddress(), QBluetoothAddress(list[3])); - QCOMPARE(socket->localPort(), list[4].toUShort()); - QCOMPARE(socket->peerName(), peerNameHCI); - - /* Disconnection */ - QSignalSpy disconnectedSpy(socket, SIGNAL(disconnected())); - - socket->disconnectFromService(); - - int disconnectTime = MaxConnectTime; - while (disconnectedSpy.count() == 0 && disconnectTime > 0) { - QTest::qWait(1000); - disconnectTime -= 1000; - } - - QCOMPARE(disconnectedSpy.count(), 1); - QCOMPARE(stateSpy.count(), 2); - QCOMPARE(qvariant_cast<QBluetoothSocket::SocketState>(stateSpy.takeFirst().at(0)), QBluetoothSocket::ClosingState); - QCOMPARE(qvariant_cast<QBluetoothSocket::SocketState>(stateSpy.takeFirst().at(0)), QBluetoothSocket::UnconnectedState); - - delete socket; } void tst_QBluetoothSocket::tst_error() @@ -755,10 +467,7 @@ void tst_QBluetoothSocket::tst_error() QCOMPARE(errorSpy.count(), 0); const QBluetoothSocket::SocketError e = socket.error(); - QVERIFY(e != QBluetoothSocket::HostNotFoundError - && e != QBluetoothSocket::NetworkError - && e != QBluetoothSocket::ServiceNotFoundError - && e != QBluetoothSocket::UnknownSocketError); + QVERIFY(e == QBluetoothSocket::NoSocketError); QVERIFY(socket.errorString() == QString()); } diff --git a/tests/auto/qbluetoothtransfermanager/qbluetoothtransfermanager.pro b/tests/auto/qbluetoothtransfermanager/qbluetoothtransfermanager.pro index aeb45868..0fb67ca1 100644 --- a/tests/auto/qbluetoothtransfermanager/qbluetoothtransfermanager.pro +++ b/tests/auto/qbluetoothtransfermanager/qbluetoothtransfermanager.pro @@ -5,7 +5,5 @@ testcase.timeout = 250 # this test is slow QT = core concurrent bluetooth testlib -INCLUDEPATH += ../../../tests/btclient - TESTDATA += *.txt DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 diff --git a/tests/auto/qbluetoothtransfermanager/tst_qbluetoothtransfermanager.cpp b/tests/auto/qbluetoothtransfermanager/tst_qbluetoothtransfermanager.cpp index fb2c1ad3..beac5b88 100644 --- a/tests/auto/qbluetoothtransfermanager/tst_qbluetoothtransfermanager.cpp +++ b/tests/auto/qbluetoothtransfermanager/tst_qbluetoothtransfermanager.cpp @@ -54,7 +54,6 @@ #include <qbluetoothdeviceinfo.h> #include <qbluetoothserviceinfo.h> #include <qbluetoothservicediscoveryagent.h> -#include <btclient.h> /* * Some tests require a Bluetooth device within the vincinity of the test diff --git a/tests/auto/ql2capserver/ql2capserver.pro b/tests/auto/ql2capserver/ql2capserver.pro deleted file mode 100644 index c87a0503..00000000 --- a/tests/auto/ql2capserver/ql2capserver.pro +++ /dev/null @@ -1,8 +0,0 @@ -SOURCES += tst_ql2capserver.cpp -TARGET = tst_ql2capserver - -# Note, not really an autotest. Requires manually setting up and running btclient first. -#CONFIG += testcase - -QT = core concurrent bluetooth testlib -DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 diff --git a/tests/auto/ql2capserver/tst_ql2capserver.cpp b/tests/auto/ql2capserver/tst_ql2capserver.cpp deleted file mode 100644 index d8a5d0d4..00000000 --- a/tests/auto/ql2capserver/tst_ql2capserver.cpp +++ /dev/null @@ -1,285 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtBluetooth module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QtTest/QtTest> - -#include <QDebug> - -#include <qbluetoothserver.h> -#include <qbluetoothsocket.h> - -QT_USE_NAMESPACE - -Q_DECLARE_METATYPE(QBluetooth::SecurityFlags); - -// Max time to wait for connection -static const int MaxConnectTime = 60 * 1000; // 1 minute in ms - -class tst_QL2capServer : public QObject -{ - Q_OBJECT - -public: - tst_QL2capServer(); - ~tst_QL2capServer(); - -private slots: - void initTestCase(); - - void tst_construction(); - - void tst_listen_data(); - void tst_listen(); - - void tst_pendingConnections_data(); - void tst_pendingConnections(); - - void tst_receive_data(); - void tst_receive(); - - void tst_secureFlags(); -}; - -tst_QL2capServer::tst_QL2capServer() -{ -} - -tst_QL2capServer::~tst_QL2capServer() -{ -} - -void tst_QL2capServer::initTestCase() -{ - qRegisterMetaType<QBluetooth::SecurityFlags>("QBluetooth::SecurityFlags"); -} - -void tst_QL2capServer::tst_construction() -{ - { - QBluetoothServer server(QBluetoothServiceInfo::L2capProtocol); - - QVERIFY(!server.isListening()); - QCOMPARE(server.maxPendingConnections(), 1); - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); - QVERIFY(server.serverAddress().isNull()); - QCOMPARE(server.serverPort(), quint16(24160)); - } -} - -void tst_QL2capServer::tst_listen_data() -{ - QTest::addColumn<QBluetoothAddress>("address"); - QTest::addColumn<quint16>("port"); - - QTest::newRow("default") << QBluetoothAddress() << quint16(0); - QTest::newRow("specified address") << QBluetoothAddress("00:11:B1:08:AD:B8") << quint16(0); - QTest::newRow("specified port") << QBluetoothAddress() << quint16(24160); - QTest::newRow("specified address/port") << QBluetoothAddress("00:11:B1:08:AD:B8") << quint16(10); -} - -void tst_QL2capServer::tst_listen() -{ - QFETCH(QBluetoothAddress, address); - QFETCH(quint16, port); - - { - QBluetoothServer server(QBluetoothServiceInfo::L2capProtocol); - - bool result = server.listen(address, port); - - QVERIFY(result); - QVERIFY(server.isListening()); - - if (!address.isNull()) - QCOMPARE(server.serverAddress(), address); - else - QVERIFY(!server.serverAddress().isNull()); - - if (port != 0) - QCOMPARE(server.serverPort(), port); - else - QVERIFY(server.serverPort() != 0); - - QCOMPARE(server.maxPendingConnections(), 1); - - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); - - server.close(); - - QVERIFY(!server.isListening()); - - QVERIFY(server.serverAddress().isNull()); - QVERIFY(server.serverPort() == 0); - - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); - } -} - -void tst_QL2capServer::tst_pendingConnections_data() -{ - QTest::addColumn<int>("maxConnections"); - - QTest::newRow("1 connection") << 1; - //QTest::newRow("2 connections") << 2; -} - -void tst_QL2capServer::tst_pendingConnections() -{ - QFETCH(int, maxConnections); - - { - QBluetoothServer server(QBluetoothServiceInfo::L2capProtocol); - - server.setMaxPendingConnections(maxConnections); - - bool result = server.listen(); - - QVERIFY(result); - QVERIFY(server.isListening()); - - qDebug() << "Listening on L2CAP channel:" << server.serverPort(); - - QCOMPARE(server.maxPendingConnections(), maxConnections); - - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); - - { - /* wait for maxConnections simultaneous connections */ - qDebug() << "Waiting for" << maxConnections << "simultaneous connections."; - - QSignalSpy connectionSpy(&server, SIGNAL(newConnection())); - - int connectTime = MaxConnectTime; - while (connectionSpy.count() < maxConnections && connectTime > 0) { - QTest::qWait(1000); - connectTime -= 1000; - } - - QList<QBluetoothSocket *> sockets; - while (server.hasPendingConnections()) - sockets.append(server.nextPendingConnection()); - - QCOMPARE(connectionSpy.count(), maxConnections); - QCOMPARE(sockets.count(), maxConnections); - - foreach (QBluetoothSocket *socket, sockets) { - qDebug() << socket->state(); - QVERIFY(socket->state() == QBluetoothSocket::ConnectedState); - QVERIFY(socket->openMode() == QIODevice::ReadWrite); - } - - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); - - while (!sockets.isEmpty()) { - QBluetoothSocket *socket = sockets.takeFirst(); - socket->close(); - delete socket; - } - } - - server.close(); - } -} - -void tst_QL2capServer::tst_receive_data() -{ - QTest::addColumn<QByteArray>("expected"); - - QTest::newRow("test") << QByteArray("hello\r\n"); -} - -void tst_QL2capServer::tst_receive() -{ - QFETCH(QByteArray, expected); - - QBluetoothServer server(QBluetoothServiceInfo::L2capProtocol); - - bool result = server.listen(); - - QVERIFY(result); - - qDebug() << "Listening on L2CAP channel:" << server.serverPort(); - - int connectTime = MaxConnectTime; - while (!server.hasPendingConnections() && connectTime > 0) { - QTest::qWait(1000); - connectTime -= 1000; - } - - QVERIFY(server.hasPendingConnections()); - - QBluetoothSocket *socket = server.nextPendingConnection(); - - QVERIFY(socket->state() == QBluetoothSocket::ConnectedState); - QVERIFY(socket->openMode() == QIODevice::ReadWrite); - - QSignalSpy readyReadSpy(socket, SIGNAL(readyRead())); - - int readyReadTime = 60000; - while (readyReadSpy.isEmpty() && readyReadTime > 0) { - QTest::qWait(1000); - readyReadTime -= 1000; - } - - QVERIFY(!readyReadSpy.isEmpty()); - - const QByteArray data = socket->readAll(); - - QCOMPARE(data, expected); -} - -void tst_QL2capServer::tst_secureFlags() -{ - QBluetoothServer server(QBluetoothServiceInfo::L2capProtocol); - QCOMPARE(server.securityFlags(), QBluetooth::NoSecurity); - - server.setSecurityFlags(QBluetooth::Encryption); - QCOMPARE(server.securityFlags(), QBluetooth::Encryption); -} - -QTEST_MAIN(tst_QL2capServer) - -#include "tst_ql2capserver.moc" diff --git a/tests/auto/qrfcommserver/README.txt b/tests/auto/qrfcommserver/README.txt deleted file mode 100644 index 8f3d2ef8..00000000 --- a/tests/auto/qrfcommserver/README.txt +++ /dev/null @@ -1,50 +0,0 @@ -This test currently can not be run automatically. - -To make this test work, manual connections must be started from a client -machine. There are 2 options for doing this, either use the rfcommclient application in the subdirectory, -or run the test manually as described below. When using rfcommclient, start qrfcommserver first and then -start rfcommclient on the other device. There might be some timing issues, so beware. - -Steps to run this test manually: - -1. start tst_qrfcommserver - -2. Wait for test display: - -QDEBUG : tst_QRfcommServer::tst_pendingConnections(1 connection) Waiting for 1 simultaneous connections - -On a linux machine run: - -rfcomm connect hci0 00:1A:9F:92:9E:5A - -On the linux machine you will see: - -Connected /dev/rfcomm0 to 00:1A:9F:92:9E:5A on channel 1 -Press CTRL-C for hangup -Disconnected - -and the test will pass. - -3. Wait for the test to then display: - -QDEBUG : tst_QRfcommServer::tst_receive(test) Listening on RFCOMM channel: 1 - -On the linux machine run: - -rfcomm connect hci0 00:1A:9F:92:9E:5A - -You will see: - -Connected /dev/rfcomm0 to 00:1A:9F:92:9E:5A on channel 1 -Press CTRL-C for hangup - -now run: - -echo -en "hello\r\n" > /dev/rfcomm0 - -The test should then pass. - -A linux machine is not required, but simple used as an example for how to make -easy rfcomm connections. Any system that will connect to a device and rfcomm -port will work. The format of the string for the second test is very important -for the test to pass. diff --git a/tests/auto/qrfcommserver/rfcommclient/main.cpp b/tests/auto/qrfcommserver/rfcommclient/main.cpp deleted file mode 100644 index be751074..00000000 --- a/tests/auto/qrfcommserver/rfcommclient/main.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtNfc module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QCoreApplication> -#include <QStringList> -#include "rfcommclient.h" -#include <qbluetoothdeviceinfo.h> -//#include <qbluetoothlocaldevice.h> -//#include <QtTest/QtTest> - -int main(int argc, char *argv[]) -{ - QCoreApplication app(argc, argv); - RfCommClient client; - QBluetoothLocalDevice localDevice; - MyThread mythread; - - QObject::connect(&client, SIGNAL(done()), &app, SLOT(quit())); - - QString address; - QString port; - QStringList args = QCoreApplication::arguments(); - - if (args.length() >= 2){ - address = args.at(1); - if (args.length() >= 3){ - port = args.at(2); - } - } - - // use previous value for client, stored earlier -// if (address.isEmpty()){ -// QSettings settings("QtDF", "bttennis"); -// address = settings.value("lastclient").toString(); -// } - - // hard-code address and port number if not provided - if (address.isEmpty()){ - address = "6C:9B:02:0C:91:D3"; // "J C7-2" - port = QString("20"); - } - - if (!address.isEmpty()){ - qDebug() << "Connecting to" << address << port; - QBluetoothDeviceInfo device = QBluetoothDeviceInfo(QBluetoothAddress(address), "", - QBluetoothDeviceInfo::MiscellaneousDevice); - QBluetoothServiceInfo service; - if (!port.isEmpty()) { - QBluetoothServiceInfo::Sequence protocolDescriptorList; - QBluetoothServiceInfo::Sequence protocol; - protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) - << QVariant::fromValue(port.toUShort()); - protocolDescriptorList.append(QVariant::fromValue(protocol)); - service.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, - protocolDescriptorList); - qDebug() << "port" << port.toUShort() << service.protocolServiceMultiplexer(); - } - else { - service.setServiceUuid(QBluetoothUuid(serviceUuid)); - } - service.setDevice(device); - // delay so that server is in waiting state - qDebug() << "Starting sleep"; - mythread.sleep(10); // seconds - qDebug() << "Finished sleeping"; - client.startClient(service); - } else { - qDebug() << "failed because address and/or port is missing " << address << port; - } - - return app.exec(); -} - diff --git a/tests/auto/qrfcommserver/rfcommclient/rfcommclient.cpp b/tests/auto/qrfcommserver/rfcommclient/rfcommclient.cpp deleted file mode 100644 index 0aa83016..00000000 --- a/tests/auto/qrfcommserver/rfcommclient/rfcommclient.cpp +++ /dev/null @@ -1,211 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtNfc module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "rfcommclient.h" - -#include <qbluetoothsocket.h> -#include <qbluetoothlocaldevice.h> - -#include <QtCore/QDataStream> -#include <QtCore/QByteArray> - -#include <QtCore/QStringList> - -void MyThread::sleep(int seconds) -{ - QThread::sleep(seconds); -} - -RfCommClient::RfCommClient(QObject *parent) -: QObject(parent), socket(0), stream(0), elapsed(new QTime), lagTimeout(0), state(listening) -{ - lagTimer.setSingleShot(true); - lagTimer.setInterval(5000); -} - -RfCommClient::~RfCommClient() -{ - stopClient(); -} - -bool RfCommClient::powerOn() -{ - qDebug() << __PRETTY_FUNCTION__ << ">>"; - // turn on BT in case it is not on - if (localDevice.hostMode() == QBluetoothLocalDevice::HostPoweredOff) { - connect(&localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)), - this, SLOT(hostModeStateChanged(QBluetoothLocalDevice::HostMode))); -// connect(localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)), -// this, SLOT(hostModeStateChanged(QBluetoothLocalDevice::HostMode))); - qDebug() << __PRETTY_FUNCTION__ << "Turning power on"; - localDevice.powerOn(); - } else { - qDebug() << __PRETTY_FUNCTION__ << "<< Power already on! returning true"; - return true; - } - qDebug() << __PRETTY_FUNCTION__ << "<< returning false"; - return false; -} - -void RfCommClient::hostModeStateChanged(QBluetoothLocalDevice::HostMode mode) -{ - qDebug() << __PRETTY_FUNCTION__ << mode; - if (mode != QBluetoothLocalDevice::HostPoweredOff) - startClient(serviceInfo); -} -//! [startClient] -void RfCommClient::startClient(const QBluetoothServiceInfo &remoteService) -{ - qDebug() << __PRETTY_FUNCTION__ << ">>"; - serviceInfo = remoteService; - - // make sure preconditions are met - if (!powerOn() || socket) { - qDebug() << __PRETTY_FUNCTION__ << "<< power not on or socket already exists!"; - return; - } - - // Connect to service - if (state == listening) - state = pendingConnections; - socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); - qDebug() << "Create socket"; - socket->connectToService(remoteService); - qDebug() << "ConnecttoService done"; - - connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket())); - connect(socket, SIGNAL(connected()), this, SLOT(connected())); - connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); - connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(error(QBluetoothSocket::SocketError))); - - qDebug() << __PRETTY_FUNCTION__ << "<<"; -} -//! [startClient] - -//! [stopClient] -void RfCommClient::stopClient() -{ - qDebug() << __PRETTY_FUNCTION__ << "closing client!"; - - lagTimer.stop(); - - delete stream; - stream = 0; - - socket->deleteLater(); - socket = 0; -} -//! [stopClient] - -//! [socketDisconnected] -void RfCommClient::socketDisconnected() -{ - // Note: it seems that the "disconnected" signal is not emitted by the socket, so this never gets called - qDebug() << __PRETTY_FUNCTION__ << "Got socketDisconnected"; - emit disconnected(); - stopClient(); - - // now reconnect and send text string - startClient(serviceInfo); - connect(&lagTimer, SIGNAL(timeout()), this, SLOT(sendText())); - lagTimer.start(); -} -//! [socketDisconnected] - -//! [readSocket] -void RfCommClient::readSocket() -{ - if (!socket) - return; - QString str; - - while (socket->bytesAvailable()) { - *stream >> str; - } - qDebug() << __PRETTY_FUNCTION__ << "socket read=" << str; -} -//! [readSocket] - -//! [connected] -void RfCommClient::connected() -{ - qDebug() << __PRETTY_FUNCTION__ << "connected to " << socket->peerName(); - stream = new QDataStream(socket); - emit connected(socket->peerName()); -} -//! [connected] - -void RfCommClient::error(QBluetoothSocket::SocketError err) -{ - qDebug() << __PRETTY_FUNCTION__ << "Got socket error" << err; - // remote side has closed the socket, effectively disconnecting it - if (state == pendingConnections) { - state = dataTransfer; - emit disconnected(); - stopClient(); - - // now reconnect and send text string - MyThread mythread; - mythread.sleep(5); - startClient(serviceInfo); - connect(&lagTimer, SIGNAL(timeout()), this, SLOT(sendText())); - lagTimer.start(); - } else { - qDebug() << __PRETTY_FUNCTION__ << "emitting done"; - emit done(); - } -} - -void RfCommClient::sendText() -{ - qDebug() << __PRETTY_FUNCTION__ << ">>"; - lagTimer.stop(); - if (stream) { - buffer.clear(); - buffer.append(QString("hello\r\n")); // ideally we would use QDataStream here - socket->write(buffer); - } - // terminate client program - emit done(); - qDebug() << __PRETTY_FUNCTION__ << "<< Terminating program"; -} - - diff --git a/tests/auto/qrfcommserver/rfcommclient/rfcommclient.h b/tests/auto/qrfcommserver/rfcommclient/rfcommclient.h deleted file mode 100644 index 48f1e760..00000000 --- a/tests/auto/qrfcommserver/rfcommclient/rfcommclient.h +++ /dev/null @@ -1,116 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtNfc module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef RFCOMMCLIENT_H -#define RFCOMMCLIENT_H - -#include <qbluetoothserviceinfo.h> -#include <qbluetoothsocket.h> - -#include <QtCore/QObject> -#include <QtCore/QTime> -#include <QtCore/QTimer> -#include <QBluetoothLocalDevice> -#include <QThread> - -static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c9"); - -class QBluetoothSocket; - -class QDataStream; - -class MyThread : public QThread -{ -public: - void sleep(int seconds); -}; - - -//! [declaration] -class RfCommClient : public QObject -{ - Q_OBJECT - - enum States { - listening, - pendingConnections, - dataTransfer - }; -public: - explicit RfCommClient(QObject *parent = 0); - ~RfCommClient(); - - void startClient(const QBluetoothServiceInfo &remoteService); - void stopClient(); - -public slots: - void error(QBluetoothSocket::SocketError); - -signals: - void connected(const QString &name); - void disconnected(); - void done(); - void lag(int ms); - -private slots: - void readSocket(); - void connected(); - void sendText(); - void socketDisconnected(); - void hostModeStateChanged(QBluetoothLocalDevice::HostMode); - -private: - bool powerOn(); - -private: - QBluetoothSocket *socket; - QDataStream *stream; - QByteArray buffer; - QBluetoothServiceInfo serviceInfo; - QBluetoothLocalDevice localDevice; - QTime *elapsed; - QTimer lagTimer; - int lagTimeout; - RfCommClient::States state; -}; -//! [declaration] - -#endif // RFCOMMCLIENT_H diff --git a/tests/auto/qrfcommserver/rfcommclient/rfcommclient.pro b/tests/auto/qrfcommserver/rfcommclient/rfcommclient.pro deleted file mode 100644 index 8c28a661..00000000 --- a/tests/auto/qrfcommserver/rfcommclient/rfcommclient.pro +++ /dev/null @@ -1,6 +0,0 @@ -TARGET = rfcommclient -SOURCES += main.cpp rfcommclient.cpp -HEADERS += rfcommclient.h - -QT = core bluetooth -DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 diff --git a/tests/auto/qrfcommserver/tst_qrfcommserver.cpp b/tests/auto/qrfcommserver/tst_qrfcommserver.cpp deleted file mode 100644 index cc64826c..00000000 --- a/tests/auto/qrfcommserver/tst_qrfcommserver.cpp +++ /dev/null @@ -1,333 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtBluetooth module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QtTest/QtTest> - -#include <QDebug> - -#include <qbluetoothserver.h> -#include <qbluetoothsocket.h> -#include <qbluetoothlocaldevice.h> - -QT_USE_NAMESPACE - -Q_DECLARE_METATYPE(QBluetooth::SecurityFlags); - -// Max time to wait for connection -static const int MaxConnectTime = 60 * 1000; // 1 minute in ms - -class tst_QRfcommServer : public QObject -{ - Q_OBJECT - -public: - tst_QRfcommServer(); - ~tst_QRfcommServer(); - -private slots: - void initTestCase(); - - void tst_construction(); - - void tst_listen_data(); - void tst_listen(); - - void tst_secureFlags(); - - void tst_pendingConnections_data(); - void tst_pendingConnections(); - - void tst_receive_data(); - void tst_receive(); - - void tst_error(); - -private: - QBluetoothLocalDevice localDevice; -}; - -tst_QRfcommServer::tst_QRfcommServer() -{ -} - -tst_QRfcommServer::~tst_QRfcommServer() -{ -} - -void tst_QRfcommServer::initTestCase() -{ - qRegisterMetaType<QBluetooth::SecurityFlags>("QBluetooth::SecurityFlags"); - - if (!QBluetoothLocalDevice::allDevices().count()) - QSKIP("Skipping test due to missing Bluetooth device"); - - // turn on BT in case it is not on - if (localDevice.hostMode() == QBluetoothLocalDevice::HostPoweredOff) { - QSignalSpy hostModeSpy(&localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode))); - QVERIFY(hostModeSpy.isEmpty()); - localDevice.powerOn(); - int connectTime = 5000; // ms - while (hostModeSpy.count() < 1 && connectTime > 0) { - QTest::qWait(500); - connectTime -= 500; - } - QVERIFY(hostModeSpy.count() > 0); - } - QBluetoothLocalDevice::HostMode hostMode= localDevice.hostMode(); - QVERIFY(hostMode == QBluetoothLocalDevice::HostConnectable - || hostMode == QBluetoothLocalDevice::HostDiscoverable - || hostMode == QBluetoothLocalDevice::HostDiscoverableLimitedInquiry); -} - -void tst_QRfcommServer::tst_construction() -{ - { - QBluetoothServer server(QBluetoothServiceInfo::RfcommProtocol); - - QVERIFY(!server.isListening()); - QCOMPARE(server.maxPendingConnections(), 1); - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); - QVERIFY(server.serverAddress().isNull()); - QCOMPARE(server.serverPort(), quint16(0)); - } -} - -void tst_QRfcommServer::tst_listen_data() -{ - QTest::addColumn<QBluetoothAddress>("address"); - QTest::addColumn<quint16>("port"); - - QTest::newRow("default") << QBluetoothAddress() << quint16(0); - QTest::newRow("specified address") << QBluetoothAddress("00:11:B1:08:AD:B8") << quint16(0); - QTest::newRow("specified port") << QBluetoothAddress() << quint16(10); - QTest::newRow("specified address/port") << QBluetoothAddress("00:11:B1:08:AD:B8") << quint16(10); -} - -void tst_QRfcommServer::tst_listen() -{ - QFETCH(QBluetoothAddress, address); - QFETCH(quint16, port); - - { - QBluetoothServer server(QBluetoothServiceInfo::RfcommProtocol); - qDebug() << "tst_listen() address=" << address.toString() << "port=" << port; - bool result = server.listen(address, port); - QTest::qWait(1000); - - QVERIFY(result); - QVERIFY(server.isListening()); - - if (!address.isNull()) - QCOMPARE(server.serverAddress(), address); - - qDebug()<<"Server Port="<<server.serverPort(); - if (port != 0) - QCOMPARE(server.serverPort(), port); - else - QVERIFY(server.serverPort() != 0); - - QCOMPARE(server.maxPendingConnections(), 1); - - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); - - server.close(); - QTest::qWait(2000); - - QVERIFY(!server.isListening()); - - QVERIFY(server.serverAddress().isNull()); - QVERIFY(server.serverPort() == 0); - - QVERIFY(server.hasPendingConnections() == false); - QVERIFY(server.nextPendingConnection() == 0); - } -} - -void tst_QRfcommServer::tst_pendingConnections_data() -{ - QTest::addColumn<int>("maxConnections"); - - QTest::newRow("1 connection") << 1; - //QTest::newRow("2 connections") << 2; -} - -void tst_QRfcommServer::tst_pendingConnections() -{ - QFETCH(int, maxConnections); - - QBluetoothServer server(QBluetoothServiceInfo::RfcommProtocol); - QBluetoothLocalDevice localDev; - - QBluetoothAddress address = localDev.address(); - server.setMaxPendingConnections(maxConnections); - bool result = server.listen(address, 20); // port == 20 - QTest::qWait(1000); - - QVERIFY(result); - QVERIFY(server.isListening()); - - qDebug() << "tst_pendingConnections() Listening on address " << address.toString() << "RFCOMM channel:" << server.serverPort(); - - QCOMPARE(server.maxPendingConnections(), maxConnections); - - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); - - /* wait for maxConnections simultaneous connections */ - qDebug() << "Waiting for" << maxConnections << "simultaneous connections."; - - QSignalSpy connectionSpy(&server, SIGNAL(newConnection())); - - int connectTime = MaxConnectTime; - while (connectionSpy.count() < maxConnections && connectTime > 0) { - QTest::qWait(1000); - connectTime -= 1000; - } - - QList<QBluetoothSocket *> sockets; - while (server.hasPendingConnections()) - sockets.append(server.nextPendingConnection()); - - QCOMPARE(connectionSpy.count(), maxConnections); - QCOMPARE(sockets.count(), maxConnections); - - foreach (QBluetoothSocket *socket, sockets) { - qDebug() << socket->state(); - QVERIFY(socket->state() == QBluetoothSocket::ConnectedState); - QVERIFY(socket->openMode() == QIODevice::ReadWrite); - } - - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); - - while (!sockets.isEmpty()) { - QBluetoothSocket *socket = sockets.takeFirst(); - socket->close(); - delete socket; - } - - server.close(); -} - -void tst_QRfcommServer::tst_receive_data() -{ - QTest::addColumn<QByteArray>("expected"); - - QTest::newRow("test") << QByteArray("hello\r\n"); -} - -void tst_QRfcommServer::tst_receive() -{ - QFETCH(QByteArray, expected); - - QBluetoothServer server(QBluetoothServiceInfo::RfcommProtocol); - QBluetoothLocalDevice localDev; - - QBluetoothAddress address = localDev.address(); - bool result = server.listen(address, 20); // port == 20 - QTest::qWait(1000); - - QVERIFY(result); - QVERIFY(server.isListening()); - - qDebug() << "Listening on address " << address.toString() << "RFCOMM channel:" << server.serverPort(); - - int connectTime = MaxConnectTime; - while (!server.hasPendingConnections() && connectTime > 0) { - QTest::qWait(1000); - connectTime -= 1000; - } - - QVERIFY(server.hasPendingConnections()); - - qDebug() << "Got connection"; - - QBluetoothSocket *socket = server.nextPendingConnection(); - - QVERIFY(socket->state() == QBluetoothSocket::ConnectedState); - QVERIFY(socket->openMode() == QIODevice::ReadWrite); - - QSignalSpy readyReadSpy(socket, SIGNAL(readyRead())); - - int readyReadTime = 60000; - while (readyReadSpy.isEmpty() && readyReadTime > 0) { - QTest::qWait(1000); - readyReadTime -= 1000; - } - - QVERIFY(!readyReadSpy.isEmpty()); - - const QByteArray data = socket->readAll(); - - QCOMPARE(data, expected); -} - -void tst_QRfcommServer::tst_secureFlags() -{ - QBluetoothServer server(QBluetoothServiceInfo::RfcommProtocol); - - server.setSecurityFlags(QBluetooth::NoSecurity); - QCOMPARE(server.securityFlags(), QBluetooth::NoSecurity); - - server.setSecurityFlags(QBluetooth::Encryption); - QCOMPARE(server.securityFlags(), QBluetooth::Encryption); -} - - -void tst_QRfcommServer::tst_error() -{ - QBluetoothServer server(QBluetoothServiceInfo::RfcommProtocol); - QSignalSpy errorSpy(&server, SIGNAL(error(QBluetoothServer::Error))); - QCOMPARE(errorSpy.count(), 0); - const QBluetoothServer::Error e = server.error(); - - QVERIFY(e != QBluetoothServer::UnknownError - && e != QBluetoothServer::PoweredOffError - && e != QBluetoothServer::InputOutputError - && e != QBluetoothServer::ServiceAlreadyRegisteredError - && e != QBluetoothServer::UnsupportedProtocolError); -} - -QTEST_MAIN(tst_QRfcommServer) - -#include "tst_qrfcommserver.moc" |