tests/auto/qresourceengine/tst_qresourceengine.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 <QtCore>
       
    45 
       
    46 #ifdef Q_OS_SYMBIAN
       
    47 #define SRCDIR "."
       
    48 #endif
       
    49 
       
    50 class tst_QResourceEngine: public QObject
       
    51 {
       
    52     Q_OBJECT
       
    53 
       
    54 private slots:
       
    55     void initTestCase();
       
    56     void cleanupTestCase();
       
    57 
       
    58     void checkUnregisterResource_data();
       
    59     void checkUnregisterResource();
       
    60     void checkStructure_data();
       
    61     void checkStructure();
       
    62     void searchPath_data();
       
    63     void searchPath();
       
    64     void doubleSlashInRoot();
       
    65 
       
    66 private:
       
    67     QString builddir;
       
    68 };
       
    69 
       
    70 Q_DECLARE_METATYPE(QLocale)
       
    71 Q_DECLARE_METATYPE(qlonglong)
       
    72 
       
    73 void tst_QResourceEngine::initTestCase()
       
    74 {
       
    75     QVERIFY(QResource::registerResource("runtime_resource.rcc"));
       
    76     QVERIFY(QResource::registerResource("runtime_resource.rcc", "/secondary_root/"));
       
    77     QString srcdir(QLatin1String(SRCDIR));
       
    78     if (!srcdir.isEmpty()) {
       
    79         builddir = QDir::current().absolutePath();
       
    80         if (!builddir.endsWith(QDir::separator()))
       
    81             builddir.append(QDir::separator());
       
    82         QDir::setCurrent(srcdir);
       
    83     }
       
    84 }
       
    85 
       
    86 void tst_QResourceEngine::cleanupTestCase()
       
    87 {
       
    88     if (!builddir.isEmpty()) {
       
    89         QDir::setCurrent(builddir);
       
    90     }
       
    91 
       
    92     // make sure we don't leak memory
       
    93     QVERIFY(QResource::unregisterResource("runtime_resource.rcc"));
       
    94     QVERIFY(QResource::unregisterResource("runtime_resource.rcc", "/secondary_root/"));
       
    95 }
       
    96 
       
    97 void tst_QResourceEngine::checkStructure_data()
       
    98 {
       
    99     QTest::addColumn<QString>("pathName");
       
   100     QTest::addColumn<QString>("contents");
       
   101     QTest::addColumn<QStringList>("containedFiles");
       
   102     QTest::addColumn<QStringList>("containedDirs");
       
   103     QTest::addColumn<QLocale>("locale");
       
   104     QTest::addColumn<qlonglong>("contentsSize");
       
   105 
       
   106     QFileInfo info;
       
   107 
       
   108     QTest::newRow("root dir")          << QString(":/")
       
   109                                        << QString()
       
   110                                        << (QStringList() << "search_file.txt")
       
   111                                        << (QStringList() << QLatin1String("aliasdir") << QLatin1String("otherdir")
       
   112                                            << QLatin1String("runtime_resource")
       
   113                                            << QLatin1String("searchpath1") << QLatin1String("searchpath2")
       
   114                                            << QLatin1String("secondary_root")
       
   115                                            << QLatin1String("test")
       
   116                                            << QLatin1String("withoutslashes"))
       
   117                                        << QLocale::c()
       
   118                                        << qlonglong(0);
       
   119 
       
   120     QTest::newRow("secondary root")  << QString(":/secondary_root/")
       
   121                                      << QString()
       
   122                                      << QStringList()
       
   123                                      << (QStringList() << QLatin1String("runtime_resource"))
       
   124                                      << QLocale::c()
       
   125                                      << qlonglong(0);
       
   126 
       
   127     QStringList roots;
       
   128     roots << QString(":/") << QString(":/runtime_resource/") << QString(":/secondary_root/runtime_resource/");
       
   129     for(int i = 0; i < roots.size(); ++i) {
       
   130         const QString root = roots.at(i);
       
   131 
       
   132         QTest::newRow(QString(root + "prefix dir").toLatin1().constData())  << QString(root + "test/abc/123/+++")
       
   133                                             << QString()
       
   134                                             << (QStringList() << QLatin1String("currentdir.txt") << QLatin1String("currentdir2.txt") << QLatin1String("parentdir.txt"))
       
   135                                             << (QStringList() << QLatin1String("subdir"))
       
   136                                             << QLocale::c()
       
   137                                             << qlonglong(0);
       
   138 
       
   139         QTest::newRow(QString(root + "parent to prefix").toLatin1().constData())  << QString(root + "test/abc/123")
       
   140                                                   << QString()
       
   141                                                   << QStringList()
       
   142                                                   << (QStringList() << QLatin1String("+++"))
       
   143                                                   << QLocale::c()
       
   144                                                   << qlonglong(0);
       
   145 
       
   146         QTest::newRow(QString(root + "two parents prefix").toLatin1().constData()) << QString(root + "test/abc")
       
   147                                                    << QString()
       
   148                                                    << QStringList()
       
   149                                                    << QStringList(QLatin1String("123"))
       
   150                                                    << QLocale::c()
       
   151                                                    << qlonglong(0);
       
   152 
       
   153         QTest::newRow(QString(root + "test dir ").toLatin1().constData())          << QString(root + "test")
       
   154                                                    << QString()
       
   155                                                    << (QStringList() << QLatin1String("testdir.txt"))
       
   156                                                    << (QStringList() << QLatin1String("abc") << QLatin1String("test"))
       
   157                                                    << QLocale::c()
       
   158                                                    << qlonglong(0);
       
   159 
       
   160         QTest::newRow(QString(root + "prefix no slashes").toLatin1().constData()) << QString(root + "withoutslashes")
       
   161                                                   << QString()
       
   162                                                   << QStringList("blahblah.txt")
       
   163                                                   << QStringList()
       
   164                                                   << QLocale::c()
       
   165                                                   << qlonglong(0);
       
   166 
       
   167         QTest::newRow(QString(root + "other dir").toLatin1().constData())         << QString(root + "otherdir")
       
   168                                                   << QString()
       
   169                                                   << QStringList(QLatin1String("otherdir.txt"))
       
   170                                                   << QStringList()
       
   171                                                   << QLocale::c()
       
   172                                                   << qlonglong(0);
       
   173 
       
   174         QTest::newRow(QString(root + "alias dir").toLatin1().constData())         << QString(root + "aliasdir")
       
   175                                                   << QString()
       
   176                                                   << QStringList(QLatin1String("aliasdir.txt"))
       
   177                                                   << QStringList()
       
   178                                                   << QLocale::c()
       
   179                                                   << qlonglong(0);
       
   180 
       
   181         QTest::newRow(QString(root + "second test dir").toLatin1().constData())   << QString(root + "test/test")
       
   182                                                   << QString()
       
   183                                                   << (QStringList() << QLatin1String("test1.txt") << QLatin1String("test2.txt"))
       
   184                                                   << QStringList()
       
   185                                                   << QLocale::c()
       
   186                                                   << qlonglong(0);
       
   187 
       
   188         info = QFileInfo("testqrc/test/test/test1.txt");
       
   189         QTest::newRow(QString(root + "test1 text").toLatin1().constData())        << QString(root + "test/test/test1.txt")
       
   190                                                   << QString("abc")
       
   191                                                   << QStringList()
       
   192                                                   << QStringList()
       
   193                                                   << QLocale::c()
       
   194                                                   << qlonglong(info.size());
       
   195 
       
   196         info = QFileInfo("testqrc/blahblah.txt");
       
   197         QTest::newRow(QString(root + "text no slashes").toLatin1().constData())   << QString(root + "withoutslashes/blahblah.txt")
       
   198                                                   << QString("qwerty")
       
   199                                                   << QStringList()
       
   200                                                   << QStringList()
       
   201                                                   << QLocale::c()
       
   202                                                   << qlonglong(info.size());
       
   203 
       
   204 
       
   205         info = QFileInfo("testqrc/test/test/test2.txt");
       
   206         QTest::newRow(QString(root + "test1 text").toLatin1().constData())        << QString(root + "test/test/test2.txt")
       
   207                                                   << QString("def")
       
   208                                                   << QStringList()
       
   209                                                   << QStringList()
       
   210                                                   << QLocale::c()
       
   211                                                   << qlonglong(info.size());
       
   212 
       
   213         info = QFileInfo("testqrc/currentdir.txt");
       
   214         QTest::newRow(QString(root + "currentdir text").toLatin1().constData())   << QString(root + "test/abc/123/+++/currentdir.txt")
       
   215                                                   << QString("\"This is the current dir\" ")
       
   216                                                   << QStringList()
       
   217                                                   << QStringList()
       
   218                                                   << QLocale::c()
       
   219                                                   << qlonglong(info.size());
       
   220 
       
   221         info = QFileInfo("testqrc/currentdir2.txt");
       
   222         QTest::newRow(QString(root + "currentdir text2").toLatin1().constData())  << QString(root + "test/abc/123/+++/currentdir2.txt")
       
   223                                                   << QString("\"This is also the current dir\" ")
       
   224                                                   << QStringList()
       
   225                                                   << QStringList()
       
   226                                                   << QLocale::c()
       
   227                                                   << qlonglong(info.size());
       
   228 
       
   229         info = QFileInfo("parentdir.txt");
       
   230         QTest::newRow(QString(root + "parentdir text").toLatin1().constData())    << QString(root + "test/abc/123/+++/parentdir.txt")
       
   231                                                   << QString("abcdefgihklmnopqrstuvwxyz ")
       
   232                                                   << QStringList()
       
   233                                                   << QStringList()
       
   234                                                   << QLocale::c()
       
   235                                                   << qlonglong(info.size());
       
   236 
       
   237         info = QFileInfo("testqrc/subdir/subdir.txt");
       
   238         QTest::newRow(QString(root + "subdir text").toLatin1().constData())       << QString(root + "test/abc/123/+++/subdir/subdir.txt")
       
   239                                                   << QString("\"This is in the sub directory\" ")
       
   240                                                   << QStringList()
       
   241                                                   << QStringList()
       
   242                                                   << QLocale::c()
       
   243                                                   << qlonglong(info.size());
       
   244 
       
   245         info = QFileInfo("testqrc/test/testdir.txt");
       
   246         QTest::newRow(QString(root + "testdir text").toLatin1().constData())      << QString(root + "test/testdir.txt")
       
   247                                                   << QString("\"This is in the test directory\" ")
       
   248                                                   << QStringList()
       
   249                                                   << QStringList()
       
   250                                                   << QLocale::c()
       
   251                                                   << qlonglong(info.size());
       
   252 
       
   253         info = QFileInfo("testqrc/otherdir/otherdir.txt");
       
   254         QTest::newRow(QString(root + "otherdir text").toLatin1().constData())     << QString(root + "otherdir/otherdir.txt")
       
   255                                                   << QString("\"This is the other dir\" ")
       
   256                                                   << QStringList()
       
   257                                                   << QStringList()
       
   258                                                   << QLocale::c()
       
   259                                                   << qlonglong(info.size());
       
   260 
       
   261         info = QFileInfo("testqrc/test/testdir2.txt");
       
   262         QTest::newRow(QString(root + "alias text").toLatin1().constData())        << QString(root + "aliasdir/aliasdir.txt")
       
   263                                                   << QString("\"This is another file in this directory\" ")
       
   264                                                   << QStringList()
       
   265                                                   << QStringList()
       
   266                                                   << QLocale::c()
       
   267                                                   << qlonglong(info.size());
       
   268 
       
   269         info = QFileInfo("testqrc/aliasdir/aliasdir.txt");
       
   270         QTest::newRow(QString(root + "korean text").toLatin1().constData())       << QString(root + "aliasdir/aliasdir.txt")
       
   271                                                   << QString("\"This is a korean text file\" ")
       
   272                                                   << QStringList()
       
   273                                                   << QStringList()
       
   274                                                   << QLocale("ko")
       
   275                                                   << qlonglong(info.size());
       
   276 
       
   277         info = QFileInfo("testqrc/aliasdir/aliasdir.txt");
       
   278         QTest::newRow(QString(root + "korean text 2").toLatin1().constData())     << QString(root + "aliasdir/aliasdir.txt")
       
   279                                                   << QString("\"This is a korean text file\" ")
       
   280                                                   << QStringList()
       
   281                                                   << QStringList()
       
   282                                                   << QLocale("ko_KR")
       
   283                                                   << qlonglong(info.size());
       
   284 
       
   285         info = QFileInfo("testqrc/test/german.txt");
       
   286         QTest::newRow(QString(root + "german text").toLatin1().constData())   << QString(root + "aliasdir/aliasdir.txt")
       
   287                                               << QString("Deutsch")
       
   288                                               << QStringList()
       
   289                                               << QStringList()
       
   290                                               << QLocale("de")
       
   291                                               << qlonglong(info.size());
       
   292 
       
   293         info = QFileInfo("testqrc/test/german.txt");
       
   294         QTest::newRow(QString(root + "german text 2").toLatin1().constData())   << QString(root + "aliasdir/aliasdir.txt")
       
   295                                                 << QString("Deutsch")
       
   296                                                 << QStringList()
       
   297                                                 << QStringList()
       
   298                                                 << QLocale("de_DE")
       
   299                                                 << qlonglong(info.size());
       
   300 
       
   301         QFile file("testqrc/aliasdir/compressme.txt");
       
   302         file.open(QFile::ReadOnly);
       
   303         info = QFileInfo("testqrc/aliasdir/compressme.txt");
       
   304         QTest::newRow(QString(root + "compressed text").toLatin1().constData())   << QString(root + "aliasdir/aliasdir.txt")
       
   305                                                   << QString(file.readAll())
       
   306                                                   << QStringList()
       
   307                                                   << QStringList()
       
   308                                                   << QLocale("de_CH")
       
   309                                                   << qlonglong(info.size());
       
   310     }
       
   311 }
       
   312 
       
   313 void tst_QResourceEngine::checkStructure()
       
   314 {
       
   315     QFETCH(QString, pathName);
       
   316     QFETCH(QString, contents);
       
   317     QFETCH(QStringList, containedFiles);
       
   318     QFETCH(QStringList, containedDirs);
       
   319     QFETCH(QLocale, locale);
       
   320     QFETCH(qlonglong, contentsSize);
       
   321 
       
   322     bool directory = (containedDirs.size() + containedFiles.size() > 0);
       
   323     QLocale::setDefault(locale);
       
   324 
       
   325     QFileInfo fileInfo(pathName);
       
   326 
       
   327     QVERIFY(fileInfo.exists());
       
   328     QCOMPARE(fileInfo.isDir(), directory);
       
   329     QCOMPARE(fileInfo.size(), contentsSize);
       
   330     //QVERIFY(fileInfo.isReadable());
       
   331     QVERIFY(!fileInfo.isWritable());
       
   332     QVERIFY(!fileInfo.isExecutable());
       
   333 
       
   334     if (directory) {
       
   335         QDir dir(pathName);
       
   336 
       
   337         // Test the Dir filter
       
   338         QFileInfoList list = dir.entryInfoList(QDir::Dirs, QDir::Name);
       
   339 //           for(int i = 0; i < list.size(); ++i)
       
   340 //               qDebug() << "one" << i << list.at(i).fileName();
       
   341 //           for(int i = 0; i < containedDirs.size(); ++i)
       
   342 //               qDebug() << "two" << i << containedDirs.at(i);
       
   343 //           qDebug() << "one" << list.size() << containedDirs.size();
       
   344        QCOMPARE(list.size(), containedDirs.size());
       
   345 //         qDebug() << "two";
       
   346 
       
   347         int i;
       
   348         for (i=0; i<list.size(); ++i) {
       
   349             QVERIFY(list.at(i).isDir());
       
   350             QCOMPARE(list.at(i).fileName(), containedDirs.at(i));
       
   351         }
       
   352 
       
   353         list = dir.entryInfoList(QDir::Files, QDir::Name);
       
   354         QCOMPARE(containedFiles.size(), list.size());
       
   355 
       
   356         for (i=0; i<list.size(); ++i) {
       
   357             QVERIFY(!list.at(i).isDir());
       
   358             QCOMPARE(list.at(i).fileName(), containedFiles.at(i));
       
   359         }
       
   360 
       
   361         list = dir.entryInfoList(QDir::NoFilter, QDir::SortFlags(QDir::Name | QDir::DirsFirst));
       
   362         QCOMPARE(containedFiles.size() + containedDirs.size(), list.size());
       
   363 
       
   364         for (i=0; i<list.size(); ++i) {
       
   365             QString expectedName;
       
   366             if (i < containedDirs.size())
       
   367                 expectedName = containedDirs.at(i);
       
   368             else
       
   369                 expectedName = containedFiles.at(i - containedDirs.size());
       
   370 
       
   371             QCOMPARE(list.at(i).fileName(), expectedName);
       
   372         }
       
   373     } else {
       
   374         QFile file(pathName);
       
   375         QVERIFY(file.open(QFile::ReadOnly));
       
   376 
       
   377         QByteArray ba = file.readAll();
       
   378         QVERIFY(QString(ba).startsWith(contents));
       
   379     }
       
   380     QLocale::setDefault(QLocale::system());
       
   381 }
       
   382 
       
   383 void tst_QResourceEngine::searchPath_data()
       
   384 {
       
   385     QTest::addColumn<QString>("searchPath");
       
   386     QTest::addColumn<QString>("file");
       
   387     QTest::addColumn<QByteArray>("expected");
       
   388 
       
   389     QTest::newRow("no_search_path")  << QString()
       
   390                                   << ":search_file.txt"
       
   391                                   << QByteArray("root\n");
       
   392     QTest::newRow("path1")  << "/searchpath1"
       
   393                          << ":search_file.txt"
       
   394                          << QByteArray("path1\n");
       
   395     QTest::newRow("no_search_path2")  << QString()
       
   396                                   << ":/search_file.txt"
       
   397                                   << QByteArray("root\n");
       
   398     QTest::newRow("path2")  << "/searchpath2"
       
   399                          << ":search_file.txt"
       
   400                          << QByteArray("path2\n");
       
   401 }
       
   402 
       
   403 void tst_QResourceEngine::searchPath()
       
   404 {
       
   405     QFETCH(QString, searchPath);
       
   406     QFETCH(QString, file);
       
   407     QFETCH(QByteArray, expected);
       
   408 
       
   409     if(!searchPath.isEmpty())
       
   410         QDir::addResourceSearchPath(searchPath);
       
   411     QFile qf(file);
       
   412     QVERIFY(qf.open(QFile::ReadOnly));
       
   413     QByteArray actual = qf.readAll();
       
   414 
       
   415     actual.replace('\r', "");
       
   416 
       
   417     QCOMPARE(actual, expected);
       
   418     qf.close();
       
   419 }
       
   420 
       
   421 void tst_QResourceEngine::checkUnregisterResource_data()
       
   422 {
       
   423     QTest::addColumn<QString>("rcc_file");
       
   424     QTest::addColumn<QString>("root");
       
   425     QTest::addColumn<QString>("file_check");
       
   426     QTest::addColumn<int>("size");
       
   427 
       
   428     QTest::newRow("currentdir.txt") << builddir + QString("runtime_resource.rcc") << QString("/check_unregister/")
       
   429                                     << QString(":/check_unregister/runtime_resource/test/abc/123/+++/currentdir.txt") 
       
   430                                     << (int)QFileInfo("testqrc/currentdir.txt").size();
       
   431 }
       
   432 
       
   433 void tst_QResourceEngine::checkUnregisterResource()
       
   434 {
       
   435     QFETCH(QString, rcc_file);
       
   436     QFETCH(QString, root);
       
   437     QFETCH(QString, file_check);
       
   438     QFETCH(int, size);
       
   439 
       
   440 
       
   441 
       
   442     QVERIFY(!QFile::exists(file_check));
       
   443     QVERIFY(QResource::registerResource(rcc_file, root));
       
   444     QVERIFY(QFile::exists(file_check));
       
   445     QVERIFY(QResource::unregisterResource(rcc_file, root));
       
   446     QVERIFY(!QFile::exists(file_check));
       
   447     QVERIFY(QResource::registerResource(rcc_file, root));
       
   448     QVERIFY(QFile::exists(file_check));
       
   449     QFileInfo fileInfo(file_check);
       
   450     fileInfo.setCaching(false);
       
   451     QVERIFY(fileInfo.exists());
       
   452     QVERIFY(!QResource::unregisterResource(rcc_file, root));
       
   453     QVERIFY(!QFile::exists(file_check));
       
   454     QCOMPARE((int)fileInfo.size(), size);
       
   455 }
       
   456 
       
   457 void tst_QResourceEngine::doubleSlashInRoot()
       
   458 {
       
   459     QVERIFY(QFile::exists(":/secondary_root/runtime_resource/search_file.txt"));
       
   460     QVERIFY(QFile::exists("://secondary_root/runtime_resource/search_file.txt"));
       
   461 }
       
   462 
       
   463 QTEST_MAIN(tst_QResourceEngine)
       
   464 
       
   465 #include "tst_qresourceengine.moc"
       
   466