Debian Codegraph @

tests/auto/qhelpenginecore/tst_qhelpenginecore.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/QUrl>
6 #include <QtCore/QFileInfo>
7 #include <QtCore/QScopeGuard>
8 #include <QtSql/QSqlDatabase>
9 #include <QtSql/QSqlQuery>
10
11 #include <QtHelp/QHelpEngineCore>
12
13 class tst_QHelpEngineCore : public QObject
14 {
15 Q_OBJECT
16
17 private slots:
18 void init();
19
20 void setupData();
21 void collectionFile();
22 void setCollectionFile();
23 void copyCollectionFile();
24
25 void namespaceName();
26 void registeredDocumentations();
27 void registerDocumentation();
28 void unregisterDocumentation();
29 void documentationFileName();
30
31 void customFilters();
32 void removeCustomFilter();
33 void addCustomFilter();
34 void filterAttributes();
35 void currentFilter();
36 void setCurrentFilter();
37
38 void filterAttributeSets();
39 void files();
40 void fileData();
41
42 void customValue();
43 void setCustomValue();
44 void removeCustomValue();
45
46 void setAutoSaveFilter();
47
48 void metaData();
49
50 private:
51 QString m_path;
52 QString m_colFile;
53 };
54
55 void tst_QHelpEngineCore::init()
56 {
57 // defined in profile
58 m_path = QLatin1String(SRCDIR);
59
60 m_path = QFileInfo(m_path).absoluteFilePath();
61
62 m_colFile = m_path + QLatin1String("/data/col.qhc");
63 if (QFile::exists(m_colFile))
64 QDir::current().remove(m_colFile);
65 if (!QFile::copy(m_path + "/data/collection.qhc", m_colFile))
66 QFAIL("Cannot copy file!");
67 QFile f(m_colFile);
68 f.setPermissions(QFile::WriteUser|QFile::ReadUser);
69 }
70
71 void tst_QHelpEngineCore::setupData()
72 {
73 QHelpEngineCore help(m_colFile, 0);
74 QCOMPARE(help.setupData(), true);
75 }
76
77 void tst_QHelpEngineCore::collectionFile()
78 {
79 QHelpEngineCore help(m_colFile, 0);
80 QCOMPARE(help.collectionFile(), QFileInfo(m_colFile).absoluteFilePath());
81 }
82
83 void tst_QHelpEngineCore::setCollectionFile()
84 {
85 QHelpEngineCore help(m_colFile, 0);
86 QCOMPARE(help.collectionFile(), QFileInfo(m_colFile).absoluteFilePath());
87
88 QString col1File = m_path + QLatin1String("/data/collection1.qhc");
89 help.setCollectionFile(col1File);
90 QCOMPARE(help.collectionFile(), QFileInfo(col1File).absoluteFilePath());
91
92 QStringList docs = help.registeredDocumentations();
93 QCOMPARE(docs.size(), 1);
94 QCOMPARE(docs.first(), QLatin1String("trolltech.com.1.0.0.test"));
95 }
96
97 void tst_QHelpEngineCore::copyCollectionFile()
98 {
99 QHelpEngineCore help(m_colFile, 0);
100 QCOMPARE(help.collectionFile(), QFileInfo(m_colFile).absoluteFilePath());
101
102 QString copiedFile = m_path + QLatin1String("/collectionCopy.qhc");
103 if (QFile::exists(copiedFile))
104 QDir::current().remove(copiedFile);
105
106 QCOMPARE(help.copyCollectionFile(copiedFile), true);
107
108 {
109 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "testdb");
110 db.setDatabaseName(copiedFile);
111 if (!db.open()) {
112 QSqlDatabase::removeDatabase("testdb");
113 QFAIL("Created database seems to be corrupt!");
114 }
115 QSqlQuery *m_query = new QSqlQuery(db);
116
117 m_query->exec("SELECT Key, Value FROM SettingsTable");
118 if (m_query->next()) {
119 QCOMPARE(m_query->value(0).toString(), QString("CurrentFilter"));
120 QCOMPARE(m_query->value(1).toString(), QString("unfiltered"));
121 } else {
122 QFAIL("Settingstable is corrupt!");
123 }
124
125 m_query->exec("SELECT NameId, FilterAttributeId FROM FilterTable");
126 int i = 0;
127 while (m_query->next()) {
128 if (i == 3) {
129 QCOMPARE(m_query->value(0).toInt(), 2);
130 QCOMPARE(m_query->value(1).toInt(), 6);
131 }
132 ++i;
133 }
134 QCOMPARE(i, 7);
135 m_query->clear();
136
137 m_query->exec("SELECT Name, FilePath FROM NamespaceTable");
138 i = 0;
139 while (m_query->next()) {
140 if (i == 0) {
141 QCOMPARE(m_query->value(0).toString(), QString("trolltech.com.3-3-8.qmake"));
142 QCOMPARE(m_query->value(1).toString(), QString("data/qmake-3.3.8.qch"));
143 }
144 ++i;
145 }
146 QCOMPARE(i, 3);
147
148 m_query->clear();
149 delete m_query;
150 }
151 QSqlDatabase::removeDatabase("testdb");
152 }
153
154 void tst_QHelpEngineCore::namespaceName()
155 {
156 QCOMPARE(QHelpEngineCore::namespaceName(m_path + "/data/qmake-3.3.8.qch"),
157 QString("trolltech.com.3-3-8.qmake"));
158 QCOMPARE(QHelpEngineCore::namespaceName(m_path + "/data/linguist-3.3.8.qch"),
159 QString("trolltech.com.3-3-8.linguist"));
160 }
161
162 void tst_QHelpEngineCore::registeredDocumentations()
163 {
164 QHelpEngineCore help(m_colFile, 0);
165 QCOMPARE(help.setupData(), true);
166 const QStringList docs = help.registeredDocumentations();
167 QCOMPARE(docs.size(), 3);
168 QStringList lst;
169 lst << "trolltech.com.3-3-8.qmake" << "trolltech.com.4-3-0.qmake"
170 << "trolltech.com.1.0.0.test";
171 for (const QString &s : docs)
172 lst.removeAll(s);
173 QCOMPARE(lst.isEmpty(), true);
174 }
175
176 void tst_QHelpEngineCore::registerDocumentation()
177 {
178 if (QFile::exists(m_colFile))
179 QDir::current().remove(m_colFile);
180 {
181 QHelpEngineCore c(m_colFile);
182 c.setReadOnly(false);
183 QCOMPARE(c.setupData(), true);
184 c.registerDocumentation(m_path + "/data/qmake-3.3.8.qch");
185 QCOMPARE(c.registeredDocumentations().size(), 1);
186 c.registerDocumentation(m_path + "/data/qmake-3.3.8.qch");
187 QCOMPARE(c.registeredDocumentations().size(), 1);
188 c.registerDocumentation(m_path + "/data/linguist-3.3.8.qch");
189 QCOMPARE(c.registeredDocumentations().size(), 2);
190 }
191
192 {
193 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "testdb");
194 db.setDatabaseName(m_colFile);
195 if (!db.open()) {
196 QSqlDatabase::removeDatabase("testdb");
197 QFAIL("Created database seems to be corrupt!");
198 }
199 QSqlQuery query(db);
200 query.exec("SELECT FilePath FROM NamespaceTable WHERE "
201 "Name=\'trolltech.com.3-3-8.linguist\'");
202 if (query.next())
203 QCOMPARE(query.value(0).toString(),
204 QString("linguist-3.3.8.qch"));
205 else
206 QFAIL("Query error!");
207 }
208 QSqlDatabase::removeDatabase("testdb");
209 }
210
211 void tst_QHelpEngineCore::unregisterDocumentation()
212 {
213 QHelpEngineCore c(m_colFile);
214 c.setReadOnly(false);
215 QCOMPARE(c.setupData(), true);
216 QCOMPARE(c.registeredDocumentations().size(), 3);
217 c.unregisterDocumentation("trolltech.com.3-3-8.qmake");
218 QCOMPARE(c.registeredDocumentations().size(), 2);
219 QCOMPARE(c.unregisterDocumentation("noexisting"), false);
220 }
221
222 void tst_QHelpEngineCore::documentationFileName()
223 {
224 QHelpEngineCore c(m_colFile);
225 QCOMPARE(c.setupData(), true);
226 QCOMPARE(c.documentationFileName(QLatin1String("trolltech.com.3-3-8.qmake")),
227 QString(m_path + "/data/qmake-3.3.8.qch"));
228 QCOMPARE(c.documentationFileName(QLatin1String("trolltech.com.1.0.0.test")),
229 QString(m_path + "/data/test.qch"));
230 QCOMPARE(c.documentationFileName(QLatin1String("trolltech.com.empty")),
231 QString());
232 }
233
234 void tst_QHelpEngineCore::customFilters()
235 {
236 QHelpEngineCore help(m_colFile, 0);
237 QCOMPARE(help.setupData(), true);
238 const QStringList custom = help.customFilters();
239 QCOMPARE(custom.size(), 4);
240 QStringList lst;
241 lst << "qmake Manual" << "Custom Filter 1"
242 << "Custom Filter 2" << "unfiltered";
243 for (const QString &s : custom)
244 lst.removeAll(s);
245 QCOMPARE(lst.size(), 0);
246 }
247
248 void tst_QHelpEngineCore::removeCustomFilter()
249 {
250 QHelpEngineCore help(m_colFile, 0);
251 QCOMPARE(help.setupData(), true);
252 help.removeCustomFilter("Custom Filter 1");
253 QStringList custom = help.customFilters();
254 QCOMPARE(custom.size(), 3);
255 QCOMPARE((bool)custom.contains("Custom Filter 1"), false);
256 }
257
258 void tst_QHelpEngineCore::addCustomFilter()
259 {
260 QHelpEngineCore help(m_colFile, 0);
261 QCOMPARE(help.setupData(), true);
262 help.addCustomFilter("Qt Tools", QStringList() << "tools" << "qt");
263 QStringList custom = help.customFilters();
264 QCOMPARE(custom.size(), 5);
265 QCOMPARE((bool)custom.contains("Qt Tools"), true);
266 }
267
268 void tst_QHelpEngineCore::filterAttributes()
269 {
270 QHelpEngineCore help(m_colFile, 0);
271 QCOMPARE(help.setupData(), true);
272 const QStringList atts = help.filterAttributes("qmake Manual");
273 QCOMPARE(atts.size(), 3);
274 QStringList lst;
275 lst << "qmake" << "tools" << "qt";
276 for (const QString &s : atts)
277 lst.removeAll(s);
278 QCOMPARE(lst.size(), 0);
279 }
280
281 void tst_QHelpEngineCore::currentFilter()
282 {
283 QHelpEngineCore help(m_colFile, 0);
284 QCOMPARE(help.setupData(), true);
285 QCOMPARE(help.currentFilter(), QString("unfiltered"));
286 }
287
288 void tst_QHelpEngineCore::setCurrentFilter()
289 {
290 QHelpEngineCore help(m_colFile, 0);
291 QCOMPARE(help.setupData(), true);
292 QCOMPARE(help.currentFilter(), QString("unfiltered"));
293 help.setCurrentFilter("qmake Manual");
294 QCOMPARE(help.currentFilter(), QString("qmake Manual"));
295 QCOMPARE(help.customValue("CurrentFilter").toString(),
296 QString("qmake Manual"));
297 }
298
299 void tst_QHelpEngineCore::filterAttributeSets()
300 {
301 QHelpEngineCore help(m_colFile, 0);
302 help.setReadOnly(false);
303 QCOMPARE(help.setupData(), true);
304 QList<QStringList> lst = help.filterAttributeSets("trolltech.com.1.0.0.test");
305 QCOMPARE(lst.size(), 2);
306 QCOMPARE(lst.first().size(), 2);
307 QCOMPARE((bool)lst.first().contains("filter1"), true);
308 QCOMPARE((bool)lst.last().contains("filter2"), true);
309 }
310
311 void tst_QHelpEngineCore::files()
312 {
313 QHelpEngineCore help(m_colFile, 0);
314 help.setReadOnly(false);
315 QCOMPARE(help.setupData(), true);
316 QList<QUrl> lst = help.files("trolltech.com.4-3-0.qmake",
317 QStringList());
318 QCOMPARE(lst.size(), 16);
319 lst = help.files("trolltech.com.4-3-0.qmake",
320 QStringList(), "png");
321 QCOMPARE(lst.size(), 2);
322 lst = help.files("trolltech.com.4-3-0.qmake",
323 QStringList() << "qt", "html");
324 QCOMPARE(lst.size(), 13);
325 lst = help.files("trolltech.com.4-3-0.qmake",
326 QStringList() << "qt" << "qmake", "html");
327 QCOMPARE(lst.size(), 13);
328 lst = help.files("trolltech.com.4-3-0.qmake",
329 QStringList() << "qt" << "qmake" << "bla", "html");
330 QCOMPARE(lst.size(), 0);
331 lst = help.files("trolltech.com.4-3-0.qmake",
332 QStringList() << "qt" << "qmake", "foo");
333
334 // print 'lst' if test fails:
335 auto printRemainder = qScopeGuard([&]{ for (const QUrl &url : lst) qDebug() << url; });
336
337 QCOMPARE(lst.size(), 0);
338
339 printRemainder.dismiss();
340 }
341
342 void tst_QHelpEngineCore::fileData()
343 {
344 QHelpEngineCore help(m_colFile, 0);
345 help.setReadOnly(false);
346 QCOMPARE(help.setupData(), true);
347 QByteArray ba = help.fileData(QUrl("NotExisting"));
348 QCOMPARE(ba.size(), 0);
349 ba = help.fileData(QUrl("qthelp://trolltech.com.1.0.0.test/testFolder/test.html"));
350 QTextStream s(ba, QIODevice::ReadOnly|QIODevice::Text);
351 QFile f(m_path + "/data/test.html");
352 if (!f.open(QIODevice::ReadOnly|QIODevice::Text))
353 QFAIL("Cannot open original file!");
354 QTextStream ts(&f);
355 QCOMPARE(s.readAll(), ts.readAll());
356 }
357
358 void tst_QHelpEngineCore::customValue()
359 {
360 QHelpEngineCore help(m_colFile, 0);
361 QCOMPARE(help.setupData(), true);
362 QCOMPARE(help.customValue("CurrentFilter").toString(),
363 QString("unfiltered"));
364 }
365
366 void tst_QHelpEngineCore::setCustomValue()
367 {
368 QHelpEngineCore help(m_colFile, 0);
369 QCOMPARE(help.setupData(), true);
370 QCOMPARE(help.setCustomValue("Test", 3), true);
371 QCOMPARE(help.customValue("Test").toInt(), 3);
372 QCOMPARE(help.removeCustomValue("Test"), true);
373 QCOMPARE(help.customValue("Test"), QVariant());
374 }
375
376 void tst_QHelpEngineCore::removeCustomValue()
377 {
378 setCustomValue();
379 }
380
381 void tst_QHelpEngineCore::setAutoSaveFilter()
382 {
383 QHelpEngineCore help(m_colFile, 0);
384 QCOMPARE(help.setupData(), true);
385 QCOMPARE(help.currentFilter(), QString("unfiltered"));
386
387 help.setAutoSaveFilter(false);
388 help.setCurrentFilter("qmake Manual");
389 QCOMPARE(help.currentFilter(), QString("qmake Manual"));
390 QCOMPARE(help.customValue("CurrentFilter").toString(),
391 QString("unfiltered"));
392 }
393
394 void tst_QHelpEngineCore::metaData()
395 {
396 QCOMPARE(QHelpEngineCore::metaData(m_path + "/data/test.qch", "author").toString(),
397 QString("Digia Plc and/or its subsidiary(-ies)"));
398 QCOMPARE(QHelpEngineCore::metaData(m_path + "/data/test.qch", "notExisting").isValid(),
399 false);
400 }
401
402 QTEST_MAIN(tst_QHelpEngineCore)
403 #include "tst_qhelpenginecore.moc"