Browse Source

added generic accessor test

pull/2/head
Skycoder42 6 years ago
parent
commit
0e79d9b585
No known key found for this signature in database GPG Key ID: 8E01AD9EF0578D2B
  1. 3
      tests/auto/mvvmcore/mvvmcore.pro
  2. 14
      tests/auto/mvvmcore/qsettingsaccessor/qsettingsaccessor.pro
  3. 56
      tests/auto/mvvmcore/qsettingsaccessor/tst_qsettingsaccessor.cpp
  4. 2
      tests/auto/mvvmcore/serviceregistry/tst_serviceregistry.cpp
  5. 122
      tests/shared/tst_isettingsaccessor.h

3
tests/auto/mvvmcore/mvvmcore.pro

@ -4,7 +4,8 @@ SUBDIRS += \
settingsgenerator \
serviceregistry \
serviceregistrytestplugin \
binding
binding \
qsettingsaccessor
serviceregistry.depends += serviceregistrytestplugin

14
tests/auto/mvvmcore/qsettingsaccessor/qsettingsaccessor.pro

@ -0,0 +1,14 @@
TEMPLATE = app
QT += testlib mvvmcore
QT -= gui
CONFIG += console
CONFIG -= app_bundle
TARGET = tst_qsettingsaccessor
HEADERS += \
../../../shared/tst_isettingsaccessor.h
SOURCES += \
tst_qsettingsaccessor.cpp

56
tests/auto/mvvmcore/qsettingsaccessor/tst_qsettingsaccessor.cpp

@ -0,0 +1,56 @@
#include <QtTest>
#include <QtCore>
#include <QtMvvmCore/QSettingsAccessor>
#include "../../../shared/tst_isettingsaccessor.h"
using namespace QtMvvm;
class QSettingsAccessorTest : public ISettingsAccessorTest
{
Q_OBJECT
protected:
ISettingsAccessor *createFirst() override;
ISettingsAccessor *createSecond() override;
bool testSyncChangeSignals() override;
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
private:
QTemporaryFile *tempFile;
};
ISettingsAccessor *QSettingsAccessorTest::createFirst()
{
auto settings = new QSettings{tempFile->fileName(), QSettings::IniFormat, this};
auto accessor = new QSettingsAccessor{settings, this};
settings->setParent(accessor);
return accessor;
}
ISettingsAccessor *QSettingsAccessorTest::createSecond()
{
return createFirst();
}
bool QSettingsAccessorTest::testSyncChangeSignals()
{
return false;
}
void QSettingsAccessorTest::initTestCase()
{
tempFile = new QTemporaryFile{this};
QVERIFY(tempFile->open());
tempFile->close();
}
void QSettingsAccessorTest::cleanupTestCase()
{
delete tempFile;
}
QTEST_MAIN(QSettingsAccessorTest)
#include "tst_qsettingsaccessor.moc"

2
tests/auto/mvvmcore/serviceregistry/tst_serviceregistry.cpp

