2015-10-19 05:48:39 +01:00
|
|
|
/*************************************************************************************
|
|
|
|
* Copyright (C) 2014 by Albert Vaca Cintora <albertvaka@gmail.com> *
|
|
|
|
* *
|
|
|
|
* This library is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU Lesser General Public *
|
|
|
|
* License as published by the Free Software Foundation; either *
|
|
|
|
* version 2.1 of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This library is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
|
|
|
|
* Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public *
|
|
|
|
* License along with this library; if not, write to the Free Software *
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
|
|
|
|
*************************************************************************************/
|
|
|
|
|
|
|
|
#include "../core/backends/lan/downloadjob.h"
|
|
|
|
|
|
|
|
#include <QTest>
|
|
|
|
#include <QTcpServer>
|
|
|
|
#include <QTcpSocket>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QHostAddress>
|
|
|
|
#include <KJob>
|
2016-06-22 11:40:14 +01:00
|
|
|
#include <QSignalSpy>
|
2015-10-19 05:48:39 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
class DownloadJobTest : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void failToConnectShouldDestroyTheJob();
|
|
|
|
void closingTheConnectionShouldDestroyTheJob();
|
|
|
|
|
2016-06-22 11:40:14 +01:00
|
|
|
private:
|
2015-10-19 05:48:39 +01:00
|
|
|
void initServer();
|
|
|
|
void initDownloadJob();
|
|
|
|
void awaitToBeDestroyedOrTimeOut();
|
|
|
|
void stopServer();
|
|
|
|
|
2016-06-22 11:40:14 +01:00
|
|
|
QPointer<DownloadJob> test;
|
|
|
|
QPointer<QTcpServer> mServer;
|
2015-10-19 05:48:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void DownloadJobTest::initServer()
|
|
|
|
{
|
2016-06-22 11:40:14 +01:00
|
|
|
delete mServer;
|
2015-10-19 05:48:39 +01:00
|
|
|
mServer = new QTcpServer(this);
|
|
|
|
QVERIFY2(mServer->listen(QHostAddress::LocalHost, 8694), "Failed to create local tcp server");
|
|
|
|
}
|
|
|
|
|
2016-06-22 11:40:14 +01:00
|
|
|
void DownloadJobTest::failToConnectShouldDestroyTheJob()
|
2015-10-19 05:48:39 +01:00
|
|
|
{
|
2016-06-22 11:40:14 +01:00
|
|
|
// no initServer
|
|
|
|
test = new DownloadJob(QHostAddress::LocalHost, {{"port", 8694}});
|
2015-10-19 05:48:39 +01:00
|
|
|
|
2017-01-15 21:12:49 +00:00
|
|
|
QSignalSpy spy(test.data(), &KJob::finished);
|
2015-10-19 05:48:39 +01:00
|
|
|
test->start();
|
|
|
|
|
2016-06-22 11:40:14 +01:00
|
|
|
QVERIFY(spy.count() || spy.wait());
|
2015-10-19 05:48:39 +01:00
|
|
|
|
2016-06-22 11:40:14 +01:00
|
|
|
QCOMPARE(test->error(), 1);
|
2015-10-19 05:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadJobTest::closingTheConnectionShouldDestroyTheJob()
|
|
|
|
{
|
|
|
|
initServer();
|
2016-06-22 11:40:14 +01:00
|
|
|
test = new DownloadJob(QHostAddress::LocalHost, {{"port", 8694}});
|
2017-01-15 21:12:49 +00:00
|
|
|
QSignalSpy spy(test.data(), &KJob::finished);
|
2016-06-22 11:40:14 +01:00
|
|
|
test->start();
|
|
|
|
mServer->close();
|
|
|
|
|
|
|
|
QVERIFY(spy.count() || spy.wait(2000));
|
2015-10-19 05:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(DownloadJobTest)
|
|
|
|
|
|
|
|
#include "downloadjobtest.moc"
|