1 // Copyright 2017 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 
5 #include "core/fxcrt/widestring.h"
6 #include "public/fpdf_formfill.h"
7 #include "public/fpdf_fwlevent.h"
8 #include "testing/embedder_test.h"
9 #include "testing/embedder_test_timer_handling_delegate.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/xfa_js_embedder_test.h"
12 
13 class CFWLEditEmbedderTest : public XFAJSEmbedderTest {
14  protected:
SetUp()15   void SetUp() override {
16     EmbedderTest::SetUp();
17     SetDelegate(&delegate_);
18   }
19 
TearDown()20   void TearDown() override {
21     UnloadPage(page());
22     EmbedderTest::TearDown();
23   }
24 
CreateAndInitializeFormPDF(const char * filename)25   void CreateAndInitializeFormPDF(const char* filename) {
26     EXPECT_TRUE(OpenDocument(filename));
27     page_ = LoadPage(0);
28     ASSERT_TRUE(page_);
29   }
30 
page() const31   FPDF_PAGE page() const { return page_; }
delegate() const32   EmbedderTestTimerHandlingDelegate delegate() const { return delegate_; }
33 
34  private:
35   FPDF_PAGE page_;
36   EmbedderTestTimerHandlingDelegate delegate_;
37 };
38 
TEST_F(CFWLEditEmbedderTest,Trivial)39 TEST_F(CFWLEditEmbedderTest, Trivial) {
40   CreateAndInitializeFormPDF("xfa/email_recommended.pdf");
41   ASSERT_EQ(0u, delegate().GetAlerts().size());
42 }
43 
TEST_F(CFWLEditEmbedderTest,LeftClickMouseSelection)44 TEST_F(CFWLEditEmbedderTest, LeftClickMouseSelection) {
45   CreateAndInitializeFormPDF("xfa/email_recommended.pdf");
46   FORM_OnLButtonDown(form_handle(), page(), 0, 115, 58);
47   for (size_t i = 0; i < 10; ++i)
48     FORM_OnChar(form_handle(), page(), 'a' + i, 0);
49 
50   // Mouse selection
51   FORM_OnLButtonDown(form_handle(), page(), 0, 128, 58);
52   FORM_OnLButtonDown(form_handle(), page(), FWL_EVENTFLAG_ShiftKey, 152, 58);
53 
54   // 12 == (2 * strlen(defgh)) + 2 (for \0\0)
55   EXPECT_EQ(12UL, FORM_GetSelectedText(form_handle(), page(), nullptr, 0));
56 
57   unsigned short buf[128];
58   unsigned long len = FORM_GetSelectedText(form_handle(), page(), &buf, 128);
59   EXPECT_STREQ(L"defgh", WideString::FromUTF16LE(buf, len).c_str());
60 }
61 
62 // TODO(crbug.com/pdfium/11): Fix this test and enable.
63 #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
64 #define MAYBE_DragMouseSelection DISABLED_DragMouseSelection
65 #else
66 #define MAYBE_DragMouseSelection DragMouseSelection
67 #endif
TEST_F(CFWLEditEmbedderTest,MAYBE_DragMouseSelection)68 TEST_F(CFWLEditEmbedderTest, MAYBE_DragMouseSelection) {
69   CreateAndInitializeFormPDF("xfa/email_recommended.pdf");
70   FORM_OnLButtonDown(form_handle(), page(), 0, 115, 58);
71   for (size_t i = 0; i < 10; ++i)
72     FORM_OnChar(form_handle(), page(), 'a' + i, 0);
73 
74   // Mouse selection
75   FORM_OnLButtonDown(form_handle(), page(), 0, 128, 58);
76   FORM_OnMouseMove(form_handle(), page(), FWL_EVENTFLAG_ShiftKey, 152, 58);
77 
78   // 12 == (2 * strlen(defgh)) + 2 (for \0\0)
79   EXPECT_EQ(12UL, FORM_GetSelectedText(form_handle(), page(), nullptr, 0));
80 
81   unsigned short buf[128];
82   unsigned long len = FORM_GetSelectedText(form_handle(), page(), &buf, 128);
83   EXPECT_STREQ(L"defgh", WideString::FromUTF16LE(buf, len).c_str());
84 
85   // TODO(hnakashima): This is incorrect. Visually 'abcdefgh' are selected.
86   const char kDraggedMD5[] = "f131526c8edd04e44de17b2647ec54c8";
87   {
88     ScopedFPDFBitmap page_bitmap =
89         RenderLoadedPageWithFlags(page(), FPDF_ANNOT);
90     CompareBitmap(page_bitmap.get(), 612, 792, kDraggedMD5);
91   }
92 }
93 
94 // TODO(crbug.com/pdfium/11): Fix this test and enable.
95 #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
96 #define MAYBE_SimpleFill DISABLED_SimpleFill
97 #else
98 #define MAYBE_SimpleFill SimpleFill
99 #endif
TEST_F(CFWLEditEmbedderTest,MAYBE_SimpleFill)100 TEST_F(CFWLEditEmbedderTest, MAYBE_SimpleFill) {
101   CreateAndInitializeFormPDF("xfa/email_recommended.pdf");
102   const char kBlankMD5[] = "8dda78a3afaf9f7b5210eb81cacc4600";
103   {
104     ScopedFPDFBitmap page_bitmap =
105         RenderLoadedPageWithFlags(page(), FPDF_ANNOT);
106     CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
107   }
108 
109   FORM_OnLButtonDown(form_handle(), page(), 0, 115, 58);
110   for (size_t i = 0; i < 10; ++i)
111     FORM_OnChar(form_handle(), page(), 'a' + i, 0);
112 
113   const char kFilledMD5[] = "211e4e46eb347aa2bc7c425556d600b0";
114   {
115     ScopedFPDFBitmap page_bitmap =
116         RenderLoadedPageWithFlags(page(), FPDF_ANNOT);
117     CompareBitmap(page_bitmap.get(), 612, 792, kFilledMD5);
118   }
119 }
120 
121 // TODO(crbug.com/pdfium/11): Fix this test and enable.
122 #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
123 #define MAYBE_FillWithNewLineWithoutMultiline \
124   DISABLED_FillWithNewLineWithoutMultiline
125 #else
126 #define MAYBE_FillWithNewLineWithoutMultiline FillWithNewLineWithoutMultiline
127 #endif
TEST_F(CFWLEditEmbedderTest,MAYBE_FillWithNewLineWithoutMultiline)128 TEST_F(CFWLEditEmbedderTest, MAYBE_FillWithNewLineWithoutMultiline) {
129   CreateAndInitializeFormPDF("xfa/email_recommended.pdf");
130   FORM_OnLButtonDown(form_handle(), page(), 0, 115, 58);
131   for (size_t i = 0; i < 5; ++i)
132     FORM_OnChar(form_handle(), page(), 'a' + i, 0);
133   FORM_OnChar(form_handle(), page(), '\r', 0);
134   for (size_t i = 5; i < 10; ++i)
135     FORM_OnChar(form_handle(), page(), 'a' + i, 0);
136 
137   const char kFilledMD5[] = "211e4e46eb347aa2bc7c425556d600b0";
138   {
139     ScopedFPDFBitmap page_bitmap =
140         RenderLoadedPageWithFlags(page(), FPDF_ANNOT);
141     CompareBitmap(page_bitmap.get(), 612, 792, kFilledMD5);
142   }
143 }
144 
145 // Disabled due to flakiness.
TEST_F(CFWLEditEmbedderTest,DISABLED_FillWithNewLineWithMultiline)146 TEST_F(CFWLEditEmbedderTest, DISABLED_FillWithNewLineWithMultiline) {
147   CreateAndInitializeFormPDF("xfa/xfa_multiline_textfield.pdf");
148   FORM_OnLButtonDown(form_handle(), page(), 0, 115, 58);
149 
150   for (size_t i = 0; i < 5; ++i)
151     FORM_OnChar(form_handle(), page(), 'a' + i, 0);
152   FORM_OnChar(form_handle(), page(), '\r', 0);
153   for (size_t i = 5; i < 10; ++i)
154     FORM_OnChar(form_handle(), page(), 'a' + i, 0);
155 
156   // Should look like:
157   // abcde
158   // fghij|
159   {
160 #if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
161     const char kFilledMultilineMD5[] = "fc1f4d5fdb2c5755005fc525b0a60ec9";
162 #else
163     const char kFilledMultilineMD5[] = "a5654e027d8b1667c20f3b86d1918003";
164 #endif  // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
165     ScopedFPDFBitmap page_bitmap =
166         RenderLoadedPageWithFlags(page(), FPDF_ANNOT);
167     CompareBitmap(page_bitmap.get(), 612, 792, kFilledMultilineMD5);
168   }
169 
170   for (size_t i = 0; i < 4; ++i)
171     FORM_OnKeyDown(form_handle(), page(), FWL_VKEY_Left, 0);
172 
173   // Should look like:
174   // abcde
175   // f|ghij
176 
177   // Two backspaces is a workaround because left arrow does not behave well
178   // in the first character of a line. It skips back to the previous line.
179   for (size_t i = 0; i < 2; ++i)
180     FORM_OnChar(form_handle(), page(), '\b', 0);
181 
182   // Should look like:
183   // abcde|ghij
184   {
185 #if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
186     const char kMultilineBackspaceMD5[] = "8bb62a8100ff1e1cc113d4033e0d824e";
187 #else
188     const char kMultilineBackspaceMD5[] = "a2f1dcab92bb1fb7c2f9ccc70100c989";
189 #endif  // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
190     ScopedFPDFBitmap page_bitmap =
191         RenderLoadedPageWithFlags(page(), FPDF_ANNOT);
192     CompareBitmap(page_bitmap.get(), 612, 792, kMultilineBackspaceMD5);
193   }
194 }
195 
196 // TODO(crbug.com/pdfium/11): Fix this test and enable.
197 #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
198 #define MAYBE_DateTimePickerTest DISABLED_DateTimePickerTest
199 #else
200 #define MAYBE_DateTimePickerTest DateTimePickerTest
201 #endif
TEST_F(CFWLEditEmbedderTest,MAYBE_DateTimePickerTest)202 TEST_F(CFWLEditEmbedderTest, MAYBE_DateTimePickerTest) {
203   CreateAndInitializeFormPDF("xfa/xfa_date_time_edit.pdf");
204   FORM_OnLButtonDown(form_handle(), page(), 0, 115, 58);
205 
206   const char kFilledMD5[] = "1036b8837a9dba75c6bd8f9347ae2eb2";
207   {
208     ScopedFPDFBitmap page_bitmap =
209         RenderLoadedPageWithFlags(page(), FPDF_ANNOT);
210     CompareBitmap(page_bitmap.get(), 612, 792, kFilledMD5);
211   }
212 }
213 
TEST_F(CFWLEditEmbedderTest,ImageEditTest)214 TEST_F(CFWLEditEmbedderTest, ImageEditTest) {
215   CreateAndInitializeFormPDF("xfa/xfa_image_edit.pdf");
216   FORM_OnLButtonDown(form_handle(), page(), 0, 115, 58);
217 
218   const char kFilledMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3";
219   {
220     ScopedFPDFBitmap page_bitmap =
221         RenderLoadedPageWithFlags(page(), FPDF_ANNOT);
222     CompareBitmap(page_bitmap.get(), 612, 792, kFilledMD5);
223   }
224 }
225 
226 // TODO(crbug.com/pdfium/11): Fix this test and enable.
227 #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
228 #define MAYBE_ComboBoxTest DISABLED_ComboBoxTest
229 #else
230 #define MAYBE_ComboBoxTest ComboBoxTest
231 #endif
TEST_F(CFWLEditEmbedderTest,MAYBE_ComboBoxTest)232 TEST_F(CFWLEditEmbedderTest, MAYBE_ComboBoxTest) {
233   CreateAndInitializeFormPDF("xfa/xfa_combobox.pdf");
234   FORM_OnLButtonDown(form_handle(), page(), 0, 115, 58);
235 
236   const char kFilledMD5[] = "dad642ae8a5afce2591ffbcabbfc58dd";
237   {
238     ScopedFPDFBitmap page_bitmap =
239         RenderLoadedPageWithFlags(page(), FPDF_ANNOT);
240     CompareBitmap(page_bitmap.get(), 612, 792, kFilledMD5);
241   }
242 }
243