@ -178,7 +178,7 @@ void ServiceRegistryTest::testPluginRegistration()
QCOMPARE(ServiceRegistry::instance()->service<PluginTestInterface>()->magicNumber(), 72);
} catch(QException &e) {
#ifdef __MINGW32__
QEXPECT_FAIL("", "Plugin loading in the tests is currently broken on mingw", Abort);
QEXPECT_FAIL("", "Plugin loading in debug builds is currently broken on mingw", Abort);
QVERIFY2(false, e.what());
#else
QFAIL(e.what());

122
tests/shared/tst_isettingsaccessor.h

@ -0,0 +1,122 @@
#ifndef TST_ISETTINGSACCESSOR_H
#define TST_ISETTINGSACCESSOR_H
#include <QtCore/QObject>
#include <QtMvvmCore/ISettingsAccessor>
#include <QtTest>
class ISettingsAccessorTest : public QObject
{
Q_OBJECT
protected:
QtMvvm::ISettingsAccessor *accessor = nullptr;
virtual QtMvvm::ISettingsAccessor *createFirst() = 0;
virtual QtMvvm::ISettingsAccessor *createSecond() = 0;
virtual bool testSyncChangeSignals() = 0;
private Q_SLOTS:
void testAccessorValid() {
accessor = createFirst();
QVERIFY(accessor);
}
void testSimpleOperations() {
QSignalSpy changedSpy{accessor, &QtMvvm::ISettingsAccessor::entryChanged};
QSignalSpy removedSpy{accessor, &QtMvvm::ISettingsAccessor::entryRemoved};
auto key = QStringLiteral("test/key");
QVERIFY(!accessor->contains(key));
QCOMPARE(accessor->load(key, 24).toInt(), 24);
QVERIFY(changedSpy.isEmpty());
QVERIFY(removedSpy.isEmpty());
accessor->save(key, 42);
QVERIFY(accessor->contains(key));
QCOMPARE(accessor->load(key, 24).toInt(), 42);
if(changedSpy.isEmpty())
QVERIFY(changedSpy.wait());
QCOMPARE(changedSpy.size(), 1);
auto changeEvent = changedSpy.takeFirst();
QCOMPARE(changeEvent[0].toString(), key);
QCOMPARE(changeEvent[1].toInt(), 42);
QVERIFY(removedSpy.isEmpty());
accessor->remove(key);
QVERIFY(!accessor->contains(key));
QCOMPARE(accessor->load(key, 24).toInt(), 24);
if(removedSpy.isEmpty())
QVERIFY(removedSpy.wait());
QCOMPARE(removedSpy.size(), 1);
QCOMPARE(removedSpy.takeFirst()[0].toString(), key);
QVERIFY(changedSpy.isEmpty());
}
void testBatchRemove() {
auto key0 = QStringLiteral("group");
auto key1 = QStringLiteral("group/key");
auto key2 = QStringLiteral("group/key/subkey");
auto key3 = QStringLiteral("group/key/subgroup/subkey");
accessor->save(key0, true);
accessor->save(key1, true);
accessor->save(key2, true);
accessor->save(key3, true);
QSignalSpy removedSpy{accessor, &QtMvvm::ISettingsAccessor::entryRemoved};
accessor->remove(key1);
while(removedSpy.size() < 3)
QVERIFY(removedSpy.wait());
QCOMPARE(removedSpy.size(), 3);
QStringList eventKeys {
removedSpy[0][0].toString(),
removedSpy[1][0].toString(),
removedSpy[2][0].toString()
};
QVERIFY(eventKeys.contains(key1));
QVERIFY(eventKeys.contains(key2));
QVERIFY(eventKeys.contains(key3));
QVERIFY(!removedSpy.wait(1000));
}
void testSync() {
auto second = createSecond();
QVERIFY(second);
QSignalSpy changedSpy{second, &QtMvvm::ISettingsAccessor::entryChanged};
QSignalSpy removedSpy{accessor, &QtMvvm::ISettingsAccessor::entryRemoved};
auto key = QStringLiteral("sync/key");
accessor->save(key, 13);
accessor->sync();
second->sync();
if(!testSyncChangeSignals())
QEXPECT_FAIL("", "The testes accessor does not support notifying changes over multiple instances", Continue);
if(changedSpy.isEmpty())
QVERIFY(changedSpy.wait());
if(!changedSpy.isEmpty()) {
QCOMPARE(changedSpy.size(), 1);
auto changeEvent = changedSpy.takeFirst();
QCOMPARE(changeEvent[0].toString(), key);
QCOMPARE(changeEvent[1].toInt(), 42);
}
QCOMPARE(second->load(key, 24).toInt(), 13);
second->remove(key);
second->sync();
accessor->sync();
if(!testSyncChangeSignals())
QEXPECT_FAIL("", "The testes accessor does not support notifying changes over multiple instances", Continue);
if(removedSpy.isEmpty())
QVERIFY(removedSpy.wait());
if(!removedSpy.isEmpty()) {
QCOMPARE(removedSpy.size(), 1);
QCOMPARE(removedSpy.takeFirst()[0].toString(), key);
}
QCOMPARE(accessor->load(key, 24).toInt(), 24);
}
};
#endif // TST_ISETTINGSACCESSOR_H
Loading…
Cancel
Save