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

+98
View File
@@ -0,0 +1,98 @@
#include <QtTest>
#include <QByteArray>
#include <QDir>
#include <QSettings>
#include <QTemporaryFile>
#include "backend/AppState.h"
#include "backend/state/BookPositionStore.h"
class BookPositionStoreTest : public QObject {
Q_OBJECT
private slots:
void defaultPositionForUnknownHash()
{
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/bps-def-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState appState(s);
BookPositionStore store(appState);
const BookPosition pos = store.load(QByteArray("nonexistent"));
QCOMPARE(pos.chunkIndex, 0);
QCOMPARE(pos.intraOffset, 0);
}
void saveAndLoad()
{
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/bps-sr-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState appState(s);
BookPositionStore store(appState);
const QByteArray hash = QByteArray("abc123deadbeef");
store.save(hash, 7, 42);
const BookPosition pos = store.load(hash);
QCOMPARE(pos.chunkIndex, 7);
QCOMPARE(pos.intraOffset, 42);
}
void separateHashesStoredIndependently()
{
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/bps-sep-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState appState(s);
BookPositionStore store(appState);
store.save(QByteArray("hash1"), 1, 10);
store.save(QByteArray("hash2"), 2, 20);
QCOMPARE(store.load(QByteArray("hash1")).chunkIndex, 1);
QCOMPARE(store.load(QByteArray("hash2")).chunkIndex, 2);
QCOMPARE(store.load(QByteArray("hash1")).intraOffset, 10);
QCOMPARE(store.load(QByteArray("hash2")).intraOffset, 20);
}
void overwriteExistingPosition()
{
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/bps-ow-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState appState(s);
BookPositionStore store(appState);
const QByteArray hash = QByteArray("overwrite-hash");
store.save(hash, 3, 30);
store.save(hash, 5, 55);
const BookPosition pos = store.load(hash);
QCOMPARE(pos.chunkIndex, 5);
QCOMPARE(pos.intraOffset, 55);
}
void emptyHashIgnored()
{
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/bps-empty-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState appState(s);
BookPositionStore store(appState);
// Neither save nor load should crash with an empty hash
store.save(QByteArray(), 1, 1);
const BookPosition pos = store.load(QByteArray());
QCOMPARE(pos.chunkIndex, 0);
QCOMPARE(pos.intraOffset, 0);
}
};
QTEST_MAIN(BookPositionStoreTest)
#include "BookPositionStoreTest.moc"
+81
View File
@@ -0,0 +1,81 @@
#include <QtTest>
#include <QDir>
#include <QFile>
#include <QSettings>
#include <QTemporaryFile>
#include "backend/AppState.h"
#include "backend/state/ReadingPreferences.h"
class ReadingPreferencesTest : public QObject {
Q_OBJECT
private slots:
void defaultsWhenEmpty()
{
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/rp-def-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState appState(s);
ReadingPreferences prefs(appState);
QCOMPARE(prefs.fontSize(), 16);
QCOMPARE(prefs.lineSpacing(), 1.6);
QCOMPARE(prefs.horizontalMargin(), 32);
QCOMPARE(prefs.verticalMargin(), 8);
}
void saveAndRestore()
{
QString fileName;
{
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + QStringLiteral("/rp-sr-XXXXXX.ini"));
tmp.setAutoRemove(false);
QVERIFY(tmp.open());
fileName = tmp.fileName();
}
{
QSettings s(fileName, QSettings::IniFormat);
AppState appState(s);
ReadingPreferences prefs(appState);
prefs.setFontSize(20);
prefs.setLineSpacing(2.0);
prefs.setHorizontalMargin(48);
prefs.setVerticalMargin(16);
prefs.save();
}
QSettings s2(fileName, QSettings::IniFormat);
AppState appState2(s2);
ReadingPreferences prefs2(appState2);
QCOMPARE(prefs2.fontSize(), 20);
QCOMPARE(prefs2.lineSpacing(), 2.0);
QCOMPARE(prefs2.horizontalMargin(), 48);
QCOMPARE(prefs2.verticalMargin(), 16);
QFile::remove(fileName);
}
void fontSizeSignal()
{
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/rp-sig-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState appState(s);
ReadingPreferences prefs(appState);
QSignalSpy spy(&prefs, &ReadingPreferences::fontSizeChanged);
prefs.setFontSize(18);
QCOMPARE(spy.count(), 1);
prefs.setFontSize(18); // no-op
QCOMPARE(spy.count(), 1);
}
};
QTEST_MAIN(ReadingPreferencesTest)
#include "ReadingPreferencesTest.moc"
+109
View File
@@ -0,0 +1,109 @@
#include <QtTest>
#include <QDir>
#include <QFile>
#include <QSettings>
#include <QTemporaryFile>
#include "backend/AppState.h"
#include "backend/state/UiState.h"
class UiStateTest : public QObject {
Q_OBJECT
private slots:
void defaultsWhenEmpty()
{
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/ui-def-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState appState(s);
UiState ui(appState);
QCOMPARE(ui.themeName(), QStringLiteral("dark"));
QCOMPARE(ui.lastPage(), QStringLiteral("starter"));
QCOMPARE(ui.sidebarOpen(), false);
QCOMPARE(ui.lastFilePath(), QString());
QCOMPARE(ui.windowWidth(), 1024);
QCOMPARE(ui.windowHeight(), 768);
QCOMPARE(ui.windowX(), -1);
QCOMPARE(ui.windowY(), -1);
}
void saveAndRestore()
{
QString fileName;
{
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + QStringLiteral("/ui-sr-XXXXXX.ini"));
tmp.setAutoRemove(false);
QVERIFY(tmp.open());
fileName = tmp.fileName();
}
{
QSettings s(fileName, QSettings::IniFormat);
AppState appState(s);
UiState ui(appState);
ui.setThemeName(QStringLiteral("light"));
ui.setLastPage(QStringLiteral("settings"));
ui.setSidebarOpen(true);
ui.setLastFilePath(QStringLiteral("/some/file.epub"));
ui.setWindowWidth(1280);
ui.setWindowHeight(800);
ui.setWindowX(100);
ui.setWindowY(50);
ui.save();
}
QSettings s2(fileName, QSettings::IniFormat);
AppState appState2(s2);
UiState ui2(appState2);
QCOMPARE(ui2.themeName(), QStringLiteral("light"));
QCOMPARE(ui2.lastPage(), QStringLiteral("settings"));
QCOMPARE(ui2.sidebarOpen(), true);
QCOMPARE(ui2.lastFilePath(), QStringLiteral("/some/file.epub"));
QCOMPARE(ui2.windowWidth(), 1280);
QCOMPARE(ui2.windowHeight(), 800);
QCOMPARE(ui2.windowX(), 100);
QCOMPARE(ui2.windowY(), 50);
QFile::remove(fileName);
}
void lastFilePathSignal()
{
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/ui-sig-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState appState(s);
UiState ui(appState);
QSignalSpy spy(&ui, &UiState::lastFilePathChanged);
ui.setLastFilePath(QStringLiteral("/a/b"));
QCOMPARE(spy.count(), 1);
ui.setLastFilePath(QStringLiteral("/a/b")); // no-op
QCOMPARE(spy.count(), 1);
}
void windowWidthSignal()
{
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/ui-sig-w-XXXXXX.ini"));
f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState appState(s);
UiState ui(appState);
QSignalSpy spy(&ui, &UiState::windowWidthChanged);
ui.setWindowWidth(999);
QCOMPARE(spy.count(), 1);
ui.setWindowWidth(999); // no-op
QCOMPARE(spy.count(), 1);
}
};
QTEST_MAIN(UiStateTest)
#include "UiStateTest.moc"