| 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/QHelpIndexWidget> |
| 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_QHelpIndexModel : public QObject |
| 71 | { |
| 72 | Q_OBJECT |
| 73 | |
| 74 | private slots: |
| 75 | void init(); |
| 76 | |
| 77 | void setupIndex(); |
| 78 | void filter(); |
| 79 | void linksForIndex(); |
| 80 | |
| 81 | private: |
| 82 | QString m_colFile; |
| 83 | }; |
| 84 | |
| 85 | void tst_QHelpIndexModel::init() |
| 86 | { |
| 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_QHelpIndexModel::setupIndex() |
| 99 | { |
| 100 | QHelpEngine h(m_colFile, 0); |
| 101 | QHelpIndexModel *m = h.indexModel(); |
| 102 | SignalWaiter w; |
| 103 | connect(sender: m, SIGNAL(indexCreated()), |
| 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->stringList().count(), 19); |
| 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 | QStringList lst; |
| 121 | lst << "foo" << "bar" << "bla" << "einstein" << "newton" ; |
| 122 | const auto stringList = m->stringList(); |
| 123 | QCOMPARE(stringList.count(), 5); |
| 124 | for (const QString &s : stringList) |
| 125 | lst.removeAll(t: s); |
| 126 | QCOMPARE(lst.isEmpty(), true); |
| 127 | } |
| 128 | |
| 129 | void tst_QHelpIndexModel::filter() |
| 130 | { |
| 131 | QHelpEngine h(m_colFile, 0); |
| 132 | QHelpIndexModel *m = h.indexModel(); |
| 133 | SignalWaiter w; |
| 134 | connect(sender: m, SIGNAL(indexCreated()), |
| 135 | receiver: &w, SLOT(stopWaiting())); |
| 136 | w.start(); |
| 137 | h.setupData(); |
| 138 | int i = 0; |
| 139 | while (w.isRunning() && i++ < 10) |
| 140 | QTest::qWait(ms: 500); |
| 141 | |
| 142 | QCOMPARE(h.currentFilter(), QString("unfiltered" )); |
| 143 | QCOMPARE(m->stringList().count(), 19); |
| 144 | |
| 145 | m->filter(filter: "foo" ); |
| 146 | QCOMPARE(m->stringList().count(), 2); |
| 147 | |
| 148 | m->filter(filter: "fo" ); |
| 149 | QCOMPARE(m->stringList().count(), 3); |
| 150 | |
| 151 | m->filter(filter: "qmake" ); |
| 152 | QCOMPARE(m->stringList().count(), 11); |
| 153 | } |
| 154 | |
| 155 | void tst_QHelpIndexModel::linksForIndex() |
| 156 | { |
| 157 | QHelpEngine h(m_colFile, 0); |
| 158 | QHelpIndexModel *m = h.indexModel(); |
| 159 | SignalWaiter w; |
| 160 | connect(sender: m, SIGNAL(indexCreated()), |
| 161 | receiver: &w, SLOT(stopWaiting())); |
| 162 | w.start(); |
| 163 | h.setupData(); |
| 164 | int i = 0; |
| 165 | while (w.isRunning() && i++ < 10) |
| 166 | QTest::qWait(ms: 500); |
| 167 | |
| 168 | QCOMPARE(h.currentFilter(), QString("unfiltered" )); |
| 169 | QMap<QString, QUrl> map; |
| 170 | map = m->linksForKeyword(keyword: "foo" ); |
| 171 | QCOMPARE(map.count(), 2); |
| 172 | QCOMPARE(map.contains("Test Manual" ), true); |
| 173 | QCOMPARE(map.value("Test Manual" ), |
| 174 | QUrl("qthelp://trolltech.com.1-0-0.test/testFolder/test.html#foo" )); |
| 175 | |
| 176 | QCOMPARE(map.contains("Fancy" ), true); |
| 177 | QCOMPARE(map.value("Fancy" ), |
| 178 | QUrl("qthelp://trolltech.com.1-0-0.test/testFolder/fancy.html#foo" )); |
| 179 | |
| 180 | map = m->linksForKeyword(keyword: "foobar" ); |
| 181 | QCOMPARE(map.count(), 1); |
| 182 | QCOMPARE(map.contains("Fancy" ), true); |
| 183 | |
| 184 | map = m->linksForKeyword(keyword: "notexisting" ); |
| 185 | QCOMPARE(map.count(), 0); |
| 186 | |
| 187 | w.start(); |
| 188 | h.setCurrentFilter("Custom Filter 1" ); |
| 189 | i = 0; |
| 190 | while (w.isRunning() && i++ < 10) |
| 191 | QTest::qWait(ms: 500); |
| 192 | |
| 193 | map = m->linksForKeyword(keyword: "foo" ); |
| 194 | QCOMPARE(map.count(), 1); |
| 195 | QCOMPARE(map.contains("Test Manual" ), true); |
| 196 | QCOMPARE(map.value("Test Manual" ), |
| 197 | QUrl("qthelp://trolltech.com.1-0-0.test/testFolder/test.html#foo" )); |
| 198 | } |
| 199 | |
| 200 | QTEST_MAIN(tst_QHelpIndexModel) |
| 201 | #include "tst_qhelpindexmodel.moc" |
| 202 | |