From 79cc3ae201483f42b8b333b29a7924ec1d4e2acd Mon Sep 17 00:00:00 2001 From: Mikolaj Boc Date: Fri, 23 Sep 2022 15:27:50 +0200 Subject: Support filter list for file input when opening a file on WASM MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The polyfill for file input on WASM now makes use of the supplied filter list. Some changes were introduced in the abstraction for filters so that they are usable both for the new file API and the legacy file input. Change-Id: Id6341be4d6a1647e17382d13da7be42491cfaf80 Reviewed-by: Morten Johan Sørvig --- tests/auto/wasm/tst_localfileapi.cpp | 113 +++++++++++++++++++++++------------ 1 file changed, 76 insertions(+), 37 deletions(-) (limited to 'tests/auto/wasm') diff --git a/tests/auto/wasm/tst_localfileapi.cpp b/tests/auto/wasm/tst_localfileapi.cpp index 0d17202d4b..95bdbcffb2 100644 --- a/tests/auto/wasm/tst_localfileapi.cpp +++ b/tests/auto/wasm/tst_localfileapi.cpp @@ -11,14 +11,16 @@ class tst_LocalFileApi : public QObject Q_OBJECT private: - emscripten::val makeAccept(std::vector types) { + emscripten::val makeAccept(std::vector types) + { auto accept = emscripten::val::object(); accept.set("application/octet-stream", emscripten::val::array(std::move(types))); return accept; } - emscripten::val makeType(QString description, std::vector acceptExtensions) { + emscripten::val makeType(QString description, std::vector acceptExtensions) + { using namespace emscripten; auto type = val::object(); @@ -65,6 +67,8 @@ private Q_SLOTS: void openFileOptions(); void saveFileOptions_data(); void saveFileOptions(); + void fileInputAccept_data(); + void fileInputAccept(); }; bool valDeepEquals(emscripten::val lhs, emscripten::val rhs) @@ -79,24 +83,27 @@ bool valDeepEquals(emscripten::val lhs, emscripten::val rhs) void tst_LocalFileApi::fileExtensionFilterTransformation_data() { QTest::addColumn("qtFileFilter"); - QTest::addColumn>("expectedWebExtensionFilter"); - - QTest::newRow("PNG extension with an asterisk") << QString("*.png") << std::make_optional(".png"); - QTest::newRow("Long extension with an asterisk") << QString("*.someotherfile") << std::make_optional(".someotherfile"); - QTest::newRow(".dat with no asterisk") << QString(".dat") << std::make_optional(".dat"); - QTest::newRow("Multiple asterisks") << QString("*ot*.abc") << std::optional(); - QTest::newRow("Filename") << QString("abcd.abc") << std::optional(); - QTest::newRow("match all") << QString("*.*") << std::optional(); + QTest::addColumn>("expectedWebExtensionFilter"); + + QTest::newRow("PNG extension with an asterisk") + << QString("*.png") << std::make_optional(".png"); + QTest::newRow("Long extension with an asterisk") + << QString("*.someotherfile") << std::make_optional(".someotherfile"); + QTest::newRow(".dat with no asterisk") + << QString(".dat") << std::make_optional(".dat"); + QTest::newRow("Multiple asterisks") << QString("*ot*.abc") << std::optional(); + QTest::newRow("Filename") << QString("abcd.abc") << std::optional(); + QTest::newRow("match all") << QString("*.*") << std::optional(); } void tst_LocalFileApi::fileExtensionFilterTransformation() { QFETCH(QString, qtFileFilter); - QFETCH(std::optional, expectedWebExtensionFilter); + QFETCH(std::optional, expectedWebExtensionFilter); auto result = LocalFileApi::Type::Accept::MimeType::Extension::fromQt(qtFileFilter); if (expectedWebExtensionFilter) { - QCOMPARE_EQ(expectedWebExtensionFilter, result->asVal().as()); + QCOMPARE_EQ(expectedWebExtensionFilter, result->value()); } else { QVERIFY(!result.has_value()); } @@ -107,34 +114,37 @@ void tst_LocalFileApi::acceptTransformation_data() using namespace emscripten; QTest::addColumn("qtFilterList"); - QTest::addColumn("expectedWebType"); + QTest::addColumn("expectedExtensionList"); - QTest::newRow("Multiple types") << QString("*.png *.other *.txt") - << makeAccept(std::vector { val(".png"), val(".other"), val(".txt") }); + QTest::newRow("Multiple types") + << QString("*.png *.other *.txt") << QStringList{ ".png", ".other", ".txt" }; - QTest::newRow("Single type") << QString("*.png") - << makeAccept(std::vector { val(".png") }); + QTest::newRow("Single type") << QString("*.png") << QStringList{ ".png" }; - QTest::newRow("No filter when accepts all") << QString("*.*") - << val::undefined(); + QTest::newRow("No filter when accepts all") << QString("*.*") << QStringList(); - QTest::newRow("No filter when one filter accepts all") << QString("*.* *.jpg") - << val::undefined(); + QTest::newRow("No filter when one filter accepts all") << QString("*.* *.jpg") << QStringList(); QTest::newRow("Weird spaces") << QString(" *.jpg *.png *.icon ") - << makeAccept(std::vector { val(".jpg"), val(".png"), val(".icon") }); + << QStringList{ ".jpg", ".png", ".icon" }; } void tst_LocalFileApi::acceptTransformation() { QFETCH(QString, qtFilterList); - QFETCH(emscripten::val, expectedWebType); + QFETCH(QStringList, expectedExtensionList); auto result = LocalFileApi::Type::Accept::fromQt(qtFilterList); - if (!expectedWebType.isUndefined()) { - QVERIFY(valDeepEquals(result->asVal(), expectedWebType)); - } else { + if (expectedExtensionList.isEmpty()) { QVERIFY(!result.has_value()); + } else { + QStringList transformed; + std::transform(result->mimeType().extensions().begin(), + result->mimeType().extensions().end(), std::back_inserter(transformed), + [](const LocalFileApi::Type::Accept::MimeType::Extension &extension) { + return extension.value().toString(); + }); + QCOMPARE_EQ(expectedExtensionList, transformed); } } @@ -143,26 +153,31 @@ void tst_LocalFileApi::typeTransformation_data() using namespace emscripten; QTest::addColumn("qtFilterList"); - QTest::addColumn("expectedWebType"); + QTest::addColumn("expectedDescription"); + QTest::addColumn("expectedExtensions"); - QTest::newRow("With description") << QString("Text files (*.txt)") - << makeType("Text files", std::vector { val(".txt") }); + QTest::newRow("With description") + << QString("Text files (*.txt)") << QString("Text files") << QStringList{ ".txt" }; - QTest::newRow("No description") << QString("*.jpg") - << makeType("", std::vector { val(".jpg") }); + QTest::newRow("No description") << QString("*.jpg") << QString("") << QStringList{ ".jpg" }; } void tst_LocalFileApi::typeTransformation() { QFETCH(QString, qtFilterList); - QFETCH(emscripten::val, expectedWebType); + QFETCH(QString, expectedDescription); + QFETCH(QStringList, expectedExtensions); auto result = LocalFileApi::Type::fromQt(qtFilterList); - if (!expectedWebType.isUndefined()) { - QVERIFY(valDeepEquals(result->asVal(), expectedWebType)); - } else { - QVERIFY(!result.has_value()); - } + QCOMPARE_EQ(result->description(), expectedDescription); + + QStringList transformed; + std::transform(result->accept()->mimeType().extensions().begin(), + result->accept()->mimeType().extensions().end(), std::back_inserter(transformed), + [](const LocalFileApi::Type::Accept::MimeType::Extension &extension) { + return extension.value().toString(); + }); + QCOMPARE_EQ(expectedExtensions, transformed); } void tst_LocalFileApi::openFileOptions_data() @@ -217,5 +232,29 @@ void tst_LocalFileApi::saveFileOptions() QVERIFY(valDeepEquals(result, expectedWebType)); } +void tst_LocalFileApi::fileInputAccept_data() +{ + using namespace emscripten; + + QTest::addColumn("qtFilterList"); + QTest::addColumn("expectedAccept"); + + QTest::newRow("Multiple files") + << QStringList{ "Text files (*.txt)", "Images (*.jpg *.png)", "*.bat" } + << ".txt,.jpg,.png,.bat"; + QTest::newRow("Spaces") << QStringList{ " Documents (*.doc)", "Everything (*.*)", + " Stuff ( *.stf *.tng)", " *.exe" } + << ".doc,.stf,.tng,.exe"; +} + +void tst_LocalFileApi::fileInputAccept() +{ + QFETCH(QStringList, qtFilterList); + QFETCH(QString, expectedAccept); + + auto result = LocalFileApi::makeFileInputAccept(qtFilterList); + QCOMPARE_EQ(expectedAccept, QString::fromStdString(result)); +} + QTEST_APPLESS_MAIN(tst_LocalFileApi) #include "tst_localfileapi.moc" -- cgit v1.2.3