Some refactor

This commit is contained in:
Agel committed 2026-04-08 14:01:03 +02:00
1 parent 7ecb7f4f4f
commit 9280c838b2
37 files changed
+1599 -981

No files matched your search

+87 -402
View File
@@ -1,437 +1,122 @@
#include <QtTest>
#include <QSignalSpy>
#include <QDir>
#include <QSettings>
#include <QTemporaryFile>
#include "backend/AppState.h"
#include "backend/LibraryModel.h"
class AppStateTest : public QObject {
Q_OBJECT
private slots:
void initTestCase()
void freshSettingsWriteVersion()
{
QCoreApplication::setOrganizationName(QStringLiteral("AsteriaTest"));
QCoreApplication::setApplicationName(QStringLiteral("AppStateTest"));
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/asteria-test-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState state(s);
QCOMPARE(s.value(QStringLiteral("version")).toInt(), AppState::SETTINGS_VERSION);
}
void cleanup()
void staleVersionClearsSettings()
{
QSettings().clear();
}
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/asteria-stale-XXXXXX.ini"));
f.open(); f.close();
void saveAndRestoreLastFilePath()
{
{
AppState state;
state.setLastFilePath(QStringLiteral("/books/test.epub"));
state.save();
QSettings s(f.fileName(), QSettings::IniFormat);
s.setValue(QStringLiteral("version"), 0);
s.setValue(QStringLiteral("someOldKey"), QStringLiteral("data"));
}
AppState state2;
state2.restore();
QCOMPARE(state2.lastFilePath(), QStringLiteral("/books/test.epub"));
QSettings s2(f.fileName(), QSettings::IniFormat);
AppState state(s2);
QVERIFY(!s2.contains(QStringLiteral("someOldKey")));
QCOMPARE(s2.value(QStringLiteral("version")).toInt(), AppState::SETTINGS_VERSION);
}
void saveAndRestoreMargins()
void saveAndLoadSection()
{
{
AppState state;
state.setHorizontalMargin(64);
state.setVerticalMargin(24);
state.save();
}
AppState state2;
state2.restore();
QCOMPARE(state2.horizontalMargin(), 64);
QCOMPARE(state2.verticalMargin(), 24);
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/asteria-sec-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState state(s);
QVariantMap data;
data[QStringLiteral("foo")] = 42;
data[QStringLiteral("bar")] = QStringLiteral("hello");
state.saveSection(QStringLiteral("test"), data);
const QVariantMap loaded = state.loadSection(QStringLiteral("test"));
QCOMPARE(loaded.value(QStringLiteral("foo")).toInt(), 42);
QCOMPARE(loaded.value(QStringLiteral("bar")).toString(), QStringLiteral("hello"));
}
void saveAndRestoreFontSize()
void saveSectionOverwritesPrevious()
{
{
AppState state;
state.setFontSize(22);
state.save();
}
AppState state2;
state2.restore();
QCOMPARE(state2.fontSize(), 22);
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/asteria-over-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState state(s);
state.saveSection(QStringLiteral("g"), {{QStringLiteral("old"), 1}});
state.saveSection(QStringLiteral("g"), {{QStringLiteral("new"), 2}});
const QVariantMap loaded = state.loadSection(QStringLiteral("g"));
QVERIFY(!loaded.contains(QStringLiteral("old")));
QCOMPARE(loaded.value(QStringLiteral("new")).toInt(), 2);
}
void saveAndRestoreLineSpacing()
void saveAndLoadArray()
{
{
AppState state;
state.setLineSpacing(2.0);
state.save();
}
AppState state2;
state2.restore();
QCOMPARE(state2.lineSpacing(), 2.0);
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/asteria-arr-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState state(s);
QList<QVariantMap> items;
items.append({{QStringLiteral("filePath"), QStringLiteral("/a/b.epub")},
{QStringLiteral("progress"), 0.5}});
items.append({{QStringLiteral("filePath"), QStringLiteral("/c/d.txt")},
{QStringLiteral("progress"), 0.0}});
state.saveArray(QStringLiteral("library"), items);
const auto loaded = state.loadArray(QStringLiteral("library"));
QCOMPARE(loaded.size(), 2);
QCOMPARE(loaded[0].value(QStringLiteral("filePath")).toString(), QStringLiteral("/a/b.epub"));
QCOMPARE(loaded[0].value(QStringLiteral("progress")).toDouble(), 0.5);
QCOMPARE(loaded[1].value(QStringLiteral("filePath")).toString(), QStringLiteral("/c/d.txt"));
}
void saveAndRestoreWindowGeometry()
void emptyArrayReturnsEmptyList()
{
{
AppState state;
state.setWindowWidth(1280);
state.setWindowHeight(800);
state.setWindowX(100);
state.setWindowY(50);
state.save();
}
AppState state2;
state2.restore();
QCOMPARE(state2.windowWidth(), 1280);
QCOMPARE(state2.windowHeight(), 800);
QCOMPARE(state2.windowX(), 100);
QCOMPARE(state2.windowY(), 50);
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/asteria-earr-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState state(s);
QCOMPARE(state.loadArray(QStringLiteral("nonexistent")).size(), 0);
}
void restoreWithNoSavedData()
void nestedSectionKey()
{
AppState state;
state.restore();
QVERIFY(state.lastFilePath().isEmpty());
QCOMPARE(state.horizontalMargin(), 32);
QCOMPARE(state.verticalMargin(), 8);
QCOMPARE(state.fontSize(), 16);
QCOMPARE(state.lineSpacing(), 1.6);
QCOMPARE(state.windowWidth(), 1024);
QCOMPARE(state.windowHeight(), 768);
QCOMPARE(state.windowX(), -1);
QCOMPARE(state.windowY(), -1);
}
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/asteria-nest-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState state(s);
void settersEmitSignals()
{
AppState state;
QSignalSpy spyFilePath(&state, &AppState::lastFilePathChanged);
QSignalSpy spyHMargin(&state, &AppState::horizontalMarginChanged);
QSignalSpy spyVMargin(&state, &AppState::verticalMarginChanged);
QSignalSpy spyFontSize(&state, &AppState::fontSizeChanged);
QSignalSpy spyLineSpacing(&state, &AppState::lineSpacingChanged);
QSignalSpy spyWidth(&state, &AppState::windowWidthChanged);
QSignalSpy spyHeight(&state, &AppState::windowHeightChanged);
QSignalSpy spyX(&state, &AppState::windowXChanged);
QSignalSpy spyY(&state, &AppState::windowYChanged);
const QString nestedSection = QStringLiteral("saved-progress/abc123");
state.saveSection(nestedSection, {{QStringLiteral("chunkIndex"), 5}});
state.setLastFilePath(QStringLiteral("/foo.txt"));
state.setHorizontalMargin(64);
state.setVerticalMargin(24);
state.setFontSize(20);
state.setLineSpacing(2.0);
state.setWindowWidth(800);
state.setWindowHeight(600);
state.setWindowX(200);
state.setWindowY(150);
QCOMPARE(spyFilePath.count(), 1);
QCOMPARE(spyHMargin.count(), 1);
QCOMPARE(spyVMargin.count(), 1);
QCOMPARE(spyFontSize.count(), 1);
QCOMPARE(spyLineSpacing.count(), 1);
QCOMPARE(spyWidth.count(), 1);
QCOMPARE(spyHeight.count(), 1);
QCOMPARE(spyX.count(), 1);
QCOMPARE(spyY.count(), 1);
}
void saveAndLoadBookPosition()
{
AppState state;
state.saveBookPosition(QStringLiteral("/books/a.epub"), 5, 120);
QVariantMap pos = state.loadBookPosition(QStringLiteral("/books/a.epub"));
QCOMPARE(pos[QStringLiteral("chunkIndex")].toInt(), 5);
QCOMPARE(pos[QStringLiteral("intraChunkOffset")].toInt(), 120);
}
void loadBookPositionForUnknownBook()
{
AppState state;
QVariantMap pos = state.loadBookPosition(QStringLiteral("/books/unknown.epub"));
QCOMPARE(pos[QStringLiteral("chunkIndex")].toInt(), 0);
QCOMPARE(pos[QStringLiteral("intraChunkOffset")].toInt(), 0);
}
void saveBookPositionForMultipleBooks()
{
AppState state;
state.saveBookPosition(QStringLiteral("/books/a.epub"), 10, 50);
state.saveBookPosition(QStringLiteral("/books/b.epub"), 20, 200);
QVariantMap posA = state.loadBookPosition(QStringLiteral("/books/a.epub"));
QVariantMap posB = state.loadBookPosition(QStringLiteral("/books/b.epub"));
QCOMPARE(posA[QStringLiteral("chunkIndex")].toInt(), 10);
QCOMPARE(posA[QStringLiteral("intraChunkOffset")].toInt(), 50);
QCOMPARE(posB[QStringLiteral("chunkIndex")].toInt(), 20);
QCOMPARE(posB[QStringLiteral("intraChunkOffset")].toInt(), 200);
}
void saveBookPositionOverwritesPrevious()
{
AppState state;
state.saveBookPosition(QStringLiteral("/books/a.epub"), 10, 50);
state.saveBookPosition(QStringLiteral("/books/a.epub"), 15, 88);
QVariantMap pos = state.loadBookPosition(QStringLiteral("/books/a.epub"));
QCOMPARE(pos[QStringLiteral("chunkIndex")].toInt(), 15);
QCOMPARE(pos[QStringLiteral("intraChunkOffset")].toInt(), 88);
}
void restoreEmitsSingleSignal()
{
AppState state;
QSignalSpy spyRestored(&state, &AppState::stateRestored);
QSignalSpy spyFilePath(&state, &AppState::lastFilePathChanged);
QSignalSpy spyHMargin(&state, &AppState::horizontalMarginChanged);
QSignalSpy spyVMargin(&state, &AppState::verticalMarginChanged);
QSignalSpy spyFontSize(&state, &AppState::fontSizeChanged);
QSignalSpy spyLineSpacing(&state, &AppState::lineSpacingChanged);
QSignalSpy spyWidth(&state, &AppState::windowWidthChanged);
QSignalSpy spyHeight(&state, &AppState::windowHeightChanged);
QSignalSpy spyX(&state, &AppState::windowXChanged);
QSignalSpy spyY(&state, &AppState::windowYChanged);
QSignalSpy spyTheme(&state, &AppState::themeNameChanged);
state.restore();
QCOMPARE(spyRestored.count(), 1);
QCOMPARE(spyFilePath.count(), 0);
QCOMPARE(spyHMargin.count(), 0);
QCOMPARE(spyVMargin.count(), 0);
QCOMPARE(spyFontSize.count(), 0);
QCOMPARE(spyLineSpacing.count(), 0);
QCOMPARE(spyWidth.count(), 0);
QCOMPARE(spyHeight.count(), 0);
QCOMPARE(spyX.count(), 0);
QCOMPARE(spyY.count(), 0);
QCOMPARE(spyTheme.count(), 0);
}
void saveAndRestoreThemeName()
{
{
AppState state;
state.setThemeName(QStringLiteral("light"));
state.save();
}
AppState state2;
state2.restore();
QCOMPARE(state2.themeName(), QStringLiteral("light"));
}
void themeNameDefaultsToDark()
{
AppState state;
QCOMPARE(state.themeName(), QStringLiteral("dark"));
}
void themeNameSetterEmitsSignal()
{
AppState state;
QSignalSpy spy(&state, &AppState::themeNameChanged);
state.setThemeName(QStringLiteral("light"));
QCOMPARE(spy.count(), 1);
}
void themeNameSetterDoesNotEmitForSameValue()
{
AppState state;
QSignalSpy spy(&state, &AppState::themeNameChanged);
state.setThemeName(QStringLiteral("dark")); // Same as default
QCOMPARE(spy.count(), 0);
}
// Library tests
void saveAndRestoreLastPage()
{
{
AppState state;
state.setLastPage(QStringLiteral("library"));
state.save();
}
AppState state2;
state2.restore();
QCOMPARE(state2.lastPage(), QStringLiteral("library"));
}
void lastPageDefaultsToStarter()
{
AppState state;
QCOMPARE(state.lastPage(), QStringLiteral("starter"));
}
void libraryBooksInitiallyEmpty()
{
AppState state;
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QVERIFY(model->rowCount() == 0);
}
void addLibraryBookAddsEntry()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.0);
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 1);
// Get data from the model
QModelIndex index = model->index(0);
QString filePath = model->data(index, LibraryModel::FilePathRole).toString();
double progress = model->data(index, LibraryModel::ProgressRole).toDouble();
QCOMPARE(filePath, QStringLiteral("/books/a.epub"));
QCOMPARE(progress, 0.0);
// Title and author are now loaded asynchronously from file metadata
// QString title = model->data(index, 2).toString(); // TitleRole = 2
// QString author = model->data(index, 3).toString(); // AuthorRole = 3
// QCOMPARE(title, QStringLiteral("Title A"));
// QCOMPARE(author, QStringLiteral("Author A"));
}
void addLibraryBookPersistsThroughSaveRestore()
{
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.5);
state.save();
}
AppState state2;
state2.restore();
auto* model = qobject_cast<QAbstractListModel*>(state2.libraryModel());
QCOMPARE(model->rowCount(), 1);
QModelIndex index = model->index(0);
QString filePath = model->data(index, LibraryModel::FilePathRole).toString();
double progress = model->data(index, LibraryModel::ProgressRole).toDouble();
QCOMPARE(filePath, QStringLiteral("/books/a.epub"));
// Title is now loaded asynchronously from file metadata
// QString title = model->data(index, 2).toString(); // TitleRole = 2
// QCOMPARE(title, QStringLiteral("Title A"));
QCOMPARE(progress, 0.5);
}
void addLibraryBookUpdatesMetadata()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.3);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.3);
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 1);
QModelIndex index = model->index(0);
// Title and author are now loaded asynchronously from file metadata
// QString title = model->data(index, 2).toString(); // TitleRole = 2
// QString author = model->data(index, 3).toString(); // AuthorRole = 3
// QCOMPARE(title, QStringLiteral("New Title"));
// QCOMPARE(author, QStringLiteral("New Author"));
}
void addLibraryBookOverwritesProgress()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.6);
// Lower value should overwrite (always update, no max logic)
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.2);
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 1);
QModelIndex index = model->index(0);
double progress = model->data(index, LibraryModel::ProgressRole).toDouble();
QCOMPARE(progress, 0.2);
}
void addLibraryBookSentinelPreservesProgress()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.5);
// -1.0 sentinel: update metadata but don't touch progress
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), -1.0);
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 1);
QModelIndex index = model->index(0);
double progress = model->data(index, LibraryModel::ProgressRole).toDouble();
QCOMPARE(progress, 0.5);
// Title is now loaded asynchronously from file metadata
// QString title = model->data(index, 2).toString(); // TitleRole = 2
// QCOMPARE(title, QStringLiteral("New Title"));
}
void addNewLibraryBookWithSentinelStartsAtZero()
{
AppState state;
// New book added with sentinel should start at 0%
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), -1.0);
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 1);
QModelIndex index = model->index(0);
double progress = model->data(index, LibraryModel::ProgressRole).toDouble();
QCOMPARE(progress, 0.0);
}
void removeLibraryBookRemovesEntry()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.5);
state.addOrUpdateLibraryBook(QStringLiteral("/books/b.epub"), 0.3);
state.removeLibraryBook(QStringLiteral("/books/a.epub"));
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 1);
QModelIndex index = model->index(0);
QString filePath = model->data(index, LibraryModel::FilePathRole).toString();
QCOMPARE(filePath, QStringLiteral("/books/b.epub"));
}
void removeNonexistentLibraryBookIsNoop()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.5);
state.removeLibraryBook(QStringLiteral("/books/nonexistent.epub"));
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 1);
}
void libraryBooksChangedSignalOnAdd()
{
AppState state;
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QSignalSpy spy(model, &QAbstractListModel::rowsInserted);
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.0);
QCOMPARE(spy.count(), 1);
}
void libraryBooksChangedSignalOnRemove()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.0);
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QSignalSpy spy(model, &QAbstractListModel::rowsRemoved);
state.removeLibraryBook(QStringLiteral("/books/a.epub"));
QCOMPARE(spy.count(), 1);
}
void multipleLibraryBooksAreStoredInOrder()
{
AppState state;
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"), 0.1);
state.addOrUpdateLibraryBook(QStringLiteral("/books/b.epub"), 0.2);
auto* model = qobject_cast<QAbstractListModel*>(state.libraryModel());
QCOMPARE(model->rowCount(), 2);
// Most recently added/updated comes first
QModelIndex index0 = model->index(0);
QModelIndex index1 = model->index(1);
QString filePath0 = model->data(index0, LibraryModel::FilePathRole).toString();
QString filePath1 = model->data(index1, LibraryModel::FilePathRole).toString();
QCOMPARE(filePath0, QStringLiteral("/books/b.epub"));
QCOMPARE(filePath1, QStringLiteral("/books/a.epub"));
const QVariantMap loaded = state.loadSection(nestedSection);
QCOMPARE(loaded.value(QStringLiteral("chunkIndex")).toInt(), 5);
}
};