aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/test/profilemanager.cpp
blob: 8f6a34fe640d52f6bfcd1d526247b5e4bffbd9ae (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
109
110
111
112
113
114
115
116
117
118
119
120
#define CATCH_CONFIG_RUNNER

// clazy:excludeall=non-pod-global-static

#include "webprofilemanager.h"
#include <QApplication>
#include <catch2/catch.hpp>

SCENARIO("WebProfileManager")
{
    const QString search{ "https://search.url/t=%1" };
    const QUrl homepage{ "https://homepage.net" };
    const QUrl newtab{ "https://newtab.net" };
    const QString default_id{ "default" };

    GIVEN("an empty profile list")
    {
        WebProfileManager<false> profiles({}, default_id, search, homepage, newtab);

        REQUIRE(profiles.idList().count() == 1);
        REQUIRE(profiles.profile(default_id) == WebProfile::defaultProfile());
        REQUIRE(profiles.profile("not-in-list") == nullptr);

        WHEN("adding a new profile")
        {
            const QString id{ "id" };
            auto *settings = WebProfile::load(QString(), search, homepage, newtab);
            auto *profile = WebProfile::load(id, settings, true);

            THEN("doesn't add profile without settings")
            {
                profiles.add(id, profile, nullptr);
                REQUIRE(profiles.idList().count() == 1);
            }

            THEN("doesn't add settings without profile")
            {
                profiles.add(id, nullptr, settings);
                REQUIRE(profiles.idList().count() == 1);
            }

            THEN("adds new profile with settings")
            {
                profiles.add(id, profile, settings);
                REQUIRE(profiles.idList().count() == 2);
            }
        }

        WHEN("moving")
        {
            WebProfileManager<false> other(std::move(profiles));
            THEN("moved has the same number of profiles")
            {
                REQUIRE(other.idList().count() == 1);
                REQUIRE(other.profile(default_id) == WebProfile::defaultProfile());
                REQUIRE(other.profile("not-in-list") == nullptr);
            }
        }
    }

    GIVEN("a number of profiles, default undefined")
    {
        REQUIRE(qEnvironmentVariableIsSet("PROFILE"));

        WebProfileManager<false> profiles(QString::fromLatin1(qgetenv("PROFILE")).split(';'), default_id, search, homepage, newtab);

        REQUIRE(profiles.idList().count() == 2);
        REQUIRE(profiles.profile(default_id) == WebProfile::defaultProfile());
        REQUIRE(profiles.profile("testing") != nullptr);
        REQUIRE(profiles.profile("not-in-list") == nullptr);

        WHEN("making global")
        {
            profiles.make_global();
            WebProfileManager other;

            THEN("global has the same number of profiles")
            {
                REQUIRE(other.idList().count() == 2);
                REQUIRE(other.profile(default_id) == WebProfile::defaultProfile());
                REQUIRE(other.profile("testing") != nullptr);
                REQUIRE(other.profile("not-in-list") == nullptr);
            }

            THEN("walking has no nullptrs")
            {
                other.walk([](const QString &, WebProfile *profile, const QSettings *settings) {
                    REQUIRE(profile != nullptr);
                    REQUIRE(settings != nullptr);
                });
            }
        }
    }

    GIVEN("a number of profiles, default defined")
    {
        REQUIRE(qEnvironmentVariableIsSet("PROFILE"));

        WebProfileManager<false> profiles(QString::fromLatin1(qgetenv("PROFILE")).split(';'), "testing", search, homepage, newtab);

        REQUIRE(profiles.idList().count() == 1);
        REQUIRE(profiles.profile("testing") == WebProfile::defaultProfile());
        REQUIRE(profiles.profile("not-in-list") == nullptr);

        WHEN("walking")
        {
            profiles.walk([](const QString &, WebProfile *profile, const QSettings *settings) {
                REQUIRE(profile != nullptr);
                REQUIRE(settings != nullptr);
            });
        }
    }
}

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    const auto r = Catch::Session().run(argc, argv);
    return r;
}