Changeset View
Changeset View
Standalone View
Standalone View
autotests/kjsfunctionstest.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 by João Netto <joaonetto901@gmail.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or modify * | ||||
5 | * it under the terms of the GNU General Public License as published by * | ||||
6 | * the Free Software Foundation; either version 2 of the License, or * | ||||
7 | * (at your option) any later version. * | ||||
8 | ***************************************************************************/ | ||||
9 | | ||||
10 | #include <QtTest> | ||||
11 | | ||||
12 | #include <QAbstractItemModel> | ||||
13 | #include <QMap> | ||||
14 | #include <QMimeType> | ||||
15 | #include <QMimeDatabase> | ||||
16 | #include <QThread> | ||||
17 | #include "../settings_core.h" | ||||
18 | #include "core/document.h" | ||||
19 | #include "core/scripter.h" | ||||
20 | #include <core/script/event_p.h> | ||||
21 | #include <core/page.h> | ||||
22 | #include <core/form.h> | ||||
23 | | ||||
24 | #include "../generators/poppler/config-okular-poppler.h" | ||||
25 | | ||||
26 | class KJSFunctionsTest: public QObject | ||||
27 | { | ||||
28 | Q_OBJECT | ||||
29 | | ||||
30 | private slots: | ||||
31 | void initTestCase(); | ||||
32 | void testNumFields(); | ||||
33 | void testNthFieldName(); | ||||
34 | void testDisplay(); | ||||
35 | void testSetClearInterval(); | ||||
36 | void testSetClearTimeOut(); | ||||
37 | void testGetOCGs(); | ||||
38 | void testOCGSetState(); | ||||
39 | void cleanupTestCase(); | ||||
40 | | ||||
41 | private: | ||||
42 | Okular::Document *m_document; | ||||
43 | QMap<QString, Okular::FormField*> m_fields; | ||||
44 | }; | ||||
45 | | ||||
46 | void KJSFunctionsTest::initTestCase() | ||||
47 | { | ||||
48 | Okular::SettingsCore::instance( QStringLiteral("kjsfunctionstest") ); | ||||
49 | m_document = new Okular::Document( nullptr ); | ||||
50 | | ||||
51 | const QString testFile = QStringLiteral( KDESRCDIR "data/kjsfunctionstest.pdf" ); | ||||
52 | QMimeDatabase db; | ||||
53 | const QMimeType mime = db.mimeTypeForFile( testFile ); | ||||
54 | QCOMPARE( m_document->openDocument( testFile, QUrl(), mime), Okular::Document::OpenSuccess ); | ||||
55 | | ||||
56 | const Okular::Page* page = m_document->page( 0 ); | ||||
57 | for ( Okular::FormField *ff: page->formFields() ) | ||||
58 | { | ||||
59 | m_fields.insert( ff->name(), ff ); | ||||
60 | } | ||||
61 | } | ||||
62 | | ||||
63 | void KJSFunctionsTest::testNumFields() | ||||
64 | { | ||||
65 | QString result = m_document->executeScript( "Doc.numFields" ); | ||||
66 | QCOMPARE( "31", result ); | ||||
67 | } | ||||
68 | | ||||
69 | void KJSFunctionsTest::testNthFieldName() | ||||
70 | { | ||||
71 | for(int i = 0;i < 21;++i) | ||||
72 | { | ||||
73 | QString result = m_document->executeScript( QString( "Doc.getNthFieldName(%1)" ).arg( i ) ); | ||||
74 | QCOMPARE( result, QString( "0.%1" ).arg(i) ); | ||||
75 | } | ||||
76 | } | ||||
77 | | ||||
78 | void KJSFunctionsTest::testDisplay() | ||||
79 | { | ||||
80 | m_document->executeScript( "field = Doc.getField(\"0.0\");field.display=display.hidden;\ | ||||
81 | field = Doc.getField(\"0.10\");field.display=display.visible;" ); | ||||
82 | QCOMPARE( false, m_fields["0.0"]->isVisible() ); | ||||
83 | QCOMPARE( true, m_fields["0.10"]->isVisible() ); | ||||
84 | | ||||
85 | m_document->executeScript( "field = Doc.getField(\"0.10\");field.display=display.hidden;\ | ||||
86 | field = Doc.getField(\"0.15\");field.display=display.visible;" ); | ||||
87 | QCOMPARE( false, m_fields["0.10"]->isVisible() ); | ||||
88 | QCOMPARE( true, m_fields["0.15"]->isVisible() ); | ||||
89 | | ||||
90 | m_document->executeScript( "field = Doc.getField(\"0.15\");field.display=display.hidden;\ | ||||
91 | field = Doc.getField(\"0.20\");field.display=display.visible;" ); | ||||
92 | QCOMPARE( false, m_fields["0.15"]->isVisible() ); | ||||
93 | QCOMPARE( true, m_fields["0.20"]->isVisible() ); | ||||
94 | | ||||
95 | m_document->executeScript( "field = Doc.getField(\"0.20\");field.display=display.hidden;\ | ||||
96 | field = Doc.getField(\"0.0\");field.display=display.visible;" ); | ||||
97 | QCOMPARE( false, m_fields["0.20"]->isVisible() ); | ||||
98 | QCOMPARE( true, m_fields["0.0"]->isVisible() ); | ||||
99 | } | ||||
100 | | ||||
101 | void delay() | ||||
102 | { | ||||
103 | QTime dieTime= QTime::currentTime().addSecs( 2 ); | ||||
104 | while (QTime::currentTime() < dieTime) | ||||
105 | QCoreApplication::processEvents(QEventLoop::AllEvents, 100); | ||||
106 | } | ||||
107 | | ||||
108 | void KJSFunctionsTest::testSetClearInterval() | ||||
109 | { | ||||
110 | QString result = m_document->executeScript( "obj = new Object();obj.idx=0;\ | ||||
111 | obj.inc=function(){obj.idx = obj.idx + 1;};\ | ||||
112 | intv = app.setInterval('obj.inc()', 450);obj.idx;" ); | ||||
113 | QCOMPARE( "0", result ); | ||||
114 | delay(); | ||||
115 | | ||||
116 | result = m_document->executeScript( "app.clearInterval(intv);obj.idx;"); | ||||
117 | QCOMPARE( "4", result ); | ||||
118 | } | ||||
119 | | ||||
120 | void KJSFunctionsTest::testSetClearTimeOut() | ||||
121 | { | ||||
122 | QString result = m_document->executeScript( "intv = app.setTimeOut('obj.inc()', 1);obj.idx;" ); | ||||
123 | QCOMPARE( "4", result ); | ||||
124 | delay(); | ||||
125 | | ||||
126 | result = m_document->executeScript( "obj.idx;" ); | ||||
127 | QCOMPARE( "5", result ); | ||||
128 | | ||||
129 | result = m_document->executeScript( "intv = app.setTimeOut('obj.inc()', 2000);obj.idx;" ); | ||||
130 | QCOMPARE( "5", result ); | ||||
131 | | ||||
132 | result = m_document->executeScript( "app.clearTimeOut(intv);obj.idx;" ); | ||||
133 | QCOMPARE( "5", result ); | ||||
134 | delay(); | ||||
135 | QCOMPARE( "5", result ); | ||||
136 | } | ||||
137 | | ||||
138 | void KJSFunctionsTest::testGetOCGs() | ||||
139 | { | ||||
140 | QAbstractItemModel *model = m_document->layersModel(); | ||||
141 | | ||||
142 | QString result = m_document->executeScript( "var ocg = this.getOCGs(this.pageNum);\ | ||||
143 | ocgName = ocg[0].name;" ); | ||||
144 | QCOMPARE( model->data( model->index( 0, 0 ), Qt::DisplayRole ).toString() , result ); | ||||
145 | | ||||
146 | result = m_document->executeScript( "ocgName = ocg[1].name;" ); | ||||
147 | QCOMPARE( model->data( model->index( 1, 0 ), Qt::DisplayRole ).toString() , result ); | ||||
148 | | ||||
149 | result = m_document->executeScript( "ocgName = ocg[2].name;" ); | ||||
150 | QCOMPARE( model->data( model->index( 2, 0 ), Qt::DisplayRole ).toString() , result ); | ||||
151 | | ||||
152 | result = m_document->executeScript( "ocgName = ocg[3].name;" ); | ||||
153 | QCOMPARE( model->data( model->index( 3, 0 ), Qt::DisplayRole ).toString() , result ); | ||||
154 | | ||||
155 | result = m_document->executeScript( "ocgName = ocg[0].initState;" ); | ||||
156 | QCOMPARE( model->data( model->index( 0, 0 ), Qt::CheckStateRole ).toBool() ? "true" : "false" | ||||
157 | , result ); | ||||
158 | | ||||
159 | result = m_document->executeScript( "ocgName = ocg[1].initState;" ); | ||||
160 | QCOMPARE( model->data( model->index( 1, 0 ), Qt::CheckStateRole ).toBool() ? "true" : "false" | ||||
161 | , result ); | ||||
162 | | ||||
163 | result = m_document->executeScript( "ocgName = ocg[2].initState;" ); | ||||
164 | QCOMPARE( model->data( model->index( 2, 0 ), Qt::CheckStateRole ).toBool() ? "true" : "false" | ||||
165 | , result ); | ||||
166 | | ||||
167 | result = m_document->executeScript( "ocgName = ocg[3].initState;" ); | ||||
168 | QCOMPARE( model->data( model->index( 3, 0 ), Qt::CheckStateRole ).toBool() ? "true" : "false" | ||||
169 | , result ); | ||||
170 | } | ||||
171 | | ||||
172 | void KJSFunctionsTest::testOCGSetState() | ||||
173 | { | ||||
174 | QAbstractItemModel *model = m_document->layersModel(); | ||||
175 | | ||||
176 | QString result = m_document->executeScript( "ocgName = ocg[0].state;" ); | ||||
177 | QCOMPARE( model->data( model->index( 0, 0 ), Qt::CheckStateRole ).toBool() ? "true" : "false", result ); | ||||
178 | | ||||
179 | result = m_document->executeScript( "ocg[0].state = false;ocgName = ocg[0].state;"); | ||||
180 | QCOMPARE( model->data( model->index( 0, 0 ), Qt::CheckStateRole ).toBool() ? "true" : "false", result ); | ||||
181 | | ||||
182 | result = m_document->executeScript( "ocgName = ocg[1].state;" ); | ||||
183 | QCOMPARE( model->data( model->index( 1, 0 ), Qt::CheckStateRole ).toBool() ? "true" : "false", result ); | ||||
184 | | ||||
185 | result = m_document->executeScript( "ocg[1].state = false;ocgName = ocg[1].state;"); | ||||
186 | QCOMPARE( model->data( model->index( 1, 0 ), Qt::CheckStateRole ).toBool() ? "true" : "false", result ); | ||||
187 | | ||||
188 | result = m_document->executeScript( "ocgName = ocg[2].state;" ); | ||||
189 | QCOMPARE( model->data( model->index( 2, 0 ), Qt::CheckStateRole ).toBool() ? "true" : "false", result ); | ||||
190 | | ||||
191 | result = m_document->executeScript( "ocg[2].state = true;ocgName = ocg[2].state;"); | ||||
192 | QCOMPARE( model->data( model->index( 2, 0 ), Qt::CheckStateRole ).toBool() ? "true" : "false", result ); | ||||
193 | | ||||
194 | result = m_document->executeScript( "ocgName = ocg[3].state;" ); | ||||
195 | QCOMPARE( model->data( model->index( 3, 0 ), Qt::CheckStateRole ).toBool() ? "true" : "false", result ); | ||||
196 | | ||||
197 | result = m_document->executeScript( "ocg[3].state = true;ocgName = ocg[3].state;"); | ||||
198 | QCOMPARE( model->data( model->index( 3, 0 ), Qt::CheckStateRole ).toBool() ? "true" : "false", result ); | ||||
199 | } | ||||
200 | | ||||
201 | void KJSFunctionsTest::cleanupTestCase() | ||||
202 | { | ||||
203 | m_document->closeDocument(); | ||||
204 | delete m_document; | ||||
205 | } | ||||
206 | | ||||
207 | | ||||
208 | QTEST_MAIN( KJSFunctionsTest ) | ||||
209 | #include "kjsfunctionstest.moc" |