110 lines
3.3 KiB
C++
110 lines
3.3 KiB
C++
#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"
|