summaryrefslogtreecommitdiffstats
path: root/tests/auto/wasm
diff options
context:
space:
mode:
authorMikolaj Boc <mikolaj.boc@qt.io>2022-09-23 15:27:50 +0200
committerMikolaj Boc <mikolaj.boc@qt.io>2023-03-07 12:50:41 +0100
commit79cc3ae201483f42b8b333b29a7924ec1d4e2acd (patch)
tree79568425e40efa1c870dbf18ca88b8345ff39051 /tests/auto/wasm
parentc6183cfc7b971160b293d7f619bb41045837c0e6 (diff)
Support filter list for file input when opening a file on WASM
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 <morten.sorvig@qt.io>
Diffstat (limited to 'tests/auto/wasm')
-rw-r--r--tests/auto/wasm/tst_localfileapi.cpp113
1 files changed, 76 insertions, 37 deletions
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<emscripten::val> types) {
+ emscripten::val makeAccept(std::vector<emscripten::val> 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<emscripten::val> acceptExtensions) {
+ emscripten::val makeType(QString description, std::vector<emscripten::val> 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<QString>("qtFileFilter");
- QTest::addColumn<std::optional<std::string>>("expectedWebExtensionFilter");
-
- QTest::newRow("PNG extension with an asterisk") << QString("*.png") << std::make_optional<std::string>(".png");
- QTest::newRow("Long extension with an asterisk") << QString("*.someotherfile") << std::make_optional<std::string>(".someotherfile");
- QTest::newRow(".dat with no asterisk") << QString(".dat") << std::make_optional<std::string>(".dat");
- QTest::newRow("Multiple asterisks") << QString("*ot*.abc") << std::optional<std::string>();
- QTest::newRow("Filename") << QString("abcd.abc") << std::optional<std::string>();
- QTest::newRow("match all") << QString("*.*") << std::optional<std::string>();
+ QTest::addColumn<std::optional<QString>>("expectedWebExtensionFilter");
+
+ QTest::newRow("PNG extension with an asterisk")
+ << QString("*.png") << std::make_optional<QString>(".png");
+ QTest::newRow("Long extension with an asterisk")
+ << QString("*.someotherfile") << std::make_optional<QString>(".someotherfile");
+ QTest::newRow(".dat with no asterisk")
+ << QString(".dat") << std::make_optional<QString>(".dat");
+ QTest::newRow("Multiple asterisks") << QString("*ot*.abc") << std::optional<QString>();
+ QTest::newRow("Filename") << QString("abcd.abc") << std::optional<QString>();
+ QTest::newRow("match all") << QString("*.*") << std::optional<QString>();
}
void tst_LocalFileApi::fileExtensionFilterTransformation()
{
QFETCH(QString, qtFileFilter);
- QFETCH(std::optional<std::string>, expectedWebExtensionFilter);
+ QFETCH(std::optional<QString>, expectedWebExtensionFilter);
auto result = LocalFileApi::Type::Accept::MimeType::Extension::fromQt(qtFileFilter);
if (expectedWebExtensionFilter) {
- QCOMPARE_EQ(expectedWebExtensionFilter, result->asVal().as<std::string>());
+ QCOMPARE_EQ(expectedWebExtensionFilter, result->value());
} else {
QVERIFY(!result.has_value());
}
@@ -107,34 +114,37 @@ void tst_LocalFileApi::acceptTransformation_data()
using namespace emscripten;
QTest::addColumn<QString>("qtFilterList");
- QTest::addColumn<emscripten::val>("expectedWebType");
+ QTest::addColumn<QStringList>("expectedExtensionList");
- QTest::newRow("Multiple types") << QString("*.png *.other *.txt")
- << makeAccept(std::vector<val> { 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> { 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> { 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<QString>("qtFilterList");
- QTest::addColumn<emscripten::val>("expectedWebType");
+ QTest::addColumn<QString>("expectedDescription");
+ QTest::addColumn<QStringList>("expectedExtensions");
- QTest::newRow("With description") << QString("Text files (*.txt)")
- << makeType("Text files", std::vector<val> { 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> { 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<QStringList>("qtFilterList");
+ QTest::addColumn<QString>("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"