#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 → 1/3 QCOMPARE(model.progress(), 1.0 / 3.0); model.goToChapter(2); // Chapter 2 of 3 → 3/3 = 1.0 QCOMPARE(model.progress(), 1.0); } 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); // Navigate and verify model is reset with new chapter's chunks 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 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"