205int main(
int argc,
char *argv[])
210 QCoreApplication app(argc, argv);
211 app.setApplicationName(u"harmonyostestrunner"_s);
212 app.setApplicationVersion(QString::fromLatin1(QT_VERSION_STR));
214 QCommandLineParser parser;
215 parser.setApplicationDescription(
216 u"Runs a single Qt auto test from an installed HarmonyOS test bundle HAP."_s);
217 parser.addHelpOption();
218 parser.addVersionOption();
220 parser.addPositionalArgument(
222 u"Path to the test shared library (e.g. /path/to/libtst_qobject.so)"_s);
224 QCommandLineOption bundleNameOpt(
226 u"HarmonyOS bundle name of the installed test HAP (env: QT_HARMONYOS_BUNDLE_NAME)"_s,
228 qEnvironmentVariable(
"QT_HARMONYOS_BUNDLE_NAME", u"org.qtproject.autotests"_s));
229 parser.addOption(bundleNameOpt);
231 QCommandLineOption abilityNameOpt(
233 u"HarmonyOS ability name inside the test HAP (env: QT_HARMONYOS_ABILITY_NAME)"_s,
235 qEnvironmentVariable(
"QT_HARMONYOS_ABILITY_NAME", u"QAbility"_s));
236 parser.addOption(abilityNameOpt);
238 QCommandLineOption hdcOpt(
240 u"Path to the hdc tool (env: QT_HARMONYOS_HDC)"_s,
242 qEnvironmentVariable(
"QT_HARMONYOS_HDC", u"hdc"_s));
243 parser.addOption(hdcOpt);
245 QCommandLineOption timeoutOpt(
247 u"Seconds to wait for a test to complete before aborting (env: QT_HARMONYOS_TEST_TIMEOUT)"_s,
249 qEnvironmentVariable(
"QT_HARMONYOS_TEST_TIMEOUT", u"300"_s));
250 parser.addOption(timeoutOpt);
252 QCommandLineOption noProgressTimeoutOpt(
253 u"no-progress-timeout"_s,
254 u"Seconds without a PASS/FAIL test case result before declaring the test hung "
255 u"(env: QT_HARMONYOS_NO_PROGRESS_TIMEOUT, 0 = disabled)"_s,
257 qEnvironmentVariable(
"QT_HARMONYOS_NO_PROGRESS_TIMEOUT", u"60"_s));
258 parser.addOption(noProgressTimeoutOpt);
260 QCommandLineOption deviceOpt(
262 u"hdc connect key (-t) for the target device — required when multiple devices "
263 u"are attached (env: QT_HARMONYOS_DEVICE)"_s,
265 qEnvironmentVariable(
"QT_HARMONYOS_DEVICE"));
266 parser.addOption(deviceOpt);
272 const QStringList positional = parser.positionalArguments();
273 if (positional.isEmpty()) {
274 fprintf(stderr,
"harmonyostestrunner: no test binary specified\n");
278 const QString testBinaryPath = positional.first();
279 const QString testLibName = QFileInfo(testBinaryPath).fileName();
281 const QStringList testArgs = positional.mid(1);
282 const QString bundleName = parser.value(bundleNameOpt);
283 const QString abilityName = parser.value(abilityNameOpt);
284 const QString hdc = parser.value(hdcOpt);
285 const int timeoutSecs = parser.value(timeoutOpt).toInt();
286 const int noProgressTimeoutSecs = parser.value(noProgressTimeoutOpt).toInt();
290 const QString runId = QString::number(QDateTime::currentMSecsSinceEpoch());
292 const QString appBase = u"/data/storage/el2/base/files"_s;
293 const QString appStdoutPath = appBase + u"/qt_stdout_"_s + runId + u".txt"_s;
294 const QString appExitCodePath = appBase + u"/qt_exitcode_"_s + runId + u".txt"_s;
296 const QString shellBase = u"/data/app/el2/100/base/"_s + bundleName + u"/files"_s;
297 const QString shellStdoutPath = shellBase + u"/qt_stdout_"_s + runId + u".txt"_s;
298 const QString shellExitCodePath = shellBase + u"/qt_exitcode_"_s + runId + u".txt"_s;
300 const QString bundleCheckOutput =
301 runHdc(hdc, {u"shell"_s, u"bm"_s, u"dump"_s, u"-n"_s, bundleName});
302 if (bundleCheckOutput.contains(u"error"_s, Qt::CaseInsensitive)
303 || bundleCheckOutput.trimmed().isEmpty()) {
305 "harmonyostestrunner: bundle '%s' is not installed on the device.\n"
306 " Build and sign the test HAP, then install it with:\n"
307 " hdc install <path/to/autotests-signed.hap>\n",
308 qPrintable(bundleName));
312#if QT_CONFIG(systemsemaphore)
313 g_runnerLock.acquire();
316 runHdc(hdc, {u"shell"_s, u"aa"_s, u"force-stop"_s, bundleName});
317 waitForProcessDeath(hdc, bundleName);
321 QStringList aaStartArgs = {
322 u"shell"_s, u"aa"_s, u"start"_s,
324 u"-a"_s, abilityName,
325 u"--ps"_s, u"io.qt.appSharedLibNameOverride"_s, testLibName,
326 u"--ps"_s, u"io.qt.debug.redirectedStdoutPath"_s, appStdoutPath,
327 u"--ps"_s, u"io.qt.debug.exitCodePath"_s, appExitCodePath,
330 u"--pb"_s, u"io.qt.useUriAsArg"_s, u"false"_s,
332 u"--pb"_s, u"io.qt.useDefaultUiAbilityInstanceInQt"_s, u"false"_s,
335 aaStartArgs << u"--pb"_s << u"io.qt.watchdogEnabled"_s << u"false"_s;
337 if (!testArgs.isEmpty()) {
338 QString json = u"["_s;
339 for (
int i = 0; i < testArgs.size(); ++i) {
342 QString escaped = testArgs.at(i);
343 escaped.replace(u'\\', u"\\\\"_s);
344 escaped.replace(u'"', u"\\\""_s);
345 json += u"\""_s + escaped + u"\""_s;
348 aaStartArgs += {u"--ps"_s, u"io.qt.appArgsJson"_s, json};
353 const QString aaOut = runHdc(hdc, aaStartArgs,
true);
354 if (aaOut.contains(u"error"_s, Qt::CaseInsensitive)
355 || aaOut.contains(u"failed"_s, Qt::CaseInsensitive)) {
356 fprintf(stderr,
"harmonyostestrunner: aa start: %s\n", qPrintable(aaOut.trimmed()));
360 if (!waitForProcessStart(hdc, bundleName, shellExitCodePath)) {
361 fprintf(stderr,
"harmonyostestrunner: %s: timed out waiting for process to start\n",
362 qPrintable(testLibName));
363#if QT_CONFIG(systemsemaphore)
364 g_runnerLock.release();
369 waitForStdoutFile(hdc, shellStdoutPath);
371 QProcess stdoutLogger;
372 const bool hasStdoutLogger = setupStdoutLogger(stdoutLogger, hdc, shellStdoutPath);
373 if (!hasStdoutLogger) {
374 fprintf(stderr,
"harmonyostestrunner: warning: failed to start stdout logger, "
375 "output may be delayed\n");
380 const int pollIntervalMs = 500;
382 QElapsedTimer elapsed;
384 int testExitCode = -1;
385 bool completed =
false;
386 int aliveCheckCounter = 0;
387 qint64 lastTestProgressAt = -1;
388 qint64 lastHeartbeatSecs = 0;
391 && elapsed.elapsed() < qint64(timeoutSecs) * 1000)
393 if (hasStdoutLogger) {
394 const QByteArray chunk = stdoutLogger.readAllStandardOutput();
395 if (!chunk.isEmpty()) {
396 fwrite(chunk.constData(), 1,
static_cast<size_t>(chunk.size()), stdout);
401 if (chunk.contains(
"PASS :") || chunk.contains(
"FAIL! :")
402 || chunk.contains(
"Totals:"))
403 lastTestProgressAt = elapsed.elapsed();
409 const QString exitContent =
410 runHdc(hdc, {u"shell"_s, u"cat"_s, shellExitCodePath});
412 const int code = exitContent.trimmed().toInt(&ok);
422 if (++aliveCheckCounter % 3 == 0 && !isProcessAlive(hdc, bundleName)) {
423 const QString exitContent =
424 runHdc(hdc, {u"shell"_s, u"cat"_s, shellExitCodePath});
426 const int code = exitContent.trimmed().toInt(&ok);
433 fprintf(stderr,
"harmonyostestrunner: %s: process exited without writing "
434 "exit code — likely crashed\n", qPrintable(testLibName));
440 if (noProgressTimeoutSecs > 0 && lastTestProgressAt >= 0
441 && elapsed.elapsed() - lastTestProgressAt
442 > qint64(noProgressTimeoutSecs) * 1000)
445 "harmonyostestrunner: %s: no test case progress for %d seconds "
446 "— main thread likely deadlocked, force-stopping\n",
447 qPrintable(testLibName), noProgressTimeoutSecs);
448 runHdc(hdc, {u"shell"_s, u"aa"_s, u"force-stop"_s, bundleName});
454 const qint64 elapsedSecs = elapsed.elapsed() / 1000;
455 if (elapsedSecs >= lastHeartbeatSecs + 30) {
456 fprintf(stderr,
"harmonyostestrunner: %s still running (%lld s elapsed)\n",
457 qPrintable(testLibName),
static_cast<
long long>(elapsedSecs));
458 lastHeartbeatSecs = elapsedSecs;
462 stdoutLogger.waitForReadyRead(pollIntervalMs);
464 QThread::msleep(pollIntervalMs);
467 if (hasStdoutLogger) {
468 if (stdoutLogger.state() != QProcess::NotRunning) {
471 while (stdoutLogger.waitForReadyRead(200)) {
472 const QByteArray chunk = stdoutLogger.readAllStandardOutput();
475 fwrite(chunk.constData(), 1,
static_cast<size_t>(chunk.size()), stdout);
478 stdoutLogger.terminate();
479 stdoutLogger.waitForFinished(5000);
481 const QByteArray finalChunk = stdoutLogger.readAllStandardOutput();
482 if (!finalChunk.isEmpty()) {
483 fwrite(finalChunk.constData(), 1,
static_cast<size_t>(finalChunk.size()), stdout);
490 runHdc(hdc, {u"shell"_s, u"aa"_s, u"force-stop"_s, bundleName});
491#if QT_CONFIG(systemsemaphore)
492 g_runnerLock.release();
497 "harmonyostestrunner: TIMEOUT — %s did not complete within %d seconds\n",
498 qPrintable(testLibName), timeoutSecs);
499 runHdc(hdc, {u"shell"_s, u"aa"_s, u"force-stop"_s, bundleName});
500#if QT_CONFIG(systemsemaphore)
501 g_runnerLock.release();
506#if QT_CONFIG(systemsemaphore)
507 g_runnerLock.release();