Debian Codegraph @

tests/auto/qhelpcontentmodel/tst_qhelpcontentmodel.cpp

raw
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/QHelpContentWidget>
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_QHelpContentModel : public QObject
46 {
47 Q_OBJECT
48
49 private slots:
50 void init();
51
52 void setupContents();
53 void contentItemAt();
54
55 private:
56 QString m_colFile;
57 };
58
59 void tst_QHelpContentModel::init()
60 {
61 // defined in profile
62 QString path = QLatin1String(SRCDIR);
63
64 m_colFile = path + QLatin1String("/data/col.qhc");
65 if (QFile::exists(m_colFile))
66 QDir::current().remove(m_colFile);
67 if (!QFile::copy(path + "/data/collection.qhc", m_colFile))
68 QFAIL("Cannot copy file!");
69 QFile f(m_colFile);
70 f.setPermissions(QFile::WriteUser|QFile::ReadUser);
71 }
72
73 void tst_QHelpContentModel::setupContents()
74 {
75 QHelpEngine h(m_colFile, 0);
76 h.setReadOnly(false);
77 QHelpContentModel *m = h.contentModel();
78 SignalWaiter w;
79 connect(m, &QHelpContentModel::contentsCreated, &w, &SignalWaiter::stopWaiting);
80 w.start();
81 h.setupData();
82 int i = 0;
83 while (w.isRunning() && i++ < 10)
84 QTest::qWait(500);
85
86 QCOMPARE(h.currentFilter(), QString("unfiltered"));
87 QCOMPARE(m->rowCount(), 4);
88
89 w.start();
90 h.setCurrentFilter("Custom Filter 1");
91 i = 0;
92 while (w.isRunning() && i++ < 10)
93 QTest::qWait(500);
94
95 QCOMPARE(m->rowCount(), 1);
96 }
97
98 void tst_QHelpContentModel::contentItemAt()
99 {
100 QHelpEngine h(m_colFile, 0);
101 h.setReadOnly(false);
102 QHelpContentModel *m = h.contentModel();
103 SignalWaiter w;
104 connect(m, &QHelpContentModel::contentsCreated, &w, &SignalWaiter::stopWaiting);
105 w.start();
106 h.setupData();
107 int i = 0;
108 while (w.isRunning() && i++ < 10)
109 QTest::qWait(500);
110
111 QCOMPARE(h.currentFilter(), QString("unfiltered"));
112
113 QModelIndex root = m->index(2, 0);
114 if (!root.isValid())
115 QFAIL("Cannot retrieve root item!");
116 QHelpContentItem *item = m->contentItemAt(root);
117 if (!item)
118 QFAIL("Cannot retrieve content item!");
119 QCOMPARE(item->title(), QString("qmake Manual"));
120
121 item = m->contentItemAt(m->index(4, 0, root));
122 QCOMPARE(item->title(), QString("qmake Concepts"));
123
124 item = m->contentItemAt(m->index(0, 0));
125 QCOMPARE(item->title(), QString("Fancy Manual"));
126
127 w.start();
128 h.setCurrentFilter("Custom Filter 1");
129 i = 0;
130 while (w.isRunning() && i++ < 10)
131 QTest::qWait(500);
132
133 item = m->contentItemAt(m->index(0, 0));
134 QCOMPARE(item->title(), QString("Test Manual"));
135 }
136
137 QTEST_MAIN(tst_QHelpContentModel)
138 #include "tst_qhelpcontentmodel.moc"