Changeset View
Changeset View
Standalone View
Standalone View
autotests/kjsfunctionstest.cpp
1 | /*************************************************************************** | 1 | /*************************************************************************** | ||
---|---|---|---|---|---|
2 | * Copyright (C) 2018 by Intevation GmbH <intevation@intevation.de> * | 2 | * Copyright (C) 2018 by Intevation GmbH <intevation@intevation.de> * | ||
3 | * * | 3 | * * | ||
4 | * This program is free software; you can redistribute it and/or modify * | 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 * | 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 * | 6 | * the Free Software Foundation; either version 2 of the License, or * | ||
7 | * (at your option) any later version. * | 7 | * (at your option) any later version. * | ||
8 | ***************************************************************************/ | 8 | ***************************************************************************/ | ||
9 | 9 | | |||
10 | #include <QtTest> | 10 | #include <QtTest> | ||
11 | 11 | | |||
12 | #include <QAbstractItemModel> | ||||
12 | #include <QMap> | 13 | #include <QMap> | ||
13 | #include <QMimeType> | 14 | #include <QMimeType> | ||
14 | #include <QMimeDatabase> | 15 | #include <QMimeDatabase> | ||
15 | #include <QThread> | 16 | #include <QThread> | ||
16 | #include "../settings_core.h" | 17 | #include "../settings_core.h" | ||
17 | #include "core/document.h" | 18 | #include "core/document.h" | ||
18 | #include "core/scripter.h" | 19 | #include "core/scripter.h" | ||
19 | #include <core/script/event_p.h> | 20 | #include <core/script/event_p.h> | ||
20 | #include <core/page.h> | 21 | #include <core/page.h> | ||
21 | #include <core/form.h> | 22 | #include <core/form.h> | ||
22 | 23 | | |||
23 | #include "../generators/poppler/config-okular-poppler.h" | 24 | #include "../generators/poppler/config-okular-poppler.h" | ||
24 | 25 | | |||
25 | class KJSFunctionsTest: public QObject | 26 | class KJSFunctionsTest: public QObject | ||
26 | { | 27 | { | ||
27 | Q_OBJECT | 28 | Q_OBJECT | ||
28 | 29 | | |||
29 | private slots: | 30 | private slots: | ||
30 | void initTestCase(); | 31 | void initTestCase(); | ||
31 | void testNumFields(); | 32 | void testNumFields(); | ||
32 | void testNthFieldName(); | 33 | void testNthFieldName(); | ||
33 | void testDisplay(); | 34 | void testDisplay(); | ||
34 | void testSetClearInterval(); | 35 | void testSetClearInterval(); | ||
35 | void testSetClearTimeOut(); | 36 | void testSetClearTimeOut(); | ||
37 | void testGetOCGs(); | ||||
38 | void testOCGSetState(); | ||||
36 | void cleanupTestCase(); | 39 | void cleanupTestCase(); | ||
37 | 40 | | |||
38 | private: | 41 | private: | ||
39 | Okular::Document *m_document; | 42 | Okular::Document *m_document; | ||
40 | QMap<QString, Okular::FormField*> m_fields; | 43 | QMap<QString, Okular::FormField*> m_fields; | ||
41 | }; | 44 | }; | ||
42 | 45 | | |||
43 | void KJSFunctionsTest::initTestCase() | 46 | void KJSFunctionsTest::initTestCase() | ||
Show All 11 Lines | 47 | { | |||
55 | { | 58 | { | ||
56 | m_fields.insert( ff->name(), ff ); | 59 | m_fields.insert( ff->name(), ff ); | ||
57 | } | 60 | } | ||
58 | } | 61 | } | ||
59 | 62 | | |||
60 | void KJSFunctionsTest::testNumFields() | 63 | void KJSFunctionsTest::testNumFields() | ||
61 | { | 64 | { | ||
62 | QString result = m_document->executeScript( "Doc.numFields" ); | 65 | QString result = m_document->executeScript( "Doc.numFields" ); | ||
63 | QCOMPARE( "22", result ); | 66 | QCOMPARE( "31", result ); | ||
64 | } | 67 | } | ||
65 | 68 | | |||
66 | void KJSFunctionsTest::testNthFieldName() | 69 | void KJSFunctionsTest::testNthFieldName() | ||
67 | { | 70 | { | ||
68 | for(int i = 0;i < 21;++i) | 71 | for(int i = 0;i < 21;++i) | ||
69 | { | 72 | { | ||
70 | QString result = m_document->executeScript( QString( "Doc.getNthFieldName(%1)" ).arg( i ) ); | 73 | QString result = m_document->executeScript( QString( "Doc.getNthFieldName(%1)" ).arg( i ) ); | ||
71 | QCOMPARE( result, QString( "0.%1" ).arg(i) ); | 74 | QCOMPARE( result, QString( "0.%1" ).arg(i) ); | ||
Show All 12 Lines | 79 | { | |||
84 | QCOMPARE( false, m_fields["0.10"]->isVisible() ); | 87 | QCOMPARE( false, m_fields["0.10"]->isVisible() ); | ||
85 | QCOMPARE( true, m_fields["0.15"]->isVisible() ); | 88 | QCOMPARE( true, m_fields["0.15"]->isVisible() ); | ||
86 | 89 | | |||
87 | m_document->executeScript( "field = Doc.getField(\"0.15\");field.display=display.hidden;\ | 90 | m_document->executeScript( "field = Doc.getField(\"0.15\");field.display=display.hidden;\ | ||
88 | field = Doc.getField(\"0.20\");field.display=display.visible;" ); | 91 | field = Doc.getField(\"0.20\");field.display=display.visible;" ); | ||
89 | QCOMPARE( false, m_fields["0.15"]->isVisible() ); | 92 | QCOMPARE( false, m_fields["0.15"]->isVisible() ); | ||
90 | QCOMPARE( true, m_fields["0.20"]->isVisible() ); | 93 | QCOMPARE( true, m_fields["0.20"]->isVisible() ); | ||
91 | 94 | | |||
92 | | ||||
93 | m_document->executeScript( "field = Doc.getField(\"0.20\");field.display=display.hidden;\ | 95 | m_document->executeScript( "field = Doc.getField(\"0.20\");field.display=display.hidden;\ | ||
94 | field = Doc.getField(\"0.0\");field.display=display.visible;" ); | 96 | field = Doc.getField(\"0.0\");field.display=display.visible;" ); | ||
95 | QCOMPARE( false, m_fields["0.20"]->isVisible() ); | 97 | QCOMPARE( false, m_fields["0.20"]->isVisible() ); | ||
96 | QCOMPARE( true, m_fields["0.0"]->isVisible() ); | 98 | QCOMPARE( true, m_fields["0.0"]->isVisible() ); | ||
97 | } | 99 | } | ||
98 | 100 | | |||
99 | void delay() | 101 | void delay() | ||
100 | { | 102 | { | ||
101 | QTime dieTime= QTime::currentTime().addSecs( 2 ); | 103 | QTime dieTime= QTime::currentTime().addSecs( 2 ); | ||
102 | while (QTime::currentTime() < dieTime) | 104 | while (QTime::currentTime() < dieTime) | ||
103 | QCoreApplication::processEvents(QEventLoop::AllEvents, 100); | 105 | QCoreApplication::processEvents(QEventLoop::AllEvents, 100); | ||
104 | } | 106 | } | ||
105 | 107 | | |||
106 | void KJSFunctionsTest::testSetClearInterval() | 108 | void KJSFunctionsTest::testSetClearInterval() | ||
107 | { | 109 | { | ||
108 | QString result = m_document->executeScript( "obj = new Object();obj.idx=0;\ | 110 | QString result = m_document->executeScript( "obj = new Object();obj.idx=0;\ | ||
109 | obj.inc=function(){obj.idx = obj.idx + 1;};\ | 111 | obj.inc=function(){obj.idx = obj.idx + 1;};\ | ||
110 | intv = app.setInterval('obj.inc()', 450);obj.idx;" ); | 112 | intv = app.setInterval('obj.inc()', 450);obj.idx;" ); | ||
111 | QCOMPARE( "0", result ); | 113 | QCOMPARE( "0", result ); | ||
112 | delay(); | 114 | delay(); | ||
115 | | ||||
113 | result = m_document->executeScript( "app.clearInterval(intv);obj.idx;"); | 116 | result = m_document->executeScript( "app.clearInterval(intv);obj.idx;"); | ||
114 | QCOMPARE( "4", result ); | 117 | QCOMPARE( "4", result ); | ||
115 | } | 118 | } | ||
116 | 119 | | |||
117 | void KJSFunctionsTest::testSetClearTimeOut() | 120 | void KJSFunctionsTest::testSetClearTimeOut() | ||
118 | { | 121 | { | ||
119 | QString result = m_document->executeScript( "intv = app.setTimeOut('obj.inc()', 1);obj.idx;" ); | 122 | QString result = m_document->executeScript( "intv = app.setTimeOut('obj.inc()', 1);obj.idx;" ); | ||
120 | QCOMPARE( "4", result ); | 123 | QCOMPARE( "4", result ); | ||
121 | delay(); | 124 | delay(); | ||
125 | | ||||
122 | result = m_document->executeScript( "obj.idx;" ); | 126 | result = m_document->executeScript( "obj.idx;" ); | ||
123 | QCOMPARE( "5", result ); | 127 | QCOMPARE( "5", result ); | ||
128 | | ||||
124 | result = m_document->executeScript( "intv = app.setTimeOut('obj.inc()', 2000);obj.idx;" ); | 129 | result = m_document->executeScript( "intv = app.setTimeOut('obj.inc()', 2000);obj.idx;" ); | ||
125 | QCOMPARE( "5", result ); | 130 | QCOMPARE( "5", result ); | ||
131 | | ||||
126 | result = m_document->executeScript( "app.clearTimeOut(intv);obj.idx;" ); | 132 | result = m_document->executeScript( "app.clearTimeOut(intv);obj.idx;" ); | ||
127 | QCOMPARE( "5", result ); | 133 | QCOMPARE( "5", result ); | ||
128 | delay(); | 134 | delay(); | ||
129 | QCOMPARE( "5", result ); | 135 | QCOMPARE( "5", result ); | ||
130 | } | 136 | } | ||
131 | 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 | | ||||
132 | void KJSFunctionsTest::cleanupTestCase() | 201 | void KJSFunctionsTest::cleanupTestCase() | ||
133 | { | 202 | { | ||
134 | m_document->closeDocument(); | 203 | m_document->closeDocument(); | ||
135 | delete m_document; | 204 | delete m_document; | ||
136 | } | 205 | } | ||
137 | 206 | | |||
138 | 207 | | |||
139 | QTEST_MAIN( KJSFunctionsTest ) | 208 | QTEST_MAIN( KJSFunctionsTest ) | ||
140 | #include "kjsfunctionstest.moc" | 209 | #include "kjsfunctionstest.moc" |