1 // Copyright 2015 PDFium Authors. All rights reserved.
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 <memory>
6 #include <string>
7 #include <vector>
8 
9 #include "build/build_config.h"
10 #include "core/fxcrt/fx_coordinates.h"
11 #include "core/fxcrt/fx_string.h"
12 #include "core/fxcrt/fx_system.h"
13 #include "public/cpp/fpdf_scopers.h"
14 #include "public/fpdf_formfill.h"
15 #include "public/fpdf_fwlevent.h"
16 #include "public/fpdf_progressive.h"
17 #include "testing/embedder_test.h"
18 #include "testing/embedder_test_mock_delegate.h"
19 #include "testing/embedder_test_timer_handling_delegate.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 
23 using testing::_;
24 
25 using FPDFFormFillEmbedderTest = EmbedderTest;
26 
27 // A base class for many related tests that involve clicking and typing into
28 // form fields.
29 class FPDFFormFillInteractiveEmbedderTest : public FPDFFormFillEmbedderTest {
30  protected:
31   FPDFFormFillInteractiveEmbedderTest() = default;
32   ~FPDFFormFillInteractiveEmbedderTest() override = default;
33 
SetUp()34   void SetUp() override {
35     FPDFFormFillEmbedderTest::SetUp();
36     ASSERT_TRUE(OpenDocument(GetDocumentName()));
37     page_ = LoadPage(0);
38     ASSERT_TRUE(page_);
39     FormSanityChecks();
40   }
41 
TearDown()42   void TearDown() override {
43     UnloadPage(page_);
44     FPDFFormFillEmbedderTest::TearDown();
45   }
46 
47   // Returns the name of the PDF to use.
48   virtual const char* GetDocumentName() const = 0;
49 
50   // Returns the type of field(s) in the PDF.
51   virtual int GetFormType() const = 0;
52 
53   // Optionally do some sanity check on the document after loading.
FormSanityChecks()54   virtual void FormSanityChecks() {}
55 
page()56   FPDF_PAGE page() { return page_; }
57 
GetFormTypeAtPoint(const CFX_PointF & point)58   int GetFormTypeAtPoint(const CFX_PointF& point) {
59     return FPDFPage_HasFormFieldAtPoint(form_handle(), page_, point.x, point.y);
60   }
61 
ClickOnFormFieldAtPoint(const CFX_PointF & point)62   void ClickOnFormFieldAtPoint(const CFX_PointF& point) {
63     // Click on the text field or combobox as specified by coordinates.
64     FORM_OnMouseMove(form_handle(), page_, 0, point.x, point.y);
65     FORM_OnLButtonDown(form_handle(), page_, 0, point.x, point.y);
66     FORM_OnLButtonUp(form_handle(), page_, 0, point.x, point.y);
67   }
68 
DoubleClickOnFormFieldAtPoint(const CFX_PointF & point)69   void DoubleClickOnFormFieldAtPoint(const CFX_PointF& point) {
70     // Click on the text field or combobox as specified by coordinates.
71     FORM_OnMouseMove(form_handle(), page_, 0, point.x, point.y);
72     FORM_OnLButtonDoubleClick(form_handle(), page_, 0, point.x, point.y);
73   }
74 
TypeTextIntoTextField(int num_chars,const CFX_PointF & point)75   void TypeTextIntoTextField(int num_chars, const CFX_PointF& point) {
76     EXPECT_EQ(GetFormType(), GetFormTypeAtPoint(point));
77     ClickOnFormFieldAtPoint(point);
78 
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       FORM_OnChar(form_handle(), page_, 'A' + i, 0);
82     }
83   }
84 
85   // Navigates to text field using the mouse and then selects text via the
86   // shift and specfied left or right arrow key.
SelectTextWithKeyboard(int num_chars,int arrow_key,const CFX_PointF & point)87   void SelectTextWithKeyboard(int num_chars,
88                               int arrow_key,
89                               const CFX_PointF& point) {
90     // Navigate to starting position for selection.
91     ClickOnFormFieldAtPoint(point);
92 
93     // Hold down shift (and don't release until entire text is selected).
94     FORM_OnKeyDown(form_handle(), page_, FWL_VKEY_Shift, 0);
95 
96     // Select text char by char via left or right arrow key.
97     for (int i = 0; i < num_chars; ++i) {
98       FORM_OnKeyDown(form_handle(), page_, arrow_key, FWL_EVENTFLAG_ShiftKey);
99       FORM_OnKeyUp(form_handle(), page_, arrow_key, FWL_EVENTFLAG_ShiftKey);
100     }
101     FORM_OnKeyUp(form_handle(), page_, FWL_VKEY_Shift, 0);
102   }
103 
104   // Uses the mouse to navigate to text field and select text.
SelectTextWithMouse(const CFX_PointF & start,const CFX_PointF & end)105   void SelectTextWithMouse(const CFX_PointF& start, const CFX_PointF& end) {
106     ASSERT(start.y == end.y);
107 
108     // Navigate to starting position and click mouse.
109     FORM_OnMouseMove(form_handle(), page_, 0, start.x, start.y);
110     FORM_OnLButtonDown(form_handle(), page_, 0, start.x, start.y);
111 
112     // Hold down mouse until reach end of desired selection.
113     FORM_OnMouseMove(form_handle(), page_, 0, end.x, end.y);
114     FORM_OnLButtonUp(form_handle(), page_, 0, end.x, end.y);
115   }
116 
CheckSelection(WideStringView expected_string)117   void CheckSelection(WideStringView expected_string) {
118     unsigned long actual_len =
119         FORM_GetSelectedText(form_handle(), page_, nullptr, 0);
120     ASSERT_NE(actual_len, 0U);
121     ASSERT_LT(actual_len, 1000U);
122 
123     std::vector<unsigned short> buf(actual_len);
124     ASSERT_EQ(actual_len, FORM_GetSelectedText(form_handle(), page_, buf.data(),
125                                                actual_len));
126 
127     int num_chars = (actual_len / sizeof(unsigned short)) - 1;
128     EXPECT_EQ(expected_string, WideString::FromUTF16LE(buf.data(), num_chars));
129   }
130 
CheckFocusedFieldText(WideStringView expected_string)131   void CheckFocusedFieldText(WideStringView expected_string) {
132     unsigned long actual_len =
133         FORM_GetFocusedText(form_handle(), page_, nullptr, 0);
134     ASSERT_NE(actual_len, 0U);
135     ASSERT_LT(actual_len, 1000U);
136 
137     std::vector<unsigned short> buf(actual_len);
138     ASSERT_EQ(actual_len, FORM_GetFocusedText(form_handle(), page_, buf.data(),
139                                               actual_len));
140 
141     int num_chars = (actual_len / sizeof(unsigned short)) - 1;
142     EXPECT_EQ(expected_string, WideString::FromUTF16LE(buf.data(), num_chars));
143   }
144 
CheckCanUndo(bool expected_result)145   void CheckCanUndo(bool expected_result) {
146     EXPECT_EQ(expected_result, !!FORM_CanUndo(form_handle(), page_));
147   }
148 
CheckCanRedo(bool expected_result)149   void CheckCanRedo(bool expected_result) {
150     EXPECT_EQ(expected_result, !!FORM_CanRedo(form_handle(), page_));
151   }
152 
PerformUndo()153   void PerformUndo() { EXPECT_TRUE(FORM_Undo(form_handle(), page_)); }
154 
PerformRedo()155   void PerformRedo() { EXPECT_TRUE(FORM_Redo(form_handle(), page_)); }
156 
SetIndexSelectedShouldSucceed(int index,bool selected)157   void SetIndexSelectedShouldSucceed(int index, bool selected) {
158     EXPECT_TRUE(FORM_SetIndexSelected(form_handle(), page_, index, selected));
159   }
160 
SetIndexSelectedShouldFail(int index,bool selected)161   void SetIndexSelectedShouldFail(int index, bool selected) {
162     EXPECT_FALSE(FORM_SetIndexSelected(form_handle(), page_, index, selected));
163   }
164 
CheckIsIndexSelected(int index,bool expected)165   void CheckIsIndexSelected(int index, bool expected) {
166     EXPECT_EQ(expected, FORM_IsIndexSelected(form_handle(), page_, index));
167   }
168 
169  private:
170   FPDF_PAGE page_ = nullptr;
171 };
172 
173 class FPDFFormFillTextFormEmbedderTest
174     : public FPDFFormFillInteractiveEmbedderTest {
175  protected:
176   FPDFFormFillTextFormEmbedderTest() = default;
177   ~FPDFFormFillTextFormEmbedderTest() override = default;
178 
GetDocumentName() const179   const char* GetDocumentName() const override {
180     // PDF with several form text fields:
181     // - "Text Box" - Regular text box with no special attributes.
182     // - "ReadOnly" - Ff: 1.
183     // - "CharLimit" - MaxLen: 10, V: Elephant.
184     return "text_form_multiple.pdf";
185   }
186 
GetFormType() const187   int GetFormType() const override { return FPDF_FORMFIELD_TEXTFIELD; }
188 
FormSanityChecks()189   void FormSanityChecks() override {
190     EXPECT_EQ(GetFormType(), GetFormTypeAtPoint(CharLimitFormBegin()));
191     EXPECT_EQ(GetFormType(), GetFormTypeAtPoint(CharLimitFormEnd()));
192     EXPECT_EQ(GetFormType(), GetFormTypeAtPoint(RegularFormBegin()));
193     EXPECT_EQ(GetFormType(), GetFormTypeAtPoint(RegularFormEnd()));
194   }
195 
SelectAllCharLimitFormTextWithMouse()196   void SelectAllCharLimitFormTextWithMouse() {
197     SelectTextWithMouse(CharLimitFormEnd(), CharLimitFormBegin());
198   }
199 
SelectAllRegularFormTextWithMouse()200   void SelectAllRegularFormTextWithMouse() {
201     SelectTextWithMouse(RegularFormEnd(), RegularFormBegin());
202   }
203 
CharLimitFormBegin() const204   const CFX_PointF& CharLimitFormBegin() const {
205     static const CFX_PointF point = CharLimitFormAtX(kFormBeginX);
206     return point;
207   }
208 
CharLimitFormEnd() const209   const CFX_PointF& CharLimitFormEnd() const {
210     static const CFX_PointF point = CharLimitFormAtX(kFormEndX);
211     return point;
212   }
213 
RegularFormBegin() const214   const CFX_PointF& RegularFormBegin() const {
215     static const CFX_PointF point = RegularFormAtX(kFormBeginX);
216     return point;
217   }
218 
RegularFormEnd() const219   const CFX_PointF& RegularFormEnd() const {
220     static const CFX_PointF point = RegularFormAtX(kFormEndX);
221     return point;
222   }
223 
CharLimitFormAtX(float x)224   static CFX_PointF CharLimitFormAtX(float x) {
225     ASSERT(x >= kFormBeginX);
226     ASSERT(x <= kFormEndX);
227     return CFX_PointF(x, kCharLimitFormY);
228   }
229 
RegularFormAtX(float x)230   static CFX_PointF RegularFormAtX(float x) {
231     ASSERT(x >= kFormBeginX);
232     ASSERT(x <= kFormEndX);
233     return CFX_PointF(x, kRegularFormY);
234   }
235 
236  private:
237   static constexpr float kFormBeginX = 102.0;
238   static constexpr float kFormEndX = 195.0;
239   static constexpr float kCharLimitFormY = 60.0;
240   static constexpr float kRegularFormY = 115.0;
241 };
242 
243 class FPDFFormFillComboBoxFormEmbedderTest
244     : public FPDFFormFillInteractiveEmbedderTest {
245  protected:
246   FPDFFormFillComboBoxFormEmbedderTest() = default;
247   ~FPDFFormFillComboBoxFormEmbedderTest() override = default;
248 
GetDocumentName() const249   const char* GetDocumentName() const override {
250     // PDF with form comboboxes:
251     // - "Combo_Editable" - Ff: 393216, 3 options with pair values.
252     // - "Combo1" - Ff: 131072, 3 options with single values.
253     // - "Combo_ReadOnly" - Ff: 131073, 3 options with single values.
254     return "combobox_form.pdf";
255   }
256 
GetFormType() const257   int GetFormType() const override { return FPDF_FORMFIELD_COMBOBOX; }
258 
FormSanityChecks()259   void FormSanityChecks() override {
260     EXPECT_EQ(GetFormType(), GetFormTypeAtPoint(EditableFormBegin()));
261     EXPECT_EQ(GetFormType(), GetFormTypeAtPoint(EditableFormEnd()));
262     EXPECT_EQ(GetFormType(), GetFormTypeAtPoint(EditableFormDropDown()));
263     EXPECT_EQ(GetFormType(), GetFormTypeAtPoint(NonEditableFormBegin()));
264     EXPECT_EQ(GetFormType(), GetFormTypeAtPoint(NonEditableFormEnd()));
265     EXPECT_EQ(GetFormType(), GetFormTypeAtPoint(NonEditableFormDropDown()));
266   }
267 
SelectEditableFormOption(int item_index)268   void SelectEditableFormOption(int item_index) {
269     ASSERT(item_index >= 0);
270     ASSERT(item_index < 3);
271     SelectOption(item_index, EditableFormDropDown());
272   }
273 
SelectNonEditableFormOption(int item_index)274   void SelectNonEditableFormOption(int item_index) {
275     ASSERT(item_index >= 0);
276     ASSERT(item_index < 26);
277     SelectOption(item_index, NonEditableFormDropDown());
278   }
279 
SelectAllEditableFormTextWithMouse()280   void SelectAllEditableFormTextWithMouse() {
281     SelectTextWithMouse(EditableFormEnd(), EditableFormBegin());
282   }
283 
FocusOnEditableForm()284   void FocusOnEditableForm() { FocusOnPoint(EditableFormDropDown()); }
285 
FocusOnNonEditableForm()286   void FocusOnNonEditableForm() { FocusOnPoint(NonEditableFormDropDown()); }
287 
FocusOnPoint(const CFX_PointF & point)288   void FocusOnPoint(const CFX_PointF& point) {
289     EXPECT_EQ(true, FORM_OnFocus(form_handle(), page(), 0, point.x, point.y));
290   }
291 
EditableFormBegin() const292   const CFX_PointF& EditableFormBegin() const {
293     static const CFX_PointF point = EditableFormAtX(kFormBeginX);
294     return point;
295   }
296 
EditableFormEnd() const297   const CFX_PointF& EditableFormEnd() const {
298     static const CFX_PointF point = EditableFormAtX(kFormEndX);
299     return point;
300   }
301 
EditableFormDropDown() const302   const CFX_PointF& EditableFormDropDown() const {
303     static const CFX_PointF point(kFormDropDownX, kEditableFormY);
304     return point;
305   }
306 
NonEditableFormBegin() const307   const CFX_PointF& NonEditableFormBegin() const {
308     static const CFX_PointF point = NonEditableFormAtX(kFormBeginX);
309     return point;
310   }
311 
NonEditableFormEnd() const312   const CFX_PointF& NonEditableFormEnd() const {
313     static const CFX_PointF point = NonEditableFormAtX(kFormEndX);
314     return point;
315   }
316 
NonEditableFormDropDown() const317   const CFX_PointF& NonEditableFormDropDown() const {
318     static const CFX_PointF point(kFormDropDownX, kNonEditableFormY);
319     return point;
320   }
321 
EditableFormAtX(float x)322   static CFX_PointF EditableFormAtX(float x) {
323     ASSERT(x >= kFormBeginX);
324     ASSERT(x <= kFormEndX);
325     return CFX_PointF(x, kEditableFormY);
326   }
327 
NonEditableFormAtX(float x)328   static CFX_PointF NonEditableFormAtX(float x) {
329     ASSERT(x >= kFormBeginX);
330     ASSERT(x <= kFormEndX);
331     return CFX_PointF(x, kNonEditableFormY);
332   }
333 
334  private:
335   // Selects one of the pre-selected values from a combobox with three options.
336   // Options are specified by |item_index|, which is 0-based.
SelectOption(int item_index,const CFX_PointF & point)337   void SelectOption(int item_index, const CFX_PointF& point) {
338     // Navigate to button for drop down and click mouse to reveal options.
339     ClickOnFormFieldAtPoint(point);
340 
341     // Calculate to Y-coordinate of dropdown option to be selected.
342     constexpr double kChoiceHeight = 15;
343     CFX_PointF option_point = point;
344     option_point.y -= kChoiceHeight * (item_index + 1);
345 
346     // Move left to avoid scrollbar.
347     option_point.x -= 20;
348 
349     // Navigate to option and click mouse to select it.
350     ClickOnFormFieldAtPoint(option_point);
351   }
352 
353   static constexpr float kFormBeginX = 102.0;
354   static constexpr float kFormEndX = 183.0;
355   static constexpr float kFormDropDownX = 192.0;
356   static constexpr float kEditableFormY = 360.0;
357   static constexpr float kNonEditableFormY = 410.0;
358 };
359 
360 class FPDFFormFillListBoxFormEmbedderTest
361     : public FPDFFormFillInteractiveEmbedderTest {
362  protected:
363   FPDFFormFillListBoxFormEmbedderTest() = default;
364   ~FPDFFormFillListBoxFormEmbedderTest() override = default;
365 
GetDocumentName() const366   const char* GetDocumentName() const override {
367     // PDF with form listboxes:
368     // - "Listbox_SingleSelect" - Ff: 0, 3 options with pair values.
369     // - "Listbox_MultiSelect" - Ff: 2097152, 26 options with single values.
370     // - "Listbox_ReadOnly" - Ff: 1, 3 options with single values.
371     // - "Listbox_MultiSelectMultipleSelected" - Ff: 2097152, 5 options with
372     // single values.
373     // - "Listbox_SingleSelectLastSelected" - Ff: 0, 10 options with single
374     // values.
375     return "listbox_form.pdf";
376   }
377 
GetFormType() const378   int GetFormType() const override { return FPDF_FORMFIELD_LISTBOX; }
379 
FormSanityChecks()380   void FormSanityChecks() override {
381     EXPECT_EQ(GetFormType(),
382               GetFormTypeAtPoint(SingleSelectFirstVisibleOption()));
383     EXPECT_EQ(GetFormType(),
384               GetFormTypeAtPoint(SingleSelectSecondVisibleOption()));
385     EXPECT_EQ(GetFormType(),
386               GetFormTypeAtPoint(MultiSelectFirstVisibleOption()));
387     EXPECT_EQ(GetFormType(),
388               GetFormTypeAtPoint(MultiSelectSecondVisibleOption()));
389     EXPECT_EQ(
390         GetFormType(),
391         GetFormTypeAtPoint(MultiSelectMultipleSelectedFirstVisibleOption()));
392     EXPECT_EQ(
393         GetFormType(),
394         GetFormTypeAtPoint(MultiSelectMultipleSelectedSecondVisibleOption()));
395     EXPECT_EQ(GetFormType(),
396               GetFormTypeAtPoint(SingleSelectLastSelectedFirstVisibleOption()));
397     EXPECT_EQ(
398         GetFormType(),
399         GetFormTypeAtPoint(SingleSelectLastSelectedSecondVisibleOption()));
400   }
401 
ClickOnSingleSelectFormOption(int item_index)402   void ClickOnSingleSelectFormOption(int item_index) {
403     // Only the first two indices are visible so can only click on those
404     // without scrolling.
405     ASSERT(item_index >= 0);
406     ASSERT(item_index < 2);
407     if (item_index == 0) {
408       ClickOnFormFieldAtPoint(SingleSelectFirstVisibleOption());
409     } else {
410       ClickOnFormFieldAtPoint(SingleSelectSecondVisibleOption());
411     }
412   }
413 
ClickOnMultiSelectFormOption(int item_index)414   void ClickOnMultiSelectFormOption(int item_index) {
415     // Only the first two indices are visible so can only click on those
416     // without scrolling.
417     ASSERT(item_index >= 0);
418     ASSERT(item_index < 2);
419     if (item_index == 0) {
420       ClickOnFormFieldAtPoint(MultiSelectFirstVisibleOption());
421     } else {
422       ClickOnFormFieldAtPoint(MultiSelectSecondVisibleOption());
423     }
424   }
425 
ClickOnMultiSelectMultipleSelectedFormOption(int item_index)426   void ClickOnMultiSelectMultipleSelectedFormOption(int item_index) {
427     // Only two indices are visible so can only click on those
428     // without scrolling.
429     ASSERT(item_index >= 0);
430     ASSERT(item_index < 2);
431     if (item_index == 0) {
432       ClickOnFormFieldAtPoint(MultiSelectMultipleSelectedFirstVisibleOption());
433     } else {
434       ClickOnFormFieldAtPoint(MultiSelectMultipleSelectedSecondVisibleOption());
435     }
436   }
437 
ClickOnSingleSelectLastSelectedFormOption(int item_index)438   void ClickOnSingleSelectLastSelectedFormOption(int item_index) {
439     // Only two indices are visible so can only click on those
440     // without scrolling.
441     ASSERT(item_index >= 0);
442     ASSERT(item_index < 2);
443     if (item_index == 0) {
444       ClickOnFormFieldAtPoint(SingleSelectLastSelectedFirstVisibleOption());
445     } else {
446       ClickOnFormFieldAtPoint(SingleSelectLastSelectedSecondVisibleOption());
447     }
448   }
449 
FocusOnSingleSelectForm()450   void FocusOnSingleSelectForm() {
451     FocusOnPoint(SingleSelectFirstVisibleOption());
452   }
453 
FocusOnMultiSelectForm()454   void FocusOnMultiSelectForm() {
455     FocusOnPoint(MultiSelectFirstVisibleOption());
456   }
457 
FocusOnMultiSelectMultipleSelectedForm()458   void FocusOnMultiSelectMultipleSelectedForm() {
459     FocusOnPoint(MultiSelectMultipleSelectedFirstVisibleOption());
460   }
461 
FocusOnSingleSelectLastSelectedForm()462   void FocusOnSingleSelectLastSelectedForm() {
463     FocusOnPoint(SingleSelectLastSelectedFirstVisibleOption());
464   }
465 
FocusOnPoint(const CFX_PointF & point)466   void FocusOnPoint(const CFX_PointF& point) {
467     EXPECT_EQ(true, FORM_OnFocus(form_handle(), page(), 0, point.x, point.y));
468   }
469 
SingleSelectFirstVisibleOption() const470   const CFX_PointF& SingleSelectFirstVisibleOption() const {
471     static const CFX_PointF point(kFormBeginX, kSingleFormYFirstVisibleOption);
472     return point;
473   }
474 
SingleSelectSecondVisibleOption() const475   const CFX_PointF& SingleSelectSecondVisibleOption() const {
476     static const CFX_PointF point(kFormBeginX, kSingleFormYSecondVisibleOption);
477     return point;
478   }
479 
MultiSelectFirstVisibleOption() const480   const CFX_PointF& MultiSelectFirstVisibleOption() const {
481     static const CFX_PointF point(kFormBeginX, kMultiFormYFirstVisibleOption);
482     return point;
483   }
484 
MultiSelectSecondVisibleOption() const485   const CFX_PointF& MultiSelectSecondVisibleOption() const {
486     static const CFX_PointF point(kFormBeginX, kMultiFormYSecondVisibleOption);
487     return point;
488   }
489 
MultiSelectMultipleSelectedFirstVisibleOption() const490   const CFX_PointF& MultiSelectMultipleSelectedFirstVisibleOption() const {
491     static const CFX_PointF point(
492         kFormBeginX, kMultiFormMultipleSelectedYFirstVisibleOption);
493     return point;
494   }
495 
MultiSelectMultipleSelectedSecondVisibleOption() const496   const CFX_PointF& MultiSelectMultipleSelectedSecondVisibleOption() const {
497     static const CFX_PointF point(
498         kFormBeginX, kMultiFormMultipleSelectedYSecondVisibleOption);
499     return point;
500   }
501 
SingleSelectLastSelectedFirstVisibleOption() const502   const CFX_PointF& SingleSelectLastSelectedFirstVisibleOption() const {
503     static const CFX_PointF point(kFormBeginX,
504                                   kSingleFormLastSelectedYFirstVisibleOption);
505     return point;
506   }
507 
SingleSelectLastSelectedSecondVisibleOption() const508   const CFX_PointF& SingleSelectLastSelectedSecondVisibleOption() const {
509     static const CFX_PointF point(kFormBeginX,
510                                   kSingleFormLastSelectedYSecondVisibleOption);
511     return point;
512   }
513 
514  private:
515   static constexpr float kFormBeginX = 102.0;
516   static constexpr float kSingleFormYFirstVisibleOption = 371.0;
517   static constexpr float kSingleFormYSecondVisibleOption = 358.0;
518   static constexpr float kMultiFormYFirstVisibleOption = 423.0;
519   static constexpr float kMultiFormYSecondVisibleOption = 408.0;
520   static constexpr float kMultiFormMultipleSelectedYFirstVisibleOption = 223.0;
521   static constexpr float kMultiFormMultipleSelectedYSecondVisibleOption = 208.0;
522   static constexpr float kSingleFormLastSelectedYFirstVisibleOption = 123.0;
523   static constexpr float kSingleFormLastSelectedYSecondVisibleOption = 108.0;
524 };
525 
TEST_F(FPDFFormFillEmbedderTest,FirstTest)526 TEST_F(FPDFFormFillEmbedderTest, FirstTest) {
527   EmbedderTestMockDelegate mock;
528   EXPECT_CALL(mock, Alert(_, _, _, _)).Times(0);
529   EXPECT_CALL(mock, UnsupportedHandler(_)).Times(0);
530   EXPECT_CALL(mock, SetTimer(_, _)).Times(0);
531   EXPECT_CALL(mock, KillTimer(_)).Times(0);
532   SetDelegate(&mock);
533 
534   EXPECT_TRUE(OpenDocument("hello_world.pdf"));
535   FPDF_PAGE page = LoadPage(0);
536   EXPECT_TRUE(page);
537   UnloadPage(page);
538 }
539 
TEST_F(FPDFFormFillEmbedderTest,BUG_487928)540 TEST_F(FPDFFormFillEmbedderTest, BUG_487928) {
541   EmbedderTestTimerHandlingDelegate delegate;
542   SetDelegate(&delegate);
543 
544   EXPECT_TRUE(OpenDocument("bug_487928.pdf"));
545   FPDF_PAGE page = LoadPage(0);
546   EXPECT_TRUE(page);
547   DoOpenActions();
548   delegate.AdvanceTime(5000);
549   UnloadPage(page);
550 }
551 
TEST_F(FPDFFormFillEmbedderTest,BUG_507316)552 TEST_F(FPDFFormFillEmbedderTest, BUG_507316) {
553   EmbedderTestTimerHandlingDelegate delegate;
554   SetDelegate(&delegate);
555 
556   EXPECT_TRUE(OpenDocument("bug_507316.pdf"));
557   FPDF_PAGE page = LoadPage(2);
558   EXPECT_TRUE(page);
559   DoOpenActions();
560   delegate.AdvanceTime(4000);
561   UnloadPage(page);
562 }
563 
TEST_F(FPDFFormFillEmbedderTest,BUG_514690)564 TEST_F(FPDFFormFillEmbedderTest, BUG_514690) {
565   EXPECT_TRUE(OpenDocument("hello_world.pdf"));
566   FPDF_PAGE page = LoadPage(0);
567   EXPECT_TRUE(page);
568 
569   // Test that FORM_OnMouseMove() etc. permit null HANDLES and PAGES.
570   FORM_OnMouseMove(nullptr, page, 0, 10.0, 10.0);
571   FORM_OnMouseMove(form_handle(), nullptr, 0, 10.0, 10.0);
572 
573   UnloadPage(page);
574 }
575 
TEST_F(FPDFFormFillEmbedderTest,BUG_900552)576 TEST_F(FPDFFormFillEmbedderTest, BUG_900552) {
577   EmbedderTestTimerHandlingDelegate delegate;
578   SetDelegate(&delegate);
579 
580   EXPECT_TRUE(OpenDocument("bug_900552.pdf"));
581   FPDF_PAGE page = LoadPage(0);
582   ASSERT_TRUE(page);
583   DoOpenActions();
584   delegate.AdvanceTime(4000);
585 
586   // Simulate a repaint.
587   FPDF_BITMAP bitmap = FPDFBitmap_Create(512, 512, 0);
588   ASSERT_TRUE(bitmap);
589   FPDF_RenderPageBitmap_Start(bitmap, page, 0, 0, 512, 512, 0, 0, nullptr);
590   FPDFBitmap_Destroy(bitmap);
591   UnloadPage(page);
592 }
593 
TEST_F(FPDFFormFillEmbedderTest,BUG_901654)594 TEST_F(FPDFFormFillEmbedderTest, BUG_901654) {
595   EmbedderTestTimerHandlingDelegate delegate;
596   SetDelegate(&delegate);
597 
598   EXPECT_TRUE(OpenDocument("bug_901654.pdf"));
599   FPDF_PAGE page = LoadPage(0);
600   ASSERT_TRUE(page);
601   DoOpenActions();
602   delegate.AdvanceTime(4000);
603 
604   // Simulate a repaint.
605   {
606     ScopedFPDFBitmap bitmap(FPDFBitmap_Create(512, 512, 0));
607     FPDF_RenderPageBitmap_Start(bitmap.get(), page, 0, 0, 512, 512, 0, 0,
608                                 nullptr);
609   }
610   UnloadPage(page);
611 }
612 
TEST_F(FPDFFormFillEmbedderTest,BUG_901654_2)613 TEST_F(FPDFFormFillEmbedderTest, BUG_901654_2) {
614   EmbedderTestTimerHandlingDelegate delegate;
615   SetDelegate(&delegate);
616 
617   EXPECT_TRUE(OpenDocument("bug_901654_2.pdf"));
618   FPDF_PAGE page = LoadPage(0);
619   ASSERT_TRUE(page);
620   DoOpenActions();
621   delegate.AdvanceTime(4000);
622 
623   // Simulate a repaint.
624   {
625     ScopedFPDFBitmap bitmap(FPDFBitmap_Create(512, 512, 0));
626     FPDF_RenderPageBitmap_Start(bitmap.get(), page, 0, 0, 512, 512, 0, 0,
627                                 nullptr);
628   }
629   UnloadPage(page);
630 }
631 
632 class DoURIActionBlockedDelegate final : public EmbedderTest::Delegate {
633  public:
DoURIAction(FPDF_BYTESTRING uri)634   void DoURIAction(FPDF_BYTESTRING uri) override {
635     FAIL() << "Navigated to " << uri;
636   }
637 };
638 
TEST_F(FPDFFormFillEmbedderTest,BUG_851821)639 TEST_F(FPDFFormFillEmbedderTest, BUG_851821) {
640   DoURIActionBlockedDelegate delegate;
641   SetDelegate(&delegate);
642 
643   EXPECT_TRUE(OpenDocument("redirect.pdf"));
644   FPDF_PAGE page = LoadPage(0);
645   EXPECT_TRUE(page);
646   DoOpenActions();
647 
648   UnloadPage(page);
649 }
650 
651 #ifdef PDF_ENABLE_V8
TEST_F(FPDFFormFillEmbedderTest,DisableJavaScript)652 TEST_F(FPDFFormFillEmbedderTest, DisableJavaScript) {
653   // Test that timers and intervals can't fire without JS.
654   EmbedderTestTimerHandlingDelegate delegate;
655   SetDelegate(&delegate);
656 
657   EXPECT_TRUE(OpenDocumentWithoutJavaScript("bug_551248.pdf"));
658   FPDF_PAGE page = LoadPage(0);
659   EXPECT_TRUE(page);
660   DoOpenActions();
661 
662   const auto& alerts = delegate.GetAlerts();
663   EXPECT_EQ(0U, alerts.size());
664 
665   delegate.AdvanceTime(1000);
666   EXPECT_EQ(0U, alerts.size());  // nothing fired.
667   delegate.AdvanceTime(1000);
668   EXPECT_EQ(0U, alerts.size());  // nothing fired.
669   delegate.AdvanceTime(1000);
670   EXPECT_EQ(0U, alerts.size());  // nothing fired.
671   delegate.AdvanceTime(1000);
672   EXPECT_EQ(0U, alerts.size());  // nothing fired.
673   delegate.AdvanceTime(1000);
674   EXPECT_EQ(0U, alerts.size());  // nothing fired.
675   delegate.AdvanceTime(1000);
676   EXPECT_EQ(0U, alerts.size());  // nothing fired.
677   delegate.AdvanceTime(1000);
678   EXPECT_EQ(0U, alerts.size());  // nothing fired.
679   UnloadPage(page);
680 }
681 
TEST_F(FPDFFormFillEmbedderTest,DocumentAActions)682 TEST_F(FPDFFormFillEmbedderTest, DocumentAActions) {
683   EmbedderTestTimerHandlingDelegate delegate;
684   SetDelegate(&delegate);
685 
686   EXPECT_TRUE(OpenDocument("document_aactions.pdf"));
687   FPDF_PAGE page = LoadPage(0);
688   EXPECT_TRUE(page);
689 
690   const auto& alerts = delegate.GetAlerts();
691   EXPECT_EQ(0U, alerts.size());
692 
693   FORM_DoDocumentAAction(form_handle(), FPDFDOC_AACTION_WS);
694   FORM_DoDocumentAAction(form_handle(), FPDFDOC_AACTION_DS);
695   FORM_DoDocumentAAction(form_handle(), FPDFDOC_AACTION_WP);
696   FORM_DoDocumentAAction(form_handle(), FPDFDOC_AACTION_DP);
697   UnloadPage(page);
698 
699   ASSERT_EQ(4U, alerts.size());
700   EXPECT_STREQ(L"Will Save", alerts[0].message.c_str());
701   EXPECT_STREQ(L"Did Save", alerts[1].message.c_str());
702   EXPECT_STREQ(L"Will Print", alerts[2].message.c_str());
703   EXPECT_STREQ(L"Did Print", alerts[3].message.c_str());
704 }
705 
TEST_F(FPDFFormFillEmbedderTest,DocumentAActionsDisableJavaScript)706 TEST_F(FPDFFormFillEmbedderTest, DocumentAActionsDisableJavaScript) {
707   EmbedderTestTimerHandlingDelegate delegate;
708   SetDelegate(&delegate);
709 
710   EXPECT_TRUE(OpenDocumentWithoutJavaScript("document_aactions.pdf"));
711   FPDF_PAGE page = LoadPage(0);
712   EXPECT_TRUE(page);
713 
714   const auto& alerts = delegate.GetAlerts();
715   EXPECT_EQ(0U, alerts.size());
716 
717   FORM_DoDocumentAAction(form_handle(), FPDFDOC_AACTION_WS);
718   FORM_DoDocumentAAction(form_handle(), FPDFDOC_AACTION_DS);
719   FORM_DoDocumentAAction(form_handle(), FPDFDOC_AACTION_WP);
720   FORM_DoDocumentAAction(form_handle(), FPDFDOC_AACTION_DP);
721   UnloadPage(page);
722 
723   ASSERT_EQ(0U, alerts.size());
724 }
725 
TEST_F(FPDFFormFillEmbedderTest,BUG_551248)726 TEST_F(FPDFFormFillEmbedderTest, BUG_551248) {
727   // Test that timers fire once and intervals fire repeatedly.
728   EmbedderTestTimerHandlingDelegate delegate;
729   SetDelegate(&delegate);
730 
731   EXPECT_TRUE(OpenDocument("bug_551248.pdf"));
732   FPDF_PAGE page = LoadPage(0);
733   EXPECT_TRUE(page);
734   DoOpenActions();
735 
736   const auto& alerts = delegate.GetAlerts();
737   EXPECT_EQ(0U, alerts.size());
738 
739   delegate.AdvanceTime(1000);
740   EXPECT_EQ(0U, alerts.size());  // nothing fired.
741   delegate.AdvanceTime(1000);
742   EXPECT_EQ(1U, alerts.size());  // interval fired.
743   delegate.AdvanceTime(1000);
744   EXPECT_EQ(2U, alerts.size());  // timer fired.
745   delegate.AdvanceTime(1000);
746   EXPECT_EQ(3U, alerts.size());  // interval fired again.
747   delegate.AdvanceTime(1000);
748   EXPECT_EQ(3U, alerts.size());  // nothing fired.
749   delegate.AdvanceTime(1000);
750   EXPECT_EQ(4U, alerts.size());  // interval fired again.
751   delegate.AdvanceTime(1000);
752   EXPECT_EQ(4U, alerts.size());  // nothing fired.
753   UnloadPage(page);
754 
755   ASSERT_EQ(4U, alerts.size());  // nothing else fired.
756 
757   EXPECT_STREQ(L"interval fired", alerts[0].message.c_str());
758   EXPECT_STREQ(L"Alert", alerts[0].title.c_str());
759   EXPECT_EQ(0, alerts[0].type);
760   EXPECT_EQ(0, alerts[0].icon);
761 
762   EXPECT_STREQ(L"timer fired", alerts[1].message.c_str());
763   EXPECT_STREQ(L"Alert", alerts[1].title.c_str());
764   EXPECT_EQ(0, alerts[1].type);
765   EXPECT_EQ(0, alerts[1].icon);
766 
767   EXPECT_STREQ(L"interval fired", alerts[2].message.c_str());
768   EXPECT_STREQ(L"Alert", alerts[2].title.c_str());
769   EXPECT_EQ(0, alerts[2].type);
770   EXPECT_EQ(0, alerts[2].icon);
771 
772   EXPECT_STREQ(L"interval fired", alerts[3].message.c_str());
773   EXPECT_STREQ(L"Alert", alerts[3].title.c_str());
774   EXPECT_EQ(0, alerts[3].type);
775   EXPECT_EQ(0, alerts[3].icon);
776 }
777 
TEST_F(FPDFFormFillEmbedderTest,BUG_620428)778 TEST_F(FPDFFormFillEmbedderTest, BUG_620428) {
779   // Test that timers and intervals are cancelable.
780   EmbedderTestTimerHandlingDelegate delegate;
781   SetDelegate(&delegate);
782 
783   EXPECT_TRUE(OpenDocument("bug_620428.pdf"));
784   FPDF_PAGE page = LoadPage(0);
785   EXPECT_TRUE(page);
786   DoOpenActions();
787   delegate.AdvanceTime(5000);
788   UnloadPage(page);
789 
790   const auto& alerts = delegate.GetAlerts();
791   ASSERT_EQ(1U, alerts.size());
792   EXPECT_STREQ(L"done", alerts[0].message.c_str());
793 }
794 
TEST_F(FPDFFormFillEmbedderTest,BUG_634394)795 TEST_F(FPDFFormFillEmbedderTest, BUG_634394) {
796   // Cancel timer inside timer callback.
797   EmbedderTestTimerHandlingDelegate delegate;
798   SetDelegate(&delegate);
799 
800   EXPECT_TRUE(OpenDocument("bug_634394.pdf"));
801   FPDF_PAGE page = LoadPage(0);
802   EXPECT_TRUE(page);
803   DoOpenActions();
804 
805   // Timers fire at most once per AdvanceTime(), allow intervals
806   // to fire several times if possible.
807   delegate.AdvanceTime(1000);
808   delegate.AdvanceTime(1000);
809   delegate.AdvanceTime(1000);
810   delegate.AdvanceTime(1000);
811   delegate.AdvanceTime(1000);
812   UnloadPage(page);
813 
814   const auto& alerts = delegate.GetAlerts();
815   EXPECT_EQ(2U, alerts.size());
816 }
817 
TEST_F(FPDFFormFillEmbedderTest,BUG_634716)818 TEST_F(FPDFFormFillEmbedderTest, BUG_634716) {
819   EmbedderTestTimerHandlingDelegate delegate;
820   SetDelegate(&delegate);
821 
822   EXPECT_TRUE(OpenDocument("bug_634716.pdf"));
823   FPDF_PAGE page = LoadPage(0);
824   EXPECT_TRUE(page);
825   DoOpenActions();
826 
827   // Timers fire at most once per AdvanceTime(), allow intervals
828   // to fire several times if possible.
829   delegate.AdvanceTime(1000);
830   delegate.AdvanceTime(1000);
831   delegate.AdvanceTime(1000);
832   delegate.AdvanceTime(1000);
833   delegate.AdvanceTime(1000);
834   UnloadPage(page);
835 
836   const auto& alerts = delegate.GetAlerts();
837   EXPECT_EQ(2U, alerts.size());
838 }
839 
TEST_F(FPDFFormFillEmbedderTest,BUG_679649)840 TEST_F(FPDFFormFillEmbedderTest, BUG_679649) {
841   EmbedderTestTimerHandlingDelegate delegate;
842   SetDelegate(&delegate);
843 
844   EXPECT_TRUE(OpenDocument("bug_679649.pdf"));
845   FPDF_PAGE page = LoadPage(0);
846   EXPECT_TRUE(page);
847 
848   delegate.SetFailNextTimer();
849   DoOpenActions();
850   delegate.AdvanceTime(2000);
851   UnloadPage(page);
852 
853   const auto& alerts = delegate.GetAlerts();
854   EXPECT_EQ(0u, alerts.size());
855 }
856 
TEST_F(FPDFFormFillEmbedderTest,BUG_707673)857 TEST_F(FPDFFormFillEmbedderTest, BUG_707673) {
858   EmbedderTestTimerHandlingDelegate delegate;
859   SetDelegate(&delegate);
860 
861   EXPECT_TRUE(OpenDocument("bug_707673.pdf"));
862   FPDF_PAGE page = LoadPage(0);
863   EXPECT_TRUE(page);
864 
865   DoOpenActions();
866   FORM_OnLButtonDown(form_handle(), page, 0, 140, 590);
867   FORM_OnLButtonUp(form_handle(), page, 0, 140, 590);
868   delegate.AdvanceTime(1000);
869   UnloadPage(page);
870 
871   const auto& alerts = delegate.GetAlerts();
872   EXPECT_EQ(0u, alerts.size());
873 }
874 
TEST_F(FPDFFormFillEmbedderTest,BUG_765384)875 TEST_F(FPDFFormFillEmbedderTest, BUG_765384) {
876   EXPECT_TRUE(OpenDocument("bug_765384.pdf"));
877   FPDF_PAGE page = LoadPage(0);
878   EXPECT_TRUE(page);
879 
880   DoOpenActions();
881   FORM_OnLButtonDown(form_handle(), page, 0, 140, 590);
882   FORM_OnLButtonUp(form_handle(), page, 0, 140, 590);
883   UnloadPage(page);
884 }
885 #endif  // PDF_ENABLE_V8
886 
887 // TODO(crbug.com/pdfium/11): Fix this test and enable.
888 #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
889 #define MAYBE_FormText DISABLED_FormText
890 #else
891 #define MAYBE_FormText FormText
892 #endif
TEST_F(FPDFFormFillEmbedderTest,MAYBE_FormText)893 TEST_F(FPDFFormFillEmbedderTest, MAYBE_FormText) {
894 #if defined(OS_MACOSX)
895   const char md5_1[] = "5f11dbe575fe197a37c3fb422559f8ff";
896   const char md5_2[] = "35b1a4b679eafc749a0b6fda750c0e8d";
897   const char md5_3[] = "65c64a7c355388f719a752aa1e23f6fe";
898 #elif defined(OS_WIN)
899   const char md5_1[] = "d3204faa62b607f0bd3893c9c22cabcb";
900   const char md5_2[] = "29d1c3fd226ca6a69597f75937690320";
901   const char md5_3[] = "5e678a55912cb568fd677bf34abb8727";
902 #else
903   const char md5_1[] = "b890950d4b9bc163b1a96797f3004b53";
904   const char md5_2[] = "11487d5597599a26e8912b9c1d9422cb";
905   const char md5_3[] = "bffe0ecea9a533f217047ee41d6be466";
906 #endif
907   {
908     EXPECT_TRUE(OpenDocument("text_form.pdf"));
909     FPDF_PAGE page = LoadPage(0);
910     ASSERT_TRUE(page);
911     ScopedFPDFBitmap bitmap1 = RenderLoadedPage(page);
912     CompareBitmap(bitmap1.get(), 300, 300, md5_1);
913 
914     // Click on the textfield
915     EXPECT_EQ(FPDF_FORMFIELD_TEXTFIELD,
916               FPDFPage_HasFormFieldAtPoint(form_handle(), page, 120.0, 120.0));
917     EXPECT_EQ(
918         0, FPDFPage_FormFieldZOrderAtPoint(form_handle(), page, 120.0, 120.0));
919     FORM_OnMouseMove(form_handle(), page, 0, 120.0, 120.0);
920     FORM_OnLButtonDown(form_handle(), page, 0, 120.0, 120.0);
921     FORM_OnLButtonUp(form_handle(), page, 0, 120.0, 120.0);
922 
923     // Write "ABC"
924     FORM_OnChar(form_handle(), page, 65, 0);
925     FORM_OnChar(form_handle(), page, 66, 0);
926     FORM_OnChar(form_handle(), page, 67, 0);
927     ScopedFPDFBitmap bitmap2 = RenderLoadedPage(page);
928     CompareBitmap(bitmap2.get(), 300, 300, md5_2);
929 
930     // Focus remains despite right clicking out of the textfield
931     FORM_OnMouseMove(form_handle(), page, 0, 15.0, 15.0);
932     FORM_OnRButtonDown(form_handle(), page, 0, 15.0, 15.0);
933     FORM_OnRButtonUp(form_handle(), page, 0, 15.0, 15.0);
934     ScopedFPDFBitmap bitmap3 = RenderLoadedPage(page);
935     CompareBitmap(bitmap3.get(), 300, 300, md5_2);
936 
937     // Take out focus by clicking out of the textfield
938     FORM_OnMouseMove(form_handle(), page, 0, 15.0, 15.0);
939     FORM_OnLButtonDown(form_handle(), page, 0, 15.0, 15.0);
940     FORM_OnLButtonUp(form_handle(), page, 0, 15.0, 15.0);
941     ScopedFPDFBitmap bitmap4 = RenderLoadedPage(page);
942     CompareBitmap(bitmap4.get(), 300, 300, md5_3);
943 
944     EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
945 
946     // Close page
947     UnloadPage(page);
948   }
949   // Check saved document
950   VerifySavedDocument(300, 300, md5_3);
951 }
952 
953 // Tests using FPDF_REVERSE_BYTE_ORDER with FPDF_FFLDraw(). The two rendered
954 // bitmaps should be different.
955 // TODO(crbug.com/pdfium/11): Fix this test and enable.
956 #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
957 #define MAYBE_BUG_1281 DISABLED_BUG_1281
958 #else
959 #define MAYBE_BUG_1281 BUG_1281
960 #endif
TEST_F(FPDFFormFillEmbedderTest,MAYBE_BUG_1281)961 TEST_F(FPDFFormFillEmbedderTest, MAYBE_BUG_1281) {
962   const char kMd5Normal[] = "6c674642154408e877d88c6c082d67e9";
963   const char kMd5ReverseByteOrder[] = "24fff03d1e663b7ece5f6e69ad837124";
964 
965   ASSERT_TRUE(OpenDocument("bug_890322.pdf"));
966   FPDF_PAGE page = LoadPage(0);
967   ASSERT_TRUE(page);
968 
969   ScopedFPDFBitmap bitmap_normal = RenderLoadedPage(page);
970   CompareBitmap(bitmap_normal.get(), 200, 200, kMd5Normal);
971 
972   ScopedFPDFBitmap bitmap_reverse_byte_order =
973       RenderLoadedPageWithFlags(page, FPDF_REVERSE_BYTE_ORDER);
974   CompareBitmap(bitmap_reverse_byte_order.get(), 200, 200,
975                 kMd5ReverseByteOrder);
976 
977   UnloadPage(page);
978 }
979 
980 #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
981 #define MAYBE_RemoveFormFieldHighlight DISABLED_RemoveFormFieldHighlight
982 #else
983 #define MAYBE_RemoveFormFieldHighlight RemoveFormFieldHighlight
984 #endif
TEST_F(FPDFFormFillEmbedderTest,MAYBE_RemoveFormFieldHighlight)985 TEST_F(FPDFFormFillEmbedderTest, MAYBE_RemoveFormFieldHighlight) {
986 #if defined(OS_MACOSX)
987   const char kMd5Normal[] = "5f11dbe575fe197a37c3fb422559f8ff";
988   const char kMd5NoHighlight[] = "575ec237c790950f40bfcaefb2e3923c";
989 #elif defined(OS_WIN)
990   const char kMd5Normal[] = "d3204faa62b607f0bd3893c9c22cabcb";
991   const char kMd5NoHighlight[] = "3ec0938828e0a37ef23f687ee95a80e1";
992 #else
993   const char kMd5Normal[] = "b890950d4b9bc163b1a96797f3004b53";
994   const char kMd5NoHighlight[] = "006010c318457810a518aa5e0b33c498";
995 #endif
996 
997   EXPECT_TRUE(OpenDocument("text_form.pdf"));
998   FPDF_PAGE page = LoadPage(0);
999   ASSERT_TRUE(page);
1000   ScopedFPDFBitmap bitmap1 = RenderLoadedPage(page);
1001   CompareBitmap(bitmap1.get(), 300, 300, kMd5Normal);
1002 
1003   // Removing the highlight changes the rendering.
1004   FPDF_RemoveFormFieldHighlight(form_handle());
1005   ScopedFPDFBitmap bitmap2 = RenderLoadedPage(page);
1006   CompareBitmap(bitmap2.get(), 300, 300, kMd5NoHighlight);
1007 
1008   // Restoring it gives the original rendering.
1009   SetInitialFormFieldHighlight(form_handle());
1010   ScopedFPDFBitmap bitmap3 = RenderLoadedPage(page);
1011   CompareBitmap(bitmap3.get(), 300, 300, kMd5Normal);
1012 
1013   UnloadPage(page);
1014 }
1015 
TEST_F(FPDFFormFillEmbedderTest,HasFormInfoNone)1016 TEST_F(FPDFFormFillEmbedderTest, HasFormInfoNone) {
1017   EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1018   EXPECT_EQ(FORMTYPE_NONE, FPDF_GetFormType(document_));
1019 }
1020 
TEST_F(FPDFFormFillEmbedderTest,HasFormInfoAcroForm)1021 TEST_F(FPDFFormFillEmbedderTest, HasFormInfoAcroForm) {
1022   EXPECT_TRUE(OpenDocument("text_form.pdf"));
1023   EXPECT_EQ(FORMTYPE_ACRO_FORM, FPDF_GetFormType(document_));
1024 }
1025 
TEST_F(FPDFFormFillEmbedderTest,HasFormInfoXFAFull)1026 TEST_F(FPDFFormFillEmbedderTest, HasFormInfoXFAFull) {
1027   EXPECT_TRUE(OpenDocument("simple_xfa.pdf"));
1028   EXPECT_EQ(FORMTYPE_XFA_FULL, FPDF_GetFormType(document_));
1029 }
1030 
TEST_F(FPDFFormFillEmbedderTest,HasFormInfoXFAForeground)1031 TEST_F(FPDFFormFillEmbedderTest, HasFormInfoXFAForeground) {
1032   EXPECT_TRUE(OpenDocument("bug_216.pdf"));
1033   EXPECT_EQ(FORMTYPE_XFA_FOREGROUND, FPDF_GetFormType(document_));
1034 }
1035 
TEST_F(FPDFFormFillEmbedderTest,BadApiInputsText)1036 TEST_F(FPDFFormFillEmbedderTest, BadApiInputsText) {
1037   ASSERT_TRUE(OpenDocument("text_form.pdf"));
1038   FPDF_PAGE page = LoadPage(0);
1039   ASSERT_TRUE(page);
1040 
1041   EXPECT_FALSE(FORM_SetIndexSelected(nullptr, nullptr, 0, true));
1042   EXPECT_FALSE(FORM_SetIndexSelected(nullptr, page, 0, true));
1043   EXPECT_FALSE(FORM_SetIndexSelected(form_handle(), nullptr, 0, true));
1044   EXPECT_FALSE(FORM_SetIndexSelected(form_handle(), page, -1, true));
1045   EXPECT_FALSE(FORM_IsIndexSelected(nullptr, nullptr, 0));
1046   EXPECT_FALSE(FORM_IsIndexSelected(nullptr, page, 0));
1047   EXPECT_FALSE(FORM_IsIndexSelected(form_handle(), nullptr, 0));
1048   EXPECT_FALSE(FORM_IsIndexSelected(form_handle(), page, -1));
1049 
1050   UnloadPage(page);
1051 }
1052 
TEST_F(FPDFFormFillEmbedderTest,BadApiInputsComboBox)1053 TEST_F(FPDFFormFillEmbedderTest, BadApiInputsComboBox) {
1054   ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1055   FPDF_PAGE page = LoadPage(0);
1056   ASSERT_TRUE(page);
1057 
1058   EXPECT_FALSE(FORM_SetIndexSelected(form_handle(), page, -1, true));
1059   EXPECT_FALSE(FORM_SetIndexSelected(form_handle(), page, 100, true));
1060   EXPECT_FALSE(FORM_IsIndexSelected(form_handle(), page, -1));
1061   EXPECT_FALSE(FORM_IsIndexSelected(form_handle(), page, 100));
1062 
1063   UnloadPage(page);
1064 }
1065 
TEST_F(FPDFFormFillEmbedderTest,BadApiInputsListBox)1066 TEST_F(FPDFFormFillEmbedderTest, BadApiInputsListBox) {
1067   ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1068   FPDF_PAGE page = LoadPage(0);
1069   ASSERT_TRUE(page);
1070 
1071   EXPECT_FALSE(FORM_SetIndexSelected(form_handle(), page, -1, true));
1072   EXPECT_FALSE(FORM_SetIndexSelected(form_handle(), page, 100, true));
1073   EXPECT_FALSE(FORM_IsIndexSelected(form_handle(), page, -1));
1074   EXPECT_FALSE(FORM_IsIndexSelected(form_handle(), page, 100));
1075 
1076   UnloadPage(page);
1077 }
1078 
TEST_F(FPDFFormFillTextFormEmbedderTest,GetSelectedTextEmptyAndBasicKeyboard)1079 TEST_F(FPDFFormFillTextFormEmbedderTest, GetSelectedTextEmptyAndBasicKeyboard) {
1080   // Test empty selection.
1081   CheckFocusedFieldText(L"");
1082   CheckSelection(L"");
1083 
1084   // Test basic selection.
1085   TypeTextIntoTextField(3, RegularFormBegin());
1086   CheckFocusedFieldText(L"ABC");
1087   SelectTextWithKeyboard(3, FWL_VKEY_Left, RegularFormAtX(123.0));
1088   CheckSelection(L"ABC");
1089 }
1090 
TEST_F(FPDFFormFillTextFormEmbedderTest,GetSelectedTextEmptyAndBasicMouse)1091 TEST_F(FPDFFormFillTextFormEmbedderTest, GetSelectedTextEmptyAndBasicMouse) {
1092   // Test empty selection.
1093   CheckFocusedFieldText(L"");
1094   CheckSelection(L"");
1095 
1096   // Test basic selection.
1097   TypeTextIntoTextField(3, RegularFormBegin());
1098   CheckFocusedFieldText(L"ABC");
1099   SelectTextWithMouse(RegularFormAtX(125.0), RegularFormBegin());
1100   CheckSelection(L"ABC");
1101 }
1102 
TEST_F(FPDFFormFillTextFormEmbedderTest,GetSelectedTextFragmentsKeyBoard)1103 TEST_F(FPDFFormFillTextFormEmbedderTest, GetSelectedTextFragmentsKeyBoard) {
1104   TypeTextIntoTextField(12, RegularFormBegin());
1105   CheckFocusedFieldText(L"ABCDEFGHIJKL");
1106 
1107   // Test selecting first character in forward direction.
1108   SelectTextWithKeyboard(1, FWL_VKEY_Right, RegularFormBegin());
1109   CheckSelection(L"A");
1110 
1111   // Test selecting entire long string in backwards direction.
1112   SelectTextWithKeyboard(12, FWL_VKEY_Left, RegularFormEnd());
1113   CheckSelection(L"ABCDEFGHIJKL");
1114 
1115   // Test selecting middle section in backwards direction.
1116   SelectTextWithKeyboard(6, FWL_VKEY_Left, RegularFormAtX(170.0));
1117   CheckSelection(L"DEFGHI");
1118 
1119   // Test selecting middle selection in forward direction.
1120   SelectTextWithKeyboard(6, FWL_VKEY_Right, RegularFormAtX(125.0));
1121   CheckSelection(L"DEFGHI");
1122 
1123   // Test selecting last character in backwards direction.
1124   SelectTextWithKeyboard(1, FWL_VKEY_Left, RegularFormEnd());
1125   CheckSelection(L"L");
1126   CheckFocusedFieldText(L"ABCDEFGHIJKL");
1127 }
1128 
TEST_F(FPDFFormFillTextFormEmbedderTest,GetSelectedTextFragmentsMouse)1129 TEST_F(FPDFFormFillTextFormEmbedderTest, GetSelectedTextFragmentsMouse) {
1130   TypeTextIntoTextField(12, RegularFormBegin());
1131 
1132   // Test selecting first character in forward direction.
1133   SelectTextWithMouse(RegularFormBegin(), RegularFormAtX(106.0));
1134   CheckSelection(L"A");
1135 
1136   // Test selecting entire long string in backwards direction.
1137   SelectAllRegularFormTextWithMouse();
1138   CheckSelection(L"ABCDEFGHIJKL");
1139 
1140   // Test selecting middle section in backwards direction.
1141   SelectTextWithMouse(RegularFormAtX(170.0), RegularFormAtX(125.0));
1142   CheckSelection(L"DEFGHI");
1143 
1144   // Test selecting middle selection in forward direction.
1145   SelectTextWithMouse(RegularFormAtX(125.0), RegularFormAtX(170.0));
1146   CheckSelection(L"DEFGHI");
1147 
1148   // Test selecting last character in backwards direction.
1149   SelectTextWithMouse(RegularFormEnd(), RegularFormAtX(186.0));
1150   CheckSelection(L"L");
1151 }
1152 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,GetSelectedTextEmptyAndBasicNormalComboBox)1153 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1154        GetSelectedTextEmptyAndBasicNormalComboBox) {
1155   // Test empty selection.
1156   CheckSelection(L"");
1157   CheckFocusedFieldText(L"");
1158 
1159   // Non-editable comboboxes don't allow selection with keyboard.
1160   SelectTextWithMouse(NonEditableFormBegin(), NonEditableFormAtX(142.0));
1161   CheckFocusedFieldText(L"Banana");
1162   CheckSelection(L"Banana");
1163 
1164   // Select other another provided option.
1165   SelectNonEditableFormOption(0);
1166   CheckFocusedFieldText(L"Apple");
1167   CheckSelection(L"Apple");
1168 }
1169 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,GetSelectedTextEmptyAndBasicEditableComboBoxKeyboard)1170 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1171        GetSelectedTextEmptyAndBasicEditableComboBoxKeyboard) {
1172   // Test empty selection.
1173   CheckSelection(L"");
1174   CheckFocusedFieldText(L"");
1175 
1176   // Test basic selection of text within user editable combobox using keyboard.
1177   TypeTextIntoTextField(3, EditableFormBegin());
1178   CheckFocusedFieldText(L"ABC");
1179   SelectTextWithKeyboard(3, FWL_VKEY_Left, EditableFormAtX(128.0));
1180   CheckSelection(L"ABC");
1181 
1182   // Select a provided option.
1183   SelectEditableFormOption(1);
1184   CheckSelection(L"Bar");
1185   CheckFocusedFieldText(L"Bar");
1186 }
1187 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,GetSelectedTextEmptyAndBasicEditableComboBoxMouse)1188 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1189        GetSelectedTextEmptyAndBasicEditableComboBoxMouse) {
1190   // Test empty selection.
1191   CheckSelection(L"");
1192 
1193   // Test basic selection of text within user editable combobox using mouse.
1194   TypeTextIntoTextField(3, EditableFormBegin());
1195   SelectTextWithMouse(EditableFormAtX(128.0), EditableFormBegin());
1196   CheckSelection(L"ABC");
1197 
1198   // Select a provided option.
1199   SelectEditableFormOption(2);
1200   CheckFocusedFieldText(L"Qux");
1201   CheckSelection(L"Qux");
1202 }
1203 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,GetSelectedTextFragmentsNormalComboBox)1204 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1205        GetSelectedTextFragmentsNormalComboBox) {
1206   CheckFocusedFieldText(L"");
1207 
1208   // Test selecting first character in forward direction.
1209   SelectTextWithMouse(NonEditableFormBegin(), NonEditableFormAtX(107.0));
1210   CheckFocusedFieldText(L"Banana");
1211   CheckSelection(L"B");
1212 
1213   // Test selecting entire string in backwards direction.
1214   SelectTextWithMouse(NonEditableFormAtX(142.0), NonEditableFormBegin());
1215   CheckSelection(L"Banana");
1216 
1217   // Test selecting middle section in backwards direction.
1218   SelectTextWithMouse(NonEditableFormAtX(135.0), NonEditableFormAtX(117.0));
1219   CheckSelection(L"nan");
1220 
1221   // Test selecting middle section in forward direction.
1222   SelectTextWithMouse(NonEditableFormAtX(117.0), NonEditableFormAtX(135.0));
1223   CheckSelection(L"nan");
1224 
1225   // Test selecting last character in backwards direction.
1226   SelectTextWithMouse(NonEditableFormAtX(142.0), NonEditableFormAtX(138.0));
1227   CheckSelection(L"a");
1228   CheckFocusedFieldText(L"Banana");
1229 
1230   // Select another option and then reset selection as first three chars.
1231   SelectNonEditableFormOption(2);
1232   CheckFocusedFieldText(L"Cherry");
1233   CheckSelection(L"Cherry");
1234   SelectTextWithMouse(NonEditableFormBegin(), NonEditableFormAtX(122.0));
1235   CheckSelection(L"Che");
1236 }
1237 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,GetSelectedTextFragmentsEditableComboBoxKeyboard)1238 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1239        GetSelectedTextFragmentsEditableComboBoxKeyboard) {
1240   CheckFocusedFieldText(L"");
1241   TypeTextIntoTextField(10, EditableFormBegin());
1242   CheckFocusedFieldText(L"ABCDEFGHIJ");
1243 
1244   // Test selecting first character in forward direction.
1245   SelectTextWithKeyboard(1, FWL_VKEY_Right, EditableFormBegin());
1246   CheckSelection(L"A");
1247 
1248   // Test selecting entire long string in backwards direction.
1249   SelectTextWithKeyboard(10, FWL_VKEY_Left, EditableFormEnd());
1250   CheckSelection(L"ABCDEFGHIJ");
1251 
1252   // Test selecting middle section in backwards direction.
1253   SelectTextWithKeyboard(5, FWL_VKEY_Left, EditableFormAtX(168.0));
1254   CheckSelection(L"DEFGH");
1255 
1256   // Test selecting middle selection in forward direction.
1257   SelectTextWithKeyboard(5, FWL_VKEY_Right, EditableFormAtX(127.0));
1258   CheckSelection(L"DEFGH");
1259 
1260   // Test selecting last character in backwards direction.
1261   SelectTextWithKeyboard(1, FWL_VKEY_Left, EditableFormEnd());
1262   CheckSelection(L"J");
1263 
1264   // Select a provided option and then reset selection as first two chars.
1265   SelectEditableFormOption(0);
1266   CheckSelection(L"Foo");
1267   SelectTextWithKeyboard(2, FWL_VKEY_Right, EditableFormBegin());
1268   CheckSelection(L"Fo");
1269   CheckFocusedFieldText(L"Foo");
1270 }
1271 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,GetSelectedTextFragmentsEditableComboBoxMouse)1272 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1273        GetSelectedTextFragmentsEditableComboBoxMouse) {
1274   TypeTextIntoTextField(10, EditableFormBegin());
1275 
1276   // Test selecting first character in forward direction.
1277   SelectTextWithMouse(EditableFormBegin(), EditableFormAtX(107.0));
1278   CheckSelection(L"A");
1279 
1280   // Test selecting entire long string in backwards direction.
1281   SelectAllEditableFormTextWithMouse();
1282   CheckSelection(L"ABCDEFGHIJ");
1283 
1284   // Test selecting middle section in backwards direction.
1285   SelectTextWithMouse(EditableFormAtX(168.0), EditableFormAtX(127.0));
1286   CheckSelection(L"DEFGH");
1287 
1288   // Test selecting middle selection in forward direction.
1289   SelectTextWithMouse(EditableFormAtX(127.0), EditableFormAtX(168.0));
1290   CheckSelection(L"DEFGH");
1291 
1292   // Test selecting last character in backwards direction.
1293   SelectTextWithMouse(EditableFormEnd(), EditableFormAtX(174.0));
1294   CheckSelection(L"J");
1295   CheckFocusedFieldText(L"ABCDEFGHIJ");
1296 }
1297 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,SetSelectionProgrammaticallyNonEditableField)1298 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1299        SetSelectionProgrammaticallyNonEditableField) {
1300   // Focus on non-editable form field and check that the value is as expected.
1301   // This is the value that is present in the field upon opening, we have not
1302   // changed it by setting focus.
1303   FocusOnNonEditableForm();
1304   CheckFocusedFieldText(L"Banana");
1305 
1306   // Make selections to change the value of the focused annotation
1307   // programmatically.
1308   SetIndexSelectedShouldSucceed(0, true);
1309   CheckFocusedFieldText(L"Apple");
1310 
1311   // Selecting an index that is already selected is success.
1312   SetIndexSelectedShouldSucceed(0, true);
1313   CheckFocusedFieldText(L"Apple");
1314 
1315   SetIndexSelectedShouldSucceed(9, true);
1316   CheckFocusedFieldText(L"Jackfruit");
1317 
1318   // Cannot deselect a combobox field - value unchanged.
1319   SetIndexSelectedShouldFail(9, false);
1320   CheckFocusedFieldText(L"Jackfruit");
1321 
1322   // Cannot select indices that are out of range - value unchanged.
1323   SetIndexSelectedShouldFail(100, true);
1324   SetIndexSelectedShouldFail(-100, true);
1325   CheckFocusedFieldText(L"Jackfruit");
1326 
1327   // Check that above actions are interchangeable with click actions, should be
1328   // able to use a combination of both.
1329   SelectNonEditableFormOption(1);
1330   CheckFocusedFieldText(L"Banana");
1331 }
1332 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,SetSelectionProgrammaticallyEditableField)1333 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1334        SetSelectionProgrammaticallyEditableField) {
1335   // Focus on editable form field and check that the value is as expected.
1336   // This is the value that is present in the field upon opening, we have not
1337   // changed it by setting focus.
1338   FocusOnEditableForm();
1339   CheckFocusedFieldText(L"");
1340 
1341   // Make selections to change value of the focused annotation
1342   // programmatically.
1343   SetIndexSelectedShouldSucceed(0, true);
1344   CheckFocusedFieldText(L"Foo");
1345 
1346   SetIndexSelectedShouldSucceed(1, true);
1347   CheckFocusedFieldText(L"Bar");
1348 
1349   // Selecting an index that is already selected is success.
1350   SetIndexSelectedShouldSucceed(1, true);
1351   CheckFocusedFieldText(L"Bar");
1352 
1353   // Cannot deselect a combobox field - value unchanged.
1354   SetIndexSelectedShouldFail(0, false);
1355   CheckFocusedFieldText(L"Bar");
1356 
1357   // Cannot select indices that are out of range - value unchanged.
1358   SetIndexSelectedShouldFail(100, true);
1359   SetIndexSelectedShouldFail(-100, true);
1360   CheckFocusedFieldText(L"Bar");
1361 
1362   // Check that above actions are interchangeable with click actions, should be
1363   // able to use a combination of both.
1364   SelectEditableFormOption(0);
1365   CheckFocusedFieldText(L"Foo");
1366 
1367   // Check that above actions are interchangeable with typing actions, should
1368   // be able to use a combination of both. Typing text into a text field after
1369   // selecting indices programmatically should be equivalent to doing so after
1370   // a user selects an index via click on the dropdown.
1371   SetIndexSelectedShouldSucceed(1, true);
1372   CheckFocusedFieldText(L"Bar");
1373   TypeTextIntoTextField(5, EditableFormBegin());
1374   CheckFocusedFieldText(L"ABCDEBar");
1375 }
1376 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,CheckIfIndexSelectedNonEditableField)1377 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1378        CheckIfIndexSelectedNonEditableField) {
1379   // Non-editable field is set to 'Banana' (index 1) upon opening.
1380   ClickOnFormFieldAtPoint(NonEditableFormBegin());
1381   for (int i = 0; i < 26; i++) {
1382     bool expected = i == 1;
1383     CheckIsIndexSelected(i, expected);
1384   }
1385 
1386   SelectNonEditableFormOption(0);
1387   CheckIsIndexSelected(0, true);
1388   for (int i = 1; i < 26; i++) {
1389     CheckIsIndexSelected(i, false);
1390   }
1391 }
1392 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,CheckIfIndexSelectedEditableField)1393 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1394        CheckIfIndexSelectedEditableField) {
1395   // Editable field has nothing selected upon opening.
1396   ClickOnFormFieldAtPoint(EditableFormBegin());
1397   CheckIsIndexSelected(0, false);
1398   CheckIsIndexSelected(1, false);
1399   CheckIsIndexSelected(2, false);
1400 
1401   SelectEditableFormOption(0);
1402   CheckIsIndexSelected(0, true);
1403   CheckIsIndexSelected(1, false);
1404   CheckIsIndexSelected(2, false);
1405 }
1406 
TEST_F(FPDFFormFillTextFormEmbedderTest,DeleteTextFieldEntireSelection)1407 TEST_F(FPDFFormFillTextFormEmbedderTest, DeleteTextFieldEntireSelection) {
1408   // Select entire contents of text field.
1409   TypeTextIntoTextField(12, RegularFormBegin());
1410   SelectAllRegularFormTextWithMouse();
1411   CheckFocusedFieldText(L"ABCDEFGHIJKL");
1412   CheckSelection(L"ABCDEFGHIJKL");
1413 
1414   // Test deleting current text selection. Select what remains after deletion to
1415   // check that remaining text is as expected.
1416   FORM_ReplaceSelection(form_handle(), page(), nullptr);
1417   CheckFocusedFieldText(L"");
1418 
1419   SelectTextWithKeyboard(12, FWL_VKEY_Left, RegularFormEnd());
1420   CheckSelection(L"");
1421 }
1422 
TEST_F(FPDFFormFillTextFormEmbedderTest,DeleteTextFieldSelectionMiddle)1423 TEST_F(FPDFFormFillTextFormEmbedderTest, DeleteTextFieldSelectionMiddle) {
1424   // Select middle section of text.
1425   TypeTextIntoTextField(12, RegularFormBegin());
1426   SelectTextWithMouse(RegularFormAtX(170.0), RegularFormAtX(125.0));
1427   CheckFocusedFieldText(L"ABCDEFGHIJKL");
1428   CheckSelection(L"DEFGHI");
1429 
1430   // Test deleting current text selection. Select what remains after deletion to
1431   // check that remaining text is as expected.
1432   FORM_ReplaceSelection(form_handle(), page(), nullptr);
1433   CheckFocusedFieldText(L"ABCJKL");
1434   SelectTextWithKeyboard(12, FWL_VKEY_Left, RegularFormEnd());
1435   CheckSelection(L"ABCJKL");
1436 }
1437 
TEST_F(FPDFFormFillTextFormEmbedderTest,DeleteTextFieldSelectionLeft)1438 TEST_F(FPDFFormFillTextFormEmbedderTest, DeleteTextFieldSelectionLeft) {
1439   // Select first few characters of text.
1440   TypeTextIntoTextField(12, RegularFormBegin());
1441   SelectTextWithMouse(RegularFormBegin(), RegularFormAtX(132.0));
1442   CheckSelection(L"ABCD");
1443 
1444   // Test deleting current text selection. Select what remains after deletion to
1445   // check that remaining text is as expected.
1446   FORM_ReplaceSelection(form_handle(), page(), nullptr);
1447   CheckFocusedFieldText(L"EFGHIJKL");
1448   SelectTextWithKeyboard(12, FWL_VKEY_Left, RegularFormEnd());
1449   CheckSelection(L"EFGHIJKL");
1450 }
1451 
TEST_F(FPDFFormFillTextFormEmbedderTest,DeleteTextFieldSelectionRight)1452 TEST_F(FPDFFormFillTextFormEmbedderTest, DeleteTextFieldSelectionRight) {
1453   // Select last few characters of text.
1454   TypeTextIntoTextField(12, RegularFormBegin());
1455   SelectTextWithMouse(RegularFormEnd(), RegularFormAtX(165.0));
1456   CheckSelection(L"IJKL");
1457 
1458   // Test deleting current text selection. Select what remains after deletion to
1459   // check that remaining text is as expected.
1460   FORM_ReplaceSelection(form_handle(), page(), nullptr);
1461   CheckFocusedFieldText(L"ABCDEFGH");
1462   SelectTextWithKeyboard(12, FWL_VKEY_Left, RegularFormEnd());
1463   CheckSelection(L"ABCDEFGH");
1464 }
1465 
TEST_F(FPDFFormFillTextFormEmbedderTest,DeleteEmptyTextFieldSelection)1466 TEST_F(FPDFFormFillTextFormEmbedderTest, DeleteEmptyTextFieldSelection) {
1467   // Do not select text.
1468   TypeTextIntoTextField(12, RegularFormBegin());
1469   CheckSelection(L"");
1470 
1471   // Test that attempt to delete empty text selection has no effect.
1472   FORM_ReplaceSelection(form_handle(), page(), nullptr);
1473   CheckFocusedFieldText(L"ABCDEFGHIJKL");
1474   SelectTextWithKeyboard(12, FWL_VKEY_Left, RegularFormEnd());
1475   CheckSelection(L"ABCDEFGHIJKL");
1476 }
1477 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,DeleteEditableComboBoxEntireSelection)1478 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1479        DeleteEditableComboBoxEntireSelection) {
1480   // Select entire contents of user-editable combobox text field.
1481   TypeTextIntoTextField(10, EditableFormBegin());
1482   SelectAllEditableFormTextWithMouse();
1483   CheckSelection(L"ABCDEFGHIJ");
1484 
1485   // Test deleting current text selection. Select what remains after deletion to
1486   // check that remaining text is as expected.
1487   FORM_ReplaceSelection(form_handle(), page(), nullptr);
1488   CheckFocusedFieldText(L"");
1489   SelectAllEditableFormTextWithMouse();
1490   CheckSelection(L"");
1491 }
1492 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,DeleteEditableComboBoxSelectionMiddle)1493 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1494        DeleteEditableComboBoxSelectionMiddle) {
1495   // Select middle section of text.
1496   TypeTextIntoTextField(10, EditableFormBegin());
1497   SelectTextWithMouse(EditableFormAtX(168.0), EditableFormAtX(127.0));
1498   CheckSelection(L"DEFGH");
1499 
1500   // Test deleting current text selection. Select what remains after deletion to
1501   // check that remaining text is as expected.
1502   FORM_ReplaceSelection(form_handle(), page(), nullptr);
1503   CheckFocusedFieldText(L"ABCIJ");
1504   SelectAllEditableFormTextWithMouse();
1505   CheckSelection(L"ABCIJ");
1506 }
1507 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,DeleteEditableComboBoxSelectionLeft)1508 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1509        DeleteEditableComboBoxSelectionLeft) {
1510   // Select first few characters of text.
1511   TypeTextIntoTextField(10, EditableFormBegin());
1512   SelectTextWithMouse(EditableFormBegin(), EditableFormAtX(132.0));
1513   CheckSelection(L"ABCD");
1514 
1515   // Test deleting current text selection. Select what remains after deletion to
1516   // check that remaining text is as expected.
1517   FORM_ReplaceSelection(form_handle(), page(), nullptr);
1518   SelectAllEditableFormTextWithMouse();
1519   CheckSelection(L"EFGHIJ");
1520 }
1521 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,DeleteEditableComboBoxSelectionRight)1522 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1523        DeleteEditableComboBoxSelectionRight) {
1524   // Select last few characters of text.
1525   TypeTextIntoTextField(10, EditableFormBegin());
1526   SelectTextWithMouse(EditableFormEnd(), EditableFormAtX(152.0));
1527   CheckSelection(L"GHIJ");
1528 
1529   // Test deleting current text selection. Select what remains after deletion to
1530   // check that remaining text is as expected.
1531   FORM_ReplaceSelection(form_handle(), page(), nullptr);
1532   SelectAllEditableFormTextWithMouse();
1533   CheckSelection(L"ABCDEF");
1534 }
1535 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,DeleteEmptyEditableComboBoxSelection)1536 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1537        DeleteEmptyEditableComboBoxSelection) {
1538   // Do not select text.
1539   TypeTextIntoTextField(10, EditableFormBegin());
1540   CheckSelection(L"");
1541 
1542   // Test that attempt to delete empty text selection has no effect.
1543   FORM_ReplaceSelection(form_handle(), page(), nullptr);
1544   SelectAllEditableFormTextWithMouse();
1545   CheckSelection(L"ABCDEFGHIJ");
1546 }
1547 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextInEmptyTextField)1548 TEST_F(FPDFFormFillTextFormEmbedderTest, InsertTextInEmptyTextField) {
1549   CheckFocusedFieldText(L"");
1550   ClickOnFormFieldAtPoint(RegularFormBegin());
1551   CheckFocusedFieldText(L"");
1552 
1553   // Test inserting text into empty text field.
1554   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1555   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1556   CheckFocusedFieldText(L"Hello");
1557 
1558   // Select entire contents of text field to check that insertion worked
1559   // as expected.
1560   CheckSelection(L"");
1561   SelectAllRegularFormTextWithMouse();
1562   CheckSelection(L"Hello");
1563 }
1564 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextInPopulatedTextFieldLeft)1565 TEST_F(FPDFFormFillTextFormEmbedderTest, InsertTextInPopulatedTextFieldLeft) {
1566   TypeTextIntoTextField(8, RegularFormBegin());
1567   CheckFocusedFieldText(L"ABCDEFGH");
1568 
1569   // Click on the leftmost part of the text field.
1570   ClickOnFormFieldAtPoint(RegularFormBegin());
1571   CheckFocusedFieldText(L"ABCDEFGH");
1572 
1573   // Test inserting text in front of existing text in text field.
1574   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1575   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1576   CheckFocusedFieldText(L"HelloABCDEFGH");
1577 
1578   // Select entire contents of text field to check that insertion worked
1579   // as expected.
1580   CheckSelection(L"");
1581   SelectAllRegularFormTextWithMouse();
1582   CheckSelection(L"HelloABCDEFGH");
1583 }
1584 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextInPopulatedTextFieldMiddle)1585 TEST_F(FPDFFormFillTextFormEmbedderTest, InsertTextInPopulatedTextFieldMiddle) {
1586   TypeTextIntoTextField(8, RegularFormBegin());
1587 
1588   // Click on the middle of the text field.
1589   ClickOnFormFieldAtPoint(RegularFormAtX(134.0));
1590 
1591   // Test inserting text in the middle of existing text in text field.
1592   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1593   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1594   CheckFocusedFieldText(L"ABCDHelloEFGH");
1595 
1596   // Select entire contents of text field to check that insertion worked
1597   // as expected.
1598   CheckSelection(L"");
1599   SelectAllRegularFormTextWithMouse();
1600   CheckSelection(L"ABCDHelloEFGH");
1601 }
1602 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextInPopulatedTextFieldRight)1603 TEST_F(FPDFFormFillTextFormEmbedderTest, InsertTextInPopulatedTextFieldRight) {
1604   TypeTextIntoTextField(8, RegularFormBegin());
1605 
1606   // Click on the rightmost part of the text field.
1607   ClickOnFormFieldAtPoint(RegularFormAtX(166.0));
1608 
1609   // Test inserting text behind existing text in text field.
1610   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1611   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1612   CheckFocusedFieldText(L"ABCDEFGHHello");
1613 
1614   // Select entire contents of text field to check that insertion worked
1615   // as expected.
1616   CheckSelection(L"");
1617   SelectAllRegularFormTextWithMouse();
1618   CheckSelection(L"ABCDEFGHHello");
1619 }
1620 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextAndReplaceSelectionInPopulatedTextFieldWhole)1621 TEST_F(FPDFFormFillTextFormEmbedderTest,
1622        InsertTextAndReplaceSelectionInPopulatedTextFieldWhole) {
1623   TypeTextIntoTextField(12, RegularFormBegin());
1624 
1625   // Select entire string in text field.
1626   CheckSelection(L"");
1627   SelectTextWithKeyboard(12, FWL_VKEY_Left, RegularFormEnd());
1628   CheckSelection(L"ABCDEFGHIJKL");
1629 
1630   // Test replacing text selection with text to be inserted.
1631   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1632   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1633   CheckFocusedFieldText(L"Hello");
1634 
1635   // Select entire contents of text field to check that insertion worked
1636   // as expected.
1637   CheckSelection(L"");
1638   SelectAllRegularFormTextWithMouse();
1639   CheckSelection(L"Hello");
1640 }
1641 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextAndReplaceSelectionInPopulatedTextFieldLeft)1642 TEST_F(FPDFFormFillTextFormEmbedderTest,
1643        InsertTextAndReplaceSelectionInPopulatedTextFieldLeft) {
1644   TypeTextIntoTextField(12, RegularFormBegin());
1645 
1646   // Select left portion of string in text field.
1647   CheckSelection(L"");
1648   SelectTextWithKeyboard(6, FWL_VKEY_Left, RegularFormAtX(148.0));
1649   CheckSelection(L"ABCDEF");
1650 
1651   // Test replacing text selection with text to be inserted.
1652   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1653   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1654   CheckFocusedFieldText(L"HelloGHIJKL");
1655 
1656   // Select entire contents of text field to check that insertion worked
1657   // as expected.
1658   CheckSelection(L"");
1659   SelectAllRegularFormTextWithMouse();
1660   CheckSelection(L"HelloGHIJKL");
1661 }
1662 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextAndReplaceSelectionInPopulatedTextFieldMiddle)1663 TEST_F(FPDFFormFillTextFormEmbedderTest,
1664        InsertTextAndReplaceSelectionInPopulatedTextFieldMiddle) {
1665   TypeTextIntoTextField(12, RegularFormBegin());
1666 
1667   // Select middle portion of string in text field.
1668   CheckSelection(L"");
1669   SelectTextWithKeyboard(6, FWL_VKEY_Left, RegularFormAtX(171.0));
1670   CheckSelection(L"DEFGHI");
1671 
1672   // Test replacing text selection with text to be inserted.
1673   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1674   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1675 
1676   // Select entire contents of text field to check that insertion worked
1677   // as expected.
1678   CheckSelection(L"");
1679   SelectAllRegularFormTextWithMouse();
1680   CheckSelection(L"ABCHelloJKL");
1681 }
1682 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextAndReplaceSelectionInPopulatedTextFieldRight)1683 TEST_F(FPDFFormFillTextFormEmbedderTest,
1684        InsertTextAndReplaceSelectionInPopulatedTextFieldRight) {
1685   TypeTextIntoTextField(12, RegularFormBegin());
1686 
1687   // Select right portion of string in text field.
1688   CheckSelection(L"");
1689   SelectTextWithKeyboard(6, FWL_VKEY_Left, RegularFormEnd());
1690   CheckSelection(L"GHIJKL");
1691 
1692   // Test replacing text selection with text to be inserted.
1693   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1694   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1695 
1696   // Select entire contents of text field to check that insertion worked
1697   // as expected.
1698   CheckSelection(L"");
1699   SelectAllRegularFormTextWithMouse();
1700   CheckSelection(L"ABCDEFHello");
1701 }
1702 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,InsertTextInEmptyEditableComboBox)1703 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1704        InsertTextInEmptyEditableComboBox) {
1705   ClickOnFormFieldAtPoint(EditableFormBegin());
1706   CheckFocusedFieldText(L"");
1707 
1708   // Test inserting text into empty user-editable combobox.
1709   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1710   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1711   CheckFocusedFieldText(L"Hello");
1712 
1713   // Select entire contents of user-editable combobox text field to check that
1714   // insertion worked as expected.
1715   CheckSelection(L"");
1716   SelectAllEditableFormTextWithMouse();
1717   CheckSelection(L"Hello");
1718 }
1719 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,InsertTextInPopulatedEditableComboBoxLeft)1720 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1721        InsertTextInPopulatedEditableComboBoxLeft) {
1722   TypeTextIntoTextField(6, EditableFormBegin());
1723 
1724   // Click on the leftmost part of the user-editable combobox.
1725   ClickOnFormFieldAtPoint(EditableFormBegin());
1726 
1727   // Test inserting text in front of existing text in user-editable combobox.
1728   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1729   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1730 
1731   // Select entire contents of user-editable combobox text field to check that
1732   // insertion worked as expected.
1733   CheckSelection(L"");
1734   SelectAllEditableFormTextWithMouse();
1735   CheckSelection(L"HelloABCDEF");
1736 }
1737 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,InsertTextInPopulatedEditableComboBoxMiddle)1738 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1739        InsertTextInPopulatedEditableComboBoxMiddle) {
1740   TypeTextIntoTextField(6, EditableFormBegin());
1741 
1742   // Click on the middle of the user-editable combobox.
1743   ClickOnFormFieldAtPoint(EditableFormAtX(126.0));
1744 
1745   // Test inserting text in the middle of existing text in user-editable
1746   // combobox.
1747   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1748   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1749 
1750   // Select entire contents of user-editable combobox text field to check that
1751   // insertion worked as expected.
1752   CheckSelection(L"");
1753   SelectAllEditableFormTextWithMouse();
1754   CheckSelection(L"ABCHelloDEF");
1755 }
1756 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,InsertTextInPopulatedEditableComboBoxRight)1757 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1758        InsertTextInPopulatedEditableComboBoxRight) {
1759   TypeTextIntoTextField(6, EditableFormBegin());
1760 
1761   // Click on the rightmost part of the user-editable combobox.
1762   ClickOnFormFieldAtPoint(EditableFormEnd());
1763 
1764   // Test inserting text behind existing text in user-editable combobox.
1765   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1766   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1767 
1768   // Select entire contents of user-editable combobox text field to check that
1769   // insertion worked as expected.
1770   CheckSelection(L"");
1771   SelectAllEditableFormTextWithMouse();
1772   CheckSelection(L"ABCDEFHello");
1773 }
1774 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,InsertTextAndReplaceSelectionInPopulatedEditableComboBoxWhole)1775 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1776        InsertTextAndReplaceSelectionInPopulatedEditableComboBoxWhole) {
1777   TypeTextIntoTextField(10, EditableFormBegin());
1778 
1779   // Select entire string in user-editable combobox.
1780   CheckSelection(L"");
1781   SelectTextWithKeyboard(10, FWL_VKEY_Left, EditableFormEnd());
1782   CheckSelection(L"ABCDEFGHIJ");
1783 
1784   // Test replacing text selection with text to be inserted.
1785   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1786   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1787 
1788   // Select entire contents of user-editable combobox text field to check that
1789   // insertion worked as expected.
1790   CheckSelection(L"");
1791   SelectAllEditableFormTextWithMouse();
1792   CheckSelection(L"Hello");
1793 }
1794 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,InsertTextAndReplaceSelectionInPopulatedEditableComboBoxLeft)1795 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1796        InsertTextAndReplaceSelectionInPopulatedEditableComboBoxLeft) {
1797   TypeTextIntoTextField(10, EditableFormBegin());
1798 
1799   // Select left portion of string in user-editable combobox.
1800   CheckSelection(L"");
1801   SelectTextWithKeyboard(5, FWL_VKEY_Left, EditableFormAtX(142.0));
1802   CheckSelection(L"ABCDE");
1803 
1804   // Test replacing text selection with text to be inserted.
1805   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1806   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1807 
1808   // Select entire contents of user-editable combobox text field to check that
1809   // insertion worked as expected.
1810   CheckSelection(L"");
1811   SelectAllEditableFormTextWithMouse();
1812   CheckSelection(L"HelloFGHIJ");
1813 }
1814 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,InsertTextAndReplaceSelectionInPopulatedEditableComboBoxMiddle)1815 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1816        InsertTextAndReplaceSelectionInPopulatedEditableComboBoxMiddle) {
1817   TypeTextIntoTextField(10, EditableFormBegin());
1818 
1819   // Select middle portion of string in user-editable combobox.
1820   CheckSelection(L"");
1821   SelectTextWithKeyboard(5, FWL_VKEY_Left, EditableFormAtX(167.0));
1822   CheckSelection(L"DEFGH");
1823 
1824   // Test replacing text selection with text to be inserted.
1825   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1826   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1827 
1828   // Select entire contents of user-editable combobox text field to check that
1829   // insertion worked as expected.
1830   CheckSelection(L"");
1831   SelectAllEditableFormTextWithMouse();
1832   CheckSelection(L"ABCHelloIJ");
1833 }
1834 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,InsertTextAndReplaceSelectionInPopulatedEditableComboBoxRight)1835 TEST_F(FPDFFormFillComboBoxFormEmbedderTest,
1836        InsertTextAndReplaceSelectionInPopulatedEditableComboBoxRight) {
1837   TypeTextIntoTextField(10, EditableFormBegin());
1838 
1839   // Select right portion of string in user-editable combobox.
1840   CheckSelection(L"");
1841   SelectTextWithKeyboard(5, FWL_VKEY_Left, EditableFormEnd());
1842   CheckSelection(L"FGHIJ");
1843 
1844   // Test replacing text selection with text to be inserted.
1845   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello");
1846   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1847 
1848   // Select entire contents of user-editable combobox text field to check that
1849   // insertion worked as expected.
1850   CheckSelection(L"");
1851   SelectAllEditableFormTextWithMouse();
1852   CheckSelection(L"ABCDEHello");
1853 }
1854 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextInEmptyCharLimitTextFieldOverflow)1855 TEST_F(FPDFFormFillTextFormEmbedderTest,
1856        InsertTextInEmptyCharLimitTextFieldOverflow) {
1857   // Click on the textfield.
1858   CheckFocusedFieldText(L"");
1859   ClickOnFormFieldAtPoint(CharLimitFormEnd());
1860   CheckFocusedFieldText(L"Elephant");
1861 
1862   // Delete pre-filled contents of text field with char limit.
1863   CheckSelection(L"");
1864   SelectAllCharLimitFormTextWithMouse();
1865   CheckSelection(L"Elephant");
1866   FORM_ReplaceSelection(form_handle(), page(), nullptr);
1867   CheckFocusedFieldText(L"");
1868 
1869   // Test inserting text into now empty text field so text to be inserted
1870   // exceeds the char limit and is cut off.
1871   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hippopotamus");
1872   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1873   CheckFocusedFieldText(L"Hippopotam");
1874 
1875   // Select entire contents of text field to check that insertion worked
1876   // as expected.
1877   CheckSelection(L"");
1878   SelectAllCharLimitFormTextWithMouse();
1879   CheckSelection(L"Hippopotam");
1880 }
1881 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextInEmptyCharLimitTextFieldFit)1882 TEST_F(FPDFFormFillTextFormEmbedderTest,
1883        InsertTextInEmptyCharLimitTextFieldFit) {
1884   // Click on the textfield.
1885   ClickOnFormFieldAtPoint(CharLimitFormEnd());
1886   CheckFocusedFieldText(L"Elephant");
1887 
1888   // Delete pre-filled contents of text field with char limit.
1889   CheckSelection(L"");
1890   SelectAllCharLimitFormTextWithMouse();
1891   CheckSelection(L"Elephant");
1892   FORM_ReplaceSelection(form_handle(), page(), nullptr);
1893 
1894   // Test inserting text into now empty text field so text to be inserted
1895   // exceeds the char limit and is cut off.
1896   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Zebra");
1897   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1898   CheckFocusedFieldText(L"Zebra");
1899 
1900   // Select entire contents of text field to check that insertion worked
1901   // as expected.
1902   CheckSelection(L"");
1903   SelectAllCharLimitFormTextWithMouse();
1904   CheckSelection(L"Zebra");
1905 }
1906 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextInPopulatedCharLimitTextFieldLeft)1907 TEST_F(FPDFFormFillTextFormEmbedderTest,
1908        InsertTextInPopulatedCharLimitTextFieldLeft) {
1909   // Click on the leftmost part of the text field.
1910   ClickOnFormFieldAtPoint(CharLimitFormBegin());
1911 
1912   // Test inserting text in front of existing text in text field.
1913   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hippopotamus");
1914   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1915 
1916   // Select entire contents of text field to check that insertion worked
1917   // as expected.
1918   CheckSelection(L"");
1919   SelectAllCharLimitFormTextWithMouse();
1920   CheckSelection(L"HiElephant");
1921 }
1922 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextInPopulatedCharLimitTextFieldMiddle)1923 TEST_F(FPDFFormFillTextFormEmbedderTest,
1924        InsertTextInPopulatedCharLimitTextFieldMiddle) {
1925   CheckFocusedFieldText(L"");
1926   TypeTextIntoTextField(8, RegularFormBegin());
1927   CheckFocusedFieldText(L"ABCDEFGH");
1928 
1929   // Click on the middle of the text field.
1930   ClickOnFormFieldAtPoint(CharLimitFormAtX(134.0));
1931   CheckFocusedFieldText(L"Elephant");
1932 
1933   // Test inserting text in the middle of existing text in text field.
1934   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hippopotamus");
1935   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1936   CheckFocusedFieldText(L"ElephHiant");
1937 
1938   // Select entire contents of text field to check that insertion worked
1939   // as expected.
1940   CheckSelection(L"");
1941   SelectAllCharLimitFormTextWithMouse();
1942   CheckSelection(L"ElephHiant");
1943 }
1944 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextInPopulatedCharLimitTextFieldRight)1945 TEST_F(FPDFFormFillTextFormEmbedderTest,
1946        InsertTextInPopulatedCharLimitTextFieldRight) {
1947   TypeTextIntoTextField(8, RegularFormBegin());
1948 
1949   // Click on the rightmost part of the text field.
1950   ClickOnFormFieldAtPoint(CharLimitFormAtX(166.0));
1951 
1952   // Test inserting text behind existing text in text field.
1953   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hippopotamus");
1954   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1955 
1956   // Select entire contents of text field to check that insertion worked
1957   // as expected.
1958   CheckSelection(L"");
1959   SelectAllCharLimitFormTextWithMouse();
1960   CheckSelection(L"ElephantHi");
1961 }
1962 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextAndReplaceSelectionInPopulatedCharLimitTextFieldWhole)1963 TEST_F(FPDFFormFillTextFormEmbedderTest,
1964        InsertTextAndReplaceSelectionInPopulatedCharLimitTextFieldWhole) {
1965   TypeTextIntoTextField(12, RegularFormBegin());
1966 
1967   // Select entire string in text field.
1968   CheckSelection(L"");
1969   SelectTextWithKeyboard(12, FWL_VKEY_Left, CharLimitFormEnd());
1970   CheckSelection(L"Elephant");
1971 
1972   // Test replacing text selection with text to be inserted.
1973   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hippopotamus");
1974   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1975 
1976   // Select entire contents of text field to check that insertion worked
1977   // as expected.
1978   CheckSelection(L"");
1979   SelectAllCharLimitFormTextWithMouse();
1980   CheckSelection(L"Hippopotam");
1981 }
1982 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextAndReplaceSelectionInPopulatedCharLimitTextFieldLeft)1983 TEST_F(FPDFFormFillTextFormEmbedderTest,
1984        InsertTextAndReplaceSelectionInPopulatedCharLimitTextFieldLeft) {
1985   TypeTextIntoTextField(12, RegularFormBegin());
1986 
1987   // Select left portion of string in text field.
1988   CheckSelection(L"");
1989   SelectTextWithKeyboard(4, FWL_VKEY_Left, CharLimitFormAtX(122.0));
1990   CheckSelection(L"Elep");
1991 
1992   // Test replacing text selection with text to be inserted.
1993   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hippopotamus");
1994   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
1995 
1996   // Select entire contents of text field to check that insertion worked
1997   // as expected.
1998   CheckSelection(L"");
1999   SelectAllCharLimitFormTextWithMouse();
2000   CheckSelection(L"Hippophant");
2001 }
2002 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextAndReplaceSelectionInPopulatedCharLimitTextFieldMiddle)2003 TEST_F(FPDFFormFillTextFormEmbedderTest,
2004        InsertTextAndReplaceSelectionInPopulatedCharLimitTextFieldMiddle) {
2005   TypeTextIntoTextField(12, RegularFormBegin());
2006 
2007   // Select middle portion of string in text field.
2008   CheckSelection(L"");
2009   SelectTextWithKeyboard(4, FWL_VKEY_Left, CharLimitFormAtX(136.0));
2010   CheckSelection(L"epha");
2011 
2012   // Test replacing text selection with text to be inserted.
2013   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hippopotamus");
2014   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
2015 
2016   // Select entire contents of text field to check that insertion worked
2017   // as expected.
2018   CheckSelection(L"");
2019   SelectAllCharLimitFormTextWithMouse();
2020   CheckSelection(L"ElHippopnt");
2021 }
2022 
TEST_F(FPDFFormFillTextFormEmbedderTest,InsertTextAndReplaceSelectionInPopulatedCharLimitTextFieldRight)2023 TEST_F(FPDFFormFillTextFormEmbedderTest,
2024        InsertTextAndReplaceSelectionInPopulatedCharLimitTextFieldRight) {
2025   TypeTextIntoTextField(12, RegularFormBegin());
2026 
2027   // Select right portion of string in text field.
2028   CheckSelection(L"");
2029   SelectTextWithKeyboard(4, FWL_VKEY_Left, CharLimitFormAtX(152.0));
2030   CheckSelection(L"hant");
2031 
2032   // Test replacing text selection with text to be inserted.
2033   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hippopotamus");
2034   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
2035 
2036   // Select entire contents of text field to check that insertion worked
2037   // as expected.
2038   CheckSelection(L"");
2039   SelectAllCharLimitFormTextWithMouse();
2040   CheckSelection(L"ElepHippop");
2041 }
2042 
TEST_F(FPDFFormFillTextFormEmbedderTest,DoubleClickInTextField)2043 TEST_F(FPDFFormFillTextFormEmbedderTest, DoubleClickInTextField) {
2044   CheckFocusedFieldText(L"");
2045   ClickOnFormFieldAtPoint(RegularFormBegin());
2046   CheckFocusedFieldText(L"");
2047 
2048   // Test inserting text into empty text field.
2049   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"Hello World");
2050   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
2051   CheckFocusedFieldText(L"Hello World");
2052 
2053   // Make sure double clicking selects the entire line.
2054   CheckSelection(L"");
2055   DoubleClickOnFormFieldAtPoint(RegularFormBegin());
2056   CheckSelection(L"Hello World");
2057 }
2058 
TEST_F(FPDFFormFillTextFormEmbedderTest,FocusChanges)2059 TEST_F(FPDFFormFillTextFormEmbedderTest, FocusChanges) {
2060   static const CFX_PointF kNonFormPoint(1, 1);
2061   CheckFocusedFieldText(L"");
2062   ClickOnFormFieldAtPoint(CharLimitFormEnd());
2063   CheckFocusedFieldText(L"Elephant");
2064   ClickOnFormFieldAtPoint(RegularFormBegin());
2065   CheckFocusedFieldText(L"");
2066   TypeTextIntoTextField(3, CharLimitFormBegin());
2067   CheckFocusedFieldText(L"ABElephant");
2068   TypeTextIntoTextField(5, RegularFormBegin());
2069   CheckFocusedFieldText(L"ABCDE");
2070   ClickOnFormFieldAtPoint(CharLimitFormEnd());
2071   CheckFocusedFieldText(L"ABElephant");
2072   ClickOnFormFieldAtPoint(kNonFormPoint);
2073   CheckFocusedFieldText(L"");
2074   ClickOnFormFieldAtPoint(kNonFormPoint);
2075   CheckFocusedFieldText(L"");
2076   ClickOnFormFieldAtPoint(CharLimitFormBegin());
2077   CheckFocusedFieldText(L"ABElephant");
2078   ClickOnFormFieldAtPoint(CharLimitFormEnd());
2079   CheckFocusedFieldText(L"ABElephant");
2080   ClickOnFormFieldAtPoint(RegularFormEnd());
2081   CheckFocusedFieldText(L"ABCDE");
2082   ClickOnFormFieldAtPoint(RegularFormBegin());
2083   CheckFocusedFieldText(L"ABCDE");
2084   ClickOnFormFieldAtPoint(RegularFormBegin());
2085   CheckFocusedFieldText(L"ABCDE");
2086   ClickOnFormFieldAtPoint(CharLimitFormBegin());
2087   CheckFocusedFieldText(L"ABElephant");
2088   FORM_ForceToKillFocus(form_handle());
2089   CheckFocusedFieldText(L"");
2090 }
2091 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,FocusChanges)2092 TEST_F(FPDFFormFillComboBoxFormEmbedderTest, FocusChanges) {
2093   static const CFX_PointF kNonFormPoint(1, 1);
2094   CheckFocusedFieldText(L"");
2095   ClickOnFormFieldAtPoint(NonEditableFormBegin());
2096   CheckFocusedFieldText(L"Banana");
2097   ClickOnFormFieldAtPoint(EditableFormBegin());
2098   CheckFocusedFieldText(L"");
2099   ClickOnFormFieldAtPoint(NonEditableFormEnd());
2100   CheckFocusedFieldText(L"Banana");
2101   ClickOnFormFieldAtPoint(NonEditableFormBegin());
2102   CheckFocusedFieldText(L"Banana");
2103   FORM_ForceToKillFocus(form_handle());
2104   CheckFocusedFieldText(L"");
2105   ClickOnFormFieldAtPoint(EditableFormBegin());
2106   CheckFocusedFieldText(L"");
2107   TypeTextIntoTextField(3, EditableFormBegin());
2108   CheckFocusedFieldText(L"ABC");
2109   ClickOnFormFieldAtPoint(kNonFormPoint);
2110   CheckFocusedFieldText(L"");
2111   TypeTextIntoTextField(3, EditableFormEnd());
2112   CheckFocusedFieldText(L"ABCABC");
2113   ClickOnFormFieldAtPoint(kNonFormPoint);
2114   CheckFocusedFieldText(L"");
2115   ClickOnFormFieldAtPoint(EditableFormDropDown());
2116   CheckFocusedFieldText(L"ABCABC");
2117   FORM_ForceToKillFocus(form_handle());
2118   CheckFocusedFieldText(L"");
2119   ClickOnFormFieldAtPoint(NonEditableFormDropDown());
2120   CheckFocusedFieldText(L"Banana");
2121   ClickOnFormFieldAtPoint(kNonFormPoint);
2122   CheckFocusedFieldText(L"");
2123   ClickOnFormFieldAtPoint(NonEditableFormEnd());
2124   CheckFocusedFieldText(L"Banana");
2125 
2126   // Typing into non-editable field results in selecting a different option.
2127   TypeTextIntoTextField(1, NonEditableFormEnd());
2128   CheckFocusedFieldText(L"Apple");
2129   TypeTextIntoTextField(3, NonEditableFormEnd());
2130   CheckFocusedFieldText(L"Cherry");
2131   TypeTextIntoTextField(2, NonEditableFormEnd());
2132   CheckFocusedFieldText(L"Banana");
2133 
2134   SelectEditableFormOption(0);
2135   CheckFocusedFieldText(L"Foo");
2136   SelectEditableFormOption(1);
2137   CheckFocusedFieldText(L"Bar");
2138   SelectEditableFormOption(2);
2139   CheckFocusedFieldText(L"Qux");
2140   SelectNonEditableFormOption(1);
2141   CheckFocusedFieldText(L"Banana");
2142   SelectNonEditableFormOption(0);
2143   CheckFocusedFieldText(L"Apple");
2144   SelectNonEditableFormOption(2);
2145   CheckFocusedFieldText(L"Cherry");
2146 
2147   // Typing into an editable field changes the text in the option.
2148   SelectEditableFormOption(0);
2149   CheckFocusedFieldText(L"Foo");
2150   TypeTextIntoTextField(5, EditableFormBegin());
2151   CheckFocusedFieldText(L"ABCDEFoo");
2152   SelectEditableFormOption(2);
2153   CheckFocusedFieldText(L"Qux");
2154   TypeTextIntoTextField(2, EditableFormEnd());
2155   CheckFocusedFieldText(L"QuxAB");
2156 
2157   // But a previously edited option is reset when selected again.
2158   SelectEditableFormOption(0);
2159   CheckFocusedFieldText(L"Foo");
2160   TypeTextIntoTextField(1, EditableFormBegin());
2161   CheckFocusedFieldText(L"AFoo");
2162   SelectEditableFormOption(0);
2163   CheckFocusedFieldText(L"Foo");
2164 }
2165 
TEST_F(FPDFFormFillTextFormEmbedderTest,UndoRedo)2166 TEST_F(FPDFFormFillTextFormEmbedderTest, UndoRedo) {
2167   ClickOnFormFieldAtPoint(RegularFormBegin());
2168   CheckFocusedFieldText(L"");
2169   CheckCanUndo(false);
2170   CheckCanRedo(false);
2171 
2172   TypeTextIntoTextField(5, RegularFormBegin());
2173   CheckFocusedFieldText(L"ABCDE");
2174   CheckCanUndo(true);
2175   CheckCanRedo(false);
2176 
2177   PerformUndo();
2178   CheckFocusedFieldText(L"ABCD");
2179   CheckCanUndo(true);
2180   CheckCanRedo(true);
2181   PerformUndo();
2182   CheckFocusedFieldText(L"ABC");
2183   CheckCanUndo(true);
2184   CheckCanRedo(true);
2185 
2186   PerformRedo();
2187   CheckFocusedFieldText(L"ABCD");
2188   CheckCanUndo(true);
2189   CheckCanRedo(true);
2190   PerformRedo();
2191   CheckFocusedFieldText(L"ABCDE");
2192   CheckCanUndo(true);
2193   CheckCanRedo(false);
2194 }
2195 
2196 // This action only applies to Listboxes and Comboboxes so should fail
2197 // gracefully for Textboxes by returning false to all operations.
TEST_F(FPDFFormFillTextFormEmbedderTest,SetIndexSelectedShouldFailGracefully)2198 TEST_F(FPDFFormFillTextFormEmbedderTest, SetIndexSelectedShouldFailGracefully) {
2199   // set focus and read text to confirm we have it
2200   ClickOnFormFieldAtPoint(CharLimitFormEnd());
2201   CheckFocusedFieldText(L"Elephant");
2202 
2203   SetIndexSelectedShouldFail(0, true);
2204   SetIndexSelectedShouldFail(0, false);
2205   SetIndexSelectedShouldFail(1, true);
2206   SetIndexSelectedShouldFail(1, false);
2207   SetIndexSelectedShouldFail(-1, true);
2208   SetIndexSelectedShouldFail(-1, false);
2209 }
2210 
2211 // This action only applies to Listboxes and Comboboxes so should fail
2212 // gracefully for Textboxes by returning false to all operations.
TEST_F(FPDFFormFillTextFormEmbedderTest,IsIndexSelectedShouldFailGracefully)2213 TEST_F(FPDFFormFillTextFormEmbedderTest, IsIndexSelectedShouldFailGracefully) {
2214   // set focus and read text to confirm we have it
2215   ClickOnFormFieldAtPoint(CharLimitFormEnd());
2216   CheckFocusedFieldText(L"Elephant");
2217 
2218   CheckIsIndexSelected(0, false);
2219   CheckIsIndexSelected(1, false);
2220   CheckIsIndexSelected(-1, false);
2221 }
2222 
TEST_F(FPDFFormFillComboBoxFormEmbedderTest,UndoRedo)2223 TEST_F(FPDFFormFillComboBoxFormEmbedderTest, UndoRedo) {
2224   ClickOnFormFieldAtPoint(NonEditableFormBegin());
2225   CheckFocusedFieldText(L"Banana");
2226   CheckCanUndo(false);
2227   CheckCanRedo(false);
2228 
2229   ClickOnFormFieldAtPoint(EditableFormBegin());
2230   CheckFocusedFieldText(L"");
2231   CheckCanUndo(false);
2232   CheckCanRedo(false);
2233 
2234   TypeTextIntoTextField(3, EditableFormBegin());
2235   CheckFocusedFieldText(L"ABC");
2236   CheckCanUndo(true);
2237   CheckCanRedo(false);
2238 
2239   PerformUndo();
2240   CheckFocusedFieldText(L"AB");
2241   CheckCanUndo(true);
2242   CheckCanRedo(true);
2243   PerformUndo();
2244   CheckFocusedFieldText(L"A");
2245   CheckCanUndo(true);
2246   CheckCanRedo(true);
2247   PerformUndo();
2248   CheckFocusedFieldText(L"");
2249   CheckCanUndo(false);
2250   CheckCanRedo(true);
2251 
2252   PerformRedo();
2253   CheckFocusedFieldText(L"A");
2254   CheckCanUndo(true);
2255   CheckCanRedo(true);
2256 }
2257 
TEST_F(FPDFFormFillListBoxFormEmbedderTest,CheckIfIndexSelectedSingleSelectField)2258 TEST_F(FPDFFormFillListBoxFormEmbedderTest,
2259        CheckIfIndexSelectedSingleSelectField) {
2260   // Nothing is selected in single select field upon opening.
2261   FocusOnSingleSelectForm();
2262   CheckIsIndexSelected(0, false);
2263   CheckIsIndexSelected(1, false);
2264   CheckIsIndexSelected(2, false);
2265 
2266   ClickOnSingleSelectFormOption(1);
2267   CheckIsIndexSelected(0, false);
2268   CheckIsIndexSelected(1, true);
2269   CheckIsIndexSelected(2, false);
2270 }
2271 
TEST_F(FPDFFormFillListBoxFormEmbedderTest,CheckIfIndexSelectedMultiSelectField)2272 TEST_F(FPDFFormFillListBoxFormEmbedderTest,
2273        CheckIfIndexSelectedMultiSelectField) {
2274   // Multiselect field set to 'Banana' (index 1) upon opening.
2275   FocusOnMultiSelectForm();
2276   for (int i = 0; i < 26; i++) {
2277     bool expected = i == 1;
2278     CheckIsIndexSelected(i, expected);
2279   }
2280 
2281   // TODO(bug_1377): Behavior should be changed to the one described below.
2282   // Multiselect field set to 'Cherry' (index 2), which is index 1 among the
2283   // visible form options because the listbox is scrolled down to have 'Banana'
2284   // (index 1) at the top.
2285   ClickOnMultiSelectFormOption(1);
2286   for (int i = 0; i < 26; i++) {
2287     bool expected = i == 1;
2288     CheckIsIndexSelected(i, expected);
2289   }
2290 }
2291 
TEST_F(FPDFFormFillListBoxFormEmbedderTest,SetSelectionProgrammaticallySingleSelectField)2292 TEST_F(FPDFFormFillListBoxFormEmbedderTest,
2293        SetSelectionProgrammaticallySingleSelectField) {
2294   // Nothing is selected in single select field upon opening.
2295   FocusOnSingleSelectForm();
2296   CheckFocusedFieldText(L"");
2297   CheckIsIndexSelected(0, false);
2298   CheckIsIndexSelected(1, false);
2299   CheckIsIndexSelected(2, false);
2300 
2301   // Make selections to change the value of the focused annotation
2302   // programmatically showing that only one value remains selected at a time.
2303   SetIndexSelectedShouldSucceed(0, true);
2304   CheckFocusedFieldText(L"Foo");
2305   CheckIsIndexSelected(0, true);
2306   CheckIsIndexSelected(1, false);
2307   CheckIsIndexSelected(2, false);
2308 
2309   SetIndexSelectedShouldSucceed(1, true);
2310   CheckFocusedFieldText(L"Bar");
2311   CheckIsIndexSelected(0, false);
2312   CheckIsIndexSelected(1, true);
2313   CheckIsIndexSelected(2, false);
2314 
2315   // Selecting/deselecting an index that is already selected/deselected is
2316   // success.
2317   SetIndexSelectedShouldSucceed(1, true);
2318   CheckFocusedFieldText(L"Bar");
2319   CheckIsIndexSelected(0, false);
2320   CheckIsIndexSelected(1, true);
2321   CheckIsIndexSelected(2, false);
2322 
2323   SetIndexSelectedShouldSucceed(2, false);
2324   CheckFocusedFieldText(L"Bar");
2325   CheckIsIndexSelected(0, false);
2326   CheckIsIndexSelected(1, true);
2327   CheckIsIndexSelected(2, false);
2328 
2329   // Cannot select indices that are out of range.
2330   SetIndexSelectedShouldFail(100, true);
2331   SetIndexSelectedShouldFail(-100, true);
2332   SetIndexSelectedShouldFail(100, false);
2333   SetIndexSelectedShouldFail(-100, false);
2334   // Confirm that previous values were not changed.
2335   CheckFocusedFieldText(L"Bar");
2336   CheckIsIndexSelected(0, false);
2337   CheckIsIndexSelected(1, true);
2338   CheckIsIndexSelected(2, false);
2339 
2340   // Unlike combobox, should be able to deselect all indices.
2341   SetIndexSelectedShouldSucceed(1, false);
2342   CheckFocusedFieldText(L"");
2343   CheckIsIndexSelected(0, false);
2344   CheckIsIndexSelected(1, false);
2345   CheckIsIndexSelected(2, false);
2346 
2347   // Check that above actions are interchangeable with click actions, should be
2348   // able to use a combination of both.
2349   ClickOnSingleSelectFormOption(1);
2350   CheckFocusedFieldText(L"Bar");
2351   CheckIsIndexSelected(0, false);
2352   CheckIsIndexSelected(1, true);
2353   CheckIsIndexSelected(2, false);
2354 }
2355 
2356 // Re: Focus Field Text - For multiselect listboxes a caret is set on the last
2357 // item to be selected/deselected. The text of that item should be returned.
TEST_F(FPDFFormFillListBoxFormEmbedderTest,SetSelectionProgrammaticallyMultiSelectField)2358 TEST_F(FPDFFormFillListBoxFormEmbedderTest,
2359        SetSelectionProgrammaticallyMultiSelectField) {
2360   // Multiselect field set to 'Banana' (index 1) upon opening.
2361   FocusOnMultiSelectForm();
2362   for (int i = 0; i < 26; i++) {
2363     bool expected = i == 1;
2364     CheckIsIndexSelected(i, expected);
2365   }
2366   CheckFocusedFieldText(L"Banana");
2367 
2368   // Select some more options.
2369   SetIndexSelectedShouldSucceed(5, true);
2370   SetIndexSelectedShouldSucceed(6, true);
2371   SetIndexSelectedShouldSucceed(20, true);
2372   for (int i = 0; i < 26; i++) {
2373     bool expected = (i == 1 || i == 5 || i == 6 || i == 20);
2374     CheckIsIndexSelected(i, expected);
2375   }
2376   CheckFocusedFieldText(L"Ugli Fruit");
2377 
2378   // Selecting indices that are already selected is success - changes nothing.
2379   SetIndexSelectedShouldSucceed(5, true);
2380   SetIndexSelectedShouldSucceed(6, true);
2381   SetIndexSelectedShouldSucceed(20, true);
2382   for (int i = 0; i < 26; i++) {
2383     bool expected = (i == 1 || i == 5 || i == 6 || i == 20);
2384     CheckIsIndexSelected(i, expected);
2385   }
2386   CheckFocusedFieldText(L"Ugli Fruit");
2387 
2388   // Deselect some options.
2389   SetIndexSelectedShouldSucceed(20, false);
2390   SetIndexSelectedShouldSucceed(1, false);
2391   for (int i = 0; i < 26; i++) {
2392     bool expected = (i == 5 || i == 6);
2393     CheckIsIndexSelected(i, expected);
2394   }
2395   CheckFocusedFieldText(L"Banana");
2396 
2397   // Deselecting indices that already aren't selected is success - does not
2398   // change the selected values but moves the focus text caret to last item we
2399   // executed on.
2400   SetIndexSelectedShouldSucceed(1, false);
2401   SetIndexSelectedShouldSucceed(3, false);
2402   for (int i = 0; i < 26; i++) {
2403     bool expected = (i == 5 || i == 6);
2404     CheckIsIndexSelected(i, expected);
2405   }
2406   CheckFocusedFieldText(L"Date");
2407 
2408   // Cannot select indices that are out of range.
2409   SetIndexSelectedShouldFail(100, true);
2410   SetIndexSelectedShouldFail(-100, true);
2411   SetIndexSelectedShouldFail(100, false);
2412   SetIndexSelectedShouldFail(-100, false);
2413   // Confirm that previous values were not changed.
2414   CheckFocusedFieldText(L"Date");
2415   for (int i = 0; i < 26; i++) {
2416     bool expected = (i == 5 || i == 6);
2417     CheckIsIndexSelected(i, expected);
2418   }
2419 
2420   // Check that above actions are interchangeable with click actions, should be
2421   // able to use a combination of both.
2422   // TODO(bug_1377): Change to click on form option 0 instead of form option 1
2423   ClickOnMultiSelectFormOption(1);
2424   for (int i = 0; i < 26; i++) {
2425     bool expected = i == 1;
2426     CheckIsIndexSelected(i, expected);
2427   }
2428   CheckFocusedFieldText(L"Banana");
2429 }
2430 
TEST_F(FPDFFormFillListBoxFormEmbedderTest,CheckIfMultipleSelected)2431 TEST_F(FPDFFormFillListBoxFormEmbedderTest, CheckIfMultipleSelected) {
2432   // Multiselect field set to 'Gamma' (index 2) and 'Epsilon' (index 4) upon
2433   // opening.
2434   FocusOnMultiSelectMultipleSelectedForm();
2435   for (int i = 0; i < 5; i++) {
2436     // TODO(bug_1377): Should be selected at index 2 and index 4.
2437     bool expected = false;
2438     CheckIsIndexSelected(i, expected);
2439   }
2440 }
2441 
TEST_F(FPDFFormFillListBoxFormEmbedderTest,CheckIfVerticalScrollIsAtFirstSelected)2442 TEST_F(FPDFFormFillListBoxFormEmbedderTest,
2443        CheckIfVerticalScrollIsAtFirstSelected) {
2444   // Multiselect field set to 'Gamma' (index 2) and 'Epsilon' (index 4) upon
2445   // opening.
2446 
2447   // TODO(bug_1377): Behavior should be changed to the one described below.
2448   // The top visible option is 'Gamma' (index 2), so the first selection should
2449   // not change. The second selection, 'Epsilon,' should be deselected.
2450   ClickOnMultiSelectMultipleSelectedFormOption(0);
2451   for (int i = 0; i < 5; i++) {
2452     bool expected = i == 0;
2453     CheckIsIndexSelected(i, expected);
2454   }
2455 }
2456 
TEST_F(FPDFFormFillListBoxFormEmbedderTest,CheckForNoOverscroll)2457 TEST_F(FPDFFormFillListBoxFormEmbedderTest, CheckForNoOverscroll) {
2458   // Only the last option in the list, 'Saskatchewan', is selected.
2459   FocusOnSingleSelectLastSelectedForm();
2460   for (int i = 0; i < 10; i++) {
2461     bool expected = i == 9;
2462     CheckIsIndexSelected(i, expected);
2463   }
2464 
2465   // Even though the top index is specified to be at 'Saskatchewan' (index 9),
2466   // the top visible option will be the one above it, 'Quebec' (index 8), to
2467   // prevent overscrolling. Therefore, clicking on the first visible option of
2468   // the list should select 'Quebec' instead of 'Saskatchewan.'
2469   ClickOnSingleSelectLastSelectedFormOption(0);
2470   for (int i = 0; i < 10; i++) {
2471     bool expected = i == 8;
2472     CheckIsIndexSelected(i, expected);
2473   }
2474 }
2475 
TEST_F(FPDFFormFillTextFormEmbedderTest,ReplaceSelection)2476 TEST_F(FPDFFormFillTextFormEmbedderTest, ReplaceSelection) {
2477   ScopedFPDFWideString text_to_insert = GetFPDFWideString(L"XYZ");
2478   ClickOnFormFieldAtPoint(RegularFormBegin());
2479   CheckCanUndo(false);
2480   CheckCanRedo(false);
2481 
2482   TypeTextIntoTextField(2, RegularFormBegin());
2483   CheckFocusedFieldText(L"AB");
2484   CheckSelection(L"");
2485   SelectTextWithKeyboard(1, FWL_VKEY_Right, RegularFormBegin());
2486   CheckSelection(L"A");
2487 
2488   FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get());
2489   CheckFocusedFieldText(L"XYZB");
2490   CheckCanUndo(true);
2491   CheckCanRedo(false);
2492 
2493   PerformUndo();
2494   CheckFocusedFieldText(L"AB");
2495   CheckCanUndo(true);
2496   CheckCanRedo(true);
2497 
2498   PerformUndo();
2499   CheckFocusedFieldText(L"A");
2500   CheckCanUndo(true);
2501   CheckCanRedo(true);
2502 
2503   PerformUndo();
2504   CheckFocusedFieldText(L"");
2505   CheckCanUndo(false);
2506   CheckCanRedo(true);
2507 
2508   PerformRedo();
2509   CheckFocusedFieldText(L"A");
2510   CheckCanUndo(true);
2511   CheckCanRedo(true);
2512 
2513   PerformRedo();
2514   CheckFocusedFieldText(L"AB");
2515   CheckCanUndo(true);
2516   CheckCanRedo(true);
2517 
2518   PerformRedo();
2519   CheckFocusedFieldText(L"XYZB");
2520   CheckCanUndo(true);
2521   CheckCanRedo(false);
2522 }
2523