/* ============================================================ * The rekonq project * ============================================================ * SPDX-License-Identifier: GPL-3.0-only * Copyright (C) 2022 aqua * ============================================================ */ #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::_; using ::testing::AtLeast; using ::testing::ContainerEq; 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.toString().compare(a) == 0; } 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 = QDir(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)).filePath("rekonqrc"); EXPECT_FALSE(path.isEmpty()); EXPECT_TRUE(path.endsWith("rekonqrc")); } TEST(settings, Settings) { Settings settings(":rekonqrc"); EXPECT_THAT(settings.filePath(), QStringEq(":rekonqrc")); const auto FirstRun = settings.value("FirstRun"); EXPECT_TRUE(FirstRun.isValid()); EXPECT_TRUE(FirstRun.toBool()); settings.setValue("FirstRun", false); EXPECT_FALSE(settings.value("FirstRun").toBool()); 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")); } TEST(settings, SettingsDialog) { 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); // There are 35 settings in total, one of which is hidden and won't be set by the dialog EXPECT_CALL(mockSettings, value).Times(35 * 2).WillRepeatedly(ReturnArg<1>()); EXPECT_CALL(mockSettings, setValue(_, _)).Times(33 + 34); EXPECT_CALL(mockSettings, setValue(QStringEq("homepage"), QVariantEq("about:blank"))); // change setting { SettingsDialog dlg(&mockSettings); auto *homepage = dlg.findChild("homepage"); EXPECT_TRUE(homepage->text() == QLatin1String("http://www.kde.org/")) << qUtf8Printable(homepage->text()); homepage->setText("about:blank"); dlg.accept(); } // reset setting { SettingsDialog dlg(&mockSettings); auto *resetBtn = dlg.findChild()->button(QDialogButtonBox::RestoreDefaults); EXPECT_FALSE(resetBtn == nullptr); resetBtn->click(); } } int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); // 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 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()); return RUN_ALL_TESTS(); }