summaryrefslogtreecommitdiffstats
path: root/tests/auto/quick/qmltests/tst_qmltests.cpp
blob: 9e928157e9f17e10727ac46ffcb8acaf57427f36 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <httpserver.h>

#if QT_CONFIG(ssl)
#include <httpsserver.h>
#endif

#include <QtCore/qscopedpointer.h>
#include <QtCore/qtemporarydir.h>
#include <QtGui/private/qinputmethod_p.h>
#include <QtQml/qqmlengine.h>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickwindow.h>
#include <QtQuickTest/quicktest.h>
#include <QtTest/qtest.h>
#include <QtWebEngineQuick/qquickwebengineprofile.h>
#include <QtWebEngineQuick/qtwebenginequickglobal.h>
#include <qt_webengine_quicktest.h>

#if defined(Q_OS_LINUX) && defined(QT_DEBUG)
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#endif

class Setup : public QObject
{
    Q_OBJECT
public:
    Setup() { }

public slots:
    void qmlEngineAvailable(QQmlEngine *engine)
    {
        engine->addImportPath(QDir(QT_TESTCASE_SOURCEDIR).canonicalPath() + "/mock-delegates");
    }
};

#if defined(Q_OS_LINUX) && defined(QT_DEBUG)
static bool debuggerPresent()
{
    int fd = open("/proc/self/status", O_RDONLY);
    if (fd == -1)
      return false;
    char buffer[2048];
    ssize_t size = read(fd, buffer, sizeof(buffer) - 1);
    if (size == -1) {
      close(fd);
      return false;
    }
    buffer[size] = 0;
    const char tracerPidToken[] = "\nTracerPid:";
    char *tracerPid = strstr(buffer, tracerPidToken);
    if (!tracerPid) {
      close(fd);
      return false;
    }
    tracerPid += sizeof(tracerPidToken);
    long int pid = strtol(tracerPid, &tracerPid, 10);
    close(fd);
    return pid != 0;
}

static void stackTrace()
{
    bool ok = false;
    const int disableStackDump = qEnvironmentVariableIntValue("QTEST_DISABLE_STACK_DUMP", &ok);
    if (ok && disableStackDump == 1)
        return;

    if (debuggerPresent())
        return;

    fprintf(stderr, "\n========= Received signal, dumping stack ==============\n");
    char cmd[512];
    qsnprintf(cmd, 512, "gdb --pid %d 2>/dev/null <<EOF\n"
                        "set prompt\n"
                        "set height 0\n"
                        "thread apply all where full\n"
                        "detach\n"
                        "quit\n"
                        "EOF\n",
                        (int)getpid());

    if (system(cmd) == -1)
        fprintf(stderr, "calling gdb failed\n");
    fprintf(stderr, "========= End of stack trace ==============\n");
}

static void sigSegvHandler(int signum)
{
    stackTrace();
    qFatal("Received signal %d", signum);
}
#endif

class TempDir : public QObject {
    Q_OBJECT

public:
    Q_INVOKABLE QString path() {
        Q_ASSERT(tempDir.isValid());
        return tempDir.isValid() ? tempDir.path() : QString();
    }

    Q_INVOKABLE QUrl pathUrl(const QString &filename = QString())
    {
        Q_ASSERT(tempDir.isValid());
        return filename.isEmpty() ? QUrl::fromLocalFile(tempDir.path())
                                  : QUrl::fromLocalFile(tempDir.filePath(filename));
    }

    Q_INVOKABLE void removeRecursive(const QString dirname)
    {
        QDir dir(dirname);
        QFileInfoList entries(dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot));
        for (int i = 0; i < entries.size(); ++i) {
            if (entries[i].isDir())
                removeRecursive(entries[i].filePath());
            else
                dir.remove(entries[i].fileName());
        }
        QDir().rmdir(dirname);
    }

    Q_INVOKABLE void createDirectory(const QString dirname) { QDir(tempDir.path()).mkdir(dirname); }

private:
    QTemporaryDir tempDir;
};

class TestInputContext : public QPlatformInputContext {
    Q_OBJECT

public:
    TestInputContext() = default;
    ~TestInputContext() { release(); }

    Q_INVOKABLE void create()
    {
        QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
        inputMethodPrivate->testContext = this;
    }

    Q_INVOKABLE void release()
    {
        QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
        inputMethodPrivate->testContext = nullptr;
    }

    void showInputPanel() override { m_visible = true; }
    void hideInputPanel() override { m_visible = false; }
    bool isInputPanelVisible() const override { return m_visible; }

private:
    bool m_visible = false;
};

