blob: 70899276b3c10cb80cb605a0ac90de7be30f3fa3 [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 Gagecb50b5f2017-06-29 09:54:19 -070025 void TypeTextIntoTextfield(FPDF_PAGE page,
26 int num_chars,
27 int form_type,
28 double x,
29 double y) {
30 ASSERT(form_type == FPDF_FORMFIELD_COMBOBOX ||
31 form_type == FPDF_FORMFIELD_TEXTFIELD);
32 EXPECT_EQ(form_type,
33 FPDFPage_HasFormFieldAtPoint(form_handle(), page, x, y));
34
35 // Click on the textfield or combobox text field as specified by
36 // coordinates.
37 FORM_OnMouseMove(form_handle(), page, 0, x, y);
38 FORM_OnLButtonDown(form_handle(), page, 0, x, y);
39 FORM_OnLButtonUp(form_handle(), page, 0, x, y);
Diana Gagedce2d722017-06-20 11:17:11 -070040
41 // Type text starting with 'A' to as many chars as specified by |num_chars|.
42 for (int i = 0; i < num_chars; ++i) {
43 FORM_OnChar(form_handle(), page, 'A' + i, 0);
44 }
45 }
46
Diana Gagecb50b5f2017-06-29 09:54:19 -070047 // Navigates to text field using the mouse and then selects text via the
Diana Gagedce2d722017-06-20 11:17:11 -070048 // shift and specfied left or right arrow key.
49 void SelectTextWithKeyboard(FPDF_PAGE page,
50 int num_chars,
51 int arrow_key,
52 double x,
53 double y) {
54 // Navigate to starting position for selection.
55 FORM_OnMouseMove(form_handle(), page, 0, x, y);
56 FORM_OnLButtonDown(form_handle(), page, 0, x, y);
57 FORM_OnLButtonUp(form_handle(), page, 0, x, y);
58
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.
112 FORM_OnMouseMove(form_handle(), page, 0, x, y);
113 FORM_OnLButtonDown(form_handle(), page, 0, x, y);
114 FORM_OnLButtonUp(form_handle(), page, 0, x, y);
115
116 // Y coordinate of dropdown option to be selected.
117 constexpr double kChoiceHeight = 15;
118 double option_y = y - kChoiceHeight * (item_index + 1);
119
120 // Navigate to option and click mouse to select it.
121 FORM_OnMouseMove(form_handle(), page, 0, x, option_y);
122 FORM_OnLButtonDown(form_handle(), page, 0, x, option_y);
123 FORM_OnLButtonUp(form_handle(), page, 0, x, option_y);
124 }
Diana Gagedce2d722017-06-20 11:17:11 -0700125};
Tom Sepeza310e002015-02-27 13:03:07 -0800126
127TEST_F(FPDFFormFillEmbeddertest, FirstTest) {
128 EmbedderTestMockDelegate mock;
129 EXPECT_CALL(mock, Alert(_, _, _, _)).Times(0);
130 EXPECT_CALL(mock, UnsupportedHandler(_)).Times(0);
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700131 EXPECT_CALL(mock, SetTimer(_, _)).Times(0);
132 EXPECT_CALL(mock, KillTimer(_)).Times(0);
Tom Sepeza310e002015-02-27 13:03:07 -0800133 SetDelegate(&mock);
134
Wei Li091f7a02015-11-09 12:09:55 -0800135 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
Tom Sepeza310e002015-02-27 13:03:07 -0800136 FPDF_PAGE page = LoadPage(0);
tsepez8e120292016-08-03 14:03:35 -0700137 EXPECT_TRUE(page);
Lei Zhangd27acae2015-05-15 15:36:02 -0700138 UnloadPage(page);
Tom Sepeza310e002015-02-27 13:03:07 -0800139}
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700140
141TEST_F(FPDFFormFillEmbeddertest, BUG_487928) {
142 EmbedderTestTimerHandlingDelegate delegate;
143 SetDelegate(&delegate);
144
Wei Li091f7a02015-11-09 12:09:55 -0800145 EXPECT_TRUE(OpenDocument("bug_487928.pdf"));
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700146 FPDF_PAGE page = LoadPage(0);
tsepez8e120292016-08-03 14:03:35 -0700147 EXPECT_TRUE(page);
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700148 DoOpenActions();
149 delegate.AdvanceTime(5000);
150 UnloadPage(page);
151}
Tom Sepez0b133982015-07-28 11:23:22 -0700152
Tom Sepez396e8722015-09-09 10:16:08 -0700153TEST_F(FPDFFormFillEmbeddertest, BUG_507316) {
154 EmbedderTestTimerHandlingDelegate delegate;
155 SetDelegate(&delegate);
156
Wei Li091f7a02015-11-09 12:09:55 -0800157 EXPECT_TRUE(OpenDocument("bug_507316.pdf"));
weili0dadcc62016-08-23 21:10:57 -0700158 FPDF_PAGE page = LoadPage(2);
tsepez8e120292016-08-03 14:03:35 -0700159 EXPECT_TRUE(page);
Tom Sepez396e8722015-09-09 10:16:08 -0700160 DoOpenActions();
161 delegate.AdvanceTime(4000);
162 UnloadPage(page);
163}
164
Tom Sepez0b133982015-07-28 11:23:22 -0700165TEST_F(FPDFFormFillEmbeddertest, BUG_514690) {
Wei Li091f7a02015-11-09 12:09:55 -0800166 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
Tom Sepez0b133982015-07-28 11:23:22 -0700167 FPDF_PAGE page = LoadPage(0);
tsepez8e120292016-08-03 14:03:35 -0700168 EXPECT_TRUE(page);
Tom Sepez0b133982015-07-28 11:23:22 -0700169
170 // Test that FORM_OnMouseMove() etc. permit null HANDLES and PAGES.
171 FORM_OnMouseMove(nullptr, page, 0, 10.0, 10.0);
172 FORM_OnMouseMove(form_handle(), nullptr, 0, 10.0, 10.0);
173
174 UnloadPage(page);
175}
Lei Zhang79e893a2015-11-04 16:02:47 -0800176
Tom Sepez4d071792016-02-04 16:53:26 -0800177#ifdef PDF_ENABLE_V8
Lei Zhang79e893a2015-11-04 16:02:47 -0800178TEST_F(FPDFFormFillEmbeddertest, BUG_551248) {
tsepez8e120292016-08-03 14:03:35 -0700179 // Test that timers fire once and intervals fire repeatedly.
Lei Zhang79e893a2015-11-04 16:02:47 -0800180 EmbedderTestTimerHandlingDelegate delegate;
181 SetDelegate(&delegate);
182
Wei Li091f7a02015-11-09 12:09:55 -0800183 EXPECT_TRUE(OpenDocument("bug_551248.pdf"));
Lei Zhang79e893a2015-11-04 16:02:47 -0800184 FPDF_PAGE page = LoadPage(0);
tsepez8e120292016-08-03 14:03:35 -0700185 EXPECT_TRUE(page);
186 DoOpenActions();
187
188 const auto& alerts = delegate.GetAlerts();
189 EXPECT_EQ(0U, alerts.size());
190
191 delegate.AdvanceTime(1000);
192 EXPECT_EQ(0U, alerts.size()); // nothing fired.
193 delegate.AdvanceTime(1000);
194 EXPECT_EQ(1U, alerts.size()); // interval fired.
195 delegate.AdvanceTime(1000);
196 EXPECT_EQ(2U, alerts.size()); // timer fired.
197 delegate.AdvanceTime(1000);
198 EXPECT_EQ(3U, alerts.size()); // interval fired again.
199 delegate.AdvanceTime(1000);
200 EXPECT_EQ(3U, alerts.size()); // nothing fired.
201 delegate.AdvanceTime(1000);
202 EXPECT_EQ(4U, alerts.size()); // interval fired again.
203 delegate.AdvanceTime(1000);
204 EXPECT_EQ(4U, alerts.size()); // nothing fired.
205 UnloadPage(page);
206
207 ASSERT_EQ(4U, alerts.size()); // nothing else fired.
208
209 EXPECT_STREQ(L"interval fired", alerts[0].message.c_str());
210 EXPECT_STREQ(L"Alert", alerts[0].title.c_str());
211 EXPECT_EQ(0, alerts[0].type);
212 EXPECT_EQ(0, alerts[0].icon);
213
214 EXPECT_STREQ(L"timer fired", alerts[1].message.c_str());
215 EXPECT_STREQ(L"Alert", alerts[1].title.c_str());
216 EXPECT_EQ(0, alerts[1].type);
217 EXPECT_EQ(0, alerts[1].icon);
218
219 EXPECT_STREQ(L"interval fired", alerts[2].message.c_str());
220 EXPECT_STREQ(L"Alert", alerts[2].title.c_str());
221 EXPECT_EQ(0, alerts[2].type);
222 EXPECT_EQ(0, alerts[2].icon);
223
224 EXPECT_STREQ(L"interval fired", alerts[3].message.c_str());
225 EXPECT_STREQ(L"Alert", alerts[3].title.c_str());
226 EXPECT_EQ(0, alerts[3].type);
227 EXPECT_EQ(0, alerts[3].icon);
228}
229
230TEST_F(FPDFFormFillEmbeddertest, BUG_620428) {
231 // Test that timers and intervals are cancelable.
232 EmbedderTestTimerHandlingDelegate delegate;
233 SetDelegate(&delegate);
234
235 EXPECT_TRUE(OpenDocument("bug_620428.pdf"));
236 FPDF_PAGE page = LoadPage(0);
237 EXPECT_TRUE(page);
Lei Zhang79e893a2015-11-04 16:02:47 -0800238 DoOpenActions();
239 delegate.AdvanceTime(5000);
240 UnloadPage(page);
241
242 const auto& alerts = delegate.GetAlerts();
tsepez0fa54b82016-08-04 12:07:28 -0700243 ASSERT_EQ(1U, alerts.size());
244 EXPECT_STREQ(L"done", alerts[0].message.c_str());
Lei Zhang79e893a2015-11-04 16:02:47 -0800245}
tsepez8e120292016-08-03 14:03:35 -0700246
tsepez32e693f2016-08-04 12:47:42 -0700247TEST_F(FPDFFormFillEmbeddertest, BUG_634394) {
248 // Cancel timer inside timer callback.
249 EmbedderTestTimerHandlingDelegate delegate;
250 SetDelegate(&delegate);
251
252 EXPECT_TRUE(OpenDocument("bug_634394.pdf"));
253 FPDF_PAGE page = LoadPage(0);
254 EXPECT_TRUE(page);
255 DoOpenActions();
256
257 // Timers fire at most once per AdvanceTime(), allow intervals
258 // to fire several times if possible.
259 delegate.AdvanceTime(1000);
260 delegate.AdvanceTime(1000);
261 delegate.AdvanceTime(1000);
262 delegate.AdvanceTime(1000);
263 delegate.AdvanceTime(1000);
264 UnloadPage(page);
265
266 const auto& alerts = delegate.GetAlerts();
267 EXPECT_EQ(2U, alerts.size());
268}
269
tsepez8ca63de2016-08-05 17:12:27 -0700270TEST_F(FPDFFormFillEmbeddertest, BUG_634716) {
271 EmbedderTestTimerHandlingDelegate delegate;
272 SetDelegate(&delegate);
273
274 EXPECT_TRUE(OpenDocument("bug_634716.pdf"));
275 FPDF_PAGE page = LoadPage(0);
276 EXPECT_TRUE(page);
277 DoOpenActions();
278
279 // Timers fire at most once per AdvanceTime(), allow intervals
280 // to fire several times if possible.
281 delegate.AdvanceTime(1000);
282 delegate.AdvanceTime(1000);
283 delegate.AdvanceTime(1000);
284 delegate.AdvanceTime(1000);
285 delegate.AdvanceTime(1000);
286 UnloadPage(page);
287
288 const auto& alerts = delegate.GetAlerts();
289 EXPECT_EQ(2U, alerts.size());
290}
291
tsepez6cf5eca2017-01-12 11:21:12 -0800292TEST_F(FPDFFormFillEmbeddertest, BUG_679649) {
293 EmbedderTestTimerHandlingDelegate delegate;
294 SetDelegate(&delegate);
295
296 EXPECT_TRUE(OpenDocument("bug_679649.pdf"));
297 FPDF_PAGE page = LoadPage(0);
298 EXPECT_TRUE(page);
299
300 delegate.SetFailNextTimer();
301 DoOpenActions();
302 delegate.AdvanceTime(2000);
303 UnloadPage(page);
304
305 const auto& alerts = delegate.GetAlerts();
306 EXPECT_EQ(0u, alerts.size());
307}
308
Tom Sepezfb7021c2017-05-31 10:29:25 -0700309TEST_F(FPDFFormFillEmbeddertest, BUG_707673) {
310 EmbedderTestTimerHandlingDelegate delegate;
311 SetDelegate(&delegate);
312
313 EXPECT_TRUE(OpenDocument("bug_707673.pdf"));
314 FPDF_PAGE page = LoadPage(0);
315 EXPECT_TRUE(page);
316
317 DoOpenActions();
318 FORM_OnLButtonDown(form_handle(), page, 0, 140, 590);
319 FORM_OnLButtonUp(form_handle(), page, 0, 140, 590);
320 delegate.AdvanceTime(1000);
321 UnloadPage(page);
322
323 const auto& alerts = delegate.GetAlerts();
324 EXPECT_EQ(0u, alerts.size());
325}
326
Tom Sepez4d071792016-02-04 16:53:26 -0800327#endif // PDF_ENABLE_V8
Nicolas Pena742977f2017-04-13 15:28:20 -0400328
329TEST_F(FPDFFormFillEmbeddertest, FormText) {
330#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
331 const char md5_1[] = "5f11dbe575fe197a37c3fb422559f8ff";
332 const char md5_2[] = "35b1a4b679eafc749a0b6fda750c0e8d";
333 const char md5_3[] = "65c64a7c355388f719a752aa1e23f6fe";
334#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400335 const char md5_1[] = "a5e3ac74c2ee123ec6710e2f0ef8424a";
336 const char md5_2[] = "4526b09382e144d5506ad92149399de6";
337 const char md5_3[] = "80356067d860088864cf50ff85d8459e";
Nicolas Pena742977f2017-04-13 15:28:20 -0400338#endif
339 {
340 EXPECT_TRUE(OpenDocument("text_form.pdf"));
341 FPDF_PAGE page = LoadPage(0);
342 ASSERT_TRUE(page);
343 std::unique_ptr<void, FPDFBitmapDeleter> bitmap1(RenderPage(page));
344 CompareBitmap(bitmap1.get(), 300, 300, md5_1);
345
346 // Click on the textfield
347 EXPECT_EQ(FPDF_FORMFIELD_TEXTFIELD,
348 FPDFPage_HasFormFieldAtPoint(form_handle(), page, 120.0, 120.0));
349 FORM_OnMouseMove(form_handle(), page, 0, 120.0, 120.0);
350 FORM_OnLButtonDown(form_handle(), page, 0, 120.0, 120.0);
351 FORM_OnLButtonUp(form_handle(), page, 0, 120.0, 120.0);
352
353 // Write "ABC"
354 FORM_OnChar(form_handle(), page, 65, 0);
355 FORM_OnChar(form_handle(), page, 66, 0);
356 FORM_OnChar(form_handle(), page, 67, 0);
357 std::unique_ptr<void, FPDFBitmapDeleter> bitmap2(RenderPage(page));
358 CompareBitmap(bitmap2.get(), 300, 300, md5_2);
359
360 // Take out focus by clicking out of the textfield
361 FORM_OnMouseMove(form_handle(), page, 0, 15.0, 15.0);
362 FORM_OnLButtonDown(form_handle(), page, 0, 15.0, 15.0);
363 FORM_OnLButtonUp(form_handle(), page, 0, 15.0, 15.0);
364 std::unique_ptr<void, FPDFBitmapDeleter> bitmap3(RenderPage(page));
365 CompareBitmap(bitmap3.get(), 300, 300, md5_3);
366
367 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
368
Nicolas Pena3ff54002017-07-05 11:55:35 -0400369 // Close page
Nicolas Pena742977f2017-04-13 15:28:20 -0400370 UnloadPage(page);
Nicolas Pena742977f2017-04-13 15:28:20 -0400371 }
372 // Check saved document
Nicolas Pena3ff54002017-07-05 11:55:35 -0400373 TestAndCloseSaved(300, 300, md5_3);
Nicolas Pena742977f2017-04-13 15:28:20 -0400374}
Diana Gagedce2d722017-06-20 11:17:11 -0700375
376TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextEmptyAndBasicKeyboard) {
377 // Open file with form text field.
378 EXPECT_TRUE(OpenDocument("text_form.pdf"));
379 FPDF_PAGE page = LoadPage(0);
380 ASSERT_TRUE(page);
381
382 // Test empty selection.
383 CheckSelection(page, CFX_WideString(L""));
384
385 // Test basic selection.
Diana Gagecb50b5f2017-06-29 09:54:19 -0700386 TypeTextIntoTextfield(page, 3, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
Diana Gagedce2d722017-06-20 11:17:11 -0700387 SelectTextWithKeyboard(page, 3, FWL_VKEY_Left, 123.0, 115.5);
388 CheckSelection(page, CFX_WideString(L"ABC"));
389
390 UnloadPage(page);
391}
392
393TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextEmptyAndBasicMouse) {
394 // Open file with form text field.
395 EXPECT_TRUE(OpenDocument("text_form.pdf"));
396 FPDF_PAGE page = LoadPage(0);
397 ASSERT_TRUE(page);
398
399 // Test empty selection.
400 CheckSelection(page, CFX_WideString(L""));
401
402 // Test basic selection.
Diana Gagecb50b5f2017-06-29 09:54:19 -0700403 TypeTextIntoTextfield(page, 3, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
Diana Gagedce2d722017-06-20 11:17:11 -0700404 SelectTextWithMouse(page, 125.0, 102.0, 115.5);
405 CheckSelection(page, CFX_WideString(L"ABC"));
406
407 UnloadPage(page);
408}
409
410TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextFragmentsKeyBoard) {
411 // Open file with form text field.
412 EXPECT_TRUE(OpenDocument("text_form.pdf"));
413 FPDF_PAGE page = LoadPage(0);
414 ASSERT_TRUE(page);
415
Diana Gagecb50b5f2017-06-29 09:54:19 -0700416 TypeTextIntoTextfield(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
Diana Gagedce2d722017-06-20 11:17:11 -0700417
418 // Test selecting first character in forward direction.
Diana Gagedce2d722017-06-20 11:17:11 -0700419 SelectTextWithKeyboard(page, 1, FWL_VKEY_Right, 102.0, 115.5);
420 CheckSelection(page, CFX_WideString(L"A"));
421
422 // Test selecting entire long string in backwards direction.
423 SelectTextWithKeyboard(page, 12, FWL_VKEY_Left, 191.0, 115.5);
424 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJKL"));
425
426 // Test selecting middle section in backwards direction.
427 SelectTextWithKeyboard(page, 6, FWL_VKEY_Left, 170.0, 115.5);
428 CheckSelection(page, CFX_WideString(L"DEFGHI"));
429
430 // Test selecting middle selection in forward direction.
431 SelectTextWithKeyboard(page, 6, FWL_VKEY_Right, 125.0, 115.5);
432 CheckSelection(page, CFX_WideString(L"DEFGHI"));
433
434 // Test selecting last character in backwards direction.
435 SelectTextWithKeyboard(page, 1, FWL_VKEY_Left, 191.0, 115.5);
436 CheckSelection(page, CFX_WideString(L"L"));
437
438 UnloadPage(page);
439}
440
441TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextFragmentsMouse) {
442 // Open file with form text field.
443 EXPECT_TRUE(OpenDocument("text_form.pdf"));
444 FPDF_PAGE page = LoadPage(0);
445 ASSERT_TRUE(page);
446
Diana Gagecb50b5f2017-06-29 09:54:19 -0700447 TypeTextIntoTextfield(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
Diana Gagedce2d722017-06-20 11:17:11 -0700448
449 // Test selecting first character in forward direction.
450 SelectTextWithMouse(page, 102.0, 106.0, 115.5);
451 CheckSelection(page, CFX_WideString(L"A"));
452
453 // Test selecting entire long string in backwards direction.
454 SelectTextWithMouse(page, 191.0, 102.0, 115.5);
455 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJKL"));
456
457 // Test selecting middle section in backwards direction.
458 SelectTextWithMouse(page, 170.0, 125.0, 115.5);
459 CheckSelection(page, CFX_WideString(L"DEFGHI"));
460
461 // Test selecting middle selection in forward direction.
462 SelectTextWithMouse(page, 125.0, 170.0, 115.5);
463 CheckSelection(page, CFX_WideString(L"DEFGHI"));
464
465 // Test selecting last character in backwards direction.
466 SelectTextWithMouse(page, 191.0, 186.0, 115.5);
467 CheckSelection(page, CFX_WideString(L"L"));
468
469 UnloadPage(page);
470}
Diana Gagecb50b5f2017-06-29 09:54:19 -0700471
472TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextEmptyAndBasicNormalComboBox) {
473 // Open file with form comboboxes.
474 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
475 FPDF_PAGE page = LoadPage(0);
476 ASSERT_TRUE(page);
477
478 // Test empty selection.
479 CheckSelection(page, CFX_WideString(L""));
480
481 // Test basic selection of text within normal, non-editable combobox.
482 // Click on normal combobox text field.
483 EXPECT_EQ(FPDF_FORMFIELD_COMBOBOX,
484 FPDFPage_HasFormFieldAtPoint(form_handle(), page, 102.0, 113.0));
485
486 // Non-editable comboboxes don't allow selection with keyboard.
487 SelectTextWithMouse(page, 102.0, 142.0, 113.0);
488 CheckSelection(page, CFX_WideString(L"Banana"));
489
490 // Select other another provided option.
491 SelectOption(page, 0, 192.0, 110.0);
492 CheckSelection(page, CFX_WideString(L"Apple"));
493
494 UnloadPage(page);
495}
496
497TEST_F(FPDFFormFillEmbeddertest,
498 GetSelectedTextEmptyAndBasicEditableComboBoxKeyboard) {
499 // Open file with form comboboxes.
500 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
501 FPDF_PAGE page = LoadPage(0);
502 ASSERT_TRUE(page);
503
504 // Test empty selection.
505 CheckSelection(page, CFX_WideString(L""));
506
507 // Test basic selection of text within user editable combobox using keyboard.
508 TypeTextIntoTextfield(page, 3, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
509 SelectTextWithKeyboard(page, 3, FWL_VKEY_Left, 128.0, 63.0);
510 CheckSelection(page, CFX_WideString(L"ABC"));
511
512 // Select a provided option.
513 SelectOption(page, 1, 192.0, 60.0);
514 CheckSelection(page, CFX_WideString(L"Bar"));
515
516 UnloadPage(page);
517}
518
519TEST_F(FPDFFormFillEmbeddertest,
520 GetSelectedTextEmptyAndBasicEditableComboBoxMouse) {
521 // Open file with form comboboxes.
522 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
523 FPDF_PAGE page = LoadPage(0);
524 ASSERT_TRUE(page);
525
526 // Test empty selection.
527 CheckSelection(page, CFX_WideString(L""));
528
529 // Test basic selection of text within user editable combobox using mouse.
530 TypeTextIntoTextfield(page, 3, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
531 SelectTextWithMouse(page, 128.0, 103.0, 63.0);
532 CheckSelection(page, CFX_WideString(L"ABC"));
533
534 // Select a provided option.
535 SelectOption(page, 2, 192.0, 60.0);
536 CheckSelection(page, CFX_WideString(L"Qux"));
537
538 UnloadPage(page);
539}
540
541TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextFragmentsNormalComboBox) {
542 // Open file with form comboboxes.
543 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
544 FPDF_PAGE page = LoadPage(0);
545 ASSERT_TRUE(page);
546
547 // Click on normal combobox text field.
548 EXPECT_EQ(FPDF_FORMFIELD_COMBOBOX,
549 FPDFPage_HasFormFieldAtPoint(form_handle(), page, 102.0, 113.0));
550
551 // Test selecting first character in forward direction.
552 SelectTextWithMouse(page, 102.0, 107.0, 113.0);
553 CheckSelection(page, CFX_WideString(L"B"));
554
555 // Test selecting entire string in backwards direction.
556 SelectTextWithMouse(page, 142.0, 102.0, 113.0);
557 CheckSelection(page, CFX_WideString(L"Banana"));
558
559 // Test selecting middle section in backwards direction.
560 SelectTextWithMouse(page, 135.0, 117.0, 113.0);
561 CheckSelection(page, CFX_WideString(L"nan"));
562
563 // Test selecting middle section in forward direction.
564 SelectTextWithMouse(page, 117.0, 135.0, 113.0);
565 CheckSelection(page, CFX_WideString(L"nan"));
566
567 // Test selecting last character in backwards direction.
568 SelectTextWithMouse(page, 142.0, 138.0, 113.0);
569 CheckSelection(page, CFX_WideString(L"a"));
570
571 // Select another option and then reset selection as first three chars.
572 SelectOption(page, 2, 192.0, 110.0);
573 CheckSelection(page, CFX_WideString(L"Cherry"));
574 SelectTextWithMouse(page, 102.0, 122.0, 113.0);
575 CheckSelection(page, CFX_WideString(L"Che"));
576
577 UnloadPage(page);
578}
579
580TEST_F(FPDFFormFillEmbeddertest,
581 GetSelectedTextFragmentsEditableComboBoxKeyboard) {
582 // Open file with form comboboxes.
583 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
584 FPDF_PAGE page = LoadPage(0);
585 ASSERT_TRUE(page);
586
587 TypeTextIntoTextfield(page, 10, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
588
589 // Test selecting first character in forward direction.
590 SelectTextWithKeyboard(page, 1, FWL_VKEY_Right, 102.0, 63.0);
591 CheckSelection(page, CFX_WideString(L"A"));
592
593 // Test selecting entire long string in backwards direction.
594 SelectTextWithKeyboard(page, 10, FWL_VKEY_Left, 178.0, 63.0);
595 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJ"));
596
597 // Test selecting middle section in backwards direction.
598 SelectTextWithKeyboard(page, 5, FWL_VKEY_Left, 168.0, 63.0);
599 CheckSelection(page, CFX_WideString(L"DEFGH"));
600
601 // Test selecting middle selection in forward direction.
602 SelectTextWithKeyboard(page, 5, FWL_VKEY_Right, 127.0, 63.0);
603 CheckSelection(page, CFX_WideString(L"DEFGH"));
604
605 // Test selecting last character in backwards direction.
606 SelectTextWithKeyboard(page, 1, FWL_VKEY_Left, 178.0, 63.0);
607 CheckSelection(page, CFX_WideString(L"J"));
608
609 // Select a provided option and then reset selection as first two chars.
610 SelectOption(page, 0, 192.0, 60.0);
611 CheckSelection(page, CFX_WideString(L"Foo"));
612 SelectTextWithKeyboard(page, 2, FWL_VKEY_Right, 102.0, 63.0);
613 CheckSelection(page, CFX_WideString(L"Fo"));
614
615 UnloadPage(page);
616}
617
618TEST_F(FPDFFormFillEmbeddertest,
619 GetSelectedTextFragmentsEditableComboBoxMouse) {
620 // Open file with form comboboxes.
621 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
622 FPDF_PAGE page = LoadPage(0);
623 ASSERT_TRUE(page);
624
625 TypeTextIntoTextfield(page, 10, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
626
627 // Test selecting first character in forward direction.
628 SelectTextWithMouse(page, 102.0, 107.0, 63.0);
629 CheckSelection(page, CFX_WideString(L"A"));
630
631 // Test selecting entire long string in backwards direction.
632 SelectTextWithMouse(page, 178.0, 102.0, 63.0);
633 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJ"));
634
635 // Test selecting middle section in backwards direction.
636 SelectTextWithMouse(page, 168.0, 127.0, 63.0);
637 CheckSelection(page, CFX_WideString(L"DEFGH"));
638
639 // Test selecting middle selection in forward direction.
640 SelectTextWithMouse(page, 127.0, 168.0, 63.0);
641 CheckSelection(page, CFX_WideString(L"DEFGH"));
642
643 // Test selecting last character in backwards direction.
644 SelectTextWithMouse(page, 178.0, 174.0, 63.0);
645 CheckSelection(page, CFX_WideString(L"J"));
646
647 UnloadPage(page);
648}