176 lines
5.6 KiB
C++
176 lines
5.6 KiB
C++
#include <QtTest/QtTest>
|
|
#include <QBuffer>
|
|
#include <QDir>
|
|
#include <QImage>
|
|
#include <QSettings>
|
|
#include <QSignalSpy>
|
|
#include <QTemporaryFile>
|
|
#include <QUrl>
|
|
|
|
#include <minizip/zip.h>
|
|
|
|
#include "backend/AppState.h"
|
|
#include "backend/FormatReader.h"
|
|
#include "backend/HistoryModel.h"
|
|
#include "backend/formats/FormatRegistry.h"
|
|
#include "backend/formats/sources/ZipByteSource.h"
|
|
|
|
static FormatRegistry makeRegistry()
|
|
{
|
|
return FormatRegistry({
|
|
FormatSpec::comic(QStringLiteral("cbz"), false, QStringLiteral("cbz"),
|
|
[] { return std::make_unique<ZipByteSource>(); }),
|
|
});
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Helpers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
static bool addEntry(zipFile zf, const char *name, const QByteArray &data)
|
|
{
|
|
zip_fileinfo fi{};
|
|
if (zipOpenNewFileInZip(zf, name, &fi,
|
|
nullptr, 0, nullptr, 0, nullptr,
|
|
Z_DEFLATED, Z_DEFAULT_COMPRESSION) != ZIP_OK)
|
|
return false;
|
|
if (zipWriteInFileInZip(zf, data.constData(), (unsigned)data.size()) != ZIP_OK) {
|
|
zipCloseFileInZip(zf);
|
|
return false;
|
|
}
|
|
return zipCloseFileInZip(zf) == ZIP_OK;
|
|
}
|
|
|
|
static QByteArray makePng(int w = 4, int h = 4, QRgb color = qRgb(255, 0, 0))
|
|
{
|
|
QImage img(w, h, QImage::Format_RGB32);
|
|
img.fill(color);
|
|
QByteArray data;
|
|
QBuffer buf(&data);
|
|
buf.open(QIODevice::WriteOnly);
|
|
img.save(&buf, "PNG");
|
|
return data;
|
|
}
|
|
|
|
static QString buildCbzWithCover()
|
|
{
|
|
static const char kComicInfo[] =
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
|
"<ComicInfo>"
|
|
" <Title>Cover Test Book</Title>"
|
|
" <Writer>Test Author</Writer>"
|
|
"</ComicInfo>";
|
|
|
|
QTemporaryFile tmp;
|
|
tmp.setFileTemplate(QDir::tempPath() + "/hbm_cover_test_XXXXXX.cbz");
|
|
tmp.setAutoRemove(false);
|
|
if (!tmp.open()) return {};
|
|
const QString path = tmp.fileName();
|
|
tmp.close();
|
|
|
|
zipFile zf = zipOpen(path.toUtf8().constData(), APPEND_STATUS_CREATE);
|
|
if (!zf) return {};
|
|
|
|
bool ok = true;
|
|
ok = ok && addEntry(zf, "ComicInfo.xml", QByteArray(kComicInfo));
|
|
ok = ok && addEntry(zf, "001_cover.png", makePng());
|
|
|
|
zipClose(zf, nullptr);
|
|
return ok ? path : QString{};
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test class
|
|
// ---------------------------------------------------------------------------
|
|
|
|
class HistoryModelTest : public QObject {
|
|
Q_OBJECT
|
|
|
|
private slots:
|
|
void addOrUpdateAndRoles()
|
|
{
|
|
FormatRegistry registry = makeRegistry();
|
|
HistoryModel model(registry);
|
|
|
|
QSignalSpy countSpy(&model, &HistoryModel::countChanged);
|
|
model.addOrUpdateBook(QStringLiteral("/path/to/book.cbz"), 0.42, QStringLiteral("2026-01-01"));
|
|
|
|
QCOMPARE(countSpy.count(), 1);
|
|
QCOMPARE(model.rowCount(), 1);
|
|
|
|
const QModelIndex idx = model.index(0, 0);
|
|
QCOMPARE(model.data(idx, HistoryModel::FilePathRole).toString(), QStringLiteral("/path/to/book.cbz"));
|
|
QCOMPARE(model.data(idx, HistoryModel::ProgressRole).toDouble(), 0.42);
|
|
QCOMPARE(model.data(idx, HistoryModel::LastOpenedRole).toString(), QStringLiteral("2026-01-01"));
|
|
QCOMPARE(model.data(idx, HistoryModel::FormatRole).toString(), QStringLiteral("CBZ"));
|
|
}
|
|
|
|
void loadMetadataPopulatesTitleAndCoverUrl()
|
|
{
|
|
const QString cbzPath = buildCbzWithCover();
|
|
QVERIFY(!cbzPath.isEmpty());
|
|
|
|
FormatRegistry registry = makeRegistry();
|
|
HistoryModel model(registry);
|
|
|
|
QSignalSpy dataSpy(&model, &QAbstractListModel::dataChanged);
|
|
model.addOrUpdateBook(cbzPath, 0.0);
|
|
|
|
QTRY_VERIFY_WITH_TIMEOUT(dataSpy.count() > 0, 5000);
|
|
|
|
QFile::remove(cbzPath);
|
|
|
|
const QModelIndex idx = model.index(0, 0);
|
|
QCOMPARE(model.data(idx, HistoryModel::TitleRole).toString(), QStringLiteral("Cover Test Book"));
|
|
QCOMPARE(model.data(idx, HistoryModel::AuthorRole).toString(), QStringLiteral("Test Author"));
|
|
|
|
const QString coverUrl = model.data(idx, HistoryModel::CoverImageUrlRole).toString();
|
|
QVERIFY(!coverUrl.isEmpty());
|
|
QVERIFY(coverUrl.startsWith(QStringLiteral("image://cbz/001_cover.png?src=")));
|
|
}
|
|
|
|
void removeBook()
|
|
{
|
|
FormatRegistry registry = makeRegistry();
|
|
HistoryModel model(registry);
|
|
|
|
model.addOrUpdateBook(QStringLiteral("/path/to/1.cbz"), 0.1);
|
|
model.addOrUpdateBook(QStringLiteral("/path/to/2.cbz"), 0.2);
|
|
QCOMPARE(model.rowCount(), 2);
|
|
|
|
model.removeBook(QStringLiteral("/path/to/1.cbz"));
|
|
QCOMPARE(model.rowCount(), 1);
|
|
QCOMPARE(model.filePath(0), QStringLiteral("/path/to/2.cbz"));
|
|
}
|
|
|
|
void loadAndSaveWithAppState()
|
|
{
|
|
QTemporaryFile f;
|
|
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/hist-test-XXXXXX.ini"));
|
|
(void)f.open(); f.close();
|
|
QSettings s(f.fileName(), QSettings::IniFormat);
|
|
AppState appState(s);
|
|
|
|
FormatRegistry registry = makeRegistry();
|
|
|
|
{
|
|
HistoryModel model(registry);
|
|
model.addOrUpdateBook(QStringLiteral("/path/to/book.cbz"), 0.75, QStringLiteral("2026-10-25"));
|
|
model.save(appState);
|
|
}
|
|
|
|
{
|
|
HistoryModel model2(registry);
|
|
model2.load(appState);
|
|
QCOMPARE(model2.rowCount(), 1);
|
|
QCOMPARE(model2.filePath(0), QStringLiteral("/path/to/book.cbz"));
|
|
QCOMPARE(model2.progress(0), 0.75);
|
|
QCOMPARE(model2.lastOpened(0), QStringLiteral("2026-10-25"));
|
|
}
|
|
}
|
|
};
|
|
|
|
QTEST_MAIN(HistoryModelTest)
|
|
#include "HistoryModelTest.moc"
|
|
|