QT_BEGIN_NAMESPACE
namespace QTest {
    int Q_TESTLIB_EXPORT defaultMouseDelay();
}
QT_END_NAMESPACE

class TestInputEvent : public QObject {
    Q_OBJECT

public:
    TestInputEvent() = default;

    Q_INVOKABLE bool mouseMultiClick(QObject *item, qreal x, qreal y, int clickCount)
    {
        QTEST_ASSERT(item);

        QWindow *view = eventWindow(item);
        if (!view)
            return false;

        for (int i = 0; i < clickCount; ++i) {
            mouseEvent(QMouseEvent::MouseButtonPress, view, item, QPointF(x, y));
            mouseEvent(QMouseEvent::MouseButtonRelease, view, item, QPointF(x, y));
        }
        QTest::lastMouseTimestamp += QTest::mouseDoubleClickInterval;

        return true;
    }

private:
    QWindow *eventWindow(QObject *item = nullptr)
    {
        QWindow *window = qobject_cast<QWindow *>(item);
        if (window)
            return window;

        QQuickItem *quickItem = qobject_cast<QQuickItem *>(item);
        if (quickItem)
            return quickItem->window();

        QQuickItem *testParentItem = qobject_cast<QQuickItem *>(parent());
        if (testParentItem)
            return testParentItem->window();

        return nullptr;
    }

    void mouseEvent(QEvent::Type type, QWindow *window, QObject *item, const QPointF &_pos)
    {
        QTest::qWait(QTest::defaultMouseDelay());
        QTest::lastMouseTimestamp += QTest::defaultMouseDelay();

        QPoint pos;
        QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
        if (sgitem)
            pos = sgitem->mapToScene(_pos).toPoint();

        QMouseEvent me(type, pos, window->mapFromGlobal(pos), Qt::LeftButton, Qt::LeftButton, {});
        me.setTimestamp(++QTest::lastMouseTimestamp);

        QSpontaneKeyEvent::setSpontaneous(&me);
        if (!qApp->notify(window, &me))
            qWarning("Mouse click event not accepted by receiving window");
    }
};

int main(int argc, char **argv)
{
#if defined(Q_OS_LINUX) && defined(QT_DEBUG)
    struct sigaction sigAction;

    sigemptyset(&sigAction.sa_mask);
    sigAction.sa_handler = &sigSegvHandler;
    sigAction.sa_flags = 0;

    sigaction(SIGSEGV, &sigAction, 0);
#endif
    QtWebEngineQuick::initialize();
    // Force to use English language for testing due to error message checks
    QLocale::setDefault(QLocale("en"));

    static QByteArrayList params = {QByteArrayLiteral("--webEngineArgs"),QByteArrayLiteral("--use-fake-device-for-media-stream")};
    QList<const char *> w_argv(argc);
    for (int i = 0; i < argc; ++i) w_argv[i] = argv[i];
    for (int i = 0; i < params.size(); ++i) w_argv.append(params[i].data());
    int w_argc = w_argv.size();
    Application app(w_argc, const_cast<char **>(w_argv.data()));

    QQuickWebEngineProfile::defaultProfile()->setOffTheRecord(true);
    qmlRegisterType<TempDir>("Test.util", 1, 0, "TempDir");
    qmlRegisterType<TestInputContext>("Test.util", 1, 0, "TestInputContext");
    qmlRegisterType<TestInputEvent>("Test.util", 1, 0, "TestInputEvent");

    QTEST_SET_MAIN_SOURCE_PATH
    qmlRegisterSingletonType<HttpServer>("Test.Shared", 1, 0, "HttpServer", [&] (QQmlEngine *, QJSEngine *) {
        auto server = new HttpServer;
        server->setResourceDirs(
                { server->sharedDataDir(),
                  QDir(QT_TESTCASE_SOURCEDIR).canonicalPath() + QLatin1String("/data") });
        return server;
    });

#if QT_CONFIG(ssl)
    qmlRegisterSingletonType<HttpsServer>(
            "Test.Shared", 1, 0, "HttpsServer", [&](QQmlEngine *, QJSEngine *) {
                return new HttpsServer(":/resources/server.pem", ":/resources/server.key", "");
            });
#endif
    Setup setup;
    int i = quick_test_main_with_setup(
            argc, argv, "qmltests",
            qPrintable(QT_TESTCASE_BUILDDIR + QLatin1String("/webengine.qmltests")), &setup);
    return i;
}

#include "tst_qmltests.moc"