#include #include #include #include #include #include "backend/AppState.h" #include "backend/ReadingModel.h" #include "backend/FormatReader.h" #include "backend/state/BookPositionStore.h" class FakeMangaReader : public FormatReader { public: ReadingMode mode() const override { return ReadingMode::Comic; } bool open(const QString &) override { return true; } QString lastError() const override { return {}; } QVector pages() const override { return { {QStringLiteral("image://cbz/0.jpg"), QSize(800, 1200)}, {QStringLiteral("image://cbz/1.jpg"), QSize(800, 1200)}, {QStringLiteral("image://cbz/2.jpg"), QSize(800, 1200)} }; } QString title() const override { return QStringLiteral("Manga Title"); } QString author() const override { return QStringLiteral("Manga Author"); } QString filePath() const override { return QStringLiteral("/fake/manga.cbz"); } QString coverImageUrl() const override { return QStringLiteral("image://cbz/0.jpg?src=/fake/manga.cbz"); } QByteArray contentHash() const override { return QByteArray("mangahash"); } bool loadMetadata(const QString &) override { return true; } }; class FakeTextReader : public FormatReader { public: ReadingMode mode() const override { return ReadingMode::Text; } bool open(const QString &) override { return true; } QString lastError() const override { return {}; } QString title() const override { return QStringLiteral("Text Book"); } QString author() const override { return QStringLiteral("Text Author"); } QString filePath() const override { return QStringLiteral("/fake/book.epub"); } QString coverImageUrl() const override { return QStringLiteral("image://epub/cover.jpg?src=/fake/book.epub"); } QByteArray contentHash() const override { return QByteArray("texthash"); } bool loadMetadata(const QString &) override { return true; } QVector chunks() const override { return HtmlBlockParser::parseChunks( QStringLiteral("

Chunk 1

Chunk 2

Chunk 3

"), QStringLiteral("text/ch1.xhtml"), 1 // targetChars = 1 forces each paragraph into its own chunk ); } }; class ManyChunksTextReader : public FormatReader { public: ReadingMode mode() const override { return ReadingMode::Text; } bool open(const QString &) override { return true; } QString lastError() const override { return {}; } QString title() const override { return QStringLiteral("Many Chunks"); } QString author() const override { return QStringLiteral("Author"); } QString filePath() const override { return QStringLiteral("/fake/many.epub"); } QString coverImageUrl() const override { return {}; } QByteArray contentHash() const override { return QByteArray("manychunks"); } bool loadMetadata(const QString &) override { return true; } QVector chunks() const override { QVector list; list.reserve(60); for (int i = 0; i < 60; ++i) { auto parsed = HtmlBlockParser::parseChunks( QStringLiteral("

Paragraph chunk number %1 with some text content.

