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 | |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 23 | class FPDFFormFillEmbeddertest : public EmbedderTest, public TestSaver { |
| 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 |
| 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 Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 390 | |
| 391 | TEST_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 Gage | cb50b5f | 2017-06-29 09:54:19 -0700 | [diff] [blame^] | 401 | TypeTextIntoTextfield(page, 3, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 402 | SelectTextWithKeyboard(page, 3, FWL_VKEY_Left, 123.0, 115.5); |
| 403 | CheckSelection(page, CFX_WideString(L"ABC")); |
| 404 | |
| 405 | UnloadPage(page); |
| 406 | } |
| 407 | |
| 408 | TEST_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 Gage | cb50b5f | 2017-06-29 09:54:19 -0700 | [diff] [blame^] | 418 | TypeTextIntoTextfield(page, 3, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 419 | SelectTextWithMouse(page, 125.0, 102.0, 115.5); |
| 420 | CheckSelection(page, CFX_WideString(L"ABC")); |
| 421 | |
| 422 | UnloadPage(page); |
| 423 | } |
| 424 | |
| 425 | TEST_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 Gage | cb50b5f | 2017-06-29 09:54:19 -0700 | [diff] [blame^] | 431 | TypeTextIntoTextfield(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 432 | |
| 433 | // Test selecting first character in forward direction. |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 434 | 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 | |
| 456 | TEST_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 Gage | cb50b5f | 2017-06-29 09:54:19 -0700 | [diff] [blame^] | 462 | TypeTextIntoTextfield(page, 12, FPDF_FORMFIELD_TEXTFIELD, 120.0, 120.0); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 463 | |
| 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 Gage | cb50b5f | 2017-06-29 09:54:19 -0700 | [diff] [blame^] | 486 | |
| 487 | TEST_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 | |
| 512 | TEST_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 | |
| 534 | TEST_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 | |
| 556 | TEST_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 | |
| 595 | TEST_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 | |
| 633 | TEST_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 | } |