Files
asteria-qt/tests/ReadingModelTest.cpp
T
2026-03-23 23:28:23 +01:00

331 lines
9.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <QtTest>
#include <QSignalSpy>
#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, {}, {"<p>text</p>"}}; }
QList<TocEntry> tableOfContents() override { return {}; }
QString title() override { return {}; }
QString author() override { return {}; }
};
// 3 chapters, 2 chunks each → 6 total chunks
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("<p>Chunk A of %1</p>").arg(index),
QStringLiteral("<p>Chunk B of %1</p>").arg(index)}};
}
QList<TocEntry> 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 setReaderLoadsAllChapters()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
// 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 chunkContentAcrossChapters()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
// 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(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()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QModelIndex badIdx = model.index(99, 0);
QVERIFY(!model.data(badIdx, ReadingModel::ContentRole).isValid());
}
void currentChapterDerivedFromScroll()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
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"));
// 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 chapterChangedSignalOnScroll()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
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 goToChapterEmitsScrollSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.goToChapter(1);
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.at(0).at(0).toInt(), 2); // chapter 1 starts at chunk 2
model.goToChapter(2);
QCOMPARE(spy.count(), 2);
QCOMPARE(spy.at(1).at(0).toInt(), 4); // chapter 2 starts at chunk 4
}
void goToChapterOutOfRangeNoSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.goToChapter(-1);
model.goToChapter(99);
QCOMPARE(spy.count(), 0);
}
void nextChapterEmitsScrollSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
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);
// 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);
// At chunk 0 again: progress = 0
model.setScrollInfo(0, 0, false);
QCOMPARE(model.progress(), 0.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 roleNamesContainsContent()
{
ReadingModel model;
QHash<int, QByteArray> 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 totalPagesEqualsAllChunks()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QCOMPARE(model.totalPages(), 6); // 3 chapters × 2 chunks
QCOMPARE(model.currentPage(), 1);
}
void setScrollInfoUpdatesCurrentPage()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setScrollInfo(0, 0, false);
QCOMPARE(model.currentPage(), 1);
model.setScrollInfo(3, 0, false);
QCOMPARE(model.currentPage(), 4);
model.setScrollInfo(5, 0, false);
QCOMPARE(model.currentPage(), 6);
}
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;
model.setReader(&reader);
QVERIFY(model.hasChapters());
}
void hasChaptersFalseInitially()
{
ReadingModel model;
QVERIFY(!model.hasChapters());
}
};
QTEST_MAIN(ReadingModelTest)
#include "ReadingModelTest.moc"