diff options
Diffstat (limited to 'tests/auto/corelib/io/qsettings/tst_qsettings.cpp')
-rw-r--r-- | tests/auto/corelib/io/qsettings/tst_qsettings.cpp | 178 |
1 files changed, 152 insertions, 26 deletions
diff --git a/tests/auto/corelib/io/qsettings/tst_qsettings.cpp b/tests/auto/corelib/io/qsettings/tst_qsettings.cpp index 19ed36589e..f4d7f076ef 100644 --- a/tests/auto/corelib/io/qsettings/tst_qsettings.cpp +++ b/tests/auto/corelib/io/qsettings/tst_qsettings.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2022 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> @@ -10,6 +10,7 @@ #include <QtCore/QCoreApplication> #include <QtCore/QDir> +#include <QtCore/QEventLoop> #include <QtCore/QtGlobal> #include <QtCore/QThread> #include <QtCore/QSysInfo> @@ -41,6 +42,13 @@ #include "qplatformdefs.h" #endif +#if defined(Q_OS_WASM) +#include <QtCore/private/qstdweb_p.h> + +#include "emscripten/threading.h" +#include "emscripten/val.h" +#endif + Q_DECLARE_METATYPE(QSettings::Format) #ifndef QSETTINGS_P_H_VERSION @@ -83,6 +91,10 @@ static void populateWithFormats() QTest::addColumn<QSettings::Format>("format"); QTest::newRow("native") << QSettings::NativeFormat; +#if defined(Q_OS_WASM) + if (qstdweb::haveJspi()) + QTest::newRow("idb") << QSettings::WebIndexedDBFormat; +#endif // defined(Q_OS_WASM) QTest::newRow("ini") << QSettings::IniFormat; QTest::newRow("custom1") << QSettings::CustomFormat1; QTest::newRow("custom2") << QSettings::CustomFormat2; @@ -102,6 +114,9 @@ private slots: void getSetCheck(); void ctor_data() { populateWithFormats(); } void ctor(); +#ifdef Q_OS_WASM + void idb(); +#endif void beginGroup(); void setValue(); void remove(); @@ -131,7 +146,7 @@ private slots: void testRegistryShortRootNames(); void testRegistry32And64Bit(); void trailingWhitespace(); -#ifdef Q_OS_MAC +#ifdef Q_OS_DARWIN void fileName(); #endif void isWritable_data() { populateWithFormats(); } @@ -323,6 +338,32 @@ void tst_QSettings::cleanupTestFiles() QSettings(QSettings::UserScope, "other.software.org").clear(); QSettings(QSettings::SystemScope, "other.software.org").clear(); #endif +#if defined(Q_OS_WASM) + emscripten::val::global("window")["localStorage"].call<void>("clear"); + if (qstdweb::haveJspi()) { + QSettings(QSettings::Format::WebIndexedDBFormat, QSettings::UserScope, "software.org", + "KillerAPP") + .clear(); + QSettings(QSettings::Format::WebIndexedDBFormat, QSettings::SystemScope, "software.org", + "KillerAPP") + .clear(); + QSettings(QSettings::Format::WebIndexedDBFormat, QSettings::UserScope, "other.software.org", + "KillerAPP") + .clear(); + QSettings(QSettings::Format::WebIndexedDBFormat, QSettings::SystemScope, + "other.software.org", "KillerAPP") + .clear(); + QSettings(QSettings::Format::WebIndexedDBFormat, QSettings::UserScope, "software.org") + .clear(); + QSettings(QSettings::Format::WebIndexedDBFormat, QSettings::SystemScope, "software.org") + .clear(); + QSettings(QSettings::Format::WebIndexedDBFormat, QSettings::UserScope, "other.software.org") + .clear(); + QSettings(QSettings::Format::WebIndexedDBFormat, QSettings::SystemScope, + "other.software.org") + .clear(); + } +#endif const QString foo(QLatin1String("foo")); @@ -497,10 +538,10 @@ void tst_QSettings::ctor() QSettings settings5(format, QSettings::UserScope, "SoftWare.ORG", "killerApp"); bool caseSensitive = true; -#if defined(Q_OS_MAC) +#if defined(Q_OS_DARWIN) if (format == QSettings::NativeFormat) { // more details in QMacSettingsPrivate::QMacSettingsPrivate(), organization was comify()-ed - caseSensitive = settings5.fileName().contains("SoftWare.ORG");; + caseSensitive = settings5.fileName().contains("SoftWare.ORG"); } else { caseSensitive = pathconf(settings5.fileName().toLatin1().constData(), _PC_CASE_SENSITIVE); } @@ -550,7 +591,7 @@ void tst_QSettings::ctor() QCOMPARE(settings1.value("alpha/beta/geometry/width").toInt(), 3); QCOMPARE(settings1.value("alpha/beta/geometry/height").toInt(), 4); QCOMPARE(settings1.value("alpha/gamma/splitter").toInt(), 5); - QCOMPARE(settings1.allKeys().count(), 6); + QCOMPARE(settings1.allKeys().size(), 6); QCOMPARE(settings2.value("alpha/beta/geometry").toInt(), -7); QCOMPARE(settings2.value("alpha/beta/geometry/x").toInt(), 1); @@ -558,7 +599,7 @@ void tst_QSettings::ctor() QCOMPARE(settings2.value("alpha/beta/geometry/width").toInt(), 3); QCOMPARE(settings2.value("alpha/beta/geometry/height").toInt(), 4); QCOMPARE(settings2.value("alpha/gamma/splitter").toInt(), 5); - QCOMPARE(settings2.allKeys().count(), 6); + QCOMPARE(settings2.allKeys().size(), 6); } { @@ -569,7 +610,7 @@ void tst_QSettings::ctor() QCOMPARE(settings1.value("alpha/beta/geometry/width").toInt(), 3); QCOMPARE(settings1.value("alpha/beta/geometry/height").toInt(), 4); QCOMPARE(settings1.value("alpha/gamma/splitter").toInt(), 5); - QCOMPARE(settings1.allKeys().count(), 6); + QCOMPARE(settings1.allKeys().size(), 6); } { @@ -578,7 +619,7 @@ void tst_QSettings::ctor() QCoreApplication::instance()->setOrganizationName(""); QCoreApplication::instance()->setApplicationName(""); QSettings settings; -#if defined(Q_OS_MAC) +#if defined(Q_OS_DARWIN) QEXPECT_FAIL("native", "Default settings on Mac are valid, despite organization domain, name, and app name being null", Continue); #endif QCOMPARE(settings.status(), QSettings::AccessError); @@ -593,7 +634,7 @@ void tst_QSettings::ctor() } QSettings settings(format, QSettings::UserScope, "", ""); -#if defined(Q_OS_MAC) +#if defined(Q_OS_DARWIN) QEXPECT_FAIL("native", "Default settings on Mac are valid, despite organization domain, name, and app name being null", Continue); #endif QCOMPARE(settings.status(), QSettings::AccessError); @@ -618,6 +659,50 @@ void tst_QSettings::ctor() } } +#if defined(Q_OS_WASM) +void tst_QSettings::idb() +{ + if (!qstdweb::haveJspi()) + QSKIP("JSPI needed for IndexedDB format"); + + QString systemScopeOrganizationWideFile; + { + QSettings settingsUserScopeAppSpecific(QSettings::Format::WebIndexedDBFormat, + QSettings::UserScope, "software.org", "KillerAPP"); + QSettings settingsUserScopeOrganizationWide(QSettings::Format::WebIndexedDBFormat, + QSettings::UserScope, "software.org"); + QSettings settingsSystemScopeAppSpecific(QSettings::Format::WebIndexedDBFormat, + QSettings::SystemScope, "software.org", + "KillerAPP"); + QSettings settingsSystemScopeOrganizationWide(QSettings::Format::WebIndexedDBFormat, + QSettings::SystemScope, "software.org"); + + settingsSystemScopeOrganizationWide.setValue("testKey", 1); + systemScopeOrganizationWideFile = settingsSystemScopeOrganizationWide.fileName(); + } + + // Emscripten's memfs has a bug that makes a file appear twice in the hashmap. + while (QFile::exists(systemScopeOrganizationWideFile)) { + Q_ASSERT(QFile::remove(systemScopeOrganizationWideFile)); + } + + QEventLoop loop; + QTimer timer; + timer.setInterval(1); + + connect(&timer, &QTimer::timeout, [&loop]() { loop.quit(); }); + timer.start(); + + loop.exec(); + { + QSettings settingsUserScopeAppSpecific(QSettings::Format::WebIndexedDBFormat, + QSettings::UserScope, "software.org", "KillerAPP"); + + QCOMPARE(settingsUserScopeAppSpecific.value("testKey").toInt(), 1); + } +} +#endif // Q_OS_WASM + void tst_QSettings::testByteArray_data() { QTest::addColumn<QByteArray>("data"); @@ -1328,7 +1413,7 @@ void tst_QSettings::testVariantTypes() testValue("collectList", l4, QVariantList); QDateTime dt = QDateTime::currentDateTime(); - dt.setOffsetFromUtc(3600); + dt.setTimeZone(QTimeZone::fromSecondsAheadOfUtc(3600)); testValue("dateTime", dt, QDateTime); #if QT_CONFIG(shortcut) @@ -1731,12 +1816,12 @@ void tst_QSettings::sync() QCOMPARE(settings2.value("moo/beta/geometry/width").toInt(), 3); QCOMPARE(settings2.value("moo/beta/geometry/height").toInt(), 4); QCOMPARE(settings2.value("moo/gamma/splitter").toInt(), 5); - QCOMPARE(settings2.allKeys().count(), 11); + QCOMPARE(settings2.allKeys().size(), 11); // Now, software.org.ini no longer exists, this is same as another app // clearing all settings. settings1.sync(); - QCOMPARE(settings1.allKeys().count(), 0); + QCOMPARE(settings1.allKeys().size(), 0); // Now "some other app" will change software.org.ini QVERIFY(QFile::rename((userConfDir + "other.software.org.ini").toLatin1(), @@ -1754,7 +1839,7 @@ void tst_QSettings::sync() QCOMPARE(settings1.value("moo/beta/geometry/width").toInt(), 3); QCOMPARE(settings1.value("moo/beta/geometry/height").toInt(), 4); QCOMPARE(settings1.value("moo/gamma/splitter").toInt(), 5); - QCOMPARE(settings1.allKeys().count(), 11); + QCOMPARE(settings1.allKeys().size(), 11); } void tst_QSettings::syncNonWriteableDir() @@ -1973,6 +2058,14 @@ void tst_QSettings::testChildKeysAndGroups() l.sort(); QCOMPARE(l, QStringList() << "bar" << "foo"); } + +#if defined(Q_OS_WASM) + // WebIndexedDBFormat does not use the cached settings file on creation, but instead always uses + // the file from the indexed DB anew. + if (format == QSettings::Format::WebIndexedDBFormat) + settings1.sync(); +#endif + { QSettings settings3(format, QSettings::UserScope, "software.org", "application"); settings3.setFallbacksEnabled(false); @@ -2050,6 +2143,16 @@ void SettingsThread::run() void tst_QSettings::testThreadSafety() { +#if !QT_CONFIG(thread) + QSKIP("This test requires threads to be enabled."); +#endif // !QT_CONFIG(thread) +#if defined(Q_OS_WASM) + if (!qstdweb::haveJspi()) + QSKIP("Test needs jspi on WASM. Calls are proxied to the main thread from SettingsThreads, " + "which necessitates the use of an event loop to yield to the main loop. Event loops " + "require jspi."); +#endif + SettingsThread threads[NumThreads]; int i, j; @@ -2057,6 +2160,19 @@ void tst_QSettings::testThreadSafety() for (i = 0; i < NumThreads; ++i) threads[i].start(i + 1); + +#if defined(Q_OS_WASM) && QT_CONFIG(thread) + QEventLoop loop; + int remaining = NumThreads; + for (int i = 0; i < NumThreads; ++i) { + QObject::connect(&threads[i], &QThread::finished, this, [&remaining, &loop]() { + if (!--remaining) + loop.quit(); + }); + } + loop.exec(); +#endif // defined(Q_OS_WASM) && QT_CONFIG(thread) + for (i = 0; i < NumThreads; ++i) threads[i].wait(); @@ -2328,6 +2444,12 @@ void tst_QSettings::fromFile() QStringList strList = QStringList() << "hope" << "destiny" << "chastity"; +#if !defined(Q_OS_WIN) + auto deleteFile = QScopeGuard([path, oldCur]() { + QFile::remove(path); + QDir::setCurrent(oldCur); + }); +#endif // !defined(Q_OS_WIN) { QSettings settings1(path, format); QVERIFY(settings1.allKeys().isEmpty()); @@ -2363,8 +2485,6 @@ void tst_QSettings::fromFile() QCOMPARE(settings1.value("gamma/foo.bar").toInt(), 4); QCOMPARE(settings1.allKeys().size(), 3); } - - QDir::setCurrent(oldCur); } static bool containsSubList(QStringList mom, QStringList son) @@ -2441,17 +2561,17 @@ void tst_QSettings::testArrays() QCOMPARE(settings1.value("ene").toInt(), 2); QCOMPARE(settings1.value("due").toInt(), 3); QCOMPARE(settings1.value("rike").toInt(), 4); - QCOMPARE(settings1.allKeys().count(), 3); + QCOMPARE(settings1.allKeys().size(), 3); settings1.setArrayIndex(1); QCOMPARE(settings1.value("ene").toInt(), 5); QCOMPARE(settings1.value("due").toInt(), 6); QCOMPARE(settings1.value("rike").toInt(), 7); - QCOMPARE(settings1.allKeys().count(), 3); + QCOMPARE(settings1.allKeys().size(), 3); settings1.setArrayIndex(2); QCOMPARE(settings1.value("ene").toInt(), 8); QCOMPARE(settings1.value("due").toInt(), 9); QCOMPARE(settings1.value("rike").toInt(), 10); - QCOMPARE(settings1.allKeys().count(), 3); + QCOMPARE(settings1.allKeys().size(), 3); settings1.endArray(); settings1.endGroup(); @@ -2501,17 +2621,17 @@ void tst_QSettings::testArrays() QCOMPARE(settings1.value("ene").toInt(), 2); QCOMPARE(settings1.value("due").toInt(), 3); QCOMPARE(settings1.value("rike").toInt(), 4); - QCOMPARE(settings1.allKeys().count(), 3); + QCOMPARE(settings1.allKeys().size(), 3); settings1.setArrayIndex(1); QCOMPARE(settings1.value("ene").toInt(), 5); QCOMPARE(settings1.value("due").toInt(), 6); QCOMPARE(settings1.value("rike").toInt(), 7); - QCOMPARE(settings1.allKeys().count(), 3); + QCOMPARE(settings1.allKeys().size(), 3); settings1.setArrayIndex(2); QCOMPARE(settings1.value("ene").toInt(), 8); QCOMPARE(settings1.value("due").toInt(), 9); QCOMPARE(settings1.value("rike").toInt(), 10); - QCOMPARE(settings1.allKeys().count(), 3); + QCOMPARE(settings1.allKeys().size(), 3); settings1.endArray(); settings1.endGroup(); @@ -2683,7 +2803,7 @@ QString escapeWeirdChars(const QString &s) QString result; bool escapeNextDigit = false; - for (int i = 0; i < s.length(); ++i) { + for (int i = 0; i < s.size(); ++i) { QChar c = s.at(i); if (c.unicode() < ' ' || c.unicode() > '~' || (escapeNextDigit && c.unicode() >= '0' && c.unicode() <= 'f')) { @@ -2902,7 +3022,7 @@ void tst_QSettings::testCaseSensitivity() } } -#ifdef Q_OS_MAC +#ifdef Q_OS_DARWIN // Please write a fileName() test for the other platforms void tst_QSettings::fileName() { @@ -3310,7 +3430,7 @@ void tst_QSettings::setPath() path checks that it has no bad side effects. */ for (int i = 0; i < 2; ++i) { -#if !defined(Q_OS_WIN) && !defined(Q_OS_MAC) +#if !defined(Q_OS_WIN) && !defined(Q_OS_DARWIN) && !defined(Q_OS_WASM) TEST_PATH(i == 0, "conf", NativeFormat, UserScope, "alpha") TEST_PATH(i == 0, "conf", NativeFormat, SystemScope, "beta") #endif @@ -3427,6 +3547,12 @@ void tst_QSettings::rainersSyncBugOnMac() if (format == QSettings::NativeFormat) QSKIP("Apple OSes do not support direct reads from and writes to .plist files, due to caching and background syncing. See QTBUG-34899."); #endif +#if defined(Q_OS_WASM) + if (format == QSettings::NativeFormat) + QSKIP("WASM's localStorage backend recognizes no concept of file"); + if (format == QSettings::WebIndexedDBFormat) + QSKIP("WASM's indexedDB backend uses the virtual FS file only as a backing store"); +#endif // Q_OS_WASM QString fileName; @@ -3497,14 +3623,14 @@ void tst_QSettings::consistentRegistryStorage() } #endif -#if defined(QT_BUILD_INTERNAL) && defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN) && !defined(Q_OS_ANDROID) && !defined(QT_NO_STANDARDPATHS) +#if defined(QT_BUILD_INTERNAL) && defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN) && !defined(Q_OS_ANDROID) && !defined(Q_OS_WASM) && !defined(QT_NO_STANDARDPATHS) QT_BEGIN_NAMESPACE extern void clearDefaultPaths(); QT_END_NAMESPACE #endif void tst_QSettings::testXdg() { -#if defined(QT_BUILD_INTERNAL) && defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN) && !defined(Q_OS_ANDROID) && !defined(QT_NO_STANDARDPATHS) +#if defined(QT_BUILD_INTERNAL) && defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN) && !defined(Q_OS_ANDROID) && !defined(Q_OS_WASM) && !defined(QT_NO_STANDARDPATHS) // Note: The XDG_CONFIG_DIRS test must be done before overriding the system path // by QSettings::setPath/setSystemIniPath (used in cleanupTestFiles()). clearDefaultPaths(); |