Simplify sidebar navigation by removing the conditional Book and Close Book items so users no longer need to manage a book left open in the background. Because there is no longer a Book item to return to the reader, navigating away from the reader to History or Custom Theme now flushes any pending scroll position and closes the active or in-flight book. Opening the Settings popup still keeps the book open behind the overlay, and clicking Files keeps the current book open until a file is actually selected so cancelling the file dialog preserves the current reading session. This also fixes book-lifecycle edge cases exposed by closing on navigation: - Cancel in-flight async loads in Book::close() and clear loading state so closing mid-load does not jump back to the reader or stall the spinner. - Emit willCloseBook before resetting the reader so AppCoordinator snapshots final progress into HistoryModel and immediately clears UiState.lastFilePath. - Mark Book as loading before clearing the outgoing reader when opening a new file to prevent HistoryPage from flashing between books, and fall back to History on load errors. - Reset ScrollPageView and MangaPageView state when a book closes, and always navigate to the reader on restoreReady so CLI-opened files are not hidden behind a previously saved History page. - Add coverage in BookTest and AppCoordinatorTest and document the behavior in AGENTS.md.
248 lines
7.4 KiB
C++
248 lines
7.4 KiB
C++
#include <QtTest>
|
|
#include <QDir>
|
|
#include <QImage>
|
|
#include <QSignalSpy>
|
|
#include <QTemporaryDir>
|
|
#include <QTemporaryFile>
|
|
|
|
#include "backend/IFileDialogService.h"
|
|
#include "backend/formats/FormatRegistry.h"
|
|
#include "backend/formats/sources/DirByteSource.h"
|
|
#include "backend/book/Book.h"
|
|
|
|
// Minimal fake dialog service — never opens a dialog; used to satisfy the Book constructor.
|
|
class FakeDialogService : public IFileDialogService {
|
|
Q_OBJECT
|
|
public:
|
|
explicit FakeDialogService(QObject *parent = nullptr) : IFileDialogService(parent) {}
|
|
void open(const QStringList &) override { /* no-op */ }
|
|
void triggerFile(const QString &path) { emit fileSelected(path); }
|
|
};
|
|
|
|
class BookTest : public QObject {
|
|
Q_OBJECT
|
|
|
|
private:
|
|
static QString createSampleComic(QTemporaryDir &tmp)
|
|
{
|
|
if (!tmp.isValid()) return {};
|
|
QImage img(8, 8, QImage::Format_RGB888);
|
|
img.fill(Qt::blue);
|
|
img.save(tmp.filePath(QStringLiteral("page01.png")), "PNG");
|
|
return tmp.path();
|
|
}
|
|
|
|
struct Ctx {
|
|
FakeDialogService *dialog = nullptr;
|
|
FormatRegistry *factory = nullptr;
|
|
Book *book = nullptr;
|
|
|
|
Ctx() {
|
|
dialog = new FakeDialogService;
|
|
factory = new FormatRegistry({
|
|
FormatSpec::comic({}, /*matchesDirectory=*/true, QStringLiteral("dir"),
|
|
[] { return std::make_unique<DirByteSource>(); }),
|
|
});
|
|
book = new Book(*dialog, *factory);
|
|
}
|
|
~Ctx() {
|
|
delete book;
|
|
delete dialog;
|
|
delete factory;
|
|
}
|
|
};
|
|
|
|
private slots:
|
|
void initialState()
|
|
{
|
|
Ctx ctx;
|
|
QVERIFY(!ctx.book->hasBook());
|
|
QVERIFY(!ctx.book->loading());
|
|
QCOMPARE(ctx.book->title(), QString());
|
|
QCOMPARE(ctx.book->author(), QString());
|
|
QCOMPARE(ctx.book->filePath(), QString());
|
|
QCOMPARE(ctx.book->coverImageUrl(),QString());
|
|
QCOMPARE(ctx.book->errorMessage(), QString());
|
|
}
|
|
|
|
void openValidFile()
|
|
{
|
|
QTemporaryDir tmp;
|
|
const QString path = createSampleComic(tmp);
|
|
QVERIFY(!path.isEmpty());
|
|
|
|
Ctx ctx;
|
|
QSignalSpy bookChanged(ctx.book, &Book::bookChanged);
|
|
QSignalSpy loading(ctx.book, &Book::loadingChanged);
|
|
|
|
ctx.book->openFromPath(path);
|
|
|
|
QVERIFY(loading.count() > 0 || ctx.book->loading());
|
|
QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook(), 5000);
|
|
|
|
QVERIFY(!ctx.book->loading());
|
|
QCOMPARE(ctx.book->filePath(), path);
|
|
QVERIFY(!ctx.book->title().isEmpty());
|
|
QVERIFY(bookChanged.count() > 0);
|
|
QVERIFY(ctx.book->coverImageUrl().startsWith(QStringLiteral("image://dir/")));
|
|
}
|
|
|
|
void openInvalidPath()
|
|
{
|
|
Ctx ctx;
|
|
QSignalSpy errorSpy(ctx.book, &Book::errorMessageChanged);
|
|
|
|
ctx.book->openFromPath(QStringLiteral("/nonexistent/file.cbz"));
|
|
|
|
QTRY_VERIFY_WITH_TIMEOUT(!ctx.book->errorMessage().isEmpty(), 5000);
|
|
QVERIFY(!ctx.book->hasBook());
|
|
QVERIFY(!ctx.book->loading());
|
|
}
|
|
|
|
void clearError()
|
|
{
|
|
Ctx ctx;
|
|
ctx.book->openFromPath(QStringLiteral("/nonexistent/file.cbz"));
|
|
QTRY_VERIFY_WITH_TIMEOUT(!ctx.book->errorMessage().isEmpty(), 5000);
|
|
|
|
QSignalSpy errorSpy(ctx.book, &Book::errorMessageChanged);
|
|
ctx.book->clearError();
|
|
QCOMPARE(ctx.book->errorMessage(), QString());
|
|
QCOMPARE(errorSpy.count(), 1);
|
|
}
|
|
|
|
void closeBook()
|
|
{
|
|
QTemporaryDir tmp;
|
|
const QString path = createSampleComic(tmp);
|
|
QVERIFY(!path.isEmpty());
|
|
|
|
Ctx ctx;
|
|
ctx.book->openFromPath(path);
|
|
QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook(), 5000);
|
|
|
|
QSignalSpy bookChanged(ctx.book, &Book::bookChanged);
|
|
ctx.book->close();
|
|
QVERIFY(!ctx.book->hasBook());
|
|
QCOMPARE(bookChanged.count(), 1);
|
|
}
|
|
|
|
void closeBookEmitsWillCloseBookBeforeReset()
|
|
{
|
|
QTemporaryDir tmp;
|
|
const QString path = createSampleComic(tmp);
|
|
QVERIFY(!path.isEmpty());
|
|
|
|
Ctx ctx;
|
|
ctx.book->openFromPath(path);
|
|
QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook(), 5000);
|
|
|
|
bool hadBookDuringWillClose = false;
|
|
QString filePathDuringWillClose;
|
|
connect(ctx.book, &Book::willCloseBook, this, [&]() {
|
|
hadBookDuringWillClose = ctx.book->hasBook();
|
|
filePathDuringWillClose = ctx.book->filePath();
|
|
});
|
|
|
|
QSignalSpy willClose(ctx.book, &Book::willCloseBook);
|
|
ctx.book->close();
|
|
|
|
QCOMPARE(willClose.count(), 1);
|
|
QVERIFY(hadBookDuringWillClose);
|
|
QCOMPARE(filePathDuringWillClose, path);
|
|
QVERIFY(!ctx.book->hasBook());
|
|
|
|
// Closing again when already closed is a no-op.
|
|
ctx.book->close();
|
|
QCOMPARE(willClose.count(), 1);
|
|
}
|
|
|
|
void closeBookCancelsInFlightOpen()
|
|
{
|
|
QTemporaryDir tmp;
|
|
const QString path = createSampleComic(tmp);
|
|
QVERIFY(!path.isEmpty());
|
|
|
|
Ctx ctx;
|
|
QSignalSpy bookChanged(ctx.book, &Book::bookChanged);
|
|
ctx.book->openFromPath(path);
|
|
QVERIFY(ctx.book->loading());
|
|
|
|
ctx.book->close();
|
|
QVERIFY(!ctx.book->loading());
|
|
QVERIFY(!ctx.book->hasBook());
|
|
|
|
// Give the background thread time to finish; cancelled load must not install the reader.
|
|
QTest::qWait(200);
|
|
QCoreApplication::processEvents();
|
|
QVERIFY(!ctx.book->hasBook());
|
|
QVERIFY(!ctx.book->loading());
|
|
QCOMPARE(bookChanged.count(), 0);
|
|
}
|
|
|
|
void openNewBookSetsLoadingBeforeClosingPrevious()
|
|
{
|
|
QTemporaryDir tmp;
|
|
const QString path = createSampleComic(tmp);
|
|
QVERIFY(!path.isEmpty());
|
|
|
|
Ctx ctx;
|
|
ctx.book->openFromPath(path);
|
|
QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook(), 5000);
|
|
|
|
bool wasLoadingWhenOldBookCleared = false;
|
|
connect(ctx.book, &Book::bookChanged, this, [&]() {
|
|
if (!ctx.book->hasBook())
|
|
wasLoadingWhenOldBookCleared = ctx.book->loading();
|
|
});
|
|
|
|
QSignalSpy willOpen(ctx.book, &Book::willOpenNewBook);
|
|
ctx.book->openFromPath(path);
|
|
QCOMPARE(willOpen.count(), 1);
|
|
QVERIFY(wasLoadingWhenOldBookCleared);
|
|
QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook(), 5000);
|
|
}
|
|
|
|
void willOpenNewBookFiredBeforeSwap()
|
|
{
|
|
QTemporaryDir tmp;
|
|
const QString path = createSampleComic(tmp);
|
|
QVERIFY(!path.isEmpty());
|
|
|
|
Ctx ctx;
|
|
ctx.book->openFromPath(path);
|
|
QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook(), 5000);
|
|
|
|
QSignalSpy willOpen(ctx.book, &Book::willOpenNewBook);
|
|
ctx.book->openFromPath(path);
|
|
QCOMPARE(willOpen.count(), 1);
|
|
}
|
|
|
|
void openFromUrl()
|
|
{
|
|
QTemporaryDir tmp;
|
|
const QString path = createSampleComic(tmp);
|
|
QVERIFY(!path.isEmpty());
|
|
|
|
Ctx ctx;
|
|
ctx.book->openFromUrl(QUrl::fromLocalFile(path));
|
|
QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook(), 5000);
|
|
QCOMPARE(ctx.book->filePath(), path);
|
|
}
|
|
|
|
void browseAndOpenDelegatesToDialog()
|
|
{
|
|
QTemporaryDir tmp;
|
|
const QString path = createSampleComic(tmp);
|
|
QVERIFY(!path.isEmpty());
|
|
|
|
Ctx ctx;
|
|
ctx.book->browseAndOpen();
|
|
ctx.dialog->triggerFile(path);
|
|
QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook(), 5000);
|
|
}
|
|
};
|
|
|
|
QTEST_MAIN(BookTest)
|
|
#include "BookTest.moc"
|