aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/test/view.cpp
blob: 8aa639a10611860d071a0c6b969e7c21fc1852e1 (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
#define CATCH_CONFIG_RUNNER

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

#include "webprofile.h"
#include "webview.h"
#include <QApplication>
#include <QMainWindow>
#include <QTimer>
#include <QtWebEngine>
#include <catch2/catch.hpp>

SCENARIO("WebView")
{
    const QString profile_id{ "default" };
    auto *settings = WebProfile::load(qgetenv("PROFILE"), "about:blank", QUrl{ "about:blank" }, QUrl{ "about:blank" });
    auto *profile = WebProfile::load(profile_id, settings, true);

    QMainWindow window;
    auto *view = new WebView(profile, nullptr);
    window.setCentralWidget(view);
    window.show();
    window.resize(800, 600);

    WHEN("created")
    {
        THEN("using the default profile")
        {
            REQUIRE(view->profile() == profile);
        }
        THEN("serialized using default profile")
        {
            const auto data = view->serialize();
            REQUIRE(data.profile == profile_id);
            REQUIRE(data.url.isEmpty());
            REQUIRE(!data.history.isEmpty());
        }
        THEN("loading a url")
        {
            // block until a loadFinished signal
            QEventLoop pause;
            QObject::connect(view, &WebView::loadFinished, &pause, &QEventLoop::quit);
            view->load(QUrl{ qgetenv("URL") });
            pause.exec();

            REQUIRE(view->isLoaded());
        }
    }

    WHEN("changing profiles")
    {
        const QString swap_profile_id{ "swap_profile" };
        auto *swap_settings = WebProfile::load(QString(), "about:blank", QUrl{ "about:blank" }, QUrl{ "about:blank" });
        auto *swap_profile = WebProfile::load(swap_profile_id, swap_settings, true);

        view->setProfile(swap_profile);
        THEN("using the swap profile")
        {
            REQUIRE(view->profile() == swap_profile);
        }
        THEN("serialized using swap profile")
        {
            const auto data = view->serialize();
            REQUIRE(data.profile == swap_profile_id);
            REQUIRE(data.url.isEmpty());
            REQUIRE(!data.history.isEmpty());
        }

        view->setProfile(profile);
        delete swap_settings;
        delete swap_profile;
    }

    // cleanup
    window.close();
    delete view;
    delete settings;
    delete profile;
}

int main(int argc, char **argv)
{
    QtWebEngine::initialize();
    QApplication app(argc, argv);

    QTimer::singleShot(0, &app, [argc, argv, &app]() {
        const auto n_failed = Catch::Session().run(argc, argv);
        app.exit(n_failed);
    });

    return app.exec();
}