From a4c7ac479d291fc3373b9c2f8f25302ecd53b0d5 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Tue, 17 Apr 2018 15:12:58 +0000 Subject: Implement FORM_GetFocusedText() API. If there is a focused form field, get its text. BUG=chromium:753216 Change-Id: I05294f14d05c1c86769055f6c9eaf9177787d9fd Reviewed-on: https://pdfium-review.googlesource.com/12072 Commit-Queue: Lei Zhang Reviewed-by: dsinclair --- fpdfsdk/fpdf_formfill_embeddertest.cpp | 192 +++++++++++++++++++++++++++++++-- 1 file changed, 181 insertions(+), 11 deletions(-) (limited to 'fpdfsdk/fpdf_formfill_embeddertest.cpp') diff --git a/fpdfsdk/fpdf_formfill_embeddertest.cpp b/fpdfsdk/fpdf_formfill_embeddertest.cpp index 99a51fd4a4..5dd4d1e1c1 100644 --- a/fpdfsdk/fpdf_formfill_embeddertest.cpp +++ b/fpdfsdk/fpdf_formfill_embeddertest.cpp @@ -107,20 +107,30 @@ class FPDFFormFillInteractiveEmbeddertest : public FPDFFormFillEmbeddertest { } void CheckSelection(const WideStringView& expected_string) { - // Calculate expected length for selected text. - int num_chars = expected_string.GetLength(); - - // Check actual selection against expected selection. - const unsigned long expected_length = - sizeof(unsigned short) * (num_chars + 1); - unsigned long sel_text_len = + unsigned long actual_len = FORM_GetSelectedText(form_handle(), page_, nullptr, 0); - ASSERT_EQ(expected_length, sel_text_len); + ASSERT_NE(actual_len, 0U); + ASSERT_LT(actual_len, 1000U); - std::vector buf(sel_text_len); - EXPECT_EQ(expected_length, FORM_GetSelectedText(form_handle(), page_, - buf.data(), sel_text_len)); + std::vector buf(actual_len); + ASSERT_EQ(actual_len, FORM_GetSelectedText(form_handle(), page_, buf.data(), + actual_len)); + int num_chars = (actual_len / sizeof(unsigned short)) - 1; + EXPECT_EQ(expected_string, WideString::FromUTF16LE(buf.data(), num_chars)); + } + + void CheckFocusedFieldText(const WideStringView& expected_string) { + unsigned long actual_len = + FORM_GetFocusedText(form_handle(), page_, nullptr, 0); + ASSERT_NE(actual_len, 0U); + ASSERT_LT(actual_len, 1000U); + + std::vector buf(actual_len); + ASSERT_EQ(actual_len, FORM_GetFocusedText(form_handle(), page_, buf.data(), + actual_len)); + + int num_chars = (actual_len / sizeof(unsigned short)) - 1; EXPECT_EQ(expected_string, WideString::FromUTF16LE(buf.data(), num_chars)); } @@ -589,26 +599,31 @@ TEST_F(FPDFFormFillEmbeddertest, HasFormInfoXFAForeground) { TEST_F(FPDFFormFillTextFormEmbeddertest, GetSelectedTextEmptyAndBasicKeyboard) { // Test empty selection. + CheckFocusedFieldText(L""); CheckSelection(L""); // Test basic selection. TypeTextIntoTextField(3, RegularFormBegin()); + CheckFocusedFieldText(L"ABC"); SelectTextWithKeyboard(3, FWL_VKEY_Left, RegularFormAtX(123.0)); CheckSelection(L"ABC"); } TEST_F(FPDFFormFillTextFormEmbeddertest, GetSelectedTextEmptyAndBasicMouse) { // Test empty selection. + CheckFocusedFieldText(L""); CheckSelection(L""); // Test basic selection. TypeTextIntoTextField(3, RegularFormBegin()); + CheckFocusedFieldText(L"ABC"); SelectTextWithMouse(RegularFormAtX(125.0), RegularFormBegin()); CheckSelection(L"ABC"); } TEST_F(FPDFFormFillTextFormEmbeddertest, GetSelectedTextFragmentsKeyBoard) { TypeTextIntoTextField(12, RegularFormBegin()); + CheckFocusedFieldText(L"ABCDEFGHIJKL"); // Test selecting first character in forward direction. SelectTextWithKeyboard(1, FWL_VKEY_Right, RegularFormBegin()); @@ -629,6 +644,7 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, GetSelectedTextFragmentsKeyBoard) { // Test selecting last character in backwards direction. SelectTextWithKeyboard(1, FWL_VKEY_Left, RegularFormEnd()); CheckSelection(L"L"); + CheckFocusedFieldText(L"ABCDEFGHIJKL"); } TEST_F(FPDFFormFillTextFormEmbeddertest, GetSelectedTextFragmentsMouse) { @@ -659,13 +675,16 @@ TEST_F(FPDFFormFillComboBoxFormEmbeddertest, GetSelectedTextEmptyAndBasicNormalComboBox) { // Test empty selection. CheckSelection(L""); + CheckFocusedFieldText(L""); // Non-editable comboboxes don't allow selection with keyboard. SelectTextWithMouse(NonEditableFormBegin(), NonEditableFormAtX(142.0)); + CheckFocusedFieldText(L"Banana"); CheckSelection(L"Banana"); // Select other another provided option. SelectNonEditableFormOption(0); + CheckFocusedFieldText(L"Apple"); CheckSelection(L"Apple"); } @@ -673,15 +692,18 @@ TEST_F(FPDFFormFillComboBoxFormEmbeddertest, GetSelectedTextEmptyAndBasicEditableComboBoxKeyboard) { // Test empty selection. CheckSelection(L""); + CheckFocusedFieldText(L""); // Test basic selection of text within user editable combobox using keyboard. TypeTextIntoTextField(3, EditableFormBegin()); + CheckFocusedFieldText(L"ABC"); SelectTextWithKeyboard(3, FWL_VKEY_Left, EditableFormAtX(128.0)); CheckSelection(L"ABC"); // Select a provided option. SelectEditableFormOption(1); CheckSelection(L"Bar"); + CheckFocusedFieldText(L"Bar"); } TEST_F(FPDFFormFillComboBoxFormEmbeddertest, @@ -696,13 +718,17 @@ TEST_F(FPDFFormFillComboBoxFormEmbeddertest, // Select a provided option. SelectEditableFormOption(2); + CheckFocusedFieldText(L"Qux"); CheckSelection(L"Qux"); } TEST_F(FPDFFormFillComboBoxFormEmbeddertest, GetSelectedTextFragmentsNormalComboBox) { + CheckFocusedFieldText(L""); + // Test selecting first character in forward direction. SelectTextWithMouse(NonEditableFormBegin(), NonEditableFormAtX(107.0)); + CheckFocusedFieldText(L"Banana"); CheckSelection(L"B"); // Test selecting entire string in backwards direction. @@ -720,9 +746,11 @@ TEST_F(FPDFFormFillComboBoxFormEmbeddertest, // Test selecting last character in backwards direction. SelectTextWithMouse(NonEditableFormAtX(142.0), NonEditableFormAtX(138.0)); CheckSelection(L"a"); + CheckFocusedFieldText(L"Banana"); // Select another option and then reset selection as first three chars. SelectNonEditableFormOption(2); + CheckFocusedFieldText(L"Cherry"); CheckSelection(L"Cherry"); SelectTextWithMouse(NonEditableFormBegin(), NonEditableFormAtX(122.0)); CheckSelection(L"Che"); @@ -730,7 +758,9 @@ TEST_F(FPDFFormFillComboBoxFormEmbeddertest, TEST_F(FPDFFormFillComboBoxFormEmbeddertest, GetSelectedTextFragmentsEditableComboBoxKeyboard) { + CheckFocusedFieldText(L""); TypeTextIntoTextField(10, EditableFormBegin()); + CheckFocusedFieldText(L"ABCDEFGHIJ"); // Test selecting first character in forward direction. SelectTextWithKeyboard(1, FWL_VKEY_Right, EditableFormBegin()); @@ -757,6 +787,7 @@ TEST_F(FPDFFormFillComboBoxFormEmbeddertest, CheckSelection(L"Foo"); SelectTextWithKeyboard(2, FWL_VKEY_Right, EditableFormBegin()); CheckSelection(L"Fo"); + CheckFocusedFieldText(L"Foo"); } TEST_F(FPDFFormFillComboBoxFormEmbeddertest, @@ -782,17 +813,20 @@ TEST_F(FPDFFormFillComboBoxFormEmbeddertest, // Test selecting last character in backwards direction. SelectTextWithMouse(EditableFormEnd(), EditableFormAtX(174.0)); CheckSelection(L"J"); + CheckFocusedFieldText(L"ABCDEFGHIJ"); } TEST_F(FPDFFormFillTextFormEmbeddertest, DeleteTextFieldEntireSelection) { // Select entire contents of text field. TypeTextIntoTextField(12, RegularFormBegin()); SelectAllRegularFormTextWithMouse(); + CheckFocusedFieldText(L"ABCDEFGHIJKL"); CheckSelection(L"ABCDEFGHIJKL"); // Test deleting current text selection. Select what remains after deletion to // check that remaining text is as expected. FORM_ReplaceSelection(form_handle(), page(), nullptr); + CheckFocusedFieldText(L""); SelectTextWithKeyboard(12, FWL_VKEY_Left, RegularFormEnd()); CheckSelection(L""); @@ -802,11 +836,13 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, DeleteTextFieldSelectionMiddle) { // Select middle section of text. TypeTextIntoTextField(12, RegularFormBegin()); SelectTextWithMouse(RegularFormAtX(170.0), RegularFormAtX(125.0)); + CheckFocusedFieldText(L"ABCDEFGHIJKL"); CheckSelection(L"DEFGHI"); // Test deleting current text selection. Select what remains after deletion to // check that remaining text is as expected. FORM_ReplaceSelection(form_handle(), page(), nullptr); + CheckFocusedFieldText(L"ABCJKL"); SelectTextWithKeyboard(12, FWL_VKEY_Left, RegularFormEnd()); CheckSelection(L"ABCJKL"); } @@ -820,6 +856,7 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, DeleteTextFieldSelectionLeft) { // Test deleting current text selection. Select what remains after deletion to // check that remaining text is as expected. FORM_ReplaceSelection(form_handle(), page(), nullptr); + CheckFocusedFieldText(L"EFGHIJKL"); SelectTextWithKeyboard(12, FWL_VKEY_Left, RegularFormEnd()); CheckSelection(L"EFGHIJKL"); } @@ -833,6 +870,7 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, DeleteTextFieldSelectionRight) { // Test deleting current text selection. Select what remains after deletion to // check that remaining text is as expected. FORM_ReplaceSelection(form_handle(), page(), nullptr); + CheckFocusedFieldText(L"ABCDEFGH"); SelectTextWithKeyboard(12, FWL_VKEY_Left, RegularFormEnd()); CheckSelection(L"ABCDEFGH"); } @@ -844,6 +882,7 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, DeleteEmptyTextFieldSelection) { // Test that attempt to delete empty text selection has no effect. FORM_ReplaceSelection(form_handle(), page(), nullptr); + CheckFocusedFieldText(L"ABCDEFGHIJKL"); SelectTextWithKeyboard(12, FWL_VKEY_Left, RegularFormEnd()); CheckSelection(L"ABCDEFGHIJKL"); } @@ -858,6 +897,7 @@ TEST_F(FPDFFormFillComboBoxFormEmbeddertest, // Test deleting current text selection. Select what remains after deletion to // check that remaining text is as expected. FORM_ReplaceSelection(form_handle(), page(), nullptr); + CheckFocusedFieldText(L""); SelectAllEditableFormTextWithMouse(); CheckSelection(L""); } @@ -872,6 +912,7 @@ TEST_F(FPDFFormFillComboBoxFormEmbeddertest, // Test deleting current text selection. Select what remains after deletion to // check that remaining text is as expected. FORM_ReplaceSelection(form_handle(), page(), nullptr); + CheckFocusedFieldText(L"ABCIJ"); SelectAllEditableFormTextWithMouse(); CheckSelection(L"ABCIJ"); } @@ -917,12 +958,15 @@ TEST_F(FPDFFormFillComboBoxFormEmbeddertest, } TEST_F(FPDFFormFillTextFormEmbeddertest, InsertTextInEmptyTextField) { + CheckFocusedFieldText(L""); ClickOnFormFieldAtPoint(RegularFormBegin()); + CheckFocusedFieldText(L""); // Test inserting text into empty text field. std::unique_ptr text_to_insert = GetFPDFWideString(L"Hello"); FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get()); + CheckFocusedFieldText(L"Hello"); // Select entire contents of text field to check that insertion worked // as expected. @@ -932,14 +976,17 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, InsertTextInEmptyTextField) { TEST_F(FPDFFormFillTextFormEmbeddertest, InsertTextInPopulatedTextFieldLeft) { TypeTextIntoTextField(8, RegularFormBegin()); + CheckFocusedFieldText(L"ABCDEFGH"); // Click on the leftmost part of the text field. ClickOnFormFieldAtPoint(RegularFormBegin()); + CheckFocusedFieldText(L"ABCDEFGH"); // Test inserting text in front of existing text in text field. std::unique_ptr text_to_insert = GetFPDFWideString(L"Hello"); FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get()); + CheckFocusedFieldText(L"HelloABCDEFGH"); // Select entire contents of text field to check that insertion worked // as expected. @@ -957,6 +1004,7 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, InsertTextInPopulatedTextFieldMiddle) { std::unique_ptr text_to_insert = GetFPDFWideString(L"Hello"); FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get()); + CheckFocusedFieldText(L"ABCDHelloEFGH"); // Select entire contents of text field to check that insertion worked // as expected. @@ -974,6 +1022,7 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, InsertTextInPopulatedTextFieldRight) { std::unique_ptr text_to_insert = GetFPDFWideString(L"Hello"); FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get()); + CheckFocusedFieldText(L"ABCDEFGHHello"); // Select entire contents of text field to check that insertion worked // as expected. @@ -993,6 +1042,7 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, std::unique_ptr text_to_insert = GetFPDFWideString(L"Hello"); FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get()); + CheckFocusedFieldText(L"Hello"); // Select entire contents of text field to check that insertion worked // as expected. @@ -1012,6 +1062,7 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, std::unique_ptr text_to_insert = GetFPDFWideString(L"Hello"); FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get()); + CheckFocusedFieldText(L"HelloGHIJKL"); // Select entire contents of text field to check that insertion worked // as expected. @@ -1060,11 +1111,13 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, TEST_F(FPDFFormFillComboBoxFormEmbeddertest, InsertTextInEmptyEditableComboBox) { ClickOnFormFieldAtPoint(EditableFormBegin()); + CheckFocusedFieldText(L""); // Test inserting text into empty user-editable combobox. std::unique_ptr text_to_insert = GetFPDFWideString(L"Hello"); FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get()); + CheckFocusedFieldText(L"Hello"); // Select entire contents of user-editable combobox text field to check that // insertion worked as expected. @@ -1206,18 +1259,22 @@ TEST_F(FPDFFormFillComboBoxFormEmbeddertest, TEST_F(FPDFFormFillTextFormEmbeddertest, InsertTextInEmptyCharLimitTextFieldOverflow) { // Click on the textfield. + CheckFocusedFieldText(L""); ClickOnFormFieldAtPoint(CharLimitFormEnd()); + CheckFocusedFieldText(L"Elephant"); // Delete pre-filled contents of text field with char limit. SelectAllCharLimitFormTextWithMouse(); CheckSelection(L"Elephant"); FORM_ReplaceSelection(form_handle(), page(), nullptr); + CheckFocusedFieldText(L""); // Test inserting text into now empty text field so text to be inserted // exceeds the char limit and is cut off. std::unique_ptr text_to_insert = GetFPDFWideString(L"Hippopotamus"); FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get()); + CheckFocusedFieldText(L"Hippopotam"); // Select entire contents of text field to check that insertion worked // as expected. @@ -1229,6 +1286,7 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, InsertTextInEmptyCharLimitTextFieldFit) { // Click on the textfield. ClickOnFormFieldAtPoint(CharLimitFormEnd()); + CheckFocusedFieldText(L"Elephant"); // Delete pre-filled contents of text field with char limit. SelectAllCharLimitFormTextWithMouse(); @@ -1240,6 +1298,7 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, std::unique_ptr text_to_insert = GetFPDFWideString(L"Zebra"); FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get()); + CheckFocusedFieldText(L"Zebra"); // Select entire contents of text field to check that insertion worked // as expected. @@ -1265,15 +1324,19 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, TEST_F(FPDFFormFillTextFormEmbeddertest, InsertTextInPopulatedCharLimitTextFieldMiddle) { + CheckFocusedFieldText(L""); TypeTextIntoTextField(8, RegularFormBegin()); + CheckFocusedFieldText(L"ABCDEFGH"); // Click on the middle of the text field. ClickOnFormFieldAtPoint(CharLimitFormAtX(134.0)); + CheckFocusedFieldText(L"Elephant"); // Test inserting text in the middle of existing text in text field. std::unique_ptr text_to_insert = GetFPDFWideString(L"Hippopotamus"); FORM_ReplaceSelection(form_handle(), page(), text_to_insert.get()); + CheckFocusedFieldText(L"ElephHiant"); // Select entire contents of text field to check that insertion worked // as expected. @@ -1374,3 +1437,110 @@ TEST_F(FPDFFormFillTextFormEmbeddertest, SelectAllCharLimitFormTextWithMouse(); CheckSelection(L"ElepHippop"); } + +TEST_F(FPDFFormFillTextFormEmbeddertest, FocusChanges) { + static const CFX_PointF kNonFormPoint(1, 1); + CheckFocusedFieldText(L""); + ClickOnFormFieldAtPoint(CharLimitFormEnd()); + CheckFocusedFieldText(L"Elephant"); + ClickOnFormFieldAtPoint(RegularFormBegin()); + CheckFocusedFieldText(L""); + TypeTextIntoTextField(3, CharLimitFormBegin()); + CheckFocusedFieldText(L"ABElephant"); + TypeTextIntoTextField(5, RegularFormBegin()); + CheckFocusedFieldText(L"ABCDE"); + ClickOnFormFieldAtPoint(CharLimitFormEnd()); + CheckFocusedFieldText(L"ABElephant"); + ClickOnFormFieldAtPoint(kNonFormPoint); + CheckFocusedFieldText(L""); + ClickOnFormFieldAtPoint(kNonFormPoint); + CheckFocusedFieldText(L""); + ClickOnFormFieldAtPoint(CharLimitFormBegin()); + CheckFocusedFieldText(L"ABElephant"); + ClickOnFormFieldAtPoint(CharLimitFormEnd()); + CheckFocusedFieldText(L"ABElephant"); + ClickOnFormFieldAtPoint(RegularFormEnd()); + CheckFocusedFieldText(L"ABCDE"); + ClickOnFormFieldAtPoint(RegularFormBegin()); + CheckFocusedFieldText(L"ABCDE"); + ClickOnFormFieldAtPoint(RegularFormBegin()); + CheckFocusedFieldText(L"ABCDE"); + ClickOnFormFieldAtPoint(CharLimitFormBegin()); + CheckFocusedFieldText(L"ABElephant"); + FORM_ForceToKillFocus(form_handle()); + CheckFocusedFieldText(L""); +} + +TEST_F(FPDFFormFillComboBoxFormEmbeddertest, FocusChanges) { + static const CFX_PointF kNonFormPoint(1, 1); + CheckFocusedFieldText(L""); + ClickOnFormFieldAtPoint(NonEditableFormBegin()); + CheckFocusedFieldText(L"Banana"); + ClickOnFormFieldAtPoint(EditableFormBegin()); + CheckFocusedFieldText(L""); + ClickOnFormFieldAtPoint(NonEditableFormEnd()); + CheckFocusedFieldText(L"Banana"); + ClickOnFormFieldAtPoint(NonEditableFormBegin()); + CheckFocusedFieldText(L"Banana"); + FORM_ForceToKillFocus(form_handle()); + CheckFocusedFieldText(L""); + ClickOnFormFieldAtPoint(EditableFormBegin()); + CheckFocusedFieldText(L""); + TypeTextIntoTextField(3, EditableFormBegin()); + CheckFocusedFieldText(L"ABC"); + ClickOnFormFieldAtPoint(kNonFormPoint); + CheckFocusedFieldText(L""); + TypeTextIntoTextField(3, EditableFormEnd()); + CheckFocusedFieldText(L"ABCABC"); + ClickOnFormFieldAtPoint(kNonFormPoint); + CheckFocusedFieldText(L""); + ClickOnFormFieldAtPoint(EditableFormDropDown()); + CheckFocusedFieldText(L"ABCABC"); + FORM_ForceToKillFocus(form_handle()); + CheckFocusedFieldText(L""); + ClickOnFormFieldAtPoint(NonEditableFormDropDown()); + CheckFocusedFieldText(L"Banana"); + ClickOnFormFieldAtPoint(kNonFormPoint); + CheckFocusedFieldText(L""); + ClickOnFormFieldAtPoint(NonEditableFormEnd()); + CheckFocusedFieldText(L"Banana"); + + // Typing into non-editable field results in selecting a different option. + TypeTextIntoTextField(1, NonEditableFormEnd()); + CheckFocusedFieldText(L"Apple"); + TypeTextIntoTextField(3, NonEditableFormEnd()); + CheckFocusedFieldText(L"Cherry"); + TypeTextIntoTextField(2, NonEditableFormEnd()); + CheckFocusedFieldText(L"Banana"); + + SelectEditableFormOption(0); + CheckFocusedFieldText(L"Foo"); + SelectEditableFormOption(1); + CheckFocusedFieldText(L"Bar"); + SelectEditableFormOption(2); + CheckFocusedFieldText(L"Qux"); + SelectNonEditableFormOption(1); + CheckFocusedFieldText(L"Banana"); + SelectNonEditableFormOption(0); + CheckFocusedFieldText(L"Apple"); + SelectNonEditableFormOption(2); + CheckFocusedFieldText(L"Cherry"); + + // Typing into an editable field changes the text in the option. + SelectEditableFormOption(0); + CheckFocusedFieldText(L"Foo"); + TypeTextIntoTextField(5, EditableFormBegin()); + CheckFocusedFieldText(L"ABCDEFoo"); + SelectEditableFormOption(2); + CheckFocusedFieldText(L"Qux"); + TypeTextIntoTextField(2, EditableFormEnd()); + CheckFocusedFieldText(L"QuxAB"); + + // But a previously edited option is reset when selected again. + SelectEditableFormOption(0); + CheckFocusedFieldText(L"Foo"); + TypeTextIntoTextField(1, EditableFormBegin()); + CheckFocusedFieldText(L"AFoo"); + SelectEditableFormOption(0); + CheckFocusedFieldText(L"Foo"); +} -- cgit v1.2.3