Commit 4cfea5e4 authored by Fabio D'Urso's avatar Fabio D'Urso

Added test to check save to native and to .okular features

parent 288f87f1
......@@ -2264,7 +2264,7 @@ bool Part::slotSaveFileAs( bool showOkularArchiveAsDefaultFormat )
// Has the user chosen to save in .okular archive format?
const bool saveAsOkularArchive = ( fd.currentMimeFilter() == "application/vnd.kde.okular-archive" );
return saveAs( saveUrl, saveAsOkularArchive );
return saveAs( saveUrl, saveAsOkularArchive ? SaveAsOkularArchive : NoSaveAsFlags );
}
else
{
......@@ -2275,10 +2275,10 @@ bool Part::slotSaveFileAs( bool showOkularArchiveAsDefaultFormat )
bool Part::saveAs(const KUrl & saveUrl)
{
// Save in the same format (.okular vs native) as the current file
return saveAs( saveUrl, isDocumentArchive /* saveAsOkularArchive */ );
return saveAs( saveUrl, isDocumentArchive ? SaveAsOkularArchive : NoSaveAsFlags );
}
bool Part::saveAs( const KUrl & saveUrl, bool saveAsOkularArchive )
bool Part::saveAs( const KUrl & saveUrl, SaveAsFlags flags )
{
KTemporaryFile tf;
QString fileName;
......@@ -2294,7 +2294,7 @@ bool Part::saveAs( const KUrl & saveUrl, bool saveAsOkularArchive )
KIO::Job *copyJob; // this will be filled with the job that writes to saveUrl
// Does the user want a .okular archive?
if ( saveAsOkularArchive )
if ( flags & SaveAsOkularArchive )
{
if ( !m_document->saveDocumentArchive( fileName ) )
{
......@@ -2351,7 +2351,7 @@ bool Part::saveAs( const KUrl & saveUrl, bool saveAsOkularArchive )
QStringList listOfwontSaves;
if ( wontSaveForms ) listOfwontSaves << i18n( "Filled form contents" );
if ( wontSaveAnnotations ) listOfwontSaves << i18n( "User annotations" );
if ( !listOfwontSaves.isEmpty() )
if ( !listOfwontSaves.isEmpty() && !( flags && SaveAsDontShowWarning ) )
{
int result = KMessageBox::warningYesNoCancelList( widget(),
i18n( "The following elements <b>cannot be saved</b> in this format and will be lost.<br>If you want to preserve them, please use the <i>Okular document archive</i> format." ),
......
......@@ -249,7 +249,16 @@ class OKULAR_PART_EXPORT Part : public KParts::ReadWritePart, public Okular::Doc
void unsetDummyMode();
void slotRenameBookmark( const DocumentViewport &viewport );
void resetStartArguments();
bool saveAs( const KUrl & saveUrl, bool saveAsOkularArchive );
enum SaveAsFlag
{
NoSaveAsFlags = 0, ///< No options
SaveAsOkularArchive = 1, ///< Save as Okular Archive (.okular) instead of document's native format
SaveAsDontShowWarning = 2, ///< Don't show warning for unsupported save features
};
Q_DECLARE_FLAGS( SaveAsFlags, SaveAsFlag )
bool saveAs( const KUrl & saveUrl, SaveAsFlags flags );
void setFileToWatch( const QString &filePath );
void unsetFileToWatch();
......
......@@ -10,6 +10,8 @@
#include <qtest_kde.h>
#include "../part.h"
#include "../core/annotations.h"
#include "../core/page.h"
#include "../ui/toc.h"
#include <KConfigDialog>
......@@ -31,6 +33,8 @@ class PartTest
void testTOCReload();
void testFowardPDF();
void testFowardPDF_data();
void testSaveAs();
void testSaveAs_data();
void testGeneratorPreferences();
};
......@@ -140,6 +144,95 @@ void PartTest::testFowardPDF_data()
QTest::newRow("utf8") << QString(KGlobal::dirs()->resourceDirs("tmp")[0] + QString::fromUtf8("ßðđđŋßðđŋ"));
}
void PartTest::testSaveAs()
{
QFETCH(QString, file);
QFETCH(QString, extension);
QFETCH(bool, nativelySupportsAnnotations);
// If we expect not to be able to preserve annotations when we write a
// native file, disable the warning otherwise the test will wait for the
// user to confirm. On the other end, if we expect annotations to be
// preserved (and thus no warning), we keep the warning on so that if it
// shows the test timeouts and we can notice that something is wrong.
const Part::SaveAsFlags saveAsNativeFlags = nativelySupportsAnnotations ?
Part::NoSaveAsFlags : Part::SaveAsDontShowWarning;
QString annotName;
QTemporaryFile archiveSave( QString( "%1/okrXXXXXX.okular" ).arg( QDir::tempPath() ) );
QTemporaryFile nativeDirectSave( QString( "%1/okrXXXXXX.%2" ).arg( QDir::tempPath() ).arg ( extension ) );
QTemporaryFile nativeFromArchiveFile( QString( "%1/okrXXXXXX.%2" ).arg( QDir::tempPath() ).arg ( extension ) );
QVERIFY( archiveSave.open() ); archiveSave.close();
QVERIFY( nativeDirectSave.open() ); nativeDirectSave.close();
QVERIFY( nativeFromArchiveFile.open() ); nativeFromArchiveFile.close();
qDebug() << "Open file, add annotation and save both natively and to .okular";
{
Okular::Part part(NULL, NULL, QVariantList(), KGlobal::mainComponent());
part.openDocument( file );
Okular::Annotation *annot = new Okular::TextAnnotation();
annot->setBoundingRectangle( Okular::NormalizedRect( 0.1, 0.1, 0.15, 0.15 ) );
annot->setContents( "annot contents" );
part.m_document->addPageAnnotation( 0, annot );
annotName = annot->uniqueName();
QVERIFY( part.saveAs( KUrl( archiveSave.fileName() ), Part::SaveAsOkularArchive ) );
QVERIFY( part.saveAs( KUrl( nativeDirectSave.fileName() ), saveAsNativeFlags ) );
part.closeUrl();
}
qDebug() << "Open the .okular, check that the annotation is present and save to native";
{
Okular::Part part(NULL, NULL, QVariantList(), KGlobal::mainComponent());
part.openDocument( archiveSave.fileName() );
QVERIFY( part.m_document->page( 0 )->annotations().size() == 1 );
QVERIFY( part.m_document->page( 0 )->annotations().first()->uniqueName() == annotName );
QVERIFY( part.saveAs( KUrl( nativeFromArchiveFile.fileName() ), saveAsNativeFlags ) );
part.closeUrl();
}
qDebug() << "Open the native file saved directly, and check that the annot"
<< "is there iff we expect it";
{
Okular::Part part(NULL, NULL, QVariantList(), KGlobal::mainComponent());
part.openDocument( nativeDirectSave.fileName() );
QCOMPARE( part.m_document->page( 0 )->annotations().size(), nativelySupportsAnnotations ? 1 : 0 );
if ( nativelySupportsAnnotations )
QVERIFY( part.m_document->page( 0 )->annotations().first()->uniqueName() == annotName );
part.closeUrl();
}
qDebug() << "Open the native file saved from the .okular, and check that the annot"
<< "is there iff we expect it";
{
Okular::Part part(NULL, NULL, QVariantList(), KGlobal::mainComponent());
part.openDocument( nativeFromArchiveFile.fileName() );
QCOMPARE( part.m_document->page( 0 )->annotations().size(), nativelySupportsAnnotations ? 1 : 0 );
if ( nativelySupportsAnnotations )
QVERIFY( part.m_document->page( 0 )->annotations().first()->uniqueName() == annotName );
part.closeUrl();
}
}
void PartTest::testSaveAs_data()
{
QTest::addColumn<QString>("file");
QTest::addColumn<QString>("extension");
QTest::addColumn<bool>("nativelySupportsAnnotations");
QTest::newRow("pdf") << KDESRCDIR "data/file1.pdf" << "pdf" << true;
QTest::newRow("epub") << KDESRCDIR "data/contents.epub" << "epub" << false;
}
void PartTest::testGeneratorPreferences()
{
KConfigDialog * dialog;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment