Branch data Line data Source code
1 : : // Copyright (C) 2021 The Qt Company Ltd.
2 : : // Copyright (C) 2020 Luxoft Sweden AB
3 : : // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4 : :
5 : : #include <QPointer>
6 : : #include <QDir>
7 : : #include <QRegularExpression>
8 : : #include <QAbstractEventDispatcher>
9 : : #include <QProcess>
10 : : #include <QCoreApplication>
11 : : #include <private/qtestlog_p.h>
12 : : #include "amtest.h"
13 : : #include "utilities.h"
14 : :
15 : : using namespace Qt::StringLiterals;
16 : :
17 : :
18 : : QT_BEGIN_NAMESPACE_AM
19 : :
20 : :
21 : 0 : AmTest::AmTest()
22 : 0 : {}
23 : :
24 : 78 : AmTest *AmTest::instance()
25 : : {
26 [ + + + - : 78 : static QPointer<AmTest> object = new AmTest;
+ - + - +
- ]
27 [ - + ]: 78 : if (!object) {
28 : 0 : qWarning("A new appman test object has been created, the behavior may be compromised");
29 [ # # ]: 0 : object = new AmTest;
30 : : }
31 : 78 : return object;
32 : : }
33 : :
34 : 273 : int AmTest::timeoutFactor() const
35 : : {
36 : 197 : return QtAM::timeoutFactor();
37 : : }
38 : :
39 : 2 : QVariant AmTest::buildConfig() const
40 : : {
41 : 2 : return qApp->property("_am_buildConfig");
42 : : }
43 : :
44 : 2 : QString AmTest::qtVersion() const
45 : : {
46 : 2 : return QString::fromLatin1(QT_VERSION_STR);
47 : : }
48 : :
49 : 0 : bool AmTest::isAsanBuild() const
50 : : {
51 : : #if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
52 : : return true;
53 : : #else
54 : 0 : return false;
55 : : #endif
56 : : }
57 : :
58 : 0 : static QtMsgType convertMsgType(AmTest::MsgType type)
59 : : {
60 : 0 : QtMsgType ret;
61 : :
62 : 0 : switch (type) {
63 : : case AmTest::WarningMsg: ret = QtWarningMsg; break;
64 : : case AmTest::CriticalMsg: ret = QtCriticalMsg; break;
65 : : case AmTest::FatalMsg: ret = QtFatalMsg; break;
66 : : case AmTest::InfoMsg: ret = QtInfoMsg; break;
67 : : default: ret = QtDebugMsg;
68 : : }
69 : 0 : return ret;
70 : : }
71 : :
72 : 0 : void AmTest::ignoreMessage(MsgType type, const char *msg)
73 : : {
74 [ # # # # ]: 0 : QTestLog::ignoreMessage(convertMsgType(type), msg);
75 : 0 : }
76 : :
77 : 0 : void AmTest::ignoreMessage(MsgType type, const QRegularExpression &expression)
78 : : {
79 [ # # # # ]: 0 : QTestLog::ignoreMessage(convertMsgType(type), expression);
80 : 0 : }
81 : :
82 : 10 : int AmTest::observeObjectDestroyed(QObject *obj)
83 : : {
84 : 10 : static int idx = 0;
85 : 10 : int index = idx++;
86 : :
87 : 10 : connect(obj, &QObject::destroyed,
88 : 10 : this, [this, index] () {
89 : 10 : emit objectDestroyed(index);
90 : : });
91 : :
92 : 10 : return index;
93 : : }
94 : :
95 : 31 : void AmTest::aboutToBlock()
96 : : {
97 : 31 : emit QAbstractEventDispatcher::instance()->aboutToBlock();
98 : 31 : }
99 : :
100 : 10 : bool AmTest::dirExists(const QString &dir)
101 : : {
102 [ + - ]: 10 : return QDir(dir).exists();
103 : : }
104 : :
105 : 38 : QVariantMap AmTest::runProgram(const QStringList &commandLine)
106 : : {
107 : 38 : QVariantMap result {
108 : 76 : { u"stdout"_s, QString() },
109 : 76 : { u"stderr"_s, QString() },
110 : 38 : { u"exitCode"_s, -1 },
111 : 76 : { u"error"_s, QString() },
112 [ + - + + : 228 : };
- - ]
113 : :
114 [ - + ]: 38 : if (commandLine.isEmpty()) {
115 [ # # ]: 0 : result[u"error"_s] = u"no command"_s;
116 : : } else {
117 : : #if QT_CONFIG(process)
118 [ + - ]: 38 : QProcess process;
119 [ + - ]: 38 : process.start(commandLine[0], commandLine.mid(1));
120 [ + - + - : 38 : if (!process.waitForStarted(5000 * timeoutFactor())) {
- + ]
121 [ # # ]: 0 : result[u"error"_s] = u"could not start process"_s;
122 : : } else {
123 [ + - + - : 38 : if (!process.waitForFinished(5000 * timeoutFactor()))
- + ]
124 [ # # ]: 0 : result[u"error"_s] = u"process did not exit"_s;
125 : : else
126 [ + - + - ]: 38 : result[u"exitCode"_s] = process.exitCode();
127 : :
128 [ + - + - : 38 : result[u"stdout"_s] = QString::fromLocal8Bit(process.readAllStandardOutput());
+ - ]
129 [ + - + - : 38 : result[u"stderr"_s] = QString::fromLocal8Bit(process.readAllStandardError());
+ - ]
130 : : }
131 : : #else
132 : : result[u"error"_s] = u"runProgram is not available in this build"_s;
133 : : #endif
134 : 38 : }
135 : 38 : return result;
136 : 0 : }
137 : :
138 : : QT_END_NAMESPACE_AM
139 : :
140 : : #include "moc_amtest.cpp"
|