blob: 7779ed25a7e1b591002ca0d3139823a92b14b4d1 [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
Diana Gagedce2d722017-06-20 11:17:11 -070023class FPDFFormFillEmbeddertest : public EmbedderTest, public TestSaver {
24 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
335 const char md5_1[] = "23baecc6e94d4c8b894cd39aa04c584c";
336 const char md5_2[] = "499df95d477dfe35ee65b823c69743b5";
337 const char md5_3[] = "8f91b62895fc505d9e17ff2d633756d4";
338#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
369 // Close everything
370 UnloadPage(page);
371 FPDFDOC_ExitFormFillEnvironment(form_handle_);
372 FPDF_CloseDocument(document_);
373 }
374 // Check saved document
375 std::string new_file = GetString();
376 FPDF_FILEACCESS file_access;
377 memset(&file_access, 0, sizeof(file_access));
378 file_access.m_FileLen = new_file.size();
379 file_access.m_GetBlock = GetBlockFromString;
380 file_access.m_Param = &new_file;
381 document_ = FPDF_LoadCustomDocument(&file_access, nullptr);
382 SetupFormFillEnvironment();
383 EXPECT_EQ(1, FPDF_GetPageCount(document_));
384 std::unique_ptr<void, FPDFPageDeleter> new_page(FPDF_LoadPage(document_, 0));
385 ASSERT_TRUE(new_page.get());
386 std::unique_ptr<void, FPDFBitmapDeleter> new_bitmap(
387 RenderPage(new_page.get()));
388 CompareBitmap(new_bitmap.get(), 300, 300, md5_3);
389}
Diana Gagedce2d722017-06-20 11:17:11 -0700390
391TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextEmptyAndBasicKeyboard) {
392 // Open file with form text field.
393 EXPECT_TRUE(OpenDocument("text_form.pdf"));
394 FPDF_PAGE page = LoadPage(0);
395 ASSERT_TRUE(page);
396
397 // Test empty selection.
398 CheckSelection(page, CFX_WideString(L""));
399
400 // Test basic selection.
Diana Gagecb50b5f2017-06-29 09:54:19 -0700401 TypeTextIntoTextfield(page, 3, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
Diana Gagedce2d722017-06-20 11:17:11 -0700402 SelectTextWithKeyboard(page, 3, FWL_VKEY_Left, 123.0, 115.5);
403 CheckSelection(page, CFX_WideString(L"ABC"));
404
405 UnloadPage(page);
406}
407
408TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextEmptyAndBasicMouse) {
409 // Open file with form text field.
410 EXPECT_TRUE(OpenDocument("text_form.pdf"));
411 FPDF_PAGE page = LoadPage(0);
412 ASSERT_TRUE(page);
413
414 // Test empty selection.
415 CheckSelection(page, CFX_WideString(L""));
416
417 // Test basic selection.
Diana Gagecb50b5f2017-06-29 09:54:19 -0700418 TypeTextIntoTextfield(page, 3, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
Diana Gagedce2d722017-06-20 11:17:11 -0700419 SelectTextWithMouse(page, 125.0, 102.0, 115.5);
420 CheckSelection(page, CFX_WideString(L"ABC"));
421
422 UnloadPage(page);
423}
424
425TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextFragmentsKeyBoard) {
426 // Open file with form text field.
427 EXPECT_TRUE(OpenDocument("text_form.pdf"));
428 FPDF_PAGE page = LoadPage(0);
429 ASSERT_TRUE(page);
430
Diana Gagecb50b5f2017-06-29 09:54:19 -0700431 TypeTextIntoTextfield(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
Diana Gagedce2d722017-06-20 11:17:11 -0700432
433 // Test selecting first character in forward direction.
Diana Gagedce2d722017-06-20 11:17:11 -0700434 SelectTextWithKeyboard(page, 1, FWL_VKEY_Right, 102.0, 115.5);
435 CheckSelection(page, CFX_WideString(L"A"));
436
437 // Test selecting entire long string in backwards direction.
438 SelectTextWithKeyboard(page, 12, FWL_VKEY_Left, 191.0, 115.5);
439 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJKL"));
440
441 // Test selecting middle section in backwards direction.
442 SelectTextWithKeyboard(page, 6, FWL_VKEY_Left, 170.0, 115.5);
443 CheckSelection(page, CFX_WideString(L"DEFGHI"));
444
445 // Test selecting middle selection in forward direction.
446 SelectTextWithKeyboard(page, 6, FWL_VKEY_Right, 125.0, 115.5);
447 CheckSelection(page, CFX_WideString(L"DEFGHI"));
448
449 // Test selecting last character in backwards direction.
450 SelectTextWithKeyboard(page, 1, FWL_VKEY_Left, 191.0, 115.5);
451 CheckSelection(page, CFX_WideString(L"L"));
452
453 UnloadPage(page);
454}
455
456TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextFragmentsMouse) {
457 // Open file with form text field.
458 EXPECT_TRUE(OpenDocument("text_form.pdf"));
459 FPDF_PAGE page = LoadPage(0);
460 ASSERT_TRUE(page);
461
Diana Gagecb50b5f2017-06-29 09:54:19 -0700462 TypeTextIntoTextfield(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0);
Diana Gagedce2d722017-06-20 11:17:11 -0700463
464 // Test selecting first character in forward direction.
465 SelectTextWithMouse(page, 102.0, 106.0, 115.5);
466 CheckSelection(page, CFX_WideString(L"A"));
467
468 // Test selecting entire long string in backwards direction.
469 SelectTextWithMouse(page, 191.0, 102.0, 115.5);
470 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJKL"));
471
472 // Test selecting middle section in backwards direction.
473 SelectTextWithMouse(page, 170.0, 125.0, 115.5);
474 CheckSelection(page, CFX_WideString(L"DEFGHI"));
475
476 // Test selecting middle selection in forward direction.
477 SelectTextWithMouse(page, 125.0, 170.0, 115.5);
478 CheckSelection(page, CFX_WideString(L"DEFGHI"));
479
480 // Test selecting last character in backwards direction.
481 SelectTextWithMouse(page, 191.0, 186.0, 115.5);
482 CheckSelection(page, CFX_WideString(L"L"));
483
484 UnloadPage(page);
485}
Diana Gagecb50b5f2017-06-29 09:54:19 -0700486
487TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextEmptyAndBasicNormalComboBox) {
488 // Open file with form comboboxes.
489 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
490 FPDF_PAGE page = LoadPage(0);
491 ASSERT_TRUE(page);
492
493 // Test empty selection.
494 CheckSelection(page, CFX_WideString(L""));
495
496 // Test basic selection of text within normal, non-editable combobox.
497 // Click on normal combobox text field.
498 EXPECT_EQ(FPDF_FORMFIELD_COMBOBOX,
499 FPDFPage_HasFormFieldAtPoint(form_handle(), page, 102.0, 113.0));
500
501 // Non-editable comboboxes don't allow selection with keyboard.
502 SelectTextWithMouse(page, 102.0, 142.0, 113.0);
503 CheckSelection(page, CFX_WideString(L"Banana"));
504
505 // Select other another provided option.
506 SelectOption(page, 0, 192.0, 110.0);
507 CheckSelection(page, CFX_WideString(L"Apple"));
508
509 UnloadPage(page);
510}
511
512TEST_F(FPDFFormFillEmbeddertest,
513 GetSelectedTextEmptyAndBasicEditableComboBoxKeyboard) {
514 // Open file with form comboboxes.
515 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
516 FPDF_PAGE page = LoadPage(0);
517 ASSERT_TRUE(page);
518
519 // Test empty selection.
520 CheckSelection(page, CFX_WideString(L""));
521
522 // Test basic selection of text within user editable combobox using keyboard.
523 TypeTextIntoTextfield(page, 3, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
524 SelectTextWithKeyboard(page, 3, FWL_VKEY_Left, 128.0, 63.0);
525 CheckSelection(page, CFX_WideString(L"ABC"));
526
527 // Select a provided option.
528 SelectOption(page, 1, 192.0, 60.0);
529 CheckSelection(page, CFX_WideString(L"Bar"));
530
531 UnloadPage(page);
532}
533
534TEST_F(FPDFFormFillEmbeddertest,
535 GetSelectedTextEmptyAndBasicEditableComboBoxMouse) {
536 // Open file with form comboboxes.
537 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
538 FPDF_PAGE page = LoadPage(0);
539 ASSERT_TRUE(page);
540
541 // Test empty selection.
542 CheckSelection(page, CFX_WideString(L""));
543
544 // Test basic selection of text within user editable combobox using mouse.
545 TypeTextIntoTextfield(page, 3, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
546 SelectTextWithMouse(page, 128.0, 103.0, 63.0);
547 CheckSelection(page, CFX_WideString(L"ABC"));
548
549 // Select a provided option.
550 SelectOption(page, 2, 192.0, 60.0);
551 CheckSelection(page, CFX_WideString(L"Qux"));
552
553 UnloadPage(page);
554}
555
556TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextFragmentsNormalComboBox) {
557 // Open file with form comboboxes.
558 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
559 FPDF_PAGE page = LoadPage(0);
560 ASSERT_TRUE(page);
561
562 // Click on normal combobox text field.
563 EXPECT_EQ(FPDF_FORMFIELD_COMBOBOX,
564 FPDFPage_HasFormFieldAtPoint(form_handle(), page, 102.0, 113.0));
565
566 // Test selecting first character in forward direction.
567 SelectTextWithMouse(page, 102.0, 107.0, 113.0);
568 CheckSelection(page, CFX_WideString(L"B"));
569
570 // Test selecting entire string in backwards direction.
571 SelectTextWithMouse(page, 142.0, 102.0, 113.0);
572 CheckSelection(page, CFX_WideString(L"Banana"));
573
574 // Test selecting middle section in backwards direction.
575 SelectTextWithMouse(page, 135.0, 117.0, 113.0);
576 CheckSelection(page, CFX_WideString(L"nan"));
577
578 // Test selecting middle section in forward direction.
579 SelectTextWithMouse(page, 117.0, 135.0, 113.0);
580 CheckSelection(page, CFX_WideString(L"nan"));
581
582 // Test selecting last character in backwards direction.
583 SelectTextWithMouse(page, 142.0, 138.0, 113.0);
584 CheckSelection(page, CFX_WideString(L"a"));
585
586 // Select another option and then reset selection as first three chars.
587 SelectOption(page, 2, 192.0, 110.0);
588 CheckSelection(page, CFX_WideString(L"Cherry"));
589 SelectTextWithMouse(page, 102.0, 122.0, 113.0);
590 CheckSelection(page, CFX_WideString(L"Che"));
591
592 UnloadPage(page);
593}
594
595TEST_F(FPDFFormFillEmbeddertest,
596 GetSelectedTextFragmentsEditableComboBoxKeyboard) {
597 // Open file with form comboboxes.
598 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
599 FPDF_PAGE page = LoadPage(0);
600 ASSERT_TRUE(page);
601
602 TypeTextIntoTextfield(page, 10, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
603
604 // Test selecting first character in forward direction.
605 SelectTextWithKeyboard(page, 1, FWL_VKEY_Right, 102.0, 63.0);
606 CheckSelection(page, CFX_WideString(L"A"));
607
608 // Test selecting entire long string in backwards direction.
609 SelectTextWithKeyboard(page, 10, FWL_VKEY_Left, 178.0, 63.0);
610 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJ"));
611
612 // Test selecting middle section in backwards direction.
613 SelectTextWithKeyboard(page, 5, FWL_VKEY_Left, 168.0, 63.0);
614 CheckSelection(page, CFX_WideString(L"DEFGH"));
615
616 // Test selecting middle selection in forward direction.
617 SelectTextWithKeyboard(page, 5, FWL_VKEY_Right, 127.0, 63.0);
618 CheckSelection(page, CFX_WideString(L"DEFGH"));
619
620 // Test selecting last character in backwards direction.
621 SelectTextWithKeyboard(page, 1, FWL_VKEY_Left, 178.0, 63.0);
622 CheckSelection(page, CFX_WideString(L"J"));
623
624 // Select a provided option and then reset selection as first two chars.
625 SelectOption(page, 0, 192.0, 60.0);
626 CheckSelection(page, CFX_WideString(L"Foo"));
627 SelectTextWithKeyboard(page, 2, FWL_VKEY_Right, 102.0, 63.0);
628 CheckSelection(page, CFX_WideString(L"Fo"));
629
630 UnloadPage(page);
631}
632
633TEST_F(FPDFFormFillEmbeddertest,
634 GetSelectedTextFragmentsEditableComboBoxMouse) {
635 // Open file with form comboboxes.
636 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
637 FPDF_PAGE page = LoadPage(0);
638 ASSERT_TRUE(page);
639
640 TypeTextIntoTextfield(page, 10, FPDF_FORMFIELD_COMBOBOX, 102.0, 62.0);
641
642 // Test selecting first character in forward direction.
643 SelectTextWithMouse(page, 102.0, 107.0, 63.0);
644 CheckSelection(page, CFX_WideString(L"A"));
645
646 // Test selecting entire long string in backwards direction.
647 SelectTextWithMouse(page, 178.0, 102.0, 63.0);
648 CheckSelection(page, CFX_WideString(L"ABCDEFGHIJ"));
649
650 // Test selecting middle section in backwards direction.
651 SelectTextWithMouse(page, 168.0, 127.0, 63.0);
652 CheckSelection(page, CFX_WideString(L"DEFGH"));
653
654 // Test selecting middle selection in forward direction.
655 SelectTextWithMouse(page, 127.0, 168.0, 63.0);
656 CheckSelection(page, CFX_WideString(L"DEFGH"));
657
658 // Test selecting last character in backwards direction.
659 SelectTextWithMouse(page, 178.0, 174.0, 63.0);
660 CheckSelection(page, CFX_WideString(L"J"));
661
662 UnloadPage(page);
663}