Android: fix Android assets handler not listing dirs with only sub dirs

Amends edd983071e0a90ee8665d2f45916fb575fc25857.

Remove redundant calls to AAssetDir_getNextFileName() in
AndroidAbstractFileEngine::setFileName(). It's enough to check
if AAssetManager_open() returns null and AAssetManager_openDir() is
valid for the provided asset file name.

As part of this fix, add some unit tests to cover/ensure assets
listing/iterating works as expected.

Fixes: QTBUG-107627
Change-Id: I37ae9cb64fbbc60699bb748895f77fd6a34fae1f
Reviewed-by: Ville Voutilainen <ville.voutilainen@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
(cherry picked from commit 89f89cedc0e62f4b66de340da57d6c56dc61baf0)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Assam Boudjelthia 2022-10-21 02:20:09 +03:00 committed by Qt Cherry-pick Bot
parent 033b1ba4dd
commit 875e90d249
5 changed files with 32 additions and 5 deletions

View File

@ -108,10 +108,12 @@ public:
FolderIterator(const QString &path)
: m_path(path)
{
// Note that empty dirs in the assets dir before the build are not going to be
// included in the final apk, so no empty folders should expected to be listed.
QJniObject files = QJniObject::callStaticObjectMethod(QtAndroid::applicationClass(),
"listAssetContent",
"(Landroid/content/res/AssetManager;Ljava/lang/String;)[Ljava/lang/String;",
QtAndroid::assets(), QJniObject::fromString(path).object());
"listAssetContent",
"(Landroid/content/res/AssetManager;Ljava/lang/String;)[Ljava/lang/String;",
QtAndroid::assets(), QJniObject::fromString(path).object());
if (files.isValid()) {
QJniEnvironment env;
jobjectArray jFiles = files.object<jobjectArray>();
@ -350,8 +352,7 @@ public:
} else {
auto *assetDir = AAssetManager_openDir(m_assetManager, m_fileName.toUtf8());
if (assetDir) {
if (AAssetDir_getNextFileName(assetDir))
m_assetInfo->type = AssetItem::Type::Folder;
m_assetInfo->type = AssetItem::Type::Folder;
AAssetDir_close(assetDir);
}
}

View File

@ -6,6 +6,7 @@
#include <QTest>
#include <QtCore/qnativeinterface.h>
#include <QtCore/qjniobject.h>
#include <QtCore/qdiriterator.h>
class tst_Android : public QObject
{
@ -13,6 +14,7 @@ Q_OBJECT
private slots:
void assetsRead();
void assetsNotWritable();
void assetsIterating();
void testAndroidSdkVersion();
void testAndroidActivity();
void testRunOnAndroidMainThread();
@ -41,6 +43,27 @@ void tst_Android::assetsNotWritable()
QVERIFY(!file.open(QIODevice::Append));
}
void tst_Android::assetsIterating()
{
QStringList assets = {"assets:/top_level_dir/file_in_top_dir.txt",
"assets:/top_level_dir/sub_dir",
"assets:/top_level_dir/sub_dir/file_in_sub_dir.txt",
"assets:/top_level_dir/sub_dir/sub_dir_2",
"assets:/top_level_dir/sub_dir/sub_dir_2/sub_dir_3",
"assets:/top_level_dir/sub_dir/sub_dir_2/sub_dir_3/file_in_sub_dir_3.txt"};
// Note that we have an "assets:/top_level_dir/sub_dir/empty_sub_dir" in the test's
// assets physical directory, but empty folders are not packaged in the built apk,
// so it's expected to not have such folder be listed in the assets on runtime
QDirIterator it("assets:/top_level_dir", QDirIterator::Subdirectories);
QStringList iteratorAssets;
while (it.hasNext())
iteratorAssets.append(it.next());
QVERIFY(assets == iteratorAssets);
}
void tst_Android::testAndroidSdkVersion()
{
QVERIFY(QNativeInterface::QAndroidApplication::sdkVersion() > 0);