From 334317b175166cd6e7371bdf9110b23318336798 Mon Sep 17 00:00:00 2001 From: Dale Glass Date: Sat, 22 Oct 2022 20:18:51 +0200 Subject: [PATCH] Test suite for settings system Proof of concept still. Adds a test-specific function in Setting::Manager. --- libraries/shared/src/SettingManager.cpp | 11 +++++ libraries/shared/src/SettingManager.h | 1 + tests/shared/src/SettingsTests.cpp | 61 +++++++++++++++++++++++++ tests/shared/src/SettingsTests.h | 25 ++++++++++ 4 files changed, 98 insertions(+) create mode 100644 tests/shared/src/SettingsTests.cpp create mode 100644 tests/shared/src/SettingsTests.h diff --git a/libraries/shared/src/SettingManager.cpp b/libraries/shared/src/SettingManager.cpp index e5920b785e..f524e8e102 100644 --- a/libraries/shared/src/SettingManager.cpp +++ b/libraries/shared/src/SettingManager.cpp @@ -120,6 +120,17 @@ namespace Setting { } } + /** + * @brief Forces saving the current configuration + * + * @warning This function is for testing only, should only be called from the test suite. + */ + void Manager::forceSave() { + withWriteLock([&] { + _qSettings.sync(); + }); + } + QString Manager::fileName() const { return resultWithReadLock([&] { return _qSettings.fileName(); diff --git a/libraries/shared/src/SettingManager.h b/libraries/shared/src/SettingManager.h index 49f3bece4b..475753a277 100644 --- a/libraries/shared/src/SettingManager.h +++ b/libraries/shared/src/SettingManager.h @@ -44,6 +44,7 @@ namespace Setting { void setArrayIndex(int i); void setValue(const QString &key, const QVariant &value); QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const; + void forceSave(); protected: ~Manager(); diff --git a/tests/shared/src/SettingsTests.cpp b/tests/shared/src/SettingsTests.cpp new file mode 100644 index 0000000000..18039a231a --- /dev/null +++ b/tests/shared/src/SettingsTests.cpp @@ -0,0 +1,61 @@ +// +// Created by Bradley Austin Davis on 2017/11/08 +// Copyright 2013-2017 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + + +#include "SettingsTests.h" + +#include +#include +#include +#include +#include +#include +#include "SettingInterface.h" + + +QTEST_MAIN(SettingsTests) + +void SettingsTests::initTestCase() { + QCoreApplication::setOrganizationName("OverteTest"); + + DependencyManager::set(); + + Setting::init(); +} + +void SettingsTests::cleanupTestCase() { + // Setting::cleanupSettingsSaveThread(); +} + +void SettingsTests::loadSettings() { + Settings s; + qDebug() << "Loaded" << s.fileName(); +} + +void SettingsTests::saveSettings() { + Settings s; + s.setValue("TestValue", "Hello"); + + auto sm = DependencyManager::get(); + sm->setValue("TestValueSM", "Hello"); + + // There seems to be a bug here, data gets lost without this call here. + sm->forceSave(); + qDebug() << "Wrote" << s.fileName(); +} + +void SettingsTests::benchmarkSaveSettings() { + auto sm = DependencyManager::get(); + int i = 0; + + QBENCHMARK { + sm->setValue("Benchmark", ++i); + sm->forceSave(); + } + +} diff --git a/tests/shared/src/SettingsTests.h b/tests/shared/src/SettingsTests.h new file mode 100644 index 0000000000..927863adb6 --- /dev/null +++ b/tests/shared/src/SettingsTests.h @@ -0,0 +1,25 @@ +// +// Created by Dale Glass 2022/10/22 +// Copyright 2022 Overte e.V. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef overte_SettingsTests_h +#define overte_SettingsTests_h + +#include + +class SettingsTests : public QObject { + Q_OBJECT +private slots: + void initTestCase(); + void loadSettings(); + void saveSettings(); + void benchmarkSaveSettings(); + + void cleanupTestCase(); +}; + +#endif // overte_SettingsTests_h