439 lines
16 KiB
C++
439 lines
16 KiB
C++
#include <QtTest>
|
|
#include <QSignalSpy>
|
|
#include <QSettings>
|
|
#include "backend/AppState.h"
|
|
|
|
class AppStateTest : public QObject {
|
|
Q_OBJECT
|
|
|
|
private slots:
|
|
void initTestCase()
|
|
{
|
|
QCoreApplication::setOrganizationName(QStringLiteral("AsteriaTest"));
|
|
QCoreApplication::setApplicationName(QStringLiteral("AppStateTest"));
|
|
}
|
|
|
|
void cleanup()
|
|
{
|
|
QSettings().clear();
|
|
}
|
|
|
|
void saveAndRestoreLastFilePath()
|
|
{
|
|
{
|
|
AppState state;
|
|
state.setLastFilePath(QStringLiteral("/books/test.epub"));
|
|
state.save();
|
|
}
|
|
AppState state2;
|
|
state2.restore();
|
|
QCOMPARE(state2.lastFilePath(), QStringLiteral("/books/test.epub"));
|
|
}
|
|
|
|
void saveAndRestoreLastChapter()
|
|
{
|
|
{
|
|
AppState state;
|
|
state.setLastChapter(5);
|
|
state.save();
|
|
}
|
|
AppState state2;
|
|
state2.restore();
|
|
QCOMPARE(state2.lastChapter(), 5);
|
|
}
|
|
|
|
void saveAndRestoreLastScrollPosition()
|
|
{
|
|
{
|
|
AppState state;
|
|
state.setLastScrollPosition(542.5);
|
|
state.save();
|
|
}
|
|
AppState state2;
|
|
state2.restore();
|
|
QCOMPARE(state2.lastScrollPosition(), 542.5);
|
|
}
|
|
|
|
void saveAndRestoreLastScrollIndex()
|
|
{
|
|
{
|
|
AppState state;
|
|
state.setLastScrollIndex(42);
|
|
state.save();
|
|
}
|
|
AppState state2;
|
|
state2.restore();
|
|
QCOMPARE(state2.lastScrollIndex(), 42);
|
|
}
|
|
|
|
void saveAndRestoreLastScrollOffset()
|
|
{
|
|
{
|
|
AppState state;
|
|
state.setLastScrollOffset(123.75);
|
|
state.save();
|
|
}
|
|
AppState state2;
|
|
state2.restore();
|
|
QCOMPARE(state2.lastScrollOffset(), 123.75);
|
|
}
|
|
|
|
void saveAndRestorePageFlipMode()
|
|
{
|
|
{
|
|
AppState state;
|
|
state.setPageFlipMode(QStringLiteral("page"));
|
|
state.save();
|
|
}
|
|
AppState state2;
|
|
state2.restore();
|
|
QCOMPARE(state2.pageFlipMode(), QStringLiteral("page"));
|
|
}
|
|
|
|
void saveAndRestoreWindowGeometry()
|
|
{
|
|
{
|
|
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);
|
|
}
|
|
|
|
void restoreWithNoSavedData()
|
|
{
|
|
AppState state;
|
|
state.restore();
|
|
QVERIFY(state.lastFilePath().isEmpty());
|
|
QCOMPARE(state.lastChapter(), 0);
|
|
QCOMPARE(state.lastScrollPosition(), 0.0);
|
|
QCOMPARE(state.lastScrollIndex(), 0);
|
|
QCOMPARE(state.lastScrollOffset(), 0.0);
|
|
QCOMPARE(state.pageFlipMode(), QStringLiteral("scroll"));
|
|
QCOMPARE(state.windowWidth(), 1024);
|
|
QCOMPARE(state.windowHeight(), 768);
|
|
QCOMPARE(state.windowX(), -1);
|
|
QCOMPARE(state.windowY(), -1);
|
|
}
|
|
|
|
void settersEmitSignals()
|
|
{
|
|
AppState state;
|
|
QSignalSpy spyFilePath(&state, &AppState::lastFilePathChanged);
|
|
QSignalSpy spyChapter(&state, &AppState::lastChapterChanged);
|
|
QSignalSpy spyScroll(&state, &AppState::lastScrollPositionChanged);
|
|
QSignalSpy spyScrollIndex(&state, &AppState::lastScrollIndexChanged);
|
|
QSignalSpy spyScrollOffset(&state, &AppState::lastScrollOffsetChanged);
|
|
QSignalSpy spyFlip(&state, &AppState::pageFlipModeChanged);
|
|
QSignalSpy spyWidth(&state, &AppState::windowWidthChanged);
|
|
QSignalSpy spyHeight(&state, &AppState::windowHeightChanged);
|
|
QSignalSpy spyX(&state, &AppState::windowXChanged);
|
|
QSignalSpy spyY(&state, &AppState::windowYChanged);
|
|
|
|
state.setLastFilePath(QStringLiteral("/foo.txt"));
|
|
state.setLastChapter(3);
|
|
state.setLastScrollPosition(10.5);
|
|
state.setLastScrollIndex(7);
|
|
state.setLastScrollOffset(55.25);
|
|
state.setPageFlipMode(QStringLiteral("page"));
|
|
state.setWindowWidth(800);
|
|
state.setWindowHeight(600);
|
|
state.setWindowX(200);
|
|
state.setWindowY(150);
|
|
|
|
QCOMPARE(spyFilePath.count(), 1);
|
|
QCOMPARE(spyChapter.count(), 1);
|
|
QCOMPARE(spyScroll.count(), 1);
|
|
QCOMPARE(spyScrollIndex.count(), 1);
|
|
QCOMPARE(spyScrollOffset.count(), 1);
|
|
QCOMPARE(spyFlip.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"), 3, 42, 123.5);
|
|
|
|
QVariantMap pos = state.loadBookPosition(QStringLiteral("/books/a.epub"));
|
|
QCOMPARE(pos["chapter"].toInt(), 3);
|
|
QCOMPARE(pos["scrollIndex"].toInt(), 42);
|
|
QCOMPARE(pos["scrollOffset"].toDouble(), 123.5);
|
|
}
|
|
|
|
void loadBookPositionForUnknownBook()
|
|
{
|
|
AppState state;
|
|
QVariantMap pos = state.loadBookPosition(QStringLiteral("/books/unknown.epub"));
|
|
QCOMPARE(pos["chapter"].toInt(), 0);
|
|
QCOMPARE(pos["scrollIndex"].toInt(), 0);
|
|
QCOMPARE(pos["scrollOffset"].toDouble(), 0.0);
|
|
}
|
|
|
|
void saveBookPositionForMultipleBooks()
|
|
{
|
|
AppState state;
|
|
state.saveBookPosition(QStringLiteral("/books/a.epub"), 1, 10, 50.0);
|
|
state.saveBookPosition(QStringLiteral("/books/b.epub"), 5, 99, 200.5);
|
|
|
|
QVariantMap posA = state.loadBookPosition(QStringLiteral("/books/a.epub"));
|
|
QVariantMap posB = state.loadBookPosition(QStringLiteral("/books/b.epub"));
|
|
|
|
QCOMPARE(posA["chapter"].toInt(), 1);
|
|
QCOMPARE(posA["scrollIndex"].toInt(), 10);
|
|
QCOMPARE(posB["chapter"].toInt(), 5);
|
|
QCOMPARE(posB["scrollIndex"].toInt(), 99);
|
|
QCOMPARE(posB["scrollOffset"].toDouble(), 200.5);
|
|
}
|
|
|
|
void saveBookPositionOverwritesPrevious()
|
|
{
|
|
AppState state;
|
|
state.saveBookPosition(QStringLiteral("/books/a.epub"), 1, 10, 50.0);
|
|
state.saveBookPosition(QStringLiteral("/books/a.epub"), 7, 33, 88.0);
|
|
|
|
QVariantMap pos = state.loadBookPosition(QStringLiteral("/books/a.epub"));
|
|
QCOMPARE(pos["chapter"].toInt(), 7);
|
|
QCOMPARE(pos["scrollIndex"].toInt(), 33);
|
|
QCOMPARE(pos["scrollOffset"].toDouble(), 88.0);
|
|
}
|
|
|
|
void restoreEmitsSingleSignal()
|
|
{
|
|
AppState state;
|
|
|
|
QSignalSpy spyRestored(&state, &AppState::stateRestored);
|
|
QSignalSpy spyFilePath(&state, &AppState::lastFilePathChanged);
|
|
QSignalSpy spyChapter(&state, &AppState::lastChapterChanged);
|
|
QSignalSpy spyScroll(&state, &AppState::lastScrollPositionChanged);
|
|
QSignalSpy spyScrollIndex(&state, &AppState::lastScrollIndexChanged);
|
|
QSignalSpy spyScrollOffset(&state, &AppState::lastScrollOffsetChanged);
|
|
QSignalSpy spyFlip(&state, &AppState::pageFlipModeChanged);
|
|
QSignalSpy spyWidth(&state, &AppState::windowWidthChanged);
|
|
QSignalSpy spyHeight(&state, &AppState::windowHeightChanged);
|
|
QSignalSpy spyX(&state, &AppState::windowXChanged);
|
|
QSignalSpy spyY(&state, &AppState::windowYChanged);
|
|
|
|
state.restore();
|
|
|
|
QCOMPARE(spyRestored.count(), 1);
|
|
QCOMPARE(spyFilePath.count(), 0);
|
|
QCOMPARE(spyChapter.count(), 0);
|
|
QCOMPARE(spyScroll.count(), 0);
|
|
QCOMPARE(spyScrollIndex.count(), 0);
|
|
QCOMPARE(spyScrollOffset.count(), 0);
|
|
QCOMPARE(spyFlip.count(), 0);
|
|
QCOMPARE(spyWidth.count(), 0);
|
|
QCOMPARE(spyHeight.count(), 0);
|
|
QCOMPARE(spyX.count(), 0);
|
|
QCOMPARE(spyY.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;
|
|
QVERIFY(state.libraryBooks().isEmpty());
|
|
}
|
|
|
|
void addLibraryBookAddsEntry()
|
|
{
|
|
AppState state;
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
|
|
QStringLiteral("Title A"),
|
|
QStringLiteral("Author A"), 0.0);
|
|
|
|
QVariantList books = state.libraryBooks();
|
|
QCOMPARE(books.size(), 1);
|
|
QVariantMap book = books.first().toMap();
|
|
QCOMPARE(book[QStringLiteral("filePath")].toString(), QStringLiteral("/books/a.epub"));
|
|
QCOMPARE(book[QStringLiteral("title")].toString(), QStringLiteral("Title A"));
|
|
QCOMPARE(book[QStringLiteral("author")].toString(), QStringLiteral("Author A"));
|
|
QCOMPARE(book[QStringLiteral("progress")].toDouble(), 0.0);
|
|
}
|
|
|
|
void addLibraryBookPersistsThroughSaveRestore()
|
|
{
|
|
{
|
|
AppState state;
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
|
|
QStringLiteral("Title A"),
|
|
QStringLiteral("Author A"), 0.5);
|
|
state.save();
|
|
}
|
|
AppState state2;
|
|
state2.restore();
|
|
QVariantList books = state2.libraryBooks();
|
|
QCOMPARE(books.size(), 1);
|
|
QVariantMap book = books.first().toMap();
|
|
QCOMPARE(book[QStringLiteral("filePath")].toString(), QStringLiteral("/books/a.epub"));
|
|
QCOMPARE(book[QStringLiteral("title")].toString(), QStringLiteral("Title A"));
|
|
QCOMPARE(book[QStringLiteral("progress")].toDouble(), 0.5);
|
|
}
|
|
|
|
void addLibraryBookUpdatesMetadata()
|
|
{
|
|
AppState state;
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
|
|
QStringLiteral("Old Title"),
|
|
QStringLiteral("Old Author"), 0.3);
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
|
|
QStringLiteral("New Title"),
|
|
QStringLiteral("New Author"), 0.3);
|
|
|
|
QVariantList books = state.libraryBooks();
|
|
QCOMPARE(books.size(), 1);
|
|
QVariantMap book = books.first().toMap();
|
|
QCOMPARE(book[QStringLiteral("title")].toString(), QStringLiteral("New Title"));
|
|
QCOMPARE(book[QStringLiteral("author")].toString(), QStringLiteral("New Author"));
|
|
}
|
|
|
|
void addLibraryBookOverwritesProgress()
|
|
{
|
|
AppState state;
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
|
|
QStringLiteral("Title A"),
|
|
QStringLiteral("Author A"), 0.6);
|
|
// Lower value should overwrite (always update, no max logic)
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
|
|
QStringLiteral("Title A"),
|
|
QStringLiteral("Author A"), 0.2);
|
|
|
|
QVariantList books = state.libraryBooks();
|
|
QCOMPARE(books.size(), 1);
|
|
QCOMPARE(books.first().toMap()[QStringLiteral("progress")].toDouble(), 0.2);
|
|
}
|
|
|
|
void addLibraryBookSentinelPreservesProgress()
|
|
{
|
|
AppState state;
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
|
|
QStringLiteral("Title A"),
|
|
QStringLiteral("Author A"), 0.5);
|
|
// -1.0 sentinel: update metadata but don't touch progress
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
|
|
QStringLiteral("New Title"),
|
|
QStringLiteral("Author A"), -1.0);
|
|
|
|
QVariantList books = state.libraryBooks();
|
|
QCOMPARE(books.size(), 1);
|
|
QVariantMap book = books.first().toMap();
|
|
QCOMPARE(book[QStringLiteral("progress")].toDouble(), 0.5);
|
|
QCOMPARE(book[QStringLiteral("title")].toString(), QStringLiteral("New Title"));
|
|
}
|
|
|
|
void addNewLibraryBookWithSentinelStartsAtZero()
|
|
{
|
|
AppState state;
|
|
// New book added with sentinel should start at 0%
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
|
|
QStringLiteral("Title A"),
|
|
QStringLiteral("Author A"), -1.0);
|
|
|
|
QVariantList books = state.libraryBooks();
|
|
QCOMPARE(books.size(), 1);
|
|
QCOMPARE(books.first().toMap()[QStringLiteral("progress")].toDouble(), 0.0);
|
|
}
|
|
|
|
void removeLibraryBookRemovesEntry()
|
|
{
|
|
AppState state;
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
|
|
QStringLiteral("Title A"),
|
|
QStringLiteral("Author A"), 0.5);
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/b.epub"),
|
|
QStringLiteral("Title B"),
|
|
QStringLiteral("Author B"), 0.3);
|
|
state.removeLibraryBook(QStringLiteral("/books/a.epub"));
|
|
|
|
QVariantList books = state.libraryBooks();
|
|
QCOMPARE(books.size(), 1);
|
|
QCOMPARE(books.first().toMap()[QStringLiteral("filePath")].toString(),
|
|
QStringLiteral("/books/b.epub"));
|
|
}
|
|
|
|
void removeNonexistentLibraryBookIsNoop()
|
|
{
|
|
AppState state;
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
|
|
QStringLiteral("Title A"),
|
|
QStringLiteral("Author A"), 0.5);
|
|
state.removeLibraryBook(QStringLiteral("/books/nonexistent.epub"));
|
|
|
|
QCOMPARE(state.libraryBooks().size(), 1);
|
|
}
|
|
|
|
void libraryBooksChangedSignalOnAdd()
|
|
{
|
|
AppState state;
|
|
QSignalSpy spy(&state, &AppState::libraryBooksChanged);
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
|
|
QStringLiteral("Title A"),
|
|
QStringLiteral("Author A"), 0.0);
|
|
QCOMPARE(spy.count(), 1);
|
|
}
|
|
|
|
void libraryBooksChangedSignalOnRemove()
|
|
{
|
|
AppState state;
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
|
|
QStringLiteral("Title A"),
|
|
QStringLiteral("Author A"), 0.0);
|
|
QSignalSpy spy(&state, &AppState::libraryBooksChanged);
|
|
state.removeLibraryBook(QStringLiteral("/books/a.epub"));
|
|
QCOMPARE(spy.count(), 1);
|
|
}
|
|
|
|
void multipleLibraryBooksAreStoredInOrder()
|
|
{
|
|
AppState state;
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/a.epub"),
|
|
QStringLiteral("Title A"),
|
|
QStringLiteral("Author A"), 0.1);
|
|
state.addOrUpdateLibraryBook(QStringLiteral("/books/b.epub"),
|
|
QStringLiteral("Title B"),
|
|
QStringLiteral("Author B"), 0.2);
|
|
|
|
QVariantList books = state.libraryBooks();
|
|
QCOMPARE(books.size(), 2);
|
|
// Most recently added/updated comes first
|
|
QCOMPARE(books.at(0).toMap()[QStringLiteral("filePath")].toString(),
|
|
QStringLiteral("/books/b.epub"));
|
|
QCOMPARE(books.at(1).toMap()[QStringLiteral("filePath")].toString(),
|
|
QStringLiteral("/books/a.epub"));
|
|
}
|
|
};
|
|
|
|
QTEST_MAIN(AppStateTest)
|
|
#include "AppStateTest.moc"
|