| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | #include <QtTest/QtTest> |
| 29 | |
| 30 | #include <QtCore/QThread> |
| 31 | #include <QtCore/QUrl> |
| 32 | #include <QtCore/QFileInfo> |
| 33 | |
| 34 | #include <QtHelp/QHelpEngine> |
| 35 | #include <QtHelp/QHelpContentWidget> |
| 36 | |
| 37 | class SignalWaiter : public QThread |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | |
| 41 | public: |
| 42 | SignalWaiter(); |
| 43 | void run(); |
| 44 | |
| 45 | public slots: |
| 46 | void stopWaiting(); |
| 47 | |
| 48 | private: |
| 49 | bool stop; |
| 50 | }; |
| 51 | |
| 52 | SignalWaiter::SignalWaiter() |
| 53 | { |
| 54 | stop = false; |
| 55 | } |
| 56 | |
| 57 | void SignalWaiter::run() |
| 58 | { |
| 59 | while (!stop) |
| 60 | msleep(500); |
| 61 | stop = false; |
| 62 | } |
| 63 | |
| 64 | void SignalWaiter::stopWaiting() |
| 65 | { |
| 66 | stop = true; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | class tst_QHelpContentModel : public QObject |
| 71 | { |
| 72 | Q_OBJECT |
| 73 | |
| 74 | private slots: |
| 75 | void init(); |
| 76 | |
| 77 | void setupContents(); |
| 78 | void contentItemAt(); |
| 79 | |
| 80 | private: |
| 81 | QString m_colFile; |
| 82 | }; |
| 83 | |
| 84 | void tst_QHelpContentModel::init() |
| 85 | { |
| 86 | // defined in profile |
| 87 | QString path = QLatin1String(SRCDIR); |
| 88 | |
| 89 | m_colFile = path + QLatin1String("/data/col.qhc" ); |
| 90 | if (QFile::exists(fileName: m_colFile)) |
| 91 | QDir::current().remove(fileName: m_colFile); |
| 92 | if (!QFile::copy(fileName: path + "/data/collection.qhc" , newName: m_colFile)) |
| 93 | QFAIL("Cannot copy file!" ); |
| 94 | QFile f(m_colFile); |
| 95 | f.setPermissions(QFile::WriteUser|QFile::ReadUser); |
| 96 | } |
| 97 | |
| 98 | void tst_QHelpContentModel::setupContents() |
| 99 | { |
| 100 | QHelpEngine h(m_colFile, 0); |
| 101 | QHelpContentModel *m = h.contentModel(); |
| 102 | SignalWaiter w; |
| 103 | connect(sender: m, SIGNAL(contentsCreated()), |
| 104 | receiver: &w, SLOT(stopWaiting())); |
| 105 | w.start(); |
| 106 | h.setupData(); |
| 107 | int i = 0; |
| 108 | while (w.isRunning() && i++ < 10) |
| 109 | QTest::qWait(ms: 500); |
| 110 | |
| 111 | QCOMPARE(h.currentFilter(), QString("unfiltered" )); |
| 112 | QCOMPARE(m->rowCount(), 4); |
| 113 | |
| 114 | w.start(); |
| 115 | h.setCurrentFilter("Custom Filter 1" ); |
| 116 | i = 0; |
| 117 | while (w.isRunning() && i++ < 10) |
| 118 | QTest::qWait(ms: 500); |
| 119 | |
| 120 | QCOMPARE(m->rowCount(), 1); |
| 121 | } |
| 122 | |
| 123 | void tst_QHelpContentModel::contentItemAt() |
| 124 | { |
| 125 | QHelpEngine h(m_colFile, 0); |
| 126 | QHelpContentModel *m = h.contentModel(); |
| 127 | SignalWaiter w; |
| 128 | connect(sender: m, SIGNAL(contentsCreated()), |
| 129 | receiver: &w, SLOT(stopWaiting())); |
| 130 | w.start(); |
| 131 | h.setupData(); |
| 132 | int i = 0; |
| 133 | while (w.isRunning() && i++ < 10) |
| 134 | QTest::qWait(ms: 500); |
| 135 | |
| 136 | QCOMPARE(h.currentFilter(), QString("unfiltered" )); |
| 137 | |
| 138 | QModelIndex root = m->index(row: 2, column: 0); |
| 139 | if (!root.isValid()) |
| 140 | QFAIL("Cannot retrieve root item!" ); |
| 141 | QHelpContentItem *item = m->contentItemAt(index: root); |
| 142 | if (!item) |
| 143 | QFAIL("Cannot retrieve content item!" ); |
| 144 | QCOMPARE(item->title(), QString("qmake Manual" )); |
| 145 | |
| 146 | item = m->contentItemAt(index: m->index(row: 4, column: 0, parent: root)); |
| 147 | QCOMPARE(item->title(), QString("qmake Concepts" )); |
| 148 | |
| 149 | item = m->contentItemAt(index: m->index(row: 0, column: 0)); |
| 150 | QCOMPARE(item->title(), QString("Fancy Manual" )); |
| 151 | |
| 152 | w.start(); |
| 153 | h.setCurrentFilter("Custom Filter 1" ); |
| 154 | i = 0; |
| 155 | while (w.isRunning() && i++ < 10) |
| 156 | QTest::qWait(ms: 500); |
| 157 | |
| 158 | item = m->contentItemAt(index: m->index(row: 0, column: 0)); |
| 159 | QCOMPARE(item->title(), QString("Test Manual" )); |
| 160 | } |
| 161 | |
| 162 | QTEST_MAIN(tst_QHelpContentModel) |
| 163 | #include "tst_qhelpcontentmodel.moc" |
| 164 | |