1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/****************************************************************************
**
** 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 <QMap>
#include <QTextStream>
#include <qbluetoothtransferrequest.h>
#include <qbluetoothtransfermanager.h>
#include <qbluetoothtransferreply.h>
#include <qbluetoothaddress.h>
#include <qbluetoothlocaldevice.h>
#include <qbluetoothdeviceinfo.h>
#include <qbluetoothserviceinfo.h>
#include <qbluetoothservicediscoveryagent.h>
/*
* Some tests require a Bluetooth device within the vincinity of the test
* machine executing this test. The tests require manual interaction
* as pairing and file transfer requests must be accepted.
* The remote device's address must be passed
* via the BT_TEST_DEVICE env variable. The remote device must be
* discoverable and the object push service must be accessible. Any
**/
QT_USE_NAMESPACE
typedef QMap<QBluetoothTransferRequest::Attribute,QVariant> tst_QBluetoothTransferManager_QParameterMap;
Q_DECLARE_METATYPE(tst_QBluetoothTransferManager_QParameterMap)
static const int MaxConnectTime = 60 * 1000; // 1 minute in ms
class tst_QBluetoothTransferManager : public QObject
{
Q_OBJECT
public:
tst_QBluetoothTransferManager();
~tst_QBluetoothTransferManager();
private slots:
void initTestCase();
void tst_construction();
void tst_request_data();
void tst_request();
void tst_sendFile_data();
void tst_sendFile();
void tst_sendBuffer_data();
void tst_sendBuffer();
private:
QBluetoothAddress remoteAddress;
};
tst_QBluetoothTransferManager::tst_QBluetoothTransferManager()
{
qRegisterMetaType<QBluetoothTransferReply*>("QBluetoothTransferReply*");
}
tst_QBluetoothTransferManager::~tst_QBluetoothTransferManager()
{
}
void tst_QBluetoothTransferManager::initTestCase()
{
const QString remote = qgetenv("BT_TEST_DEVICE");
if (!remote.isEmpty()) {
remoteAddress = QBluetoothAddress(remote);
QVERIFY(!remoteAddress.isNull());
qWarning() << "Using remote device " << remote << " for testing. Ensure that the device is discoverable for pairing requests";
} else {
qWarning() << "Not using any remote device for testing. Set BT_TEST_DEVICE env to run manual tests involving a remote device";
QSKIP("Remote upload test not possible. Set BT_TEST_DEVICE");
}
if (!QBluetoothLocalDevice::allDevices().count())
QSKIP("Skipping test due to missing Bluetooth device");
// start Bluetooth if not started
QBluetoothLocalDevice *device = new QBluetoothLocalDevice();
device->powerOn();
delete device;
}
void tst_QBluetoothTransferManager::tst_construction()
{
QBluetoothTransferManager *manager = new QBluetoothTransferManager();
QVERIFY(manager);
delete manager;
}
void tst_QBluetoothTransferManager::tst_request_data()
{
QTest::addColumn<QBluetoothAddress>("address");
QTest::addColumn<QMap<QBluetoothTransferRequest::Attribute, QVariant> >("parameters");
QMap<QBluetoothTransferRequest::Attribute, QVariant> inparameters;
inparameters.insert(QBluetoothTransferRequest::DescriptionAttribute, "Description");
inparameters.insert(QBluetoothTransferRequest::LengthAttribute, QVariant(1024));
inparameters.insert(QBluetoothTransferRequest::TypeAttribute, "OPP");
inparameters.insert(QBluetoothTransferRequest::NameAttribute, "name");
inparameters.insert(QBluetoothTransferRequest::TimeAttribute, QDateTime::currentDateTime());
QTest::newRow("TESTDATA") << QBluetoothAddress("00:11:22:33:44:55:66") << inparameters;
}
void tst_QBluetoothTransferManager::tst_request()
{
QFETCH(QBluetoothAddress, address);
QFETCH(tst_QBluetoothTransferManager_QParameterMap, parameters);
QBluetoothTransferRequest transferRequest(address);
foreach (QBluetoothTransferRequest::Attribute key, parameters.keys())
QCOMPARE(transferRequest.attribute(key), QVariant());
foreach (QBluetoothTransferRequest::Attribute key, parameters.keys())
transferRequest.setAttribute((QBluetoothTransferRequest::Attribute)key, parameters[key]);
QCOMPARE(transferRequest.address(), address);
foreach (QBluetoothTransferRequest::Attribute key, parameters.keys())
QCOMPARE(transferRequest.attribute(key), parameters[key]);
//test copy constructor
QBluetoothTransferRequest constructorCopy = transferRequest;
QVERIFY(constructorCopy == transferRequest);
QVERIFY(!(constructorCopy != transferRequest));
QCOMPARE(constructorCopy.address(), address);
foreach (QBluetoothTransferRequest::Attribute key, parameters.keys())
QCOMPARE(constructorCopy.attribute(key), parameters[key]);
//test assignment operator
QBluetoothTransferRequest request;
QVERIFY(request.address().isNull());
foreach (QBluetoothTransferRequest::Attribute key, parameters.keys())
QCOMPARE(request.attribute(key), QVariant());
request = transferRequest;
QCOMPARE(request.address(), address);
foreach (QBluetoothTransferRequest::Attribute key, parameters.keys())
QCOMPARE(request.attribute(key), parameters[key]);
//test that it's a true and independent copy
constructorCopy.setAttribute(QBluetoothTransferRequest::DescriptionAttribute, "newDescription");
request.setAttribute(QBluetoothTransferRequest::TypeAttribute, "FTP");
QCOMPARE(constructorCopy.attribute(QBluetoothTransferRequest::DescriptionAttribute).toString(),QString("newDescription"));
QCOMPARE(request.attribute(QBluetoothTransferRequest::DescriptionAttribute).toString(),QString("Description"));
QCOMPARE(transferRequest.attribute(QBluetoothTransferRequest::DescriptionAttribute).toString(),QString("Description"));
QCOMPARE(constructorCopy.attribute(QBluetoothTransferRequest::TypeAttribute).toString(),QString("OPP"));
QCOMPARE(request.attribute(QBluetoothTransferRequest::TypeAttribute).toString(),QString("FTP"));
QCOMPARE(transferRequest.attribute(QBluetoothTransferRequest::TypeAttribute).toString(),QString("OPP"));
}
void tst_QBluetoothTransferManager::tst_sendFile_data()
{
QTest::addColumn<QBluetoothAddress>("deviceAddress");
QTest::addColumn<bool>("expectSuccess");
QTest::addColumn<bool>("isInvalidFile");
QTest::newRow("Push to remote test device") << remoteAddress << true << false;
QTest::newRow("Push of non-existing file") << remoteAddress << false << true;
QTest::newRow("Push to invalid address") << QBluetoothAddress() << false << false;
QTest::newRow("Push to non-existend device") << QBluetoothAddress("11:22:33:44:55:66") << false << false;
}
void tst_QBluetoothTransferManager::tst_sendFile()
{
QFETCH(QBluetoothAddress, deviceAddress);
QFETCH(bool, expectSuccess);
QFETCH(bool, isInvalidFile);
QBluetoothLocalDevice dev;
if (expectSuccess) {
dev.requestPairing(deviceAddress, QBluetoothLocalDevice::Paired);
QTest::qWait(5000);
QCOMPARE(dev.pairingStatus(deviceAddress), QBluetoothLocalDevice::Paired);
}
QBluetoothTransferRequest request(deviceAddress);
QCOMPARE(request.address(), deviceAddress);
QBluetoothTransferManager manager;
QString fileHandle;
if (!isInvalidFile) {
fileHandle = QFINDTESTDATA("testfile.txt");
QVERIFY(!fileHandle.isEmpty());
} else {
fileHandle = ("arbitraryFileName.txt"); //file doesn't exist
}
QFile f(fileHandle);
QCOMPARE(f.exists(), !isInvalidFile);
qDebug() << "Transferring file to " << deviceAddress.toString();
if (expectSuccess)
qDebug() << "Please accept Object push request on remote device";
QBluetoothTransferReply* reply = manager.put(request, &f);
QSignalSpy finishedSpy(reply, SIGNAL(finished(QBluetoothTransferReply*)));
QSignalSpy progressSpy(reply, SIGNAL(transferProgress(qint64,qint64)));
QCOMPARE(reply->request(), request);
QVERIFY(reply->manager() == &manager);
QVERIFY(!reply->isFinished());
QVERIFY(reply->isRunning());
const int maxWaitTime = 20 * 1000; //20s
for (int time = 0;
time<maxWaitTime && (finishedSpy.count()==0);
time+=1000) {
QTest::qWait(1000); //if interval
}
QVERIFY(finishedSpy.count()>0);
if (expectSuccess) {
QVERIFY(progressSpy.count()>0);
QCOMPARE(reply->error(), QBluetoothTransferReply::NoError);
QCOMPARE(reply->errorString(), QString());
} else {
QVERIFY(progressSpy.count() == 0);
if (isInvalidFile)
QVERIFY(reply->error() == QBluetoothTransferReply::FileNotFoundError);
else
QVERIFY(reply->error() != QBluetoothTransferReply::NoError);
QVERIFY(!reply->errorString().isEmpty());
}
QVERIFY(reply->isFinished());
QVERIFY(!reply->isRunning());
}
void tst_QBluetoothTransferManager::tst_sendBuffer_data()
{
QTest::addColumn<QBluetoothAddress>("deviceAddress");
QTest::addColumn<bool>("expectSuccess");
QTest::addColumn<QByteArray>("data");
QTest::newRow("Push to remote test device") << remoteAddress << true <<
QByteArray("This is a very long byte arry which we are going to access via a QBuffer"); ;
QTest::newRow("Push to invalid address") << QBluetoothAddress() << false << QByteArray("test");
QTest::newRow("Push to non-existend device") << QBluetoothAddress("11:22:33:44:55:66") << false << QByteArray("test");
}
void tst_QBluetoothTransferManager::tst_sendBuffer()
{
QFETCH(QBluetoothAddress, deviceAddress);
QFETCH(bool, expectSuccess);
QFETCH(QByteArray, data);
QBuffer buffer;
buffer.setData(data);
buffer.open(QIODevice::ReadOnly);
buffer.seek(0);
QBluetoothLocalDevice dev;
if (expectSuccess) {
dev.requestPairing(deviceAddress, QBluetoothLocalDevice::Paired);
QTest::qWait(2000);
QCOMPARE(dev.pairingStatus(deviceAddress), QBluetoothLocalDevice::Paired);
}
QBluetoothTransferRequest request(deviceAddress);
QCOMPARE(request.address(), deviceAddress);
QBluetoothTransferManager manager;
qDebug() << "Transferring test buffer to " << deviceAddress.toString();
if (expectSuccess)
qDebug() << "Please accept Object push request on remote device";
QBluetoothTransferReply* reply = manager.put(request, &buffer);
QSignalSpy finishedSpy(reply, SIGNAL(finished(QBluetoothTransferReply*)));
QSignalSpy progressSpy(reply, SIGNAL(transferProgress(qint64,qint64)));
QCOMPARE(reply->request(), request);
QVERIFY(reply->manager() == &manager);
QVERIFY(!reply->isFinished());
QVERIFY(reply->isRunning());
const int maxWaitTime = 20 * 1000; //20s
for (int time = 0;
time<maxWaitTime && (finishedSpy.count()==0);
time+=10000) {
QTest::qWait(10000); //if interval
}
QVERIFY(finishedSpy.count()>0);
if (expectSuccess) {
QVERIFY(progressSpy.count()>0);
QCOMPARE(reply->error(), QBluetoothTransferReply::NoError);
QCOMPARE(reply->errorString(), QString());
} else {
QVERIFY(progressSpy.count() == 0);
QVERIFY(reply->error() != QBluetoothTransferReply::NoError);
QVERIFY(!reply->errorString().isEmpty());
}
QVERIFY(reply->isFinished());
QVERIFY(!reply->isRunning());
}
QTEST_MAIN(tst_QBluetoothTransferManager)
#include "tst_qbluetoothtransfermanager.moc"
|