163 lines
4.5 KiB
C++
163 lines
4.5 KiB
C++
#include <QtTest>
|
|
#include <QSignalSpy>
|
|
#include <QUrl>
|
|
#include <QSettings>
|
|
#include "backend/BookController.h"
|
|
#include "backend/ReadingModel.h"
|
|
#include "backend/AppState.h"
|
|
|
|
class BookControllerTest : public QObject {
|
|
Q_OBJECT
|
|
|
|
private:
|
|
QString fixturePath(const QString &filename) const
|
|
{
|
|
return QStringLiteral(FIXTURES_DIR) + "/" + filename;
|
|
}
|
|
|
|
private slots:
|
|
void initTestCase()
|
|
{
|
|
QCoreApplication::setOrganizationName(QStringLiteral("AsteriaTest"));
|
|
QCoreApplication::setApplicationName(QStringLiteral("BookControllerTest"));
|
|
}
|
|
|
|
void cleanup()
|
|
{
|
|
QSettings().clear();
|
|
}
|
|
void openValidBook()
|
|
{
|
|
BookController ctrl;
|
|
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
|
|
|
|
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
|
|
|
|
QCOMPARE(readySpy.count(), 1);
|
|
QVERIFY(ctrl.hasBook());
|
|
QCOMPARE(ctrl.bookTitle(), QStringLiteral("sample"));
|
|
}
|
|
|
|
void openInvalidPath()
|
|
{
|
|
BookController ctrl;
|
|
QSignalSpy errorSpy(&ctrl, &BookController::bookError);
|
|
|
|
ctrl.openBook(QUrl::fromLocalFile("/tmp/nonexistent_asteria_file.txt"));
|
|
|
|
QCOMPARE(errorSpy.count(), 1);
|
|
QVERIFY(!ctrl.errorMessage().isEmpty());
|
|
QVERIFY(!ctrl.hasBook());
|
|
}
|
|
|
|
void openUnsupportedFormat()
|
|
{
|
|
BookController ctrl;
|
|
QSignalSpy errorSpy(&ctrl, &BookController::bookError);
|
|
|
|
ctrl.openBook(QUrl::fromLocalFile("/tmp/file.xyz"));
|
|
|
|
QCOMPARE(errorSpy.count(), 1);
|
|
QCOMPARE(ctrl.errorMessage(), QStringLiteral("No format readers available"));
|
|
QVERIFY(!ctrl.hasBook());
|
|
}
|
|
|
|
void closeBook()
|
|
{
|
|
BookController ctrl;
|
|
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
|
|
QVERIFY(ctrl.hasBook());
|
|
|
|
QSignalSpy changedSpy(&ctrl, &BookController::bookChanged);
|
|
ctrl.closeBook();
|
|
|
|
QVERIFY(!ctrl.hasBook());
|
|
QVERIFY(ctrl.bookTitle().isEmpty());
|
|
QVERIFY(ctrl.bookAuthor().isEmpty());
|
|
QCOMPARE(changedSpy.count(), 1);
|
|
}
|
|
|
|
void openBookSetsReadingModel()
|
|
{
|
|
BookController ctrl;
|
|
ReadingModel model;
|
|
ctrl.setReadingModel(&model);
|
|
|
|
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
|
|
|
|
QVERIFY(model.totalChapters() > 0);
|
|
}
|
|
|
|
void loadingTransitions()
|
|
{
|
|
BookController ctrl;
|
|
QSignalSpy loadingSpy(&ctrl, &BookController::loadingChanged);
|
|
|
|
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
|
|
|
|
// loading goes true then false → 2 emissions
|
|
QCOMPARE(loadingSpy.count(), 2);
|
|
// After completion, loading should be false
|
|
QVERIFY(!ctrl.loading());
|
|
}
|
|
|
|
void currentFilePathInitiallyEmpty()
|
|
{
|
|
BookController ctrl;
|
|
QVERIFY(ctrl.currentFilePath().isEmpty());
|
|
}
|
|
|
|
void currentFilePathSetOnOpen()
|
|
{
|
|
BookController ctrl;
|
|
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
|
|
QCOMPARE(ctrl.currentFilePath(), fixturePath("sample.txt"));
|
|
}
|
|
|
|
void currentFilePathClearedOnClose()
|
|
{
|
|
BookController ctrl;
|
|
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
|
|
ctrl.closeBook();
|
|
QVERIFY(ctrl.currentFilePath().isEmpty());
|
|
}
|
|
|
|
void clearErrorResetsMessage()
|
|
{
|
|
BookController ctrl;
|
|
ctrl.openBook(QUrl::fromLocalFile("/tmp/file.xyz"));
|
|
QVERIFY(!ctrl.errorMessage().isEmpty());
|
|
|
|
QSignalSpy spy(&ctrl, &BookController::errorMessageChanged);
|
|
ctrl.clearError();
|
|
|
|
QVERIFY(ctrl.errorMessage().isEmpty());
|
|
QCOMPARE(spy.count(), 1);
|
|
}
|
|
|
|
void openBookSavesPositionOfPrevious()
|
|
{
|
|
AppState appState;
|
|
ReadingModel model;
|
|
BookController ctrl;
|
|
ctrl.setReadingModel(&model);
|
|
ctrl.setAppState(&appState);
|
|
|
|
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
|
|
QVERIFY(ctrl.hasBook());
|
|
QString firstPath = ctrl.currentFilePath();
|
|
|
|
// Attempt to open a second book — position of first book should be saved
|
|
// regardless of whether the second open succeeds
|
|
ctrl.openBook(QUrl::fromLocalFile("/tmp/file.xyz"));
|
|
|
|
// Position for sample.txt should have been saved (chapter 0 at minimum)
|
|
QVariantMap pos = appState.loadBookPosition(firstPath);
|
|
QVERIFY(pos.contains("chapter"));
|
|
QCOMPARE(pos["chapter"].toInt(), 0);
|
|
}
|
|
};
|
|
|
|
QTEST_MAIN(BookControllerTest)
|
|
#include "BookControllerTest.moc"
|