Display all chapters

This commit is contained in:
Agel committed 2026-03-23 23:28:23 +01:00
1 parent d808f45346
commit 808ec3f787
4 files changed
+170 -159

No files matched your search

+120 -120
View File
@@ -14,6 +14,7 @@ public:
QString author() override { return {}; }
};
// 3 chapters, 2 chunks each → 6 total chunks
class FakeReader : public FormatReader {
public:
bool open(const QString &) override { return true; }
@@ -48,38 +49,40 @@ private slots:
QCOMPARE(model.rowCount(QModelIndex()), 0);
}
void setReaderLoadsFirstChapter()
void setReaderLoadsAllChapters()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QCOMPARE(model.currentChapter(), 0);
// All 3 chapters × 2 chunks = 6 total rows
QCOMPARE(model.rowCount(QModelIndex()), 6);
QCOMPARE(model.totalChapters(), 3);
QCOMPARE(model.currentChapter(), 0);
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 0"));
}
void rowCountMatchesChunks()
void chunkContentAcrossChapters()
{
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(),
// Chapter 0: chunks 0,1
QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk A of 0</p>"));
QCOMPARE(model.data(idx1, ReadingModel::ContentRole).toString(),
QCOMPARE(model.data(model.index(1, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk B of 0</p>"));
// Chapter 1: chunks 2,3
QCOMPARE(model.data(model.index(2, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk A of 1</p>"));
QCOMPARE(model.data(model.index(3, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk B of 1</p>"));
// Chapter 2: chunks 4,5
QCOMPARE(model.data(model.index(4, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk A of 2</p>"));
QCOMPARE(model.data(model.index(5, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk B of 2</p>"));
}
void dataInvalidForOutOfRange()
@@ -92,123 +95,146 @@ private slots:
QVERIFY(!model.data(badIdx, ReadingModel::ContentRole).isValid());
}
void nextChapter()
void currentChapterDerivedFromScroll()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
model.nextChapter();
QCOMPARE(model.currentChapter(), 0);
// Chunk 2 = first chunk of chapter 1
model.setScrollInfo(2, 0, false);
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("<p>Chunk A of 1</p>"));
// Chunk 4 = first chunk of chapter 2
model.setScrollInfo(4, 0, false);
QCOMPARE(model.currentChapter(), 2);
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 2"));
// Back to chapter 0
model.setScrollInfo(0, 0, false);
QCOMPARE(model.currentChapter(), 0);
}
void nextChapterAtEnd()
void chapterChangedSignalOnScroll()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.goToChapter(2);
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
model.nextChapter();
QCOMPARE(model.currentChapter(), 2);
model.setScrollInfo(0, 0, false); // still chapter 0 — no signal
QCOMPARE(spy.count(), 0);
model.setScrollInfo(2, 0, false); // chapter 1 → signal
QCOMPARE(spy.count(), 1);
model.setScrollInfo(3, 0, false); // still chapter 1 — no signal
QCOMPARE(spy.count(), 1);
model.setScrollInfo(4, 0, false); // chapter 2 → signal
QCOMPARE(spy.count(), 2);
}
void previousChapter()
void goToChapterEmitsScrollSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
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);
QCOMPARE(spy.at(0).at(0).toInt(), 2); // chapter 1 starts at chunk 2
model.goToChapter(2);
QCOMPARE(model.currentChapter(), 2);
QCOMPARE(model.rowCount(QModelIndex()), 2);
QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk A of 2</p>"));
QCOMPARE(spy.count(), 2);
QCOMPARE(spy.at(1).at(0).toInt(), 4); // chapter 2 starts at chunk 4
}
void goToChapterOutOfRange()
void goToChapterOutOfRangeNoSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.goToChapter(-1);
QCOMPARE(model.currentChapter(), 0);
QCOMPARE(spy.count(), 0);
model.goToChapter(99);
QCOMPARE(model.currentChapter(), 0);
QCOMPARE(spy.count(), 0);
}
void progressCalculation()
void nextChapterEmitsScrollSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
// Chapter 0 of 3, at chunk 0 → byte offset 0 → fraction 0 → 0/3 = 0.0
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.nextChapter(); // chapter 0 → 1
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.at(0).at(0).toInt(), 2);
}
void nextChapterAtEndNoSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setScrollInfo(4, 0, false); // chapter 2
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.nextChapter();
QCOMPARE(spy.count(), 0);
}
void previousChapterEmitsScrollSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setScrollInfo(4, 0, false); // chapter 2
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.previousChapter();
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.at(0).at(0).toInt(), 2); // chapter 1 starts at chunk 2
}
void previousChapterAtStartNoSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.previousChapter();
QCOMPARE(spy.count(), 0);
}
void progressIsBookWide()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
// At chunk 0: progress = 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);
}
// At chunk 3 (half-way): progress ~0.5
model.setScrollInfo(3, 0, false);
double p = model.progress();
QVERIFY(p > 0.0);
QVERIFY(p < 1.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
// At chunk 0 again: progress = 0
model.setScrollInfo(0, 0, false);
QCOMPARE(model.progress(), 0.0);
}
void setReaderNull()
@@ -224,20 +250,6 @@ private slots:
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("<p>Chunk A of 1</p>"));
}
void roleNamesContainsContent()
{
ReadingModel model;
@@ -253,28 +265,13 @@ private slots:
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()
void totalPagesEqualsAllChunks()
{
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.totalPages(), 6); // 3 chapters × 2 chunks
QCOMPARE(model.currentPage(), 1);
}
@@ -282,13 +279,16 @@ private slots:
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader); // 2 chunks
model.setReader(&reader);
model.setScrollInfo(0, 0, false);
QCOMPARE(model.currentPage(), 1);
model.setScrollInfo(1, 0, false);
QCOMPARE(model.currentPage(), 2);
model.setScrollInfo(3, 0, false);
QCOMPARE(model.currentPage(), 4);
model.setScrollInfo(5, 0, false);
QCOMPARE(model.currentPage(), 6);
}
void atYEndGivesLastPage()
@@ -313,7 +313,7 @@ private slots:
void hasChaptersTrueForMultipleChapters()
{
ReadingModel model;
FakeReader reader; // 3 chapters
FakeReader reader;
model.setReader(&reader);
QVERIFY(model.hasChapters());