File: testsettings.cpp

package info (click to toggle)
libquotient 0.9.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,588 kB
  • sloc: xml: 39,103; cpp: 25,226; sh: 97; makefile: 10
file content (82 lines) | stat: -rw-r--r-- 2,780 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
// SPDX-FileCopyrightText: The Quotient Project Contributors
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <QTest>

#include <Quotient/settings.h>

using namespace Qt::Literals;
using Quotient::Settings, Quotient::SettingsGroup, Quotient::AccountSettings;

class TestSettings : public QObject {
    Q_OBJECT

public:
    static void initMain();

private slots:
    void initTestCase();
    void accountSettings_data() const;
    void accountSettings();

private:
    static inline const auto AccountsGroupName = u"Accounts"_s;
    QSettings qSettings{};
};

void TestSettings::initMain() { QCoreApplication::setOrganizationName(u"Quotient"_s); }

void TestSettings::initTestCase()
{
    qSettings.remove(AccountsGroupName);
}

void TestSettings::accountSettings_data() const
{
    QTest::addColumn<QString>("mxId");

    QTest::newRow("normal MXID") << u"@user:example.org"_s;
    QTest::newRow("MXID with slashes") << u"@user/with\\slashes:example.org"_s; // Test #842
}

void TestSettings::accountSettings()
{
    static const auto homeserverUrl = QUrl(u"https://example.org"_s);
    static const auto deviceName = u"SomeDevice"_s;
    QFETCH(QString, mxId);
    const auto escapedMxId = Settings::escapedForSettings(mxId);

    QVERIFY(SettingsGroup(AccountsGroupName).childGroups().empty()); // Pre-requisite
    qSettings.beginGroup(AccountsGroupName);
    { // Test writing to account settings
        AccountSettings accSettings(mxId);
        QCOMPARE(accSettings.userId(), mxId);
        QCOMPARE(accSettings.group(), AccountsGroupName % u'/' % escapedMxId);
        accSettings.setDeviceName(deviceName);
        QCOMPARE(accSettings.deviceName(), deviceName);
        accSettings.setHomeserver(homeserverUrl);
        QCOMPARE(accSettings.homeserver(), homeserverUrl);
    }

    qSettings.sync();
    // NB: QSettings::contains() doesn't work on groups, only on leaf keys; hence childGroups below
    auto childGroups = qSettings.childGroups();
    QVERIFY(childGroups.contains(escapedMxId));
    QVERIFY(SettingsGroup(AccountsGroupName).childGroups().contains(mxId));
    { // Test reading what was previously written
        SettingsGroup allAccountNames(AccountsGroupName);
        QCOMPARE(allAccountNames.childGroups().size(), 1);
        AccountSettings accSettings(allAccountNames.childGroups().back());
        QCOMPARE(accSettings.userId(), mxId);
        QCOMPARE(accSettings.deviceName(), deviceName);
        QCOMPARE(accSettings.homeserver(), homeserverUrl);
    }
    SettingsGroup(AccountsGroupName).remove(mxId); // Finally, test removal
    qSettings.sync();
    childGroups = qSettings.childGroups();
    QVERIFY(!childGroups.contains(escapedMxId));
    qSettings.endGroup();
}

QTEST_GUILESS_MAIN(TestSettings)
#include "testsettings.moc"