Commit 1c1a073c authored by Adriaan de Groot's avatar Adriaan de Groot 💬 Committed by Albert Astals Cid

Improve testPrintD

Summary:
 - swap out manual management for a PrintDHelper class that allocates
   an action and MessageBoxHelper, runs the script, and checks the
   result.
 - use _data() to run the helper with different data; this improves
   observability as well.

Test Plan:  - Run tests, see same results as before

Subscribers: okular-devel

Tags: #okular

Differential Revision: https://phabricator.kde.org/D23164
parent a7845890
Pipeline #6665 passed with stage
in 14 minutes and 16 seconds
......@@ -90,6 +90,7 @@ private slots:
void cleanupTestCase();
void testAlert();
void testPrintD();
void testPrintD_data();
#endif
private:
Okular::Document *m_document;
......@@ -310,53 +311,68 @@ void KJSFunctionsTest::testAlert()
delete action;
}
void KJSFunctionsTest::testPrintD()
/** @brief Checks a single JS action against an expected result
*
* Runs an action with the given @p script and checks that it
* does pop-up a messagebox with the given @p result text.
*/
class PrintDHelper
{
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;
public:
PrintDHelper( Okular::Document* document, const QString& script, const QString& result ) :
action( new Okular::ScriptAction( Okular::JavaScript, script ) ),
box( new MessageBoxHelper( QMessageBox::Ok, result, QMessageBox::Critical, QStringLiteral( "Okular" ), false ) )
{
document->processAction( action.get() );
}
private:
QScopedPointer< Okular::ScriptAction > action;
QScopedPointer< MessageBoxHelper > box;
} ;
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;
void KJSFunctionsTest::testPrintD_data()
{
QTest::addColumn<QString>("script");
QTest::addColumn<QString>("result");
QTest::newRow("mmyyy")
<< QStringLiteral( "var date = new Date( 2010, 0, 5, 11, 10, 32, 1 );\
ret = app.alert( util.printd( \"mm\\\\yyyy\", date ) );" )
<< QStringLiteral( "01\\2010" );
QTest::newRow("myy")
<< QStringLiteral( "ret = app.alert( util.printd( \"m\\\\yy\", date ) );" )
<< QStringLiteral( "1\\10" );
QTest::newRow("ddmmHHMM")
<< QStringLiteral( "ret = app.alert( util.printd( \"dd\\\\mm HH:MM\", date ) );" )
<< QStringLiteral( "05\\01 11:10" );
QTest::newRow("ddmmHHMMss")
<< QStringLiteral( "ret = app.alert( util.printd( \"dd\\\\mm HH:MM:ss\", date ) );" )
<< QStringLiteral( "05\\01 11:10:32" );
QTest::newRow("yyyymmHHMMss")
<< QStringLiteral( "ret = app.alert( util.printd( \"yyyy\\\\mm HH:MM:ss\", date ) );" )
<< QStringLiteral( "2010\\01 11:10:32" );
QTest::newRow("0")
<< QStringLiteral( "ret = app.alert( util.printd( 0, date ) );" )
<< QStringLiteral( "D:20100105111032" );
QTest::newRow("1")
<< QStringLiteral( "ret = app.alert( util.printd( 1, date ) );" )
<< QStringLiteral( "2010.01.05 11:10:32" );
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;
QTest::newRow("2")
<< QStringLiteral( "ret = app.alert( util.printd( 2, date ) );" )
<< QString( date.toString( locale.dateFormat( QLocale::ShortFormat ) ) + QStringLiteral( " 11:10:32" ) );
}
void KJSFunctionsTest::testPrintD()
{
QFETCH(QString, script);
QFETCH(QString, result);
QVERIFY( script.contains( "printd" ) );
PrintDHelper test( m_document, script, result );
}
void KJSFunctionsTest::cleanupTestCase()
......
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