2021-09-26 04:19:02 +08:00
|
|
|
/*
|
|
|
|
Copyright © 2021 by The qTox Project Contributors
|
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
qTox 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "src/widget/form/filesform.h"
|
|
|
|
#include "src/friendlist.h"
|
|
|
|
#include "src/model/friend.h"
|
|
|
|
|
|
|
|
#include <QTest>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
class TestFileTransferList : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
private slots:
|
2022-03-29 18:42:50 +08:00
|
|
|
void init();
|
2021-09-26 04:19:02 +08:00
|
|
|
void testFileTransferListConversion();
|
|
|
|
void testEditorActionConversion();
|
|
|
|
|
|
|
|
void testFileName();
|
|
|
|
// NOTE: Testing contact return requires a lookup in FriendList which goes
|
|
|
|
// down a large dependency chain that is not linked to this test
|
|
|
|
// void testContact();
|
|
|
|
void testProgress();
|
|
|
|
void testSize();
|
|
|
|
void testSpeed();
|
|
|
|
void testStatus();
|
|
|
|
void testControl();
|
|
|
|
void testAvatarIgnored();
|
|
|
|
void testMultipleFiles();
|
|
|
|
void testFileRemoval();
|
2022-03-29 18:42:50 +08:00
|
|
|
private:
|
|
|
|
std::unique_ptr<FileTransferList::Model> model;
|
|
|
|
std::unique_ptr<FriendList> friendList;
|
2021-09-26 04:19:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
using namespace FileTransferList;
|
|
|
|
|
2022-03-29 18:42:50 +08:00
|
|
|
void TestFileTransferList::init()
|
|
|
|
{
|
|
|
|
friendList = std::unique_ptr<FriendList>(new FriendList());
|
|
|
|
model = std::unique_ptr<Model>(new Model(*friendList));
|
|
|
|
}
|
|
|
|
|
2021-09-26 04:19:02 +08:00
|
|
|
void TestFileTransferList::testFileTransferListConversion()
|
|
|
|
{
|
2022-03-29 18:42:50 +08:00
|
|
|
for (int i = 0; i < model->columnCount(); ++i) {
|
2021-09-26 04:19:02 +08:00
|
|
|
QVERIFY(toFileTransferListColumn(i) != Column::invalid);
|
|
|
|
}
|
|
|
|
QCOMPARE(toFileTransferListColumn(100), Column::invalid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestFileTransferList::testEditorActionConversion()
|
|
|
|
{
|
|
|
|
QCOMPARE(toEditorAction(static_cast<int>(EditorAction::pause)), EditorAction::pause);
|
|
|
|
QCOMPARE(toEditorAction(static_cast<int>(EditorAction::cancel)), EditorAction::cancel);
|
|
|
|
QCOMPARE(toEditorAction(55), EditorAction::invalid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestFileTransferList::testFileName()
|
|
|
|
{
|
|
|
|
ToxFile file;
|
|
|
|
file.fileKind = TOX_FILE_KIND_DATA;
|
|
|
|
file.fileName = "Test";
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
2022-03-29 18:42:50 +08:00
|
|
|
const auto idx = model->index(0, static_cast<int>(Column::fileName));
|
2021-09-26 04:19:02 +08:00
|
|
|
const auto fileName = idx.data();
|
|
|
|
|
|
|
|
QCOMPARE(fileName.toString(), QString("Test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestFileTransferList::testProgress()
|
|
|
|
{
|
|
|
|
ToxFile file(0, 0, "", "", 1000, ToxFile::FileDirection::SENDING);
|
|
|
|
file.progress.addSample(100, QTime(1, 0, 0));
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
2022-03-29 18:42:50 +08:00
|
|
|
const auto idx = model->index(0, static_cast<int>(Column::progress));
|
2021-09-26 04:19:02 +08:00
|
|
|
const auto progress = idx.data();
|
|
|
|
|
|
|
|
// Progress should be in percent units, 100/1000 == 10
|
|
|
|
QCOMPARE(progress.toFloat(), 10.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestFileTransferList::testSize()
|
|
|
|
{
|
|
|
|
ToxFile file(0, 0, "", "", 1000, ToxFile::FileDirection::SENDING);
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
2022-03-29 18:42:50 +08:00
|
|
|
const auto idx = model->index(0, static_cast<int>(Column::size));
|
2021-09-26 04:19:02 +08:00
|
|
|
auto size = idx.data();
|
|
|
|
|
|
|
|
// Size should be a human readable string
|
|
|
|
QCOMPARE(size.toString(), QString("1000B"));
|
|
|
|
|
|
|
|
// 1GB + a little to avoid floating point inaccuracy
|
|
|
|
file = ToxFile(0, 0, "", "", 1024 * 1024 * 1024 + 2, ToxFile::FileDirection::SENDING);
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
size = idx.data();
|
|
|
|
QCOMPARE(size.toString(), QString("1.00GiB"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestFileTransferList::testSpeed()
|
|
|
|
{
|
|
|
|
ToxFile file(0, 0, "", "", 1024 * 1024, ToxFile::FileDirection::SENDING);
|
|
|
|
file.progress.addSample(100 * 1024, QTime(1, 0, 0));
|
|
|
|
file.progress.addSample(200 * 1024, QTime(1, 0, 1));
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
2022-03-29 18:42:50 +08:00
|
|
|
const auto idx = model->index(0, static_cast<int>(Column::speed));
|
2021-09-26 04:19:02 +08:00
|
|
|
const auto speed = idx.data();
|
|
|
|
|
|
|
|
// Speed should be a human readable string
|
|
|
|
QCOMPARE(speed.toString(), QString("100KiB/s"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestFileTransferList::testStatus()
|
|
|
|
{
|
|
|
|
ToxFile file(0, 0, "", "", 1024 * 1024, ToxFile::FileDirection::SENDING);
|
|
|
|
file.status = ToxFile::TRANSMITTING;
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
2022-03-29 18:42:50 +08:00
|
|
|
const auto idx = model->index(0, static_cast<int>(Column::status));
|
2021-09-26 04:19:02 +08:00
|
|
|
auto status = idx.data();
|
|
|
|
|
|
|
|
QCOMPARE(status.toString(), QString("Transmitting"));
|
|
|
|
|
|
|
|
file.status = ToxFile::PAUSED;
|
|
|
|
file.pauseStatus.remotePause();
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
status = idx.data();
|
|
|
|
|
|
|
|
QCOMPARE(status.toString(), QString("Remote paused"));
|
|
|
|
|
|
|
|
file.status = ToxFile::PAUSED;
|
|
|
|
file.pauseStatus.localPause();
|
|
|
|
file.pauseStatus.remoteResume();
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
status = idx.data();
|
|
|
|
|
|
|
|
QCOMPARE(status.toString(), QString("Paused"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestFileTransferList::testControl()
|
|
|
|
{
|
|
|
|
bool cancelCalled = false;
|
|
|
|
bool pauseCalled = false;
|
|
|
|
|
2022-03-29 18:42:50 +08:00
|
|
|
QObject::connect(model.get(), &Model::cancel, [&] (ToxFile file) {
|
2022-03-11 15:53:13 +08:00
|
|
|
std::ignore = file;
|
2021-09-26 04:19:02 +08:00
|
|
|
cancelCalled = true;
|
|
|
|
});
|
|
|
|
|
2022-03-29 18:42:50 +08:00
|
|
|
QObject::connect(model.get(), &Model::togglePause, [&] (ToxFile file) {
|
2022-03-11 15:53:13 +08:00
|
|
|
std::ignore = file;
|
2021-09-26 04:19:02 +08:00
|
|
|
pauseCalled = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
ToxFile file(0, 0, "", "", 1024 * 1024, ToxFile::FileDirection::SENDING);
|
|
|
|
file.status = ToxFile::TRANSMITTING;
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
2022-03-29 18:42:50 +08:00
|
|
|
const auto idx = model->index(0, static_cast<int>(Column::control));
|
|
|
|
model->setData(idx, static_cast<int>(EditorAction::pause));
|
2021-09-26 04:19:02 +08:00
|
|
|
|
|
|
|
QVERIFY(pauseCalled);
|
|
|
|
QVERIFY(!cancelCalled);
|
|
|
|
|
|
|
|
pauseCalled = false;
|
2022-03-29 18:42:50 +08:00
|
|
|
model->setData(idx, static_cast<int>(EditorAction::cancel));
|
2021-09-26 04:19:02 +08:00
|
|
|
QVERIFY(!pauseCalled);
|
|
|
|
QVERIFY(cancelCalled);
|
|
|
|
|
|
|
|
file.status = ToxFile::TRANSMITTING;
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
// True if paused
|
|
|
|
QCOMPARE(idx.data().toBool(), false);
|
|
|
|
|
|
|
|
file.status = ToxFile::PAUSED;
|
|
|
|
file.pauseStatus.localPause();
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
// True if _local_ paused
|
|
|
|
QCOMPARE(idx.data().toBool(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestFileTransferList::testAvatarIgnored()
|
|
|
|
{
|
|
|
|
ToxFile file;
|
|
|
|
file.fileKind = TOX_FILE_KIND_AVATAR;
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
2022-03-29 18:42:50 +08:00
|
|
|
QCOMPARE(model->rowCount(), 0);
|
2021-09-26 04:19:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestFileTransferList::testMultipleFiles()
|
|
|
|
{
|
|
|
|
ToxFile file;
|
|
|
|
file.resumeFileId = QByteArray();
|
|
|
|
file.fileKind = TOX_FILE_KIND_DATA;
|
|
|
|
file.status = ToxFile::TRANSMITTING;
|
|
|
|
file.fileName = "a";
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
|
|
|
// File map keys off resume file ID
|
|
|
|
file.resumeFileId = QByteArray("asdfasdf");
|
|
|
|
file.fileName = "b";
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
2022-03-29 18:42:50 +08:00
|
|
|
QCOMPARE(model->rowCount(), 2);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
2022-03-29 18:42:50 +08:00
|
|
|
auto idx = model->index(0, static_cast<int>(Column::fileName));
|
2021-09-26 04:19:02 +08:00
|
|
|
QCOMPARE(idx.data().toString(), QString("a"));
|
|
|
|
|
2022-03-29 18:42:50 +08:00
|
|
|
idx = model->index(1, static_cast<int>(Column::fileName));
|
2021-09-26 04:19:02 +08:00
|
|
|
QCOMPARE(idx.data().toString(), QString("b"));
|
|
|
|
|
|
|
|
// File name should be updated instead of inserting a new file since the
|
|
|
|
// resume file ID is the same
|
|
|
|
file.fileName = "c";
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
|
|
|
QCOMPARE(model->rowCount(), 2);
|
2021-09-26 04:19:02 +08:00
|
|
|
QCOMPARE(idx.data().toString(), QString("c"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestFileTransferList::testFileRemoval()
|
|
|
|
{
|
|
|
|
// Model should keep files in the list if they are finished, but not if they
|
|
|
|
// were broken or canceled
|
|
|
|
|
|
|
|
ToxFile file;
|
|
|
|
file.fileKind = TOX_FILE_KIND_DATA;
|
|
|
|
file.status = ToxFile::TRANSMITTING;
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
2022-03-29 18:42:50 +08:00
|
|
|
QCOMPARE(model->rowCount(), 1);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
|
|
|
file.status = ToxFile::BROKEN;
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
|
|
|
QCOMPARE(model->rowCount(), 0);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
|
|
|
file.status = ToxFile::TRANSMITTING;
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
|
|
|
QCOMPARE(model->rowCount(), 1);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
|
|
|
file.status = ToxFile::CANCELED;
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
|
|
|
QCOMPARE(model->rowCount(), 0);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
|
|
|
file.status = ToxFile::TRANSMITTING;
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
|
|
|
QCOMPARE(model->rowCount(), 1);
|
2021-09-26 04:19:02 +08:00
|
|
|
|
|
|
|
file.status = ToxFile::FINISHED;
|
2022-03-29 18:42:50 +08:00
|
|
|
model->onFileUpdated(file);
|
|
|
|
QCOMPARE(model->rowCount(), 1);
|
2021-09-26 04:19:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(TestFileTransferList)
|
|
|
|
#include "filesform_test.moc"
|