From 10e636b4e5a001ed2ffbb8e5c0a672904accc817 Mon Sep 17 00:00:00 2001 From: Alex Blasche Date: Thu, 27 Feb 2014 11:34:44 +0100 Subject: Fix qbluetoothsocket unit test Task-number: QTBUG-22017 Change-Id: I876b8a052873d198e8991b7eab3ca70714eb0dcc Reviewed-by: Nedim Hadzic Reviewed-by: Alex Blasche --- src/bluetooth/qbluetoothsocket_p.cpp | 23 +- tests/auto/qbluetoothsocket/README.txt | 17 +- .../auto/qbluetoothsocket/tst_qbluetoothsocket.cpp | 559 +++++---------------- tests/bttestui/btlocaldevice.cpp | 15 +- 4 files changed, 174 insertions(+), 440 deletions(-) diff --git a/src/bluetooth/qbluetoothsocket_p.cpp b/src/bluetooth/qbluetoothsocket_p.cpp index bacf7490..d64e7ecf 100644 --- a/src/bluetooth/qbluetoothsocket_p.cpp +++ b/src/bluetooth/qbluetoothsocket_p.cpp @@ -45,6 +45,10 @@ QT_BEGIN_NAMESPACE QBluetoothSocketPrivate::QBluetoothSocketPrivate() + : socket(-1), + socketType(QBluetoothServiceInfo::UnknownProtocol), + state(QBluetoothSocket::UnconnectedState), + socketError(QBluetoothSocket::NoSocketError) { } @@ -54,7 +58,7 @@ QBluetoothSocketPrivate::~QBluetoothSocketPrivate() bool QBluetoothSocketPrivate::ensureNativeSocket(QBluetoothServiceInfo::Protocol type) { - Q_UNUSED(type); + socketType = type; return false; } @@ -111,6 +115,14 @@ qint64 QBluetoothSocketPrivate::writeData(const char *data, qint64 maxSize) { Q_UNUSED(data); Q_UNUSED(maxSize); + + Q_Q(QBluetoothSocket); + + if (state != QBluetoothSocket::ConnectedState) { + errorString = QBluetoothSocket::tr("Cannot write while not connected"); + q->setSocketError(QBluetoothSocket::OperationError); + return -1; + } return 0; } @@ -118,6 +130,15 @@ qint64 QBluetoothSocketPrivate::readData(char *data, qint64 maxSize) { Q_UNUSED(data); Q_UNUSED(maxSize); + + Q_Q(QBluetoothSocket); + + if (state != QBluetoothSocket::ConnectedState) { + errorString = QBluetoothSocket::tr("Cannot write while not connected"); + q->setSocketError(QBluetoothSocket::OperationError); + return -1; + } + return 0; } 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/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 -#include - #include #include #include #include #include -#include - 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"); qRegisterMetaType("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 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(); + QVERIFY(er == QBluetoothSocket::OperationError); + } } { QBluetoothSocket socket(socketType); - QCOMPARE(socket.socketType(), socketType); } } -void tst_QBluetoothSocket::tst_clientConnection_data() -{ - QTest::addColumn("socketprotocol"); - QTest::addColumn("shutdown"); - QTest::addColumn("address"); - QTest::addColumn("port"); - QTest::addColumn("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::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::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::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::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::ClosingState); - QCOMPARE(stateSpy.takeFirst().at(0).value(), QBluetoothSocket::UnconnectedState); - QCOMPARE(socket->state(), QBluetoothSocket::UnconnectedState); - - } - else { - QCOMPARE(stateSpy.count(), 1); - QCOMPARE(stateSpy.takeFirst().at(0).value(), 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::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("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::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::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::ClosingState); @@ -470,8 +313,6 @@ void tst_QBluetoothSocket::tst_serviceConnection() QCOMPARE(stateSpy.count(), 1); QCOMPARE(stateSpy.takeFirst().at(0).value(), 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(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(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(stateSpy.takeFirst().at(0)), QBluetoothSocket::ClosingState); QCOMPARE(qvariant_cast(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("localName"); -// QTest::addColumn("localAddress"); -// QTest::addColumn("localPort"); - QTest::addColumn("peerName"); - QTest::addColumn("peerAddress"); - QTest::addColumn("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(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(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(stateSpy.takeFirst().at(0)), QBluetoothSocket::ClosingState); - QCOMPARE(qvariant_cast(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/bttestui/btlocaldevice.cpp b/tests/bttestui/btlocaldevice.cpp index b73a2a73..22a89275 100644 --- a/tests/bttestui/btlocaldevice.cpp +++ b/tests/bttestui/btlocaldevice.cpp @@ -413,6 +413,7 @@ void BtLocalDevice::dumpSocketInformation() << socket->peerName() << socket->peerPort(); qDebug() << "socket type:" << socket->socketType(); qDebug() << "socket state:" << socket->state(); + qDebug() << "socket bytesAvailable()" << socket->bytesAvailable(); QString tmp; switch (socket->error()) { case QBluetoothSocket::NoSocketError: tmp += "NoSocketError"; break; @@ -583,9 +584,18 @@ void BtLocalDevice::clientSocketReadyRead() return; while (socket->canReadLine()) { - QByteArray line = socket->readLine().trimmed(); + const QByteArray line = socket->readLine().trimmed(); + QString lineString = QString::fromUtf8(line.constData(), line.length()); qDebug() << ">>(" << socket->peerName() << ")>>" - << QString::fromUtf8(line.constData(), line.length()); + << lineString; + + //when using the tst_QBluetoothSocket we echo received text back + //Any line starting with "Echo:" will be echoed + if (lineString.startsWith(QStringLiteral("Echo:"))) { + qDebug() << "Assuming tst_qbluetoothsocket as client. Echoing back."; + lineString += QLatin1Char('\n'); + socket->write(lineString.toUtf8()); + } } } @@ -616,6 +626,7 @@ void BtLocalDevice::dumpServerInformation() qDebug() << "##" << client->peerAddress().toString() << client->peerName() << client->peerPort(); qDebug() << client->socketType() << client->state(); + qDebug() << "Pending bytes: " << client->bytesAvailable(); QString tmp; switch (client->error()) { case QBluetoothSocket::NoSocketError: tmp += "NoSocketError"; break; -- cgit v1.2.3