").arg(i), QStringLiteral("text/ch1.xhtml") ); list.append(parsed); } return list; } }; class VariableCharsTextReader : public FormatReader { public: explicit VariableCharsTextReader(const QVector &charsPerChunk) { m_chunks.reserve(charsPerChunk.size()); for (int i = 0; i < charsPerChunk.size(); ++i) { ContentChunk chunk; chunk.sectionHref = QStringLiteral("sec_%1.xhtml").arg(i); chunk.charCount = charsPerChunk[i]; BlockItem p; p.kind = BlockItem::Kind::Paragraph; InlineItem t; t.kind = InlineItem::Kind::Text; t.text = QString(charsPerChunk[i], QLatin1Char('A')); p.inlines.append(t); chunk.blocks.append(p); m_chunks.append(chunk); } } ReadingMode mode() const override { return ReadingMode::Text; } bool open(const QString &) override { return true; } QString lastError() const override { return {}; } QString title() const override { return QStringLiteral("Norm Reader"); } QString author() const override { return QStringLiteral("Author"); } QString filePath() const override { return QStringLiteral("/fake/norm.epub"); } QString coverImageUrl() const override { return {}; } QByteArray contentHash() const override { return QByteArray("normhash"); } bool loadMetadata(const QString &) override { return true; } QVector chunks() const override { return m_chunks; } private: QVector m_chunks; }; class ReadingModelTest : public QObject { Q_OBJECT private: struct Ctx { QTemporaryFile f; QSettings *s = nullptr; AppState *appState = nullptr; BookPositionStore *store = nullptr; Ctx() { f.setFileTemplate(QDir::tempPath() + QStringLiteral("/rm-test-XXXXXX.ini")); (void)f.open(); f.close(); s = new QSettings(f.fileName(), QSettings::IniFormat); appState = new AppState(*s); store = new BookPositionStore(*appState); } ~Ctx() { delete store; delete appState; delete s; } }; private slots: void initialState() { Ctx ctx; ReadingModel model(*ctx.store); QVERIFY(!model.isTextMode()); QCOMPARE(model.progress(), 0.0); QCOMPARE(model.currentPage(), 0); QCOMPARE(model.totalPages(), 0); QCOMPARE(model.totalPagePairs(), 0); QVERIFY(model.pageShift()); QVERIFY(model.mangaRTL()); } void setReaderLoadsPagesAndEmitsRestoreReady() { Ctx ctx; ReadingModel model(*ctx.store); FakeMangaReader reader; QSignalSpy restoreSpy(&model, &ReadingModel::restoreReady); model.setReader(&reader); QVERIFY(!model.isTextMode()); QCOMPARE(model.imageUrlForChunk(0), QStringLiteral("image://cbz/0.jpg")); QCOMPARE(model.imageUrlForChunk(1), QStringLiteral("image://cbz/1.jpg")); QCOMPARE(model.imageUrlForChunk(2), QStringLiteral("image://cbz/2.jpg")); QCOMPARE(model.totalPagePairs(), 3); QCOMPARE(model.totalPages(), 3); QCOMPARE(model.currentPage(), 1); QTRY_COMPARE(restoreSpy.count(), 1); QCOMPARE(restoreSpy.first().at(0).toInt(), 0); } void scrollInfoUpdatesCurrentPageAndProgressAndSaves() { Ctx ctx; ReadingModel model(*ctx.store); FakeMangaReader reader; model.setReader(&reader); QSignalSpy pageSpy(&model, &ReadingModel::currentPageChanged); QSignalSpy savedSpy(&model, &ReadingModel::positionSaved); model.setScrollInfo(1, 0.5); QCOMPARE(model.currentPage(), 2); QCOMPARE(model.progress(), 0.5); QCOMPARE(pageSpy.count(), 1); QCOMPARE(savedSpy.count(), 1); const auto savedPos = ctx.store->load(reader.contentHash()); QCOMPARE(savedPos.chunkIndex, 1); QCOMPARE(savedPos.progress, 0.5); } void landscapeModeAndPageShift() { Ctx ctx; ReadingModel model(*ctx.store); FakeMangaReader reader; model.setReader(&reader); model.setMangaLandscape(true); // Default pageShift = true: chunk 0 solo (cover), chunk 1 solo (shift), chunk 2 solo -> 3 pairs QCOMPARE(model.totalPagePairs(), 3); QSignalSpy shiftSpy(&model, &ReadingModel::pageShiftChanged); QSignalSpy rebuildSpy(&model, &ReadingModel::pagePairsRebuilt); model.setPageShift(false); QCOMPARE(shiftSpy.count(), 1); QCOMPARE(rebuildSpy.count(), 1); QCOMPARE(model.totalPagePairs(), 2); QVariantList pair0 = model.pagePair(0); QCOMPARE(pair0.size(), 1); QCOMPARE(pair0.at(0).toInt(), 0); QVariantList pair1 = model.pagePair(1); QCOMPARE(pair1.size(), 2); QCOMPARE(pair1.at(0).toInt(), 1); QCOMPARE(pair1.at(1).toInt(), 2); } void mangaRTLToggle() { Ctx ctx; ReadingModel model(*ctx.store); FakeMangaReader reader; model.setReader(&reader); QSignalSpy rtlSpy(&model, &ReadingModel::mangaRTLChanged); model.setMangaRTL(false); QCOMPARE(rtlSpy.count(), 1); QVERIFY(!model.mangaRTL()); } void textModeContinuousChunks() { Ctx ctx; ReadingModel model(*ctx.store); FakeTextReader reader; QSignalSpy modeSpy(&model, &ReadingModel::modeChanged); QSignalSpy restoreSpy(&model, &ReadingModel::restoreReady); model.setReader(&reader); QCOMPARE(modeSpy.count(), 1); QVERIFY(model.isTextMode()); QCOMPARE(model.rowCount(), 3); QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(), QStringLiteral("text/ch1.xhtml")); QCOMPARE(model.data(model.index(1, 0), ReadingModel::ContentRole).toString(), QStringLiteral("text/ch1.xhtml")); QCOMPARE(model.data(model.index(2, 0), ReadingModel::ContentRole).toString(), QStringLiteral("text/ch1.xhtml")); QTRY_COMPARE(restoreSpy.count(), 1); // Test resize fast path QSignalSpy dataSpy(&model, &ReadingModel::dataChanged); model.updateLayoutMetrics(800, 16, 1.2); QVERIFY(dataSpy.count() >= 1); QCOMPARE(model.rowCount(), 3); QCOMPARE(model.data(model.index(0, 0), ReadingModel::LayoutRole).value().contentWidth, 800); } void setReaderNullResetsState() { Ctx ctx; ReadingModel model(*ctx.store); FakeMangaReader reader; model.setReader(&reader); model.setReader(nullptr); QCOMPARE(model.totalPagePairs(), 0); QCOMPARE(model.totalPages(), 0); QCOMPARE(model.currentPage(), 0); QCOMPARE(model.progress(), 0.0); QVERIFY(!model.isTextMode()); QCOMPARE(model.rowCount(), 0); } void useBookColoursToggleAndPersistence() { Ctx ctx; ReadingModel model(*ctx.store); FakeTextReader reader; QCOMPARE(model.useBookColours(), false); // Pre-save position with useBookColours = true for reader BookPosition savedPos; savedPos.chunkIndex = 1; savedPos.useBookColours = true; ctx.store->save(reader.contentHash(), savedPos); QSignalSpy spy(&model, &ReadingModel::useBookColoursChanged); model.setReader(&reader); QCOMPARE(model.useBookColours(), true); QVERIFY(spy.count() >= 1); // Toggle to false spy.clear(); model.setUseBookColours(false); QCOMPARE(model.useBookColours(), false); QCOMPARE(spy.count(), 1); // Verify it was persisted to store BookPosition loaded = ctx.store->load(reader.contentHash()); QCOMPARE(loaded.useBookColours, false); // Setting reader to null resets useBookColours to false model.setUseBookColours(true); QCOMPARE(model.useBookColours(), true); model.setReader(nullptr); QCOMPARE(model.useBookColours(), false); } void visibleFirstLayoutAndOnDemandLayout() { Ctx ctx; ReadingModel model(*ctx.store); ManyChunksTextReader reader; QSignalSpy restoreSpy(&model, &ReadingModel::restoreReady); model.setReader(&reader); // restoreReady must be emitted immediately from Phase 1 (<1ms) QCOMPARE(restoreSpy.count(), 1); QCOMPARE(restoreSpy.first().at(0).toInt(), 0); // Row 0..25 are laid out immediately QVERIFY(model.data(model.index(0, 0), ReadingModel::PixelHeightRole).toReal() > 0); QVERIFY(!model.data(model.index(0, 0), ReadingModel::LayoutRole).value().lines.isEmpty()); // Far-away row (e.g. 55) layout should be computed on-demand if queried QVERIFY(model.data(model.index(55, 0), ReadingModel::PixelHeightRole).toReal() > 0); const auto layout55 = model.data(model.index(55, 0), ReadingModel::LayoutRole).value(); QVERIFY(!layout55.lines.isEmpty()); } void normalizedPageCountCalculations() { Ctx ctx; ReadingModel model(*ctx.store); // 1. Empty book QCOMPARE(model.totalPages(), 0); QCOMPARE(model.currentPage(), 0); QCOMPARE(model.currentChunk(), 0); // 2. Short book: 500 characters (< 1800) -> 1 page VariableCharsTextReader shortReader({500}); model.setReader(&shortReader); QCOMPARE(model.totalPages(), 1); QCOMPARE(model.currentPage(), 1); QCOMPARE(model.currentChunk(), 0); // 3. Exactly 1800 characters -> 1 page VariableCharsTextReader exactReader({1800}); model.setReader(&exactReader); QCOMPARE(model.totalPages(), 1); QCOMPARE(model.currentPage(), 1); // 4. 1801 characters -> 2 pages VariableCharsTextReader splitReader({1801}); model.setReader(&splitReader); QCOMPARE(model.totalPages(), 2); QCOMPARE(model.currentPage(), 1); // 5. Multi-chunk book: 5 chunks with 720 chars each (3600 chars total) -> 2 pages VariableCharsTextReader multiChunk({720, 720, 720, 720, 720}); model.setReader(&multiChunk); QCOMPARE(model.rowCount(), 5); QCOMPARE(model.totalPages(), 2); QCOMPARE(model.currentPage(), 1); } void normalizedPageCurrentPageProgression() { Ctx ctx; ReadingModel model(*ctx.store); // 6 chunks of 900 chars each = 5400 chars = 3 normalized pages (1800 chars/page) // Chunk 0: 0..900 (Page 1) // Chunk 1: 900..1800 (Page 1) // Chunk 2: 1800..2700 (Page 2) // Chunk 3: 2700..3600 (Page 2) // Chunk 4: 3600..4500 (Page 3) // Chunk 5: 4500..5400 (Page 3) VariableCharsTextReader reader({900, 900, 900, 900, 900, 900}); model.setReader(&reader); QCOMPARE(model.totalPages(), 3); QCOMPARE(model.currentPage(), 1); QCOMPARE(model.currentChunk(), 0); QSignalSpy pageSpy(&model, &ReadingModel::currentPageChanged); QSignalSpy chunkSpy(&model, &ReadingModel::currentChunkChanged); // Scroll to chunk 1: 900 chars scrolled -> still Page 1 model.setScrollPosition(1, 0.0, 0.16); QCOMPARE(model.currentChunk(), 1); QCOMPARE(model.currentPage(), 1); QCOMPARE(chunkSpy.count(), 1); QCOMPARE(pageSpy.count(), 0); // Scroll to chunk 2: 1800 chars scrolled -> Page 2 model.setScrollPosition(2, 0.0, 0.33); QCOMPARE(model.currentChunk(), 2); QCOMPARE(model.currentPage(), 2); QCOMPARE(pageSpy.count(), 1); // Scroll to chunk 3: 2700 chars scrolled -> Page 2 model.setScrollPosition(3, 0.0, 0.50); QCOMPARE(model.currentChunk(), 3); QCOMPARE(model.currentPage(), 2); // Scroll to chunk 4: 3600 chars scrolled -> Page 3 model.setScrollPosition(4, 0.0, 0.67); QCOMPARE(model.currentChunk(), 4); QCOMPARE(model.currentPage(), 3); QCOMPARE(pageSpy.count(), 2); // Scroll to end of chunk 5: Page 3 (clamped to totalPages) model.setScrollPosition(5, 100.0, 1.0); QCOMPARE(model.currentChunk(), 5); QCOMPARE(model.currentPage(), 3); } void viewportGeometryInvariance() { Ctx ctx; ReadingModel model(*ctx.store); // 10 chunks of 1800 chars each = 18000 chars = 10 normalized pages QVector chunkSizes(10, 1800); VariableCharsTextReader reader(chunkSizes); model.setReader(&reader); QCOMPARE(model.totalPages(), 10); QCOMPARE(model.currentPage(), 1); // Scroll to chunk 3 (Page 4) model.setScrollPosition(3, 0.0, 0.3); QCOMPARE(model.currentPage(), 4); QCOMPARE(model.currentChunk(), 3); // Simulate resizing viewport width and changing font preferences model.updateLayoutMetrics(300, 12, 1.0, 3); QCOMPARE(model.totalPages(), 10); QCOMPARE(model.currentPage(), 4); QCOMPARE(model.currentChunk(), 3); model.updateLayoutMetrics(1400, 28, 1.8, 3); QCOMPARE(model.totalPages(), 10); QCOMPARE(model.currentPage(), 4); QCOMPARE(model.currentChunk(), 3); model.updateLayoutMetrics(800, 16, 1.2, 3); QCOMPARE(model.totalPages(), 10); QCOMPARE(model.currentPage(), 4); QCOMPARE(model.currentChunk(), 3); } }; QTEST_MAIN(ReadingModelTest) #include "ReadingModelTest.moc"