diff --git a/generators/chm/CMakeLists.txt b/generators/chm/CMakeLists.txt index 22015955d..83abe4e24 100644 --- a/generators/chm/CMakeLists.txt +++ b/generators/chm/CMakeLists.txt @@ -22,6 +22,16 @@ set(okularGenerator_chmlib_SRCS okular_add_generator(okularGenerator_chmlib ${okularGenerator_chmlib_SRCS}) target_link_libraries(okularGenerator_chmlib okularcore ${CHM_LIBRARY} KF5::KHtml) +########### autotests ############### + +add_definitions( -DKDESRCDIR="${CMAKE_CURRENT_SOURCE_DIR}/" ) +ecm_add_test(autotests/chmgeneratortest.cpp + TEST_NAME "chmgeneratortest" + LINK_LIBRARIES Qt5::Test KF5::CoreAddons okularcore +) + +target_compile_definitions(chmgeneratortest PRIVATE -DGENERATOR_PATH="$") + ########### install files ############### install( FILES okularChm.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR} ) install( PROGRAMS okularApplication_chm.desktop org.kde.mobile.okular_chm.desktop DESTINATION ${KDE_INSTALL_APPDIR} ) diff --git a/generators/chm/autotests/chmgeneratortest.cpp b/generators/chm/autotests/chmgeneratortest.cpp new file mode 100644 index 000000000..6b0b8efc7 --- /dev/null +++ b/generators/chm/autotests/chmgeneratortest.cpp @@ -0,0 +1,104 @@ +/*************************************************************************** + * Copyright (C) 2017 by Gilbert Assaf * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#include + +#include "core/document.h" +#include "core/page.h" +#include "settings_core.h" +#include "core/textpage.h" + + +class ChmGeneratorTest +: public QObject +{ + Q_OBJECT + + private slots: + void initTestCase(); + void testDocumentStructure(); + void testDocumentContent(); + void cleanupTestCase(); + + private: + Okular::Document *m_document; +}; + +void ChmGeneratorTest::initTestCase() +{ + Okular::SettingsCore::instance( QStringLiteral("ChmGeneratorTest") ); + m_document = new Okular::Document( 0 ); + const QString testFile = QStringLiteral(KDESRCDIR "autotests/data/test.chm"); + QMimeDatabase db; + const QMimeType mime = db.mimeTypeForFile( testFile ); + QCOMPARE( m_document->openDocument(testFile, QUrl(), mime), Okular::Document::OpenSuccess ); +} + + +void ChmGeneratorTest::cleanupTestCase() +{ + m_document->closeDocument(); + delete m_document; +} + +void ChmGeneratorTest::testDocumentStructure() +{ + unsigned int expectedPageNr = 6; + QCOMPARE( m_document->pages(), expectedPageNr); + QCOMPARE( m_document->metaData(QLatin1String("DocumentTitle")).toString(), QStringLiteral("okular test chm") ); + + const Okular::DocumentSynopsis *docSyn = m_document->documentSynopsis(); + QDomElement heading1 = docSyn->documentElement(); + QCOMPARE( heading1.tagName(), QStringLiteral("Heading 1") ); + + QDomElement topic1 = heading1.firstChildElement(); + QCOMPARE( topic1.tagName(), QStringLiteral("Topic 1") ); + + QDomElement heading1_1 = topic1.nextSiblingElement(); + QCOMPARE( heading1_1.tagName(), QStringLiteral("Heading 1.1") ); + + QDomElement topic1_1 = heading1_1.firstChildElement(); + QCOMPARE( topic1_1.tagName(), QStringLiteral("Topic 1.1") ); + + QDomElement heading2 = heading1.nextSiblingElement(); + QCOMPARE( heading2.tagName(), QStringLiteral("Heading 2") ); +} + +void ChmGeneratorTest::testDocumentContent() +{ + const Okular::Page *page0 = m_document->page(0); + QCOMPARE( page0->number(), 0); + m_document->requestTextPage( page0->number() ); + QVERIFY( page0->hasTextPage() ); + QCOMPARE( page0->text(), QStringLiteral("Heading 1This is an example Text.") ); + + const Okular::Page *page1 = m_document->page(1); + QCOMPARE( page1->number(), 1); + m_document->requestTextPage( page1->number() ); + QVERIFY( page1->hasTextPage() ); + QCOMPARE( page1->text(), QStringLiteral("Topic 1This is an example Text.") ); + + const Okular::Page *page2 = m_document->page(2); + QCOMPARE( page2->number(), 2); + m_document->requestTextPage( page2->number() ); + QVERIFY( page2->hasTextPage() ); + QCOMPARE( page2->text(), QStringLiteral("Heading 1.1With html title.") ); + + //Test page, who doesn't have an TOC entry, but one in chm index + const Okular::Page *indexPage1 = m_document->page(5); + QCOMPARE( indexPage1->number(), 5); + m_document->requestTextPage( indexPage1->number() ); + QVERIFY( indexPage1->hasTextPage() ); + QCOMPARE( indexPage1->text(), QStringLiteral("Index 1This is an example Text.") ); +} + +QTEST_MAIN( ChmGeneratorTest ) +#include "chmgeneratortest.moc" + +/* kate: replace-tabs on; tab-width 4; */ diff --git a/generators/chm/autotests/data/test.chm b/generators/chm/autotests/data/test.chm new file mode 100644 index 000000000..fb566369d Binary files /dev/null and b/generators/chm/autotests/data/test.chm differ diff --git a/generators/chm/generator_chm.cpp b/generators/chm/generator_chm.cpp index 40474c7c6..b6a770ece 100644 --- a/generators/chm/generator_chm.cpp +++ b/generators/chm/generator_chm.cpp @@ -78,6 +78,9 @@ bool CHMGenerator::loadDocument( const QString & fileName, QVector< Okular::Page // fill m_docSyn QMap lastIndentElement; + QMap tmpPageList; + int pageNum = 0; + foreach(const LCHMParsedEntry &e, topics) { QDomElement item = m_docSyn.createElement(e.name); @@ -87,6 +90,11 @@ bool CHMGenerator::loadDocument( const QString & fileName, QVector< Okular::Page if (url.contains(QChar::fromLatin1('%'))) url = QString::fromUtf8(QByteArray::fromPercentEncoding(url.toUtf8())); item.setAttribute(QStringLiteral("ViewportName"), url); + if(!tmpPageList.contains(url)) + {//add a page only once + tmpPageList.insert(url, pageNum); + pageNum++; + } } item.setAttribute(QStringLiteral("Icon"), e.imageid); if (e.indent == 0) m_docSyn.appendChild(item); @@ -95,12 +103,13 @@ bool CHMGenerator::loadDocument( const QString & fileName, QVector< Okular::Page } // fill m_urlPage and m_pageUrl - int pageNum = 0; QStringList pageList; m_file->enumerateFiles(&pageList); const QString home = m_file->homeUrl(); if (home != QLatin1String("/")) pageList.prepend(home); + m_pageUrl.resize(pageNum); + foreach (const QString &url, pageList) { const QString urlLower = url.toLower(); @@ -108,15 +117,22 @@ bool CHMGenerator::loadDocument( const QString & fileName, QVector< Okular::Page continue; int pos = url.indexOf (QLatin1Char(('#'))); + // insert the url into the maps, but insert always the variant without the #ref part QString tmpUrl = pos == -1 ? url : url.left(pos); // url already there, abort insertion if (m_urlPage.contains(tmpUrl)) continue; - // insert the url into the maps, but insert always the variant without the #ref part - m_urlPage.insert(tmpUrl, pageNum); - m_pageUrl.append(tmpUrl); - pageNum++; + int foundPage = tmpPageList.value(tmpUrl, -1); + if (foundPage != -1 ) { + m_urlPage.insert(tmpUrl, foundPage); + m_pageUrl[foundPage] = tmpUrl; + } else { + //add pages not present in toc + m_urlPage.insert(tmpUrl, pageNum); + m_pageUrl.append(tmpUrl); + pageNum++; + } } pagesVector.resize(m_pageUrl.count());