#include #include #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, {}, {"

text

"}}; } QList tableOfContents() override { return {}; } QString title() override { return {}; } QString author() override { return {}; } }; class FakeReader : public FormatReader { public: bool open(const QString &) override { return true; } QString lastError() override { return {}; } int chapterCount() override { return 3; } Chapter loadChapter(int index) override { if (index < 0 || index >= 3) return {index, {}, {}}; return {index, QStringLiteral("Chapter %1").arg(index), {QStringLiteral("

Chunk A of %1

").arg(index), QStringLiteral("

Chunk B of %1

").arg(index)}}; } QList tableOfContents() override { return {}; } QString title() override { return QStringLiteral("Fake Book"); } QString author() override { return QStringLiteral("Fake Author"); } }; class ReadingModelTest : public QObject { Q_OBJECT private slots: void initialState() { ReadingModel model; QCOMPARE(model.totalChapters(), 0); QCOMPARE(model.progress(), 0.0); QVERIFY(model.chapterTitle().isEmpty()); QCOMPARE(model.rowCount(QModelIndex()), 0); } void setReaderLoadsFirstChapter() { ReadingModel model; FakeReader reader; model.setReader(&reader); QCOMPARE(model.currentChapter(), 0); QCOMPARE(model.totalChapters(), 3); QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 0")); } void rowCountMatchesChunks() { ReadingModel model; FakeReader reader; model.setReader(&reader); QCOMPARE(model.rowCount(QModelIndex()), 2); } void dataReturnsChunkContent() { ReadingModel model; FakeReader reader; model.setReader(&reader); QModelIndex idx0 = model.index(0, 0); QModelIndex idx1 = model.index(1, 0); QCOMPARE(model.data(idx0, ReadingModel::ContentRole).toString(), QStringLiteral("

Chunk A of 0

")); QCOMPARE(model.data(idx1, ReadingModel::ContentRole).toString(), QStringLiteral("

Chunk B of 0

")); } void dataInvalidForOutOfRange() { ReadingModel model; FakeReader reader; model.setReader(&reader); QModelIndex badIdx = model.index(99, 0); QVERIFY(!model.data(badIdx, ReadingModel::ContentRole).isValid()); } void nextChapter() { ReadingModel model; FakeReader reader; model.setReader(&reader); QSignalSpy spy(&model, &ReadingModel::chapterChanged); model.nextChapter(); QCOMPARE(model.currentChapter(), 1); QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 1")); QCOMPARE(spy.count(), 1); QCOMPARE(model.rowCount(QModelIndex()), 2); QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(), QStringLiteral("

Chunk A of 1

")); } void nextChapterAtEnd() { ReadingModel model; FakeReader reader; model.setReader(&reader); model.goToChapter(2); QSignalSpy spy(&model, &ReadingModel::chapterChanged); model.nextChapter(); QCOMPARE(model.currentChapter(), 2); QCOMPARE(spy.count(), 0); } void previousChapter() { ReadingModel model; FakeReader reader; model.setReader(&reader); model.goToChapter(1); QSignalSpy spy(&model, &ReadingModel::chapterChanged); model.previousChapter(); QCOMPARE(model.currentChapter(), 0); QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 0")); QCOMPARE(spy.count(), 1); } void previousChapterAtStart() { ReadingModel model; FakeReader reader; model.setReader(&reader); QSignalSpy spy(&model, &ReadingModel::chapterChanged); model.previousChapter(); QCOMPARE(model.currentChapter(), 0); QCOMPARE(spy.count(), 0); } void goToChapter() { ReadingModel model; FakeReader reader; model.setReader(&reader); model.goToChapter(2); QCOMPARE(model.currentChapter(), 2); QCOMPARE(model.rowCount(QModelIndex()), 2); QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(), QStringLiteral("

Chunk A of 2

")); } void goToChapterOutOfRange() { ReadingModel model; FakeReader reader; model.setReader(&reader); QSignalSpy spy(&model, &ReadingModel::chapterChanged); model.goToChapter(-1); QCOMPARE(model.currentChapter(), 0); QCOMPARE(spy.count(), 0); model.goToChapter(99); QCOMPARE(model.currentChapter(), 0); QCOMPARE(spy.count(), 0); } void progressCalculation() { ReadingModel model; FakeReader reader; model.setReader(&reader); // Chapter 0 of 3, at chunk 0 → byte offset 0 → fraction 0 → 0/3 = 0.0 QCOMPARE(model.progress(), 0.0); model.goToChapter(2); // Chapter 2 of 3, at chunk 0 → fraction 0 → 2/3 QCOMPARE(model.progress(), 2.0 / 3.0); } void progressUsesBytes() { ReadingModel model; FakeReader reader; model.setReader(&reader); // chapter 0: 2 equal-ish chunks // At chunk 0: fraction = 0 (byteOffset[0] = 0) double p0 = model.progress(); QCOMPARE(p0, 0.0); // Scroll to chunk 1: fraction = bytes[0] / totalBytes > 0 model.setScrollInfo(1, 0, false); double p1 = model.progress(); QVERIFY(p1 > 0.0); QVERIFY(p1 < 1.0 / 3.0); // still in chapter 0 of 3 } void setReaderNull() { ReadingModel model; FakeReader reader; model.setReader(&reader); model.setReader(nullptr); QCOMPARE(model.totalChapters(), 0); QCOMPARE(model.progress(), 0.0); QVERIFY(model.chapterTitle().isEmpty()); QCOMPARE(model.rowCount(QModelIndex()), 0); } void navigationResetsModel() { ReadingModel model; FakeReader reader; model.setReader(&reader); QCOMPARE(model.rowCount(QModelIndex()), 2); model.nextChapter(); QCOMPARE(model.rowCount(QModelIndex()), 2); QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(), QStringLiteral("

Chunk A of 1

")); } void roleNamesContainsContent() { ReadingModel model; QHash roles = model.roleNames(); QVERIFY(roles.contains(ReadingModel::ContentRole)); QCOMPARE(roles.value(ReadingModel::ContentRole), QByteArray("content")); } void initialPageState() { ReadingModel model; QCOMPARE(model.currentPage(), 0); QCOMPARE(model.totalPages(), 0); } void chapterLoadSetsPages() { // Pages are set immediately on chapter load (totalPages = chunk count) ReadingModel model; FakeReader reader; model.setReader(&reader); QCOMPARE(model.totalPages(), 2); // FakeReader returns 2 chunks QCOMPARE(model.currentPage(), 1); } void chapterLoadResetsPages() { ReadingModel model; FakeReader reader; model.setReader(&reader); model.setScrollInfo(1, 0, false); QCOMPARE(model.currentPage(), 2); model.goToChapter(1); // After chapter change, pages reset to new chapter's chunk count and position 1 QCOMPARE(model.totalPages(), 2); QCOMPARE(model.currentPage(), 1); } void setScrollInfoUpdatesCurrentPage() { ReadingModel model; FakeReader reader; model.setReader(&reader); // 2 chunks model.setScrollInfo(0, 0, false); QCOMPARE(model.currentPage(), 1); model.setScrollInfo(1, 0, false); QCOMPARE(model.currentPage(), 2); } void atYEndGivesLastPage() { ReadingModel model; FakeReader reader; model.setReader(&reader); model.setScrollInfo(0, 0, true); // atYEnd = true QCOMPARE(model.currentPage(), model.totalPages()); } void hasChaptersFalseForSingleChapter() { ReadingModel model; SingleChapterReader reader; model.setReader(&reader); QVERIFY(!model.hasChapters()); } void hasChaptersTrueForMultipleChapters() { ReadingModel model; FakeReader reader; // 3 chapters model.setReader(&reader); QVERIFY(model.hasChapters()); } void hasChaptersFalseInitially() { ReadingModel model; QVERIFY(!model.hasChapters()); } }; QTEST_MAIN(ReadingModelTest) #include "ReadingModelTest.moc"