blob: 296dd4fc5d328b2d5b33d9294bcff1be41a995c6 [file] [log] [blame]
Lei Zhangaf680b12017-06-02 16:42:59 -07001// 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
Tom Sepezd8ac6682018-10-04 19:29:30 +00005#include "fpdfsdk/pwl/cpwl_edit.h"
6
Lei Zhangaf680b12017-06-02 16:42:59 -07007#include "fpdfsdk/cpdfsdk_annot.h"
Dan Sinclaircbf76e62018-03-28 21:00:35 +00008#include "fpdfsdk/cpdfsdk_annotiterator.h"
Lei Zhangaf680b12017-06-02 16:42:59 -07009#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
10#include "fpdfsdk/formfiller/cffl_formfiller.h"
11#include "fpdfsdk/formfiller/cffl_interactiveformfiller.h"
Lei Zhangac756642018-12-13 20:47:10 +000012#include "public/fpdf_fwlevent.h"
Lei Zhangaf680b12017-06-02 16:42:59 -070013#include "testing/embedder_test.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
Lei Zhangab41f252018-12-23 03:10:50 +000016class CPWLEditEmbedderTest : public EmbedderTest {
Diana Gagedce2d722017-06-20 11:17:11 -070017 protected:
18 void SetUp() override {
19 EmbedderTest::SetUp();
20 CreateAndInitializeFormPDF();
21 }
22
23 void TearDown() override {
24 UnloadPage(GetPage());
25 EmbedderTest::TearDown();
26 }
27
28 void CreateAndInitializeFormPDF() {
Diana Gageab390972017-07-28 17:07:39 -070029 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gagedce2d722017-06-20 11:17:11 -070030 m_page = LoadPage(0);
31 ASSERT_TRUE(m_page);
32
Tom Sepez2563fc32018-08-01 19:13:06 +000033 m_pFormFillEnv =
34 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(form_handle());
Dan Sinclaircbf76e62018-03-28 21:00:35 +000035 CPDFSDK_AnnotIterator iter(m_pFormFillEnv->GetPageView(0),
36 CPDF_Annot::Subtype::WIDGET);
Diana Gageab390972017-07-28 17:07:39 -070037 // Normal text field.
38 m_pAnnot = iter.GetFirstAnnot();
39 ASSERT_TRUE(m_pAnnot);
40 ASSERT_EQ(CPDF_Annot::Subtype::WIDGET, m_pAnnot->GetAnnotSubtype());
Diana Gagedce2d722017-06-20 11:17:11 -070041
Diana Gageab390972017-07-28 17:07:39 -070042 // Read-only text field.
43 CPDFSDK_Annot* pAnnotReadOnly = iter.GetNextAnnot(m_pAnnot);
Diana Gagedce2d722017-06-20 11:17:11 -070044
Diana Gageab390972017-07-28 17:07:39 -070045 // Pre-filled text field with char limit of 10.
46 m_pAnnotCharLimit = iter.GetNextAnnot(pAnnotReadOnly);
47 ASSERT_TRUE(m_pAnnotCharLimit);
48 ASSERT_EQ(CPDF_Annot::Subtype::WIDGET,
49 m_pAnnotCharLimit->GetAnnotSubtype());
50 CPDFSDK_Annot* pLastAnnot = iter.GetLastAnnot();
51 ASSERT_EQ(m_pAnnotCharLimit, pLastAnnot);
52 }
53
54 void FormFillerAndWindowSetup(CPDFSDK_Annot* pAnnotTextField) {
Diana Gagedce2d722017-06-20 11:17:11 -070055 CFFL_InteractiveFormFiller* pInteractiveFormFiller =
Diana Gageab390972017-07-28 17:07:39 -070056 m_pFormFillEnv->GetInteractiveFormFiller();
Diana Gagedce2d722017-06-20 11:17:11 -070057 {
Diana Gageab390972017-07-28 17:07:39 -070058 CPDFSDK_Annot::ObservedPtr pObserved(pAnnotTextField);
Diana Gagedce2d722017-06-20 11:17:11 -070059 EXPECT_TRUE(pInteractiveFormFiller->OnSetFocus(&pObserved, 0));
60 }
61
Diana Gageab390972017-07-28 17:07:39 -070062 m_pFormFiller =
63 pInteractiveFormFiller->GetFormFiller(pAnnotTextField, false);
Diana Gagedce2d722017-06-20 11:17:11 -070064 ASSERT_TRUE(m_pFormFiller);
65
66 CPWL_Wnd* pWindow =
Diana Gageab390972017-07-28 17:07:39 -070067 m_pFormFiller->GetPDFWindow(m_pFormFillEnv->GetPageView(0), false);
Diana Gagedce2d722017-06-20 11:17:11 -070068 ASSERT_TRUE(pWindow);
Diana Gagedce2d722017-06-20 11:17:11 -070069 m_pEdit = static_cast<CPWL_Edit*>(pWindow);
70 }
71
Diana Gagebe38e162017-07-26 20:32:00 -070072 void TypeTextIntoTextField(int num_chars) {
73 // Type text starting with 'A' to as many chars as specified by |num_chars|.
74 for (int i = 0; i < num_chars; ++i) {
75 EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), i + 'A', 0));
76 }
77 }
78
Diana Gagedce2d722017-06-20 11:17:11 -070079 FPDF_PAGE GetPage() { return m_page; }
80 CPWL_Edit* GetCPWLEdit() { return m_pEdit; }
81 CFFL_FormFiller* GetCFFLFormFiller() { return m_pFormFiller; }
82 CPDFSDK_Annot* GetCPDFSDKAnnot() { return m_pAnnot; }
Diana Gageab390972017-07-28 17:07:39 -070083 CPDFSDK_Annot* GetCPDFSDKAnnotCharLimit() { return m_pAnnotCharLimit; }
Diana Gagedce2d722017-06-20 11:17:11 -070084
85 private:
86 FPDF_PAGE m_page;
87 CPWL_Edit* m_pEdit;
88 CFFL_FormFiller* m_pFormFiller;
89 CPDFSDK_Annot* m_pAnnot;
Diana Gageab390972017-07-28 17:07:39 -070090 CPDFSDK_Annot* m_pAnnotCharLimit;
91 CPDFSDK_FormFillEnvironment* m_pFormFillEnv;
Diana Gagedce2d722017-06-20 11:17:11 -070092};
Lei Zhangaf680b12017-06-02 16:42:59 -070093
Lei Zhangab41f252018-12-23 03:10:50 +000094TEST_F(CPWLEditEmbedderTest, TypeText) {
Diana Gageab390972017-07-28 17:07:39 -070095 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
Diana Gagedce2d722017-06-20 11:17:11 -070096 EXPECT_TRUE(GetCPWLEdit()->GetText().IsEmpty());
97 EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'a', 0));
98 EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'b', 0));
99 EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'c', 0));
Lei Zhangaf680b12017-06-02 16:42:59 -0700100
Diana Gagedce2d722017-06-20 11:17:11 -0700101 EXPECT_STREQ(L"abc", GetCPWLEdit()->GetText().c_str());
102}
Lei Zhangaf680b12017-06-02 16:42:59 -0700103
Lei Zhangab41f252018-12-23 03:10:50 +0000104TEST_F(CPWLEditEmbedderTest, GetSelectedTextEmptyAndBasic) {
Diana Gageab390972017-07-28 17:07:39 -0700105 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
Diana Gagedce2d722017-06-20 11:17:11 -0700106 // Attempt to set selection before text has been typed to test that
107 // selection is identified as empty.
108 //
109 // Select from character index [0, 3) within form text field.
Diana Gage4d02e902017-07-20 17:20:31 -0700110 GetCPWLEdit()->SetSelection(0, 3);
Diana Gagedce2d722017-06-20 11:17:11 -0700111 EXPECT_TRUE(GetCPWLEdit()->GetSelectedText().IsEmpty());
112
113 EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'a', 0));
114 EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'b', 0));
115 EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'c', 0));
Diana Gage4d02e902017-07-20 17:20:31 -0700116 GetCPWLEdit()->SetSelection(0, 2);
Diana Gagedce2d722017-06-20 11:17:11 -0700117
118 EXPECT_STREQ(L"ab", GetCPWLEdit()->GetSelectedText().c_str());
119}
120
Lei Zhangab41f252018-12-23 03:10:50 +0000121TEST_F(CPWLEditEmbedderTest, GetSelectedTextFragments) {
Diana Gageab390972017-07-28 17:07:39 -0700122 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
Diana Gagebe38e162017-07-26 20:32:00 -0700123 TypeTextIntoTextField(50);
Lei Zhangaf680b12017-06-02 16:42:59 -0700124
Diana Gage4d02e902017-07-20 17:20:31 -0700125 GetCPWLEdit()->SetSelection(0, 0);
Diana Gagedce2d722017-06-20 11:17:11 -0700126 EXPECT_TRUE(GetCPWLEdit()->GetSelectedText().IsEmpty());
Lei Zhangaf680b12017-06-02 16:42:59 -0700127
Diana Gage4d02e902017-07-20 17:20:31 -0700128 GetCPWLEdit()->SetSelection(0, 1);
Diana Gagedce2d722017-06-20 11:17:11 -0700129 EXPECT_STREQ(L"A", GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhangaf680b12017-06-02 16:42:59 -0700130
Diana Gage4d02e902017-07-20 17:20:31 -0700131 GetCPWLEdit()->SetSelection(0, -1);
Diana Gagedce2d722017-06-20 11:17:11 -0700132 EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
133 GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhangaf680b12017-06-02 16:42:59 -0700134
Diana Gage4d02e902017-07-20 17:20:31 -0700135 GetCPWLEdit()->SetSelection(-8, -1);
Diana Gagedce2d722017-06-20 11:17:11 -0700136 EXPECT_TRUE(GetCPWLEdit()->GetSelectedText().IsEmpty());
Lei Zhangaf680b12017-06-02 16:42:59 -0700137
Diana Gage4d02e902017-07-20 17:20:31 -0700138 GetCPWLEdit()->SetSelection(23, 12);
Diana Gagedce2d722017-06-20 11:17:11 -0700139 EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhangaf680b12017-06-02 16:42:59 -0700140
Diana Gage4d02e902017-07-20 17:20:31 -0700141 GetCPWLEdit()->SetSelection(12, 23);
Diana Gagedce2d722017-06-20 11:17:11 -0700142 EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhangaf680b12017-06-02 16:42:59 -0700143
Diana Gage4d02e902017-07-20 17:20:31 -0700144 GetCPWLEdit()->SetSelection(49, 50);
Diana Gagedce2d722017-06-20 11:17:11 -0700145 EXPECT_STREQ(L"r", GetCPWLEdit()->GetSelectedText().c_str());
Diana Gagea37f2f12017-07-25 17:39:50 -0700146
147 GetCPWLEdit()->SetSelection(49, 55);
148 EXPECT_STREQ(L"r", GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhangaf680b12017-06-02 16:42:59 -0700149}
Diana Gage1c7f1422017-07-24 11:19:52 -0700150
Lei Zhangab41f252018-12-23 03:10:50 +0000151TEST_F(CPWLEditEmbedderTest, DeleteEntireTextSelection) {
Diana Gageab390972017-07-28 17:07:39 -0700152 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
Diana Gagebe38e162017-07-26 20:32:00 -0700153 TypeTextIntoTextField(50);
Diana Gage1c7f1422017-07-24 11:19:52 -0700154
155 GetCPWLEdit()->SetSelection(0, -1);
156 EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
157 GetCPWLEdit()->GetSelectedText().c_str());
158
Lei Zhang38adfc12017-08-25 22:05:18 -0700159 GetCPWLEdit()->ReplaceSelection(L"");
Diana Gage1c7f1422017-07-24 11:19:52 -0700160 EXPECT_TRUE(GetCPWLEdit()->GetText().IsEmpty());
161}
162
Lei Zhangab41f252018-12-23 03:10:50 +0000163TEST_F(CPWLEditEmbedderTest, DeleteTextSelectionMiddle) {
Diana Gageab390972017-07-28 17:07:39 -0700164 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
Diana Gagebe38e162017-07-26 20:32:00 -0700165 TypeTextIntoTextField(50);
Diana Gage1c7f1422017-07-24 11:19:52 -0700166
167 GetCPWLEdit()->SetSelection(12, 23);
168 EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLEdit()->GetSelectedText().c_str());
169
Lei Zhang38adfc12017-08-25 22:05:18 -0700170 GetCPWLEdit()->ReplaceSelection(L"");
Diana Gage1c7f1422017-07-24 11:19:52 -0700171 EXPECT_STREQ(L"ABCDEFGHIJKLXYZ[\\]^_`abcdefghijklmnopqr",
172 GetCPWLEdit()->GetText().c_str());
173}
174
Lei Zhangab41f252018-12-23 03:10:50 +0000175TEST_F(CPWLEditEmbedderTest, DeleteTextSelectionLeft) {
Diana Gageab390972017-07-28 17:07:39 -0700176 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
Diana Gagebe38e162017-07-26 20:32:00 -0700177 TypeTextIntoTextField(50);
Diana Gage1c7f1422017-07-24 11:19:52 -0700178
179 GetCPWLEdit()->SetSelection(0, 5);
180 EXPECT_STREQ(L"ABCDE", GetCPWLEdit()->GetSelectedText().c_str());
181
Lei Zhang38adfc12017-08-25 22:05:18 -0700182 GetCPWLEdit()->ReplaceSelection(L"");
Diana Gage1c7f1422017-07-24 11:19:52 -0700183 EXPECT_STREQ(L"FGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
184 GetCPWLEdit()->GetText().c_str());
185}
186
Lei Zhangab41f252018-12-23 03:10:50 +0000187TEST_F(CPWLEditEmbedderTest, DeleteTextSelectionRight) {
Diana Gageab390972017-07-28 17:07:39 -0700188 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
Diana Gagebe38e162017-07-26 20:32:00 -0700189 TypeTextIntoTextField(50);
Diana Gage1c7f1422017-07-24 11:19:52 -0700190
191 GetCPWLEdit()->SetSelection(45, 50);
192 EXPECT_STREQ(L"nopqr", GetCPWLEdit()->GetSelectedText().c_str());
193
Lei Zhang38adfc12017-08-25 22:05:18 -0700194 GetCPWLEdit()->ReplaceSelection(L"");
Diana Gage1c7f1422017-07-24 11:19:52 -0700195 EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm",
196 GetCPWLEdit()->GetText().c_str());
197}
198
Lei Zhangab41f252018-12-23 03:10:50 +0000199TEST_F(CPWLEditEmbedderTest, DeleteEmptyTextSelection) {
Diana Gageab390972017-07-28 17:07:39 -0700200 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
Diana Gagebe38e162017-07-26 20:32:00 -0700201 TypeTextIntoTextField(50);
Diana Gage1c7f1422017-07-24 11:19:52 -0700202
Lei Zhang38adfc12017-08-25 22:05:18 -0700203 GetCPWLEdit()->ReplaceSelection(L"");
Diana Gage1c7f1422017-07-24 11:19:52 -0700204 EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
205 GetCPWLEdit()->GetText().c_str());
206}
Diana Gageab390972017-07-28 17:07:39 -0700207
Lei Zhangab41f252018-12-23 03:10:50 +0000208TEST_F(CPWLEditEmbedderTest, InsertTextInEmptyTextField) {
Diana Gageab390972017-07-28 17:07:39 -0700209 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
Lei Zhang38adfc12017-08-25 22:05:18 -0700210 GetCPWLEdit()->ReplaceSelection(L"Hello");
Diana Gageab390972017-07-28 17:07:39 -0700211 EXPECT_STREQ(L"Hello", GetCPWLEdit()->GetText().c_str());
212}
213
Lei Zhangab41f252018-12-23 03:10:50 +0000214TEST_F(CPWLEditEmbedderTest, InsertTextInPopulatedTextFieldLeft) {
Diana Gageab390972017-07-28 17:07:39 -0700215 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
216 TypeTextIntoTextField(10);
217
218 // Move cursor to beginning of text field.
219 EXPECT_TRUE(
220 GetCFFLFormFiller()->OnKeyDown(GetCPDFSDKAnnot(), FWL_VKEY_Home, 0));
221
Lei Zhang38adfc12017-08-25 22:05:18 -0700222 GetCPWLEdit()->ReplaceSelection(L"Hello");
Diana Gageab390972017-07-28 17:07:39 -0700223 EXPECT_STREQ(L"HelloABCDEFGHIJ", GetCPWLEdit()->GetText().c_str());
224}
225
Lei Zhangab41f252018-12-23 03:10:50 +0000226TEST_F(CPWLEditEmbedderTest, InsertTextInPopulatedTextFieldMiddle) {
Diana Gageab390972017-07-28 17:07:39 -0700227 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
228 TypeTextIntoTextField(10);
229
230 // Move cursor to middle of text field.
231 for (int i = 0; i < 5; ++i) {
232 EXPECT_TRUE(
233 GetCFFLFormFiller()->OnKeyDown(GetCPDFSDKAnnot(), FWL_VKEY_Left, 0));
234 }
235
Lei Zhang38adfc12017-08-25 22:05:18 -0700236 GetCPWLEdit()->ReplaceSelection(L"Hello");
Diana Gageab390972017-07-28 17:07:39 -0700237 EXPECT_STREQ(L"ABCDEHelloFGHIJ", GetCPWLEdit()->GetText().c_str());
238}
239
Lei Zhangab41f252018-12-23 03:10:50 +0000240TEST_F(CPWLEditEmbedderTest, InsertTextInPopulatedTextFieldRight) {
Diana Gageab390972017-07-28 17:07:39 -0700241 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
242 TypeTextIntoTextField(10);
243
Lei Zhang38adfc12017-08-25 22:05:18 -0700244 GetCPWLEdit()->ReplaceSelection(L"Hello");
Diana Gageab390972017-07-28 17:07:39 -0700245 EXPECT_STREQ(L"ABCDEFGHIJHello", GetCPWLEdit()->GetText().c_str());
246}
247
Lei Zhangab41f252018-12-23 03:10:50 +0000248TEST_F(CPWLEditEmbedderTest,
Diana Gageab390972017-07-28 17:07:39 -0700249 InsertTextAndReplaceSelectionInPopulatedTextFieldWhole) {
250 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
251 TypeTextIntoTextField(10);
252
253 GetCPWLEdit()->SetSelection(0, -1);
254 EXPECT_STREQ(L"ABCDEFGHIJ", GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhang38adfc12017-08-25 22:05:18 -0700255 GetCPWLEdit()->ReplaceSelection(L"Hello");
Diana Gageab390972017-07-28 17:07:39 -0700256 EXPECT_STREQ(L"Hello", GetCPWLEdit()->GetText().c_str());
257}
258
Lei Zhangab41f252018-12-23 03:10:50 +0000259TEST_F(CPWLEditEmbedderTest,
Diana Gageab390972017-07-28 17:07:39 -0700260 InsertTextAndReplaceSelectionInPopulatedTextFieldLeft) {
261 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
262 TypeTextIntoTextField(10);
263
264 GetCPWLEdit()->SetSelection(0, 5);
265 EXPECT_STREQ(L"ABCDE", GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhang38adfc12017-08-25 22:05:18 -0700266 GetCPWLEdit()->ReplaceSelection(L"Hello");
Diana Gageab390972017-07-28 17:07:39 -0700267 EXPECT_STREQ(L"HelloFGHIJ", GetCPWLEdit()->GetText().c_str());
268}
269
Lei Zhangab41f252018-12-23 03:10:50 +0000270TEST_F(CPWLEditEmbedderTest,
Diana Gageab390972017-07-28 17:07:39 -0700271 InsertTextAndReplaceSelectionInPopulatedTextFieldMiddle) {
272 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
273 TypeTextIntoTextField(10);
274
275 GetCPWLEdit()->SetSelection(2, 7);
276 EXPECT_STREQ(L"CDEFG", GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhang38adfc12017-08-25 22:05:18 -0700277 GetCPWLEdit()->ReplaceSelection(L"Hello");
Diana Gageab390972017-07-28 17:07:39 -0700278 EXPECT_STREQ(L"ABHelloHIJ", GetCPWLEdit()->GetText().c_str());
279}
280
Lei Zhangab41f252018-12-23 03:10:50 +0000281TEST_F(CPWLEditEmbedderTest,
Diana Gageab390972017-07-28 17:07:39 -0700282 InsertTextAndReplaceSelectionInPopulatedTextFieldRight) {
283 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
284 TypeTextIntoTextField(10);
285
286 GetCPWLEdit()->SetSelection(5, 10);
287 EXPECT_STREQ(L"FGHIJ", GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhang38adfc12017-08-25 22:05:18 -0700288 GetCPWLEdit()->ReplaceSelection(L"Hello");
Diana Gageab390972017-07-28 17:07:39 -0700289 EXPECT_STREQ(L"ABCDEHello", GetCPWLEdit()->GetText().c_str());
290}
291
Lei Zhangab41f252018-12-23 03:10:50 +0000292TEST_F(CPWLEditEmbedderTest, InsertTextInEmptyCharLimitTextFieldOverflow) {
Diana Gageab390972017-07-28 17:07:39 -0700293 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
294 GetCPWLEdit()->SetSelection(0, -1);
295 EXPECT_STREQ(L"Elephant", GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhang38adfc12017-08-25 22:05:18 -0700296 GetCPWLEdit()->ReplaceSelection(L"");
Diana Gageab390972017-07-28 17:07:39 -0700297
Lei Zhang38adfc12017-08-25 22:05:18 -0700298 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
Diana Gageab390972017-07-28 17:07:39 -0700299 EXPECT_STREQ(L"Hippopotam", GetCPWLEdit()->GetText().c_str());
300}
301
Lei Zhangab41f252018-12-23 03:10:50 +0000302TEST_F(CPWLEditEmbedderTest, InsertTextInEmptyCharLimitTextFieldFit) {
Diana Gageab390972017-07-28 17:07:39 -0700303 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
304 GetCPWLEdit()->SetSelection(0, -1);
305 EXPECT_STREQ(L"Elephant", GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhang38adfc12017-08-25 22:05:18 -0700306 GetCPWLEdit()->ReplaceSelection(L"");
Diana Gageab390972017-07-28 17:07:39 -0700307
Lei Zhang38adfc12017-08-25 22:05:18 -0700308 GetCPWLEdit()->ReplaceSelection(L"Zebra");
Diana Gageab390972017-07-28 17:07:39 -0700309 EXPECT_STREQ(L"Zebra", GetCPWLEdit()->GetText().c_str());
310}
311
Lei Zhangab41f252018-12-23 03:10:50 +0000312TEST_F(CPWLEditEmbedderTest, InsertTextInPopulatedCharLimitTextFieldLeft) {
Diana Gageab390972017-07-28 17:07:39 -0700313 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
Lei Zhang38adfc12017-08-25 22:05:18 -0700314 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
Diana Gageab390972017-07-28 17:07:39 -0700315 EXPECT_STREQ(L"HiElephant", GetCPWLEdit()->GetText().c_str());
316}
317
Lei Zhangab41f252018-12-23 03:10:50 +0000318TEST_F(CPWLEditEmbedderTest, InsertTextInPopulatedCharLimitTextFieldMiddle) {
Diana Gageab390972017-07-28 17:07:39 -0700319 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
320 // Move cursor to middle of text field.
321 for (int i = 0; i < 5; ++i) {
322 EXPECT_TRUE(GetCFFLFormFiller()->OnKeyDown(GetCPDFSDKAnnotCharLimit(),
323 FWL_VKEY_Right, 0));
324 }
325
Lei Zhang38adfc12017-08-25 22:05:18 -0700326 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
Diana Gageab390972017-07-28 17:07:39 -0700327 EXPECT_STREQ(L"ElephHiant", GetCPWLEdit()->GetText().c_str());
328}
329
Lei Zhangab41f252018-12-23 03:10:50 +0000330TEST_F(CPWLEditEmbedderTest, InsertTextInPopulatedCharLimitTextFieldRight) {
Diana Gageab390972017-07-28 17:07:39 -0700331 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
332 // Move cursor to end of text field.
333 EXPECT_TRUE(GetCFFLFormFiller()->OnKeyDown(GetCPDFSDKAnnotCharLimit(),
334 FWL_VKEY_End, 0));
335
Lei Zhang38adfc12017-08-25 22:05:18 -0700336 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
Diana Gageab390972017-07-28 17:07:39 -0700337 EXPECT_STREQ(L"ElephantHi", GetCPWLEdit()->GetText().c_str());
338}
339
Lei Zhangab41f252018-12-23 03:10:50 +0000340TEST_F(CPWLEditEmbedderTest,
Diana Gageab390972017-07-28 17:07:39 -0700341 InsertTextAndReplaceSelectionInPopulatedCharLimitTextFieldWhole) {
342 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
343 GetCPWLEdit()->SetSelection(0, -1);
344 EXPECT_STREQ(L"Elephant", GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhang38adfc12017-08-25 22:05:18 -0700345 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
Diana Gageab390972017-07-28 17:07:39 -0700346 EXPECT_STREQ(L"Hippopotam", GetCPWLEdit()->GetText().c_str());
347}
348
Lei Zhangab41f252018-12-23 03:10:50 +0000349TEST_F(CPWLEditEmbedderTest,
Diana Gageab390972017-07-28 17:07:39 -0700350 InsertTextAndReplaceSelectionInPopulatedCharLimitTextFieldLeft) {
351 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
352 GetCPWLEdit()->SetSelection(0, 4);
353 EXPECT_STREQ(L"Elep", GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhang38adfc12017-08-25 22:05:18 -0700354 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
Diana Gageab390972017-07-28 17:07:39 -0700355 EXPECT_STREQ(L"Hippophant", GetCPWLEdit()->GetText().c_str());
356}
357
Lei Zhangab41f252018-12-23 03:10:50 +0000358TEST_F(CPWLEditEmbedderTest,
Diana Gageab390972017-07-28 17:07:39 -0700359 InsertTextAndReplaceSelectionInPopulatedCharLimitTextFieldMiddle) {
360 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
361 GetCPWLEdit()->SetSelection(2, 6);
362 EXPECT_STREQ(L"epha", GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhang38adfc12017-08-25 22:05:18 -0700363 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
Diana Gageab390972017-07-28 17:07:39 -0700364 EXPECT_STREQ(L"ElHippopnt", GetCPWLEdit()->GetText().c_str());
365}
366
Lei Zhangab41f252018-12-23 03:10:50 +0000367TEST_F(CPWLEditEmbedderTest,
Diana Gageab390972017-07-28 17:07:39 -0700368 InsertTextAndReplaceSelectionInPopulatedCharLimitTextFieldRight) {
369 FormFillerAndWindowSetup(GetCPDFSDKAnnotCharLimit());
370 GetCPWLEdit()->SetSelection(4, 8);
371 EXPECT_STREQ(L"hant", GetCPWLEdit()->GetSelectedText().c_str());
Lei Zhang38adfc12017-08-25 22:05:18 -0700372 GetCPWLEdit()->ReplaceSelection(L"Hippopotamus");
Diana Gageab390972017-07-28 17:07:39 -0700373 EXPECT_STREQ(L"ElepHippop", GetCPWLEdit()->GetText().c_str());
374}
Ryan Harrisonf8763bb2017-08-31 14:22:39 -0400375
Lei Zhangab41f252018-12-23 03:10:50 +0000376TEST_F(CPWLEditEmbedderTest, SetTextWithEndCarriageFeed) {
Ryan Harrisonf8763bb2017-08-31 14:22:39 -0400377 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
378 GetCPWLEdit()->SetText(L"Foo\r");
379 EXPECT_STREQ(L"Foo", GetCPWLEdit()->GetText().c_str());
380}
381
Lei Zhangab41f252018-12-23 03:10:50 +0000382TEST_F(CPWLEditEmbedderTest, SetTextWithEndNewline) {
Ryan Harrisonf8763bb2017-08-31 14:22:39 -0400383 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
384 GetCPWLEdit()->SetText(L"Foo\n");
385 EXPECT_STREQ(L"Foo", GetCPWLEdit()->GetText().c_str());
386}
387
Lei Zhangab41f252018-12-23 03:10:50 +0000388TEST_F(CPWLEditEmbedderTest, SetTextWithEndCarriageFeedAndNewLine) {
Ryan Harrisonf8763bb2017-08-31 14:22:39 -0400389 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
390 GetCPWLEdit()->SetText(L"Foo\r\n");
391 EXPECT_STREQ(L"Foo", GetCPWLEdit()->GetText().c_str());
392}
393
Lei Zhangab41f252018-12-23 03:10:50 +0000394TEST_F(CPWLEditEmbedderTest, SetTextWithEndNewLineAndCarriageFeed) {
Ryan Harrisonf8763bb2017-08-31 14:22:39 -0400395 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
396 GetCPWLEdit()->SetText(L"Foo\n\r");
397 EXPECT_STREQ(L"Foo", GetCPWLEdit()->GetText().c_str());
398}
399
Lei Zhangab41f252018-12-23 03:10:50 +0000400TEST_F(CPWLEditEmbedderTest, SetTextWithBodyCarriageFeed) {
Ryan Harrisonf8763bb2017-08-31 14:22:39 -0400401 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
402 GetCPWLEdit()->SetText(L"Foo\rBar");
403 EXPECT_STREQ(L"FooBar", GetCPWLEdit()->GetText().c_str());
404}
405
Lei Zhangab41f252018-12-23 03:10:50 +0000406TEST_F(CPWLEditEmbedderTest, SetTextWithBodyNewline) {
Ryan Harrisonf8763bb2017-08-31 14:22:39 -0400407 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
408 GetCPWLEdit()->SetText(L"Foo\nBar");
409 EXPECT_STREQ(L"FooBar", GetCPWLEdit()->GetText().c_str());
410}
411
Lei Zhangab41f252018-12-23 03:10:50 +0000412TEST_F(CPWLEditEmbedderTest, SetTextWithBodyCarriageFeedAndNewLine) {
Ryan Harrisonf8763bb2017-08-31 14:22:39 -0400413 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
414 GetCPWLEdit()->SetText(L"Foo\r\nBar");
415 EXPECT_STREQ(L"FooBar", GetCPWLEdit()->GetText().c_str());
416}
417
Lei Zhangab41f252018-12-23 03:10:50 +0000418TEST_F(CPWLEditEmbedderTest, SetTextWithBodyNewLineAndCarriageFeed) {
Ryan Harrisonf8763bb2017-08-31 14:22:39 -0400419 FormFillerAndWindowSetup(GetCPDFSDKAnnot());
420 GetCPWLEdit()->SetText(L"Foo\n\rBar");
421 EXPECT_STREQ(L"FooBar", GetCPWLEdit()->GetText().c_str());
422}