tests/auto/qpluginloader/tst_qpluginloader.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 #include <qdir.h>
       
    45 #include <qpluginloader.h>
       
    46 #include "theplugin/plugininterface.h"
       
    47 
       
    48 // Helper macros to let us know if some suffixes are valid
       
    49 #define bundle_VALID    false
       
    50 #define dylib_VALID     false
       
    51 #define sl_VALID        false
       
    52 #define a_VALID         false
       
    53 #define so_VALID        false
       
    54 #define dll_VALID       false
       
    55 
       
    56 #if defined(Q_OS_DARWIN)
       
    57 # undef bundle_VALID
       
    58 # undef dylib_VALID
       
    59 # undef so_VALID
       
    60 # define bundle_VALID   true
       
    61 # define dylib_VALID    true
       
    62 # define so_VALID       true
       
    63 # define SUFFIX         ".dylib"
       
    64 # define PREFIX         "lib"
       
    65 
       
    66 #elif defined(Q_OS_HPUX) && !defined(__ia64)
       
    67 # undef sl_VALID
       
    68 # define sl_VALID       true
       
    69 # define SUFFIX         ".sl"
       
    70 # define PREFIX         "lib"
       
    71 
       
    72 #elif defined(Q_OS_AIX)
       
    73 # undef a_VALID
       
    74 # undef so_VALID
       
    75 # define a_VALID        true
       
    76 # define so_VALID       true
       
    77 # define SUFFIX         ".so"
       
    78 # define PREFIX         "lib"
       
    79 
       
    80 #elif defined(Q_OS_WIN)
       
    81 # undef dll_VALID
       
    82 # define dll_VALID      true
       
    83 # ifdef QT_NO_DEBUG
       
    84 #  define SUFFIX         ".dll"
       
    85 # else
       
    86 #  define SUFFIX         "d.dll"
       
    87 # endif
       
    88 # define PREFIX         ""
       
    89 
       
    90 #elif defined(Q_OS_SYMBIAN)
       
    91 # undef dll_VALID
       
    92 # define dll_VALID      true
       
    93 # define SUFFIX         ".dll"
       
    94 # define PREFIX         ""
       
    95 
       
    96 #else  // all other Unix
       
    97 # undef so_VALID
       
    98 # define so_VALID       true
       
    99 # define SUFFIX         ".so"
       
   100 # define PREFIX         "lib"
       
   101 #endif
       
   102 
       
   103 static QString sys_qualifiedLibraryName(const QString &fileName)
       
   104 {
       
   105     QString currDir = QDir::currentPath();
       
   106     return currDir + "/bin/" + PREFIX + fileName + SUFFIX;
       
   107 }
       
   108 
       
   109 //TESTED_CLASS=
       
   110 //TESTED_FILES=
       
   111 
       
   112 QT_FORWARD_DECLARE_CLASS(QPluginLoader)
       
   113 class tst_QPluginLoader : public QObject
       
   114 {
       
   115     Q_OBJECT
       
   116 
       
   117 public:
       
   118     tst_QPluginLoader();
       
   119     virtual ~tst_QPluginLoader();
       
   120 
       
   121 private slots:
       
   122     void errorString();
       
   123     void loadHints();
       
   124     void deleteinstanceOnUnload();
       
   125     void checkingStubsFromDifferentDrives();
       
   126 };
       
   127 
       
   128 tst_QPluginLoader::tst_QPluginLoader()
       
   129 
       
   130 {
       
   131 }
       
   132 
       
   133 tst_QPluginLoader::~tst_QPluginLoader()
       
   134 {
       
   135 }
       
   136 
       
   137 //#define SHOW_ERRORS 1
       
   138 
       
   139 void tst_QPluginLoader::errorString()
       
   140 {
       
   141 #if defined(Q_OS_WINCE)
       
   142     // On WinCE we need an QCoreApplication object for current dir
       
   143     int argc = 0;
       
   144     QCoreApplication app(argc,0);
       
   145 #endif
       
   146     const QString unknown(QLatin1String("Unknown error"));
       
   147 
       
   148     {
       
   149     QPluginLoader loader; // default constructed
       
   150     bool loaded = loader.load();
       
   151 #ifdef SHOW_ERRORS
       
   152     qDebug() << loader.errorString();
       
   153 #endif
       
   154     QCOMPARE(loaded, false);
       
   155     QCOMPARE(loader.errorString(), unknown);
       
   156 
       
   157     QObject *obj = loader.instance();
       
   158 #ifdef SHOW_ERRORS
       
   159     qDebug() << loader.errorString();
       
   160 #endif
       
   161     QCOMPARE(obj, static_cast<QObject*>(0));
       
   162     QCOMPARE(loader.errorString(), unknown);
       
   163 
       
   164     bool unloaded = loader.unload();
       
   165 #ifdef SHOW_ERRORS
       
   166     qDebug() << loader.errorString();
       
   167 #endif
       
   168     QCOMPARE(unloaded, false);
       
   169     QCOMPARE(loader.errorString(), unknown);
       
   170     }
       
   171     {
       
   172     QPluginLoader loader( sys_qualifiedLibraryName("mylib"));     //not a plugin
       
   173     bool loaded = loader.load();
       
   174 #ifdef SHOW_ERRORS
       
   175     qDebug() << loader.errorString();
       
   176 #endif
       
   177     QCOMPARE(loaded, false);
       
   178     QVERIFY(loader.errorString() != unknown);
       
   179 
       
   180     QObject *obj = loader.instance();
       
   181 #ifdef SHOW_ERRORS
       
   182     qDebug() << loader.errorString();
       
   183 #endif
       
   184     QCOMPARE(obj, static_cast<QObject*>(0));
       
   185     QVERIFY(loader.errorString() != unknown);
       
   186 
       
   187     bool unloaded = loader.unload();
       
   188 #ifdef SHOW_ERRORS
       
   189     qDebug() << loader.errorString();
       
   190 #endif
       
   191     QCOMPARE(unloaded, false);
       
   192     QVERIFY(loader.errorString() != unknown);
       
   193     }
       
   194 
       
   195     {
       
   196     QPluginLoader loader( sys_qualifiedLibraryName("nosuchfile"));     //not a file
       
   197     bool loaded = loader.load();
       
   198 #ifdef SHOW_ERRORS
       
   199     qDebug() << loader.errorString();
       
   200 #endif
       
   201     QCOMPARE(loaded, false);
       
   202     QVERIFY(loader.errorString() != unknown);
       
   203 
       
   204     QObject *obj = loader.instance();
       
   205 #ifdef SHOW_ERRORS
       
   206     qDebug() << loader.errorString();
       
   207 #endif
       
   208     QCOMPARE(obj, static_cast<QObject*>(0));
       
   209     QVERIFY(loader.errorString() != unknown);
       
   210 
       
   211     bool unloaded = loader.unload();
       
   212 #ifdef SHOW_ERRORS
       
   213     qDebug() << loader.errorString();
       
   214 #endif
       
   215     QCOMPARE(unloaded, false);
       
   216     QVERIFY(loader.errorString() != unknown);
       
   217     }
       
   218 
       
   219 #if !defined Q_OS_WIN && !defined Q_OS_MAC && !defined Q_OS_HPUX && !defined Q_OS_SYMBIAN
       
   220     {
       
   221     QPluginLoader loader( sys_qualifiedLibraryName("almostplugin"));     //a plugin with unresolved symbols
       
   222     loader.setLoadHints(QLibrary::ResolveAllSymbolsHint);
       
   223     QCOMPARE(loader.load(), false);
       
   224 #ifdef SHOW_ERRORS
       
   225     qDebug() << loader.errorString();
       
   226 #endif
       
   227     QVERIFY(loader.errorString() != unknown);
       
   228 
       
   229     QCOMPARE(loader.instance(), static_cast<QObject*>(0));
       
   230 #ifdef SHOW_ERRORS
       
   231     qDebug() << loader.errorString();
       
   232 #endif
       
   233     QVERIFY(loader.errorString() != unknown);
       
   234 
       
   235     QCOMPARE(loader.unload(), false);
       
   236 #ifdef SHOW_ERRORS
       
   237     qDebug() << loader.errorString();
       
   238 #endif
       
   239     QVERIFY(loader.errorString() != unknown);
       
   240     }
       
   241 #endif
       
   242 
       
   243     {
       
   244     QPluginLoader loader( sys_qualifiedLibraryName("theplugin"));     //a plugin
       
   245     QCOMPARE(loader.load(), true);
       
   246     QCOMPARE(loader.errorString(), unknown);
       
   247 
       
   248     QVERIFY(loader.instance() !=  static_cast<QObject*>(0));
       
   249     QCOMPARE(loader.errorString(), unknown);
       
   250 
       
   251     // Make sure that plugin really works
       
   252     PluginInterface* theplugin = qobject_cast<PluginInterface*>(loader.instance());
       
   253     QString pluginName = theplugin->pluginName();
       
   254     QCOMPARE(pluginName, QLatin1String("Plugin ok"));
       
   255 
       
   256     QCOMPARE(loader.unload(), true);
       
   257     QCOMPARE(loader.errorString(), unknown);
       
   258     }
       
   259 }
       
   260 
       
   261 void tst_QPluginLoader::loadHints()
       
   262 {
       
   263     QPluginLoader loader;
       
   264     QCOMPARE(loader.loadHints(), (QLibrary::LoadHints)0);   //Do not crash
       
   265     loader.setLoadHints(QLibrary::ResolveAllSymbolsHint);
       
   266     loader.setFileName( sys_qualifiedLibraryName("theplugin"));     //a plugin
       
   267     QCOMPARE(loader.loadHints(), QLibrary::ResolveAllSymbolsHint);
       
   268 }
       
   269 
       
   270 void tst_QPluginLoader::deleteinstanceOnUnload()
       
   271 {
       
   272     for (int pass = 0; pass < 2; ++pass) {
       
   273         QPluginLoader loader1;
       
   274         loader1.setFileName( sys_qualifiedLibraryName("theplugin"));     //a plugin
       
   275         if (pass == 0)
       
   276             loader1.load(); // not recommended, instance() should do the job.
       
   277         PluginInterface *instance1 = qobject_cast<PluginInterface*>(loader1.instance());
       
   278         QVERIFY(instance1);
       
   279         QCOMPARE(instance1->pluginName(), QLatin1String("Plugin ok"));
       
   280 
       
   281         QPluginLoader loader2;
       
   282         loader2.setFileName( sys_qualifiedLibraryName("theplugin"));     //a plugin
       
   283         if (pass == 0)
       
   284             loader2.load(); // not recommended, instance() should do the job.
       
   285         PluginInterface *instance2 = qobject_cast<PluginInterface*>(loader2.instance());
       
   286         QCOMPARE(instance2->pluginName(), QLatin1String("Plugin ok"));
       
   287 
       
   288         QSignalSpy spy1(loader1.instance(), SIGNAL(destroyed()));
       
   289         QSignalSpy spy2(loader2.instance(), SIGNAL(destroyed()));
       
   290         QCOMPARE(loader1.unload(), false);  // refcount not reached 0, not really unloaded
       
   291         QCOMPARE(spy1.count(), 0);
       
   292         QCOMPARE(spy2.count(), 0);
       
   293         QCOMPARE(instance1->pluginName(), QLatin1String("Plugin ok"));
       
   294         QCOMPARE(instance2->pluginName(), QLatin1String("Plugin ok"));
       
   295         QCOMPARE(loader2.unload(), true);   // refcount reached 0, did really unload
       
   296         QCOMPARE(spy1.count(), 1);
       
   297         QCOMPARE(spy2.count(), 1);
       
   298     }
       
   299 }
       
   300 
       
   301 void tst_QPluginLoader::checkingStubsFromDifferentDrives()
       
   302 {
       
   303 #if defined(Q_OS_SYMBIAN)
       
   304 
       
   305     // This test needs C-drive + some additional drive (driveForStubs)
       
   306 
       
   307     const QString driveForStubs("E:/");// != "C:/"
       
   308     const QString stubDir("system/temp/stubtest/");
       
   309     const QString stubName("dummyStub.qtplugin");
       
   310     const QString fullStubFileName(stubDir + stubName);
       
   311     QDir dir(driveForStubs);
       
   312     bool test1(false); bool test2(false);
       
   313 
       
   314     // initial clean up
       
   315     QFile::remove(driveForStubs + fullStubFileName);
       
   316     dir.rmdir(driveForStubs + stubDir);
       
   317 
       
   318     // create a stub dir and do stub drive check
       
   319     if (!dir.mkpath(stubDir))
       
   320         QSKIP("Required drive not available for this test", SkipSingle);
       
   321 
       
   322     {// test without stub, should not be found
       
   323     QPluginLoader loader("C:/" + fullStubFileName);
       
   324     test1 = !loader.fileName().length();
       
   325     }
       
   326 
       
   327     // create a stub to defined drive
       
   328     QFile tempFile(driveForStubs + fullStubFileName);
       
   329     tempFile.open(QIODevice::ReadWrite);
       
   330     QFileInfo fileInfo(tempFile);
       
   331 
       
   332     {// now should be found even tried to find from C:
       
   333     QPluginLoader loader("C:/" + fullStubFileName);
       
   334     test2 = (loader.fileName() == fileInfo.absoluteFilePath());
       
   335     }
       
   336 
       
   337     // clean up
       
   338     tempFile.close();
       
   339     if (!QFile::remove(driveForStubs + fullStubFileName))
       
   340         QWARN("Could not remove stub file");
       
   341     if (!dir.rmdir(driveForStubs + stubDir))
       
   342         QWARN("Could not remove stub directory");
       
   343 
       
   344     // test after cleanup
       
   345     QVERIFY(test1);
       
   346     QVERIFY(test2);
       
   347 
       
   348 #endif//Q_OS_SYMBIAN
       
   349 }
       
   350 
       
   351 QTEST_APPLESS_MAIN(tst_QPluginLoader)
       
   352 #include "tst_qpluginloader.moc"