Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cpwl_edit_embeddertest.cpp
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "fpdfsdk/pwl/cpwl_edit.h"
6
7#include <utility>
8
9#include "fpdfsdk/cpdfsdk_annotiterator.h"
10#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
11#include "fpdfsdk/cpdfsdk_helpers.h"
12#include "fpdfsdk/cpdfsdk_widget.h"
13#include "fpdfsdk/formfiller/cffl_formfield.h"
14#include "fpdfsdk/formfiller/cffl_interactiveformfiller.h"
15#include "public/fpdf_fwlevent.h"
16#include "testing/embedder_test.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
20 protected:
25
30
32 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
33 m_page = LoadPage(0);
34 ASSERT_TRUE(m_page);
35
36 m_pFormFillEnv =
37 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(form_handle());
40 // Normal text field.
42 ASSERT_TRUE(m_pAnnot);
43
44 // Read-only text field.
45 CPDFSDK_Annot* pAnnotReadOnly = iter.GetNextAnnot(m_pAnnot);
46
47 // Pre-filled text field with char limit of 10.
48 m_pAnnotCharLimit = ToCPDFSDKWidget(iter.GetNextAnnot(pAnnotReadOnly));
49 ASSERT_TRUE(m_pAnnotCharLimit);
50
51 // Password text field.
52 CPDFSDK_Annot* password_annot = iter.GetNextAnnot(m_pAnnotCharLimit);
53 ASSERT_TRUE(password_annot);
54 ASSERT_EQ(CPDF_Annot::Subtype::WIDGET, password_annot->GetAnnotSubtype());
55
56 CPDFSDK_Annot* pLastAnnot = iter.GetLastAnnot();
57 ASSERT_EQ(password_annot, pLastAnnot);
58 }
59
60 void FormFillerAndWindowSetup(CPDFSDK_Widget* pAnnotTextField) {
61 CFFL_InteractiveFormFiller* pInteractiveFormFiller =
62 m_pFormFillEnv->GetInteractiveFormFiller();
63 {
64 ObservedPtr<CPDFSDK_Widget> pObserved(pAnnotTextField);
65 EXPECT_TRUE(pInteractiveFormFiller->OnSetFocus(pObserved, {}));
66 }
67
68 m_pFormFiller =
69 pInteractiveFormFiller->GetFormFieldForTesting(pAnnotTextField);
70 ASSERT_TRUE(m_pFormFiller);
71
72 CPWL_Wnd* pWindow =
73 m_pFormFiller->GetPWLWindow(m_pFormFillEnv->GetPageViewAtIndex(0));
74 ASSERT_TRUE(pWindow);
75 m_pEdit = static_cast<CPWL_Edit*>(pWindow);
76 }
77
78 void TypeTextIntoTextField(int num_chars) {
79 // Type text starting with 'A' to as many chars as specified by |num_chars|.
80 for (int i = 0; i < num_chars; ++i) {
81 EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), i + 'A', {}));
82 }
83 }
84
85 FPDF_PAGE GetPage() { return m_page; }
86 CPWL_Edit* GetCPWLEdit() { return m_pEdit; }
87 CFFL_FormField* GetCFFLFormFiller() { return m_pFormFiller; }
88 CPDFSDK_Widget* GetCPDFSDKAnnot() { return m_pAnnot; }
89 CPDFSDK_Widget* GetCPDFSDKAnnotCharLimit() { return m_pAnnotCharLimit; }
90
91 private:
92 FPDF_PAGE m_page;
93 CPWL_Edit* m_pEdit;
94 CFFL_FormField* m_pFormFiller;
95 CPDFSDK_Widget* m_pAnnot;
96 CPDFSDK_Widget* m_pAnnotCharLimit;
97 CPDFSDK_FormFillEnvironment* m_pFormFillEnv;
98};
99
101 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
102 EXPECT_TRUE(GetCPWLEdit()->GetText().IsEmpty());
103 EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'a', {}));
104 EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'b', {}));
105 EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'c', {}));
106
107 EXPECT_STREQ(L"abc", GetCPWLEdit()->GetText().c_str());
108}
109
111 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
112 // Attempt to set selection before text has been typed to test that
113 // selection is identified as empty.
114 //
115 // Select from character index [0, 3) within form text field.
116 GetCPWLEdit()->SetSelection(0, 3);
117 EXPECT_TRUE(GetCPWLEdit()->GetSelectedText().IsEmpty());
118
119 EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'a', {}));
120 EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'b', {}));
121 EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'c', {}));
122 GetCPWLEdit()->SetSelection(0, 2);
123
124 EXPECT_STREQ(L"ab", GetCPWLEdit()->GetSelectedText().c_str());
125}
126
128 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
129 TypeTextIntoTextField(50);
130
131 GetCPWLEdit()->SetSelection(0, 0);
132 EXPECT_TRUE(GetCPWLEdit()->GetSelectedText().IsEmpty());
133
134 GetCPWLEdit()->SetSelection(0, 1);
135 EXPECT_STREQ(L"A", GetCPWLEdit()->GetSelectedText().c_str());
136
137 GetCPWLEdit()->SetSelection(0, -1);
138 EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
139 GetCPWLEdit()->GetSelectedText().c_str());
140
141 GetCPWLEdit()->SetSelection(-8, -1);
142 EXPECT_TRUE(GetCPWLEdit()->GetSelectedText().IsEmpty());
143
144 GetCPWLEdit()->SetSelection(23, 12);
145 EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLEdit()->GetSelectedText().c_str());
146
147 GetCPWLEdit()->SetSelection(12, 23);
148 EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLEdit()->GetSelectedText().c_str());
149
150 GetCPWLEdit()->SetSelection(49, 50);
151 EXPECT_STREQ(L"r", GetCPWLEdit()->GetSelectedText().c_str());
152
153 GetCPWLEdit()->SetSelection(49, 55);
154 EXPECT_STREQ(L"r", GetCPWLEdit()->GetSelectedText().c_str());
155}
156
158 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
159 TypeTextIntoTextField(50);
160
161 GetCPWLEdit()->SetSelection(0, -1);
162 EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
163 GetCPWLEdit()->GetSelectedText().c_str());
164
165 GetCPWLEdit()->ReplaceSelection(L"");
166 EXPECT_TRUE(GetCPWLEdit()->GetText().IsEmpty());
167}
168
170 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
171 TypeTextIntoTextField(50);
172
173 GetCPWLEdit()->SetSelection(12, 23);
174 EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLEdit()->GetSelectedText().c_str());
175
176 GetCPWLEdit()->ReplaceSelection(L"");
177 EXPECT_STREQ(L"ABCDEFGHIJKLXYZ[\\]^_`abcdefghijklmnopqr",
178 GetCPWLEdit()->GetText().c_str());
179}
180
182 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
183 TypeTextIntoTextField(50);
184
185 GetCPWLEdit()->SetSelection(0, 5);
186 EXPECT_STREQ(L"ABCDE", GetCPWLEdit()->GetSelectedText().c_str());
187
188 GetCPWLEdit()->ReplaceSelection(L"");
189 EXPECT_STREQ(L"FGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
190 GetCPWLEdit()->GetText().c_str());
191}
192
194 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
195 TypeTextIntoTextField(50);
196
197 GetCPWLEdit()->SetSelection(45, 50);
198 EXPECT_STREQ(L"nopqr", GetCPWLEdit()->GetSelectedText().c_str());
199
200 GetCPWLEdit()->ReplaceSelection(L"");
201 EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm",
202 GetCPWLEdit()->GetText().c_str());
203}
204
206 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
207 TypeTextIntoTextField(50);
208
209 GetCPWLEdit()->ReplaceSelection(L"");
210 EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
211 GetCPWLEdit()->GetText().c_str());
212}
213
215 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
216 GetCPWLEdit()->ReplaceSelection(L"Hello");
217 EXPECT_STREQ(L"Hello", GetCPWLEdit()->GetText().c_str());
218}
219
221 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
222 TypeTextIntoTextField(10);
223
224 // Move cursor to beginning of text field.
225 EXPECT_TRUE(GetCFFLFormFiller()->OnKeyDown(FWL_VKEY_Home, {}));
226
227 GetCPWLEdit()->ReplaceSelection(L"Hello");
228 EXPECT_STREQ(L"HelloABCDEFGHIJ", GetCPWLEdit()->GetText().c_str());
229}
230
232 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
233 TypeTextIntoTextField(10);
234
235 // Move cursor to middle of text field.
236 for (int i = 0; i < 5; ++i) {
237 EXPECT_TRUE(GetCFFLFormFiller()->OnKeyDown(FWL_VKEY_Left, {}));
238 }
239
240 GetCPWLEdit()->ReplaceSelection(L"Hello");
241 EXPECT_STREQ(L"ABCDEHelloFGHIJ", GetCPWLEdit()->GetText().c_str());
242}
243
245 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
246 TypeTextIntoTextField(10);
247
248 GetCPWLEdit()->ReplaceSelection(L"Hello");
249 EXPECT_STREQ(L"ABCDEFGHIJHello", GetCPWLEdit()->GetText().c_str());
250}
251
254 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
255 TypeTextIntoTextField(10);
256
257 GetCPWLEdit()->SetSelection(0, -1);
258 EXPECT_STREQ(L"ABCDEFGHIJ", GetCPWLEdit()->GetSelectedText().c_str());
259 GetCPWLEdit()->ReplaceSelection(L"Hello");
260 EXPECT_STREQ(L"Hello", GetCPWLEdit()->GetText().c_str());
261}
262
265 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
266 TypeTextIntoTextField(10);
267
268 GetCPWLEdit()->SetSelection(0, 5);
269 EXPECT_STREQ(L"ABCDE", GetCPWLEdit()->GetSelectedText().c_str());
270 GetCPWLEdit()->ReplaceSelection(L"Hello");
271 EXPECT_STREQ(L"HelloFGHIJ", GetCPWLEdit()->GetText().c_str());
272}
273
276 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
277 TypeTextIntoTextField(10);
278
279 GetCPWLEdit()->SetSelection(2, 7);
280 EXPECT_STREQ(L"CDEFG", GetCPWLEdit()->GetSelectedText().c_str());
281 GetCPWLEdit()->ReplaceSelection(L"Hello");
282 EXPECT_STREQ(L"ABHelloHIJ", GetCPWLEdit()->GetText().c_str());
283}
284
287 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
288 TypeTextIntoTextField(10);
289
290 GetCPWLEdit()->SetSelection(5, 10);
291 EXPECT_STREQ(L"FGHIJ", GetCPWLEdit()->GetSelectedText().c_str());
292 GetCPWLEdit()->ReplaceSelection(L"Hello");
293 EXPECT_STREQ(L"ABCDEHello", GetCPWLEdit()->GetText().c_str());
294}
295
297 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
298 GetCPWLEdit()->SetSelection(0, -1);
299 EXPECT_STREQ(L"Elephant", GetCPWLEdit()->GetSelectedText().c_str());
300 GetCPWLEdit()->ReplaceSelection(L"");
301
302 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
303 EXPECT_STREQ(L"Hippopotam", GetCPWLEdit()->GetText().c_str());
304}
305
307 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
308 GetCPWLEdit()->SetSelection(0, -1);
309 EXPECT_STREQ(L"Elephant", GetCPWLEdit()->GetSelectedText().c_str());
310 GetCPWLEdit()->ReplaceSelection(L"");
311
312 GetCPWLEdit()->ReplaceSelection(L"Zebra");
313 EXPECT_STREQ(L"Zebra", GetCPWLEdit()->GetText().c_str());
314}
315
317 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
318 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
319 EXPECT_STREQ(L"HiElephant", GetCPWLEdit()->GetText().c_str());
320}
321
323 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
324 // Move cursor to middle of text field.
325 for (int i = 0; i < 5; ++i) {
326 EXPECT_TRUE(GetCFFLFormFiller()->OnKeyDown(FWL_VKEY_Right, {}));
327 }
328
329 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
330 EXPECT_STREQ(L"ElephHiant", GetCPWLEdit()->GetText().c_str());
331}
332
334 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
335 // Move cursor to end of text field.
336 EXPECT_TRUE(GetCFFLFormFiller()->OnKeyDown(FWL_VKEY_End, {}));
337
338 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
339 EXPECT_STREQ(L"ElephantHi", GetCPWLEdit()->GetText().c_str());
340}
341
344 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
345 GetCPWLEdit()->SetSelection(0, -1);
346 EXPECT_STREQ(L"Elephant", GetCPWLEdit()->GetSelectedText().c_str());
347 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
348 EXPECT_STREQ(L"Hippopotam", GetCPWLEdit()->GetText().c_str());
349}
350
353 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
354 GetCPWLEdit()->SetSelection(0, 4);
355 EXPECT_STREQ(L"Elep", GetCPWLEdit()->GetSelectedText().c_str());
356 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
357 EXPECT_STREQ(L"Hippophant", GetCPWLEdit()->GetText().c_str());
358}
359
362 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
363 GetCPWLEdit()->SetSelection(2, 6);
364 EXPECT_STREQ(L"epha", GetCPWLEdit()->GetSelectedText().c_str());
365 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
366 EXPECT_STREQ(L"ElHippopnt", GetCPWLEdit()->GetText().c_str());
367}
368
371 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
372 GetCPWLEdit()->SetSelection(4, 8);
373 EXPECT_STREQ(L"hant", GetCPWLEdit()->GetSelectedText().c_str());
374 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
375 EXPECT_STREQ(L"ElepHippop", GetCPWLEdit()->GetText().c_str());
376}
377
379 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
380 GetCPWLEdit()->SetText(L"Foo\r");
381 EXPECT_STREQ(L"Foo", GetCPWLEdit()->GetText().c_str());
382}
383
385 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
386 GetCPWLEdit()->SetText(L"Foo\n");
387 EXPECT_STREQ(L"Foo", GetCPWLEdit()->GetText().c_str());
388}
389
391 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
392 GetCPWLEdit()->SetText(L"Foo\r\n");
393 EXPECT_STREQ(L"Foo", GetCPWLEdit()->GetText().c_str());
394}
395
397 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
398 GetCPWLEdit()->SetText(L"Foo\n\r");
399 EXPECT_STREQ(L"Foo", GetCPWLEdit()->GetText().c_str());
400}
401
403 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
404 GetCPWLEdit()->SetText(L"Foo\rBar");
405 EXPECT_STREQ(L"FooBar", GetCPWLEdit()->GetText().c_str());
406}
407
409 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
410 GetCPWLEdit()->SetText(L"Foo\nBar");
411 EXPECT_STREQ(L"FooBar", GetCPWLEdit()->GetText().c_str());
412}
413
415 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
416 GetCPWLEdit()->SetText(L"Foo\r\nBar");
417 EXPECT_STREQ(L"FooBar", GetCPWLEdit()->GetText().c_str());
418}
419
421 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
422 GetCPWLEdit()->SetText(L"Foo\n\rBar");
423 EXPECT_STREQ(L"FooBar", GetCPWLEdit()->GetText().c_str());
424}
425
427 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
428 TypeTextIntoTextField(10);
429
430 GetCPWLEdit()->SetSelection(1, 3);
431 EXPECT_STREQ(L"ABCDEFGHIJ", GetCPWLEdit()->GetText().c_str());
432 GetCPWLEdit()->ReplaceAndKeepSelection(L"xyz");
433 EXPECT_STREQ(L"AxyzDEFGHIJ", GetCPWLEdit()->GetText().c_str());
434 EXPECT_STREQ(L"xyz", GetCPWLEdit()->GetSelectedText().c_str());
435 EXPECT_EQ(GetCPWLEdit()->GetSelection(), std::make_pair(1, 4));
436
437 GetCPWLEdit()->SetSelection(4, 1);
438 GetCPWLEdit()->ReplaceAndKeepSelection(L"12");
439 EXPECT_STREQ(L"A12DEFGHIJ", GetCPWLEdit()->GetText().c_str());
440 EXPECT_STREQ(L"12", GetCPWLEdit()->GetSelectedText().c_str());
441 EXPECT_EQ(GetCPWLEdit()->GetSelection(), std::make_pair(1, 3));
442}
virtual bool OnChar(CPDFSDK_Widget *pAnnot, uint32_t nChar, Mask< FWL_EVENTFLAG > nFlags)
CPWL_Wnd * GetPWLWindow(const CPDFSDK_PageView *pPageView) const
bool OnSetFocus(ObservedPtr< CPDFSDK_Widget > &pWidget, Mask< FWL_EVENTFLAG > nFlag)
CFFL_FormField * GetFormFieldForTesting(CPDFSDK_Widget *pAnnot)
CPDFSDK_Annot * GetNextAnnot(CPDFSDK_Annot *pAnnot)
virtual CPDF_Annot::Subtype GetAnnotSubtype() const =0
CPDFSDK_PageView * GetPageViewAtIndex(int nIndex)
CFFL_InteractiveFormFiller * GetInteractiveFormFiller()
CPDFSDK_Widget * GetCPDFSDKAnnot()
CFFL_FormField * GetCFFLFormFiller()
void TypeTextIntoTextField(int num_chars)
CPDFSDK_Widget * GetCPDFSDKAnnotCharLimit()
void FormFillerAndWindowSetup(CPDFSDK_Widget *pAnnotTextField)
void SetUp() override
void TearDown() override
void UnloadPage(FPDF_PAGE page)
FPDF_PAGE LoadPage(int page_number)
FPDF_FORMHANDLE form_handle() const
CPDFSDK_Widget * ToCPDFSDKWidget(CPDFSDK_Annot *pAnnot)
TEST_F(CPWLEditEmbedderTest, TypeText)
@ FWL_VKEY_End
@ FWL_VKEY_Right
@ FWL_VKEY_Home
@ FWL_VKEY_Left