qtecomplugins/xqplugins/tests/auto/xqpluginloader/tst_xqpluginloader.cpp
branchRCL_3
changeset 10 cd2778e5acfe
parent 9 5d007b20cfd0
child 11 19a54be74e5e
equal deleted inserted replaced
9:5d007b20cfd0 10:cd2778e5acfe
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 * 
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not, 
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 */ 
       
    18 
       
    19 #include <QtTest/QtTest>
       
    20 #include <qdir.h>
       
    21 #include <xqpluginloader.h>
       
    22 #include <xqplugininfo.h>
       
    23 #include "theplugin/plugininterface.h"
       
    24 
       
    25 #include <e32cmn.h>
       
    26 // Helper macros to let us know if some suffixes are valid
       
    27 #define bundle_VALID    false
       
    28 #define dylib_VALID     false
       
    29 #define sl_VALID        false
       
    30 #define a_VALID         false
       
    31 #define so_VALID        false
       
    32 #define dll_VALID       false
       
    33 
       
    34 #if defined(Q_OS_DARWIN)
       
    35 # undef bundle_VALID
       
    36 # undef dylib_VALID
       
    37 # undef so_VALID
       
    38 # define bundle_VALID   true
       
    39 # define dylib_VALID    true
       
    40 # define so_VALID       true
       
    41 # define SUFFIX         ".dylib"
       
    42 # define PREFIX         "lib"
       
    43 
       
    44 #elif defined(Q_OS_HPUX) && !defined(__ia64)
       
    45 # undef sl_VALID
       
    46 # define sl_VALID       true
       
    47 # define SUFFIX         ".sl"
       
    48 # define PREFIX         "lib"
       
    49 
       
    50 #elif defined(Q_OS_AIX)
       
    51 # undef a_VALID
       
    52 # undef so_VALID
       
    53 # define a_VALID        true
       
    54 # define so_VALID       true
       
    55 # define SUFFIX         ".so"
       
    56 # define PREFIX         "lib"
       
    57 
       
    58 #elif defined(Q_OS_WIN)
       
    59 # undef dll_VALID
       
    60 # define dll_VALID      true
       
    61 # ifdef QT_NO_DEBUG
       
    62 #  define SUFFIX         ".dll"
       
    63 # else
       
    64 #  define SUFFIX         "d.dll"
       
    65 # endif
       
    66 # define PREFIX         ""
       
    67 
       
    68 #elif defined(Q_OS_SYMBIAN)
       
    69 # undef dll_VALID
       
    70 # define dll_VALID      true
       
    71 # define SUFFIX         ".dll"
       
    72 # define PREFIX         ""
       
    73 
       
    74 #else  // all other Unix
       
    75 # undef so_VALID
       
    76 # define so_VALID       true
       
    77 # define SUFFIX         ".so"
       
    78 # define PREFIX         "lib"
       
    79 #endif
       
    80 
       
    81 static QString sys_qualifiedLibraryName(const QString &fileName)
       
    82 {
       
    83     QString currDir = QDir::currentPath();
       
    84     return currDir + "/bin/" + PREFIX + fileName + SUFFIX;
       
    85 }
       
    86 
       
    87 //TESTED_CLASS=
       
    88 //TESTED_FILES=
       
    89 
       
    90 QT_FORWARD_DECLARE_CLASS(QPluginLoader)
       
    91 class tst_XQPluginLoader : public QObject
       
    92 {
       
    93     Q_OBJECT
       
    94 
       
    95 public:
       
    96     tst_XQPluginLoader();
       
    97     virtual ~tst_XQPluginLoader();
       
    98 
       
    99 private slots:
       
   100     void errorString();
       
   101     void deleteinstanceOnUnload();
       
   102     void listThePlugins();
       
   103 
       
   104 };
       
   105 
       
   106 tst_XQPluginLoader::tst_XQPluginLoader()
       
   107 
       
   108 {
       
   109 }
       
   110 
       
   111 tst_XQPluginLoader::~tst_XQPluginLoader()
       
   112 {
       
   113 }
       
   114 
       
   115 //#define SHOW_ERRORS 1
       
   116 
       
   117 void tst_XQPluginLoader::errorString()
       
   118 {
       
   119 #if defined(Q_OS_WINCE)
       
   120     // On WinCE we need an QCoreApplication object for current dir
       
   121     int argc = 0;
       
   122     QCoreApplication app(argc,0);
       
   123 #endif
       
   124     const QString unknown(QLatin1String("Unknown error"));
       
   125 
       
   126     {
       
   127     XQPluginLoader loader; // default constructed
       
   128     bool loaded = loader.load();
       
   129 #ifdef SHOW_ERRORS
       
   130     qDebug() << loader.errorString();
       
   131 #endif
       
   132     QCOMPARE(loaded, false);
       
   133     QCOMPARE(loader.errorString(), unknown);
       
   134 
       
   135     QObject *obj = loader.instance();
       
   136 #ifdef SHOW_ERRORS
       
   137     qDebug() << loader.errorString();
       
   138 #endif
       
   139     QCOMPARE(obj, static_cast<QObject*>(0));
       
   140     QCOMPARE(loader.errorString(), unknown);
       
   141 
       
   142     bool unloaded = loader.unload();
       
   143 #ifdef SHOW_ERRORS
       
   144     qDebug() << loader.errorString();
       
   145 #endif
       
   146     QCOMPARE(unloaded, false);
       
   147     QCOMPARE(loader.errorString(), unknown);
       
   148     }
       
   149     {
       
   150     XQPluginLoader loader( 0xE6E9EB83 );     //not a plugin sys_qualifiedLibraryName("mylib")
       
   151     bool loaded = loader.load();
       
   152 #ifdef SHOW_ERRORS
       
   153     qDebug() << loader.errorString();
       
   154 #endif
       
   155     QCOMPARE(loaded, false);
       
   156     QVERIFY(loader.errorString() != unknown);
       
   157 
       
   158     QObject *obj = loader.instance();
       
   159 #ifdef SHOW_ERRORS
       
   160     qDebug() << loader.errorString();
       
   161 #endif
       
   162     QCOMPARE(obj, static_cast<QObject*>(0));
       
   163     QVERIFY(loader.errorString() != unknown);
       
   164 
       
   165     bool unloaded = loader.unload();
       
   166 #ifdef SHOW_ERRORS
       
   167     qDebug() << loader.errorString();
       
   168 #endif
       
   169     QCOMPARE(unloaded, false);
       
   170     QVERIFY(loader.errorString() != unknown);
       
   171     }
       
   172 
       
   173     {
       
   174     XQPluginLoader loader( KNullUid.iUid );     //not a file sys_qualifiedLibraryName("nosuchfile")
       
   175     bool loaded = loader.load();
       
   176 #ifdef SHOW_ERRORS
       
   177     qDebug() << loader.errorString();
       
   178 #endif
       
   179     QCOMPARE(loaded, false);
       
   180     QVERIFY(loader.errorString() != unknown);
       
   181 
       
   182     QObject *obj = loader.instance();
       
   183 #ifdef SHOW_ERRORS
       
   184     qDebug() << loader.errorString();
       
   185 #endif
       
   186     QCOMPARE(obj, static_cast<QObject*>(0));
       
   187     QVERIFY(loader.errorString() != unknown);
       
   188 
       
   189     bool unloaded = loader.unload();
       
   190 #ifdef SHOW_ERRORS
       
   191     qDebug() << loader.errorString();
       
   192 #endif
       
   193     QCOMPARE(unloaded, false);
       
   194     QVERIFY(loader.errorString() != unknown);
       
   195     }
       
   196 
       
   197 #if !defined Q_OS_WIN && !defined Q_OS_MAC && !defined Q_OS_HPUX && !defined Q_OS_SYMBIAN
       
   198     {
       
   199     XQPluginLoader loader(  );     //a plugin with unresolved symbols sys_qualifiedLibraryName("almostplugin")
       
   200     QCOMPARE(loader.load(), false);
       
   201 #ifdef SHOW_ERRORS
       
   202     qDebug() << loader.errorString();
       
   203 #endif
       
   204     QVERIFY(loader.errorString() != unknown);
       
   205 
       
   206     QCOMPARE(loader.instance(), static_cast<QObject*>(0));
       
   207 #ifdef SHOW_ERRORS
       
   208     qDebug() << loader.errorString();
       
   209 #endif
       
   210     QVERIFY(loader.errorString() != unknown);
       
   211 
       
   212     QCOMPARE(loader.unload(), false);
       
   213 #ifdef SHOW_ERRORS
       
   214     qDebug() << loader.errorString();
       
   215 #endif
       
   216     QVERIFY(loader.errorString() != unknown);
       
   217     }
       
   218 #endif
       
   219 
       
   220     {
       
   221     XQPluginLoader loader( 0xE6E9EB84 );     //a plugin sys_qualifiedLibraryName("theplugin")
       
   222     QCOMPARE(loader.load(), true);
       
   223     QCOMPARE(loader.errorString(), unknown);
       
   224 
       
   225     QVERIFY(loader.instance() !=  static_cast<QObject*>(0));
       
   226     QCOMPARE(loader.errorString(), unknown);
       
   227 
       
   228     // Make sure that plugin really works
       
   229     PluginInterface* theplugin = qobject_cast<PluginInterface*>(loader.instance());
       
   230     QString pluginName = theplugin->pluginName();
       
   231     QCOMPARE(pluginName, QLatin1String("Plugin ok"));
       
   232 
       
   233     QCOMPARE(loader.unload(), true);
       
   234     QCOMPARE(loader.errorString(), unknown);
       
   235     }
       
   236 }
       
   237 
       
   238 void tst_XQPluginLoader::deleteinstanceOnUnload()
       
   239 {
       
   240     for (int pass = 0; pass < 2; ++pass) {
       
   241         XQPluginLoader loader1;
       
   242         loader1.setUid( 0xE6E9EB84 ); // sys_qualifiedLibraryName("theplugin")
       
   243         if (pass == 0)
       
   244             loader1.load(); // not recommended, instance() should do the job.
       
   245         PluginInterface *instance1 = qobject_cast<PluginInterface*>(loader1.instance());
       
   246         QCOMPARE(instance1->pluginName(), QLatin1String("Plugin ok"));
       
   247 
       
   248         XQPluginLoader loader2;
       
   249         loader2.setUid( 0xE6E9EB84 ); // sys_qualifiedLibraryName("theplugin")
       
   250         if (pass == 0)
       
   251             loader2.load(); // not recommended, instance() should do the job.
       
   252         PluginInterface *instance2 = qobject_cast<PluginInterface*>(loader2.instance());
       
   253         QCOMPARE(instance2->pluginName(), QLatin1String("Plugin ok"));
       
   254 
       
   255         QSignalSpy spy1(loader1.instance(), SIGNAL(destroyed()));
       
   256         QSignalSpy spy2(loader2.instance(), SIGNAL(destroyed()));
       
   257         QCOMPARE(loader1.unload(), false);  // refcount not reached 0, not really unloaded
       
   258         QCOMPARE(spy1.count(), 0);
       
   259         QCOMPARE(spy2.count(), 0);
       
   260         QCOMPARE(instance1->pluginName(), QLatin1String("Plugin ok"));
       
   261         QCOMPARE(instance2->pluginName(), QLatin1String("Plugin ok"));
       
   262         QCOMPARE(loader2.unload(), true);   // refcount reached 0, did really unload
       
   263         QCOMPARE(spy1.count(), 1);
       
   264         QCOMPARE(spy2.count(), 1);
       
   265     }
       
   266 }
       
   267 void tst_XQPluginLoader::listThePlugins()
       
   268 {
       
   269     QList<XQPluginInfo> plugins;
       
   270     XQPluginLoader loader;
       
   271     loader.listImplementations( tr( "xtheplugin.dll" ), plugins );
       
   272     QVERIFY( plugins.count() !=  0 );
       
   273     for( int i( 0 ); i < plugins.count(); ++i ) {
       
   274         
       
   275         loader.setUid( plugins[i ].uid() );
       
   276         QCOMPARE( loader.load(), true );
       
   277         PluginInterface *instance = qobject_cast<PluginInterface*>(loader.instance());
       
   278         QCOMPARE(instance->pluginName(), QLatin1String("Plugin ok"));
       
   279         QCOMPARE(loader.unload(), true);
       
   280         
       
   281     }
       
   282     plugins.clear();
       
   283 }
       
   284 
       
   285 QTEST_APPLESS_MAIN(tst_XQPluginLoader)
       
   286 #include "tst_xqpluginloader.moc"
       
   287