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/QFileInfo> |
31 | #include <QtSql/QSqlDatabase> |
32 | #include <QtSql/QSqlQuery> |
33 | |
34 | #include "../../../src/assistant/qhelpgenerator/qhelpprojectdata_p.h" |
35 | #include "../../../src/assistant/qhelpgenerator/helpgenerator.h" |
36 | |
37 | class tst_QHelpGenerator : public QObject |
38 | { |
39 | Q_OBJECT |
40 | |
41 | private slots: |
42 | void initTestCase(); |
43 | void generateHelp(); |
44 | // Check that two runs of the generator creates the same file twice |
45 | void generateTwice(); |
46 | |
47 | private: |
48 | void checkNamespace(); |
49 | void checkFilters(); |
50 | void checkIndices(); |
51 | void checkFiles(); |
52 | void checkMetaData(); |
53 | |
54 | QString m_outputFile; |
55 | QSqlQuery *m_query; |
56 | }; |
57 | |
58 | void tst_QHelpGenerator::initTestCase() |
59 | { |
60 | QString path = QLatin1String(SRCDIR); |
61 | m_outputFile = path + QLatin1String("/data/test.qch" ); |
62 | if (QFile::exists(fileName: m_outputFile)) { |
63 | QDir d; |
64 | if (!d.remove(fileName: m_outputFile)) |
65 | QFAIL("Cannot remove old output file!" ); |
66 | } |
67 | } |
68 | |
69 | void tst_QHelpGenerator::generateHelp() |
70 | { |
71 | // defined in profile |
72 | QString path = QLatin1String(SRCDIR); |
73 | |
74 | QString inputFile(path + "/data/test.qhp" ); |
75 | QHelpProjectData data; |
76 | if (!data.readData(fileName: inputFile)) |
77 | QFAIL("Cannot read qthp file!" ); |
78 | |
79 | HelpGenerator generator; |
80 | QCOMPARE(generator.generate(&data, m_outputFile), true); |
81 | |
82 | { |
83 | QSqlDatabase db = QSqlDatabase::addDatabase(type: "QSQLITE" , connectionName: "testdb" ); |
84 | db.setDatabaseName(m_outputFile); |
85 | if (!db.open()) { |
86 | QSqlDatabase::removeDatabase(connectionName: "testdb" ); |
87 | QFAIL("Created database seems to be corrupt!" ); |
88 | } |
89 | m_query = new QSqlQuery(db); |
90 | checkNamespace(); |
91 | checkFilters(); |
92 | checkIndices(); |
93 | checkFiles(); |
94 | checkMetaData(); |
95 | |
96 | m_query->clear(); |
97 | delete m_query; |
98 | } |
99 | QSqlDatabase::removeDatabase(connectionName: "testdb" ); |
100 | |
101 | // check if db is still in use... |
102 | initTestCase(); |
103 | } |
104 | |
105 | void tst_QHelpGenerator::checkNamespace() |
106 | { |
107 | m_query->exec(query: "SELECT Id, Name FROM NamespaceTable" ); |
108 | if (m_query->next() |
109 | && m_query->value(i: 1).toString() == QLatin1String("trolltech.com.1.0.0.test" )) |
110 | return; |
111 | QFAIL("Namespace Error!" ); |
112 | } |
113 | |
114 | void tst_QHelpGenerator::checkFilters() |
115 | { |
116 | m_query->exec(query: "SELECT COUNT(Id) FROM FilterAttributeTable" ); |
117 | if (!m_query->next() || m_query->value(i: 0).toInt() != 3) |
118 | QFAIL("FilterAttribute Error!" ); |
119 | |
120 | m_query->exec(query: "SELECT a.Name FROM FilterAttributeTable a, FilterTable b, " |
121 | "FilterNameTable c WHERE c.Id=b.NameId AND b.FilterAttributeID=a.Id " |
122 | "AND c.Name=\'Custom Filter 2\'" ); |
123 | QStringList lst; |
124 | while (m_query->next()) |
125 | lst.append(t: m_query->value(i: 0).toString()); |
126 | if (!lst.contains(str: "test" ) || !lst.contains(str: "filter2" )) |
127 | QFAIL("FilterAttribute Error!" ); |
128 | } |
129 | |
130 | void tst_QHelpGenerator::checkIndices() |
131 | { |
132 | m_query->exec(query: "SELECT a.Name, b.Anchor FROM FileNameTable a, " |
133 | "IndexTable b, IndexFilterTable c, FilterAttributeTable d " |
134 | "WHERE a.FileID=b.FileId AND b.Id=c.IndexId " |
135 | "AND c.FilterAttributeId=d.Id AND d.Name=\'filter1\' AND b.Name=\'foo\'" ); |
136 | if (!m_query->next() || m_query->value(i: 0).toString() != QLatin1String("test.html" ) |
137 | || m_query->value(i: 1).toString() != QLatin1String("foo" )) |
138 | QFAIL("Index Error!" ); |
139 | |
140 | m_query->exec(query: "SELECT COUNT(a.Id) FROM IndexTable a, " |
141 | "IndexFilterTable b, FilterAttributeTable c WHERE a.Id=b.IndexId " |
142 | "AND b.FilterAttributeId=c.Id AND c.Name=\'filter2\'" ); |
143 | if (!m_query->next() || m_query->value(i: 0).toInt() != 3) |
144 | QFAIL("Index Error!" ); |
145 | } |
146 | |
147 | void tst_QHelpGenerator::checkFiles() |
148 | { |
149 | m_query->exec(query: "SELECT COUNT(a.FileId) FROM FileNameTable a, FolderTable b " |
150 | "WHERE a.FolderId=b.Id AND b.Name=\'testFolder\'" ); |
151 | if (!m_query->next() || m_query->value(i: 0).toInt() != 6) |
152 | QFAIL("File Error!" ); |
153 | |
154 | QStringList lst; |
155 | lst << "classic.css" << "test.html" << "people.html" << "sub/about.html" ; |
156 | m_query->exec(query: "SELECT a.Name FROM FileNameTable a, FileFilterTable b, " |
157 | "FilterAttributeTable c WHERE c.Id=b.FilterAttributeId " |
158 | "AND b.FileId=a.FileID AND c.Name=\'filter1\'" ); |
159 | while (m_query->next()) |
160 | lst.removeAll(t: m_query->value(i: 0).toString()); |
161 | QCOMPARE(lst.count(), 0); |
162 | |
163 | QMap<int, QStringList> fileAtts; |
164 | m_query->exec(query: "SELECT a.Id, b.Name FROM FileAttributeSetTable a, " |
165 | "FilterAttributeTable b WHERE a.FilterAttributeId=b.Id" ); |
166 | while (m_query->next()) { |
167 | int id = m_query->value(i: 0).toInt(); |
168 | if (!fileAtts.contains(akey: id)) |
169 | fileAtts.insert(akey: id, avalue: QStringList()); |
170 | fileAtts[id].append(t: m_query->value(i: 1).toString()); |
171 | } |
172 | QCOMPARE(fileAtts.count(), 2); |
173 | QVERIFY(fileAtts.value(1).contains("test" )); |
174 | QVERIFY(fileAtts.value(1).contains("filter1" )); |
175 | QVERIFY(!fileAtts.value(1).contains("filter2" )); |
176 | QVERIFY(fileAtts.value(2).contains("test" )); |
177 | QVERIFY(fileAtts.value(2).contains("filter2" )); |
178 | } |
179 | |
180 | void tst_QHelpGenerator::checkMetaData() |
181 | { |
182 | m_query->exec(query: "SELECT COUNT(Value) FROM MetaDataTable" ); |
183 | if (!m_query->next()) |
184 | QFAIL("Meta Data Error" ); |
185 | QCOMPARE(m_query->value(0).toInt(), 3); |
186 | |
187 | m_query->exec(query: "SELECT Value FROM MetaDataTable WHERE Name=\'author\'" ); |
188 | if (!m_query->next()) |
189 | QFAIL("Meta Data Error" ); |
190 | QCOMPARE(m_query->value(0).toString(), QString("Digia Plc and/or its subsidiary(-ies)" )); |
191 | |
192 | } |
193 | |
194 | void tst_QHelpGenerator::generateTwice() |
195 | { |
196 | // defined in profile |
197 | QString path = QLatin1String(SRCDIR); |
198 | |
199 | QString inputFile(path + "/data/test.qhp" ); |
200 | QHelpProjectData data; |
201 | if (!data.readData(fileName: inputFile)) |
202 | QFAIL("Cannot read qhp file!" ); |
203 | |
204 | HelpGenerator generator1; |
205 | HelpGenerator generator2; |
206 | QString outputFile1 = path + QLatin1String("/data/test1.qch" ); |
207 | QString outputFile2 = path + QLatin1String("/data/test2.qch" ); |
208 | QCOMPARE(generator1.generate(&data, outputFile1), true); |
209 | QCOMPARE(generator2.generate(&data, outputFile2), true); |
210 | |
211 | QFile f1(outputFile1); |
212 | QFile f2(outputFile2); |
213 | QVERIFY(f1.open(QIODevice::ReadOnly)); |
214 | QVERIFY(f2.open(QIODevice::ReadOnly)); |
215 | |
216 | QByteArray arr1 = f1.readAll(); |
217 | QByteArray arr2 = f2.readAll(); |
218 | |
219 | QFile::remove(fileName: outputFile1); |
220 | QFile::remove(fileName: outputFile2); |
221 | QCOMPARE(arr1, arr2); |
222 | } |
223 | |
224 | QTEST_MAIN(tst_QHelpGenerator) |
225 | #include "tst_qhelpgenerator.moc" |
226 | |