forked from KDE/okular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditformstest.cpp
418 lines (358 loc) · 13.9 KB
/
editformstest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
SPDX-FileCopyrightText: 2013 Jon Mease <[email protected]>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <QTest>
#include "../settings_core.h"
#include "core/document.h"
#include <QMimeDatabase>
#include <QMimeType>
#include <core/form.h>
#include <core/page.h>
class EditFormsTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void init();
void cleanup();
void testRadioButtonForm();
void testCheckBoxForm();
void testTextLineForm();
void testTextAreaForm();
void testFileEditForm();
void testComboEditForm();
void testListSingleEdit();
void testListMultiEdit();
// helper methods
void verifyRadioButtonStates(bool state1, bool state2, bool state3);
void setRadioButtonStates(bool state1, bool state2, bool state3);
void verifyTextForm(Okular::FormFieldText *form);
private:
Okular::Document *m_document;
QList<Okular::FormFieldButton *> m_radioButtonForms;
QList<Okular::FormFieldButton *> m_checkBoxForms;
Okular::FormFieldText *m_textLineForm;
Okular::FormFieldText *m_textAreaForm;
Okular::FormFieldText *m_fileEditForm;
Okular::FormFieldChoice *m_comboEdit;
Okular::FormFieldChoice *m_listSingleEdit;
Okular::FormFieldChoice *m_listMultiEdit;
};
void EditFormsTest::initTestCase()
{
Okular::SettingsCore::instance(QStringLiteral("editformstest"));
m_document = new Okular::Document(nullptr);
}
void EditFormsTest::cleanupTestCase()
{
delete m_document;
}
void EditFormsTest::init()
{
const QString testFile = QStringLiteral(KDESRCDIR "data/formSamples.pdf");
QMimeDatabase db;
const QMimeType mime = db.mimeTypeForFile(testFile);
QCOMPARE(m_document->openDocument(testFile, QUrl(), mime), Okular::Document::OpenSuccess);
// Undo and Redo should be unavailable when docuemnt is first opened.
QVERIFY(!m_document->canUndo());
QVERIFY(!m_document->canRedo());
const Okular::Page *page = m_document->page(0);
const QList<Okular::FormField *> pageFields = page->formFields();
// Clear lists
m_checkBoxForms.clear();
m_radioButtonForms.clear();
// Collect forms of the various types
for (Okular::FormField *ff : pageFields) {
ff->type();
switch (ff->type()) {
case Okular::FormField::FormButton: {
Okular::FormFieldButton *ffb = static_cast<Okular::FormFieldButton *>(ff);
switch (ffb->buttonType()) {
case Okular::FormFieldButton::Push:
break;
case Okular::FormFieldButton::CheckBox:
m_checkBoxForms.append(ffb);
break;
case Okular::FormFieldButton::Radio:
m_radioButtonForms.append(ffb);
break;
default:;
}
break;
}
case Okular::FormField::FormText: {
Okular::FormFieldText *fft = static_cast<Okular::FormFieldText *>(ff);
switch (fft->textType()) {
case Okular::FormFieldText::Multiline:
m_textAreaForm = fft;
break;
case Okular::FormFieldText::Normal:
m_textLineForm = fft;
break;
case Okular::FormFieldText::FileSelect:
m_fileEditForm = fft;
break;
}
break;
}
case Okular::FormField::FormChoice: {
Okular::FormFieldChoice *ffc = static_cast<Okular::FormFieldChoice *>(ff);
switch (ffc->choiceType()) {
case Okular::FormFieldChoice::ListBox:
if (ffc->multiSelect()) {
m_listMultiEdit = ffc;
} else {
m_listSingleEdit = ffc;
}
break;
case Okular::FormFieldChoice::ComboBox:
m_comboEdit = ffc;
break;
}
break;
}
default:;
}
}
}
void EditFormsTest::cleanup()
{
m_document->closeDocument();
}
void EditFormsTest::testRadioButtonForm()
{
// Initially the first radio button is checked
verifyRadioButtonStates(true, false, false);
// Set the second radio to checked and make sure the first
// is now unchecked and that an undo action is available
setRadioButtonStates(false, true, false);
verifyRadioButtonStates(false, true, false);
QVERIFY(m_document->canUndo());
// Now undo the action
m_document->undo();
verifyRadioButtonStates(true, false, false);
QVERIFY(!m_document->canUndo());
QVERIFY(m_document->canRedo());
// Now redo the action
m_document->redo();
verifyRadioButtonStates(false, true, false);
QVERIFY(m_document->canUndo());
QVERIFY(!m_document->canRedo());
}
void EditFormsTest::testCheckBoxForm()
{
// Examine the first and second checkboxes
// Initially both checkboxes are unchecked
QVERIFY(m_checkBoxForms[0]->state() == false);
QVERIFY(m_checkBoxForms[1]->state() == false);
// Set checkbox 1 to true
m_document->editFormButtons(0, QList<Okular::FormFieldButton *>() << m_checkBoxForms[0], QList<bool>() << true);
QVERIFY(m_checkBoxForms[0]->state() == true);
QVERIFY(m_checkBoxForms[1]->state() == false);
QVERIFY(m_document->canUndo());
// Set checkbox 2 to true
m_document->editFormButtons(0, QList<Okular::FormFieldButton *>() << m_checkBoxForms[1], QList<bool>() << true);
QVERIFY(m_checkBoxForms[0]->state() == true);
QVERIFY(m_checkBoxForms[1]->state() == true);
QVERIFY(m_document->canUndo());
// Undo checking of second checkbox
m_document->undo();
QVERIFY(m_checkBoxForms[0]->state() == true);
QVERIFY(m_checkBoxForms[1]->state() == false);
QVERIFY(m_document->canUndo());
QVERIFY(m_document->canRedo());
// Undo checking of first checkbox
m_document->undo();
QVERIFY(m_checkBoxForms[0]->state() == false);
QVERIFY(m_checkBoxForms[1]->state() == false);
QVERIFY(!m_document->canUndo());
QVERIFY(m_document->canRedo());
// Redo checking of first checkbox
m_document->redo();
QVERIFY(m_checkBoxForms[0]->state() == true);
QVERIFY(m_checkBoxForms[1]->state() == false);
QVERIFY(m_document->canUndo());
QVERIFY(m_document->canRedo());
}
void EditFormsTest::testTextLineForm()
{
verifyTextForm(m_textLineForm);
}
void EditFormsTest::testTextAreaForm()
{
verifyTextForm(m_textAreaForm);
}
void EditFormsTest::testFileEditForm()
{
verifyTextForm(m_fileEditForm);
}
void EditFormsTest::testComboEditForm()
{
// Editable combo with predefined choices:
// - combo1
// - combo2
// - combo3
// Initially no choice is selected
QCOMPARE(m_comboEdit->currentChoices().length(), 0);
QCOMPARE(m_comboEdit->editChoice(), QLatin1String(""));
// Select first choice
m_document->editFormCombo(0, m_comboEdit, QStringLiteral("combo1"), 0, 0, 0);
QCOMPARE(m_comboEdit->currentChoices().length(), 1);
QCOMPARE(m_comboEdit->currentChoices().constFirst(), 0);
QCOMPARE(m_comboEdit->editChoice(), QLatin1String(""));
QVERIFY(m_document->canUndo());
QVERIFY(!m_document->canRedo());
// Select third choice
m_document->editFormCombo(0, m_comboEdit, QStringLiteral("combo3"), 0, 0, 0);
QCOMPARE(m_comboEdit->currentChoices().length(), 1);
QCOMPARE(m_comboEdit->currentChoices().constFirst(), 2);
QCOMPARE(m_comboEdit->editChoice(), QLatin1String(""));
QVERIFY(m_document->canUndo());
QVERIFY(!m_document->canRedo());
// Undo and verify that first choice is selected
m_document->undo();
QCOMPARE(m_comboEdit->currentChoices().length(), 1);
QCOMPARE(m_comboEdit->currentChoices().constFirst(), 0);
QVERIFY(m_document->canUndo());
QVERIFY(m_document->canRedo());
// Redo and verify that third choice is selected
m_document->redo();
QCOMPARE(m_comboEdit->currentChoices().length(), 1);
QCOMPARE(m_comboEdit->currentChoices().constFirst(), 2);
QVERIFY(m_document->canUndo());
QVERIFY(!m_document->canRedo());
// Select a custom choice and verify that no predefined choices are selected
m_document->editFormCombo(0, m_comboEdit, QStringLiteral("comboEdit"), 0, 0, 0);
QCOMPARE(m_comboEdit->currentChoices().length(), 0);
QCOMPARE(m_comboEdit->editChoice(), QStringLiteral("comboEdit"));
QVERIFY(m_document->canUndo());
QVERIFY(!m_document->canRedo());
// Undo and verify that third choice is selected
m_document->undo();
QCOMPARE(m_comboEdit->currentChoices().length(), 1);
QCOMPARE(m_comboEdit->currentChoices().constFirst(), 2);
QVERIFY(m_document->canUndo());
QVERIFY(m_document->canRedo());
}
void EditFormsTest::testListSingleEdit()
{
// A list with three items that allows only single selections
// Initially no choice is selected
QCOMPARE(m_listSingleEdit->currentChoices().length(), 0);
// Select first item
m_document->editFormList(0, m_listSingleEdit, QList<int>() << 0);
QCOMPARE(m_listSingleEdit->currentChoices().length(), 1);
QCOMPARE(m_listSingleEdit->currentChoices().constFirst(), 0);
QVERIFY(m_document->canUndo());
QVERIFY(!m_document->canRedo());
// Select second item
m_document->editFormList(0, m_listSingleEdit, QList<int>() << 1);
QCOMPARE(m_listSingleEdit->currentChoices().length(), 1);
QCOMPARE(m_listSingleEdit->currentChoices().constFirst(), 1);
QVERIFY(m_document->canUndo());
QVERIFY(!m_document->canRedo());
// Undo and verify that first item is selected
m_document->undo();
QCOMPARE(m_listSingleEdit->currentChoices().length(), 1);
QCOMPARE(m_listSingleEdit->currentChoices().constFirst(), 0);
QVERIFY(m_document->canUndo());
QVERIFY(m_document->canRedo());
// Redo and verify that second item is selected
m_document->redo();
QCOMPARE(m_listSingleEdit->currentChoices().length(), 1);
QCOMPARE(m_listSingleEdit->currentChoices().constFirst(), 1);
QVERIFY(m_document->canUndo());
QVERIFY(!m_document->canRedo());
}
void EditFormsTest::testListMultiEdit()
{
// A list with three items that allows for multiple selections
// Initially no choice is selected
QCOMPARE(m_listMultiEdit->currentChoices().length(), 0);
// Select first item
m_document->editFormList(0, m_listMultiEdit, QList<int>() << 0);
QCOMPARE(m_listMultiEdit->currentChoices(), QList<int>() << 0);
QVERIFY(m_document->canUndo());
QVERIFY(!m_document->canRedo());
// Select first and third items
m_document->editFormList(0, m_listMultiEdit, QList<int>() << 0 << 2);
QCOMPARE(m_listMultiEdit->currentChoices(), QList<int>() << 0 << 2);
QVERIFY(m_document->canUndo());
QVERIFY(!m_document->canRedo());
// Select all three items
m_document->editFormList(0, m_listMultiEdit, QList<int>() << 0 << 1 << 2);
QCOMPARE(m_listMultiEdit->currentChoices(), QList<int>() << 0 << 1 << 2);
QVERIFY(m_document->canUndo());
QVERIFY(!m_document->canRedo());
// Undo and verify that first and third items are selected
m_document->undo();
QCOMPARE(m_listMultiEdit->currentChoices(), QList<int>() << 0 << 2);
QVERIFY(m_document->canUndo());
QVERIFY(m_document->canRedo());
// Undo and verify that first item is selected
m_document->undo();
QCOMPARE(m_listMultiEdit->currentChoices(), QList<int>() << 0);
QVERIFY(m_document->canUndo());
QVERIFY(m_document->canRedo());
// Redo and verify that first and third items are selected
m_document->redo();
QCOMPARE(m_listMultiEdit->currentChoices(), QList<int>() << 0 << 2);
QVERIFY(m_document->canUndo());
QVERIFY(m_document->canRedo());
}
// helper methods
void EditFormsTest::verifyRadioButtonStates(bool state1, bool state2, bool state3)
{
QVERIFY(m_radioButtonForms[0]->state() == state1);
QVERIFY(m_radioButtonForms[1]->state() == state2);
QVERIFY(m_radioButtonForms[2]->state() == state3);
}
void EditFormsTest::setRadioButtonStates(bool state1, bool state2, bool state3)
{
QList<bool> newButtonStates;
newButtonStates.append(state1);
newButtonStates.append(state2);
newButtonStates.append(state3);
m_document->editFormButtons(0, m_radioButtonForms, newButtonStates);
}
void EditFormsTest::verifyTextForm(Okular::FormFieldText *form)
{
// Text in form is initially empty
QCOMPARE(form->text(), QLatin1String(""));
// Insert the string "Hello" into the form
m_document->editFormText(0, form, QStringLiteral("Hello"), 5, 0, 0);
QCOMPARE(form->text(), QStringLiteral("Hello"));
QVERIFY(m_document->canUndo());
QVERIFY(!m_document->canRedo());
// Undo the insertion and verify that form is empty again
m_document->undo();
QCOMPARE(form->text(), QLatin1String(""));
QVERIFY(!m_document->canUndo());
QVERIFY(m_document->canRedo());
// Redo the insertion of "Hello"
m_document->redo();
QCOMPARE(form->text(), QStringLiteral("Hello"));
QVERIFY(m_document->canUndo());
QVERIFY(!m_document->canRedo());
// Type "_World" after "Hello"
m_document->editFormText(0, form, QStringLiteral("Hello_"), 6, 5, 5);
m_document->editFormText(0, form, QStringLiteral("Hello_W"), 7, 6, 6);
m_document->editFormText(0, form, QStringLiteral("Hello_Wo"), 8, 7, 7);
m_document->editFormText(0, form, QStringLiteral("Hello_Wor"), 9, 8, 8);
m_document->editFormText(0, form, QStringLiteral("Hello_Worl"), 10, 9, 9);
m_document->editFormText(0, form, QStringLiteral("Hello_World"), 11, 10, 10);
// Verify that character insertion operations were merged together into a single undo command
m_document->undo();
QCOMPARE(form->text(), QStringLiteral("Hello"));
QVERIFY(m_document->canUndo());
QVERIFY(m_document->canRedo());
// Verify that one more undo gets us back to the original state (empty form)
m_document->undo();
QCOMPARE(form->text(), QLatin1String(""));
QVERIFY(!m_document->canUndo());
QVERIFY(m_document->canRedo());
}
QTEST_MAIN(EditFormsTest)
#include "editformstest.moc"