diff options
author | aqua <aqua@iserlohn-fortress.net> | 2022-09-08 10:27:30 +0300 |
---|---|---|
committer | aqua <aqua@iserlohn-fortress.net> | 2022-09-08 16:33:03 +0300 |
commit | af1eff03c9e839914aab4109970c4a9f6fac8a99 (patch) | |
tree | ae11034560b0e1c270614077ffd8f0f7485c7381 /src/settings/test | |
parent | Add RekonqWindow tests (diff) | |
download | rekonq-af1eff03c9e839914aab4109970c4a9f6fac8a99.tar.xz |
RekonqSettings: add resetValue and remove value's defaultValue parameter
- generate default rekonqrc and include it in libsettings
- Settings uses :/settings/rekonqrc to provide default values
- Add Settings::path() to get the default settings location
Diffstat (limited to 'src/settings/test')
-rw-r--r-- | src/settings/test/rekonqrc | 37 | ||||
-rw-r--r-- | src/settings/test/settings_mock.hpp | 3 | ||||
-rw-r--r-- | src/settings/test/test_settings.cpp | 82 | ||||
-rw-r--r-- | src/settings/test/test_settings.qrc | 7 | ||||
-rw-r--r-- | src/settings/test/test_settingsdialog.cpp | 9 |
5 files changed, 65 insertions, 73 deletions
diff --git a/src/settings/test/rekonqrc b/src/settings/test/rekonqrc deleted file mode 100644 index fe67194c..00000000 --- a/src/settings/test/rekonqrc +++ /dev/null @@ -1,37 +0,0 @@ -[General] -FirstRun=true -lang=TODO: change type to StringList -homepage=about:blank -newtab=about:blank -searchUrl=https://duckduckgo.com/?q=%1 -pluginPath=TODO: change type to Path - -[Network] -downloadPath=TODO: change type to Path -downloadPathAsk=true -userAgent=TODO - -[Appearance] -IconTheme=tabler -defaultFontSize=16 -minFontSize=7 -defaultEncoding=ISO 8859-1 -defaultZoom=10 - -[Shortcuts] -actionShowSidebar=Ctrl+B -actionShowSearchBar=Ctrl+F -actionSettings=Ctrl+S -actionQuit=Ctrl+Q -actionNewTab=Ctrl+T -actionCloseTab=Ctrl+W -actionSwitchTabLeft=Ctrl+Left -actionSwitchTabRight=Ctrl+Right -actionFocusAddressBar=F6 -actionBack=Alt+Left -actionForward=Alt+Right -actionRefresh=F5 -actionReload=Ctrl+F5 -actionOpen=Ctrl+O -actionBookmark=Ctrl+D - diff --git a/src/settings/test/settings_mock.hpp b/src/settings/test/settings_mock.hpp index bc3fbc2f..bf0e137a 100644 --- a/src/settings/test/settings_mock.hpp +++ b/src/settings/test/settings_mock.hpp @@ -21,7 +21,8 @@ public: MOCK_METHOD(void, endGroup, (), (override)); MOCK_METHOD(void, setValue, (const QString &, const QVariant &), (override)); - MOCK_METHOD(QVariant, value, (const QString &, const QVariant &), (const, override)); + MOCK_METHOD(void, resetValue, (const QString &), (override)); + MOCK_METHOD(QVariant, value, (const QString &), (const, override)); MOCK_METHOD(QString, filePath, (), (const, override)); }; 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(); } diff --git a/src/settings/test/test_settings.qrc b/src/settings/test/test_settings.qrc deleted file mode 100644 index 16e74fef..00000000 --- a/src/settings/test/test_settings.qrc +++ /dev/null @@ -1,7 +0,0 @@ -<?xml version='1.0' encoding='utf-8' standalone='yes'?> -<!DOCTYPE RCC> -<RCC version="1.0"> - <qresource prefix="/"> - <file alias="rekonqrc">rekonqrc</file> - </qresource> -</RCC> diff --git a/src/settings/test/test_settingsdialog.cpp b/src/settings/test/test_settingsdialog.cpp index a50cf307..a89ec82d 100644 --- a/src/settings/test/test_settingsdialog.cpp +++ b/src/settings/test/test_settingsdialog.cpp @@ -5,20 +5,15 @@ * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net> * ============================================================ */ +#include "../settings.hpp" #include "../settingsdialog.h" -#include "settings_mock.hpp" #include <QApplication> -using ::testing::AtLeast; -using ::testing::NiceMock; -using ::testing::ReturnArg; - int main(int argc, char **argv) { QApplication app(argc, argv); - NiceMock<MockSettings> settings; - EXPECT_CALL(settings, value).WillRepeatedly(ReturnArg<1>()); + Settings settings(Settings::path()); SettingsDialog dlg(&settings); dlg.show(); |