summaryrefslogtreecommitdiff
path: root/src/tools/testrunner/testrunner.cpp
blob: fedab32e58f6cf49f0ebcbd8ea1bebcf2df663de (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
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "testrunner.h"

#include <QCoreApplication>
#include <QEventLoop>
#include <QQmlEngine>
#include <QRegularExpression>
#include <QDir>
#include <QFileInfo>

#include <qlogging.h>
#include <QtTest/qtestsystem.h>
#include <private/quicktest_p.h>
#include <private/quicktestresult_p.h>
#include "amtest.h"


QT_BEGIN_NAMESPACE
namespace QTest {
    extern Q_TESTLIB_EXPORT bool printAvailableFunctions;
    extern Q_TESTLIB_EXPORT void qtest_qParseArgs(int argc, char *argv[], bool qml);
}
QT_END_NAMESPACE

QT_BEGIN_NAMESPACE_AM

static QObject *amTest(QQmlEngine *engine, QJSEngine *jsEngine)
{
    Q_UNUSED(engine);
    Q_UNUSED(jsEngine);
    return AmTest::instance();
}

void TestRunner::initialize(const QString &testFile, const QStringList &testRunnerArguments)
{
    Q_ASSERT(!testRunnerArguments.isEmpty());

    const QString name = QFileInfo(testRunnerArguments.at(0)).fileName() + qSL("::") + QDir().relativeFilePath(testFile);
    static const char *programName = qstrdup(name.toLocal8Bit().constData());
    QuickTestResult::setProgramName(programName);

    // Convert all the arguments back into a char * array.
    // These need to be alive as long as the program is running!
    static QVector<char *> testArgV;
    for (const auto &arg : testRunnerArguments)
        testArgV << qstrdup(arg.toLocal8Bit().constData());

    atexit([]() {
        delete [] programName;
        std::for_each(testArgV.constBegin(), testArgV.constEnd(), [](char *arg) {
            delete [] arg;
        });
    });

    QuickTestResult::setCurrentAppname(testArgV.constFirst());

    // Allocate a QuickTestResult to create QBenchmarkGlobalData, otherwise the benchmark options don't work
    QuickTestResult result;
    // We would call QuickTestResult::parseArgs here, but that would include qml options in the help
    // which we don't support
    QTest::qtest_qParseArgs(testArgV.size(), testArgV.data(), false /*no qml options*/);
    qputenv("QT_QTESTLIB_RUNNING", "1");

    // Register the test object and application manager test add-on
    qmlRegisterSingletonType<AmTest>("QtApplicationManager.SystemUI", 2, 0, "AmTest", amTest);
}

int TestRunner::exec(QQmlEngine *qmlEngine)
{
    QEventLoop eventLoop;

    int typeId = qmlTypeId("QtTest", 1, 2, "QTestRootObject");
    QTestRootObject* inst = qmlEngine->singletonInstance<QTestRootObject*>(typeId);

    inst->setWindowShown(true);

    if (QTest::printAvailableFunctions)
        return 0;

    if (inst->hasTestCase())
        eventLoop.exec();

    QuickTestResult::setProgramName(nullptr);

    return QuickTestResult::exitCode();
}

QT_END_NAMESPACE_AM