/* ============================================================ * The rekonq project * ============================================================ * SPDX-License-Identifier: GPL-3.0-only * Copyright (C) 2022 aqua * ============================================================ */ #include #include #include #include #include #include #include #include // Software under Test #include "../helpers.hpp" #include "../settings.hpp" #include "../settingsdialog.h" #include "settings_mock.hpp" using ::testing::_; // NOLINT(bugprone-reserved-identifier) using ::testing::AtLeast; using ::testing::ContainerEq; using ::testing::Return; using ::testing::ReturnArg; MATCHER_P(QStringEq, a, "") { *result_listener << "where the arg is " << qUtf8Printable(arg); return arg.compare(a) == 0; } MATCHER_P(QVariantEq, a, "") { *result_listener << "where the arg is " << qUtf8Printable(arg.toString()); return arg == QVariant(a); } TEST(settings, getFont) { const auto serif = getFont(QFont::Serif); EXPECT_EQ(serif.styleHint(), QFont::Serif) << qUtf8Printable(serif.toString()); const auto fixed = getFont(QFont::Monospace); EXPECT_EQ(fixed.styleHint(), QFont::Monospace) << qUtf8Printable(fixed.toString()); } TEST(settings, settingsPath) { const auto path = Settings::path(); EXPECT_FALSE(path.isEmpty()); EXPECT_TRUE(path.endsWith("rekonqrc")); QFile defaultSettingsPath(":/settings/rekonqrc"); EXPECT_TRUE(defaultSettingsPath.exists()); } class TestSettings : public Settings { public: TestSettings(const QString &path = QString()) : Settings(path, nullptr){}; ~TestSettings() override = default; [[nodiscard]] auto groups() const { return d->childGroups(); } [[nodiscard]] auto keys() const { return d->childKeys(); } }; TEST(settings, Settings) { TestSettings settings; EXPECT_TRUE(settings.filePath().isEmpty()); EXPECT_TRUE(settings.groups().isEmpty()); EXPECT_TRUE(settings.keys().isEmpty()); const auto FirstRun = settings.value("FirstRun"); EXPECT_TRUE(FirstRun.isValid()); EXPECT_TRUE(FirstRun.toBool()); settings.setValue("FirstRun", false); EXPECT_FALSE(settings.value("FirstRun").toBool()); EXPECT_TRUE(settings.groups().isEmpty()); EXPECT_EQ(settings.keys().count(), 1); settings.beginGroup("Network"); const auto downloadPathAsk = settings.value("downloadPathAsk"); EXPECT_TRUE(downloadPathAsk.isValid()); EXPECT_TRUE(downloadPathAsk.toBool()); settings.endGroup(); const auto searchUrl = settings.value("searchUrl"); EXPECT_TRUE(searchUrl.isValid()); EXPECT_EQ(searchUrl.toString(), QString("https://duckduckgo.com/?q=%1")); EXPECT_TRUE(settings.groups().isEmpty()); EXPECT_EQ(settings.keys().count(), 1); } TEST(settings, SettingsDialog_mock) { constexpr unsigned n_settings = 36; // there are 36 settings in total MockSettings mockSettings; // There are 4 groups in total, but General should not be calling beginGroup/endGroup // beginGroup/endGroup are called twice: during the ctor and when accepted EXPECT_CALL(mockSettings, beginGroup).Times(3 * 4); EXPECT_CALL(mockSettings, endGroup).Times(3 * 4); EXPECT_CALL(mockSettings, value).Times(n_settings * 2 - 2).WillRepeatedly(ReturnArg<0>()); EXPECT_CALL(mockSettings, value(QStringEq("homepage"))).Times(2).WillRepeatedly(Return(QVariant("about:blank"))); // expect accept to call setValue on all non-hidden settings EXPECT_CALL(mockSettings, setValue(_, _)).Times(n_settings - 2); EXPECT_CALL(mockSettings, setValue(QStringEq("homepage"), QVariantEq("https://kde.org"))); EXPECT_CALL(mockSettings, setValue(QStringEq("FirstRun"), QVariantEq(false))); // expect resetBtn to call resetValue on all non-hidden settings EXPECT_CALL(mockSettings, resetValue).Times(n_settings - 1); // change setting { SettingsDialog dlg(&mockSettings); auto *homepage = dlg.findChild("homepage"); ASSERT_FALSE(homepage == nullptr); EXPECT_TRUE(homepage->text() == QLatin1String("about:blank")) << qUtf8Printable(homepage->text()); homepage->setText("https://kde.org"); dlg.accept(); } // reset setting { SettingsDialog dlg(&mockSettings); auto *resetBtn = dlg.findChild()->button(QDialogButtonBox::RestoreDefaults); ASSERT_FALSE(resetBtn == nullptr); resetBtn->click(); } } TEST(settings, SettingsDialog) { const auto path = Settings::path(); ASSERT_FALSE(QFile::exists(path)); { Settings settings(path); EXPECT_TRUE(settings.value("FirstRun").toBool()); SettingsDialog dlg(&settings); auto *resetBtn = dlg.findChild()->button(QDialogButtonBox::RestoreDefaults); ASSERT_FALSE(resetBtn == nullptr); resetBtn->click(); } ASSERT_TRUE(QFile::exists(path)); ASSERT_TRUE(QFile::remove(path)); } int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); Q_INIT_RESOURCE(settings); QApplication app(argc, argv); QStandardPaths::setTestModeEnabled(true); return RUN_ALL_TESTS(); }