Move the libraryPath tests from tst_QApplication to tst_QCoreApplication

Those aren't GUI tests. I suppose they've been in tst_QApplication since
Qt 2 or 3, when there was no QCore/GuiApplication yet, and were never
moved.

Pick-to: 6.9
Change-Id: I30b3c1a309ba2c720210fffd045ebd0bcfa803fe
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Thiago Macieira 2025-01-06 20:20:22 -03:00
parent cc4abefb1d
commit 9fe44e4779
3 changed files with 214 additions and 211 deletions

View File

@ -17,6 +17,8 @@
#include <QtCore/qt_windows.h> #include <QtCore/qt_windows.h>
#endif #endif
Q_LOGGING_CATEGORY(lcTests, "qt.tests." QT_STRINGIFY(tst_QCoreApplication))
typedef QCoreApplication TestApplication; typedef QCoreApplication TestApplication;
class EventSpy : public QObject class EventSpy : public QObject
@ -211,6 +213,213 @@ void tst_QCoreApplication::argc()
} }
} }
#if QT_CONFIG(library)
static char argv0name[] =
#ifdef QT_GUI_LIB
"tst_qguiapplication"
#else
"tst_qcoreapplication"
#endif
;
static char *argv0 = argv0name;
static bool isPathListIncluded(const QStringList &l, const QStringList &r)
{
int size = r.size();
if (size > l.size())
return false;
#if defined (Q_OS_WIN)
Qt::CaseSensitivity cs = Qt::CaseInsensitive;
#else
Qt::CaseSensitivity cs = Qt::CaseSensitive;
#endif
int i = 0, j = 0;
for ( ; i < l.size() && j < r.size(); ++i) {
if (QDir::toNativeSeparators(l[i]).compare(QDir::toNativeSeparators(r[j]), cs) == 0) {
++j;
i = -1;
}
}
return j == r.size();
}
void tst_QCoreApplication::libraryPaths()
{
const QString testDir = QFileInfo(QFINDTESTDATA("CMakeLists.txt")).absolutePath();
QVERIFY(!testDir.isEmpty());
{
QCoreApplication::setLibraryPaths(QStringList() << testDir);
QCOMPARE(QCoreApplication::libraryPaths(), (QStringList() << testDir));
// creating QCoreApplication adds the applicationDirPath to the libraryPath
int argc = 1;
QCoreApplication app(argc, &argv0);
QString appDirPath = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
QStringList actual = QCoreApplication::libraryPaths();
actual.sort();
QStringList expected;
expected << testDir << appDirPath;
expected = QSet<QString>(expected.constBegin(), expected.constEnd()).values();
expected.sort();
QVERIFY2(isPathListIncluded(actual, expected),
qPrintable("actual:\n - " + actual.join("\n - ") +
"\nexpected:\n - " + expected.join("\n - ")));
}
{
// creating QCoreApplication adds the applicationDirPath and plugin install path to the libraryPath
int argc = 1;
QCoreApplication app(argc, &argv0);
QString appDirPath = QCoreApplication::applicationDirPath();
QString installPathPlugins = QLibraryInfo::path(QLibraryInfo::PluginsPath);
QStringList actual = QCoreApplication::libraryPaths();
actual.sort();
QStringList expected;
expected << installPathPlugins << appDirPath;
expected = QSet<QString>(expected.constBegin(), expected.constEnd()).values();
expected.sort();
#if defined(Q_OS_VXWORKS)
QEXPECT_FAIL("", "QTBUG-130736: Actual paths on VxWorks differ from expected", Abort);
#endif
QVERIFY2(isPathListIncluded(actual, expected),
qPrintable("actual:\n - " + actual.join("\n - ") +
"\nexpected:\n - " + expected.join("\n - ")));
// setting the library paths overrides everything
QCoreApplication::setLibraryPaths(QStringList() << testDir);
QVERIFY2(isPathListIncluded(QCoreApplication::libraryPaths(), (QStringList() << testDir)),
qPrintable("actual:\n - " + QCoreApplication::libraryPaths().join("\n - ") +
"\nexpected:\n - " + testDir));
}
{
qCDebug(lcTests) << "Initial library path:" << QCoreApplication::libraryPaths();
int count = QCoreApplication::libraryPaths().size();
#if 0
QCOMPARE(count, 1); // before creating QCoreApplication, only the PluginsPath is in the libraryPaths()
#endif
QString installPathPlugins = QLibraryInfo::path(QLibraryInfo::PluginsPath);
QCoreApplication::addLibraryPath(installPathPlugins);
qCDebug(lcTests) << "installPathPlugins" << installPathPlugins;
qCDebug(lcTests) << "After adding plugins path:" << QCoreApplication::libraryPaths();
QCOMPARE(QCoreApplication::libraryPaths().size(), count);
QCoreApplication::addLibraryPath(testDir);
QCOMPARE(QCoreApplication::libraryPaths().size(), count + 1);
// creating QCoreApplication adds the applicationDirPath to the libraryPath
int argc = 1;
QCoreApplication app(argc, &argv0);
QString appDirPath = QCoreApplication::applicationDirPath();
qCDebug(lcTests) << QCoreApplication::libraryPaths();
// On Windows CE these are identical and might also be the case for other
// systems too
if (appDirPath != installPathPlugins)
QCOMPARE(QCoreApplication::libraryPaths().size(), count + 2);
}
{
int argc = 1;
QCoreApplication app(argc, &argv0);
qCDebug(lcTests) << "Initial library path:" << QCoreApplication::libraryPaths();
int count = QCoreApplication::libraryPaths().size();
QString installPathPlugins = QLibraryInfo::path(QLibraryInfo::PluginsPath);
QCoreApplication::addLibraryPath(installPathPlugins);
qCDebug(lcTests) << "installPathPlugins" << installPathPlugins;
qCDebug(lcTests) << "After adding plugins path:" << QCoreApplication::libraryPaths();
QCOMPARE(QCoreApplication::libraryPaths().size(), count);
QString appDirPath = QCoreApplication::applicationDirPath();
QCoreApplication::addLibraryPath(appDirPath);
QCoreApplication::addLibraryPath(appDirPath + "/..");
qCDebug(lcTests) << "appDirPath" << appDirPath;
qCDebug(lcTests) << "After adding appDirPath && appDirPath + /..:" << QCoreApplication::libraryPaths();
QCOMPARE(QCoreApplication::libraryPaths().size(), count + 1);
#ifdef Q_OS_MACOS
QCoreApplication::addLibraryPath(appDirPath + "/../MacOS");
#else
QCoreApplication::addLibraryPath(appDirPath + "/tmp/..");
#endif
qCDebug(lcTests) << "After adding appDirPath + /tmp/..:" << QCoreApplication::libraryPaths();
QCOMPARE(QCoreApplication::libraryPaths().size(), count + 1);
}
}
void tst_QCoreApplication::libraryPaths_qt_plugin_path()
{
int argc = 1;
QCoreApplication app(argc, &argv0);
QString appDirPath = QCoreApplication::applicationDirPath();
// Our hook into libraryPaths() initialization: Set the QT_PLUGIN_PATH environment variable
QString installPathPluginsDeCanon = appDirPath + QString::fromLatin1("/tmp/..");
QByteArray ascii = QFile::encodeName(installPathPluginsDeCanon);
qputenv("QT_PLUGIN_PATH", ascii);
QVERIFY(!QCoreApplication::libraryPaths().contains(appDirPath + QString::fromLatin1("/tmp/..")));
}
void tst_QCoreApplication::libraryPaths_qt_plugin_path_2()
{
#ifdef Q_OS_UNIX
QByteArray validPath = QDir("/tmp").canonicalPath().toLatin1();
QByteArray nonExistentPath = "/nonexistent";
QByteArray pluginPath = validPath + ':' + nonExistentPath;
#elif defined(Q_OS_WIN)
QByteArray validPath = "C:\\windows";
QByteArray nonExistentPath = "Z:\\nonexistent";
QByteArray pluginPath = validPath + ';' + nonExistentPath;
#endif
{
// Our hook into libraryPaths() initialization: Set the QT_PLUGIN_PATH environment variable
qputenv("QT_PLUGIN_PATH", pluginPath);
int argc = 1;
QCoreApplication app(argc, &argv0);
// library path list should contain the default plus the one valid path
QStringList expected =
QStringList()
<< QLibraryInfo::path(QLibraryInfo::PluginsPath)
<< QDir(QCoreApplication::applicationDirPath()).canonicalPath()
<< QDir(QDir::fromNativeSeparators(QString::fromLatin1(validPath))).canonicalPath();
#if defined(Q_OS_VXWORKS)
QEXPECT_FAIL("", "QTBUG-130736: Actual paths on VxWorks differ from expected", Abort);
#endif
QVERIFY2(isPathListIncluded(QCoreApplication::libraryPaths(), expected),
qPrintable("actual:\n - " + QCoreApplication::libraryPaths().join("\n - ") +
"\nexpected:\n - " + expected.join("\n - ")));
}
{
int argc = 1;
QCoreApplication app(argc, &argv0);
// library paths are initialized by the QCoreApplication, setting
// the environment variable here doesn't work
qputenv("QT_PLUGIN_PATH", pluginPath);
// library path list should contain the default
QStringList expected =
QStringList()
<< QLibraryInfo::path(QLibraryInfo::PluginsPath)
<< QCoreApplication::applicationDirPath();
QVERIFY(isPathListIncluded(QCoreApplication::libraryPaths(), expected));
qputenv("QT_PLUGIN_PATH", nullptr);
}
}
#endif
class EventGenerator : public QObject class EventGenerator : public QObject
{ {
Q_OBJECT Q_OBJECT

View File

@ -18,6 +18,11 @@ private slots:
void qAppName(); void qAppName();
void qAppVersion(); void qAppVersion();
void argc(); void argc();
#if QT_CONFIG(library)
void libraryPaths();
void libraryPaths_qt_plugin_path();
void libraryPaths_qt_plugin_path_2();
#endif
void postEvent(); void postEvent();
void removePostedEvents(); void removePostedEvents();
#if QT_CONFIG(thread) #if QT_CONFIG(thread)

View File

@ -49,8 +49,6 @@
#include <algorithm> #include <algorithm>
Q_LOGGING_CATEGORY(lcTests, "qt.widgets.tests")
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
extern bool Q_GUI_EXPORT qt_tab_all_widgets(); // from qapplication.cpp extern bool Q_GUI_EXPORT qt_tab_all_widgets(); // from qapplication.cpp
@ -91,12 +89,6 @@ private slots:
void testDeleteLaterProcessEvents4(); void testDeleteLaterProcessEvents4();
void testDeleteLaterProcessEvents5(); void testDeleteLaterProcessEvents5();
#if QT_CONFIG(library)
void libraryPaths();
void libraryPaths_qt_plugin_path();
void libraryPaths_qt_plugin_path_2();
#endif
#ifdef QT_BUILD_INTERNAL #ifdef QT_BUILD_INTERNAL
void sendPostedEvents(); void sendPostedEvents();
#endif // ifdef QT_BUILD_INTERNAL #endif // ifdef QT_BUILD_INTERNAL
@ -960,209 +952,6 @@ void tst_QApplication::closeAllWindows()
qDeleteAll(QApplication::topLevelWidgets()); qDeleteAll(QApplication::topLevelWidgets());
} }
bool isPathListIncluded(const QStringList &l, const QStringList &r)
{
int size = r.size();
if (size > l.size())
return false;
#if defined (Q_OS_WIN)
Qt::CaseSensitivity cs = Qt::CaseInsensitive;
#else
Qt::CaseSensitivity cs = Qt::CaseSensitive;
#endif
int i = 0, j = 0;
for ( ; i < l.size() && j < r.size(); ++i) {
if (QDir::toNativeSeparators(l[i]).compare(QDir::toNativeSeparators(r[j]), cs) == 0) {
++j;
i = -1;
}
}
return j == r.size();
}
#if QT_CONFIG(library)
void tst_QApplication::libraryPaths()
{
#ifndef BUILTIN_TESTDATA
const QString testDir = QFileInfo(QFINDTESTDATA("test/CMakeLists.txt")).absolutePath();
#else
const QString testDir = QFileInfo(QFINDTESTDATA("CMakeLists.txt")).absolutePath();
#endif
QVERIFY(!testDir.isEmpty());
{
QApplication::setLibraryPaths(QStringList() << testDir);
QCOMPARE(QApplication::libraryPaths(), (QStringList() << testDir));
// creating QApplication adds the applicationDirPath to the libraryPath
int argc = 1;
QApplication app(argc, &argv0);
QString appDirPath = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
QStringList actual = QApplication::libraryPaths();
actual.sort();
QStringList expected;
expected << testDir << appDirPath;
expected = QSet<QString>(expected.constBegin(), expected.constEnd()).values();
expected.sort();
QVERIFY2(isPathListIncluded(actual, expected),
qPrintable("actual:\n - " + actual.join("\n - ") +
"\nexpected:\n - " + expected.join("\n - ")));
}
{
// creating QApplication adds the applicationDirPath and plugin install path to the libraryPath
int argc = 1;
QApplication app(argc, &argv0);
QString appDirPath = QCoreApplication::applicationDirPath();
QString installPathPlugins = QLibraryInfo::path(QLibraryInfo::PluginsPath);
QStringList actual = QApplication::libraryPaths();
actual.sort();
QStringList expected;
expected << installPathPlugins << appDirPath;
expected = QSet<QString>(expected.constBegin(), expected.constEnd()).values();
expected.sort();
#if defined(Q_OS_VXWORKS)
QEXPECT_FAIL("", "QTBUG-130736: Actual paths on VxWorks differ from expected", Abort);
#endif
QVERIFY2(isPathListIncluded(actual, expected),
qPrintable("actual:\n - " + actual.join("\n - ") +
"\nexpected:\n - " + expected.join("\n - ")));
// setting the library paths overrides everything
QApplication::setLibraryPaths(QStringList() << testDir);
QVERIFY2(isPathListIncluded(QApplication::libraryPaths(), (QStringList() << testDir)),
qPrintable("actual:\n - " + QApplication::libraryPaths().join("\n - ") +
"\nexpected:\n - " + testDir));
}
{
qCDebug(lcTests) << "Initial library path:" << QApplication::libraryPaths();
int count = QApplication::libraryPaths().size();
#if 0
// this test doesn't work if KDE 4 is installed
QCOMPARE(count, 1); // before creating QApplication, only the PluginsPath is in the libraryPaths()
#endif
QString installPathPlugins = QLibraryInfo::path(QLibraryInfo::PluginsPath);
QApplication::addLibraryPath(installPathPlugins);
qCDebug(lcTests) << "installPathPlugins" << installPathPlugins;
qCDebug(lcTests) << "After adding plugins path:" << QApplication::libraryPaths();
QCOMPARE(QApplication::libraryPaths().size(), count);
QApplication::addLibraryPath(testDir);
QCOMPARE(QApplication::libraryPaths().size(), count + 1);
// creating QApplication adds the applicationDirPath to the libraryPath
int argc = 1;
QApplication app(argc, &argv0);
QString appDirPath = QCoreApplication::applicationDirPath();
qCDebug(lcTests) << QApplication::libraryPaths();
// On Windows CE these are identical and might also be the case for other
// systems too
if (appDirPath != installPathPlugins)
QCOMPARE(QApplication::libraryPaths().size(), count + 2);
}
{
int argc = 1;
QApplication app(argc, &argv0);
qCDebug(lcTests) << "Initial library path:" << QCoreApplication::libraryPaths();
int count = QCoreApplication::libraryPaths().size();
QString installPathPlugins = QLibraryInfo::path(QLibraryInfo::PluginsPath);
QCoreApplication::addLibraryPath(installPathPlugins);
qCDebug(lcTests) << "installPathPlugins" << installPathPlugins;
qCDebug(lcTests) << "After adding plugins path:" << QCoreApplication::libraryPaths();
QCOMPARE(QCoreApplication::libraryPaths().size(), count);
QString appDirPath = QCoreApplication::applicationDirPath();
QCoreApplication::addLibraryPath(appDirPath);
QCoreApplication::addLibraryPath(appDirPath + "/..");
qCDebug(lcTests) << "appDirPath" << appDirPath;
qCDebug(lcTests) << "After adding appDirPath && appDirPath + /..:" << QCoreApplication::libraryPaths();
QCOMPARE(QCoreApplication::libraryPaths().size(), count + 1);
#ifdef Q_OS_MACOS
QCoreApplication::addLibraryPath(appDirPath + "/../MacOS");
#else
QCoreApplication::addLibraryPath(appDirPath + "/tmp/..");
#endif
qCDebug(lcTests) << "After adding appDirPath + /tmp/..:" << QCoreApplication::libraryPaths();
QCOMPARE(QCoreApplication::libraryPaths().size(), count + 1);
}
}
void tst_QApplication::libraryPaths_qt_plugin_path()
{
int argc = 1;
QApplication app(argc, &argv0);
QString appDirPath = QCoreApplication::applicationDirPath();
// Our hook into libraryPaths() initialization: Set the QT_PLUGIN_PATH environment variable
QString installPathPluginsDeCanon = appDirPath + QString::fromLatin1("/tmp/..");
QByteArray ascii = QFile::encodeName(installPathPluginsDeCanon);
qputenv("QT_PLUGIN_PATH", ascii);
QVERIFY(!QCoreApplication::libraryPaths().contains(appDirPath + QString::fromLatin1("/tmp/..")));
}
void tst_QApplication::libraryPaths_qt_plugin_path_2()
{
#ifdef Q_OS_UNIX
QByteArray validPath = QDir("/tmp").canonicalPath().toLatin1();
QByteArray nonExistentPath = "/nonexistent";
QByteArray pluginPath = validPath + ':' + nonExistentPath;
#elif defined(Q_OS_WIN)
QByteArray validPath = "C:\\windows";
QByteArray nonExistentPath = "Z:\\nonexistent";
QByteArray pluginPath = validPath + ';' + nonExistentPath;
#endif
{
// Our hook into libraryPaths() initialization: Set the QT_PLUGIN_PATH environment variable
qputenv("QT_PLUGIN_PATH", pluginPath);
int argc = 1;
QApplication app(argc, &argv0);
// library path list should contain the default plus the one valid path
QStringList expected =
QStringList()
<< QLibraryInfo::path(QLibraryInfo::PluginsPath)
<< QDir(QCoreApplication::applicationDirPath()).canonicalPath()
<< QDir(QDir::fromNativeSeparators(QString::fromLatin1(validPath))).canonicalPath();
#if defined(Q_OS_VXWORKS)
QEXPECT_FAIL("", "QTBUG-130736: Actual paths on VxWorks differ from expected", Abort);
#endif
QVERIFY2(isPathListIncluded(QCoreApplication::libraryPaths(), expected),
qPrintable("actual:\n - " + QCoreApplication::libraryPaths().join("\n - ") +
"\nexpected:\n - " + expected.join("\n - ")));
}
{
int argc = 1;
QApplication app(argc, &argv0);
// library paths are initialized by the QApplication, setting
// the environment variable here doesn't work
qputenv("QT_PLUGIN_PATH", pluginPath);
// library path list should contain the default
QStringList expected =
QStringList()
<< QLibraryInfo::path(QLibraryInfo::PluginsPath)
<< QCoreApplication::applicationDirPath();
QVERIFY(isPathListIncluded(QCoreApplication::libraryPaths(), expected));
qputenv("QT_PLUGIN_PATH", nullptr);
}
}
#endif
#ifdef QT_BUILD_INTERNAL #ifdef QT_BUILD_INTERNAL
class SendPostedEventsTester : public QObject class SendPostedEventsTester : public QObject
{ {