-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathqtmonkey_app.cpp
165 lines (152 loc) · 5.22 KB
/
qtmonkey_app.cpp
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
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <QtCore/QCoreApplication>
#include <QtCore/QProcess>
#include <QtCore/QTextStream>
#include "common.hpp"
#include "qtmonkey.hpp"
using qt_monkey_common::operator<<;
namespace
{
class ConsoleApplication final : public QCoreApplication
{
public:
ConsoleApplication(int &argc, char **argv) : QCoreApplication(argc, argv) {}
bool notify(QObject *receiver, QEvent *event) override
{
try {
return QCoreApplication::notify(receiver, event);
} catch (const std::exception &ex) {
qFatal("%s: catch exception: %s", Q_FUNC_INFO, ex.what());
return false;
}
}
};
static inline std::ostream &operator<<(std::ostream &os, const QString &str)
{
os << str.toLocal8Bit();
return os;
}
} // namespace
#if QT_VERSION >= 0x050000
static void msgHandler(QtMsgType type, const QMessageLogContext &,
const QString &msg)
#else
static void msgHandler(QtMsgType type, const char *msg)
#endif
{
static auto startTime = std::chrono::steady_clock::now();
using std::clog;
const double timeDiff
= std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - startTime)
.count()
/ 1000.;
switch (type) {
case QtDebugMsg:
clog << "Debug(" << timeDiff << "): " << msg << std::endl;
break;
case QtWarningMsg:
clog << "Warning(" << timeDiff << "): " << msg << std::endl;
break;
case QtCriticalMsg:
clog << "Critical(" << timeDiff << "): " << msg << std::endl;
break;
#if QT_VERSION >= 0x050000
case QtInfoMsg:
clog << "Info(" << timeDiff << "): " << msg << std::endl;
break;
#endif
case QtFatalMsg:
clog << "Fatal(" << timeDiff << "): " << msg << std::endl;
std::exit(EXIT_FAILURE);
}
}
static QString usage()
{
return T_("Usage: %1 [--exit-on-script-error] [--encoding file_encoding] "
"[--trace-script-exec] "
"[--save-screenshots path/to/dir maxium_number] "
"[--script path/to/script] "
"--user-app "
"path/to/application [application's command line args]\n")
.arg(QCoreApplication::applicationFilePath());
}
int main(int argc, char *argv[])
{
std::ios_base::sync_with_stdio(false);
ConsoleApplication app(argc, argv);
INSTALL_QT_MSG_HANDLER(msgHandler);
bool exitOnScriptError = false;
int userAppOffset = -1;
QStringList scripts;
const char *encoding = "UTF-8";
QString codeToRunBeforeAll;
for (int i = 1; i < argc; ++i)
if (std::strcmp(argv[i], "--user-app") == 0) {
if ((i + 1) >= argc) {
std::cerr << qPrintable(usage());
return EXIT_FAILURE;
}
++i;
userAppOffset = i;
break;
} else if (std::strcmp(argv[i], "--script") == 0) {
if ((i + 1) >= argc) {
std::cerr << qPrintable(usage());
return EXIT_FAILURE;
}
++i;
scripts.append(QFile::decodeName(argv[i]));
} else if (std::strcmp(argv[i], "--exit-on-script-error") == 0) {
exitOnScriptError = true;
} else if (std::strcmp(argv[i], "--encoding") == 0) {
if ((i + 1) >= argc) {
std::cerr << qPrintable(usage());
return EXIT_FAILURE;
}
++i;
encoding = argv[i];
} else if (std::strcmp(argv[i], "--trace-script-exec") == 0) {
codeToRunBeforeAll
+= QStringLiteral("Test.setTraceEnabled(true);\n");
} else if (std::strcmp(argv[i], "--save-screenshots") == 0) {
int nSteps = -1;
if ((i + 2) >= argc || sscanf(argv[i + 2], "%d", &nSteps) != 1) {
std::cerr << qPrintable(usage());
return EXIT_FAILURE;
}
const QString path = argv[i + 1];
i += 2;
codeToRunBeforeAll
+= QStringLiteral("Test.saveScreenshots(\"%1\", %2);\n")
.arg(path)
.arg(nSteps);
} else if (std::strcmp(argv[i], "--help") == 0
|| std::strcmp(argv[i], "-h") == 0) {
std::cout << qPrintable(usage());
return EXIT_SUCCESS;
} else {
std::cerr << qPrintable(T_("Unknown option: %1\n").arg(argv[i]))
<< qPrintable(usage());
return EXIT_FAILURE;
}
if (userAppOffset == -1) {
std::cerr << qPrintable(
T_("You should set path and args for user app with --user-app\n"));
return EXIT_FAILURE;
}
QStringList userAppArgs;
for (int i = userAppOffset + 1; i < argc; ++i)
userAppArgs << QString::fromLocal8Bit(argv[i]);
qt_monkey_app::QtMonkey monkey(exitOnScriptError);
if (!scripts.empty()
&& !monkey.runScriptFromFile(std::move(codeToRunBeforeAll),
std::move(scripts), encoding))
return EXIT_FAILURE;
monkey.runApp(QString::fromLocal8Bit(argv[userAppOffset]),
std::move(userAppArgs));
return app.exec();
}