| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
#include <QtTest/QtTest> |
| 4 |
|
| 5 |
#include <QtCore/QThread> |
| 6 |
#include <QtCore/QUrl> |
| 7 |
#include <QtCore/QFileInfo> |
| 8 |
|
| 9 |
#include <QtHelp/QHelpEngine> |
| 10 |
#include <QtHelp/QHelpIndexWidget> |
| 11 |
|
| 12 |
class SignalWaiter : public QThread |
| 13 |
{ |
| 14 |
Q_OBJECT |
| 15 |
|
| 16 |
public: |
| 17 |
SignalWaiter(); |
| 18 |
void run() override; |
| 19 |
|
| 20 |
public slots: |
| 21 |
void stopWaiting(); |
| 22 |
|
| 23 |
private: |
| 24 |
bool stop; |
| 25 |
}; |
| 26 |
|
| 27 |
SignalWaiter::SignalWaiter() |
| 28 |
{ |
| 29 |
stop = false; |
| 30 |
} |
| 31 |
|
| 32 |
void SignalWaiter::run() |
| 33 |
{ |
| 34 |
while (!stop) |
| 35 |
msleep(500); |
| 36 |
stop = false; |
| 37 |
} |
| 38 |
|
| 39 |
void SignalWaiter::stopWaiting() |
| 40 |
{ |
| 41 |
stop = true; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
class tst_QHelpIndexModel : public QObject |
| 46 |
{ |
| 47 |
Q_OBJECT |
| 48 |
|
| 49 |
private slots: |
| 50 |
void init(); |
| 51 |
|
| 52 |
void setupIndex(); |
| 53 |
void filter(); |
| 54 |
|
| 55 |
private: |
| 56 |
QString m_colFile; |
| 57 |
}; |
| 58 |
|
| 59 |
void tst_QHelpIndexModel::init() |
| 60 |
{ |
| 61 |
QString path = QLatin1String(SRCDIR); |
| 62 |
|
| 63 |
m_colFile = path + QLatin1String("/data/col.qhc"); |
| 64 |
if (QFile::exists(m_colFile)) |
| 65 |
QDir::current().remove(m_colFile); |
| 66 |
if (!QFile::copy(path + "/data/collection.qhc", m_colFile)) |
| 67 |
QFAIL("Cannot copy file!"); |
| 68 |
QFile f(m_colFile); |
| 69 |
f.setPermissions(QFile::WriteUser|QFile::ReadUser); |
| 70 |
} |
| 71 |
|
| 72 |
void tst_QHelpIndexModel::setupIndex() |
| 73 |
{ |
| 74 |
QHelpEngine h(m_colFile, 0); |
| 75 |
h.setReadOnly(false); |
| 76 |
QHelpIndexModel *m = h.indexModel(); |
| 77 |
SignalWaiter w; |
| 78 |
connect(m, &QHelpIndexModel::indexCreated, &w, &SignalWaiter::stopWaiting); |
| 79 |
w.start(); |
| 80 |
h.setupData(); |
| 81 |
int i = 0; |
| 82 |
while (w.isRunning() && i++ < 10) |
| 83 |
QTest::qWait(500); |
| 84 |
|
| 85 |
QCOMPARE(h.currentFilter(), QString("unfiltered")); |
| 86 |
QCOMPARE(m->stringList().size(), 19); |
| 87 |
|
| 88 |
w.start(); |
| 89 |
h.setCurrentFilter("Custom Filter 1"); |
| 90 |
i = 0; |
| 91 |
while (w.isRunning() && i++ < 10) |
| 92 |
QTest::qWait(500); |
| 93 |
|
| 94 |
QStringList lst; |
| 95 |
lst << "foo" << "bar" << "bla" << "einstein" << "newton"; |
| 96 |
const auto stringList = m->stringList(); |
| 97 |
QCOMPARE(stringList.size(), 5); |
| 98 |
for (const QString &s : stringList) |
| 99 |
lst.removeAll(s); |
| 100 |
QCOMPARE(lst.isEmpty(), true); |
| 101 |
} |
| 102 |
|
| 103 |
void tst_QHelpIndexModel::filter() |
| 104 |
{ |
| 105 |
QHelpEngine h(m_colFile, 0); |
| 106 |
h.setReadOnly(false); |
| 107 |
QHelpIndexModel *m = h.indexModel(); |
| 108 |
SignalWaiter w; |
| 109 |
connect(m, &QHelpIndexModel::indexCreated, &w, &SignalWaiter::stopWaiting); |
| 110 |
w.start(); |
| 111 |
h.setupData(); |
| 112 |
int i = 0; |
| 113 |
while (w.isRunning() && i++ < 10) |
| 114 |
QTest::qWait(500); |
| 115 |
|
| 116 |
QCOMPARE(h.currentFilter(), QString("unfiltered")); |
| 117 |
QCOMPARE(m->stringList().size(), 19); |
| 118 |
|
| 119 |
m->filter("foo"); |
| 120 |
QCOMPARE(m->stringList().size(), 2); |
| 121 |
|
| 122 |
m->filter("fo"); |
| 123 |
QCOMPARE(m->stringList().size(), 3); |
| 124 |
|
| 125 |
m->filter("qmake"); |
| 126 |
QCOMPARE(m->stringList().size(), 11); |
| 127 |
} |
| 128 |
|
| 129 |
QTEST_MAIN(tst_QHelpIndexModel) |
| 130 |
#include "tst_qhelpindexmodel.moc" |