File: ut_qsettingsbackend.cpp

package info (click to toggle)
dtkcore 5.7.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,728 kB
  • sloc: cpp: 22,021; ansic: 183; python: 68; xml: 58; makefile: 27; sh: 15
file content (97 lines) | stat: -rw-r--r-- 2,933 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <gtest/gtest.h>
#include <QFile>
#include <QTextStream>
#include <QJsonObject>
#include "settings/dsettings.h"
#include "settings/dsettingsoption.h"
#include "settings/dsettingsgroup.h"
#include "settings/backend/gsettingsbackend.h"
#include "settings/backend/qsettingbackend.h"

DCORE_USE_NAMESPACE


class ut_QSettingsBackend : public testing::Test
{
protected:
    void SetUp() override;
    void TearDown() override;
    QString jsonContent;
    QString iniContent;
};

void ut_QSettingsBackend::SetUp()
{
    QFile file("/tmp/test.ini");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;
    QTextStream out(&file);
    jsonContent = " { \"groups\": [{ "
                  " \"key\": \"base\", "
                  " \"name\": \"Basic settings\", "
                  " \"groups\": [{ "
                  " \"key\": \"open_action\", "
                  " \"name\": \"Open Action\", "
                  " \"options\": [{ "
                  " \"key\": \"alway_open_on_new\", "
                  " \"type\": \"checkbox\", "
                  " \"text\": \"Always Open On New Windows\", "
                  " \"default\": true "
                  " }]  "
                  " }] }]}";
    iniContent = "[Test] \n \
                  value=false  ";

    out << iniContent;
    file.close();
}

void ut_QSettingsBackend::TearDown()
{
    QFile file("/tmp/test.ini");
    if (file.exists())
        file.remove();
}

TEST_F(ut_QSettingsBackend, testQSettingsBackendKeys)
{
    DSettings tmpSetting;
    QSettingBackend qBackend("/tmp/test.ini");
    tmpSetting.setBackend(&qBackend);
    QStringList qKeys = qBackend.keys();
    ASSERT_TRUE(!qKeys.isEmpty());
}

TEST_F(ut_QSettingsBackend, testQSettingsBackendGetOption)
{
    QPointer<DSettings> tmpSetting = DSettings::fromJson(jsonContent.toLatin1());
    QScopedPointer<DSettings> scopeSettings(tmpSetting.data());
    QSettingBackend qBackend("/tmp/test.ini");
    scopeSettings->setBackend(&qBackend);
    scopeSettings->sync();
    QStringList qKeys = qBackend.keys();
    ASSERT_TRUE(!qKeys.isEmpty());
    QVariant value = qBackend.getOption("Test");
    ASSERT_TRUE(!value.toBool());
}

TEST_F(ut_QSettingsBackend, testQSettingsBackendDoOption)
{
    QPointer<DSettings> tmpSetting = DSettings::fromJson(jsonContent.toLatin1());
    QScopedPointer<DSettings> scopeSettings(tmpSetting.data());
    QSettingBackend qBackend("/tmp/test.ini");
    scopeSettings->setBackend(&qBackend);
    Q_EMIT qBackend.setOption("Test", true);

    QStringList qKeys = qBackend.keys();
    ASSERT_TRUE(!qKeys.isEmpty());
    QVariant value = qBackend.getOption("Test");
    ASSERT_TRUE(!value.toBool());

    // ensure `DSettings` is released before `SettingBackend` if `doSetOption` maybe execute.
    scopeSettings.reset();
}