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
|
// SPDX-FileCopyrightText: 2023 Filipe Azevedo <pasnox@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
// Qt
#include <QFile>
#include <QImage>
#include <QMimeDatabase>
#include <QMimeType>
#include <QSignalSpy>
#include <QTemporaryDir>
#include <QTest>
// Kaidan
#include "FileModel.h"
#include "FileProxyModel.h"
#include "Test.h"
class FileModelTest : public Test
{
Q_OBJECT
private Q_SLOTS:
void initTestCase() override
{
Test::initTestCase();
qRegisterMetaType<QPersistentModelIndex>();
qRegisterMetaType<QList<QPersistentModelIndex>>();
qRegisterMetaType<QAbstractItemModel::LayoutChangeHint>();
QVERIFY(m_dir.isValid());
}
void cleanupTestCase()
{
const QFileInfo fileInfo(m_dir.path());
m_dir.remove();
QVERIFY(!fileInfo.exists());
}
void testFileProxyModel()
{
FileModel model;
FileProxyModel proxy;
proxy.setSourceModel(&model);
// Everything is empty.
QCOMPARE(model.rowCount(), 0);
QCOMPARE(proxy.rowCount(), 0);
QCOMPARE(proxy.mode(), FileProxyModel::Mode::All);
// Create files backed by local or only remote files.
const auto localImage1 = createFile(QColor(Qt::red), true);
const auto localImage2 = createFile(QColor(Qt::green), true);
const auto localImage3 = createFile(QColor(Qt::blue), true);
const auto image4 = createFile(QColor(Qt::blue), false);
const auto localText1 = createFile(QStringLiteral("text1"), true);
const auto localText2 = createFile(QStringLiteral("text2"), true);
const auto localText3 = createFile(QStringLiteral("text3"), true);
const auto text4 = createFile(QStringLiteral("text4"), false);
QVERIFY(QFile::exists(localImage1.localFilePath));
QVERIFY(QFile::exists(localImage2.localFilePath));
QVERIFY(QFile::exists(localImage3.localFilePath));
QVERIFY(!QFile::exists(image4.localFilePath));
QVERIFY(QFile::exists(localText1.localFilePath));
QVERIFY(QFile::exists(localText2.localFilePath));
QVERIFY(QFile::exists(localText3.localFilePath));
QVERIFY(!QFile::exists(text4.localFilePath));
// Set up spies.
QSignalSpy modelAboutToBeReset(&model, &FileModel::modelAboutToBeReset);
QSignalSpy modelReset(&model, &FileModel::modelReset);
QSignalSpy proxyAboutToBeReset(&proxy, &FileProxyModel::modelAboutToBeReset);
QSignalSpy proxyReset(&proxy, &FileProxyModel::modelReset);
QSignalSpy proxyLayoutAboutToBeChanged(&proxy, &FileProxyModel::layoutAboutToBeChanged);
QSignalSpy proxyLayoutChanged(&proxy, &FileProxyModel::layoutChanged);
QSignalSpy proxyCheckedCountChanged(&proxy, &FileProxyModel::checkedCountChanged);
QSignalSpy proxyFilesDeleted(&proxy, &FileProxyModel::filesDeleted);
const auto clearSpies = [&]() {
modelAboutToBeReset.clear();
modelReset.clear();
proxyAboutToBeReset.clear();
proxyReset.clear();
proxyLayoutAboutToBeChanged.clear();
proxyLayoutChanged.clear();
proxyCheckedCountChanged.clear();
proxyFilesDeleted.clear();
};
// Set files.
model.setFiles({localImage1, localImage2, image4, localImage3, localText1, localText2, text4, localText3});
// Check emitted signals and counts.
QVERIFY(modelAboutToBeReset.count() == 1 || modelAboutToBeReset.wait());
QVERIFY(modelReset.count() == 1 || modelReset.wait());
QVERIFY(proxyAboutToBeReset.count() == 1 || proxyAboutToBeReset.wait());
QVERIFY(proxyReset.count() == 1 || proxyReset.wait());
QCOMPARE(model.rowCount(), 8);
// Not downloaded or manually deleted files are not counted.
QCOMPARE(proxy.rowCount(), 6);
clearSpies();
// Check "Images" count.
proxy.setMode(FileProxyModel::Mode::Images);
QCOMPARE(proxy.rowCount(), 3);
clearSpies();
// Check "Videos" count.
proxy.setMode(FileProxyModel::Mode::Videos);
QCOMPARE(proxy.rowCount(), 0);
clearSpies();
// Check "Other" count.
proxy.setMode(FileProxyModel::Mode::Other);
QCOMPARE(proxy.rowCount(), 3);
clearSpies();
// Check "All" count.
proxy.setMode(FileProxyModel::Mode::All);
QCOMPARE(proxy.rowCount(), 6);
clearSpies();
const QModelIndex localImage1Index = proxy.index(0, 0);
const QModelIndex localText2Index = proxy.index(5, 0);
// Check some files for deletion.
QVERIFY(proxy.setData(localImage1Index, Qt::Checked, Qt::CheckStateRole));
QVERIFY(proxy.setData(localText2Index, Qt::Checked, Qt::CheckStateRole));
QVERIFY(proxyCheckedCountChanged.count() == 2 || proxyCheckedCountChanged.wait());
QVERIFY(proxyCheckedCountChanged.count() == 2);
QCOMPARE(proxy.checkedCount(), 2);
clearSpies();
// Delete checked files.
proxy.deleteChecked();
QVERIFY(proxyCheckedCountChanged.count() == 1 || proxyCheckedCountChanged.wait());
QVERIFY(proxyFilesDeleted.count() == 1 || proxyFilesDeleted.wait());
QCOMPARE(proxy.checkedCount(), 0);
// Not downloaded or manually deleted files are not counted.
QCOMPARE(proxy.rowCount(), 4);
clearSpies();
}
private:
QString filePath(const QColor &color) const
{
return m_dir.filePath(QStringLiteral("%1.png").arg(color.name().remove(QStringLiteral("#"))));
}
QString filePath(const QString &content) const
{
return m_dir.filePath(QStringLiteral("%1.txt").arg(qChecksum(QByteArrayView(content.toUtf8().constData(), content.length()))));
}
QImage createImage(const QColor &color) const
{
const QFileInfo fileInfo(filePath(color));
if (fileInfo.exists()) {
return QImage(fileInfo.filePath());
}
QImage img(100, 100, QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::red);
Q_ASSERT(img.save(fileInfo.filePath()));
return img;
}
QByteArray createText(const QString &content) const
{
QFile file(filePath(content));
if (file.exists()) {
if (file.open(QIODevice::ReadOnly)) {
return file.readAll();
}
Q_UNREACHABLE();
}
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
if (file.write(content.toUtf8()) == -1) {
file.remove();
Q_UNREACHABLE();
}
}
return content.toUtf8();
}
File createFile(const QColor &color, bool local) const
{
if (local) {
createImage(color);
}
const QFileInfo fileInfo(filePath(color));
File file;
file.id = qChecksum(QByteArrayView(fileInfo.fileName().toUtf8().constData(), fileInfo.fileName().length()));
file.fileGroupId = file.id - 10;
file.name = fileInfo.baseName();
file.mimeType = m_db.mimeTypeForFile(fileInfo);
file.size = fileInfo.size();
file.lastModified = fileInfo.lastModified();
file.localFilePath = local ? fileInfo.filePath() : QString();
file.httpSources.append({file.id, QUrl(QStringLiteral("http://kaidan.im/colors/%1").arg(fileInfo.fileName()))});
return file;
}
File createFile(const QString &content, bool local) const
{
if (local) {
createText(content);
}
const QFileInfo fileInfo(filePath(content));
File file;
file.id = qChecksum(QByteArrayView(fileInfo.fileName().toUtf8().constData(), fileInfo.fileName().length()));
file.fileGroupId = file.id - 10;
file.name = fileInfo.baseName();
file.mimeType = m_db.mimeTypeForFile(fileInfo);
file.size = fileInfo.size();
file.lastModified = fileInfo.lastModified();
file.localFilePath = local ? fileInfo.filePath() : QString();
file.httpSources.append({file.id, QUrl(QStringLiteral("http://kaidan.im/texts/%1").arg(fileInfo.fileName()))});
return file;
}
private:
QMimeDatabase m_db;
QTemporaryDir m_dir;
};
QTEST_GUILESS_MAIN(FileModelTest)
#include "FileModelTest.moc"
|