Debian Codegraph @

tests/ut_gsettingsbackend.cpp

raw
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_GSettings : public testing::Test
19 {
20 protected:
21 void SetUp() override;
22 void TearDown() override;
23 DSettings *settings = nullptr;
24 GSettingsBackend * gSettingBackend = nullptr;
25 QString jsonContent;
26 };
27
28 void ut_GSettings::SetUp()
29 {
30 GTEST_SKIP_("Do not test GSettingsBackend. schema was removed...");
31 QFile file("/tmp/test.json");
32 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
33 return;
34 QTextStream out(&file);
35 jsonContent = " { "
36 " \"gsettings\": { "
37 " \"id\": \"com.deepin.dtk\","
38 " \"path\": \"/dtk/deepin/deepin-terminal/\" "
39 "},"
40 " \"groups\": [{ "
41 " \"key\": \"base\", "
42 " \"name\": \"Basic settings\", "
43 " \"groups\": [{ "
44 " \"key\": \"open_action\", "
45 " \"name\": \"Open Action\", "
46 " \"options\": [{ "
47 " \"key\": \"paletteType\", "
48 " \"type\": \"checkbox\", "
49 " \"text\": \"Always Open On New Windows\", "
50 " \"default\": true "
51 " }] "
52 " }] }]"
53 "}";
54 out << jsonContent;
55 file.close();
56 settings = DSettings::fromJson(jsonContent.toLatin1());
57 gSettingBackend = new GSettingsBackend(settings);
58
59 }
60
61 void ut_GSettings::TearDown()
62 {
63 if (gSettingBackend) {
64 delete gSettingBackend;
65 gSettingBackend = nullptr;
66 }
67 if (settings) {
68 delete settings;
69 settings = nullptr;
70 }
71 QFile file("/tmp/test.json");
72 if (file.exists())
73 file.remove();
74 }
75
76 TEST_F(ut_GSettings, testGSettingBackendKeys)
77 {
78 QStringList qKeys = gSettingBackend->keys();
79 ASSERT_TRUE(!qKeys.isEmpty());
80 }
81
82 TEST_F(ut_GSettings, testGSettingBackendGetOption)
83 {
84 QStringList qKeys = gSettingBackend->keys();
85 ASSERT_TRUE(!qKeys.isEmpty());
86 QVariant value = gSettingBackend->getOption(qKeys[0]);
87 ASSERT_TRUE(!value.toString().isEmpty());
88 }
89
90