Lei Zhang | af680b1 | 2017-06-02 16:42:59 -0700 | [diff] [blame] | 1 | // Copyright 2017 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 "fpdfsdk/cba_annotiterator.h" |
| 6 | #include "fpdfsdk/cpdfsdk_annot.h" |
| 7 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
| 8 | #include "fpdfsdk/formfiller/cffl_formfiller.h" |
| 9 | #include "fpdfsdk/formfiller/cffl_interactiveformfiller.h" |
Dan Sinclair | c411eb9 | 2017-07-25 09:39:30 -0400 | [diff] [blame^] | 10 | #include "fpdfsdk/pwl/cpwl_wnd.h" |
Lei Zhang | af680b1 | 2017-06-02 16:42:59 -0700 | [diff] [blame] | 11 | #include "testing/embedder_test.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 14 | class CPWLEditEmbeddertest : public EmbedderTest { |
| 15 | protected: |
| 16 | void SetUp() override { |
| 17 | EmbedderTest::SetUp(); |
| 18 | CreateAndInitializeFormPDF(); |
| 19 | } |
| 20 | |
| 21 | void TearDown() override { |
| 22 | UnloadPage(GetPage()); |
| 23 | EmbedderTest::TearDown(); |
| 24 | } |
| 25 | |
| 26 | void CreateAndInitializeFormPDF() { |
| 27 | EXPECT_TRUE(OpenDocument("text_form.pdf")); |
| 28 | m_page = LoadPage(0); |
| 29 | ASSERT_TRUE(m_page); |
| 30 | |
| 31 | CPDFSDK_FormFillEnvironment* pFormFillEnv = |
| 32 | static_cast<CPDFSDK_FormFillEnvironment*>(form_handle()); |
| 33 | |
| 34 | { |
| 35 | CBA_AnnotIterator iter(pFormFillEnv->GetPageView(0), |
| 36 | CPDF_Annot::Subtype::WIDGET); |
| 37 | m_pAnnot = iter.GetFirstAnnot(); |
| 38 | CPDFSDK_Annot* pLastAnnot = iter.GetLastAnnot(); |
| 39 | ASSERT_EQ(m_pAnnot, pLastAnnot); |
| 40 | ASSERT_TRUE(m_pAnnot); |
| 41 | ASSERT_EQ(CPDF_Annot::Subtype::WIDGET, m_pAnnot->GetAnnotSubtype()); |
| 42 | } |
| 43 | |
| 44 | CFFL_InteractiveFormFiller* pInteractiveFormFiller = |
| 45 | pFormFillEnv->GetInteractiveFormFiller(); |
| 46 | { |
| 47 | CPDFSDK_Annot::ObservedPtr pObserved(m_pAnnot); |
| 48 | EXPECT_TRUE(pInteractiveFormFiller->OnSetFocus(&pObserved, 0)); |
| 49 | } |
| 50 | |
| 51 | m_pFormFiller = pInteractiveFormFiller->GetFormFiller(m_pAnnot, false); |
| 52 | ASSERT_TRUE(m_pFormFiller); |
| 53 | |
| 54 | CPWL_Wnd* pWindow = |
| 55 | m_pFormFiller->GetPDFWindow(pFormFillEnv->GetPageView(0), false); |
| 56 | ASSERT_TRUE(pWindow); |
| 57 | ASSERT_EQ(PWL_CLASSNAME_EDIT, pWindow->GetClassName()); |
| 58 | |
| 59 | m_pEdit = static_cast<CPWL_Edit*>(pWindow); |
| 60 | } |
| 61 | |
| 62 | FPDF_PAGE GetPage() { return m_page; } |
| 63 | CPWL_Edit* GetCPWLEdit() { return m_pEdit; } |
| 64 | CFFL_FormFiller* GetCFFLFormFiller() { return m_pFormFiller; } |
| 65 | CPDFSDK_Annot* GetCPDFSDKAnnot() { return m_pAnnot; } |
| 66 | |
| 67 | private: |
| 68 | FPDF_PAGE m_page; |
| 69 | CPWL_Edit* m_pEdit; |
| 70 | CFFL_FormFiller* m_pFormFiller; |
| 71 | CPDFSDK_Annot* m_pAnnot; |
| 72 | }; |
Lei Zhang | af680b1 | 2017-06-02 16:42:59 -0700 | [diff] [blame] | 73 | |
| 74 | TEST_F(CPWLEditEmbeddertest, TypeText) { |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 75 | EXPECT_TRUE(GetCPWLEdit()->GetText().IsEmpty()); |
| 76 | EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'a', 0)); |
| 77 | EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'b', 0)); |
| 78 | EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'c', 0)); |
Lei Zhang | af680b1 | 2017-06-02 16:42:59 -0700 | [diff] [blame] | 79 | |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 80 | EXPECT_STREQ(L"abc", GetCPWLEdit()->GetText().c_str()); |
| 81 | } |
Lei Zhang | af680b1 | 2017-06-02 16:42:59 -0700 | [diff] [blame] | 82 | |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 83 | TEST_F(CPWLEditEmbeddertest, GetSelectedTextEmptyAndBasic) { |
| 84 | // Attempt to set selection before text has been typed to test that |
| 85 | // selection is identified as empty. |
| 86 | // |
| 87 | // Select from character index [0, 3) within form text field. |
Diana Gage | 4d02e90 | 2017-07-20 17:20:31 -0700 | [diff] [blame] | 88 | GetCPWLEdit()->SetSelection(0, 3); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 89 | EXPECT_TRUE(GetCPWLEdit()->GetSelectedText().IsEmpty()); |
| 90 | |
| 91 | EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'a', 0)); |
| 92 | EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'b', 0)); |
| 93 | EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'c', 0)); |
Diana Gage | 4d02e90 | 2017-07-20 17:20:31 -0700 | [diff] [blame] | 94 | GetCPWLEdit()->SetSelection(0, 2); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 95 | |
| 96 | EXPECT_STREQ(L"ab", GetCPWLEdit()->GetSelectedText().c_str()); |
| 97 | } |
| 98 | |
| 99 | TEST_F(CPWLEditEmbeddertest, GetSelectedTextFragments) { |
| 100 | for (int i = 0; i < 50; ++i) { |
| 101 | EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), i + 'A', 0)); |
Lei Zhang | af680b1 | 2017-06-02 16:42:59 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Diana Gage | 4d02e90 | 2017-07-20 17:20:31 -0700 | [diff] [blame] | 104 | GetCPWLEdit()->SetSelection(0, 0); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 105 | EXPECT_TRUE(GetCPWLEdit()->GetSelectedText().IsEmpty()); |
Lei Zhang | af680b1 | 2017-06-02 16:42:59 -0700 | [diff] [blame] | 106 | |
Diana Gage | 4d02e90 | 2017-07-20 17:20:31 -0700 | [diff] [blame] | 107 | GetCPWLEdit()->SetSelection(0, 1); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 108 | EXPECT_STREQ(L"A", GetCPWLEdit()->GetSelectedText().c_str()); |
Lei Zhang | af680b1 | 2017-06-02 16:42:59 -0700 | [diff] [blame] | 109 | |
Diana Gage | 4d02e90 | 2017-07-20 17:20:31 -0700 | [diff] [blame] | 110 | GetCPWLEdit()->SetSelection(0, -1); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 111 | EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr", |
| 112 | GetCPWLEdit()->GetSelectedText().c_str()); |
Lei Zhang | af680b1 | 2017-06-02 16:42:59 -0700 | [diff] [blame] | 113 | |
Diana Gage | 4d02e90 | 2017-07-20 17:20:31 -0700 | [diff] [blame] | 114 | GetCPWLEdit()->SetSelection(-8, -1); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 115 | EXPECT_TRUE(GetCPWLEdit()->GetSelectedText().IsEmpty()); |
Lei Zhang | af680b1 | 2017-06-02 16:42:59 -0700 | [diff] [blame] | 116 | |
Diana Gage | 4d02e90 | 2017-07-20 17:20:31 -0700 | [diff] [blame] | 117 | GetCPWLEdit()->SetSelection(23, 12); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 118 | EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLEdit()->GetSelectedText().c_str()); |
Lei Zhang | af680b1 | 2017-06-02 16:42:59 -0700 | [diff] [blame] | 119 | |
Diana Gage | 4d02e90 | 2017-07-20 17:20:31 -0700 | [diff] [blame] | 120 | GetCPWLEdit()->SetSelection(12, 23); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 121 | EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLEdit()->GetSelectedText().c_str()); |
Lei Zhang | af680b1 | 2017-06-02 16:42:59 -0700 | [diff] [blame] | 122 | |
Diana Gage | 4d02e90 | 2017-07-20 17:20:31 -0700 | [diff] [blame] | 123 | GetCPWLEdit()->SetSelection(49, 50); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 124 | EXPECT_STREQ(L"r", GetCPWLEdit()->GetSelectedText().c_str()); |
Lei Zhang | af680b1 | 2017-06-02 16:42:59 -0700 | [diff] [blame] | 125 | } |
Diana Gage | 1c7f142 | 2017-07-24 11:19:52 -0700 | [diff] [blame] | 126 | |
| 127 | TEST_F(CPWLEditEmbeddertest, DeleteEntireTextSelection) { |
| 128 | for (int i = 0; i < 50; ++i) { |
| 129 | EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), i + 'A', 0)); |
| 130 | } |
| 131 | |
| 132 | GetCPWLEdit()->SetSelection(0, -1); |
| 133 | EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr", |
| 134 | GetCPWLEdit()->GetSelectedText().c_str()); |
| 135 | |
| 136 | GetCPWLEdit()->DeleteSelectedText(); |
| 137 | EXPECT_TRUE(GetCPWLEdit()->GetText().IsEmpty()); |
| 138 | } |
| 139 | |
| 140 | TEST_F(CPWLEditEmbeddertest, DeleteTextSelectionMiddle) { |
| 141 | for (int i = 0; i < 50; ++i) { |
| 142 | EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), i + 'A', 0)); |
| 143 | } |
| 144 | |
| 145 | GetCPWLEdit()->SetSelection(12, 23); |
| 146 | EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLEdit()->GetSelectedText().c_str()); |
| 147 | |
| 148 | GetCPWLEdit()->DeleteSelectedText(); |
| 149 | EXPECT_STREQ(L"ABCDEFGHIJKLXYZ[\\]^_`abcdefghijklmnopqr", |
| 150 | GetCPWLEdit()->GetText().c_str()); |
| 151 | } |
| 152 | |
| 153 | TEST_F(CPWLEditEmbeddertest, DeleteTextSelectionLeft) { |
| 154 | for (int i = 0; i < 50; ++i) { |
| 155 | EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), i + 'A', 0)); |
| 156 | } |
| 157 | |
| 158 | GetCPWLEdit()->SetSelection(0, 5); |
| 159 | EXPECT_STREQ(L"ABCDE", GetCPWLEdit()->GetSelectedText().c_str()); |
| 160 | |
| 161 | GetCPWLEdit()->DeleteSelectedText(); |
| 162 | EXPECT_STREQ(L"FGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr", |
| 163 | GetCPWLEdit()->GetText().c_str()); |
| 164 | } |
| 165 | |
| 166 | TEST_F(CPWLEditEmbeddertest, DeleteTextSelectionRight) { |
| 167 | for (int i = 0; i < 50; ++i) { |
| 168 | EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), i + 'A', 0)); |
| 169 | } |
| 170 | |
| 171 | GetCPWLEdit()->SetSelection(45, 50); |
| 172 | EXPECT_STREQ(L"nopqr", GetCPWLEdit()->GetSelectedText().c_str()); |
| 173 | |
| 174 | GetCPWLEdit()->DeleteSelectedText(); |
| 175 | EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm", |
| 176 | GetCPWLEdit()->GetText().c_str()); |
| 177 | } |
| 178 | |
| 179 | TEST_F(CPWLEditEmbeddertest, DeleteEmptyTextSelection) { |
| 180 | for (int i = 0; i < 50; ++i) { |
| 181 | EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), i + 'A', 0)); |
| 182 | } |
| 183 | |
| 184 | GetCPWLEdit()->DeleteSelectedText(); |
| 185 | EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr", |
| 186 | GetCPWLEdit()->GetText().c_str()); |
| 187 | } |