tests/auto/qhelpcontentmodel/tst_qhelpcontentmodel.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/QHelpContentWidget>
       
    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_QHelpContentModel : public QObject
       
    86 {
       
    87     Q_OBJECT
       
    88 
       
    89 private slots:
       
    90     void init();
       
    91 
       
    92     void setupContents();
       
    93     void contentItemAt();
       
    94 
       
    95 private:
       
    96     QString m_colFile;
       
    97 };
       
    98 
       
    99 void tst_QHelpContentModel::init()
       
   100 {
       
   101     // defined in profile
       
   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_QHelpContentModel::setupContents()
       
   114 {
       
   115     QHelpEngine h(m_colFile, 0);
       
   116     QHelpContentModel *m = h.contentModel();
       
   117     SignalWaiter w;
       
   118     connect(m, SIGNAL(contentsCreated()),
       
   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->rowCount(), 4);
       
   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      QCOMPARE(m->rowCount(), 1);
       
   136 }
       
   137 
       
   138 void tst_QHelpContentModel::contentItemAt()
       
   139 {
       
   140     QHelpEngine h(m_colFile, 0);
       
   141     QHelpContentModel *m = h.contentModel();
       
   142     SignalWaiter w;
       
   143     connect(m, SIGNAL(contentsCreated()),
       
   144         &w, SLOT(stopWaiting()));
       
   145     w.start();
       
   146     h.setupData();
       
   147     int i = 0;
       
   148     while (w.isRunning() && i++ < 10)
       
   149         QTest::qWait(500);
       
   150 
       
   151     QCOMPARE(h.currentFilter(), QString("unfiltered"));
       
   152 
       
   153     QModelIndex root = m->index(0, 0);
       
   154     if (!root.isValid())
       
   155         QFAIL("Cannot retrieve root item!");
       
   156     QHelpContentItem *item = m->contentItemAt(root);
       
   157     if (!item)
       
   158         QFAIL("Cannot retrieve content item!");
       
   159     QCOMPARE(item->title(), QString("qmake Manual"));
       
   160 
       
   161     item = m->contentItemAt(m->index(4, 0, root));
       
   162     QCOMPARE(item->title(), QString("qmake Concepts"));
       
   163 
       
   164     item = m->contentItemAt(m->index(3, 0));
       
   165     QCOMPARE(item->title(), QString("Fancy Manual"));
       
   166 
       
   167     w.start();
       
   168     h.setCurrentFilter("Custom Filter 1");
       
   169     i = 0;
       
   170     while (w.isRunning() && i++ < 10)
       
   171         QTest::qWait(500);
       
   172 
       
   173     item = m->contentItemAt(m->index(0, 0));
       
   174     QCOMPARE(item->title(), QString("Test Manual"));
       
   175 }
       
   176 
       
   177 QTEST_MAIN(tst_QHelpContentModel)
       
   178 #include "tst_qhelpcontentmodel.moc"
       
   179 
       
   180 #else // QT_NO_BUILD_TOOLS
       
   181 QTEST_NOOP_MAIN
       
   182 #endif