kjsfunctionstest.cpp 15.7 KB
Newer Older
1
/***************************************************************************
2
 *   Copyright (C) 2019 by João Netto <joaonetto901@gmail.com>             *
3 4 5 6 7 8 9 10 11
 *                                                                         *
 *   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 <QtTest>

12
#include <QAbstractItemModel>
13
#include <QMap>
14 15
#include <QAbstractButton>
#include <QMessageBox>
16 17 18 19
#include <QMimeType>
#include <QMimeDatabase>
#include <QThread>
#include "../settings_core.h"
20
#include "core/action.h"
21 22 23 24 25 26 27 28
#include "core/document.h"
#include "core/scripter.h"
#include <core/script/event_p.h>
#include <core/page.h>
#include <core/form.h>

#include "../generators/poppler/config-okular-poppler.h"

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
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;
};


78 79 80 81 82
class KJSFunctionsTest: public QObject
{
    Q_OBJECT

private slots:
83
#ifdef HAVE_POPPLER_0_79
84 85 86 87 88
    void initTestCase();
    void testNthFieldName();
    void testDisplay();
    void testSetClearInterval();
    void testSetClearTimeOut();
89
    void testGetOCGs();
90
    void cleanupTestCase();
91 92
    void testAlert();
    void testPrintD();
93
#endif
94 95 96 97 98
private:
    Okular::Document *m_document;
    QMap<QString, Okular::FormField*> m_fields;
};

99 100
#ifdef HAVE_POPPLER_0_79

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
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)
    {
122 123 124 125 126 127
        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() );
        m_fields[QString( "0.%1" ).arg(i)]->setVisible( false ); 
        delete action;
128 129 130 131 132
    }
}

void KJSFunctionsTest::testDisplay()
{
133 134 135 136
    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 );
137
    QCOMPARE( false, m_fields["0.0"]->isVisible() );
138
    QCOMPARE( false, m_fields["0.0"]->isPrintable() );
139
    QCOMPARE( true, m_fields["0.10"]->isVisible() );
140 141
    QCOMPARE( true, m_fields["0.10"]->isPrintable() );
    delete action;
142
    
143 144 145 146
    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 );
147
    QCOMPARE( false, m_fields["0.10"]->isVisible() );
148
    QCOMPARE( true, m_fields["0.10"]->isPrintable() );
149
    QCOMPARE( true, m_fields["0.15"]->isVisible() );
150 151
    QCOMPARE( false, m_fields["0.15"]->isPrintable() );
    delete action;
152

153 154 155 156
    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 );
157
    QCOMPARE( false, m_fields["0.15"]->isVisible() );
158
    QCOMPARE( false, m_fields["0.15"]->isPrintable() );
159
    QCOMPARE( true, m_fields["0.20"]->isVisible() );
160 161
    QCOMPARE( true, m_fields["0.20"]->isPrintable() );
    delete action;
162

163 164 165 166
    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 );
167
    QCOMPARE( false, m_fields["0.20"]->isVisible() );
168
    QCOMPARE( false, m_fields["0.20"]->isPrintable() );
169
    QCOMPARE( true, m_fields["0.0"]->isVisible() );
170 171
    QCOMPARE( true, m_fields["0.0"]->isPrintable() );
    delete action;
172 173 174 175 176 177 178 179 180 181 182
}

void delay()
{
    QTime dieTime= QTime::currentTime().addSecs( 2 );
    while (QTime::currentTime() < dieTime)
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

void KJSFunctionsTest::testSetClearInterval()
{
183 184 185 186 187 188 189 190 191 192
    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 );
    delete action;
193
    delay();
194

195 196 197 198 199
    action = new Okular::ScriptAction( Okular::JavaScript, 
        QStringLiteral( "app.clearInterval(intv);obj.idx;" ) );
    m_document->processAction( action );
    QCOMPARE( m_fields["0.3"]->isVisible() , true );
    delete action;
200 201 202 203
}

void KJSFunctionsTest::testSetClearTimeOut()
{
204 205 206 207 208
    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 );
209
    delay();
210
    delete action;
211
    
212
    QCOMPARE( m_fields["0.4"]->isVisible() , true );
213
    
214 215 216 217 218
    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 );
    delete action;
219
    
220 221 222 223
    action = new Okular::ScriptAction( Okular::JavaScript, 
        QStringLiteral(  "app.clearTimeOut(intv);obj.idx;" ) );
    m_document->processAction( action );
    QCOMPARE( m_fields["0.4"]->isVisible() , true );
224
    delay();
225 226
    QCOMPARE( m_fields["0.4"]->isVisible() , true );
    delete action;
227 228
}

229 230 231 232
void KJSFunctionsTest::testGetOCGs()
{
    QAbstractItemModel *model = m_document->layersModel();

233 234 235 236 237 238 239 240 241 242 243 244
    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 );
    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 );
    delete action;
245
    
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    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 );
    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 );
    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 );
    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 );
    delete action;
269
    
270 271 272 273 274 275 276 277 278 279 280
    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 );
    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 );
    delete action;
281 282
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
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, 
316
                                                             QStringLiteral( "var date = new Date( 2010, 0, 5, 11, 10, 32, 1 );\
317 318 319 320 321 322 323 324 325 326 327 328
                                                                              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 ) );" ) );
329
    messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "05\\01 11:10" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) );
330 331 332 333
    m_document->processAction( action );
    delete action;

    action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( \"dd\\\\mm HH:MM:ss\", date ) );" ) );
334
    messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "05\\01 11:10:32" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) );
335 336 337 338 339 340 341
    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;
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

    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;
360
}
361

362 363 364 365 366 367
void KJSFunctionsTest::cleanupTestCase()
{
    m_document->closeDocument();
    delete m_document;
}

368
#endif
369 370 371

QTEST_MAIN( KJSFunctionsTest )
#include "kjsfunctionstest.moc"