tests/auto/qhelpindexmodel/tst_qhelpindexmodel.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 #include <QtTest/QtTest>
       
    42 
       
    43 #ifndef QT_NO_BUILD_TOOLS
       
    44 
       
    45 #include <QtCore/QThread>
       
    46 #include <QtCore/QUrl>
       
    47 #include <QtCore/QFileInfo>
       
    48 
       
    49 #include <QtHelp/QHelpEngine>
       
    50 #include <QtHelp/QHelpIndexWidget>
       
    51 
       
    52 class SignalWaiter : public QThread
       
    53 {
       
    54     Q_OBJECT
       
    55 
       
    56 public:
       
    57     SignalWaiter();
       
    58     void run();
       
    59 
       
    60 public slots:
       
    61     void stopWaiting();
       
    62 
       
    63 private:
       
    64     bool stop;
       
    65 };
       
    66 
       
    67 SignalWaiter::SignalWaiter()
       
    68 {
       
    69     stop = false;
       
    70 }
       
    71 
       
    72 void SignalWaiter::run()
       
    73 {
       
    74     while (!stop)
       
    75         msleep(500);
       
    76     stop = false;
       
    77 }
       
    78 
       
    79 void SignalWaiter::stopWaiting()
       
    80 {
       
    81     stop = true;
       
    82 }
       
    83 
       
    84 
       
    85 class tst_QHelpIndexModel : public QObject
       
    86 {
       
    87     Q_OBJECT
       
    88 
       
    89 private slots:
       
    90     void init();
       
    91 
       
    92     void setupIndex();
       
    93     void filter();
       
    94     void linksForIndex();
       
    95 
       
    96 private:
       
    97     QString m_colFile;
       
    98 };
       
    99 
       
   100 void tst_QHelpIndexModel::init()
       
   101 {
       
   102     QString path = QLatin1String(SRCDIR);
       
   103 
       
   104     m_colFile = path + QLatin1String("/data/col.qhc");
       
   105     if (QFile::exists(m_colFile))
       
   106         QDir::current().remove(m_colFile);
       
   107     if (!QFile::copy(path + "/data/collection.qhc", m_colFile))
       
   108         QFAIL("Cannot copy file!");
       
   109     QFile f(m_colFile);
       
   110     f.setPermissions(QFile::WriteUser|QFile::ReadUser);
       
   111 }
       
   112 
       
   113 void tst_QHelpIndexModel::setupIndex()
       
   114 {
       
   115     QHelpEngine h(m_colFile, 0);
       
   116     QHelpIndexModel *m = h.indexModel();
       
   117     SignalWaiter w;
       
   118     connect(m, SIGNAL(indexCreated()),
       
   119         &w, SLOT(stopWaiting()));
       
   120     w.start();
       
   121     h.setupData();
       
   122     int i = 0;
       
   123     while (w.isRunning() && i++ < 10)
       
   124         QTest::qWait(500);
       
   125 
       
   126     QCOMPARE(h.currentFilter(), QString("unfiltered"));
       
   127     QCOMPARE(m->stringList().count(), 19);
       
   128 
       
   129     w.start();
       
   130     h.setCurrentFilter("Custom Filter 1");
       
   131     i = 0;
       
   132     while (w.isRunning() && i++ < 10)
       
   133         QTest::qWait(500);
       
   134 
       
   135     QStringList lst;
       
   136     lst << "foo" << "bar" << "bla" << "einstein" << "newton";
       
   137     QCOMPARE(m->stringList().count(), 5);
       
   138     foreach (QString s, m->stringList())
       
   139         lst.removeAll(s);
       
   140     QCOMPARE(lst.isEmpty(), true);
       
   141 }
       
   142 
       
   143 void tst_QHelpIndexModel::filter()
       
   144 {
       
   145     QHelpEngine h(m_colFile, 0);
       
   146     QHelpIndexModel *m = h.indexModel();
       
   147     SignalWaiter w;
       
   148     connect(m, SIGNAL(indexCreated()),
       
   149         &w, SLOT(stopWaiting()));
       
   150     w.start();
       
   151     h.setupData();
       
   152     int i = 0;
       
   153     while (w.isRunning() && i++ < 10)
       
   154         QTest::qWait(500);
       
   155 
       
   156     QCOMPARE(h.currentFilter(), QString("unfiltered"));
       
   157     QCOMPARE(m->stringList().count(), 19);
       
   158 
       
   159     m->filter("foo");
       
   160     QCOMPARE(m->stringList().count(), 2);
       
   161 
       
   162     m->filter("fo");
       
   163     QCOMPARE(m->stringList().count(), 3);
       
   164 
       
   165     m->filter("qmake");
       
   166     QCOMPARE(m->stringList().count(), 11);
       
   167 }
       
   168 
       
   169 void tst_QHelpIndexModel::linksForIndex()
       
   170 {
       
   171     QHelpEngine h(m_colFile, 0);
       
   172     QHelpIndexModel *m = h.indexModel();
       
   173     SignalWaiter w;
       
   174     connect(m, SIGNAL(indexCreated()),
       
   175         &w, SLOT(stopWaiting()));
       
   176     w.start();
       
   177     h.setupData();
       
   178     int i = 0;
       
   179     while (w.isRunning() && i++ < 10)
       
   180         QTest::qWait(500);
       
   181 
       
   182     QCOMPARE(h.currentFilter(), QString("unfiltered"));
       
   183     QMap<QString, QUrl> map;
       
   184     map = m->linksForKeyword("foo");
       
   185     QCOMPARE(map.count(), 2);
       
   186     QCOMPARE(map.contains("Test Manual"), true);
       
   187     QCOMPARE(map.value("Test Manual"),
       
   188         QUrl("qthelp://trolltech.com.1-0-0.test/testFolder/test.html#foo"));
       
   189 
       
   190     QCOMPARE(map.contains("Fancy"), true);
       
   191     QCOMPARE(map.value("Fancy"),
       
   192         QUrl("qthelp://trolltech.com.1-0-0.test/testFolder/fancy.html#foo"));
       
   193 
       
   194     map = m->linksForKeyword("foobar");
       
   195     QCOMPARE(map.count(), 1);
       
   196     QCOMPARE(map.contains("Fancy"), true);
       
   197 
       
   198     map = m->linksForKeyword("notexisting");
       
   199     QCOMPARE(map.count(), 0);
       
   200 
       
   201     w.start();
       
   202     h.setCurrentFilter("Custom Filter 1");
       
   203     i = 0;
       
   204     while (w.isRunning() && i++ < 10)
       
   205         QTest::qWait(500);
       
   206 
       
   207     map = m->linksForKeyword("foo");
       
   208     QCOMPARE(map.count(), 1);
       
   209     QCOMPARE(map.contains("Test Manual"), true);
       
   210     QCOMPARE(map.value("Test Manual"),
       
   211         QUrl("qthelp://trolltech.com.1-0-0.test/testFolder/test.html#foo"));
       
   212 }
       
   213 
       
   214 QTEST_MAIN(tst_QHelpIndexModel)
       
   215 #include "tst_qhelpindexmodel.moc"
       
   216 
       
   217 #else // QT_NO_BUILD_TOOLS
       
   218 QTEST_NOOP_MAIN
       
   219 #endif