File indexing completed on 2022-12-06 19:12:10

0001 /* This file is part of the KDE libraries
0002     Copyright (c) 2005-2006 David Faure <faure@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License version 2 as published by the Free Software Foundation.
0007 
0008     This library is distributed in the hope that it will be useful,
0009     but WITHOUT ANY WARRANTY; without even the implied warranty of
0010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011     Library General Public License for more details.
0012 
0013     You should have received a copy of the GNU Library General Public License
0014     along with this library; see the file COPYING.LIB.  If not, write to
0015     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016     Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #undef QT_NO_CAST_FROM_ASCII
0020 
0021 #include "klibloadertest.h"
0022 
0023 #include <QTest>
0024 
0025 QTEST_MAIN(KLibLoaderTest)
0026 
0027 #include <klibloader.h>
0028 #include <QDir>
0029 #include <QDebug>
0030 
0031 // Only new-style plugins are supported, even though we are doing
0032 // loading with a deprecated class.
0033 static const char s_kpluginFactoryModule[] = "klibloadertestmodule5";
0034 #if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN)
0035 static const char s_modExt[] = ".dll";
0036 #else
0037 static const char s_modExt[] = ".so";
0038 #endif
0039 #define MODULE_PATH(mod) QFileInfo(QFINDTESTDATA(QString::fromLatin1(mod) + s_modExt)).canonicalFilePath()
0040 
0041 void KLibLoaderTest::initTestCase()
0042 {
0043     const QString libdir = QDir::currentPath();
0044     qDebug() << "Adding" << libdir << "to LD_LIBRARY_PATH";
0045     qputenv("LD_LIBRARY_PATH", qgetenv("LD_LIBRARY_PATH") + ":" + QFile::encodeName(libdir));
0046 }
0047 
0048 void KLibLoaderTest::testFactory()
0049 {
0050     KPluginFactory *factory = KLibLoader::self()->factory(s_kpluginFactoryModule);
0051     if (!factory) {
0052         QVERIFY(factory);
0053     } else {
0054         QObject *obj = factory->create<QObject>();
0055         QVERIFY(obj);
0056         delete obj;
0057     }
0058 }
0059 
0060 void KLibLoaderTest::testFactory_hints()
0061 {
0062     // the hints will be ignored, but we want to check the call will still compile
0063     KPluginFactory *factory = KLibLoader::self()->factory(s_kpluginFactoryModule,
0064             QLibrary::ResolveAllSymbolsHint);
0065     if (!factory) {
0066         QVERIFY(factory);
0067     } else {
0068         QObject *obj = factory->create<QObject>();
0069         QVERIFY(obj);
0070         delete obj;
0071     }
0072 }
0073 
0074 void KLibLoaderTest::testFactory_noexist()
0075 {
0076     KPluginFactory *factory = KLibLoader::self()->factory("idontexist");
0077     QVERIFY(!factory);
0078 }
0079 
0080 void KLibLoaderTest::testLibrary()
0081 {
0082     KLibrary *lib = KLibLoader::self()->library(s_kpluginFactoryModule);
0083     QVERIFY(lib);
0084     QVERIFY(lib->isLoaded());
0085     QCOMPARE(QFileInfo(lib->fileName()).canonicalFilePath(),
0086              MODULE_PATH(s_kpluginFactoryModule));
0087 }
0088 
0089 void KLibLoaderTest::testLibrary_hints()
0090 {
0091     // the hints will be ignored, but we want to check the call will still compile
0092     KLibrary *lib = KLibLoader::self()->library(s_kpluginFactoryModule,
0093             QLibrary::ResolveAllSymbolsHint);
0094     QVERIFY(lib);
0095     QVERIFY(lib->isLoaded());
0096     QCOMPARE(QFileInfo(lib->fileName()).canonicalFilePath(),
0097              MODULE_PATH(s_kpluginFactoryModule));
0098 }
0099 
0100 void KLibLoaderTest::testLibrary_noexist()
0101 {
0102     KLibrary *lib = KLibLoader::self()->library("idontexist");
0103     QVERIFY(!lib);
0104 }
0105 
0106 void KLibLoaderTest::testFindLibrary()
0107 {
0108     const QString library = KLibLoader::findLibrary(s_kpluginFactoryModule);
0109     QVERIFY(!library.isEmpty());
0110     QCOMPARE(QFileInfo(library).canonicalFilePath(),
0111              MODULE_PATH(s_kpluginFactoryModule));
0112 }
0113 
0114 void KLibLoaderTest::testFindLibrary_noexist()
0115 {
0116     const QString library = KLibLoader::findLibrary("idontexist");
0117     QVERIFY(library.isEmpty());
0118 }