diff --git a/autotests/documenttest.cpp b/autotests/documenttest.cpp index aa696f36c..3ef82198f 100644 --- a/autotests/documenttest.cpp +++ b/autotests/documenttest.cpp @@ -1,133 +1,133 @@ /*************************************************************************** * Copyright (C) 2013 by Fabio D'Urso * * Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group * * company, info@kdab.com. Work sponsored by the * * LiMux project of the city of Munich * * * * 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 #include "../core/annotations.h" #include "../core/document.h" #include "../core/document_p.h" #include "../core/generator.h" #include "../core/observer.h" #include "../core/page.h" #include "../core/rotationjob_p.h" #include "../settings_core.h" class DocumentTest : public QObject { Q_OBJECT private slots: void testCloseDuringRotationJob(); void testDocdataMigration(); }; // Test that we don't crash if the document is closed while a RotationJob // is enqueued/running void DocumentTest::testCloseDuringRotationJob() { Okular::SettingsCore::instance( QStringLiteral("documenttest") ); Okular::Document *m_document = new Okular::Document( nullptr ); const QString testFile = QStringLiteral(KDESRCDIR "data/file1.pdf"); QMimeDatabase db; const QMimeType mime = db.mimeTypeForFile( testFile ); Okular::DocumentObserver *dummyDocumentObserver = new Okular::DocumentObserver(); m_document->addObserver( dummyDocumentObserver ); m_document->openDocument( testFile, QUrl(), mime ); m_document->setRotation( 1 ); // Tell ThreadWeaver not to start any new job ThreadWeaver::Queue::instance()->suspend(); // Request a pixmap. A RotationJob will be enqueued but not started Okular::PixmapRequest *pixmapReq = new Okular::PixmapRequest( dummyDocumentObserver, 0, 100, 100, 1, Okular::PixmapRequest::NoFeature ); m_document->requestPixmaps( QLinkedList() << pixmapReq ); // Delete the document delete m_document; // Resume job processing and wait for the RotationJob to finish ThreadWeaver::Queue::instance()->resume(); ThreadWeaver::Queue::instance()->finish(); qApp->processEvents(); delete dummyDocumentObserver; } // Test that, if there's a XML file in docdata referring to a document, we // detect that it must be migrated, that it doesn't get wiped out if you close // the document without migrating and that it does get wiped out after migrating void DocumentTest::testDocdataMigration() { Okular::SettingsCore::instance( QStringLiteral("documenttest") ); const QUrl testFileUrl = QUrl::fromLocalFile(KDESRCDIR "data/file1.pdf"); const QString testFilePath = testFileUrl.toLocalFile(); const qint64 testFileSize = QFileInfo(testFilePath).size(); // Copy XML file to the docdata/ directory const QString docDataPath = Okular::DocumentPrivate::docDataFileName(testFileUrl, testFileSize); QFile::remove(docDataPath); QVERIFY( QFile::copy(KDESRCDIR "data/file1-docdata.xml", docDataPath) ); // Open our document Okular::Document *m_document = new Okular::Document( 0 ); QMimeDatabase db; const QMimeType mime = db.mimeTypeForFile( testFilePath ); QCOMPARE( m_document->openDocument( testFilePath, testFileUrl, mime ), Okular::Document::OpenSuccess ); // Check that the annotation from file1-docdata.xml was loaded QCOMPARE( m_document->page( 0 )->annotations().size(), 1 ); QCOMPARE( m_document->page( 0 )->annotations().first()->uniqueName(), QString("testannot") ); // Check that we detect that it must be migrated - QCOMPARE( m_document->isDocdataMigrationNeeded(), true ); + QVERIFY( m_document->isDocdataMigrationNeeded() ); m_document->closeDocument(); // Reopen the document and check that the annotation is still present // (because we have not migrated) QCOMPARE( m_document->openDocument( testFilePath, testFileUrl, mime ), Okular::Document::OpenSuccess ); QCOMPARE( m_document->page( 0 )->annotations().size(), 1 ); QCOMPARE( m_document->page( 0 )->annotations().first()->uniqueName(), QString("testannot") ); - QCOMPARE( m_document->isDocdataMigrationNeeded(), true ); + QVERIFY( m_document->isDocdataMigrationNeeded() ); // Do the migration QTemporaryFile migratedSaveFile( QStringLiteral( "%1/okrXXXXXX.pdf" ).arg( QDir::tempPath() ) ); QVERIFY( migratedSaveFile.open() ); migratedSaveFile.close(); QVERIFY( m_document->saveChanges( migratedSaveFile.fileName() ) ); m_document->docdataMigrationDone(); - QCOMPARE( m_document->isDocdataMigrationNeeded(), false ); + QVERIFY( !m_document->isDocdataMigrationNeeded() ); m_document->closeDocument(); // Now the docdata file should have no annotations, let's check QCOMPARE( m_document->openDocument( testFilePath, testFileUrl, mime ), Okular::Document::OpenSuccess ); QCOMPARE( m_document->page( 0 )->annotations().size(), 0 ); - QCOMPARE( m_document->isDocdataMigrationNeeded(), false ); + QVERIFY( !m_document->isDocdataMigrationNeeded() ); m_document->closeDocument(); // And the new file should have 1 annotation, let's check QCOMPARE( m_document->openDocument( migratedSaveFile.fileName(), QUrl::fromLocalFile(migratedSaveFile.fileName()), mime ), Okular::Document::OpenSuccess ); QCOMPARE( m_document->page( 0 )->annotations().size(), 1 ); - QCOMPARE( m_document->isDocdataMigrationNeeded(), false ); + QVERIFY( !m_document->isDocdataMigrationNeeded() ); m_document->closeDocument(); delete m_document; } QTEST_MAIN( DocumentTest ) #include "documenttest.moc" diff --git a/autotests/kjsfunctionstest.cpp b/autotests/kjsfunctionstest.cpp index 759fdd58f..8839e982c 100644 --- a/autotests/kjsfunctionstest.cpp +++ b/autotests/kjsfunctionstest.cpp @@ -1,371 +1,371 @@ /*************************************************************************** * Copyright (C) 2019 by João Netto * * * * 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 #include #include #include #include #include #include #include "../settings_core.h" #include "core/action.h" #include "core/document.h" #include "core/scripter.h" #include #include #include #include "../generators/poppler/config-okular-poppler.h" class MessageBoxHelper : public QObject { Q_OBJECT public: MessageBoxHelper(QMessageBox::StandardButton b, QString message, QMessageBox::Icon icon, QString title, bool hasCheckBox ) : m_button(b), m_clicked(false), m_message(message), m_icon(icon), m_title(title), m_checkBox(hasCheckBox) { QTimer::singleShot(0, this, &MessageBoxHelper::closeMessageBox); } ~MessageBoxHelper() { QVERIFY(m_clicked); } private slots: void closeMessageBox() { QWidgetList allToplevelWidgets = QApplication::topLevelWidgets(); QMessageBox *mb = nullptr; foreach ( QWidget *w, allToplevelWidgets ) { if ( w->inherits( "QMessageBox" ) ) { mb = qobject_cast< QMessageBox * >( w ); QCOMPARE( m_message, mb->text() ); QCOMPARE( m_title, mb->windowTitle() ); QCOMPARE( m_icon, mb->icon() ); QCheckBox *box = mb->checkBox(); QCOMPARE( m_checkBox, box != nullptr ); mb->button( m_button )->click(); } } if (!mb) { QTimer::singleShot(0, this, &MessageBoxHelper::closeMessageBox); return; } m_clicked = true; } private: QMessageBox::StandardButton m_button; bool m_clicked; QString m_message; QMessageBox::Icon m_icon; QString m_title; bool m_checkBox; }; class KJSFunctionsTest: public QObject { Q_OBJECT private slots: #ifdef HAVE_POPPLER_0_79 void initTestCase(); void testNthFieldName(); void testDisplay(); void testSetClearInterval(); void testSetClearTimeOut(); void testGetOCGs(); void cleanupTestCase(); void testAlert(); void testPrintD(); #endif private: Okular::Document *m_document; QMap m_fields; }; #ifdef HAVE_POPPLER_0_79 void KJSFunctionsTest::initTestCase() { Okular::SettingsCore::instance( QStringLiteral("kjsfunctionstest") ); m_document = new Okular::Document( nullptr ); const QString testFile = QStringLiteral( KDESRCDIR "data/kjsfunctionstest.pdf" ); QMimeDatabase db; const QMimeType mime = db.mimeTypeForFile( testFile ); QCOMPARE( m_document->openDocument( testFile, QUrl(), mime), Okular::Document::OpenSuccess ); const Okular::Page* page = m_document->page( 0 ); for ( Okular::FormField *ff: page->formFields() ) { m_fields.insert( ff->name(), ff ); } } void KJSFunctionsTest::testNthFieldName() { for(int i = 0;i < 21;++i) { Okular::ScriptAction *action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "var field = Doc.getField( Doc.getNthFieldName(%1) );\ field.display = display.visible;" ).arg( i ) ); m_document->processAction( action ); - QCOMPARE( true, m_fields[QString( "0.%1" ).arg(i)]->isVisible() ); + QVERIFY( m_fields[QString( "0.%1" ).arg(i)]->isVisible() ); m_fields[QString( "0.%1" ).arg(i)]->setVisible( false ); delete action; } } void KJSFunctionsTest::testDisplay() { Okular::ScriptAction *action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "field = Doc.getField(\"0.0\");field.display=display.hidden;\ field = Doc.getField(\"0.10\");field.display=display.visible;" ) ); m_document->processAction( action ); - QCOMPARE( false, m_fields["0.0"]->isVisible() ); - QCOMPARE( false, m_fields["0.0"]->isPrintable() ); - QCOMPARE( true, m_fields["0.10"]->isVisible() ); - QCOMPARE( true, m_fields["0.10"]->isPrintable() ); + QVERIFY( !m_fields["0.0"]->isVisible() ); + QVERIFY( !m_fields["0.0"]->isPrintable() ); + QVERIFY( m_fields["0.10"]->isVisible() ); + QVERIFY( m_fields["0.10"]->isPrintable() ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "field = Doc.getField(\"0.10\");field.display=display.noView;\ field = Doc.getField(\"0.15\");field.display=display.noPrint;" ) ); m_document->processAction( action ); - QCOMPARE( false, m_fields["0.10"]->isVisible() ); - QCOMPARE( true, m_fields["0.10"]->isPrintable() ); - QCOMPARE( true, m_fields["0.15"]->isVisible() ); - QCOMPARE( false, m_fields["0.15"]->isPrintable() ); + QVERIFY( !m_fields["0.10"]->isVisible() ); + QVERIFY( m_fields["0.10"]->isPrintable() ); + QVERIFY( m_fields["0.15"]->isVisible() ); + QVERIFY( !m_fields["0.15"]->isPrintable() ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "field = Doc.getField(\"0.15\");field.display=display.hidden;\ field = Doc.getField(\"0.20\");field.display=display.visible;" ) ); m_document->processAction( action ); - QCOMPARE( false, m_fields["0.15"]->isVisible() ); - QCOMPARE( false, m_fields["0.15"]->isPrintable() ); - QCOMPARE( true, m_fields["0.20"]->isVisible() ); - QCOMPARE( true, m_fields["0.20"]->isPrintable() ); + QVERIFY( !m_fields["0.15"]->isVisible() ); + QVERIFY( !m_fields["0.15"]->isPrintable() ); + QVERIFY( m_fields["0.20"]->isVisible() ); + QVERIFY( m_fields["0.20"]->isPrintable() ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "field = Doc.getField(\"0.20\");field.display=display.hidden;\ field = Doc.getField(\"0.0\");field.display=display.visible;" ) ); m_document->processAction( action ); - QCOMPARE( false, m_fields["0.20"]->isVisible() ); - QCOMPARE( false, m_fields["0.20"]->isPrintable() ); - QCOMPARE( true, m_fields["0.0"]->isVisible() ); - QCOMPARE( true, m_fields["0.0"]->isPrintable() ); + QVERIFY( !m_fields["0.20"]->isVisible() ); + QVERIFY( !m_fields["0.20"]->isPrintable() ); + QVERIFY( m_fields["0.0"]->isVisible() ); + QVERIFY( m_fields["0.0"]->isPrintable() ); delete action; } void delay() { QTime dieTime= QTime::currentTime().addSecs( 2 ); while (QTime::currentTime() < dieTime) QCoreApplication::processEvents(QEventLoop::AllEvents, 100); } void KJSFunctionsTest::testSetClearInterval() { Okular::ScriptAction *action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "obj = new Object();obj.idx=0;\ obj.inc=function(){field = Doc.getField(Doc.getNthFieldName(obj.idx));\ field.display = display.visible;\ obj.idx = obj.idx + 1;};\ intv = app.setInterval('obj.inc()', 450);obj.idx;" ) ); m_document->processAction( action ); - QCOMPARE( m_fields["0.0"]->isVisible() , true ); - QCOMPARE( m_fields["0.3"]->isVisible() , false ); + QVERIFY( m_fields["0.0"]->isVisible() ); + QVERIFY( !m_fields["0.3"]->isVisible() ); delete action; delay(); action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "app.clearInterval(intv);obj.idx;" ) ); m_document->processAction( action ); - QCOMPARE( m_fields["0.3"]->isVisible() , true ); + QVERIFY( m_fields["0.3"]->isVisible() ); delete action; } void KJSFunctionsTest::testSetClearTimeOut() { Okular::ScriptAction *action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "intv = app.setTimeOut('obj.inc()', 1);obj.idx;" ) ); m_document->processAction( action ); - QCOMPARE( m_fields["0.3"]->isVisible() , true); - QCOMPARE( m_fields["0.4"]->isVisible() , false ); + QVERIFY( m_fields["0.3"]->isVisible() ); + QVERIFY( !m_fields["0.4"]->isVisible() ); delay(); delete action; - QCOMPARE( m_fields["0.4"]->isVisible() , true ); + QVERIFY( m_fields["0.4"]->isVisible() ); action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "intv = app.setTimeOut('obj.inc()', 2000);obj.idx;" ) ); m_document->processAction( action ); - QCOMPARE( m_fields["0.4"]->isVisible() , true ); + QVERIFY( m_fields["0.4"]->isVisible() ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "app.clearTimeOut(intv);obj.idx;" ) ); m_document->processAction( action ); - QCOMPARE( m_fields["0.4"]->isVisible() , true ); + QVERIFY( m_fields["0.4"]->isVisible() ); delay(); - QCOMPARE( m_fields["0.4"]->isVisible() , true ); + QVERIFY( m_fields["0.4"]->isVisible() ); delete action; } void KJSFunctionsTest::testGetOCGs() { QAbstractItemModel *model = m_document->layersModel(); Okular::ScriptAction *action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "var ocg = this.getOCGs(this.pageNum);\ ocg[0].state = false;" ) ); m_document->processAction( action ); - QCOMPARE( model->data( model->index( 0, 0 ), Qt::CheckStateRole ).toBool() , false ); + QVERIFY( !model->data( model->index( 0, 0 ), Qt::CheckStateRole ).toBool() ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ocg[0].state = true;" ) ); m_document->processAction( action ); - QCOMPARE( model->data( model->index( 0, 0 ), Qt::CheckStateRole ).toBool() , true ); + QVERIFY( model->data( model->index( 0, 0 ), Qt::CheckStateRole ).toBool() ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ocg[1].state = false;" ) ); m_document->processAction( action ); - QCOMPARE( model->data( model->index( 1, 0 ), Qt::CheckStateRole ).toBool() , false ); + QVERIFY( !model->data( model->index( 1, 0 ), Qt::CheckStateRole ).toBool() ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ocg[1].state = true;" ) ); m_document->processAction( action ); - QCOMPARE( model->data( model->index( 1, 0 ), Qt::CheckStateRole ).toBool() , true ); + QVERIFY( model->data( model->index( 1, 0 ), Qt::CheckStateRole ).toBool() ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ocg[2].state = false;" ) ); m_document->processAction( action ); - QCOMPARE( model->data( model->index( 2, 0 ), Qt::CheckStateRole ).toBool() , false ); + QVERIFY( !model->data( model->index( 2, 0 ), Qt::CheckStateRole ).toBool() ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ocg[2].state = true;" ) ); m_document->processAction( action ); - QCOMPARE( model->data( model->index( 2, 0 ), Qt::CheckStateRole ).toBool() , true ); + QVERIFY( model->data( model->index( 2, 0 ), Qt::CheckStateRole ).toBool() ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ocg[3].state = false;" ) ); m_document->processAction( action ); - QCOMPARE( model->data( model->index( 3, 0 ), Qt::CheckStateRole ).toBool() , false ); + QVERIFY( !model->data( model->index( 3, 0 ), Qt::CheckStateRole ).toBool() ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ocg[3].state = true;" ) ); m_document->processAction( action ); - QCOMPARE( model->data( model->index( 3, 0 ), Qt::CheckStateRole ).toBool() , true ); + QVERIFY( model->data( model->index( 3, 0 ), Qt::CheckStateRole ).toBool() ); delete action; } void KJSFunctionsTest::testAlert() { Okular::ScriptAction *action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( \"Random Message\" );" ) ); QScopedPointer< MessageBoxHelper > messageBoxHelper; messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "Random Message" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) ); m_document->processAction( action ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( \"Empty Message\", 1 );" ) ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "Empty Message" ), QMessageBox::Warning, QStringLiteral( "Okular" ), false ) ); m_document->processAction( action ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( \"No Message\", 2, 2 );" ) ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Yes, QStringLiteral( "No Message" ), QMessageBox::Question, QStringLiteral( "Okular" ), false ) ); m_document->processAction( action ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( \"No\", 3, 2, \"Test Dialog\" );" ) ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::No, QStringLiteral( "No" ), QMessageBox::Information, QStringLiteral( "Test Dialog" ), false ) ); m_document->processAction( action ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "var oCheckBox = new Object();\ ret = app.alert( \"Cancel\", 3, 3, \"Test Dialog\", 0, oCheckBox );" ) ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Cancel, QStringLiteral( "Cancel" ), QMessageBox::Information, QStringLiteral( "Test Dialog" ), true ) ); m_document->processAction( action ); delete action; } void KJSFunctionsTest::testPrintD() { Okular::ScriptAction *action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "var date = new Date( 2010, 0, 5, 11, 10, 32, 1 );\ ret = app.alert( util.printd( \"mm\\\\yyyy\", date ) );" ) ); QScopedPointer< MessageBoxHelper > messageBoxHelper; messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "01\\2010" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) ); m_document->processAction( action ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( \"m\\\\yy\", date ) );" ) ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "1\\10" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) ); m_document->processAction( action ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( \"dd\\\\mm HH:MM\", date ) );" ) ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "05\\01 11:10" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) ); m_document->processAction( action ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( \"dd\\\\mm HH:MM:ss\", date ) );" ) ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "05\\01 11:10:32" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) ); m_document->processAction( action ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( \"yyyy\\\\mm HH:MM:ss\", date ) );" ) ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "2010\\01 11:10:32" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) ); m_document->processAction( action ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( 0, date ) );" ) ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "D:20100105111032" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) ); m_document->processAction( action ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( 1, date ) );" ) ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "2010.01.05 11:10:32" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) ); m_document->processAction( action ); delete action; action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( 2, date ) );" ) ); QLocale locale = QLocale::system(); QDate date( 2010, 1, 5 ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, date.toString( locale.dateFormat( QLocale::ShortFormat ) ) + QStringLiteral( " 11:10:32" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) ); m_document->processAction( action ); delete action; } void KJSFunctionsTest::cleanupTestCase() { m_document->closeDocument(); delete m_document; } #endif QTEST_MAIN( KJSFunctionsTest ) #include "kjsfunctionstest.moc" diff --git a/autotests/visibilitytest.cpp b/autotests/visibilitytest.cpp index e00638317..c5a38c89b 100644 --- a/autotests/visibilitytest.cpp +++ b/autotests/visibilitytest.cpp @@ -1,177 +1,177 @@ /*************************************************************************** * Copyright (C) 2018 by Intevation GmbH * * * * 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 #include #include #include "../settings_core.h" #include "core/document.h" #include #include #include "../generators/poppler/config-okular-poppler.h" class VisibilityTest: public QObject { Q_OBJECT private slots: void initTestCase(); void cleanupTestCase(); void testJavaScriptVisibility(); void testSaveLoad(); void testActionVisibility(); private: void verifyTargetStates( bool visible ); Okular::Document *m_document; QMap m_fields; }; void VisibilityTest::initTestCase() { Okular::SettingsCore::instance( QStringLiteral("visibilitytest") ); m_document = new Okular::Document( nullptr ); const QString testFile = QStringLiteral( KDESRCDIR "data/visibilitytest.pdf" ); QMimeDatabase db; const QMimeType mime = db.mimeTypeForFile( testFile ); QCOMPARE( m_document->openDocument( testFile, QUrl(), mime), Okular::Document::OpenSuccess ); // The test document has four buttons: // HideScriptButton -> Hides targets with JavaScript // ShowScriptButton -> Shows targets with JavaScript // HideActionButton -> Hides targets with HideAction // ShowActionButton -> Shows targets with HideAction // // The target fields are: // TargetButton TargetText TargetCheck TargetDropDown TargetRadio // // With two radio buttons named TargetRadio. const Okular::Page* page = m_document->page( 0 ); for ( Okular::FormField *ff: page->formFields() ) { m_fields.insert( ff->name(), ff ); } } void VisibilityTest::cleanupTestCase() { m_document->closeDocument(); delete m_document; } void VisibilityTest::verifyTargetStates( bool visible ) { QCOMPARE( m_fields[QStringLiteral( "TargetButton" )]->isVisible(), visible ); QCOMPARE( m_fields[QStringLiteral( "TargetText" )]->isVisible(), visible ); QCOMPARE( m_fields[QStringLiteral( "TargetCheck" )]->isVisible(), visible ); QCOMPARE( m_fields[QStringLiteral( "TargetDropDown" )]->isVisible(), visible ); // Radios do not properly inherit a name from the parent group so // this does not work yet (And would probably need some list handling). // QCOMPARE( m_fields[QStringLiteral( "TargetRadio" )].isVisible(), visible ); } void VisibilityTest::testJavaScriptVisibility() { #ifndef HAVE_POPPLER_0_64 return; #endif auto hideBtn = m_fields[QStringLiteral( "HideScriptButton" )]; auto showBtn = m_fields[QStringLiteral( "ShowScriptButton" )]; // We start with all fields visible verifyTargetStates( true ); m_document->processAction( hideBtn->activationAction() ); // Now all should be hidden verifyTargetStates( false ); // And show again m_document->processAction( showBtn->activationAction() ); verifyTargetStates( true ); } void VisibilityTest::testSaveLoad() { #ifndef HAVE_POPPLER_0_64 return; #endif auto hideBtn = m_fields[QStringLiteral( "HideScriptButton" )]; auto showBtn = m_fields[QStringLiteral( "ShowScriptButton" )]; // We start with all fields visible verifyTargetStates( true ); m_document->processAction( hideBtn->activationAction() ); // Now all should be hidden verifyTargetStates( false ); // Save the changed states QTemporaryFile saveFile( QStringLiteral( "%1/okrXXXXXX.pdf" ).arg( QDir::tempPath() ) ); QVERIFY( saveFile.open() ); saveFile.close(); QVERIFY( m_document->saveChanges( saveFile.fileName() ) ); auto newDoc = new Okular::Document( nullptr ); QMimeDatabase db; const QMimeType mime = db.mimeTypeForFile( saveFile.fileName() ); QCOMPARE( newDoc->openDocument( saveFile.fileName(), QUrl(), mime), Okular::Document::OpenSuccess ); const Okular::Page* page = newDoc->page( 0 ); bool anyChecked = false; // Saveguard against accidental test passing here ;-) for ( Okular::FormField *ff: page->formFields() ) { if ( ff->name().startsWith( QStringLiteral( "Target" ) ) ) { - QCOMPARE( ff->isVisible(), false ); + QVERIFY( !ff->isVisible() ); anyChecked = true; } } QVERIFY(anyChecked); newDoc->closeDocument(); delete newDoc; // Restore the state of the member document m_document->processAction( showBtn->activationAction() ); } void VisibilityTest::testActionVisibility() { #ifndef HAVE_POPPLER_0_64 return; #endif auto hideBtn = m_fields[QStringLiteral( "HideActionButton" )]; auto showBtn = m_fields[QStringLiteral( "ShowActionButton" )]; verifyTargetStates( true ); m_document->processAction( hideBtn->activationAction() ); verifyTargetStates( false ); m_document->processAction( showBtn->activationAction() ); verifyTargetStates( true ); } QTEST_MAIN( VisibilityTest ) #include "visibilitytest.moc"