| 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_DSettings : public testing::Test |
| 19 |
{ |
| 20 |
protected: |
| 21 |
void SetUp() override; |
| 22 |
void TearDown() override; |
| 23 |
DSettings *settings; |
| 24 |
QString jsonContent; |
| 25 |
}; |
| 26 |
|
| 27 |
void ut_DSettings::SetUp() |
| 28 |
{ |
| 29 |
settings = new DSettings; |
| 30 |
QFile file("/tmp/test.json"); |
| 31 |
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) |
| 32 |
return; |
| 33 |
QTextStream out(&file); |
| 34 |
jsonContent = " { \"groups\": [{ " |
| 35 |
" \"key\": \"base\", " |
| 36 |
" \"name\": \"Basic settings\", " |
| 37 |
" \"groups\": [{ " |
| 38 |
" \"key\": \"open_action\", " |
| 39 |
" \"name\": \"Open Action\", " |
| 40 |
" \"options\": [{ " |
| 41 |
" \"key\": \"alway_open_on_new\", " |
| 42 |
" \"type\": \"checkbox\", " |
| 43 |
" \"text\": \"Always Open On New Windows\", " |
| 44 |
" \"default\": true " |
| 45 |
" }] " |
| 46 |
" }] }]}"; |
| 47 |
out << jsonContent; |
| 48 |
file.close(); |
| 49 |
} |
| 50 |
|
| 51 |
void ut_DSettings::TearDown() |
| 52 |
{ |
| 53 |
if (settings) { |
| 54 |
delete settings; |
| 55 |
settings = nullptr; |
| 56 |
} |
| 57 |
QFile file("/tmp/test.json"); |
| 58 |
if (file.exists()) |
| 59 |
file.remove(); |
| 60 |
} |
| 61 |
|
| 62 |
TEST_F(ut_DSettings, testDSettingSetBackend) |
| 63 |
{ |
| 64 |
QPointer<DSettings> tmpSetting = DSettings::fromJson(jsonContent.toLatin1()); |
| 65 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 66 |
QSettingBackend qBackend("/tmp/test.json"); |
| 67 |
scopeSettings->setBackend(&qBackend); |
| 68 |
QStringList qKeys = qBackend.keys(); |
| 69 |
ASSERT_TRUE(qKeys.isEmpty()); |
| 70 |
} |
| 71 |
|
| 72 |
TEST_F(ut_DSettings, testDSettingFromJson) |
| 73 |
{ |
| 74 |
QPointer<DSettings> tmpSetting = DSettings::fromJson(jsonContent.toLatin1()); |
| 75 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 76 |
auto keys = scopeSettings->keys(); |
| 77 |
ASSERT_TRUE(!keys.isEmpty()); |
| 78 |
} |
| 79 |
|
| 80 |
TEST_F(ut_DSettings, testDSettingFromJsonFile) |
| 81 |
{ |
| 82 |
QPointer<DSettings> tmpSetting = DSettings::fromJsonFile("/tmp/test.json"); |
| 83 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 84 |
QStringList keys = scopeSettings->keys(); |
| 85 |
ASSERT_TRUE(!keys.isEmpty()); |
| 86 |
} |
| 87 |
|
| 88 |
TEST_F(ut_DSettings, testDSettingMeta) |
| 89 |
{ |
| 90 |
QPointer<DSettings> tmpSetting = DSettings::fromJsonFile("/tmp/test.json"); |
| 91 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 92 |
QJsonObject jsonObject = scopeSettings->meta(); |
| 93 |
ASSERT_TRUE(!jsonObject.isEmpty()); |
| 94 |
} |
| 95 |
|
| 96 |
TEST_F(ut_DSettings, testDSettingKeys) |
| 97 |
{ |
| 98 |
QPointer<DSettings> tmpSetting = DSettings::fromJsonFile("/tmp/test.json"); |
| 99 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 100 |
QStringList keys = scopeSettings->keys(); |
| 101 |
ASSERT_TRUE(!keys.isEmpty()); |
| 102 |
} |
| 103 |
|
| 104 |
TEST_F(ut_DSettings, testDSettingOptions) |
| 105 |
{ |
| 106 |
QPointer<DSettings> tmpSetting = DSettings::fromJsonFile("/tmp/test.json"); |
| 107 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 108 |
QList<QPointer<DSettingsOption>> options = scopeSettings->options(); |
| 109 |
ASSERT_TRUE(!options.isEmpty()); |
| 110 |
} |
| 111 |
|
| 112 |
TEST_F(ut_DSettings, testDSettingOption) |
| 113 |
{ |
| 114 |
QPointer<DSettings> tmpSetting = DSettings::fromJsonFile("/tmp/test.json"); |
| 115 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 116 |
QStringList keys = scopeSettings->keys(); |
| 117 |
QPointer<DSettingsOption> option = scopeSettings->option(keys[0]); |
| 118 |
QString optionKey = option->key(); |
| 119 |
ASSERT_TRUE(!optionKey.isEmpty()); |
| 120 |
QString optionName = option->name(); |
| 121 |
ASSERT_TRUE(optionName.isEmpty()); |
| 122 |
ASSERT_TRUE(option->canReset()); |
| 123 |
ASSERT_TRUE(option->defaultValue().toBool()); |
| 124 |
ASSERT_TRUE(option->viewType() == "checkbox"); |
| 125 |
} |
| 126 |
|
| 127 |
TEST_F(ut_DSettings, testDSettingValue) |
| 128 |
{ |
| 129 |
QPointer<DSettings> tmpSetting = DSettings::fromJsonFile("/tmp/test.json"); |
| 130 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 131 |
QStringList keys = scopeSettings->keys(); |
| 132 |
QVariant value = scopeSettings->value(keys[0]); |
| 133 |
ASSERT_TRUE(value.toBool()); |
| 134 |
} |
| 135 |
|
| 136 |
TEST_F(ut_DSettings, testDSettingGroupKeys) |
| 137 |
{ |
| 138 |
QPointer<DSettings> tmpSetting = DSettings::fromJsonFile("/tmp/test.json"); |
| 139 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 140 |
QStringList groupKeys = scopeSettings->groupKeys(); |
| 141 |
ASSERT_TRUE(!groupKeys.isEmpty()); |
| 142 |
} |
| 143 |
|
| 144 |
TEST_F(ut_DSettings, testDSettingGroups) |
| 145 |
{ |
| 146 |
QPointer<DSettings> tmpSetting = DSettings::fromJsonFile("/tmp/test.json"); |
| 147 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 148 |
QList<QPointer<DSettingsGroup>> groups = scopeSettings->groups(); |
| 149 |
ASSERT_TRUE(!groups.isEmpty()); |
| 150 |
} |
| 151 |
|
| 152 |
TEST_F(ut_DSettings, testDSettingGroup) |
| 153 |
{ |
| 154 |
QPointer<DSettings> tmpSetting = DSettings::fromJsonFile("/tmp/test.json"); |
| 155 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 156 |
QStringList keys = scopeSettings->keys(); |
| 157 |
QPointer<DSettingsGroup> group = scopeSettings->group("base.open_action"); |
| 158 |
QString optionKey = group->key(); |
| 159 |
ASSERT_TRUE(!optionKey.isEmpty()); |
| 160 |
} |
| 161 |
|
| 162 |
TEST_F(ut_DSettings, testDSettingGetOption) |
| 163 |
{ |
| 164 |
QPointer<DSettings> tmpSetting = DSettings::fromJsonFile("/tmp/test.json"); |
| 165 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 166 |
QStringList keys = scopeSettings->keys(); |
| 167 |
QVariant option = scopeSettings->getOption(keys[0]); |
| 168 |
ASSERT_TRUE(option.toBool()); |
| 169 |
} |
| 170 |
|
| 171 |
TEST_F(ut_DSettings, testDSettingSync) |
| 172 |
{ |
| 173 |
QPointer<DSettings> tmpSetting = DSettings::fromJson(jsonContent.toLatin1()); |
| 174 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 175 |
scopeSettings->sync(); |
| 176 |
QStringList keys = scopeSettings->keys(); |
| 177 |
ASSERT_TRUE(!keys.isEmpty()); |
| 178 |
} |
| 179 |
|
| 180 |
TEST_F(ut_DSettings, testDSettingReset) |
| 181 |
{ |
| 182 |
QPointer<DSettings> tmpSetting = DSettings::fromJson(jsonContent.toLatin1()); |
| 183 |
QScopedPointer<DSettings> scopeSettings(tmpSetting.data()); |
| 184 |
scopeSettings->reset(); |
| 185 |
QStringList keys = scopeSettings->keys(); |
| 186 |
QVariant option = scopeSettings->getOption(keys[0]); |
| 187 |
ASSERT_TRUE(option.toBool()); |
| 188 |
} |