Skip to content

Commit

Permalink
Enhance TestSettings
Browse files Browse the repository at this point in the history
Test both "normal" MXIDs and MXIDs with slashes; actually test reading
into AccountSettings from the settings storage.
  • Loading branch information
KitsuneRal committed Dec 26, 2024
1 parent f717150 commit 82ae218
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions autotests/testsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TestSettings : public QObject {

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

private:
Expand All @@ -25,30 +26,47 @@ 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()
{
const auto mxId = u"@user/with\\slashes:example.org"_s; // Test #842
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);
const auto homeserverUrl = QUrl(u"https://example.org"_s);

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);
QVERIFY(accSettings.homeserver() == homeserverUrl);
// Bypass SettingsGroup::get() that prepends the group name and check that the group name
// has actually been prepended.
QVERIFY(accSettings.Settings::get<QUrl>(AccountsGroupName % u'/' % escapedMxId % u'/'
% u"homeserver"_s)
== 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));
SettingsGroup(AccountsGroupName).remove(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));
Expand Down

0 comments on commit 82ae218

Please sign in to comment.