Upgrade to Qt 6.10.3, refactor AppState for LibraryModel integration, add ChunkMeasurer tests, and update CI/docker configurations.

This commit is contained in:
Agel committed 2026-04-03 13:55:28 +02:00
1 parent 0303107fa2
commit a31b20534a
53 files changed
+2095 -880

No files matched your search

+107 -186
View File
@@ -2,6 +2,7 @@
#include <QSignalSpy>
#include <QSettings>
#include "backend/AppState.h"
#include "backend/LibraryModel.h"
class AppStateTest : public QObject {
Q_OBJECT
@@ -30,54 +31,6 @@ private slots:
QCOMPARE(state2.lastFilePath(), QStringLiteral("/books/test.epub"));
}
void saveAndRestoreLastChapter()
{
{
AppState state;
state.setLastChapter(5);
state.save();
}
AppState state2;
state2.restore();
QCOMPARE(state2.lastChapter(), 5);
}
void saveAndRestoreLastScrollPosition()
{
{
AppState state;
state.setLastScrollPosition(542.5);
state.save();
}
AppState state2;
state2.restore();
QCOMPARE(state2.lastScrollPosition(), 542.5);
}
void saveAndRestoreLastScrollIndex()
{
{
AppState state;
state.setLastScrollIndex(42);
state.save();
}
AppState state2;
state2.restore();
QCOMPARE(state2.lastScrollIndex(), 42);
}
void saveAndRestoreLastScrollOffset()
{
{
AppState state;
state.setLastScrollOffset(123.75);
state.save();
}
AppState state2;
state2.restore();
QCOMPARE(state2.lastScrollOffset(), 123.75);
}
void saveAndRestoreMargins()
{
{
@@ -139,10 +92,6 @@ private slots:
AppState state;
state.restore();
QVERIFY(state.lastFilePath().isEmpty());
QCOMPARE(state.lastChapter(), 0);
QCOMPARE(state.lastScrollPosition(), 0.0);
QCOMPARE(state.lastScrollIndex(), 0);
QCOMPARE(state.lastScrollOffset(), 0.0);
QCOMPARE(state.horizontalMargin(), 32);
QCOMPARE(state.verticalMargin(), 8);
QCOMPARE(state.fontSize(), 16);
@@ -157,10 +106,6 @@ private slots:
{
AppState state;
QSignalSpy spyFilePath(&state, &AppState::lastFilePathChanged);
QSignalSpy spyChapter(&state, &AppState::lastChapterChanged);
QSignalSpy spyScroll(&state, &AppState::lastScrollPositionChanged);
QSignalSpy spyScrollIndex(&state, &AppState::lastScrollIndexChanged);
QSignalSpy spyScrollOffset(&state, &AppState::lastScrollOffsetChanged);
QSignalSpy spyHMargin(&state, &AppState::horizontalMarginChanged);
QSignalSpy spyVMargin(&state, &AppState::verticalMarginChanged);
QSignalSpy spyFontSize(&state, &AppState::fontSizeChanged);
@@ -171,10 +116,6 @@ private slots:
QSignalSpy spyY(&state, &AppState::windowYChanged);
state.setLastFilePath(QStringLiteral("/foo.txt"));
state.setLastChapter(3);
state.setLastScrollPosition(10.5);
state.setLastScrollIndex(7);
state.setLastScrollOffset(55.25);
state.setHorizontalMargin(64);
state.setVerticalMargin(24);
state.setFontSize(20);
@@ -185,10 +126,6 @@ private slots:
state.setWindowY(150);
QCOMPARE(spyFilePath.count(), 1);
QCOMPARE(spyChapter.count(), 1);
QCOMPARE(spyScroll.count(), 1);
QCOMPARE(spyScrollIndex.count(), 1);
QCOMPARE(spyScrollOffset.count(), 1);
QCOMPARE(spyHMargin.count(), 1);
QCOMPARE(spyVMargin.count(), 1);
QCOMPARE(spyFontSize.count(), 1);
@@ -202,49 +139,41 @@ private slots:
void saveAndLoadBookPosition()
{
AppState state;
state.saveBookPosition(QStringLiteral("/books/a.epub"), 3, 42, 123.5);
state.saveBookPosition(QStringLiteral("/books/a.epub"), 5, 120);
QVariantMap pos = state.loadBookPosition(QStringLiteral("/books/a.epub"));
QCOMPARE(pos["chapter"].toInt(), 3);
QCOMPARE(pos["scrollIndex"].toInt(), 42);
QCOMPARE(pos["scrollOffset"].toDouble(), 123.5);
QCOMPARE(pos[QStringLiteral("chunkIndex")].toInt(), 5);
QCOMPARE(pos[QStringLiteral("intraChunkOffset")].toInt(), 120);
}
void loadBookPositionForUnknownBook()
{
AppState state;
QVariantMap pos = state.loadBookPosition(QStringLiteral("/books/unknown.epub"));
QCOMPARE(pos["chapter"].toInt(), 0);
QCOMPARE(pos["scrollIndex"].toInt(), 0);
QCOMPARE(pos["scrollOffset"].toDouble(), 0.0);
QCOMPARE(pos[QStringLiteral("chunkIndex")].toInt(), 0);
QCOMPARE(pos[QStringLiteral("intraChunkOffset")].toInt(), 0);
}
void saveBookPositionForMultipleBooks()
{
AppState state;
state.saveBookPosition(QStringLiteral("/books/a.epub"), 1, 10, 50.0);
state.saveBookPosition(QStringLiteral("/books/b.epub"), 5, 99, 200.5);
state.saveBookPosition(QStringLiteral("/books/a.epub"), 10, 50);
state.saveBookPosition(QStringLiteral("/books/b.epub"), 20, 200);
QVariantMap posA = state.loadBookPosition(QStringLiteral("/books/a.epub"));
QVariantMap posB = state.loadBookPosition(QStringLiteral("/books/b.epub"));
QCOMPARE(posA["chapter"].toInt(), 1);
QCOMPARE(posA["scrollIndex"].toInt(), 10);
QCOMPARE(posB["chapter"].toInt(), 5);
QCOMPARE(posB["scrollIndex"].toInt(), 99);
QCOMPARE(posB["scrollOffset"].toDouble(), 200.5);
QCOMPARE(posA[QStringLiteral("chunkIndex")].toInt(), 10);
QCOMPARE(posA[QStringLiteral("intraChunkOffset")].toInt(), 50);
QCOMPARE(posB[QStringLiteral("chunkIndex")].toInt(), 20);
QCOMPARE(posB[QStringLiteral("intraChunkOffset")].toInt(), 200);
}
void saveBookPositionOverwritesPrevious()
{
AppState state;
state.saveBookPosition(QStringLiteral("/books/a.epub"), 1, 10, 50.0);
state.saveBookPosition(QStringLiteral("/books/a.epub"), 7, 33, 88.0);
state.saveBookPosition(QStringLiteral("/books/a.epub"), 10, 50);
state.saveBookPosition(QStringLiteral("/books/a.epub"), 15, 88);
QVariantMap pos = state.loadBookPosition(QStringLiteral("/books/a.epub"));
QCOMPARE(pos["chapter"].toInt(), 7);
QCOMPARE(pos["scrollIndex"].toInt(), 33);
QCOMPARE(pos["scrollOffset"].toDouble(), 88.0);
QCOMPARE(pos[QStringLiteral("chunkIndex")].toInt(), 15);
QCOMPARE(pos[QStringLiteral("intraChunkOffset")].toInt(), 88);
}
void restoreEmitsSingleSignal()
@@ -253,10 +182,6 @@ private slots:
QSignalSpy spyRestored(&state, &AppState::stateRestored);
QSignalSpy spyFilePath(&state, &AppState::lastFilePathChanged);
QSignalSpy spyChapter(&state, &AppState::lastChapterChanged);
QSignalSpy spyScroll(&state, &AppState::lastScrollPositionChanged);
QSignalSpy spyScrollIndex(&state, &AppState::lastScrollIndexChanged);
QSignalSpy spyScrollOffset(&state, &AppState::lastScrollOffsetChanged);
QSignalSpy spyHMargin(&state, &AppState::horizontalMarginChanged);
QSignalSpy spyVMargin(&state, &AppState::verticalMarginChanged);
QSignalSpy spyFontSize(&state, &AppState::fontSizeChanged);
@@ -271,10 +196,6 @@ private slots:
QCOMPARE(spyRestored.count(), 1);
QCOMPARE(spyFilePath.count(), 0);
QCOMPARE(spyChapter.count(), 0);
QCOMPARE(spyScroll.count(), 0);
QCOMPARE(spyScrollIndex.count(), 0);
QCOMPARE(spyScrollOffset.count(), 0);
QCOMPARE(spyHMargin.count(), 0);
QCOMPARE(spyVMargin.count(), 0);
QCOMPARE(spyFontSize.count(), 0);
@@ -343,153 +264,155 @@ private slots:
void libraryBooksInitiallyEmpty()
{
AppState state;
QVERIFY(state.libraryBooks().isEmpty());
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QVERIFY(model->rowCount() == 0);
}
void addLibraryBookAddsEntry()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
QStringLiteral("Title A"),
QStringLiteral("Author A"), 0.0);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.0);
QVariantList books = state.libraryBooks();
QCOMPARE(books.size(), 1);
QVariantMap book = books.first().toMap();
QCOMPARE(book[QStringLiteral("filePath")].toString(), QStringLiteral("/books/a.epub"));
QCOMPARE(book[QStringLiteral("title")].toString(), QStringLiteral("Title A"));
QCOMPARE(book[QStringLiteral("author")].toString(), QStringLiteral("Author A"));
QCOMPARE(book[QStringLiteral("progress")].toDouble(), 0.0);
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 1);
// Get data from the model
QModelIndex index = model->index(0);
QString filePath = model->data(index, LibraryModel::FilePathRole).toString();
double progress = model->data(index, LibraryModel::ProgressRole).toDouble();
QCOMPARE(filePath, QStringLiteral("/books/a.epub"));
QCOMPARE(progress, 0.0);
// Title and author are now loaded asynchronously from file metadata
// QString title = model->data(index, 2).toString(); // TitleRole = 2
// QString author = model->data(index, 3).toString(); // AuthorRole = 3
// QCOMPARE(title, QStringLiteral("Title A"));
// QCOMPARE(author, QStringLiteral("Author A"));
}
void addLibraryBookPersistsThroughSaveRestore()
{
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
QStringLiteral("Title A"),
QStringLiteral("Author A"), 0.5);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.5);
state.save();
}
AppState state2;
state2.restore();
QVariantList books = state2.libraryBooks();
QCOMPARE(books.size(), 1);
QVariantMap book = books.first().toMap();
QCOMPARE(book[QStringLiteral("filePath")].toString(), QStringLiteral("/books/a.epub"));
QCOMPARE(book[QStringLiteral("title")].toString(), QStringLiteral("Title A"));
QCOMPARE(book[QStringLiteral("progress")].toDouble(), 0.5);
auto* model = qobject_cast<QAbstractListModel*>(state2.libraryModel());
QCOMPARE(model->rowCount(), 1);
QModelIndex index = model->index(0);
QString filePath = model->data(index, LibraryModel::FilePathRole).toString();
double progress = model->data(index, LibraryModel::ProgressRole).toDouble();
QCOMPARE(filePath, QStringLiteral("/books/a.epub"));
// Title is now loaded asynchronously from file metadata
// QString title = model->data(index, 2).toString(); // TitleRole = 2
// QCOMPARE(title, QStringLiteral("Title A"));
QCOMPARE(progress, 0.5);
}
void addLibraryBookUpdatesMetadata()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
QStringLiteral("Old Title"),
QStringLiteral("Old Author"), 0.3);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
QStringLiteral("New Title"),
QStringLiteral("New Author"), 0.3);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.3);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.3);
QVariantList books = state.libraryBooks();
QCOMPARE(books.size(), 1);
QVariantMap book = books.first().toMap();
QCOMPARE(book[QStringLiteral("title")].toString(), QStringLiteral("New Title"));
QCOMPARE(book[QStringLiteral("author")].toString(), QStringLiteral("New Author"));
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 1);
QModelIndex index = model->index(0);
// Title and author are now loaded asynchronously from file metadata
// QString title = model->data(index, 2).toString(); // TitleRole = 2
// QString author = model->data(index, 3).toString(); // AuthorRole = 3
// QCOMPARE(title, QStringLiteral("New Title"));
// QCOMPARE(author, QStringLiteral("New Author"));
}
void addLibraryBookOverwritesProgress()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
QStringLiteral("Title A"),
QStringLiteral("Author A"), 0.6);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.6);
// Lower value should overwrite (always update, no max logic)
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
QStringLiteral("Title A"),
QStringLiteral("Author A"), 0.2);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.2);
QVariantList books = state.libraryBooks();
QCOMPARE(books.size(), 1);
QCOMPARE(books.first().toMap()[QStringLiteral("progress")].toDouble(), 0.2);
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 1);
QModelIndex index = model->index(0);
double progress = model->data(index, LibraryModel::ProgressRole).toDouble();
QCOMPARE(progress, 0.2);
}
void addLibraryBookSentinelPreservesProgress()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
QStringLiteral("Title A"),
QStringLiteral("Author A"), 0.5);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.5);
// -1.0 sentinel: update metadata but don't touch progress
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
QStringLiteral("New Title"),
QStringLiteral("Author A"), -1.0);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), -1.0);
QVariantList books = state.libraryBooks();
QCOMPARE(books.size(), 1);
QVariantMap book = books.first().toMap();
QCOMPARE(book[QStringLiteral("progress")].toDouble(), 0.5);
QCOMPARE(book[QStringLiteral("title")].toString(), QStringLiteral("New Title"));
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 1);
QModelIndex index = model->index(0);
double progress = model->data(index, LibraryModel::ProgressRole).toDouble();
QCOMPARE(progress, 0.5);
// Title is now loaded asynchronously from file metadata
// QString title = model->data(index, 2).toString(); // TitleRole = 2
// QCOMPARE(title, QStringLiteral("New Title"));
}
void addNewLibraryBookWithSentinelStartsAtZero()
{
AppState state;
// New book added with sentinel should start at 0%
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
QStringLiteral("Title A"),
QStringLiteral("Author A"), -1.0);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), -1.0);
QVariantList books = state.libraryBooks();
QCOMPARE(books.size(), 1);
QCOMPARE(books.first().toMap()[QStringLiteral("progress")].toDouble(), 0.0);
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 1);
QModelIndex index = model->index(0);
double progress = model->data(index, LibraryModel::ProgressRole).toDouble();
QCOMPARE(progress, 0.0);
}
void removeLibraryBookRemovesEntry()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
QStringLiteral("Title A"),
QStringLiteral("Author A"), 0.5);
state.addOrUpdateLibraryBook(QStringLiteral("/books/b.epub"),
QStringLiteral("Title B"),
QStringLiteral("Author B"), 0.3);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.5);
state.addOrUpdateLibraryBook(QStringLiteral("/books/b.epub"), 0.3);
state.removeLibraryBook(QStringLiteral("/books/a.epub"));
QVariantList books = state.libraryBooks();
QCOMPARE(books.size(), 1);
QCOMPARE(books.first().toMap()[QStringLiteral("filePath")].toString(),
QStringLiteral("/books/b.epub"));
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 1);
QModelIndex index = model->index(0);
QString filePath = model->data(index, LibraryModel::FilePathRole).toString();
QCOMPARE(filePath, QStringLiteral("/books/b.epub"));
}
void removeNonexistentLibraryBookIsNoop()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
QStringLiteral("Title A"),
QStringLiteral("Author A"), 0.5);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.5);
state.removeLibraryBook(QStringLiteral("/books/nonexistent.epub"));
QCOMPARE(state.libraryBooks().size(), 1);
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 1);
}
void libraryBooksChangedSignalOnAdd()
{
AppState state;
QSignalSpy spy(&state, &AppState::libraryBooksChanged);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
QStringLiteral("Title A"),
QStringLiteral("Author A"), 0.0);
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QSignalSpy spy(model, &QAbstractListModel::rowsInserted);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.0);
QCOMPARE(spy.count(), 1);
}
void libraryBooksChangedSignalOnRemove()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
QStringLiteral("Title A"),
QStringLiteral("Author A"), 0.0);
QSignalSpy spy(&state, &AppState::libraryBooksChanged);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.0);
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QSignalSpy spy(model, &QAbstractListModel::rowsRemoved);
state.removeLibraryBook(QStringLiteral("/books/a.epub"));
QCOMPARE(spy.count(), 1);
}
@@ -497,20 +420,18 @@ private slots:
void multipleLibraryBooksAreStoredInOrder()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
QStringLiteral("Title A"),
QStringLiteral("Author A"), 0.1);
state.addOrUpdateLibraryBook(QStringLiteral("/books/b.epub"),
QStringLiteral("Title B"),
QStringLiteral("Author B"), 0.2);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.1);
state.addOrUpdateLibraryBook(QStringLiteral("/books/b.epub"), 0.2);
QVariantList books = state.libraryBooks();
QCOMPARE(books.size(), 2);
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 2);
// Most recently added/updated comes first
QCOMPARE(books.at(0).toMap()[QStringLiteral("filePath")].toString(),
QStringLiteral("/books/b.epub"));
QCOMPARE(books.at(1).toMap()[QStringLiteral("filePath")].toString(),
QStringLiteral("/books/a.epub"));
QModelIndex index0 = model->index(0);
QModelIndex index1 = model->index(1);
QString filePath0 = model->data(index0, LibraryModel::FilePathRole).toString();
QString filePath1 = model->data(index1, LibraryModel::FilePathRole).toString();
QCOMPARE(filePath0, QStringLiteral("/books/b.epub"));
QCOMPARE(filePath1, QStringLiteral("/books/a.epub"));
}
};
+90 -3
View File
@@ -6,6 +6,7 @@
#include "backend/ReadingModel.h"
#include "backend/AppState.h"
#include "backend/IFileDialogService.h"
#include "backend/LibraryModel.h"
class FakeFileDialogService : public IFileDialogService {
Q_OBJECT
@@ -45,6 +46,7 @@ private slots:
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
QCOMPARE(readySpy.count(), 1);
QVERIFY(ctrl.hasBook());
@@ -57,6 +59,7 @@ private slots:
QSignalSpy errorSpy(&ctrl, &BookController::bookError);
ctrl.openBook(QUrl::fromLocalFile("/tmp/nonexistent_asteria_file.txt"));
QVERIFY(errorSpy.wait(5000));
QCOMPARE(errorSpy.count(), 1);
QVERIFY(!ctrl.errorMessage().isEmpty());
@@ -69,6 +72,7 @@ private slots:
QSignalSpy errorSpy(&ctrl, &BookController::bookError);
ctrl.openBook(QUrl::fromLocalFile("/tmp/file.xyz"));
QVERIFY(errorSpy.wait(5000));
QCOMPARE(errorSpy.count(), 1);
QCOMPARE(ctrl.errorMessage(), QStringLiteral("No format readers available"));
@@ -78,7 +82,9 @@ private slots:
void closeBook()
{
BookController ctrl;
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
QVERIFY(ctrl.hasBook());
QSignalSpy changedSpy(&ctrl, &BookController::bookChanged);
@@ -95,8 +101,10 @@ private slots:
BookController ctrl;
ReadingModel model;
ctrl.setReadingModel(&model);
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
QVERIFY(model.totalChapters() > 0);
}
@@ -105,10 +113,15 @@ private slots:
{
BookController ctrl;
QSignalSpy loadingSpy(&ctrl, &BookController::loadingChanged);
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
// loading goes true then false → 2 emissions
QCOMPARE(loadingSpy.count(), 1);
QVERIFY(ctrl.loading());
QVERIFY(readySpy.wait(5000));
QCOMPARE(loadingSpy.count(), 2);
// After completion, loading should be false
QVERIFY(!ctrl.loading());
@@ -123,7 +136,9 @@ private slots:
void currentFilePathSetOnOpen()
{
BookController ctrl;
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
QCOMPARE(ctrl.currentFilePath(), fixturePath("sample.txt"));
}
@@ -138,7 +153,9 @@ private slots:
void clearErrorResetsMessage()
{
BookController ctrl;
QSignalSpy errorSpy(&ctrl, &BookController::bookError);
ctrl.openBook(QUrl::fromLocalFile("/tmp/file.xyz"));
QVERIFY(errorSpy.wait(5000));
QVERIFY(!ctrl.errorMessage().isEmpty());
QSignalSpy spy(&ctrl, &BookController::errorMessageChanged);
@@ -155,8 +172,10 @@ private slots:
BookController ctrl;
ctrl.setReadingModel(&model);
ctrl.setAppState(&appState);
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
QVERIFY(ctrl.hasBook());
QString firstPath = ctrl.currentFilePath();
@@ -164,10 +183,10 @@ private slots:
// regardless of whether the second open succeeds
ctrl.openBook(QUrl::fromLocalFile("/tmp/file.xyz"));
// Position for sample.txt should have been saved (chapter 0 at minimum)
// Position for sample.txt should have been saved (0 at minimum)
QVariantMap pos = appState.loadBookPosition(firstPath);
QVERIFY(pos.contains("chapter"));
QCOMPARE(pos["chapter"].toInt(), 0);
QCOMPARE(pos[QStringLiteral("chunkIndex")].toInt(), 0);
QCOMPARE(pos[QStringLiteral("intraChunkOffset")].toInt(), 0);
}
// IFileDialogService tests
@@ -191,6 +210,7 @@ private slots:
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
svc.simulateSelection(fixturePath("sample.txt"));
QVERIFY(readySpy.wait(5000));
QCOMPARE(readySpy.count(), 1);
QVERIFY(ctrl.hasBook());
@@ -203,6 +223,73 @@ private slots:
ctrl.browseAndOpen();
QVERIFY(!ctrl.hasBook());
}
void coverImageUrlInitiallyEmpty()
{
BookController ctrl;
QVERIFY(ctrl.coverImageUrl().isEmpty());
}
void txtBookHasNoCoverImage()
{
BookController ctrl;
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
QVERIFY(ctrl.hasBook());
QVERIFY(ctrl.coverImageUrl().isEmpty());
}
void coverImageUrlClearedOnClose()
{
BookController ctrl;
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
ctrl.closeBook();
QVERIFY(ctrl.coverImageUrl().isEmpty());
}
void openBookUpdatesLibrary()
{
AppState appState;
ReadingModel model;
BookController ctrl;
ctrl.setReadingModel(&model);
ctrl.setAppState(&appState);
auto* libraryModel = qobject_cast<QAbstractListModel*>(appState.libraryModel());
QCOMPARE(libraryModel->rowCount(), 0);
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
// First book open does NOT update the library (only previous reader is saved)
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
QVERIFY(ctrl.hasBook());
QCOMPARE(libraryModel->rowCount(), 0);
// Closing the book should save it to library
ctrl.closeBook();
QCOMPARE(libraryModel->rowCount(), 1);
QCOMPARE(libraryModel->data(libraryModel->index(0), LibraryModel::FilePathRole).toString(),
fixturePath("sample.txt"));
}
void closeBookUpdatesLibrary()
{
AppState appState;
ReadingModel model;
BookController ctrl;
ctrl.setReadingModel(&model);
ctrl.setAppState(&appState);
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(readySpy.wait(5000));
ctrl.closeBook();
auto* libraryModel = qobject_cast<QAbstractListModel*>(appState.libraryModel());
QCOMPARE(libraryModel->rowCount(), 1);
QCOMPARE(libraryModel->data(libraryModel->index(0), LibraryModel::FilePathRole).toString(),
fixturePath("sample.txt"));
}
};
QTEST_MAIN(BookControllerTest)
+81
View File
@@ -0,0 +1,81 @@
#include <QtTest>
#include "backend/ChunkMeasurer.h"
#include "backend/IImageLoader.h"
// A mock image loader that returns a solid-color image of a fixed size
class FixedSizeImageLoader : public IImageLoader {
QSize m_size;
public:
explicit FixedSizeImageLoader(const QSize &size) : m_size(size) {}
QImage load(const QUrl &, int maxWidth) const override {
int w = qMin(m_size.width(), maxWidth);
int h = m_size.height();
if (w <= 0 || h <= 0) return {};
QImage img(w, h, QImage::Format_RGB32);
img.fill(Qt::red);
return img;
}
std::unique_ptr<IImageLoader> clone() const override {
return std::make_unique<FixedSizeImageLoader>(m_size);
}
};
class ChunkMeasurerTest : public QObject {
Q_OBJECT
private slots:
void returnsOneHeightPerChunk() {
ChunkMeasurer m;
QStringList chunks = {"<p>First</p>", "<p>Second</p>", "<p>Third</p>"};
auto h = m.measure(chunks, 800);
QCOMPARE(h.size(), 3);
}
void heightsArePositive() {
ChunkMeasurer m;
auto h = m.measure({"<p>Hello world</p>", "<p>Another paragraph</p>"}, 800);
for (int v : h) QVERIFY(v > 0);
}
void emptyInputReturnsEmpty() {
ChunkMeasurer m;
QVERIFY(m.measure({}, 800).isEmpty());
}
void zeroWidthClampedToOne() {
ChunkMeasurer m;
auto h = m.measure({"<p>text</p>"}, 0);
QCOMPARE(h.size(), 1);
QVERIFY(h.first() > 0);
}
void narrowerWidthGivesGreaterOrEqualHeight() {
ChunkMeasurer m;
QStringList chunks = {"<p>A long paragraph that should wrap when the width is very narrow.</p>"};
int h800 = m.measure(chunks, 800).first();
int h200 = m.measure(chunks, 200).first();
QVERIFY(h200 >= h800);
}
void imageChunkMeasuredWithLoader() {
// 600x400 image, but maxWidth=400 so it gets scaled to 400x267
auto loader = std::make_unique<FixedSizeImageLoader>(QSize(600, 400));
ChunkMeasurer m(std::move(loader));
// Chunk with an image URL
QStringList chunks = {QStringLiteral("<p><img src=\"image://epub/test.jpg\"/></p>")};
auto h = m.measure(chunks, 400);
QCOMPARE(h.size(), 1);
QVERIFY(h.first() > 0); // should include image height
}
void nullLoaderTextOnlyWorks() {
ChunkMeasurer m(nullptr);
auto h = m.measure({"<p>text only</p>"}, 800);
QCOMPARE(h.size(), 1);
QVERIFY(h.first() > 0);
}
void parallelResultsAreConsistent() {
ChunkMeasurer m;
QStringList chunks;
for (int i = 0; i < 100; ++i)
chunks << QStringLiteral("<p>Paragraph number %1 with some text content.</p>").arg(i);
auto h1 = m.measure(chunks, 600);
auto h2 = m.measure(chunks, 600);
QCOMPARE(h1, h2);
}
};
QTEST_MAIN(ChunkMeasurerTest)
#include "ChunkMeasurerTest.moc"
+1 -1
View File
@@ -217,7 +217,7 @@ private slots:
QVERIFY(!epubPath.isEmpty());
EpubImageProvider provider;
provider.setEpubPath(epubPath);
provider.setFilePath(epubPath);
provider.clear();
QQuickImageResponse *resp = provider.requestImageResponse(
-53
View File
@@ -1,53 +0,0 @@
#include <QtTest>
#include "backend/QDocumentChunkMeasurer.h"
class QDocumentChunkMeasurerTest : public QObject {
Q_OBJECT
private slots:
void returnsOneHeightPerChunk()
{
QDocumentChunkMeasurer measurer;
QStringList chunks = {"<p>First</p>", "<p>Second</p>", "<p>Third</p>"};
QVector<qreal> heights = measurer.measure(chunks, 800);
QCOMPARE(heights.size(), 3);
}
void heightsArePositive()
{
QDocumentChunkMeasurer measurer;
QStringList chunks = {"<p>Hello world</p>", "<p>Another paragraph</p>"};
QVector<qreal> heights = measurer.measure(chunks, 800);
for (qreal h : heights)
QVERIFY(h > 0.0);
}
void emptyChunkListReturnsEmpty()
{
QDocumentChunkMeasurer measurer;
QVector<qreal> heights = measurer.measure({}, 800);
QVERIFY(heights.isEmpty());
}
void zeroWidthClampedToOne()
{
// Must not crash and must return positive heights
QDocumentChunkMeasurer measurer;
QStringList chunks = {"<p>text</p>"};
QVector<qreal> heights = measurer.measure(chunks, 0);
QCOMPARE(heights.size(), 1);
QVERIFY(heights.first() > 0.0);
}
void narrowerWidthGivesGreaterOrEqualHeight()
{
QDocumentChunkMeasurer measurer;
QStringList chunks = {"<p>A long paragraph that should wrap when the width is very narrow.</p>"};
qreal height800 = measurer.measure(chunks, 800).first();
qreal height200 = measurer.measure(chunks, 200).first();
QVERIFY(height200 >= height800);
}
};
QTEST_MAIN(QDocumentChunkMeasurerTest)
#include "QDocumentChunkMeasurerTest.moc"
+148 -68
View File
@@ -3,18 +3,7 @@
#include "backend/ReadingModel.h"
#include "backend/FormatReader.h"
class SingleChapterReader : public FormatReader {
public:
bool open(const QString &) override { return true; }
QString lastError() override { return {}; }
int chapterCount() override { return 1; }
Chapter loadChapter(int) override { return {0, {}, {"<p>text</p>"}}; }
QList<TocEntry> tableOfContents() override { return {}; }
QString title() override { return {}; }
QString author() override { return {}; }
};
// 3 chapters, 2 chunks each → 6 total chunks
// A fake reader that has 3 chapters, 2 chunks each → 6 total chunks
class FakeReader : public FormatReader {
public:
bool open(const QString &) override { return true; }
@@ -34,6 +23,23 @@ public:
QList<TocEntry> tableOfContents() override { return {}; }
QString title() override { return QStringLiteral("Fake Book"); }
QString author() override { return QStringLiteral("Fake Author"); }
QString filePath() override { return QStringLiteral("/fake/path.txt"); }
QString coverImagePath() override { return {}; }
bool loadMetadata(const QString &) override { return true; }
};
class SingleChapterReader : public FormatReader {
public:
bool open(const QString &) override { return true; }
QString lastError() override { return {}; }
int chapterCount() override { return 1; }
Chapter loadChapter(int) override { return {0, {}, {"<p>text</p>"}}; }
QList<TocEntry> tableOfContents() override { return {}; }
QString title() override { return {}; }
QString author() override { return {}; }
QString filePath() override { return QStringLiteral("/fake/single.txt"); }
QString coverImagePath() override { return {}; }
bool loadMetadata(const QString &) override { return true; }
};
class ReadingModelTest : public QObject {
@@ -104,17 +110,17 @@ private slots:
QCOMPARE(model.currentChapter(), 0);
// Chunk 2 = first chunk of chapter 1
model.setScrollInfo(2, 0, false);
model.setScrollInfo(2, 0.0);
QCOMPARE(model.currentChapter(), 1);
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 1"));
// Chunk 4 = first chunk of chapter 2
model.setScrollInfo(4, 0, false);
model.setScrollInfo(4, 0.0);
QCOMPARE(model.currentChapter(), 2);
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 2"));
// Back to chapter 0
model.setScrollInfo(0, 0, false);
model.setScrollInfo(0, 0.0);
QCOMPARE(model.currentChapter(), 0);
}
@@ -126,16 +132,16 @@ private slots:
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
model.setScrollInfo(0, 0, false); // still chapter 0 — no signal
model.setScrollInfo(0, 0.0); // still chapter 0 — no signal
QCOMPARE(spy.count(), 0);
model.setScrollInfo(2, 0, false); // chapter 1 → signal
model.setScrollInfo(2, 0.0); // chapter 1 → signal
QCOMPARE(spy.count(), 1);
model.setScrollInfo(3, 0, false); // still chapter 1 — no signal
model.setScrollInfo(3, 0.0); // still chapter 1 — no signal
QCOMPARE(spy.count(), 1);
model.setScrollInfo(4, 0, false); // chapter 2 → signal
model.setScrollInfo(4, 0.0); // chapter 2 → signal
QCOMPARE(spy.count(), 2);
}
@@ -186,7 +192,7 @@ private slots:
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setScrollInfo(4, 0, false); // chapter 2
model.setScrollInfo(4, 0.0); // chapter 2
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.nextChapter();
@@ -198,7 +204,7 @@ private slots:
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setScrollInfo(4, 0, false); // chapter 2
model.setScrollInfo(4, 0.0); // chapter 2
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.previousChapter();
@@ -217,26 +223,6 @@ private slots:
QCOMPARE(spy.count(), 0);
}
void progressIsBookWide()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
// At chunk 0: progress = 0
QCOMPARE(model.progress(), 0.0);
// At chunk 3 (half-way): progress ~0.5
model.setScrollInfo(3, 0, false);
double p = model.progress();
QVERIFY(p > 0.0);
QVERIFY(p < 1.0);
// At chunk 0 again: progress = 0
model.setScrollInfo(0, 0, false);
QCOMPARE(model.progress(), 0.0);
}
void setReaderNull()
{
ReadingModel model;
@@ -265,40 +251,21 @@ private slots:
QCOMPARE(model.totalPages(), 0);
}
void totalPagesEqualsAllChunks()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QCOMPARE(model.totalPages(), 6); // 3 chapters × 2 chunks
QCOMPARE(model.currentPage(), 1);
}
void setScrollInfoUpdatesCurrentPage()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setScrollInfo(0, 0, false);
QCOMPARE(model.currentPage(), 1);
model.setScrollInfo(0, 0.0);
// If m_totalPixelHeight is 0, yOffset=0, yOffset/totalHeight is NaN.
// qBound(0, NaN, 1) is 0. 0 * 6 * 1.5 = 0.
QCOMPARE(model.currentPage(), 0);
model.setScrollInfo(3, 0, false);
QCOMPARE(model.currentPage(), 4);
model.setScrollInfo(5, 0, false);
QCOMPARE(model.currentPage(), 6);
}
void atYEndGivesLastPage()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setScrollInfo(0, 0, true); // atYEnd = true
QCOMPARE(model.currentPage(), model.totalPages());
model.setScrollInfo(3, 100.0);
// If m_totalPixelHeight is 0, yOffset=100, yOffset/totalHeight is Infinity.
// qBound(0, Inf, 1) is 1.0. 1.0 * 6 * 1.5 = 9.
QCOMPARE(model.currentPage(), 9);
}
void hasChaptersFalseForSingleChapter()
@@ -324,6 +291,119 @@ private slots:
ReadingModel model;
QVERIFY(!model.hasChapters());
}
// --- Cover image tests ---
void coverImagePrependedToChunks()
{
FakeReader reader;
ReadingModel model;
model.setCoverImageUrl("image://epub/OEBPS/cover.jpg");
model.setReader(&reader);
// Should have cover image chunk + original chunks
QCOMPARE(model.rowCount(QModelIndex()), 7); // 1 cover + 6 original
// First chunk should contain cover image HTML
QString firstChunk = model.data(model.index(0, 0), ReadingModel::ContentRole).toString();
QVERIFY(firstChunk.contains("class=\"cover-image\""));
QVERIFY(firstChunk.contains("image://epub/OEBPS/cover.jpg"));
}
void coverImageNotAddedWhenUrlEmpty()
{
FakeReader reader;
ReadingModel model;
model.setReader(&reader);
model.setCoverImageUrl("");
// Should have only original chunks (no cover)
QCOMPARE(model.rowCount(QModelIndex()), 6);
}
void chapterNavigationSkipsCoverImage()
{
FakeReader reader;
ReadingModel model;
model.setCoverImageUrl("image://epub/OEBPS/cover.jpg");
model.setReader(&reader);
// Chapter 0 should start at chunk 1 (after cover image)
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.goToChapter(0);
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.takeFirst().at(0).toInt(), 1); // Should scroll to chunk 1 (after cover)
// Navigate to chapter 2 (last chapter in FakeReader)
model.goToChapter(2);
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.takeFirst().at(0).toInt(), 5); // Should scroll to chunk 5 (chapter 2 start)
}
void coverImageSetAfterReaderReloadsChunks()
{
FakeReader reader;
ReadingModel model;
model.setReader(&reader);
QCOMPARE(model.rowCount(QModelIndex()), 6); // no cover yet
model.setCoverImageUrl(QStringLiteral("image://epub/OEBPS/cover.jpg"));
// setCoverImageUrl doesn't call loadAllChapters(), it only sets the URL.
// The URL is used when loadAllChapters() is called (via setReader).
// If we want to update it, we might need to re-set the reader or call a reload.
// Looking at ReadingModel.cpp, setCoverImageUrl ONLY sets the member.
model.setReader(&reader); // Re-set to reload with cover
QCOMPARE(model.rowCount(QModelIndex()), 7); // 1 cover + 6
QString first = model.data(model.index(0, 0), ReadingModel::ContentRole).toString();
QVERIFY(first.contains(QStringLiteral("class=\"cover-image\"")));
QVERIFY(first.contains(QStringLiteral("image://epub/OEBPS/cover.jpg")));
}
void clearCoverImageUrlRemovesCover()
{
FakeReader reader;
ReadingModel model;
model.setCoverImageUrl(QStringLiteral("image://epub/OEBPS/cover.jpg"));
model.setReader(&reader);
QCOMPARE(model.rowCount(QModelIndex()), 7); // 1 cover + 6
model.setCoverImageUrl(QStringLiteral(""));
model.setReader(&reader); // Re-set to reload without cover
QCOMPARE(model.rowCount(QModelIndex()), 6); // cover removed
QString first = model.data(model.index(0, 0), ReadingModel::ContentRole).toString();
QVERIFY(!first.contains(QStringLiteral("class=\"cover-image\"")));
}
void roleNamesHasNewRoles()
{
ReadingModel model;
auto roles = model.roleNames();
QVERIFY(roles.contains(ReadingModel::PixelHeightRole));
QVERIFY(roles.contains(ReadingModel::ChapterRole));
QCOMPARE(roles.value(ReadingModel::PixelHeightRole), QByteArray("pixelHeight"));
QCOMPARE(roles.value(ReadingModel::ChapterRole), QByteArray("chapter"));
}
void totalPixelHeightInitiallyZero()
{
ReadingModel model;
QCOMPARE(model.totalPixelHeight(), 0.0);
}
void totalPixelHeightResetOnReaderSet()
{
ReadingModel model;
FakeReader reader;
QSignalSpy spy(&model, &ReadingModel::totalPixelHeightChanged);
model.setReader(&reader);
// loadAllChapters resets m_totalPixelHeight to 0 and emits the signal
QVERIFY(spy.count() >= 1);
QCOMPARE(model.totalPixelHeight(), 0.0); // unmeasured chunks have pixelHeight -1, not yet summed
}
};
QTEST_MAIN(ReadingModelTest)
+119 -4
View File
@@ -305,6 +305,88 @@ static QString buildEpubLargeChapter()
return ok ? path : QString{};
}
// Build an EPUB with a cover image using both meta tag and properties attribute
static QString buildEpubWithCoverImage()
{
static const char opfCover[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">"
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
" <dc:title>Cover Book</dc:title>"
" <dc:creator>Cover Tester</dc:creator>"
" <meta name=\"cover\" content=\"cover-id\"/>"
" </metadata>"
" <manifest>"
" <item id=\"cover-id\" href=\"cover.jpg\" media-type=\"image/jpeg\" properties=\"cover-image\"/>"
" <item id=\"ch1\" href=\"chapter1.xhtml\" media-type=\"application/xhtml+xml\"/>"
" </manifest>"
" <spine>"
" <itemref idref=\"ch1\"/>"
" </spine>"
"</package>";
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + "/epub_cover_XXXXXX.epub");
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, "mimetype", QByteArray(kMimetype), true);
ok = ok && addEntry(zf, "META-INF/container.xml", QByteArray(kContainer));
ok = ok && addEntry(zf, "OEBPS/content.opf", QByteArray(opfCover));
ok = ok && addEntry(zf, "OEBPS/cover.jpg", QByteArray("DUMMY_JPEG"));
ok = ok && addEntry(zf, "OEBPS/chapter1.xhtml", QByteArray(kChapter1));
zipClose(zf, nullptr);
return ok ? path : QString{};
}
// Build an EPUB with cover image in subdirectory
static QString buildEpubWithCoverImageInSubdir()
{
static const char opfCoverSubdir[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">"
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
" <dc:title>Cover Subdir Book</dc:title>"
" <dc:creator>Subdir Tester</dc:creator>"
" <meta name=\"cover\" content=\"cover-img\"/>"
" </metadata>"
" <manifest>"
" <item id=\"cover-img\" href=\"Images/cover.jpg\" media-type=\"image/jpeg\" properties=\"cover-image\"/>"
" <item id=\"ch1\" href=\"chapter1.xhtml\" media-type=\"application/xhtml+xml\"/>"
" </manifest>"
" <spine>"
" <itemref idref=\"ch1\"/>"
" </spine>"
"</package>";
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + "/epub_coversub_XXXXXX.epub");
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, "mimetype", QByteArray(kMimetype), true);
ok = ok && addEntry(zf, "META-INF/container.xml", QByteArray(kContainer));
ok = ok && addEntry(zf, "OEBPS/content.opf", QByteArray(opfCoverSubdir));
ok = ok && addEntry(zf, "OEBPS/Images/cover.jpg", QByteArray("DUMMY_JPEG"));
ok = ok && addEntry(zf, "OEBPS/chapter1.xhtml", QByteArray(kChapter1));
zipClose(zf, nullptr);
return ok ? path : QString{};
}
// ---------------------------------------------------------------------------
// Test class
// ---------------------------------------------------------------------------
@@ -318,6 +400,8 @@ private:
QString m_largePath;
QString m_imgPath;
QString m_relImgPath;
QString m_coverPath;
QString m_coverSubdirPath;
private slots:
void initTestCase()
@@ -327,11 +411,15 @@ private slots:
m_largePath = buildEpubLargeChapter();
m_imgPath = buildEpubWithImage();
m_relImgPath = buildEpubWithRelativeImage();
m_coverPath = buildEpubWithCoverImage();
m_coverSubdirPath = buildEpubWithCoverImageInSubdir();
QVERIFY2(!m_epubPath.isEmpty(), "Failed to build sample EPUB fixture");
QVERIFY2(!m_noNcxPath.isEmpty(), "Failed to build no-NCX EPUB fixture");
QVERIFY2(!m_largePath.isEmpty(), "Failed to build large chapter EPUB fixture");
QVERIFY2(!m_imgPath.isEmpty(), "Failed to build image EPUB fixture");
QVERIFY2(!m_relImgPath.isEmpty(), "Failed to build relative-image EPUB fixture");
QVERIFY2(!m_coverPath.isEmpty(), "Failed to build cover image EPUB fixture");
QVERIFY2(!m_coverSubdirPath.isEmpty(), "Failed to build cover image subdir EPUB fixture");
}
void cleanupTestCase()
@@ -341,6 +429,8 @@ private slots:
QFile::remove(m_largePath);
QFile::remove(m_imgPath);
QFile::remove(m_relImgPath);
QFile::remove(m_coverPath);
QFile::remove(m_coverSubdirPath);
}
// --- Phase 2: error handling ---
@@ -479,8 +569,9 @@ private slots:
const Chapter ch = reader.loadChapter(0);
QVERIFY(!ch.contentChunks.isEmpty());
const QString combined = ch.contentChunks.join('\n');
QVERIFY2(combined.contains(QStringLiteral("image://epub/OEBPS/Images/test.png")),
qPrintable(QStringLiteral("Expected image://epub/OEBPS/Images/test.png in: ") + combined));
const QString expected = QStringLiteral("image://epub/OEBPS/Images/test.png");
QVERIFY2(combined.contains(expected),
qPrintable(QStringLiteral("Expected ") + expected + QStringLiteral(" in: ") + combined));
}
void relativeImagePathResolved()
@@ -490,8 +581,32 @@ private slots:
const Chapter ch = reader.loadChapter(0);
QVERIFY(!ch.contentChunks.isEmpty());
const QString combined = ch.contentChunks.join('\n');
QVERIFY2(combined.contains(QStringLiteral("image://epub/OEBPS/Images/test.png")),
qPrintable(QStringLiteral("Expected image://epub/OEBPS/Images/test.png in: ") + combined));
const QString expected = QStringLiteral("image://epub/OEBPS/Images/test.png");
QVERIFY2(combined.contains(expected),
qPrintable(QStringLiteral("Expected ") + expected + QStringLiteral(" in: ") + combined));
}
// --- Phase 8: cover image detection ---
void coverImagePathExtracted()
{
EpubReader reader;
QVERIFY(reader.open(m_coverPath));
QCOMPARE(reader.coverImagePath(), QStringLiteral("OEBPS/cover.jpg"));
}
void coverImagePathWithSubdirectory()
{
EpubReader reader;
QVERIFY(reader.open(m_coverSubdirPath));
QCOMPARE(reader.coverImagePath(), QStringLiteral("OEBPS/Images/cover.jpg"));
}
void coverImagePathEmptyForEpubWithoutCover()
{
EpubReader reader;
QVERIFY(reader.open(m_epubPath));
QVERIFY(reader.coverImagePath().isEmpty());
}
};