Skip to content

Commit

Permalink
Android: handle quoted args passed to an app
Browse files Browse the repository at this point in the history
Currently, arguments passed to the app through applicationArguments
extra bundle treat every space as an argument separator. This then
doesn't handle the case where an argument is a space separated quoted
multi-word. This is more apparent when androidtestrunner is passing
test arguments to the app where an argument can be a test case with
a data tag that contains a space, which then is treated as two separate
tag names.

This change makes sure that androidtestrunner quotes each argument,
and the app doesn't split the arguments list by spaces, but rather
passed the argument string directly to c++ where
QProcess::splitCommand() is used to get the correct set of arguments
that will be passed to main().

Pick-to: 6.4 6.3 6.2
Task-number: QTBUG-104730
Change-Id: I45d8ca979d90f2a383c84623f0eb2eec29bba727
Reviewed-by: Dimitrios Apostolou <[email protected]>
Reviewed-by: Ville Voutilainen <[email protected]>
  • Loading branch information
Issam-b committed Aug 11, 2022
1 parent 55af91f commit edc024e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ public void startApp(final boolean firstStart)
}

if (appParams != null)
loaderParams.putString(APPLICATION_PARAMETERS_KEY, appParams.replace(' ', '\t').trim());
loaderParams.putString(APPLICATION_PARAMETERS_KEY, appParams);

loadApplication(loaderParams);
return;
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/platforms/android/androidjnimain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <QtCore/qbasicatomic.h>
#include <QtCore/qjnienvironment.h>
#include <QtCore/qjniobject.h>
#include <QtCore/qprocess.h>
#include <QtCore/qresource.h>
#include <QtCore/qthread.h>
#include <QtGui/private/qguiapplication_p.h>
Expand Down Expand Up @@ -445,11 +446,11 @@ static jboolean startQtAndroidPlugin(JNIEnv *env, jobject /*object*/, jstring pa
m_mainLibraryHnd = nullptr;

const char *nativeString = env->GetStringUTFChars(paramsString, 0);
QByteArray string = nativeString;
const QStringList argsList = QProcess::splitCommand(QString::fromUtf8(nativeString));
env->ReleaseStringUTFChars(paramsString, nativeString);

for (auto str : string.split('\t'))
m_applicationParams.append(str.split(' '));
for (const QString &arg : argsList)
m_applicationParams.append(arg.toUtf8());

// Go home
QDir::setCurrent(QDir::homePath());
Expand Down
15 changes: 9 additions & 6 deletions src/tools/androidtestrunner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ static bool parseTestArgs()

QString file;
QString logType;
QString unhandledArgs;
QStringList unhandledArgs;
for (int i = 0; i < g_options.testArgsList.size(); ++i) {
const QString &arg = g_options.testArgsList[i].trimmed();
if (arg == QStringLiteral("--"))
Expand All @@ -335,7 +335,7 @@ static bool parseTestArgs()
if (match.hasMatch()) {
logType = match.capturedTexts().at(1);
} else {
unhandledArgs += QStringLiteral(" %1").arg(arg);
unhandledArgs << QStringLiteral(" \\\"%1\\\"").arg(arg);
}
}
}
Expand All @@ -345,10 +345,13 @@ static bool parseTestArgs()
for (const auto &format : g_options.outFiles.keys())
g_options.testArgs += QStringLiteral(" -o output.%1,%1").arg(format);

g_options.testArgs += unhandledArgs;
g_options.testArgs = QStringLiteral("shell am start -e applicationArguments \\\"%1\\\" -n %2/%3").arg(shellQuote(g_options.testArgs.trimmed()),
g_options.package,
g_options.activity);
g_options.testArgs += unhandledArgs.join(u' ');

g_options.testArgs = QStringLiteral("shell am start -e applicationArguments \"%1\" -n %2/%3")
.arg(shellQuote(g_options.testArgs.trimmed()))
.arg(g_options.package)
.arg(g_options.activity);

return true;
}

Expand Down

0 comments on commit edc024e

Please sign in to comment.