220int main(
int argc,
char *argv[])
225 QCoreApplication app(argc, argv);
226 app.setApplicationName(u"harmonyostestrunner"_s);
227 app.setApplicationVersion(QString::fromLatin1(QT_VERSION_STR));
229 QCommandLineParser parser;
230 parser.setApplicationDescription(
231 u"Runs a single Qt auto test from an installed HarmonyOS test bundle HAP."_s);
232 parser.addHelpOption();
233 parser.addVersionOption();
235 parser.addPositionalArgument(
237 u"Path to the test shared library (e.g. /path/to/libtst_qobject.so)"_s);
239 QCommandLineOption bundleNameOpt(
241 u"HarmonyOS bundle name of the installed test HAP (env: QT_HARMONYOS_BUNDLE_NAME)"_s,
243 qEnvironmentVariable(
"QT_HARMONYOS_BUNDLE_NAME", u"org.qtproject.autotests"_s));
244 parser.addOption(bundleNameOpt);
246 QCommandLineOption abilityNameOpt(
248 u"HarmonyOS ability name inside the test HAP (env: QT_HARMONYOS_ABILITY_NAME)"_s,
250 qEnvironmentVariable(
"QT_HARMONYOS_ABILITY_NAME", u"QAbility"_s));
251 parser.addOption(abilityNameOpt);
253 QCommandLineOption hdcOpt(
255 u"Path to the hdc tool (env: QT_HARMONYOS_HDC)"_s,
257 qEnvironmentVariable(
"QT_HARMONYOS_HDC", u"hdc"_s));
258 parser.addOption(hdcOpt);
260 QCommandLineOption timeoutOpt(
262 u"Seconds to wait for a test to complete before aborting (env: QT_HARMONYOS_TEST_TIMEOUT)"_s,
264 qEnvironmentVariable(
"QT_HARMONYOS_TEST_TIMEOUT", u"300"_s));
265 parser.addOption(timeoutOpt);
267 QCommandLineOption noProgressTimeoutOpt(
268 u"no-progress-timeout"_s,
269 u"Seconds without a PASS/FAIL test case result before declaring the test hung "
270 u"(env: QT_HARMONYOS_NO_PROGRESS_TIMEOUT, 0 = disabled)"_s,
272 qEnvironmentVariable(
"QT_HARMONYOS_NO_PROGRESS_TIMEOUT", u"60"_s));
273 parser.addOption(noProgressTimeoutOpt);
275 QCommandLineOption deviceOpt(
277 u"hdc connect key (-t) for the target device — required when multiple devices "
278 u"are attached (env: QT_HARMONYOS_DEVICE)"_s,
280 qEnvironmentVariable(
"QT_HARMONYOS_DEVICE"));
281 parser.addOption(deviceOpt);
287 const QStringList positional = parser.positionalArguments();
288 if (positional.isEmpty()) {
289 fprintf(stderr,
"harmonyostestrunner: no test binary specified\n");
293 const QString testBinaryPath = positional.first();
294 const QString testLibName = QFileInfo(testBinaryPath).fileName();
296 const QStringList testArgs = positional.mid(1);
297 const QString bundleName = parser.value(bundleNameOpt);
298 const QString abilityName = parser.value(abilityNameOpt);
299 const QString hdc = parser.value(hdcOpt);
300 const int timeoutSecs = parser.value(timeoutOpt).toInt();
301 const int noProgressTimeoutSecs = parser.value(noProgressTimeoutOpt).toInt();
305 const QString runId = QString::number(QDateTime::currentMSecsSinceEpoch());
307 const QString appBase = u"/data/storage/el2/base/files"_s;
308 const QString appStdoutPath = appBase + u"/qt_stdout_"_s + runId + u".txt"_s;
309 const QString appExitCodePath = appBase + u"/qt_exitcode_"_s + runId + u".txt"_s;
311 const QString shellBase = u"/data/app/el2/100/base/"_s + bundleName + u"/files"_s;
312 const QString shellStdoutPath = shellBase + u"/qt_stdout_"_s + runId + u".txt"_s;
313 const QString shellExitCodePath = shellBase + u"/qt_exitcode_"_s + runId + u".txt"_s;
315 const QString bundleCheckOutput =
316 runHdc(hdc, {u"shell"_s, u"bm"_s, u"dump"_s, u"-n"_s, bundleName});
317 if (bundleCheckOutput.contains(u"error"_s, Qt::CaseInsensitive)
318 || bundleCheckOutput.trimmed().isEmpty()) {
320 "harmonyostestrunner: bundle '%s' is not installed on the device.\n"
321 " Build and sign the test HAP, then install it with:\n"
322 " hdc install <path/to/autotests-signed.hap>\n",
323 qPrintable(bundleName));
327#if QT_CONFIG(systemsemaphore)
328 TestRunnerSystemSemaphore runnerLock(runnerLockKey(g_hdcConnectKey, bundleName));
329 g_runnerLock = &runnerLock;
330 runnerLock.acquire();
333 runHdc(hdc, {u"shell"_s, u"aa"_s, u"force-stop"_s, bundleName});
334 waitForProcessDeath(hdc, bundleName);
338 QStringList aaStartArgs = {
339 u"shell"_s, u"aa"_s, u"start"_s,
341 u"-a"_s, abilityName,
342 u"--ps"_s, u"io.qt.appSharedLibNameOverride"_s, testLibName,
343 u"--ps"_s, u"io.qt.debug.redirectedStdoutPath"_s, appStdoutPath,
344 u"--ps"_s, u"io.qt.debug.exitCodePath"_s, appExitCodePath,
347 u"--pb"_s, u"io.qt.useUriAsArg"_s, u"false"_s,
349 u"--pb"_s, u"io.qt.useDefaultUiAbilityInstanceInQt"_s, u"false"_s,
352 aaStartArgs << u"--pb"_s << u"io.qt.watchdogEnabled"_s << u"false"_s;
354 if (!testArgs.isEmpty()) {
355 const QString json = QString::fromUtf8(
356 QJsonDocument(QJsonArray::fromStringList(testArgs)).toJson(QJsonDocument::Compact));
357 aaStartArgs += {u"--ps"_s, u"io.qt.appArgsJson"_s, shellSingleQuote(json)};
362 const QString aaOut = runHdc(hdc, aaStartArgs,
true);
363 if (aaOut.contains(u"error"_s, Qt::CaseInsensitive)
364 || aaOut.contains(u"failed"_s, Qt::CaseInsensitive)) {
365 fprintf(stderr,
"harmonyostestrunner: aa start: %s\n", qPrintable(aaOut.trimmed()));
369 if (!waitForProcessStart(hdc, bundleName, shellExitCodePath)) {
370 fprintf(stderr,
"harmonyostestrunner: %s: timed out waiting for process to start\n",
371 qPrintable(testLibName));
372#if QT_CONFIG(systemsemaphore)
373 runnerLock.release();
378 waitForStdoutFile(hdc, shellStdoutPath);
380 QProcess stdoutLogger;
381 const bool hasStdoutLogger = setupStdoutLogger(stdoutLogger, hdc, shellStdoutPath);
382 if (!hasStdoutLogger) {
383 fprintf(stderr,
"harmonyostestrunner: warning: failed to start stdout logger, "
384 "output may be delayed\n");
389 const int pollIntervalMs = 500;
391 QElapsedTimer elapsed;
393 int testExitCode = -1;
394 bool completed =
false;
395 int aliveCheckCounter = 0;
396 qint64 lastTestProgressAt = -1;
397 qint64 lastHeartbeatSecs = 0;
400 && elapsed.elapsed() < qint64(timeoutSecs) * 1000)
402 if (hasStdoutLogger) {
403 const QByteArray chunk = stdoutLogger.readAllStandardOutput();
404 if (!chunk.isEmpty()) {
405 fwrite(chunk.constData(), 1,
static_cast<size_t>(chunk.size()), stdout);
410 if (chunk.contains(
"PASS :") || chunk.contains(
"FAIL! :")
411 || chunk.contains(
"Totals:"))
412 lastTestProgressAt = elapsed.elapsed();
418 const QString exitContent =
419 runHdc(hdc, {u"shell"_s, u"cat"_s, shellExitCodePath});
421 const int code = exitContent.trimmed().toInt(&ok);
431 if (++aliveCheckCounter % 3 == 0 && !isProcessAlive(hdc, bundleName)) {
432 const QString exitContent =
433 runHdc(hdc, {u"shell"_s, u"cat"_s, shellExitCodePath});
435 const int code = exitContent.trimmed().toInt(&ok);
442 fprintf(stderr,
"harmonyostestrunner: %s: process exited without writing "
443 "exit code — likely crashed\n", qPrintable(testLibName));
449 if (noProgressTimeoutSecs > 0 && lastTestProgressAt >= 0
450 && elapsed.elapsed() - lastTestProgressAt
451 > qint64(noProgressTimeoutSecs) * 1000)
454 "harmonyostestrunner: %s: no test case progress for %d seconds "
455 "— main thread likely deadlocked, force-stopping\n",
456 qPrintable(testLibName), noProgressTimeoutSecs);
457 runHdc(hdc, {u"shell"_s, u"aa"_s, u"force-stop"_s, bundleName});
463 const qint64 elapsedSecs = elapsed.elapsed() / 1000;
464 if (elapsedSecs >= lastHeartbeatSecs + 30) {
465 fprintf(stderr,
"harmonyostestrunner: %s still running (%lld s elapsed)\n",
466 qPrintable(testLibName),
static_cast<
long long>(elapsedSecs));
467 lastHeartbeatSecs = elapsedSecs;
471 stdoutLogger.waitForReadyRead(pollIntervalMs);
473 QThread::msleep(pollIntervalMs);
476 if (hasStdoutLogger) {
477 if (stdoutLogger.state() != QProcess::NotRunning) {
480 while (stdoutLogger.waitForReadyRead(200)) {
481 const QByteArray chunk = stdoutLogger.readAllStandardOutput();
484 fwrite(chunk.constData(), 1,
static_cast<size_t>(chunk.size()), stdout);
487 stdoutLogger.terminate();
488 stdoutLogger.waitForFinished(5000);
490 const QByteArray finalChunk = stdoutLogger.readAllStandardOutput();
491 if (!finalChunk.isEmpty()) {
492 fwrite(finalChunk.constData(), 1,
static_cast<size_t>(finalChunk.size()), stdout);
499 runHdc(hdc, {u"shell"_s, u"aa"_s, u"force-stop"_s, bundleName});
500#if QT_CONFIG(systemsemaphore)
501 runnerLock.release();
506 "harmonyostestrunner: TIMEOUT — %s did not complete within %d seconds\n",
507 qPrintable(testLibName), timeoutSecs);
508 runHdc(hdc, {u"shell"_s, u"aa"_s, u"force-stop"_s, bundleName});
509#if QT_CONFIG(systemsemaphore)
510 runnerLock.release();
515#if QT_CONFIG(systemsemaphore)
516 runnerLock.release();