164 lines
4.2 KiB
C++
164 lines
4.2 KiB
C++
#include <QtTest>
|
|
#include <QSignalSpy>
|
|
#include "backend/ReadingModel.h"
|
|
#include "backend/FormatReader.h"
|
|
|
|
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>Content %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.chapterContent().isEmpty());
|
|
QVERIFY(model.chapterTitle().isEmpty());
|
|
}
|
|
|
|
void setReaderLoadsFirstChapter()
|
|
{
|
|
ReadingModel model;
|
|
FakeReader reader;
|
|
model.setReader(&reader);
|
|
|
|
QCOMPARE(model.currentChapter(), 0);
|
|
QCOMPARE(model.totalChapters(), 3);
|
|
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 0"));
|
|
QCOMPARE(model.chapterContent(), QStringLiteral("<p>Content 0</p>"));
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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.chapterContent(), QStringLiteral("<p>Content 2</p>"));
|
|
}
|
|
|
|
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.chapterContent().isEmpty());
|
|
QVERIFY(model.chapterTitle().isEmpty());
|
|
}
|
|
};
|
|
|
|
QTEST_MAIN(ReadingModelTest)
|
|
#include "ReadingModelTest.moc"
|