1 /* This file is part of the KDE libraries
2     Copyright (c) 2005-2006 David Faure <faure@kde.org>
3 
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License version 2 as published by the Free Software Foundation.
7 
8     This library is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11     Library General Public License for more details.
12 
13     You should have received a copy of the GNU Library General Public License
14     along with this library; see the file COPYING.LIB.  If not, write to
15     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16     Boston, MA 02110-1301, USA.
17 */
18 
19 #undef QT_NO_CAST_FROM_ASCII
20 
21 #include "klibloadertest.h"
22 
23 #include <QTest>
24 
25 QTEST_MAIN(KLibLoaderTest)
26 
27 #include <klibloader.h>
28 #include <QDir>
29 #include <QDebug>
30 
31 // Only new-style plugins are supported, even though we are doing
32 // loading with a deprecated class.
33 static const char s_kpluginFactoryModule[] = "klibloadertestmodule5";
34 #if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN)
35 static const char s_modExt[] = ".dll";
36 #else
37 static const char s_modExt[] = ".so";
38 #endif
39 #define MODULE_PATH(mod) QFileInfo(QFINDTESTDATA(QString::fromLatin1(mod) + s_modExt)).canonicalFilePath()
40 
initTestCase()41 void KLibLoaderTest::initTestCase()
42 {
43     const QString libdir = QDir::currentPath();
44     qDebug() << "Adding" << libdir << "to LD_LIBRARY_PATH";
45     qputenv("LD_LIBRARY_PATH", qgetenv("LD_LIBRARY_PATH") + ":" + QFile::encodeName(libdir));
46 }
47 
testFactory()48 void KLibLoaderTest::testFactory()
49 {
50     KPluginFactory *factory = KLibLoader::self()->factory(s_kpluginFactoryModule);
51     if (!factory) {
52         QVERIFY(factory);
53     } else {
54         QObject *obj = factory->create<QObject>();
55         QVERIFY(obj);
56         delete obj;
57     }
58 }
59 
testFactory_hints()60 void KLibLoaderTest::testFactory_hints()
61 {
62     // the hints will be ignored, but we want to check the call will still compile
63     KPluginFactory *factory = KLibLoader::self()->factory(s_kpluginFactoryModule,
64             QLibrary::ResolveAllSymbolsHint);
65     if (!factory) {
66         QVERIFY(factory);
67     } else {
68         QObject *obj = factory->create<QObject>();
69         QVERIFY(obj);
70         delete obj;
71     }
72 }
73 
testFactory_noexist()74 void KLibLoaderTest::testFactory_noexist()
75 {
76     KPluginFactory *factory = KLibLoader::self()->factory("idontexist");
77     QVERIFY(!factory);
78 }
79 
testLibrary()80 void KLibLoaderTest::testLibrary()
81 {
82     KLibrary *lib = KLibLoader::self()->library(s_kpluginFactoryModule);
83     QVERIFY(lib);
84     QVERIFY(lib->isLoaded());
85     QCOMPARE(QFileInfo(lib->fileName()).canonicalFilePath(),
86              MODULE_PATH(s_kpluginFactoryModule));
87 }
88 
testLibrary_hints()89 void KLibLoaderTest::testLibrary_hints()
90 {
91     // the hints will be ignored, but we want to check the call will still compile
92     KLibrary *lib = KLibLoader::self()->library(s_kpluginFactoryModule,
93             QLibrary::ResolveAllSymbolsHint);
94     QVERIFY(lib);
95     QVERIFY(lib->isLoaded());
96     QCOMPARE(QFileInfo(lib->fileName()).canonicalFilePath(),
97              MODULE_PATH(s_kpluginFactoryModule));
98 }
99 
testLibrary_noexist()100 void KLibLoaderTest::testLibrary_noexist()
101 {
102     KLibrary *lib = KLibLoader::self()->library("idontexist");
103     QVERIFY(!lib);
104 }
105 
testFindLibrary()106 void KLibLoaderTest::testFindLibrary()
107 {
108     const QString library = KLibLoader::findLibrary(s_kpluginFactoryModule);
109     QVERIFY(!library.isEmpty());
110     QCOMPARE(QFileInfo(library).canonicalFilePath(),
111              MODULE_PATH(s_kpluginFactoryModule));
112 }
113 
testFindLibrary_noexist()114 void KLibLoaderTest::testFindLibrary_noexist()
115 {
116     const QString library = KLibLoader::findLibrary("idontexist");
117     QVERIFY(library.isEmpty());
118 }
119