Tom Sepez | a310e00 | 2015-02-27 13:03:07 -0800 | [diff] [blame] | 1 | // 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 Pena | 742977f | 2017-04-13 15:28:20 -0400 | [diff] [blame] | 5 | #include <memory> |
| 6 | #include <string> |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 7 | #include <vector> |
Nicolas Pena | 742977f | 2017-04-13 15:28:20 -0400 | [diff] [blame] | 8 | |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 9 | #include "core/fxcrt/fx_string.h" |
Nicolas Pena | 742977f | 2017-04-13 15:28:20 -0400 | [diff] [blame] | 10 | #include "core/fxcrt/fx_system.h" |
| 11 | #include "public/cpp/fpdf_deleters.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 12 | #include "public/fpdf_formfill.h" |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 13 | #include "public/fpdf_fwlevent.h" |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 14 | #include "testing/embedder_test.h" |
| 15 | #include "testing/embedder_test_mock_delegate.h" |
| 16 | #include "testing/embedder_test_timer_handling_delegate.h" |
Tom Sepez | a310e00 | 2015-02-27 13:03:07 -0800 | [diff] [blame] | 17 | #include "testing/gmock/include/gmock/gmock.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
| 20 | using testing::_; |
| 21 | using testing::Return; |
| 22 | |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 23 | class FPDFFormFillEmbeddertest : public EmbedderTest { |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 24 | protected: |
Diana Gage | cb50b5f | 2017-06-29 09:54:19 -0700 | [diff] [blame] | 25 | 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 Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 40 | |
| 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 Gage | cb50b5f | 2017-06-29 09:54:19 -0700 | [diff] [blame] | 47 | // Navigates to text field using the mouse and then selects text via the |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 48 | // 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 Gage | cb50b5f | 2017-06-29 09:54:19 -0700 | [diff] [blame] | 70 | // Uses the mouse to navigate to text field and select text. |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 71 | 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 Gage | cb50b5f | 2017-06-29 09:54:19 -0700 | [diff] [blame] | 102 | |
| 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 Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 125 | }; |
Tom Sepez | a310e00 | 2015-02-27 13:03:07 -0800 | [diff] [blame] | 126 | |
| 127 | TEST_F(FPDFFormFillEmbeddertest, FirstTest) { |
| 128 | EmbedderTestMockDelegate mock; |
| 129 | EXPECT_CALL(mock, Alert(_, _, _, _)).Times(0); |
| 130 | EXPECT_CALL(mock, UnsupportedHandler(_)).Times(0); |
Tom Sepez | 6efc0ad | 2015-06-02 17:11:18 -0700 | [diff] [blame] | 131 | EXPECT_CALL(mock, SetTimer(_, _)).Times(0); |
| 132 | EXPECT_CALL(mock, KillTimer(_)).Times(0); |
Tom Sepez | a310e00 | 2015-02-27 13:03:07 -0800 | [diff] [blame] | 133 | SetDelegate(&mock); |
| 134 | |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 135 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
Tom Sepez | a310e00 | 2015-02-27 13:03:07 -0800 | [diff] [blame] | 136 | FPDF_PAGE page = LoadPage(0); |
tsepez | 8e12029 | 2016-08-03 14:03:35 -0700 | [diff] [blame] | 137 | EXPECT_TRUE(page); |
Lei Zhang | d27acae | 2015-05-15 15:36:02 -0700 | [diff] [blame] | 138 | UnloadPage(page); |
Tom Sepez | a310e00 | 2015-02-27 13:03:07 -0800 | [diff] [blame] | 139 | } |
Tom Sepez | 6efc0ad | 2015-06-02 17:11:18 -0700 | [diff] [blame] | 140 | |
| 141 | TEST_F(FPDFFormFillEmbeddertest, BUG_487928) { |
| 142 | EmbedderTestTimerHandlingDelegate delegate; |
| 143 | SetDelegate(&delegate); |
| 144 | |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 145 | EXPECT_TRUE(OpenDocument("bug_487928.pdf")); |
Tom Sepez | 6efc0ad | 2015-06-02 17:11:18 -0700 | [diff] [blame] | 146 | FPDF_PAGE page = LoadPage(0); |
tsepez | 8e12029 | 2016-08-03 14:03:35 -0700 | [diff] [blame] | 147 | EXPECT_TRUE(page); |
Tom Sepez | 6efc0ad | 2015-06-02 17:11:18 -0700 | [diff] [blame] | 148 | DoOpenActions(); |
| 149 | delegate.AdvanceTime(5000); |
| 150 | UnloadPage(page); |
| 151 | } |
Tom Sepez | 0b13398 | 2015-07-28 11:23:22 -0700 | [diff] [blame] | 152 | |
Tom Sepez | 396e872 | 2015-09-09 10:16:08 -0700 | [diff] [blame] | 153 | TEST_F(FPDFFormFillEmbeddertest, BUG_507316) { |
| 154 | EmbedderTestTimerHandlingDelegate delegate; |
| 155 | SetDelegate(&delegate); |
| 156 | |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 157 | EXPECT_TRUE(OpenDocument("bug_507316.pdf")); |
weili | 0dadcc6 | 2016-08-23 21:10:57 -0700 | [diff] [blame] | 158 | FPDF_PAGE page = LoadPage(2); |
tsepez | 8e12029 | 2016-08-03 14:03:35 -0700 | [diff] [blame] | 159 | EXPECT_TRUE(page); |
Tom Sepez | 396e872 | 2015-09-09 10:16:08 -0700 | [diff] [blame] | 160 | DoOpenActions(); |
| 161 | delegate.AdvanceTime(4000); |
| 162 | UnloadPage(page); |
| 163 | } |
| 164 | |
Tom Sepez | 0b13398 | 2015-07-28 11:23:22 -0700 | [diff] [blame] | 165 | TEST_F(FPDFFormFillEmbeddertest, BUG_514690) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 166 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
Tom Sepez | 0b13398 | 2015-07-28 11:23:22 -0700 | [diff] [blame] | 167 | FPDF_PAGE page = LoadPage(0); |
tsepez | 8e12029 | 2016-08-03 14:03:35 -0700 | [diff] [blame] | 168 | EXPECT_TRUE(page); |
Tom Sepez | 0b13398 | 2015-07-28 11:23:22 -0700 | [diff] [blame] | 169 | |
| 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 Zhang | 79e893a | 2015-11-04 16:02:47 -0800 | [diff] [blame] | 176 | |
Tom Sepez | 4d07179 | 2016-02-04 16:53:26 -0800 | [diff] [blame] | 177 | #ifdef PDF_ENABLE_V8 |
Lei Zhang | 79e893a | 2015-11-04 16:02:47 -0800 | [diff] [blame] | 178 | TEST_F(FPDFFormFillEmbeddertest, BUG_551248) { |
tsepez | 8e12029 | 2016-08-03 14:03:35 -0700 | [diff] [blame] | 179 | // Test that timers fire once and intervals fire repeatedly. |
Lei Zhang | 79e893a | 2015-11-04 16:02:47 -0800 | [diff] [blame] | 180 | EmbedderTestTimerHandlingDelegate delegate; |
| 181 | SetDelegate(&delegate); |
| 182 | |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 183 | EXPECT_TRUE(OpenDocument("bug_551248.pdf")); |
Lei Zhang | 79e893a | 2015-11-04 16:02:47 -0800 | [diff] [blame] | 184 | FPDF_PAGE page = LoadPage(0); |
tsepez | 8e12029 | 2016-08-03 14:03:35 -0700 | [diff] [blame] | 185 | 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 | |
| 230 | TEST_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 Zhang | 79e893a | 2015-11-04 16:02:47 -0800 | [diff] [blame] | 238 | DoOpenActions(); |
| 239 | delegate.AdvanceTime(5000); |
| 240 | UnloadPage(page); |
| 241 | |
| 242 | const auto& alerts = delegate.GetAlerts(); |
tsepez | 0fa54b8 | 2016-08-04 12:07:28 -0700 | [diff] [blame] | 243 | ASSERT_EQ(1U, alerts.size()); |
| 244 | EXPECT_STREQ(L"done", alerts[0].message.c_str()); |
Lei Zhang | 79e893a | 2015-11-04 16:02:47 -0800 | [diff] [blame] | 245 | } |
tsepez | 8e12029 | 2016-08-03 14:03:35 -0700 | [diff] [blame] | 246 | |
tsepez | 32e693f | 2016-08-04 12:47:42 -0700 | [diff] [blame] | 247 | TEST_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 | |
tsepez | 8ca63de | 2016-08-05 17:12:27 -0700 | [diff] [blame] | 270 | TEST_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 | |
tsepez | 6cf5eca | 2017-01-12 11:21:12 -0800 | [diff] [blame] | 292 | TEST_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 Sepez | fb7021c | 2017-05-31 10:29:25 -0700 | [diff] [blame] | 309 | TEST_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 Sepez | 4d07179 | 2016-02-04 16:53:26 -0800 | [diff] [blame] | 327 | #endif // PDF_ENABLE_V8 |
Nicolas Pena | 742977f | 2017-04-13 15:28:20 -0400 | [diff] [blame] | 328 | |
| 329 | TEST_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 Pena | 00c3cfd | 2017-07-10 17:29:54 -0400 | [diff] [blame^] | 335 | const char md5_1[] = "a5e3ac74c2ee123ec6710e2f0ef8424a"; |
| 336 | const char md5_2[] = "4526b09382e144d5506ad92149399de6"; |
| 337 | const char md5_3[] = "80356067d860088864cf50ff85d8459e"; |
Nicolas Pena | 742977f | 2017-04-13 15:28:20 -0400 | [diff] [blame] | 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 | |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 369 | // Close page |
Nicolas Pena | 742977f | 2017-04-13 15:28:20 -0400 | [diff] [blame] | 370 | UnloadPage(page); |
Nicolas Pena | 742977f | 2017-04-13 15:28:20 -0400 | [diff] [blame] | 371 | } |
| 372 | // Check saved document |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 373 | TestAndCloseSaved(300, 300, md5_3); |
Nicolas Pena | 742977f | 2017-04-13 15:28:20 -0400 | [diff] [blame] | 374 | } |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 375 | |
| 376 | TEST_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 Gage | cb50b5f | 2017-06-29 09:54:19 -0700 | [diff] [blame] | 386 | TypeTextIntoTextfield(page, 3, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 387 | SelectTextWithKeyboard(page, 3, FWL_VKEY_Left, 123.0, 115.5); |
| 388 | CheckSelection(page, CFX_WideString(L"ABC")); |
| 389 | |
| 390 | UnloadPage(page); |
| 391 | } |
| 392 | |
| 393 | TEST_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 Gage | cb50b5f | 2017-06-29 09:54:19 -0700 | [diff] [blame] | 403 | TypeTextIntoTextfield(page, 3, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 404 | SelectTextWithMouse(page, 125.0, 102.0, 115.5); |
| 405 | CheckSelection(page, CFX_WideString(L"ABC")); |
| 406 | |
| 407 | UnloadPage(page); |
| 408 | } |
| 409 | |
| 410 | TEST_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 Gage | cb50b5f | 2017-06-29 09:54:19 -0700 | [diff] [blame] | 416 | TypeTextIntoTextfield(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 417 | |
| 418 | // Test selecting first character in forward direction. |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 419 | 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 | |
| 441 | TEST_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 Gage | cb50b5f | 2017-06-29 09:54:19 -0700 | [diff] [blame] | 447 | TypeTextIntoTextfield(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 448 | |
| 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 Gage | cb50b5f | 2017-06-29 09:54:19 -0700 | [diff] [blame] | 471 | |
| 472 | TEST_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 | |
| 497 | TEST_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 | |
| 519 | TEST_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 | |
| 541 | TEST_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 | |
| 580 | TEST_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 | |
| 618 | TEST_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 | } |