summaryrefslogtreecommitdiff
path: root/src/settings/test/test_settings.cpp
blob: fdb976f6aff2657b9633534011c6218b26607ccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* ============================================================
 *     The rekonq project
 * ============================================================
 * SPDX-License-Identifier: GPL-3.0-only
 * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net>
 * ============================================================ */

#include <QApplication>
#include <QDir>
#include <QLineEdit>
#include <QStandardPaths>
#include <gtest/gtest.h>

// 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;

const char *settingsFile = nullptr;

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(settingsFile);

  const auto FirstRun = settings.value("FirstRun");
  EXPECT_TRUE(FirstRun.isValid());
  EXPECT_TRUE(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 * 2);
  EXPECT_CALL(mockSettings, endGroup).Times(3 * 2);
  // 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).WillRepeatedly(ReturnArg<1>());
  EXPECT_CALL(mockSettings, setValue(_, _)).Times(33);
  EXPECT_CALL(mockSettings, setValue(QStringEq("homepage"), QVariantEq("about:blank")));

  SettingsDialog dlg(&mockSettings);
  auto *homepage = dlg.findChild<QLineEdit *>("homepage");
  EXPECT_TRUE(homepage->text() == QLatin1String("http://www.kde.org/")) << qUtf8Printable(homepage->text());
  homepage->setText("about:blank");
  dlg.accept();
}

int main(int argc, char **argv)
{
  ::testing::InitGoogleTest(&argc, argv);
  if (argc > 1) settingsFile = argv[argc - 1];

  // 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());

  return RUN_ALL_TESTS();
}