#include #include #include #include #include #include "backend/AppState.h" #include "backend/IFileDialogService.h" #include "backend/formats/ReaderFactory.h" #include "backend/formats/TxtReader.h" #include "backend/formats/EpubReader.h" #include "backend/state/BookPositionStore.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 epubFixture() { #ifdef FIXTURES_DIR return QStringLiteral(FIXTURES_DIR) + QStringLiteral("/sample.epub"); #else return QString(); #endif } static QString txtFixture() { #ifdef FIXTURES_DIR return QStringLiteral(FIXTURES_DIR) + QStringLiteral("/sample.txt"); #else return QString(); #endif } struct Ctx { QTemporaryFile f; QSettings *s = nullptr; AppState *appState = nullptr; BookPositionStore *store = nullptr; FakeDialogService *dialog = nullptr; ReaderFactory *factory = nullptr; Book *book = nullptr; Ctx() { f.setFileTemplate(QDir::tempPath() + QStringLiteral("/book-test-XXXXXX.ini")); f.open(); f.close(); s = new QSettings(f.fileName(), QSettings::IniFormat); appState = new AppState(*s); store = new BookPositionStore(*appState); dialog = new FakeDialogService; factory = new ReaderFactory({ [] { return std::make_unique(); }, [] { return std::make_unique(); }, }); book = new Book(*dialog, *store, *factory); } ~Ctx() { delete book; delete dialog; delete factory; delete store; delete appState; delete s; } }; 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()); QVERIFY(ctx.book->toc().isEmpty()); } void openValidTxtFile() { const QString path = txtFixture(); if (path.isEmpty() || !QFileInfo::exists(path)) QSKIP("No TXT fixture"); Ctx ctx; QSignalSpy bookChanged(ctx.book, &Book::bookChanged); QSignalSpy loading(ctx.book, &Book::loadingChanged); ctx.book->openFromPath(path); // loading should have become true QVERIFY(loading.count() > 0 || ctx.book->loading()); // Wait for the async open to complete 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); } void openInvalidPath() { Ctx ctx; QSignalSpy errorSpy(ctx.book, &Book::errorMessageChanged); ctx.book->openFromPath(QStringLiteral("/nonexistent/file.txt")); 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.txt")); 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() { const QString path = txtFixture(); if (path.isEmpty() || !QFileInfo::exists(path)) QSKIP("No TXT fixture"); 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 willOpenNewBookFiredBeforeSwap() { const QString path = txtFixture(); if (path.isEmpty() || !QFileInfo::exists(path)) QSKIP("No TXT fixture"); Ctx ctx; ctx.book->openFromPath(path); QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook(), 5000); QSignalSpy willOpen(ctx.book, &Book::willOpenNewBook); ctx.book->openFromPath(path); // second open triggers willOpenNewBook QCOMPARE(willOpen.count(), 1); } void openFromUrl() { const QString path = txtFixture(); if (path.isEmpty() || !QFileInfo::exists(path)) QSKIP("No TXT fixture"); Ctx ctx; ctx.book->openFromUrl(QUrl::fromLocalFile(path)); QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook(), 5000); QCOMPARE(ctx.book->filePath(), path); } void browseAndOpenDelegatesToDialog() { Ctx ctx; QSignalSpy loading(ctx.book, &Book::loadingChanged); const QString path = txtFixture(); if (path.isEmpty() || !QFileInfo::exists(path)) QSKIP("No TXT fixture"); // Simulate the dialog emitting fileSelected ctx.book->browseAndOpen(); // calls dialog.open() which in FakeDialogService is a no-op ctx.dialog->triggerFile(path); QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook(), 5000); } void saveCurrentPositionUsesHash() { const QString path = txtFixture(); if (path.isEmpty() || !QFileInfo::exists(path)) QSKIP("No TXT fixture"); Ctx ctx; ctx.book->openFromPath(path); QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook(), 5000); ctx.book->saveCurrentPosition(3, 17); const QByteArray hash = ctx.book->reader()->contentHash(); QVERIFY(!hash.isEmpty()); const BookPosition pos = ctx.store->load(hash); QCOMPARE(pos.chunkIndex, 3); QCOMPARE(pos.intraOffset, 17); } void contentWidthSignal() { Ctx ctx; QSignalSpy spy(ctx.book, &Book::contentWidthChanged); ctx.book->setContentWidth(800); QCOMPARE(spy.count(), 1); QCOMPARE(spy.first().first().toInt(), 800); ctx.book->setContentWidth(800); // no-op QCOMPARE(spy.count(), 1); } }; QTEST_MAIN(BookTest) #include "BookTest.moc"