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