diff options
Diffstat (limited to 'src/settings/test/test_settings.cpp')
-rw-r--r-- | src/settings/test/test_settings.cpp | 82 |
1 files changed, 61 insertions, 21 deletions
diff --git a/src/settings/test/test_settings.cpp b/src/settings/test/test_settings.cpp index 6efc64c3..a797a224 100644 --- a/src/settings/test/test_settings.cpp +++ b/src/settings/test/test_settings.cpp @@ -7,9 +7,10 @@ #include <QApplication> #include <QDialogButtonBox> -#include <QDir> +#include <QFile> #include <QLineEdit> #include <QPushButton> +#include <QSettings> #include <QStandardPaths> #include <gtest/gtest.h> @@ -19,9 +20,10 @@ #include "../settingsdialog.h" #include "settings_mock.hpp" -using ::testing::_; +using ::testing::_; // NOLINT(bugprone-reserved-identifier) using ::testing::AtLeast; using ::testing::ContainerEq; +using ::testing::Return; using ::testing::ReturnArg; MATCHER_P(QStringEq, a, "") @@ -32,7 +34,7 @@ MATCHER_P(QStringEq, a, "") MATCHER_P(QVariantEq, a, "") { *result_listener << "where the arg is " << qUtf8Printable(arg.toString()); - return arg.toString().compare(a) == 0; + return arg == QVariant(a); } TEST(settings, getFont) @@ -46,15 +48,30 @@ TEST(settings, getFont) TEST(settings, settingsPath) { - const auto path = QDir(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)).filePath("rekonqrc"); + 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) { - Settings settings(":rekonqrc"); - EXPECT_THAT(settings.filePath(), QStringEq(":rekonqrc")); + 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()); @@ -63,6 +80,9 @@ TEST(settings, Settings) 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()); @@ -72,9 +92,12 @@ TEST(settings, Settings) 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) +TEST(settings, SettingsDialog_mock) { constexpr unsigned n_settings = 36; // there are 36 settings in total MockSettings mockSettings; @@ -82,16 +105,20 @@ TEST(settings, SettingsDialog) // 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).WillRepeatedly(ReturnArg<1>()); - // 1 setting is hidden and won't be set by the dialog - // save and reset will both call setValue on all non-hidden settings - EXPECT_CALL(mockSettings, setValue(_, _)).Times(n_settings * 2 - 3); + 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<QLineEdit *>("homepage"); + ASSERT_FALSE(homepage == nullptr); EXPECT_TRUE(homepage->text() == QLatin1String("about:blank")) << qUtf8Printable(homepage->text()); homepage->setText("https://kde.org"); dlg.accept(); @@ -101,24 +128,37 @@ TEST(settings, SettingsDialog) { SettingsDialog dlg(&mockSettings); auto *resetBtn = dlg.findChild<QDialogButtonBox *>()->button(QDialogButtonBox::RestoreDefaults); - EXPECT_FALSE(resetBtn == nullptr); + 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<QDialogButtonBox *>()->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); - // handling fonts requires a QGuiApplication - // The proper platform name needs to be added to the argument list before the QGuiApplication constructor is called - // This needs to be done here for gtest_discover_tests to work - QList<char *> args; - for (int i = 0; i < argc; ++i) args.append(argv[i]); - args.append({"-platform", "offscreen"}); - int args_count = args.count(); - - QApplication app(args_count, args.data()); + QApplication app(argc, argv); + QStandardPaths::setTestModeEnabled(true); return RUN_ALL_TESTS(); } |