1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <QtTest/QtTest>
42 
43 #ifndef QT_NO_BUILD_TOOLS
44 
45 #include <QtCore/QUrl>
46 #include <QtCore/QFileInfo>
47 #include <QtSql/QSqlDatabase>
48 #include <QtSql/QSqlQuery>
49 
50 #include <QtHelp/QHelpEngineCore>
51 
52 class tst_QHelpEngineCore : public QObject
53 {
54     Q_OBJECT
55 
56 private slots:
57     void init();
58 
59     void setupData();
60     void collectionFile();
61     void setCollectionFile();
62     void copyCollectionFile();
63 
64     void namespaceName();
65     void registeredDocumentations();
66     void registerDocumentation();
67     void unregisterDocumentation();
68     void documentationFileName();
69 
70     void customFilters();
71     void removeCustomFilter();
72     void addCustomFilter();
73     void filterAttributes();
74     void currentFilter();
75     void setCurrentFilter();
76 
77     void filterAttributeSets();
78     void files();
79     void fileData();
80 
81     void linksForIdentifier();
82 
83     void customValue();
84     void setCustomValue();
85     void removeCustomValue();
86 
87     void setAutoSaveFilter();
88 
89     void metaData();
90 
91 private:
92     QString m_path;
93     QString m_colFile;
94 };
95 
init()96 void tst_QHelpEngineCore::init()
97 {
98     // defined in profile
99     m_path = QLatin1String(SRCDIR);
100 
101     m_path = QFileInfo(m_path).absoluteFilePath();
102 
103     m_colFile = m_path + QLatin1String("/data/col.qhc");
104     if (QFile::exists(m_colFile))
105         QDir::current().remove(m_colFile);
106     if (!QFile::copy(m_path + "/data/collection.qhc", m_colFile))
107         QFAIL("Cannot copy file!");
108     QFile f(m_colFile);
109     f.setPermissions(QFile::WriteUser|QFile::ReadUser);
110 }
111 
setupData()112 void tst_QHelpEngineCore::setupData()
113 {
114     QHelpEngineCore help(m_colFile, 0);
115     QCOMPARE(help.setupData(), true);
116 }
117 
collectionFile()118 void tst_QHelpEngineCore::collectionFile()
119 {
120     QHelpEngineCore help(m_colFile, 0);
121     QCOMPARE(help.collectionFile(), QFileInfo(m_colFile).absoluteFilePath());
122 }
123 
setCollectionFile()124 void tst_QHelpEngineCore::setCollectionFile()
125 {
126     QHelpEngineCore help(m_colFile, 0);
127     QCOMPARE(help.collectionFile(), QFileInfo(m_colFile).absoluteFilePath());
128 
129     QString col1File = m_path + QLatin1String("/data/collection1.qhc");
130     help.setCollectionFile(col1File);
131     QCOMPARE(help.collectionFile(), QFileInfo(col1File).absoluteFilePath());
132 
133     QStringList docs = help.registeredDocumentations();
134     QCOMPARE(docs.count(), 1);
135     QCOMPARE(docs.first(), QLatin1String("trolltech.com.1-0-0.test"));
136 }
137 
copyCollectionFile()138 void tst_QHelpEngineCore::copyCollectionFile()
139 {
140     QHelpEngineCore help(m_colFile, 0);
141     QCOMPARE(help.collectionFile(), QFileInfo(m_colFile).absoluteFilePath());
142 
143     QString copiedFile = m_path + QLatin1String("/collectionCopy.qhc");
144     if (QFile::exists(copiedFile))
145         QDir::current().remove(copiedFile);
146 
147     QCOMPARE(help.copyCollectionFile(copiedFile), true);
148 
149     {
150         QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "testdb");
151         db.setDatabaseName(copiedFile);
152         if (!db.open()) {
153             QSqlDatabase::removeDatabase("testdb");
154             QFAIL("Created database seems to be corrupt!");
155         }
156         QSqlQuery *m_query = new QSqlQuery(db);
157 
158         m_query->exec("SELECT Key, Value FROM SettingsTable");
159         if (m_query->next()) {
160             QCOMPARE(m_query->value(0).toString(), QString("CurrentFilter"));
161             QCOMPARE(m_query->value(1).toString(), QString("unfiltered"));
162         } else {
163             QFAIL("Settingstable is corrupt!");
164         }
165 
166         m_query->exec("SELECT NameId, FilterAttributeId FROM FilterTable");
167         int i = 0;
168         while (m_query->next()) {
169             if (i == 3) {
170                 QCOMPARE(m_query->value(0).toInt(), 2);
171                 QCOMPARE(m_query->value(1).toInt(), 6);
172             }
173             ++i;
174         }
175         QCOMPARE(i, 7);
176         m_query->clear();
177 
178         m_query->exec("SELECT Name, FilePath FROM NamespaceTable");
179         i = 0;
180         while (m_query->next()) {
181             if (i == 0) {
182                 QCOMPARE(m_query->value(0).toString(), QString("trolltech.com.3-3-8.qmake"));
183                 QCOMPARE(m_query->value(1).toString(), QString("data/qmake-3.3.8.qch"));
184             }
185             ++i;
186         }
187         QCOMPARE(i, 3);
188 
189         m_query->clear();
190         delete m_query;
191     }
192     QSqlDatabase::removeDatabase("testdb");
193 }
194 
namespaceName()195 void tst_QHelpEngineCore::namespaceName()
196 {
197     QCOMPARE(QHelpEngineCore::namespaceName(m_path + "/data/qmake-3.3.8.qch"),
198         QString("trolltech.com.3-3-8.qmake"));
199     QCOMPARE(QHelpEngineCore::namespaceName(m_path + "/data/linguist-3.3.8.qch"),
200         QString("trolltech.com.3-3-8.linguist"));
201 }
202 
registeredDocumentations()203 void tst_QHelpEngineCore::registeredDocumentations()
204 {
205     QHelpEngineCore help(m_colFile, 0);
206     QCOMPARE(help.setupData(), true);
207     QStringList docs = help.registeredDocumentations();
208     QCOMPARE(docs.count(), 3);
209     QStringList lst;
210     lst << "trolltech.com.3-3-8.qmake" << "trolltech.com.4-3-0.qmake"
211         << "trolltech.com.1-0-0.test";
212     foreach (QString s, docs)
213         lst.removeAll(s);
214     QCOMPARE(lst.isEmpty(), true);
215 }
216 
registerDocumentation()217 void tst_QHelpEngineCore::registerDocumentation()
218 {
219     if (QFile::exists(m_colFile))
220         QDir::current().remove(m_colFile);
221     {
222         QHelpEngineCore c(m_colFile);
223         QCOMPARE(c.setupData(), true);
224         c.registerDocumentation(m_path + "/data/qmake-3.3.8.qch");
225         QCOMPARE(c.registeredDocumentations().count(), 1);
226         c.registerDocumentation(m_path + "/data/qmake-3.3.8.qch");
227         QCOMPARE(c.registeredDocumentations().count(), 1);
228         c.registerDocumentation(m_path + "/data/linguist-3.3.8.qch");
229         QCOMPARE(c.registeredDocumentations().count(), 2);
230     }
231 
232     {
233         QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "testdb");
234         db.setDatabaseName(m_colFile);
235         if (!db.open()) {
236             QSqlDatabase::removeDatabase("testdb");
237             QFAIL("Created database seems to be corrupt!");
238         }
239         QSqlQuery query(db);
240         query.exec("SELECT FilePath FROM NamespaceTable WHERE "
241             "Name=\'trolltech.com.3-3-8.linguist\'");
242         if (query.next())
243             QCOMPARE(query.value(0).toString(),
244                 QString("linguist-3.3.8.qch"));
245         else
246             QFAIL("Query error!");
247     }
248     QSqlDatabase::removeDatabase("testdb");
249 }
250 
unregisterDocumentation()251 void tst_QHelpEngineCore::unregisterDocumentation()
252 {
253     QHelpEngineCore c(m_colFile);
254     QCOMPARE(c.setupData(), true);
255     QCOMPARE(c.registeredDocumentations().count(), 3);
256     c.unregisterDocumentation("trolltech.com.3-3-8.qmake");
257     QCOMPARE(c.registeredDocumentations().count(), 2);
258     QCOMPARE(c.unregisterDocumentation("noexisting"), false);
259 }
260 
documentationFileName()261 void tst_QHelpEngineCore::documentationFileName()
262 {
263     QHelpEngineCore c(m_colFile);
264     QCOMPARE(c.setupData(), true);
265     QCOMPARE(c.documentationFileName(QLatin1String("trolltech.com.3-3-8.qmake")),
266         QString(m_path + "/data/qmake-3.3.8.qch"));
267     QCOMPARE(c.documentationFileName(QLatin1String("trolltech.com.1-0-0.test")),
268         QString(m_path + "/data/test.qch"));
269     QCOMPARE(c.documentationFileName(QLatin1String("trolltech.com.empty")),
270         QString());
271 }
272 
customFilters()273 void tst_QHelpEngineCore::customFilters()
274 {
275     QHelpEngineCore help(m_colFile, 0);
276     QCOMPARE(help.setupData(), true);
277     QStringList custom = help.customFilters();
278     QCOMPARE(custom.count(), 4);
279     QStringList lst;
280     lst << "qmake Manual" << "Custom Filter 1"
281         << "Custom Filter 2" << "unfiltered";
282     foreach (QString s, custom)
283         lst.removeAll(s);
284     QCOMPARE(lst.count(), 0);
285 }
286 
removeCustomFilter()287 void tst_QHelpEngineCore::removeCustomFilter()
288 {
289     QHelpEngineCore help(m_colFile, 0);
290     QCOMPARE(help.setupData(), true);
291     help.removeCustomFilter("Custom Filter 1");
292     QStringList custom = help.customFilters();
293     QCOMPARE(custom.count(), 3);
294     QCOMPARE((bool)custom.contains("Custom Filter 1"), false);
295 }
296 
addCustomFilter()297 void tst_QHelpEngineCore::addCustomFilter()
298 {
299     QHelpEngineCore help(m_colFile, 0);
300     QCOMPARE(help.setupData(), true);
301     help.addCustomFilter("Qt Tools", QStringList() << "tools" << "qt");
302     QStringList custom = help.customFilters();
303     QCOMPARE(custom.count(), 5);
304     QCOMPARE((bool)custom.contains("Qt Tools"), true);
305 }
306 
filterAttributes()307 void tst_QHelpEngineCore::filterAttributes()
308 {
309     QHelpEngineCore help(m_colFile, 0);
310     QCOMPARE(help.setupData(), true);
311     QStringList atts = help.filterAttributes("qmake Manual");
312     QCOMPARE(atts.count(), 3);
313     QStringList lst;
314     lst << "qmake" << "tools" << "qt";
315     foreach (QString s, atts)
316         lst.removeAll(s);
317     QCOMPARE(lst.count(), 0);
318 }
319 
currentFilter()320 void tst_QHelpEngineCore::currentFilter()
321 {
322     QHelpEngineCore help(m_colFile, 0);
323     QCOMPARE(help.setupData(), true);
324     QCOMPARE(help.currentFilter(), QString("unfiltered"));
325 }
326 
setCurrentFilter()327 void tst_QHelpEngineCore::setCurrentFilter()
328 {
329     QHelpEngineCore help(m_colFile, 0);
330     QCOMPARE(help.setupData(), true);
331     QCOMPARE(help.currentFilter(), QString("unfiltered"));
332     help.setCurrentFilter("qmake Manual");
333     QCOMPARE(help.currentFilter(), QString("qmake Manual"));
334     QCOMPARE(help.customValue("CurrentFilter").toString(),
335         QString("qmake Manual"));
336 }
337 
filterAttributeSets()338 void tst_QHelpEngineCore::filterAttributeSets()
339 {
340     QHelpEngineCore help(m_colFile, 0);
341     QCOMPARE(help.setupData(), true);
342     QList<QStringList> lst = help.filterAttributeSets("trolltech.com.1-0-0.test");
343     QCOMPARE(lst.count(), 2);
344     QCOMPARE(lst.first().count(), 2);
345     QCOMPARE((bool)lst.first().contains("filter1"), true);
346     QCOMPARE((bool)lst.last().contains("filter2"), true);
347 }
348 
files()349 void tst_QHelpEngineCore::files()
350 {
351     QHelpEngineCore help(m_colFile, 0);
352     QCOMPARE(help.setupData(), true);
353     QList<QUrl> lst = help.files("trolltech.com.4-3-0.qmake",
354         QStringList());
355     QCOMPARE(lst.count(), 16);
356     lst = help.files("trolltech.com.4-3-0.qmake",
357         QStringList(), "png");
358     QCOMPARE(lst.count(), 2);
359     lst = help.files("trolltech.com.4-3-0.qmake",
360         QStringList() << "qt", "html");
361     QCOMPARE(lst.count(), 13);
362     lst = help.files("trolltech.com.4-3-0.qmake",
363         QStringList() << "qt" << "qmake", "html");
364     QCOMPARE(lst.count(), 13);
365     lst = help.files("trolltech.com.4-3-0.qmake",
366         QStringList() << "qt" << "qmake" << "bla", "html");
367     QCOMPARE(lst.count(), 0);
368     lst = help.files("trolltech.com.4-3-0.qmake",
369         QStringList() << "qt" << "qmake", "foo");
370     QCOMPARE(lst.count(), 0);
371 
372     foreach (QUrl url, lst)
373         qDebug() << url;
374 
375 }
376 
fileData()377 void tst_QHelpEngineCore::fileData()
378 {
379     QHelpEngineCore help(m_colFile, 0);
380     QCOMPARE(help.setupData(), true);
381     QByteArray ba = help.fileData(QUrl("NotExisting"));
382     QCOMPARE(ba.size(), 0);
383     ba = help.fileData(QUrl("qthelp://trolltech.com.1-0-0.test/testFolder/test.html"));
384     QTextStream s(ba, QIODevice::ReadOnly|QIODevice::Text);
385     QFile f(m_path + "/data/test.html");
386     if (!f.open(QIODevice::ReadOnly|QIODevice::Text))
387         QFAIL("Cannot open original file!");
388     QTextStream ts(&f);
389     QCOMPARE(s.readAll(), ts.readAll());
390 }
391 
linksForIdentifier()392 void tst_QHelpEngineCore::linksForIdentifier()
393 {
394     QHelpEngineCore help(m_colFile, 0);
395     QCOMPARE(help.setupData(), true);
396     QMap<QString, QUrl> map;
397     map = help.linksForIdentifier("Test::foo");
398     QCOMPARE(map.contains("Test Manual"), true);
399     QCOMPARE(map.count(), 1);
400     QCOMPARE(map.value("Test Manual"),
401         QUrl("qthelp://trolltech.com.1-0-0.test/testFolder/test.html#foo"));
402 
403     help.setCurrentFilter("Custom Filter 2");
404     map = help.linksForIdentifier("People::newton");
405     QCOMPARE(map.isEmpty(), true);
406     map = help.linksForIdentifier("Fancy::foobar");
407     QCOMPARE(map.contains("Fancy"), true);
408     QCOMPARE(map.count(), 1);
409     QCOMPARE(map.value("Fancy"),
410         QUrl("qthelp://trolltech.com.1-0-0.test/testFolder/fancy.html#foobar"));
411 }
412 
customValue()413 void tst_QHelpEngineCore::customValue()
414 {
415     QHelpEngineCore help(m_colFile, 0);
416     QCOMPARE(help.setupData(), true);
417     QCOMPARE(help.customValue("CurrentFilter").toString(),
418         QString("unfiltered"));
419 }
420 
setCustomValue()421 void tst_QHelpEngineCore::setCustomValue()
422 {
423     QHelpEngineCore help(m_colFile, 0);
424     QCOMPARE(help.setupData(), true);
425     QCOMPARE(help.setCustomValue("Test", 3), true);
426     QCOMPARE(help.customValue("Test").toInt(), 3);
427     QCOMPARE(help.removeCustomValue("Test"), true);
428     QCOMPARE(help.customValue("Test"), QVariant());
429 }
430 
removeCustomValue()431 void tst_QHelpEngineCore::removeCustomValue()
432 {
433     setCustomValue();
434 }
435 
setAutoSaveFilter()436 void tst_QHelpEngineCore::setAutoSaveFilter()
437 {
438     QHelpEngineCore help(m_colFile, 0);
439     QCOMPARE(help.setupData(), true);
440     QCOMPARE(help.currentFilter(), QString("unfiltered"));
441 
442     help.setAutoSaveFilter(false);
443     help.setCurrentFilter("qmake Manual");
444     QCOMPARE(help.currentFilter(), QString("qmake Manual"));
445     QCOMPARE(help.customValue("CurrentFilter").toString(),
446         QString("unfiltered"));
447 }
448 
metaData()449 void tst_QHelpEngineCore::metaData()
450 {
451     QCOMPARE(QHelpEngineCore::metaData(m_path + "/data/test.qch", "author").toString(),
452         QString("Nokia Corporation and/or its subsidiary(-ies)"));
453     QCOMPARE(QHelpEngineCore::metaData(m_path + "/data/test.qch", "notExisting").isValid(),
454         false);
455 }
456 
457 QTEST_MAIN(tst_QHelpEngineCore)
458 #include "tst_qhelpenginecore.moc"
459 
460 #else // QT_NO_BUILD_TOOLS
461 QTEST_NOOP_MAIN
462 #endif
463