1 // Copyright 2017 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "fpdfsdk/pwl/cpwl_combo_box_embeddertest.h"
6
7 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
8 #include "fpdfsdk/cpdfsdk_helpers.h"
9 #include "fpdfsdk/cpdfsdk_widget.h"
10 #include "fpdfsdk/formfiller/cffl_formfield.h"
11 #include "fpdfsdk/formfiller/cffl_interactiveformfiller.h"
12 #include "fpdfsdk/pwl/cpwl_combo_box.h"
13 #include "fpdfsdk/pwl/cpwl_wnd.h"
14 #include "public/fpdf_fwlevent.h"
15 #include "testing/embedder_test.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 class CPWLComboBoxEditEmbedderTest : public CPWLComboBoxEmbedderTest {};
19
TEST_F(CPWLComboBoxEditEmbedderTest,GetSelectedTextEmptyAndBasicNormal)20 TEST_F(CPWLComboBoxEditEmbedderTest, GetSelectedTextEmptyAndBasicNormal) {
21 FormFillerAndWindowSetup(GetCPDFSDKAnnotNormal());
22
23 // Automatically pre-filled with "Banana".
24 EXPECT_FALSE(GetCPWLComboBox()->GetText().IsEmpty());
25 EXPECT_STREQ(L"Banana", GetCPWLComboBox()->GetText().c_str());
26
27 // Check that selection is initially empty, then select entire word.
28 EXPECT_TRUE(GetCPWLComboBox()->GetSelectedText().IsEmpty());
29 GetCPWLComboBox()->SetSelectText();
30 EXPECT_STREQ(L"Banana", GetCPWLComboBox()->GetSelectedText().c_str());
31
32 // Select other options.
33 GetCPWLComboBox()->SetSelect(0);
34 EXPECT_STREQ(L"Apple", GetCPWLComboBox()->GetSelectedText().c_str());
35 GetCPWLComboBox()->SetSelect(2);
36 EXPECT_STREQ(L"Cherry", GetCPWLComboBox()->GetSelectedText().c_str());
37
38 // Verify that combobox text cannot be edited.
39 EXPECT_FALSE(GetCFFLFormField()->OnChar(GetCPDFSDKAnnotNormal(), 'a', {}));
40 }
41
TEST_F(CPWLComboBoxEditEmbedderTest,GetSelectedTextFragmentsNormal)42 TEST_F(CPWLComboBoxEditEmbedderTest, GetSelectedTextFragmentsNormal) {
43 FormFillerAndWindowSetup(GetCPDFSDKAnnotNormal());
44 EXPECT_STREQ(L"Banana", GetCPWLComboBox()->GetText().c_str());
45
46 GetCPWLComboBox()->SetEditSelection(0, 0);
47 EXPECT_TRUE(GetCPWLComboBox()->GetSelectedText().IsEmpty());
48
49 GetCPWLComboBox()->SetEditSelection(0, 1);
50 EXPECT_STREQ(L"B", GetCPWLComboBox()->GetSelectedText().c_str());
51
52 GetCPWLComboBox()->SetEditSelection(0, -1);
53 EXPECT_STREQ(L"Banana", GetCPWLComboBox()->GetSelectedText().c_str());
54
55 GetCPWLComboBox()->SetEditSelection(-8, -1);
56 EXPECT_TRUE(GetCPWLComboBox()->GetSelectedText().IsEmpty());
57
58 GetCPWLComboBox()->SetEditSelection(4, 1);
59 EXPECT_STREQ(L"ana", GetCPWLComboBox()->GetSelectedText().c_str());
60
61 GetCPWLComboBox()->SetEditSelection(1, 4);
62 EXPECT_STREQ(L"ana", GetCPWLComboBox()->GetSelectedText().c_str());
63
64 GetCPWLComboBox()->SetEditSelection(5, 6);
65 EXPECT_STREQ(L"a", GetCPWLComboBox()->GetSelectedText().c_str());
66 }
67
TEST_F(CPWLComboBoxEditEmbedderTest,GetSelectedTextEmptyAndBasicEditable)68 TEST_F(CPWLComboBoxEditEmbedderTest, GetSelectedTextEmptyAndBasicEditable) {
69 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
70 EXPECT_TRUE(GetCPWLComboBox()->GetText().IsEmpty());
71
72 // Check selection is initially empty, then select a provided option.
73 EXPECT_TRUE(GetCPWLComboBox()->GetSelectedText().IsEmpty());
74 GetCPWLComboBox()->SetSelect(0);
75 GetCPWLComboBox()->SetSelectText();
76 EXPECT_STREQ(L"Foo", GetCPWLComboBox()->GetSelectedText().c_str());
77
78 // Select another option and then select last char of that option.
79 GetCPWLComboBox()->SetSelect(1);
80 EXPECT_STREQ(L"Bar", GetCPWLComboBox()->GetSelectedText().c_str());
81 GetCPWLComboBox()->SetEditSelection(2, 3);
82 EXPECT_STREQ(L"r", GetCPWLComboBox()->GetSelectedText().c_str());
83
84 // Type into editable combobox text field and select new text.
85 EXPECT_TRUE(
86 GetCFFLFormField()->OnChar(GetCPDFSDKAnnotUserEditable(), 'a', {}));
87 EXPECT_TRUE(
88 GetCFFLFormField()->OnChar(GetCPDFSDKAnnotUserEditable(), 'b', {}));
89 EXPECT_TRUE(
90 GetCFFLFormField()->OnChar(GetCPDFSDKAnnotUserEditable(), 'c', {}));
91
92 EXPECT_TRUE(GetCPWLComboBox()->GetSelectedText().IsEmpty());
93 GetCPWLComboBox()->SetEditSelection(0, 5);
94 EXPECT_STREQ(L"Baabc", GetCPWLComboBox()->GetSelectedText().c_str());
95 }
96
TEST_F(CPWLComboBoxEditEmbedderTest,GetSelectedTextFragmentsEditable)97 TEST_F(CPWLComboBoxEditEmbedderTest, GetSelectedTextFragmentsEditable) {
98 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
99 TypeTextIntoTextField(50);
100
101 GetCPWLComboBox()->SetEditSelection(0, 0);
102 EXPECT_TRUE(GetCPWLComboBox()->GetSelectedText().IsEmpty());
103
104 GetCPWLComboBox()->SetEditSelection(0, 1);
105 EXPECT_STREQ(L"A", GetCPWLComboBox()->GetSelectedText().c_str());
106
107 GetCPWLComboBox()->SetEditSelection(0, -1);
108 EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
109 GetCPWLComboBox()->GetSelectedText().c_str());
110
111 GetCPWLComboBox()->SetEditSelection(-8, -1);
112 EXPECT_TRUE(GetCPWLComboBox()->GetSelectedText().IsEmpty());
113
114 GetCPWLComboBox()->SetEditSelection(23, 12);
115 EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLComboBox()->GetSelectedText().c_str());
116
117 GetCPWLComboBox()->SetEditSelection(12, 23);
118 EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLComboBox()->GetSelectedText().c_str());
119
120 GetCPWLComboBox()->SetEditSelection(49, 50);
121 EXPECT_STREQ(L"r", GetCPWLComboBox()->GetSelectedText().c_str());
122
123 GetCPWLComboBox()->SetEditSelection(49, 55);
124 EXPECT_STREQ(L"r", GetCPWLComboBox()->GetSelectedText().c_str());
125 }
126
TEST_F(CPWLComboBoxEditEmbedderTest,DeleteEntireTextSelection)127 TEST_F(CPWLComboBoxEditEmbedderTest, DeleteEntireTextSelection) {
128 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
129 TypeTextIntoTextField(50);
130
131 GetCPWLComboBox()->SetEditSelection(0, -1);
132 EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
133 GetCPWLComboBox()->GetSelectedText().c_str());
134
135 GetCPWLComboBox()->ReplaceSelection(L"");
136 EXPECT_TRUE(GetCPWLComboBox()->GetText().IsEmpty());
137 }
138
TEST_F(CPWLComboBoxEditEmbedderTest,DeleteTextSelectionMiddle)139 TEST_F(CPWLComboBoxEditEmbedderTest, DeleteTextSelectionMiddle) {
140 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
141 TypeTextIntoTextField(50);
142
143 GetCPWLComboBox()->SetEditSelection(12, 23);
144 EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLComboBox()->GetSelectedText().c_str());
145
146 GetCPWLComboBox()->ReplaceSelection(L"");
147 EXPECT_STREQ(L"ABCDEFGHIJKLXYZ[\\]^_`abcdefghijklmnopqr",
148 GetCPWLComboBox()->GetText().c_str());
149 }
150
TEST_F(CPWLComboBoxEditEmbedderTest,DeleteTextSelectionLeft)151 TEST_F(CPWLComboBoxEditEmbedderTest, DeleteTextSelectionLeft) {
152 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
153 TypeTextIntoTextField(50);
154
155 GetCPWLComboBox()->SetEditSelection(0, 5);
156 EXPECT_STREQ(L"ABCDE", GetCPWLComboBox()->GetSelectedText().c_str());
157
158 GetCPWLComboBox()->ReplaceSelection(L"");
159 EXPECT_STREQ(L"FGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
160 GetCPWLComboBox()->GetText().c_str());
161 }
162
TEST_F(CPWLComboBoxEditEmbedderTest,DeleteTextSelectionRight)163 TEST_F(CPWLComboBoxEditEmbedderTest, DeleteTextSelectionRight) {
164 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
165 TypeTextIntoTextField(50);
166
167 GetCPWLComboBox()->SetEditSelection(45, 50);
168 EXPECT_STREQ(L"nopqr", GetCPWLComboBox()->GetSelectedText().c_str());
169
170 GetCPWLComboBox()->ReplaceSelection(L"");
171 EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm",
172 GetCPWLComboBox()->GetText().c_str());
173 }
174
TEST_F(CPWLComboBoxEditEmbedderTest,DeleteEmptyTextSelection)175 TEST_F(CPWLComboBoxEditEmbedderTest, DeleteEmptyTextSelection) {
176 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
177 TypeTextIntoTextField(50);
178
179 GetCPWLComboBox()->ReplaceSelection(L"");
180 EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
181 GetCPWLComboBox()->GetText().c_str());
182 }
183
TEST_F(CPWLComboBoxEditEmbedderTest,InsertTextInEmptyEditableComboBox)184 TEST_F(CPWLComboBoxEditEmbedderTest, InsertTextInEmptyEditableComboBox) {
185 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
186 GetCPWLComboBox()->ReplaceSelection(L"Hello");
187 EXPECT_STREQ(L"Hello", GetCPWLComboBox()->GetText().c_str());
188 }
189
TEST_F(CPWLComboBoxEditEmbedderTest,InsertTextInPopulatedEditableComboBoxLeft)190 TEST_F(CPWLComboBoxEditEmbedderTest,
191 InsertTextInPopulatedEditableComboBoxLeft) {
192 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
193 TypeTextIntoTextField(10);
194
195 // Move cursor to beginning of user-editable combobox text field.
196 EXPECT_TRUE(GetCFFLFormField()->OnKeyDown(FWL_VKEY_Home, {}));
197
198 GetCPWLComboBox()->ReplaceSelection(L"Hello");
199 EXPECT_STREQ(L"HelloABCDEFGHIJ", GetCPWLComboBox()->GetText().c_str());
200 }
201
TEST_F(CPWLComboBoxEditEmbedderTest,InsertTextInPopulatedEditableComboBoxMiddle)202 TEST_F(CPWLComboBoxEditEmbedderTest,
203 InsertTextInPopulatedEditableComboBoxMiddle) {
204 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
205 TypeTextIntoTextField(10);
206
207 // Move cursor to middle of user-editable combobox text field.
208 for (int i = 0; i < 5; ++i) {
209 EXPECT_TRUE(GetCFFLFormField()->OnKeyDown(FWL_VKEY_Left, {}));
210 }
211
212 GetCPWLComboBox()->ReplaceSelection(L"Hello");
213 EXPECT_STREQ(L"ABCDEHelloFGHIJ", GetCPWLComboBox()->GetText().c_str());
214 }
215
TEST_F(CPWLComboBoxEditEmbedderTest,InsertTextInPopulatedEditableComboBoxRight)216 TEST_F(CPWLComboBoxEditEmbedderTest,
217 InsertTextInPopulatedEditableComboBoxRight) {
218 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
219 TypeTextIntoTextField(10);
220
221 GetCPWLComboBox()->ReplaceSelection(L"Hello");
222 EXPECT_STREQ(L"ABCDEFGHIJHello", GetCPWLComboBox()->GetText().c_str());
223 }
224
TEST_F(CPWLComboBoxEditEmbedderTest,InsertTextAndReplaceSelectionInPopulatedEditableComboBoxWhole)225 TEST_F(CPWLComboBoxEditEmbedderTest,
226 InsertTextAndReplaceSelectionInPopulatedEditableComboBoxWhole) {
227 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
228 TypeTextIntoTextField(10);
229
230 GetCPWLComboBox()->SetEditSelection(0, -1);
231 EXPECT_STREQ(L"ABCDEFGHIJ", GetCPWLComboBox()->GetSelectedText().c_str());
232 GetCPWLComboBox()->ReplaceSelection(L"Hello");
233 EXPECT_STREQ(L"Hello", GetCPWLComboBox()->GetText().c_str());
234 }
235
TEST_F(CPWLComboBoxEditEmbedderTest,InsertTextAndReplaceSelectionInPopulatedEditableComboBoxLeft)236 TEST_F(CPWLComboBoxEditEmbedderTest,
237 InsertTextAndReplaceSelectionInPopulatedEditableComboBoxLeft) {
238 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
239 TypeTextIntoTextField(10);
240
241 GetCPWLComboBox()->SetEditSelection(0, 5);
242 EXPECT_STREQ(L"ABCDE", GetCPWLComboBox()->GetSelectedText().c_str());
243 GetCPWLComboBox()->ReplaceSelection(L"Hello");
244 EXPECT_STREQ(L"HelloFGHIJ", GetCPWLComboBox()->GetText().c_str());
245 }
246
TEST_F(CPWLComboBoxEditEmbedderTest,InsertTextAndReplaceSelectionInPopulatedEditableComboBoxMiddle)247 TEST_F(CPWLComboBoxEditEmbedderTest,
248 InsertTextAndReplaceSelectionInPopulatedEditableComboBoxMiddle) {
249 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
250 TypeTextIntoTextField(10);
251
252 GetCPWLComboBox()->SetEditSelection(2, 7);
253 EXPECT_STREQ(L"CDEFG", GetCPWLComboBox()->GetSelectedText().c_str());
254 GetCPWLComboBox()->ReplaceSelection(L"Hello");
255 EXPECT_STREQ(L"ABHelloHIJ", GetCPWLComboBox()->GetText().c_str());
256 }
257
TEST_F(CPWLComboBoxEditEmbedderTest,InsertTextAndReplaceSelectionInPopulatedEditableComboBoxRight)258 TEST_F(CPWLComboBoxEditEmbedderTest,
259 InsertTextAndReplaceSelectionInPopulatedEditableComboBoxRight) {
260 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
261 TypeTextIntoTextField(10);
262
263 GetCPWLComboBox()->SetEditSelection(5, 10);
264 EXPECT_STREQ(L"FGHIJ", GetCPWLComboBox()->GetSelectedText().c_str());
265 GetCPWLComboBox()->ReplaceSelection(L"Hello");
266 EXPECT_STREQ(L"ABCDEHello", GetCPWLComboBox()->GetText().c_str());
267 }
268
TEST_F(CPWLComboBoxEditEmbedderTest,ReplaceAndKeepSelection)269 TEST_F(CPWLComboBoxEditEmbedderTest, ReplaceAndKeepSelection) {
270 FormFillerAndWindowSetup(GetCPDFSDKAnnotUserEditable());
271 TypeTextIntoTextField(10);
272
273 GetCPWLComboBox()->SetEditSelection(1, 3);
274 EXPECT_STREQ(L"ABCDEFGHIJ", GetCPWLComboBox()->GetText().c_str());
275 GetCPWLComboBox()->ReplaceAndKeepSelection(L"xyz");
276 EXPECT_STREQ(L"AxyzDEFGHIJ", GetCPWLComboBox()->GetText().c_str());
277 EXPECT_STREQ(L"xyz", GetCPWLComboBox()->GetSelectedText().c_str());
278
279 GetCPWLComboBox()->SetEditSelection(4, 1);
280 GetCPWLComboBox()->ReplaceAndKeepSelection(L"12");
281 EXPECT_STREQ(L"A12DEFGHIJ", GetCPWLComboBox()->GetText().c_str());
282 EXPECT_STREQ(L"12", GetCPWLComboBox()->GetSelectedText().c_str());
283 }
284