blob: 1505fdc390df1955c709e4d8ba767b4a5de14acc [file] [log] [blame]
Tom Sepeza310e002015-02-27 13:03:07 -08001// 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
Nicolas Pena742977f2017-04-13 15:28:20 -04005#include <memory>
6#include <string>
Diana Gagedce2d722017-06-20 11:17:11 -07007#include <vector>
Nicolas Pena742977f2017-04-13 15:28:20 -04008
Diana Gagedce2d722017-06-20 11:17:11 -07009#include "core/fxcrt/fx_string.h"
Nicolas Pena742977f2017-04-13 15:28:20 -040010#include "core/fxcrt/fx_system.h"
11#include "public/cpp/fpdf_deleters.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080012#include "public/fpdf_formfill.h"
Diana Gagedce2d722017-06-20 11:17:11 -070013#include "public/fpdf_fwlevent.h"
Wei Li091f7a02015-11-09 12:09:55 -080014#include "testing/embedder_test.h"
15#include "testing/embedder_test_mock_delegate.h"
16#include "testing/embedder_test_timer_handling_delegate.h"
Tom Sepeza310e002015-02-27 13:03:07 -080017#include "testing/gmock/include/gmock/gmock.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
20using testing::_;
21using testing::Return;
22
Nicolas Pena3ff54002017-07-05 11:55:35 -040023class FPDFFormFillEmbeddertest : public EmbedderTest {
Diana Gagedce2d722017-06-20 11:17:11 -070024 protected:
Diana Gagef8c02762017-07-26 14:20:12 -070025 void ClickOnFormFieldAtPoint(FPDF_PAGE page, double x, double y) {
26 // Click on the text field or combobox as specified by coordinates.
27 FORM_OnMouseMove(form_handle(), page, 0, x, y);
28 FORM_OnLButtonDown(form_handle(), page, 0, x, y);
29 FORM_OnLButtonUp(form_handle(), page, 0, x, y);
30 }
31
Diana Gage1c7f1422017-07-24 11:19:52 -070032 void TypeTextIntoTextField(FPDF_PAGE page,
Diana Gagecb50b5f2017-06-29 09:54:19 -070033 int num_chars,
34 int form_type,
35 double x,
36 double y) {
37 ASSERT(form_type == FPDF_FORMFIELD_COMBOBOX ||
38 form_type == FPDF_FORMFIELD_TEXTFIELD);
39 EXPECT_EQ(form_type,
40 FPDFPage_HasFormFieldAtPoint(form_handle(), page, x, y));
Diana Gagef8c02762017-07-26 14:20:12 -070041 ClickOnFormFieldAtPoint(page, x, y);
Diana Gagedce2d722017-06-20 11:17:11 -070042
43 // Type text starting with 'A' to as many chars as specified by |num_chars|.
44 for (int i = 0; i < num_chars; ++i) {
45 FORM_OnChar(form_handle(), page, 'A' + i, 0);
46 }
47 }
48
Diana Gagecb50b5f2017-06-29 09:54:19 -070049 // Navigates to text field using the mouse and then selects text via the
Diana Gagedce2d722017-06-20 11:17:11 -070050 // shift and specfied left or right arrow key.
51 void SelectTextWithKeyboard(FPDF_PAGE page,
52 int num_chars,
53 int arrow_key,
54 double x,
55 double y) {
56 // Navigate to starting position for selection.
Diana Gagef8c02762017-07-26 14:20:12 -070057 ClickOnFormFieldAtPoint(page, x, y);
Diana Gagedce2d722017-06-20 11:17:11 -070058
59 // Hold down shift (and don't release until entire text is selected).
60 FORM_OnKeyDown(form_handle(), page, FWL_VKEY_Shift, 0);
61
62 // Select text char by char via left or right arrow key.
63 for (int i = 0; i < num_chars; ++i) {
64 FORM_OnKeyDown(form_handle(), page, arrow_key, FWL_EVENTFLAG_ShiftKey);
65 FORM_OnKeyUp(form_handle(), page, arrow_key, FWL_EVENTFLAG_ShiftKey);
66 }
67 FORM_OnKeyUp(form_handle(), page, FWL_VKEY_Shift, 0);
68 }
69
Diana Gagecb50b5f2017-06-29 09:54:19 -070070 // Uses the mouse to navigate to text field and select text.
Diana Gagedce2d722017-06-20 11:17:11 -070071 void SelectTextWithMouse(FPDF_PAGE page,
72 double start_x,
73 double end_x,
74 double y) {
75 // Navigate to starting position and click mouse.
76 FORM_OnMouseMove(form_handle(), page, 0, start_x, y);
77 FORM_OnLButtonDown(form_handle(), page, 0, start_x, y);
78
79 // Hold down mouse until reach end of desired selection.
80 FORM_OnMouseMove(form_handle(), page, 0, end_x, y);
81 FORM_OnLButtonUp(form_handle(), page, 0, end_x, y);
82 }
83
84 void CheckSelection(FPDF_PAGE page, const CFX_WideString& expected_string) {
85 // Calculate expected length for selected text.
86 int num_chars = expected_string.GetLength();
87
88 // Check actual selection against expected selection.
89 const unsigned long expected_length =
90 sizeof(unsigned short) * (num_chars + 1);
91 unsigned long sel_text_len =
92 FORM_GetSelectedText(form_handle(), page, nullptr, 0);
93 ASSERT_EQ(expected_length, sel_text_len);
94
95 std::vector<unsigned short> buf(sel_text_len);
96 EXPECT_EQ(expected_length, FORM_GetSelectedText(form_handle(), page,
97 buf.data(), sel_text_len));
98
99 EXPECT_EQ(expected_string,
100 CFX_WideString::FromUTF16LE(buf.data(), num_chars));
101 }
Diana Gagecb50b5f2017-06-29 09:54:19 -0700102
103 // Selects one of the pre-selected values from a combobox with three options.
104 // Options are specified by |item_index|, which is 0-based.
105 void SelectOption(FPDF_PAGE page, int32_t item_index, double x, double y) {
106 // Only relevant for comboboxes with three choices and the same dimensions
107 // as those in combobox_form.pdf.
108 ASSERT(item_index >= 0);
109 ASSERT(item_index < 3);
110
111 // Navigate to button for drop down and click mouse to reveal options.
Diana Gagef8c02762017-07-26 14:20:12 -0700112 ClickOnFormFieldAtPoint(page, x, y);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700113
114 // Y coordinate of dropdown option to be selected.
115 constexpr double kChoiceHeight = 15;
116 double option_y = y - kChoiceHeight * (item_index + 1);
117
118 // Navigate to option and click mouse to select it.
Diana Gagef8c02762017-07-26 14:20:12 -0700119 ClickOnFormFieldAtPoint(page, x, option_y);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700120 }
Diana Gagedce2d722017-06-20 11:17:11 -0700121};
Tom Sepeza310e002015-02-27 13:03:07 -0800122
123TEST_F(FPDFFormFillEmbeddertest, FirstTest) {
124 EmbedderTestMockDelegate mock;
125 EXPECT_CALL(mock, Alert(_, _, _, _)).Times(0);
126 EXPECT_CALL(mock, UnsupportedHandler(_)).Times(0);
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700127 EXPECT_CALL(mock, SetTimer(_, _)).Times(0);
128 EXPECT_CALL(mock, KillTimer(_)).Times(0);
Tom Sepeza310e002015-02-27 13:03:07 -0800129 SetDelegate(&mock);
130
Wei Li091f7a02015-11-09 12:09:55 -0800131 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
Tom Sepeza310e002015-02-27 13:03:07 -0800132 FPDF_PAGE page = LoadPage(0);
tsepez8e120292016-08-03 14:03:35 -0700133 EXPECT_TRUE(page);
Lei Zhangd27acae2015-05-15 15:36:02 -0700134 UnloadPage(page);
Tom Sepeza310e002015-02-27 13:03:07 -0800135}
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700136
137TEST_F(FPDFFormFillEmbeddertest, BUG_487928) {
138 EmbedderTestTimerHandlingDelegate delegate;
139 SetDelegate(&delegate);
140
Wei Li091f7a02015-11-09 12:09:55 -0800141 EXPECT_TRUE(OpenDocument("bug_487928.pdf"));
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700142 FPDF_PAGE page = LoadPage(0);
tsepez8e120292016-08-03 14:03:35 -0700143 EXPECT_TRUE(page);
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700144 DoOpenActions();
145 delegate.AdvanceTime(5000);
146 UnloadPage(page);
147}
Tom Sepez0b133982015-07-28 11:23:22 -0700148
Tom Sepez396e8722015-09-09 10:16:08 -0700149TEST_F(FPDFFormFillEmbeddertest, BUG_507316) {
150 EmbedderTestTimerHandlingDelegate delegate;
151 SetDelegate(&delegate);
152
Wei Li091f7a02015-11-09 12:09:55 -0800153 EXPECT_TRUE(OpenDocument("bug_507316.pdf"));
weili0dadcc62016-08-23 21:10:57 -0700154 FPDF_PAGE page = LoadPage(2);
tsepez8e120292016-08-03 14:03:35 -0700155 EXPECT_TRUE(page);
Tom Sepez396e8722015-09-09 10:16:08 -0700156 DoOpenActions();
157 delegate.AdvanceTime(4000);
158 UnloadPage(page);
159}
160
Tom Sepez0b133982015-07-28 11:23:22 -0700161TEST_F(FPDFFormFillEmbeddertest, BUG_514690) {
Wei Li091f7a02015-11-09 12:09:55 -0800162 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
Tom Sepez0b133982015-07-28 11:23:22 -0700163 FPDF_PAGE page = LoadPage(0);
tsepez8e120292016-08-03 14:03:35 -0700164 EXPECT_TRUE(page);
Tom Sepez0b133982015-07-28 11:23:22 -0700165
166 // Test that FORM_OnMouseMove() etc. permit null HANDLES and PAGES.
167 FORM_OnMouseMove(nullptr, page, 0, 10.0, 10.0);
168 FORM_OnMouseMove(form_handle(), nullptr, 0, 10.0, 10.0);
169
170 UnloadPage(page);
171}
Lei Zhang79e893a2015-11-04 16:02:47 -0800172
Tom Sepez4d071792016-02-04 16:53:26 -0800173#ifdef PDF_ENABLE_V8
Lei Zhang79e893a2015-11-04 16:02:47 -0800174TEST_F(FPDFFormFillEmbeddertest, BUG_551248) {
tsepez8e120292016-08-03 14:03:35 -0700175 // Test that timers fire once and intervals fire repeatedly.
Lei Zhang79e893a2015-11-04 16:02:47 -0800176 EmbedderTestTimerHandlingDelegate delegate;
177 SetDelegate(&delegate);
178
Wei Li091f7a02015-11-09 12:09:55 -0800179 EXPECT_TRUE(OpenDocument("bug_551248.pdf"));
Lei Zhang79e893a2015-11-04 16:02:47 -0800180 FPDF_PAGE page = LoadPage(0);
tsepez8e120292016-08-03 14:03:35 -0700181 EXPECT_TRUE(page);
182 DoOpenActions();
183
184 const auto& alerts = delegate.GetAlerts();
185 EXPECT_EQ(0U, alerts.size());
186
187 delegate.AdvanceTime(1000);
188 EXPECT_EQ(0U, alerts.size()); // nothing fired.
189 delegate.AdvanceTime(1000);
190 EXPECT_EQ(1U, alerts.size()); // interval fired.
191 delegate.AdvanceTime(1000);
192 EXPECT_EQ(2U, alerts.size()); // timer fired.
193 delegate.AdvanceTime(1000);
194 EXPECT_EQ(3U, alerts.size()); // interval fired again.
195 delegate.AdvanceTime(1000);
196 EXPECT_EQ(3U, alerts.size()); // nothing fired.
197 delegate.AdvanceTime(1000);
198 EXPECT_EQ(4U, alerts.size()); // interval fired again.
199 delegate.AdvanceTime(1000);
200 EXPECT_EQ(4U, alerts.size()); // nothing fired.
201 UnloadPage(page);
202
203 ASSERT_EQ(4U, alerts.size()); // nothing else fired.
204
205 EXPECT_STREQ(L"interval fired", alerts[0].message.c_str());
206 EXPECT_STREQ(L"Alert", alerts[0].title.c_str());
207 EXPECT_EQ(0, alerts[0].type);
208 EXPECT_EQ(0, alerts[0].icon);
209
210 EXPECT_STREQ(L"timer fired", alerts[1].message.c_str());
211 EXPECT_STREQ(L"Alert", alerts[1].title.c_str());
212 EXPECT_EQ(0, alerts[1].type);
213 EXPECT_EQ(0, alerts[1].icon);
214
215 EXPECT_STREQ(L"interval fired", alerts[2].message.c_str());
216 EXPECT_STREQ(L"Alert", alerts[2].title.c_str());
217 EXPECT_EQ(0, alerts[2].type);
218 EXPECT_EQ(0, alerts[2].icon);
219
220 EXPECT_STREQ(L"interval fired", alerts[3].message.c_str());
221 EXPECT_STREQ(L"Alert", alerts[3].title.c_str());
222 EXPECT_EQ(0, alerts[3].type);
223 EXPECT_EQ(0, alerts[3].icon);
224}
225
226TEST_F(FPDFFormFillEmbeddertest, BUG_620428) {
227 // Test that timers and intervals are cancelable.
228 EmbedderTestTimerHandlingDelegate delegate;
229 SetDelegate(&delegate);
230
231 EXPECT_TRUE(OpenDocument("bug_620428.pdf"));
232 FPDF_PAGE page = LoadPage(0);
233 EXPECT_TRUE(page);
Lei Zhang79e893a2015-11-04 16:02:47 -0800234 DoOpenActions();
235 delegate.AdvanceTime(5000);
236 UnloadPage(page);
237
238 const auto& alerts = delegate.GetAlerts();
tsepez0fa54b82016-08-04 12:07:28 -0700239 ASSERT_EQ(1U, alerts.size());
240 EXPECT_STREQ(L"done", alerts[0].message.c_str());
Lei Zhang79e893a2015-11-04 16:02:47 -0800241}
tsepez8e120292016-08-03 14:03:35 -0700242
tsepez32e693f2016-08-04 12:47:42 -0700243TEST_F(FPDFFormFillEmbeddertest, BUG_634394) {
244 // Cancel timer inside timer callback.
245 EmbedderTestTimerHandlingDelegate delegate;
246 SetDelegate(&delegate);
247
248 EXPECT_TRUE(OpenDocument("bug_634394.pdf"));
249 FPDF_PAGE page = LoadPage(0);
250 EXPECT_TRUE(page);
251 DoOpenActions();
252
253 // Timers fire at most once per AdvanceTime(), allow intervals
254 // to fire several times if possible.
255 delegate.AdvanceTime(1000);
256 delegate.AdvanceTime(1000);
257 delegate.AdvanceTime(1000);
258 delegate.AdvanceTime(1000);
259 delegate.AdvanceTime(1000);
260 UnloadPage(page);
261
262 const auto& alerts = delegate.GetAlerts();
263 EXPECT_EQ(2U, alerts.size());
264}
265
tsepez8ca63de2016-08-05 17:12:27 -0700266TEST_F(FPDFFormFillEmbeddertest, BUG_634716) {
267 EmbedderTestTimerHandlingDelegate delegate;
268 SetDelegate(&delegate);
269
270 EXPECT_TRUE(OpenDocument("bug_634716.pdf"));
271 FPDF_PAGE page = LoadPage(0);
272 EXPECT_TRUE(page);
273 DoOpenActions();
274
275 // Timers fire at most once per AdvanceTime(), allow intervals
276 // to fire several times if possible.
277 delegate.AdvanceTime(1000);
278 delegate.AdvanceTime(1000);
279 delegate.AdvanceTime(1000);
280 delegate.AdvanceTime(1000);
281 delegate.AdvanceTime(1000);
282 UnloadPage(page);
283
284 const auto& alerts = delegate.GetAlerts();
285 EXPECT_EQ(2U, alerts.size());
286}
287
tsepez6cf5eca2017-01-12 11:21:12 -0800288TEST_F(FPDFFormFillEmbeddertest, BUG_679649) {
289 EmbedderTestTimerHandlingDelegate delegate;
290 SetDelegate(&delegate);
291
292 EXPECT_TRUE(OpenDocument("bug_679649.pdf"));
293 FPDF_PAGE page = LoadPage(0);
294 EXPECT_TRUE(page);
295
296 delegate.SetFailNextTimer();
297 DoOpenActions();
298 delegate.AdvanceTime(2000);
299 UnloadPage(page);
300
301 const auto& alerts = delegate.GetAlerts();
302 EXPECT_EQ(0u, alerts.size());
303}
304
Tom Sepezfb7021c2017-05-31 10:29:25 -0700305TEST_F(FPDFFormFillEmbeddertest, BUG_707673) {
306 EmbedderTestTimerHandlingDelegate delegate;
307 SetDelegate(&delegate);
308
309 EXPECT_TRUE(OpenDocument("bug_707673.pdf"));
310 FPDF_PAGE page = LoadPage(0);
311 EXPECT_TRUE(page);
312
313 DoOpenActions();
314 FORM_OnLButtonDown(form_handle(), page, 0, 140, 590);
315 FORM_OnLButtonUp(form_handle(), page, 0, 140, 590);
316 delegate.AdvanceTime(1000);
317 UnloadPage(page);
318
319 const auto& alerts = delegate.GetAlerts();
320 EXPECT_EQ(0u, alerts.size());
321}
322
Tom Sepez4d071792016-02-04 16:53:26 -0800323#endif // PDF_ENABLE_V8
Nicolas Pena742977f2017-04-13 15:28:20 -0400324
325TEST_F(FPDFFormFillEmbeddertest, FormText) {
326#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
327 const char md5_1[] = "5f11dbe575fe197a37c3fb422559f8ff";
328 const char md5_2[] = "35b1a4b679eafc749a0b6fda750c0e8d";
329 const char md5_3[] = "65c64a7c355388f719a752aa1e23f6fe";
330#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400331 const char md5_1[] = "a5e3ac74c2ee123ec6710e2f0ef8424a";
332 const char md5_2[] = "4526b09382e144d5506ad92149399de6";
333 const char md5_3[] = "80356067d860088864cf50ff85d8459e";
Nicolas Pena742977f2017-04-13 15:28:20 -0400334#endif
335 {
336 EXPECT_TRUE(OpenDocument("text_form.pdf"));
337 FPDF_PAGE page = LoadPage(0);
338 ASSERT_TRUE(page);
339 std::unique_ptr<void, FPDFBitmapDeleter> bitmap1(RenderPage(page));
340 CompareBitmap(bitmap1.get(), 300, 300, md5_1);
341
342 // Click on the textfield
343 EXPECT_EQ(FPDF_FORMFIELD_TEXTFIELD,
344 FPDFPage_HasFormFieldAtPoint(form_handle(), page, 120.0, 120.0));
345 FORM_OnMouseMove(form_handle(), page, 0, 120.0, 120.0);
346 FORM_OnLButtonDown(form_handle(), page, 0, 120.0, 120.0);
347 FORM_OnLButtonUp(form_handle(), page, 0, 120.0, 120.0);
348
349 // Write "ABC"
350 FORM_OnChar(form_handle(), page, 65, 0);
351 FORM_OnChar(form_handle(), page, 66, 0);
352 FORM_OnChar(form_handle(), page, 67, 0);
353 std::unique_ptr<void, FPDFBitmapDeleter> bitmap2(RenderPage(page));
354 CompareBitmap(bitmap2.get(), 300, 300, md5_2);
355
356 // Take out focus by clicking out of the textfield
357 FORM_OnMouseMove(form_handle(), page, 0, 15.0, 15.0);
358 FORM_OnLButtonDown(form_handle(), page, 0, 15.0, 15.0);
359 FORM_OnLButtonUp(form_handle(), page, 0, 15.0, 15.0);
360 std::unique_ptr<void, FPDFBitmapDeleter> bitmap3(RenderPage(page));
361 CompareBitmap(bitmap3.get(), 300, 300, md5_3);
362
363 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
364
Nicolas Pena3ff54002017-07-05 11:55:35 -0400365 // Close page
Nicolas Pena742977f2017-04-13 15:28:20 -0400366 UnloadPage(page);
Nicolas Pena742977f2017-04-13 15:28:20 -0400367 }
368 // Check saved document
Nicolas Pena3ff54002017-07-05 11:55:35 -0400369 TestAndCloseSaved(300, 300, md5_3);
Nicolas Pena742977f2017-04-13 15:28:20 -0400370}
Diana Gagedce2d722017-06-20 11:17:11 -0700371
372TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextEmptyAndBasicKeyboard) {
373 // Open file with form text field.
374 EXPECT_TRUE(OpenDocument("text_form.pdf"));
375 FPDF_PAGE page = LoadPage(0);
376 ASSERT_TRUE(page);
377
378 // Test empty selection.
Diana Gage1c7f1422017-07-24 11:19:52 -0700379 CheckSelection(page, CFX_WideString());
Diana Gagedce2d722017-06-20 11:17:11 -0700380
381 // Test basic selection.
Diana Gage1c7f1422017-07-24 11:19:52 -0700382 TypeTextIntoTextField(page, 3, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
Diana Gagedce2d722017-06-20 11:17:11 -0700383 SelectTextWithKeyboard(page, 3, FWL_VKEY_Left, 123.0, 115.5);
384 CheckSelection(page, CFX_WideString(L"ABC"));
385
386 UnloadPage(page);
387}
388
389TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextEmptyAndBasicMouse) {
390 // Open file with form text field.
391 EXPECT_TRUE(OpenDocument("text_form.pdf"));
392 FPDF_PAGE page = LoadPage(0);
393 ASSERT_TRUE(page);
394
395 // Test empty selection.
Diana Gage1c7f1422017-07-24 11:19:52 -0700396 CheckSelection(page, CFX_WideString());
Diana Gagedce2d722017-06-20 11:17:11 -0700397
398 // Test basic selection.
Diana Gage1c7f1422017-07-24 11:19:52 -0700399 TypeTextIntoTextField(page, 3, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
Diana Gagedce2d722017-06-20 11:17:11 -0700400 SelectTextWithMouse(page, 125.0, 102.0, 115.5);
401 CheckSelection(page, CFX_WideString(L"ABC"));
402
403 UnloadPage(page);
404}
405
406TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextFragmentsKeyBoard) {
407 // Open file with form text field.
408 EXPECT_TRUE(OpenDocument("text_form.pdf"));
409 FPDF_PAGE page = LoadPage(0);
410 ASSERT_TRUE(page);
411
Diana Gage1c7f1422017-07-24 11:19:52 -0700412 TypeTextIntoTextField(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
Diana Gagedce2d722017-06-20 11:17:11 -0700413
414 // Test selecting first character in forward direction.
Diana Gagedce2d722017-06-20 11:17:11 -0700415 SelectTextWithKeyboard(page, 1, FWL_VKEY_Right, 102.0, 115.5);
416 CheckSelection(page, CFX_WideString(L"A"));
417
418 // Test selecting entire long string in backwards direction.
419 SelectTextWithKeyboard(page, 12, FWL_VKEY_Left, 191.0, 115.5);
420 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJKL"));
421
422 // Test selecting middle section in backwards direction.
423 SelectTextWithKeyboard(page, 6, FWL_VKEY_Left, 170.0, 115.5);
424 CheckSelection(page, CFX_WideString(L"DEFGHI"));
425
426 // Test selecting middle selection in forward direction.
427 SelectTextWithKeyboard(page, 6, FWL_VKEY_Right, 125.0, 115.5);
428 CheckSelection(page, CFX_WideString(L"DEFGHI"));
429
430 // Test selecting last character in backwards direction.
431 SelectTextWithKeyboard(page, 1, FWL_VKEY_Left, 191.0, 115.5);
432 CheckSelection(page, CFX_WideString(L"L"));
433
434 UnloadPage(page);
435}
436
437TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextFragmentsMouse) {
438 // Open file with form text field.
439 EXPECT_TRUE(OpenDocument("text_form.pdf"));
440 FPDF_PAGE page = LoadPage(0);
441 ASSERT_TRUE(page);
442
Diana Gage1c7f1422017-07-24 11:19:52 -0700443 TypeTextIntoTextField(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
Diana Gagedce2d722017-06-20 11:17:11 -0700444
445 // Test selecting first character in forward direction.
446 SelectTextWithMouse(page, 102.0, 106.0, 115.5);
447 CheckSelection(page, CFX_WideString(L"A"));
448
449 // Test selecting entire long string in backwards direction.
450 SelectTextWithMouse(page, 191.0, 102.0, 115.5);
451 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJKL"));
452
453 // Test selecting middle section in backwards direction.
454 SelectTextWithMouse(page, 170.0, 125.0, 115.5);
455 CheckSelection(page, CFX_WideString(L"DEFGHI"));
456
457 // Test selecting middle selection in forward direction.
458 SelectTextWithMouse(page, 125.0, 170.0, 115.5);
459 CheckSelection(page, CFX_WideString(L"DEFGHI"));
460
461 // Test selecting last character in backwards direction.
462 SelectTextWithMouse(page, 191.0, 186.0, 115.5);
463 CheckSelection(page, CFX_WideString(L"L"));
464
465 UnloadPage(page);
466}
Diana Gagecb50b5f2017-06-29 09:54:19 -0700467
468TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextEmptyAndBasicNormalComboBox) {
469 // Open file with form comboboxes.
470 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
471 FPDF_PAGE page = LoadPage(0);
472 ASSERT_TRUE(page);
473
474 // Test empty selection.
Diana Gage1c7f1422017-07-24 11:19:52 -0700475 CheckSelection(page, CFX_WideString());
Diana Gagecb50b5f2017-06-29 09:54:19 -0700476
477 // Test basic selection of text within normal, non-editable combobox.
478 // Click on normal combobox text field.
479 EXPECT_EQ(FPDF_FORMFIELD_COMBOBOX,
480 FPDFPage_HasFormFieldAtPoint(form_handle(), page, 102.0, 113.0));
481
482 // Non-editable comboboxes don't allow selection with keyboard.
483 SelectTextWithMouse(page, 102.0, 142.0, 113.0);
484 CheckSelection(page, CFX_WideString(L"Banana"));
485
486 // Select other another provided option.
487 SelectOption(page, 0, 192.0, 110.0);
488 CheckSelection(page, CFX_WideString(L"Apple"));
489
490 UnloadPage(page);
491}
492
493TEST_F(FPDFFormFillEmbeddertest,
494 GetSelectedTextEmptyAndBasicEditableComboBoxKeyboard) {
495 // Open file with form comboboxes.
496 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
497 FPDF_PAGE page = LoadPage(0);
498 ASSERT_TRUE(page);
499
500 // Test empty selection.
Diana Gage1c7f1422017-07-24 11:19:52 -0700501 CheckSelection(page, CFX_WideString());
Diana Gagecb50b5f2017-06-29 09:54:19 -0700502
503 // Test basic selection of text within user editable combobox using keyboard.
Diana Gage1c7f1422017-07-24 11:19:52 -0700504 TypeTextIntoTextField(page, 3, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
Diana Gagef8c02762017-07-26 14:20:12 -0700505 SelectTextWithKeyboard(page, 3, FWL_VKEY_Left, 128.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700506 CheckSelection(page, CFX_WideString(L"ABC"));
507
508 // Select a provided option.
509 SelectOption(page, 1, 192.0, 60.0);
510 CheckSelection(page, CFX_WideString(L"Bar"));
511
512 UnloadPage(page);
513}
514
515TEST_F(FPDFFormFillEmbeddertest,
516 GetSelectedTextEmptyAndBasicEditableComboBoxMouse) {
517 // Open file with form comboboxes.
518 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
519 FPDF_PAGE page = LoadPage(0);
520 ASSERT_TRUE(page);
521
522 // Test empty selection.
Diana Gage1c7f1422017-07-24 11:19:52 -0700523 CheckSelection(page, CFX_WideString());
Diana Gagecb50b5f2017-06-29 09:54:19 -0700524
525 // Test basic selection of text within user editable combobox using mouse.
Diana Gage1c7f1422017-07-24 11:19:52 -0700526 TypeTextIntoTextField(page, 3, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
Diana Gagef8c02762017-07-26 14:20:12 -0700527 SelectTextWithMouse(page, 128.0, 103.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700528 CheckSelection(page, CFX_WideString(L"ABC"));
529
530 // Select a provided option.
531 SelectOption(page, 2, 192.0, 60.0);
532 CheckSelection(page, CFX_WideString(L"Qux"));
533
534 UnloadPage(page);
535}
536
537TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextFragmentsNormalComboBox) {
538 // Open file with form comboboxes.
539 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
540 FPDF_PAGE page = LoadPage(0);
541 ASSERT_TRUE(page);
542
543 // Click on normal combobox text field.
544 EXPECT_EQ(FPDF_FORMFIELD_COMBOBOX,
545 FPDFPage_HasFormFieldAtPoint(form_handle(), page, 102.0, 113.0));
546
547 // Test selecting first character in forward direction.
548 SelectTextWithMouse(page, 102.0, 107.0, 113.0);
549 CheckSelection(page, CFX_WideString(L"B"));
550
551 // Test selecting entire string in backwards direction.
552 SelectTextWithMouse(page, 142.0, 102.0, 113.0);
553 CheckSelection(page, CFX_WideString(L"Banana"));
554
555 // Test selecting middle section in backwards direction.
556 SelectTextWithMouse(page, 135.0, 117.0, 113.0);
557 CheckSelection(page, CFX_WideString(L"nan"));
558
559 // Test selecting middle section in forward direction.
560 SelectTextWithMouse(page, 117.0, 135.0, 113.0);
561 CheckSelection(page, CFX_WideString(L"nan"));
562
563 // Test selecting last character in backwards direction.
564 SelectTextWithMouse(page, 142.0, 138.0, 113.0);
565 CheckSelection(page, CFX_WideString(L"a"));
566
567 // Select another option and then reset selection as first three chars.
568 SelectOption(page, 2, 192.0, 110.0);
569 CheckSelection(page, CFX_WideString(L"Cherry"));
570 SelectTextWithMouse(page, 102.0, 122.0, 113.0);
571 CheckSelection(page, CFX_WideString(L"Che"));
572
573 UnloadPage(page);
574}
575
576TEST_F(FPDFFormFillEmbeddertest,
577 GetSelectedTextFragmentsEditableComboBoxKeyboard) {
578 // Open file with form comboboxes.
579 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
580 FPDF_PAGE page = LoadPage(0);
581 ASSERT_TRUE(page);
582
Diana Gage1c7f1422017-07-24 11:19:52 -0700583 TypeTextIntoTextField(page, 10, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700584
585 // Test selecting first character in forward direction.
Diana Gagef8c02762017-07-26 14:20:12 -0700586 SelectTextWithKeyboard(page, 1, FWL_VKEY_Right, 102.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700587 CheckSelection(page, CFX_WideString(L"A"));
588
589 // Test selecting entire long string in backwards direction.
Diana Gagef8c02762017-07-26 14:20:12 -0700590 SelectTextWithKeyboard(page, 10, FWL_VKEY_Left, 178.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700591 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJ"));
592
593 // Test selecting middle section in backwards direction.
Diana Gagef8c02762017-07-26 14:20:12 -0700594 SelectTextWithKeyboard(page, 5, FWL_VKEY_Left, 168.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700595 CheckSelection(page, CFX_WideString(L"DEFGH"));
596
597 // Test selecting middle selection in forward direction.
Diana Gagef8c02762017-07-26 14:20:12 -0700598 SelectTextWithKeyboard(page, 5, FWL_VKEY_Right, 127.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700599 CheckSelection(page, CFX_WideString(L"DEFGH"));
600
601 // Test selecting last character in backwards direction.
Diana Gagef8c02762017-07-26 14:20:12 -0700602 SelectTextWithKeyboard(page, 1, FWL_VKEY_Left, 178.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700603 CheckSelection(page, CFX_WideString(L"J"));
604
605 // Select a provided option and then reset selection as first two chars.
606 SelectOption(page, 0, 192.0, 60.0);
607 CheckSelection(page, CFX_WideString(L"Foo"));
Diana Gagef8c02762017-07-26 14:20:12 -0700608 SelectTextWithKeyboard(page, 2, FWL_VKEY_Right, 102.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700609 CheckSelection(page, CFX_WideString(L"Fo"));
610
611 UnloadPage(page);
612}
613
614TEST_F(FPDFFormFillEmbeddertest,
615 GetSelectedTextFragmentsEditableComboBoxMouse) {
616 // Open file with form comboboxes.
617 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
618 FPDF_PAGE page = LoadPage(0);
619 ASSERT_TRUE(page);
620
Diana Gage1c7f1422017-07-24 11:19:52 -0700621 TypeTextIntoTextField(page, 10, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700622
623 // Test selecting first character in forward direction.
Diana Gagef8c02762017-07-26 14:20:12 -0700624 SelectTextWithMouse(page, 102.0, 107.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700625 CheckSelection(page, CFX_WideString(L"A"));
626
627 // Test selecting entire long string in backwards direction.
Diana Gagef8c02762017-07-26 14:20:12 -0700628 SelectTextWithMouse(page, 178.0, 102.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700629 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJ"));
630
631 // Test selecting middle section in backwards direction.
Diana Gagef8c02762017-07-26 14:20:12 -0700632 SelectTextWithMouse(page, 168.0, 127.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700633 CheckSelection(page, CFX_WideString(L"DEFGH"));
634
635 // Test selecting middle selection in forward direction.
Diana Gagef8c02762017-07-26 14:20:12 -0700636 SelectTextWithMouse(page, 127.0, 168.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700637 CheckSelection(page, CFX_WideString(L"DEFGH"));
638
639 // Test selecting last character in backwards direction.
Diana Gagef8c02762017-07-26 14:20:12 -0700640 SelectTextWithMouse(page, 178.0, 174.0, 62.0);
Diana Gagecb50b5f2017-06-29 09:54:19 -0700641 CheckSelection(page, CFX_WideString(L"J"));
642
643 UnloadPage(page);
644}
Diana Gage1c7f1422017-07-24 11:19:52 -0700645
646TEST_F(FPDFFormFillEmbeddertest, DeleteTextFieldEntireSelection) {
647 // Open file with form text field.
648 EXPECT_TRUE(OpenDocument("text_form.pdf"));
649 FPDF_PAGE page = LoadPage(0);
650 ASSERT_TRUE(page);
651
652 // Select entire contents of text field.
653 TypeTextIntoTextField(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
654 SelectTextWithMouse(page, 191.0, 102.0, 115.5);
655 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJKL"));
656
657 // Test deleting current text selection. Select what remains after deletion to
658 // check that remaining text is as expected.
659 FORM_DeleteSelectedText(form_handle(), page);
660 SelectTextWithKeyboard(page, 12, FWL_VKEY_Left, 191.0, 115.5);
661 CheckSelection(page, CFX_WideString());
662
663 UnloadPage(page);
664}
665
666TEST_F(FPDFFormFillEmbeddertest, DeleteTextFieldSelectionMiddle) {
667 // Open file with form text field.
668 EXPECT_TRUE(OpenDocument("text_form.pdf"));
669 FPDF_PAGE page = LoadPage(0);
670 ASSERT_TRUE(page);
671
672 // Select middle section of text.
673 TypeTextIntoTextField(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
674 SelectTextWithMouse(page, 170.0, 125.0, 115.5);
675 CheckSelection(page, CFX_WideString(L"DEFGHI"));
676
677 // Test deleting current text selection. Select what remains after deletion to
678 // check that remaining text is as expected.
679 FORM_DeleteSelectedText(form_handle(), page);
680 SelectTextWithKeyboard(page, 12, FWL_VKEY_Left, 191.0, 115.5);
681 CheckSelection(page, CFX_WideString(L"ABCJKL"));
682
683 UnloadPage(page);
684}
685
686TEST_F(FPDFFormFillEmbeddertest, DeleteTextFieldSelectionLeft) {
687 // Open file with form text field.
688 EXPECT_TRUE(OpenDocument("text_form.pdf"));
689 FPDF_PAGE page = LoadPage(0);
690 ASSERT_TRUE(page);
691
692 // Select first few characters of text.
693 TypeTextIntoTextField(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
694 SelectTextWithMouse(page, 102.0, 132.0, 115.5);
695 CheckSelection(page, CFX_WideString(L"ABCD"));
696
697 // Test deleting current text selection. Select what remains after deletion to
698 // check that remaining text is as expected.
699 FORM_DeleteSelectedText(form_handle(), page);
700 SelectTextWithKeyboard(page, 12, FWL_VKEY_Left, 191.0, 115.5);
701 CheckSelection(page, CFX_WideString(L"EFGHIJKL"));
702
703 UnloadPage(page);
704}
705
706TEST_F(FPDFFormFillEmbeddertest, DeleteTextFieldSelectionRight) {
707 // Open file with form text field.
708 EXPECT_TRUE(OpenDocument("text_form.pdf"));
709 FPDF_PAGE page = LoadPage(0);
710 ASSERT_TRUE(page);
711
712 // Select last few characters of text.
713 TypeTextIntoTextField(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
714 SelectTextWithMouse(page, 191.0, 165.0, 115.5);
715 CheckSelection(page, CFX_WideString(L"IJKL"));
716
717 // Test deleting current text selection. Select what remains after deletion to
718 // check that remaining text is as expected.
719 FORM_DeleteSelectedText(form_handle(), page);
720 SelectTextWithKeyboard(page, 12, FWL_VKEY_Left, 191.0, 115.5);
721 CheckSelection(page, CFX_WideString(L"ABCDEFGH"));
722
723 UnloadPage(page);
724}
725
726TEST_F(FPDFFormFillEmbeddertest, DeleteEmptyTextFieldSelection) {
727 // Open file with form text field.
728 EXPECT_TRUE(OpenDocument("text_form.pdf"));
729 FPDF_PAGE page = LoadPage(0);
730 ASSERT_TRUE(page);
731
732 // Do not select text.
733 TypeTextIntoTextField(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
734 CheckSelection(page, CFX_WideString());
735
736 // Test that attempt to delete empty text selection has no effect.
737 FORM_DeleteSelectedText(form_handle(), page);
738 SelectTextWithKeyboard(page, 12, FWL_VKEY_Left, 191.0, 115.5);
739 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJKL"));
740
741 UnloadPage(page);
742}
743
744TEST_F(FPDFFormFillEmbeddertest, DeleteEditableComboBoxEntireSelection) {
745 // Open file with form comboboxes.
746 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
747 FPDF_PAGE page = LoadPage(0);
748 ASSERT_TRUE(page);
749
750 // Select entire contents of user-editable combobox text field.
751 TypeTextIntoTextField(page, 10, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
Diana Gagef8c02762017-07-26 14:20:12 -0700752 SelectTextWithMouse(page, 178.0, 102.0, 62.0);
Diana Gage1c7f1422017-07-24 11:19:52 -0700753 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJ"));
754
755 // Test deleting current text selection. Select what remains after deletion to
756 // check that remaining text is as expected.
757 FORM_DeleteSelectedText(form_handle(), page);
Diana Gagef8c02762017-07-26 14:20:12 -0700758 SelectTextWithMouse(page, 178.0, 102.0, 62.0);
Diana Gage1c7f1422017-07-24 11:19:52 -0700759 CheckSelection(page, CFX_WideString());
760
761 UnloadPage(page);
762}
763
764TEST_F(FPDFFormFillEmbeddertest, DeleteEditableComboBoxSelectionMiddle) {
765 // Open file with form comboboxes.
766 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
767 FPDF_PAGE page = LoadPage(0);
768 ASSERT_TRUE(page);
769
770 // Select middle section of text.
771 TypeTextIntoTextField(page, 10, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
Diana Gagef8c02762017-07-26 14:20:12 -0700772 SelectTextWithMouse(page, 168.0, 127.0, 62.0);
Diana Gage1c7f1422017-07-24 11:19:52 -0700773 CheckSelection(page, CFX_WideString(L"DEFGH"));
774
775 // Test deleting current text selection. Select what remains after deletion to
776 // check that remaining text is as expected.
777 FORM_DeleteSelectedText(form_handle(), page);
Diana Gagef8c02762017-07-26 14:20:12 -0700778 SelectTextWithMouse(page, 178.0, 102.0, 62.0);
Diana Gage1c7f1422017-07-24 11:19:52 -0700779 CheckSelection(page, CFX_WideString(L"ABCIJ"));
780
781 UnloadPage(page);
782}
783
784TEST_F(FPDFFormFillEmbeddertest, DeleteEditableComboBoxSelectionLeft) {
785 // Open file with form comboboxes.
786 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
787 FPDF_PAGE page = LoadPage(0);
788 ASSERT_TRUE(page);
789
790 // Select first few characters of text.
791 TypeTextIntoTextField(page, 10, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
Diana Gagef8c02762017-07-26 14:20:12 -0700792 SelectTextWithMouse(page, 102.0, 132.0, 62.0);
Diana Gage1c7f1422017-07-24 11:19:52 -0700793 CheckSelection(page, CFX_WideString(L"ABCD"));
794
795 // Test deleting current text selection. Select what remains after deletion to
796 // check that remaining text is as expected.
797 FORM_DeleteSelectedText(form_handle(), page);
Diana Gagef8c02762017-07-26 14:20:12 -0700798 SelectTextWithMouse(page, 178.0, 102.0, 62.0);
Diana Gage1c7f1422017-07-24 11:19:52 -0700799 CheckSelection(page, CFX_WideString(L"EFGHIJ"));
800
801 UnloadPage(page);
802}
803
804TEST_F(FPDFFormFillEmbeddertest, DeleteEditableComboBoxSelectionRight) {
805 // Open file with form comboboxes.
806 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
807 FPDF_PAGE page = LoadPage(0);
808 ASSERT_TRUE(page);
809
810 // Select last few characters of text.
811 TypeTextIntoTextField(page, 10, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
Diana Gagef8c02762017-07-26 14:20:12 -0700812 SelectTextWithMouse(page, 178.0, 152.0, 62.0);
Diana Gage1c7f1422017-07-24 11:19:52 -0700813 CheckSelection(page, CFX_WideString(L"GHIJ"));
814
815 // Test deleting current text selection. Select what remains after deletion to
816 // check that remaining text is as expected.
817 FORM_DeleteSelectedText(form_handle(), page);
Diana Gagef8c02762017-07-26 14:20:12 -0700818 SelectTextWithMouse(page, 178.0, 102.0, 62.0);
Diana Gage1c7f1422017-07-24 11:19:52 -0700819 CheckSelection(page, CFX_WideString(L"ABCDEF"));
820
821 UnloadPage(page);
822}
823
824TEST_F(FPDFFormFillEmbeddertest, DeleteEmptyEditableComboBoxSelection) {
825 // Open file with form comboboxes.
826 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
827 FPDF_PAGE page = LoadPage(0);
828 ASSERT_TRUE(page);
829
830 // Do not select text.
831 TypeTextIntoTextField(page, 10, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
832 CheckSelection(page, CFX_WideString());
833
834 // Test that attempt to delete empty text selection has no effect.
835 FORM_DeleteSelectedText(form_handle(), page);
Diana Gagef8c02762017-07-26 14:20:12 -0700836 SelectTextWithMouse(page, 178.0, 102.0, 62.0);
Diana Gage1c7f1422017-07-24 11:19:52 -0700837 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJ"));
838
839 UnloadPage(page);
840}