summaryrefslogtreecommitdiffstats
path: root/tests/manual/wasm/qtloader_integration/main.cpp
blob: 4bb502b69c88eba19263b8d76228c9b1363ec58c (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtWidgets/QtWidgets>

#include <iostream>
#include <sstream>

#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <emscripten.h>

#include <QtGui/qpa/qplatformscreen.h>

namespace {
constexpr int ExitValueImmediateReturn = 42;
constexpr int ExitValueFromExitApp = 22;

std::string screenInformation()
{
    auto screens = qGuiApp->screens();
    std::ostringstream out;
    out << "[";
    const char *separator = "";
    for (const auto &screen : screens) {
        out << separator;
        out << "[" << std::to_string(screen->geometry().x()) << ","
            << std::to_string(screen->geometry().y()) << ","
            << std::to_string(screen->geometry().width()) << ","
            << std::to_string(screen->geometry().height()) << "]";
        separator = ",";
    }
    out << "]";
    return out.str();
}

std::string logicalDpi()
{
    auto screens = qGuiApp->screens();
    std::ostringstream out;
    out << "[";
    const char *separator = "";
    for (const auto &screen : screens) {
        out << separator;
        out << "[" << std::to_string(screen->handle()->logicalDpi().first) << ", "
            << std::to_string(screen->handle()->logicalDpi().second) << "]";
        separator = ",";
    }
    out << "]";
    return out.str();
}

std::string preloadedFiles()
{
    QStringList files = QDir("/preload").entryList(QDir::Files);
    std::ostringstream out;
    out << "[";
    const char *separator = "";
    for (const auto &file : files) {
        out << separator;
        out << file.toStdString();
        separator = ",";
    }
    out << "]";
    return out.str();
}

void crash()
{
    std::abort();
}

void stackOverflow()
{
    stackOverflow(); // should eventually termniate with exception
}

void exitApp()
{
    emscripten_force_exit(ExitValueFromExitApp);
}

void produceOutput()
{
    fprintf(stdout, "Sample output!\n");
}

std::string retrieveArguments()
{
    auto arguments = QApplication::arguments();
    std::ostringstream out;
    out << "[";
    const char *separator = "";
    for (const auto &argument : arguments) {
        out << separator;
        out << "'" << argument.toStdString() << "'";
        separator = ",";
    }
    out << "]";
    return out.str();
}

std::string getEnvironmentVariable(std::string name) {
    return QString::fromLatin1(qgetenv(name.c_str())).toStdString();
}
} // namespace

class AppWindow : public QObject
{
    Q_OBJECT
public:
    AppWindow() : m_layout(new QVBoxLayout(&m_ui))
    {
        addWidget<QLabel>("Qt Loader integration tests");

        m_ui.setLayout(m_layout);
    }

    void show() { m_ui.show(); }

    ~AppWindow() = default;

private:
    template<class T, class... Args>
    T *addWidget(Args... args)
    {
        T *widget = new T(std::forward<Args>(args)..., &m_ui);
        m_layout->addWidget(widget);
        return widget;
    }

    QWidget m_ui;
    QVBoxLayout *m_layout;
};

int main(int argc, char **argv)
{
    QApplication application(argc, argv);
    const auto arguments = application.arguments();
    const bool exitImmediately =
            std::find(arguments.begin(), arguments.end(), QStringLiteral("--exit-immediately"))
            != arguments.end();
    if (exitImmediately)
        emscripten_force_exit(ExitValueImmediateReturn);

    const bool crashImmediately =
            std::find(arguments.begin(), arguments.end(), QStringLiteral("--crash-immediately"))
            != arguments.end();
    if (crashImmediately)
        crash();

    const bool stackOverflowImmediately =
            std::find(arguments.begin(), arguments.end(), QStringLiteral("--stack-owerflow-immediately"))
            != arguments.end();
    if (stackOverflowImmediately)
        stackOverflow();

    const bool noGui = std::find(arguments.begin(), arguments.end(), QStringLiteral("--no-gui"))
            != arguments.end();

    if (!noGui) {
        AppWindow window;
        window.show();
        return application.exec();
    }
    return application.exec();
}

EMSCRIPTEN_BINDINGS(qtLoaderIntegrationTest)
{
    emscripten::constant("EXIT_VALUE_IMMEDIATE_RETURN", ExitValueImmediateReturn);
    emscripten::constant("EXIT_VALUE_FROM_EXIT_APP", ExitValueFromExitApp);

    emscripten::function("screenInformation", &screenInformation);
    emscripten::function("logicalDpi", &logicalDpi);
    emscripten::function("preloadedFiles", &preloadedFiles);
    emscripten::function("crash", &crash);
    emscripten::function("exitApp", &exitApp);
    emscripten::function("produceOutput", &produceOutput);
    emscripten::function("retrieveArguments", &retrieveArguments);
    emscripten::function("getEnvironmentVariable", &getEnvironmentVariable);
}

#include "main.moc"