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
|
// Copyright 2017 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "fpdfsdk/cba_annotiterator.h"
#include "fpdfsdk/cpdfsdk_annot.h"
#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
#include "fpdfsdk/formfiller/cffl_formfiller.h"
#include "fpdfsdk/formfiller/cffl_interactiveformfiller.h"
#include "fpdfsdk/pwl/cpwl_combo_box.h"
#include "fpdfsdk/pwl/cpwl_wnd.h"
#include "testing/embedder_test.h"
#include "testing/gtest/include/gtest/gtest.h"
class CPWLComboBoxEditEmbeddertest : public EmbedderTest {
protected:
void SetUp() override {
EmbedderTest::SetUp();
CreateAndInitializeFormComboboxPDF();
}
void TearDown() override {
UnloadPage(GetPage());
EmbedderTest::TearDown();
}
void CreateAndInitializeFormComboboxPDF() {
EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
m_page = LoadPage(0);
ASSERT_TRUE(m_page);
m_pFormFillEnv = static_cast<CPDFSDK_FormFillEnvironment*>(form_handle());
CBA_AnnotIterator iter(m_pFormFillEnv->GetPageView(0),
CPDF_Annot::Subtype::WIDGET);
// User editable combobox.
m_pAnnotEditable = iter.GetFirstAnnot();
ASSERT_TRUE(m_pAnnotEditable);
ASSERT_EQ(CPDF_Annot::Subtype::WIDGET, m_pAnnotEditable->GetAnnotSubtype());
// Normal combobox with pre-selected value.
m_pAnnotNormal = iter.GetNextAnnot(m_pAnnotEditable);
ASSERT_TRUE(m_pAnnotNormal);
ASSERT_EQ(CPDF_Annot::Subtype::WIDGET, m_pAnnotNormal->GetAnnotSubtype());
// Read-only combobox.
CPDFSDK_Annot* pAnnotReadOnly = iter.GetNextAnnot(m_pAnnotNormal);
CPDFSDK_Annot* pLastAnnot = iter.GetLastAnnot();
ASSERT_EQ(pAnnotReadOnly, pLastAnnot);
}
void FormFillerAndWindowSetup(CPDFSDK_Annot* pAnnotCombobox) {
CFFL_InteractiveFormFiller* pInteractiveFormFiller =
m_pFormFillEnv->GetInteractiveFormFiller();
{
CPDFSDK_Annot::ObservedPtr pObserved(pAnnotCombobox);
EXPECT_TRUE(pInteractiveFormFiller->OnSetFocus(&pObserved, 0));
}
m_pFormFiller =
pInteractiveFormFiller->GetFormFiller(pAnnotCombobox, false);
ASSERT_TRUE(m_pFormFiller);
CPWL_Wnd* pWindow =
m_pFormFiller->GetPDFWindow(m_pFormFillEnv->GetPageView(0), false);
ASSERT_TRUE(pWindow);
ASSERT_EQ("CPWL_ComboBox", pWindow->GetClassName());
m_pComboBox = static_cast<CPWL_ComboBox*>(pWindow);
}
void TypeTextIntoTextField(int num_chars) {
// Type text starting with 'A' to as many chars as specified by |num_chars|.
for (int i = 0; i < num_chars; ++i) {
EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnotUserEditable(),
i + 'A', 0));
}
}
FPDF_PAGE GetPage() const { return m_page; }
CPWL_ComboBox* GetCPWLComboBox() const { return m_pComboBox; }
CFFL_FormFiller* GetCFFLFormFiller() const { return m_pFormFiller; }
CPDFSDK_Annot* GetCPDFSDKAnnotNormal() const { return m_pAnnotNormal; }
CPDFSDK_Annot* GetCPDFSDKAnnotUserEditable() const {
return m_pAnnotEditable;
}
CPDFSDK_FormFillEnvironment* GetCPDFSDKFormFillEnv() const {
return m_pFormFillEnv;
}
private:
FPDF_PAGE m_page;
CPWL_ComboBox* m_pComboBox;
CFFL_FormFiller* m_pFormFiller;
CPDFSDK_Annot* m_pAnnotNormal;
CPDFSDK_Annot* m_pAnnotEditable;
CPDFSDK_FormFillEnvironment* m_pFormFillEnv;
};
TEST_F(CPWLComboBoxEditEmbeddertest, GetSelectedTextEmptyAndBasicNormal) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotNormal());
// Automatically pre-filled with "Banana".
EXPECT_FALSE(GetCPWLComboBox()->GetText().IsEmpty());
EXPECT_STREQ(L"Banana", GetCPWLComboBox()->GetText().c_str());
// Check that selection is intially empty, then select entire word.
EXPECT_TRUE(GetCPWLComboBox()->GetSelectedText().IsEmpty());
GetCPWLComboBox()->SetSelectText();
EXPECT_STREQ(L"Banana", GetCPWLComboBox()->GetSelectedText().c_str());
// Select other options.
GetCPWLComboBox()->SetSelect(0);
EXPECT_STREQ(L"Apple", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->SetSelect(2);
EXPECT_STREQ(L"Cherry", GetCPWLComboBox()->GetSelectedText().c_str());
// Verify that combobox text cannot be edited.
EXPECT_FALSE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnotNormal(), 'a', 0));
}
TEST_F(CPWLComboBoxEditEmbeddertest, GetSelectedTextFragmentsNormal) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotNormal());
EXPECT_STREQ(L"Banana", GetCPWLComboBox()->GetText().c_str());
GetCPWLComboBox()->SetEditSelection(0, 0);
EXPECT_TRUE(GetCPWLComboBox()->GetSelectedText().IsEmpty());
GetCPWLComboBox()->SetEditSelection(0, 1);
EXPECT_STREQ(L"B", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->SetEditSelection(0, -1);
EXPECT_STREQ(L"Banana", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->SetEditSelection(-8, -1);
EXPECT_TRUE(GetCPWLComboBox()->GetSelectedText().IsEmpty());
GetCPWLComboBox()->SetEditSelection(4, 1);
EXPECT_STREQ(L"ana", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->SetEditSelection(1, 4);
EXPECT_STREQ(L"ana", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->SetEditSelection(5, 6);
EXPECT_STREQ(L"a", GetCPWLComboBox()->GetSelectedText().c_str());
}
TEST_F(CPWLComboBoxEditEmbeddertest, GetSelectedTextEmptyAndBasicEditable) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
EXPECT_TRUE(GetCPWLComboBox()->GetText().IsEmpty());
// Check selection is intially empty, then select a provided option.
EXPECT_TRUE(GetCPWLComboBox()->GetSelectedText().IsEmpty());
GetCPWLComboBox()->SetSelect(0);
GetCPWLComboBox()->SetSelectText();
EXPECT_STREQ(L"Foo", GetCPWLComboBox()->GetSelectedText().c_str());
// Select another option and then select last char of that option.
GetCPWLComboBox()->SetSelect(1);
EXPECT_STREQ(L"Bar", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->SetEditSelection(2, 3);
EXPECT_STREQ(L"r", GetCPWLComboBox()->GetSelectedText().c_str());
// Type into editable combobox text field and select new text.
EXPECT_TRUE(
GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnotUserEditable(), 'a', 0));
EXPECT_TRUE(
GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnotUserEditable(), 'b', 0));
EXPECT_TRUE(
GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnotUserEditable(), 'c', 0));
EXPECT_TRUE(GetCPWLComboBox()->GetSelectedText().IsEmpty());
GetCPWLComboBox()->SetEditSelection(0, 5);
EXPECT_STREQ(L"Baabc", GetCPWLComboBox()->GetSelectedText().c_str());
}
TEST_F(CPWLComboBoxEditEmbeddertest, GetSelectedTextFragmentsEditable) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
TypeTextIntoTextField(50);
GetCPWLComboBox()->SetEditSelection(0, 0);
EXPECT_TRUE(GetCPWLComboBox()->GetSelectedText().IsEmpty());
GetCPWLComboBox()->SetEditSelection(0, 1);
EXPECT_STREQ(L"A", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->SetEditSelection(0, -1);
EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->SetEditSelection(-8, -1);
EXPECT_TRUE(GetCPWLComboBox()->GetSelectedText().IsEmpty());
GetCPWLComboBox()->SetEditSelection(23, 12);
EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->SetEditSelection(12, 23);
EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->SetEditSelection(49, 50);
EXPECT_STREQ(L"r", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->SetEditSelection(49, 55);
EXPECT_STREQ(L"r", GetCPWLComboBox()->GetSelectedText().c_str());
}
TEST_F(CPWLComboBoxEditEmbeddertest, DeleteEntireTextSelection) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
TypeTextIntoTextField(50);
GetCPWLComboBox()->SetEditSelection(0, -1);
EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->ReplaceSelection(L"");
EXPECT_TRUE(GetCPWLComboBox()->GetText().IsEmpty());
}
TEST_F(CPWLComboBoxEditEmbeddertest, DeleteTextSelectionMiddle) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
TypeTextIntoTextField(50);
GetCPWLComboBox()->SetEditSelection(12, 23);
EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->ReplaceSelection(L"");
EXPECT_STREQ(L"ABCDEFGHIJKLXYZ[\\]^_`abcdefghijklmnopqr",
GetCPWLComboBox()->GetText().c_str());
}
TEST_F(CPWLComboBoxEditEmbeddertest, DeleteTextSelectionLeft) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
TypeTextIntoTextField(50);
GetCPWLComboBox()->SetEditSelection(0, 5);
EXPECT_STREQ(L"ABCDE", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->ReplaceSelection(L"");
EXPECT_STREQ(L"FGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
GetCPWLComboBox()->GetText().c_str());
}
TEST_F(CPWLComboBoxEditEmbeddertest, DeleteTextSelectionRight) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
TypeTextIntoTextField(50);
GetCPWLComboBox()->SetEditSelection(45, 50);
EXPECT_STREQ(L"nopqr", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->ReplaceSelection(L"");
EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm",
GetCPWLComboBox()->GetText().c_str());
}
TEST_F(CPWLComboBoxEditEmbeddertest, DeleteEmptyTextSelection) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
TypeTextIntoTextField(50);
GetCPWLComboBox()->ReplaceSelection(L"");
EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
GetCPWLComboBox()->GetText().c_str());
}
TEST_F(CPWLComboBoxEditEmbeddertest, InsertTextInEmptyEditableComboBox) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
GetCPWLComboBox()->ReplaceSelection(L"Hello");
EXPECT_STREQ(L"Hello", GetCPWLComboBox()->GetText().c_str());
}
TEST_F(CPWLComboBoxEditEmbeddertest,
InsertTextInPopulatedEditableComboBoxLeft) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
TypeTextIntoTextField(10);
// Move cursor to beginning of user-editable combobox text field.
EXPECT_TRUE(GetCFFLFormFiller()->OnKeyDown(GetCPDFSDKAnnotUserEditable(),
FWL_VKEY_Home, 0));
GetCPWLComboBox()->ReplaceSelection(L"Hello");
EXPECT_STREQ(L"HelloABCDEFGHIJ", GetCPWLComboBox()->GetText().c_str());
}
TEST_F(CPWLComboBoxEditEmbeddertest,
InsertTextInPopulatedEditableComboBoxMiddle) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
TypeTextIntoTextField(10);
// Move cursor to middle of user-editable combobox text field.
for (int i = 0; i < 5; ++i) {
EXPECT_TRUE(GetCFFLFormFiller()->OnKeyDown(GetCPDFSDKAnnotUserEditable(),
FWL_VKEY_Left, 0));
}
GetCPWLComboBox()->ReplaceSelection(L"Hello");
EXPECT_STREQ(L"ABCDEHelloFGHIJ", GetCPWLComboBox()->GetText().c_str());
}
TEST_F(CPWLComboBoxEditEmbeddertest,
InsertTextInPopulatedEditableComboBoxRight) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
TypeTextIntoTextField(10);
GetCPWLComboBox()->ReplaceSelection(L"Hello");
EXPECT_STREQ(L"ABCDEFGHIJHello", GetCPWLComboBox()->GetText().c_str());
}
TEST_F(CPWLComboBoxEditEmbeddertest,
InsertTextAndReplaceSelectionInPopulatedEditableComboBoxWhole) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
TypeTextIntoTextField(10);
GetCPWLComboBox()->SetEditSelection(0, -1);
EXPECT_STREQ(L"ABCDEFGHIJ", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->ReplaceSelection(L"Hello");
EXPECT_STREQ(L"Hello", GetCPWLComboBox()->GetText().c_str());
}
TEST_F(CPWLComboBoxEditEmbeddertest,
InsertTextAndReplaceSelectionInPopulatedEditableComboBoxLeft) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
TypeTextIntoTextField(10);
GetCPWLComboBox()->SetEditSelection(0, 5);
EXPECT_STREQ(L"ABCDE", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->ReplaceSelection(L"Hello");
EXPECT_STREQ(L"HelloFGHIJ", GetCPWLComboBox()->GetText().c_str());
}
TEST_F(CPWLComboBoxEditEmbeddertest,
InsertTextAndReplaceSelectionInPopulatedEditableComboBoxMiddle) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
TypeTextIntoTextField(10);
GetCPWLComboBox()->SetEditSelection(2, 7);
EXPECT_STREQ(L"CDEFG", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->ReplaceSelection(L"Hello");
EXPECT_STREQ(L"ABHelloHIJ", GetCPWLComboBox()->GetText().c_str());
}
TEST_F(CPWLComboBoxEditEmbeddertest,
InsertTextAndReplaceSelectionInPopulatedEditableComboBoxRight) {
FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
TypeTextIntoTextField(10);
GetCPWLComboBox()->SetEditSelection(5, 10);
EXPECT_STREQ(L"FGHIJ", GetCPWLComboBox()->GetSelectedText().c_str());
GetCPWLComboBox()->ReplaceSelection(L"Hello");
EXPECT_STREQ(L"ABCDEHello", GetCPWLComboBox()->GetText().c_str());
}
|