| 1 |
// SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include <gtest/gtest.h> |
| 6 |
#include <QFile> |
| 7 |
#include <QTextStream> |
| 8 |
#include <QJsonObject> |
| 9 |
#include "settings/dsettings.h" |
| 10 |
#include "settings/dsettingsoption.h" |
| 11 |
#include "settings/dsettingsgroup.h" |
| 12 |
#include "settings/backend/gsettingsbackend.h" |
| 13 |
#include "settings/backend/qsettingbackend.h" |
| 14 |
|
| 15 |
DCORE_USE_NAMESPACE |
| 16 |
|
| 17 |
|
| 18 |
class ut_QSettingsBackend : public testing::Test |
| 19 |
{ |
| 20 |
protected: |
| 21 |
void SetUp() override; |
| 22 |
void TearDown() override; |
| 23 |
QString jsonContent; |
| 24 |
QString iniContent; |
| 25 |
}; |
| 26 |
|
| 27 |
void ut_QSettingsBackend::SetUp() |
| 28 |
{ |
| 29 |
QFile file("/tmp/test.ini"); |
| 30 |
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) |
| 31 |
return; |
| 32 |
QTextStream out(&file); |
| 33 |
jsonContent = " { \"groups\": [{ " |
| 34 |
" \"key\": \"base\", " |
| 35 |
" \"name\": \"Basic settings\", " |
| 36 |
" \"groups\": [{ " |
| 37 |
" \"key\": \"open_action\", " |
| 38 |
" \"name\": \"Open Action\", " |
| 39 |
" \"options\": [{ " |
| 40 |
" \"key\": \"alway_open_on_new\", " |
| 41 |
" \"type\": \"checkbox\", " |
| 42 |
" \"text\": \"Always Open On New Windows\", " |
| 43 |
" \"default\": true " |
| 44 |
" }] " |
| 45 |
" }] }]}"; |
| 46 |
iniContent = "[Test] \n \ |
| 47 |
value=false "; |
| 48 |
|
| 49 |
out << iniContent; |
| 50 |
file.close(); |
| 51 |
} |
| 52 |
|
| 53 |
void ut_QSettingsBackend::TearDown() |
| 54 |
{ |
| 55 |
QFile file("/tmp/test.ini"); |
| 56 |
if (file.exists()) |
| 57 |
file.remove(); |
| 58 |
} |
| 59 |
|
| 60 |
TEST_F(ut_QSettingsBackend, testQSettingsBackendKeys) |
| 61 |
{ |
| 62 |
DSettings tmpSetting; |
| 63 |
QSettingBackend qBackend("/tmp/test.ini"); |
| 64 |
tmpSetting.setBackend(&qBackend); |
| 65 |
QStringList qKeys = qBackend.keys(); |
| 66 |
ASSERT_TRUE(!qKeys.isEmpty()); |
| 67 |
} |
| 68 |
|
| 69 |
TEST_F(ut_QSettingsBackend, testQSettingsBackendGetOption) |
| 70 |
{ |
| 71 |
QPointer<DSettings> tmpSetting = DSettings::fromJson(jsonContent.toLatin1()); |
| 72 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 73 |
QSettingBackend qBackend("/tmp/test.ini"); |
| 74 |
scopeSettings->setBackend(&qBackend); |
| 75 |
scopeSettings->sync(); |
| 76 |
QStringList qKeys = qBackend.keys(); |
| 77 |
ASSERT_TRUE(!qKeys.isEmpty()); |
| 78 |
QVariant value = qBackend.getOption("Test"); |
| 79 |
ASSERT_TRUE(!value.toBool()); |
| 80 |
} |
| 81 |
|
| 82 |
TEST_F(ut_QSettingsBackend, testQSettingsBackendDoOption) |
| 83 |
{ |
| 84 |
QPointer<DSettings> tmpSetting = DSettings::fromJson(jsonContent.toLatin1()); |
| 85 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 86 |
QSettingBackend qBackend("/tmp/test.ini"); |
| 87 |
scopeSettings->setBackend(&qBackend); |
| 88 |
Q_EMIT qBackend.setOption("Test", true); |
| 89 |
|
| 90 |
QStringList qKeys = qBackend.keys(); |
| 91 |
ASSERT_TRUE(!qKeys.isEmpty()); |
| 92 |
QVariant value = qBackend.getOption("Test"); |
| 93 |
ASSERT_TRUE(!value.toBool()); |
| 94 |
|
| 95 |
// ensure `DSettings` is released before `SettingBackend` if `doSetOption` maybe execute. |
| 96 |
scopeSettings.reset(); |
| 97 |
} |