Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 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 | |
Tom Sepez | 204ab05 | 2020-06-12 21:33:48 +0000 | [diff] [blame] | 5 | #include "public/fpdf_annot.h" |
| 6 | |
| 7 | #include <limits.h> |
| 8 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 9 | #include <algorithm> |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 10 | #include <cwchar> |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 11 | #include <memory> |
| 12 | #include <string> |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
Lei Zhang | e4cdac5 | 2019-04-30 16:45:57 +0000 | [diff] [blame] | 15 | #include "build/build_config.h" |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 16 | #include "constants/annotation_common.h" |
Tom Sepez | 204ab05 | 2020-06-12 21:33:48 +0000 | [diff] [blame] | 17 | #include "core/fpdfapi/page/cpdf_annotcontext.h" |
| 18 | #include "core/fpdfapi/page/cpdf_pagemodule.h" |
| 19 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 20 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 21 | #include "core/fxcrt/fx_system.h" |
Tom Sepez | 204ab05 | 2020-06-12 21:33:48 +0000 | [diff] [blame] | 22 | #include "fpdfsdk/cpdfsdk_helpers.h" |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 23 | #include "public/cpp/fpdf_scopers.h" |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 24 | #include "public/fpdf_edit.h" |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 25 | #include "public/fpdf_formfill.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 26 | #include "public/fpdfview.h" |
| 27 | #include "testing/embedder_test.h" |
Hui Yingst | b4baceb | 2020-04-28 23:46:10 +0000 | [diff] [blame] | 28 | #include "testing/embedder_test_constants.h" |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 29 | #include "testing/fx_string_testhelpers.h" |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 30 | #include "testing/gmock/include/gmock/gmock-matchers.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 31 | #include "testing/gtest/include/gtest/gtest.h" |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 32 | #include "testing/utils/hash.h" |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 33 | #include "third_party/base/span.h" |
Lei Zhang | c9b7299 | 2020-04-22 01:03:52 +0000 | [diff] [blame] | 34 | #include "third_party/base/stl_util.h" |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | |
Tom Sepez | 204ab05 | 2020-06-12 21:33:48 +0000 | [diff] [blame] | 38 | const wchar_t kStreamData[] = |
| 39 | L"/GS gs 0.0 0.0 0.0 RG 4 w 211.8 747.6 m 211.8 744.8 " |
| 40 | L"212.6 743.0 214.2 740.8 " |
| 41 | L"c 215.4 739.0 216.8 737.1 218.9 736.1 c 220.8 735.1 221.4 733.0 " |
| 42 | L"223.7 732.4 c 232.6 729.9 242.0 730.8 251.2 730.8 c 257.5 730.8 " |
| 43 | L"263.0 732.9 269.0 734.4 c S"; |
| 44 | |
Hui Yingst | 4a21df0 | 2020-05-13 03:15:44 +0000 | [diff] [blame] | 45 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 46 | #if defined(OS_LINUX) |
| 47 | const char kAnnotationStampWithApChecksum[] = |
| 48 | "db83eaadc92967e3ac9bebfc6178ca75"; |
| 49 | #else |
| 50 | const char kAnnotationStampWithApChecksum[] = |
| 51 | "3c87b4a8e51245964357fb5f5fbc612b"; |
| 52 | #endif // defined(OS_LINUX) |
| 53 | #else |
Hui Yingst | b4baceb | 2020-04-28 23:46:10 +0000 | [diff] [blame] | 54 | #if defined(OS_WIN) |
| 55 | const char kAnnotationStampWithApChecksum[] = |
| 56 | "6aa001a77ec05d0f1b0d1d22e28744d4"; |
| 57 | #elif defined(OS_MACOSX) |
| 58 | const char kAnnotationStampWithApChecksum[] = |
| 59 | "80d7b6cc7b13a78d77a6151bc846e80b"; |
| 60 | #else |
| 61 | const char kAnnotationStampWithApChecksum[] = |
| 62 | "b42cef463483e668eaf4055a65e4f1f5"; |
| 63 | #endif |
Hui Yingst | 4a21df0 | 2020-05-13 03:15:44 +0000 | [diff] [blame] | 64 | #endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Hui Yingst | b4baceb | 2020-04-28 23:46:10 +0000 | [diff] [blame] | 65 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 66 | void VerifyFocusableAnnotSubtypes( |
| 67 | FPDF_FORMHANDLE form_handle, |
| 68 | pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) { |
| 69 | ASSERT_EQ(static_cast<int>(expected_subtypes.size()), |
| 70 | FPDFAnnot_GetFocusableSubtypesCount(form_handle)); |
| 71 | |
| 72 | std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes( |
| 73 | expected_subtypes.size()); |
| 74 | ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes( |
| 75 | form_handle, actual_subtypes.data(), actual_subtypes.size())); |
| 76 | for (size_t i = 0; i < expected_subtypes.size(); ++i) |
| 77 | ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]); |
| 78 | } |
| 79 | |
| 80 | void SetAndVerifyFocusableAnnotSubtypes( |
| 81 | FPDF_FORMHANDLE form_handle, |
| 82 | pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) { |
| 83 | ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(), |
| 84 | subtypes.size())); |
| 85 | VerifyFocusableAnnotSubtypes(form_handle, subtypes); |
| 86 | } |
| 87 | |
| 88 | void VerifyAnnotationSubtypesAndFocusability( |
| 89 | FPDF_FORMHANDLE form_handle, |
| 90 | FPDF_PAGE page, |
| 91 | pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes, |
| 92 | pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) { |
| 93 | ASSERT_EQ(static_cast<int>(expected_subtypes.size()), |
| 94 | FPDFPage_GetAnnotCount(page)); |
| 95 | for (size_t i = 0; i < expected_subtypes.size(); ++i) { |
| 96 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i)); |
| 97 | ASSERT_TRUE(annot); |
| 98 | EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get())); |
| 99 | |
Lei Zhang | f245ae6 | 2020-05-15 21:49:46 +0000 | [diff] [blame] | 100 | bool expected_focusable = |
| 101 | pdfium::Contains(expected_focusable_subtypes, expected_subtypes[i]); |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 102 | EXPECT_EQ(expected_focusable, |
| 103 | FORM_SetFocusedAnnot(form_handle, annot.get())); |
| 104 | |
| 105 | // Kill the focus so the next test starts in an unfocused state. |
| 106 | FORM_ForceToKillFocus(form_handle); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | } // namespace |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 111 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 112 | class FPDFAnnotEmbedderTest : public EmbedderTest {}; |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 113 | |
Tom Sepez | 204ab05 | 2020-06-12 21:33:48 +0000 | [diff] [blame] | 114 | TEST_F(FPDFAnnotEmbedderTest, SetAP) { |
| 115 | ScopedFPDFDocument doc(FPDF_CreateNewDocument()); |
| 116 | ASSERT_TRUE(doc); |
| 117 | ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100)); |
| 118 | ASSERT_TRUE(page); |
| 119 | ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData); |
| 120 | ASSERT_TRUE(ap_stream); |
| 121 | |
| 122 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK)); |
| 123 | ASSERT_TRUE(annot); |
| 124 | |
| 125 | // Negative case: FPDFAnnot_SetAP() should fail if bounding rect is not yet |
| 126 | // set on the annotation. |
| 127 | EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 128 | ap_stream.get())); |
| 129 | |
| 130 | const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f}; |
| 131 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect)); |
| 132 | |
| 133 | ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, |
| 134 | /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/255)); |
| 135 | |
| 136 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 137 | ap_stream.get())); |
| 138 | |
| 139 | // Verify that appearance stream is created as form XObject |
| 140 | CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get()); |
| 141 | ASSERT_TRUE(context); |
| 142 | CPDF_Dictionary* annot_dict = context->GetAnnotDict(); |
| 143 | ASSERT_TRUE(annot_dict); |
| 144 | CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP); |
| 145 | ASSERT_TRUE(ap_dict); |
| 146 | CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N"); |
| 147 | ASSERT_TRUE(stream_dict); |
| 148 | // Check for non-existence of resources dictionary in case of opaque color |
| 149 | CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources"); |
| 150 | ASSERT_FALSE(resources_dict); |
| 151 | ByteString type = stream_dict->GetStringFor(pdfium::annotation::kType); |
| 152 | EXPECT_EQ("XObject", type); |
| 153 | ByteString sub_type = stream_dict->GetStringFor(pdfium::annotation::kSubtype); |
| 154 | EXPECT_EQ("Form", sub_type); |
| 155 | |
| 156 | // Check that the appearance stream is same as we just set. |
| 157 | const uint32_t kStreamDataSize = |
| 158 | pdfium::size(kStreamData) * sizeof(FPDF_WCHAR); |
| 159 | unsigned long normal_length_bytes = FPDFAnnot_GetAP( |
| 160 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0); |
| 161 | ASSERT_EQ(kStreamDataSize, normal_length_bytes); |
| 162 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes); |
| 163 | EXPECT_EQ(kStreamDataSize, |
| 164 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 165 | buf.data(), normal_length_bytes)); |
| 166 | EXPECT_EQ(kStreamData, GetPlatformWString(buf.data())); |
| 167 | } |
| 168 | |
| 169 | TEST_F(FPDFAnnotEmbedderTest, SetAPWithOpacity) { |
| 170 | ScopedFPDFDocument doc(FPDF_CreateNewDocument()); |
| 171 | ASSERT_TRUE(doc); |
| 172 | ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100)); |
| 173 | ASSERT_TRUE(page); |
| 174 | ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData); |
| 175 | ASSERT_TRUE(ap_stream); |
| 176 | |
| 177 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK)); |
| 178 | ASSERT_TRUE(annot); |
| 179 | |
| 180 | ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, |
| 181 | /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/102)); |
| 182 | |
| 183 | const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f}; |
| 184 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect)); |
| 185 | |
| 186 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 187 | ap_stream.get())); |
| 188 | |
| 189 | CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get()); |
| 190 | ASSERT_TRUE(context); |
| 191 | CPDF_Dictionary* annot_dict = context->GetAnnotDict(); |
| 192 | ASSERT_TRUE(annot_dict); |
| 193 | CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP); |
| 194 | ASSERT_TRUE(ap_dict); |
| 195 | CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N"); |
| 196 | ASSERT_TRUE(stream_dict); |
| 197 | CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources"); |
| 198 | ASSERT_TRUE(stream_dict); |
| 199 | CPDF_Dictionary* extGState_dict = resources_dict->GetDictFor("ExtGState"); |
| 200 | ASSERT_TRUE(extGState_dict); |
| 201 | CPDF_Dictionary* gs_dict = extGState_dict->GetDictFor("GS"); |
| 202 | ASSERT_TRUE(gs_dict); |
| 203 | ByteString type = gs_dict->GetStringFor(pdfium::annotation::kType); |
| 204 | EXPECT_EQ("ExtGState", type); |
| 205 | float opacity = gs_dict->GetNumberFor("CA"); |
| 206 | // Opacity value of 102 is represented as 0.4f (=104/255) in /CA entry. |
| 207 | EXPECT_FLOAT_EQ(0.4f, opacity); |
| 208 | ByteString blend_mode = gs_dict->GetStringFor("BM"); |
| 209 | EXPECT_EQ("Normal", blend_mode); |
| 210 | bool alpha_source_flag = gs_dict->GetBooleanFor("AIS", true); |
| 211 | EXPECT_FALSE(alpha_source_flag); |
| 212 | } |
| 213 | |
| 214 | TEST_F(FPDFAnnotEmbedderTest, InkListAPIValidations) { |
| 215 | ScopedFPDFDocument doc(FPDF_CreateNewDocument()); |
| 216 | ASSERT_TRUE(doc); |
| 217 | ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100)); |
| 218 | ASSERT_TRUE(page); |
| 219 | |
| 220 | // Create a new ink annotation. |
| 221 | ScopedFPDFAnnotation ink_annot( |
| 222 | FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK)); |
| 223 | ASSERT_TRUE(ink_annot); |
| 224 | CPDF_AnnotContext* context = |
| 225 | CPDFAnnotContextFromFPDFAnnotation(ink_annot.get()); |
| 226 | ASSERT_TRUE(context); |
| 227 | CPDF_Dictionary* annot_dict = context->GetAnnotDict(); |
| 228 | ASSERT_TRUE(annot_dict); |
| 229 | |
| 230 | static constexpr FS_POINTF kFirstInkStroke[] = { |
| 231 | {80.0f, 90.0f}, {81.0f, 91.0f}, {82.0f, 92.0f}, |
| 232 | {83.0f, 93.0f}, {84.0f, 94.0f}, {85.0f, 95.0f}}; |
| 233 | static constexpr size_t kFirstStrokePointCount = |
| 234 | pdfium::size(kFirstInkStroke); |
| 235 | |
| 236 | static constexpr FS_POINTF kSecondInkStroke[] = { |
| 237 | {70.0f, 90.0f}, {71.0f, 91.0f}, {72.0f, 92.0f}}; |
| 238 | static constexpr size_t kSecondStrokePointCount = |
| 239 | pdfium::size(kSecondInkStroke); |
| 240 | |
| 241 | static constexpr FS_POINTF kThirdInkStroke[] = {{60.0f, 90.0f}, |
| 242 | {61.0f, 91.0f}, |
| 243 | {62.0f, 92.0f}, |
| 244 | {63.0f, 93.0f}, |
| 245 | {64.0f, 94.0f}}; |
| 246 | static constexpr size_t kThirdStrokePointCount = |
| 247 | pdfium::size(kThirdInkStroke); |
| 248 | |
| 249 | // Negative test: |annot| is passed as nullptr. |
| 250 | EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(nullptr, kFirstInkStroke, |
| 251 | kFirstStrokePointCount)); |
| 252 | |
| 253 | // Negative test: |annot| is not ink annotation. |
| 254 | // Create a new highlight annotation. |
| 255 | ScopedFPDFAnnotation highlight_annot( |
| 256 | FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT)); |
| 257 | ASSERT_TRUE(highlight_annot); |
| 258 | EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(highlight_annot.get(), kFirstInkStroke, |
| 259 | kFirstStrokePointCount)); |
| 260 | |
| 261 | // Negative test: passing |point_count| as 0. |
| 262 | EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke, 0)); |
| 263 | |
| 264 | // Negative test: passing |points| array as nullptr. |
| 265 | EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), nullptr, |
| 266 | kFirstStrokePointCount)); |
| 267 | |
| 268 | // Negative test: passing |point_count| more than ULONG_MAX/2. |
| 269 | EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke, |
| 270 | ULONG_MAX / 2 + 1)); |
| 271 | |
| 272 | // InkStroke should get added to ink annotation. Also inklist should get |
| 273 | // created. |
| 274 | EXPECT_EQ(0, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke, |
| 275 | kFirstStrokePointCount)); |
| 276 | |
| 277 | CPDF_Array* inklist = annot_dict->GetArrayFor("InkList"); |
| 278 | ASSERT_TRUE(inklist); |
| 279 | EXPECT_EQ(1u, inklist->size()); |
| 280 | EXPECT_EQ(kFirstStrokePointCount * 2, inklist->GetArrayAt(0)->size()); |
| 281 | |
| 282 | // Adding another inkStroke to ink annotation with all valid paremeters. |
| 283 | // InkList already exists in ink_annot. |
| 284 | EXPECT_EQ(1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke, |
| 285 | kSecondStrokePointCount)); |
| 286 | EXPECT_EQ(2u, inklist->size()); |
| 287 | EXPECT_EQ(kSecondStrokePointCount * 2, inklist->GetArrayAt(1)->size()); |
| 288 | |
| 289 | // Adding one more InkStroke to the ink annotation. |point_count| passed is |
| 290 | // less than the data available in |buffer|. |
| 291 | EXPECT_EQ(2, FPDFAnnot_AddInkStroke(ink_annot.get(), kThirdInkStroke, |
| 292 | kThirdStrokePointCount - 1)); |
| 293 | EXPECT_EQ(3u, inklist->size()); |
| 294 | EXPECT_EQ((kThirdStrokePointCount - 1) * 2, inklist->GetArrayAt(2)->size()); |
| 295 | } |
| 296 | |
| 297 | TEST_F(FPDFAnnotEmbedderTest, RemoveInkList) { |
| 298 | ScopedFPDFDocument doc(FPDF_CreateNewDocument()); |
| 299 | ASSERT_TRUE(doc); |
| 300 | ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100)); |
| 301 | ASSERT_TRUE(page); |
| 302 | |
| 303 | // Negative test: |annot| is passed as nullptr. |
| 304 | EXPECT_FALSE(FPDFAnnot_RemoveInkList(nullptr)); |
| 305 | |
| 306 | // Negative test: |annot| is not ink annotation. |
| 307 | // Create a new highlight annotation. |
| 308 | ScopedFPDFAnnotation highlight_annot( |
| 309 | FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT)); |
| 310 | ASSERT_TRUE(highlight_annot); |
| 311 | EXPECT_FALSE(FPDFAnnot_RemoveInkList(highlight_annot.get())); |
| 312 | |
| 313 | // Create a new ink annotation. |
| 314 | ScopedFPDFAnnotation ink_annot( |
| 315 | FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK)); |
| 316 | ASSERT_TRUE(ink_annot); |
| 317 | CPDF_AnnotContext* context = |
| 318 | CPDFAnnotContextFromFPDFAnnotation(ink_annot.get()); |
| 319 | ASSERT_TRUE(context); |
| 320 | CPDF_Dictionary* annot_dict = context->GetAnnotDict(); |
| 321 | ASSERT_TRUE(annot_dict); |
| 322 | |
| 323 | static constexpr FS_POINTF kInkStroke[] = {{80.0f, 90.0f}, {81.0f, 91.0f}, |
| 324 | {82.0f, 92.0f}, {83.0f, 93.0f}, |
| 325 | {84.0f, 94.0f}, {85.0f, 95.0f}}; |
| 326 | static constexpr size_t kPointCount = pdfium::size(kInkStroke); |
| 327 | |
| 328 | // InkStroke should get added to ink annotation. Also inklist should get |
| 329 | // created. |
| 330 | EXPECT_EQ(0, |
| 331 | FPDFAnnot_AddInkStroke(ink_annot.get(), kInkStroke, kPointCount)); |
| 332 | |
| 333 | CPDF_Array* inklist = annot_dict->GetArrayFor("InkList"); |
| 334 | ASSERT_TRUE(inklist); |
| 335 | ASSERT_EQ(1u, inklist->size()); |
| 336 | EXPECT_EQ(kPointCount * 2, inklist->GetArrayAt(0)->size()); |
| 337 | |
| 338 | // Remove inklist. |
| 339 | EXPECT_TRUE(FPDFAnnot_RemoveInkList(ink_annot.get())); |
| 340 | EXPECT_FALSE(annot_dict->KeyExist("InkList")); |
| 341 | } |
| 342 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 343 | TEST_F(FPDFAnnotEmbedderTest, BadParams) { |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 344 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| 345 | FPDF_PAGE page = LoadPage(0); |
| 346 | ASSERT_TRUE(page); |
| 347 | |
| 348 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr)); |
| 349 | |
| 350 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0)); |
| 351 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1)); |
| 352 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1)); |
| 353 | EXPECT_FALSE(FPDFPage_GetAnnot(page, -1)); |
| 354 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 1)); |
| 355 | |
| 356 | EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr)); |
| 357 | |
| 358 | EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr)); |
| 359 | |
| 360 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0)); |
| 361 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1)); |
| 362 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1)); |
| 363 | |
| 364 | EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo")); |
| 365 | |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 366 | static const wchar_t kContents[] = L"Bar"; |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 367 | ScopedFPDFWideString text = GetFPDFWideString(kContents); |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 368 | EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get())); |
| 369 | |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 370 | FPDF_WCHAR buffer[64]; |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 371 | EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0)); |
| 372 | EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0)); |
| 373 | EXPECT_EQ(0u, |
| 374 | FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer))); |
| 375 | |
| 376 | UnloadPage(page); |
| 377 | } |
| 378 | |
Lei Zhang | 3d9a097 | 2019-03-04 19:34:09 +0000 | [diff] [blame] | 379 | TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) { |
| 380 | ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf")); |
| 381 | FPDF_PAGE page = LoadPage(0); |
| 382 | ASSERT_TRUE(page); |
| 383 | |
| 384 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Lei Zhang | 98dc8c0 | 2019-03-04 19:40:30 +0000 | [diff] [blame] | 385 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | 3d9a097 | 2019-03-04 19:34:09 +0000 | [diff] [blame] | 386 | |
| 387 | UnloadPage(page); |
| 388 | } |
| 389 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 390 | TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) { |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 391 | // Open a file with one annotation and load its first page. |
| 392 | ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 393 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 394 | ASSERT_TRUE(page); |
| 395 | |
| 396 | // This annotation has a malformed appearance stream, which does not have its |
| 397 | // normal appearance defined, only its rollover appearance. In this case, its |
| 398 | // normal appearance should be generated, allowing the highlight annotation to |
| 399 | // still display. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 400 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | a98e366 | 2018-02-07 20:28:35 +0000 | [diff] [blame] | 401 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 402 | |
| 403 | UnloadPage(page); |
| 404 | } |
| 405 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 406 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 407 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 408 | #define MAYBE_RenderMultilineMarkupAnnotWithoutAP \ |
| 409 | DISABLED_RenderMultilineMarkupAnnotWithoutAP |
| 410 | #else |
| 411 | #define MAYBE_RenderMultilineMarkupAnnotWithoutAP \ |
| 412 | RenderMultilineMarkupAnnotWithoutAP |
| 413 | #endif |
| 414 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_RenderMultilineMarkupAnnotWithoutAP) { |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 415 | static const char kMd5[] = "76512832d88017668d9acc7aacd13dae"; |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 416 | // Open a file with multiline markup annotations. |
Ralf Sippl | b3a5240 | 2018-03-19 23:30:28 +0000 | [diff] [blame] | 417 | ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf")); |
| 418 | FPDF_PAGE page = LoadPage(0); |
| 419 | ASSERT_TRUE(page); |
| 420 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 421 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 422 | CompareBitmap(bitmap.get(), 595, 842, kMd5); |
Ralf Sippl | b3a5240 | 2018-03-19 23:30:28 +0000 | [diff] [blame] | 423 | |
| 424 | UnloadPage(page); |
| 425 | } |
| 426 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 427 | TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) { |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 428 | // Open a file with one annotation and load its first page. |
| 429 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 430 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 431 | ASSERT_TRUE(page); |
| 432 | |
| 433 | // Check that there is a total of 1 annotation on its first page. |
| 434 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 435 | |
| 436 | // Check that the annotation is of type "highlight". |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 437 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 438 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 439 | ASSERT_TRUE(annot); |
| 440 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 441 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 442 | // Check that the annotation color is yellow. |
| 443 | unsigned int R; |
| 444 | unsigned int G; |
| 445 | unsigned int B; |
| 446 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 447 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 448 | &G, &B, &A)); |
| 449 | EXPECT_EQ(255u, R); |
| 450 | EXPECT_EQ(255u, G); |
| 451 | EXPECT_EQ(0u, B); |
| 452 | EXPECT_EQ(255u, A); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 453 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 454 | // Check that the author is correct. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 455 | static const char kAuthorKey[] = "T"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 456 | EXPECT_EQ(FPDF_OBJECT_STRING, |
| 457 | FPDFAnnot_GetValueType(annot.get(), kAuthorKey)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 458 | unsigned long length_bytes = |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 459 | FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 460 | ASSERT_EQ(28u, length_bytes); |
| 461 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 462 | EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(), |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 463 | length_bytes)); |
| 464 | EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 465 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 466 | // Check that the content is correct. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 467 | EXPECT_EQ( |
| 468 | FPDF_OBJECT_STRING, |
| 469 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 470 | length_bytes = FPDFAnnot_GetStringValue( |
| 471 | annot.get(), pdfium::annotation::kContents, nullptr, 0); |
| 472 | ASSERT_EQ(2690u, length_bytes); |
| 473 | buf = GetFPDFWideStringBuffer(length_bytes); |
| 474 | EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(), |
| 475 | pdfium::annotation::kContents, |
| 476 | buf.data(), length_bytes)); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 477 | static const wchar_t kContents[] = |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 478 | L"This is a note for that highlight annotation. Very long highlight " |
| 479 | "annotation. Long long long Long long longLong long longLong long " |
| 480 | "longLong long longLong long longLong long longLong long longLong long " |
| 481 | "longLong long longLong long longLong long longLong long longLong long " |
| 482 | "longLong long longLong long longLong long longLong long longLong long " |
| 483 | "longLong long longLong long longLong long longLong long longLong long " |
| 484 | "longLong long longLong long longLong long longLong long longLong long " |
| 485 | "longLong long longLong long longLong long longLong long longLong long " |
| 486 | "longLong long longLong long longLong long longLong long longLong long " |
| 487 | "longLong long longLong long longLong long longLong long longLong long " |
| 488 | "longLong long longLong long longLong long longLong long longLong long " |
| 489 | "longLong long longLong long longLong long longLong long longLong long " |
| 490 | "longLong long longLong long longLong long longLong long longLong long " |
| 491 | "longLong long longLong long longLong long longLong long longLong long " |
| 492 | "longLong long longLong long longLong long longLong long longLong long " |
| 493 | "longLong long longLong long longLong long longLong long longLong long " |
| 494 | "longLong long longLong long longLong long longLong long longLong long " |
| 495 | "longLong long longLong long longLong long longLong long longLong long " |
| 496 | "longLong long longLong long longLong long longLong long longLong long " |
| 497 | "longLong long long. END"; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 498 | EXPECT_EQ(kContents, GetPlatformWString(buf.data())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 499 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 500 | // Check that the quadpoints are correct. |
| 501 | FS_QUADPOINTSF quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 502 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 503 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 504 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 505 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 506 | EXPECT_EQ(706.264465f, quadpoints.y4); |
| 507 | } |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 508 | UnloadPageNoEvents(page); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 509 | } |
| 510 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 511 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 512 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 513 | #define MAYBE_ExtractInkMultiple DISABLED_ExtractInkMultiple |
| 514 | #else |
| 515 | #define MAYBE_ExtractInkMultiple ExtractInkMultiple |
| 516 | #endif |
| 517 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_ExtractInkMultiple) { |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 518 | // Open a file with three annotations and load its first page. |
| 519 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 520 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 521 | ASSERT_TRUE(page); |
| 522 | |
| 523 | // Check that there is a total of 3 annotation on its first page. |
| 524 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 525 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 526 | { |
| 527 | // Check that the third annotation is of type "ink". |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 528 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 529 | ASSERT_TRUE(annot); |
| 530 | EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 531 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 532 | // Check that the annotation color is blue with opacity. |
| 533 | unsigned int R; |
| 534 | unsigned int G; |
| 535 | unsigned int B; |
| 536 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 537 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 538 | &G, &B, &A)); |
| 539 | EXPECT_EQ(0u, R); |
| 540 | EXPECT_EQ(0u, G); |
| 541 | EXPECT_EQ(255u, B); |
| 542 | EXPECT_EQ(76u, A); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 543 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 544 | // Check that there is no content. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 545 | EXPECT_EQ(2u, FPDFAnnot_GetStringValue( |
| 546 | annot.get(), pdfium::annotation::kContents, nullptr, 0)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 547 | |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 548 | // Check that the rectangle coordinates are correct. |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 549 | // Note that upon rendering, the rectangle coordinates will be adjusted. |
| 550 | FS_RECTF rect; |
| 551 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 552 | EXPECT_EQ(351.820404f, rect.left); |
| 553 | EXPECT_EQ(583.830688f, rect.bottom); |
| 554 | EXPECT_EQ(475.336090f, rect.right); |
| 555 | EXPECT_EQ(681.535034f, rect.top); |
| 556 | } |
Tom Sepez | ef43c26 | 2018-11-07 16:41:32 +0000 | [diff] [blame] | 557 | { |
| 558 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 559 | CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a"); |
| 560 | } |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 561 | UnloadPageNoEvents(page); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 562 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 563 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 564 | TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 565 | // Open a file with one annotation and load its first page. |
| 566 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 567 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 568 | ASSERT_TRUE(page); |
| 569 | |
| 570 | // Add an annotation with an illegal subtype. |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 571 | ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 572 | |
| 573 | UnloadPage(page); |
| 574 | } |
| 575 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 576 | TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 577 | // Open a file with no annotation and load its first page. |
| 578 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 579 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 580 | ASSERT_TRUE(page); |
| 581 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(page)); |
| 582 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 583 | { |
| 584 | // Add a text annotation to the page. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 585 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 586 | ASSERT_TRUE(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 587 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 588 | // Check that there is now 1 annotations on this page. |
| 589 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 590 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 591 | // Check that the subtype of the annotation is correct. |
| 592 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 593 | } |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 594 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 595 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 596 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 597 | ASSERT_TRUE(annot); |
| 598 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 599 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 600 | // Set the color of the annotation. |
| 601 | ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51, |
| 602 | 102, 153, 204)); |
| 603 | // Check that the color has been set correctly. |
| 604 | unsigned int R; |
| 605 | unsigned int G; |
| 606 | unsigned int B; |
| 607 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 608 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 609 | &G, &B, &A)); |
| 610 | EXPECT_EQ(51u, R); |
| 611 | EXPECT_EQ(102u, G); |
| 612 | EXPECT_EQ(153u, B); |
| 613 | EXPECT_EQ(204u, A); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 614 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 615 | // Change the color of the annotation. |
| 616 | ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204, |
| 617 | 153, 102, 51)); |
| 618 | // Check that the color has been set correctly. |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 619 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 620 | &G, &B, &A)); |
| 621 | EXPECT_EQ(204u, R); |
| 622 | EXPECT_EQ(153u, G); |
| 623 | EXPECT_EQ(102u, B); |
| 624 | EXPECT_EQ(51u, A); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 625 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 626 | // Set the annotation rectangle. |
| 627 | FS_RECTF rect; |
| 628 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 629 | EXPECT_EQ(0.f, rect.left); |
| 630 | EXPECT_EQ(0.f, rect.right); |
| 631 | rect.left = 35; |
| 632 | rect.bottom = 150; |
| 633 | rect.right = 53; |
| 634 | rect.top = 165; |
| 635 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 636 | // Check that the annotation rectangle has been set correctly. |
| 637 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 638 | EXPECT_EQ(35.f, rect.left); |
| 639 | EXPECT_EQ(150.f, rect.bottom); |
| 640 | EXPECT_EQ(53.f, rect.right); |
| 641 | EXPECT_EQ(165.f, rect.top); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 642 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 643 | // Set the content of the annotation. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 644 | static const wchar_t kContents[] = L"Hello! This is a customized content."; |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 645 | ScopedFPDFWideString text = GetFPDFWideString(kContents); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 646 | ASSERT_TRUE(FPDFAnnot_SetStringValue( |
| 647 | annot.get(), pdfium::annotation::kContents, text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 648 | // Check that the content has been set correctly. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 649 | unsigned long length_bytes = FPDFAnnot_GetStringValue( |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 650 | annot.get(), pdfium::annotation::kContents, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 651 | ASSERT_EQ(74u, length_bytes); |
| 652 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 653 | EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(), |
| 654 | pdfium::annotation::kContents, |
| 655 | buf.data(), length_bytes)); |
| 656 | EXPECT_EQ(kContents, GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 657 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 658 | UnloadPage(page); |
| 659 | } |
| 660 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 661 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 662 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 663 | #define MAYBE_AddAndSaveUnderlineAnnotation \ |
| 664 | DISABLED_AddAndSaveUnderlineAnnotation |
| 665 | #else |
| 666 | #define MAYBE_AddAndSaveUnderlineAnnotation AddAndSaveUnderlineAnnotation |
| 667 | #endif |
| 668 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndSaveUnderlineAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 669 | // Open a file with one annotation and load its first page. |
| 670 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 671 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 672 | ASSERT_TRUE(page); |
| 673 | |
| 674 | // Check that there is a total of one annotation on its first page, and verify |
| 675 | // its quadpoints. |
| 676 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 677 | FS_QUADPOINTSF quadpoints; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 678 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 679 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 680 | ASSERT_TRUE(annot); |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 681 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 682 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 683 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 684 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 685 | EXPECT_EQ(706.264465f, quadpoints.y4); |
| 686 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 687 | |
| 688 | // Add an underline annotation to the page and set its quadpoints. |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 689 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 690 | ScopedFPDFAnnotation annot( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 691 | FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE)); |
| 692 | ASSERT_TRUE(annot); |
| 693 | quadpoints.x1 = 140.802643f; |
| 694 | quadpoints.x3 = 140.802643f; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 695 | ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 696 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 697 | |
| 698 | // Save the document, closing the page and document. |
| 699 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 700 | UnloadPage(page); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 701 | |
| 702 | // Open the saved document. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 703 | static const char kMd5[] = "dba153419f67b7c0c0e3d22d3e8910d5"; |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 704 | |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 705 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 706 | page = LoadSavedPage(0); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 707 | VerifySavedRendering(page, 612, 792, kMd5); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 708 | |
| 709 | // Check that the saved document has 2 annotations on the first page |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 710 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 711 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 712 | { |
| 713 | // Check that the second annotation is an underline annotation and verify |
| 714 | // its quadpoints. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 715 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 716 | ASSERT_TRUE(new_annot); |
| 717 | EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get())); |
| 718 | FS_QUADPOINTSF new_quadpoints; |
| 719 | ASSERT_TRUE( |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 720 | FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 721 | EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f); |
| 722 | EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f); |
| 723 | EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f); |
| 724 | EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f); |
| 725 | } |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 726 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 727 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 728 | CloseSavedDocument(); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 729 | } |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 730 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 731 | TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) { |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 732 | // Open a file with four annotations and load its first page. |
| 733 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
| 734 | FPDF_PAGE page = LoadPage(0); |
| 735 | ASSERT_TRUE(page); |
| 736 | EXPECT_EQ(4, FPDFPage_GetAnnotCount(page)); |
| 737 | |
| 738 | // Retrieve the highlight annotation. |
| 739 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 740 | ASSERT_TRUE(annot); |
| 741 | ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot)); |
| 742 | |
| 743 | FS_QUADPOINTSF quadpoints; |
| 744 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints)); |
| 745 | |
| 746 | { |
| 747 | // Verify the current one set of quadpoints. |
| 748 | ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 749 | |
| 750 | EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f); |
| 751 | EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f); |
| 752 | EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f); |
| 753 | EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f); |
| 754 | } |
| 755 | |
| 756 | { |
| 757 | // Update the quadpoints. |
| 758 | FS_QUADPOINTSF new_quadpoints = quadpoints; |
| 759 | new_quadpoints.y1 -= 20.f; |
| 760 | new_quadpoints.y2 -= 20.f; |
| 761 | new_quadpoints.y3 -= 20.f; |
| 762 | new_quadpoints.y4 -= 20.f; |
| 763 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints)); |
| 764 | |
| 765 | // Verify added quadpoint set |
| 766 | ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 767 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints)); |
| 768 | EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f); |
| 769 | EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f); |
| 770 | EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f); |
| 771 | EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f); |
| 772 | } |
| 773 | |
| 774 | { |
| 775 | // Append a new set of quadpoints. |
| 776 | FS_QUADPOINTSF new_quadpoints = quadpoints; |
| 777 | new_quadpoints.y1 += 20.f; |
| 778 | new_quadpoints.y2 += 20.f; |
| 779 | new_quadpoints.y3 += 20.f; |
| 780 | new_quadpoints.y4 += 20.f; |
| 781 | ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints)); |
| 782 | |
| 783 | // Verify added quadpoint set |
| 784 | ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 785 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints)); |
| 786 | EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f); |
| 787 | EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f); |
| 788 | EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f); |
| 789 | EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f); |
| 790 | } |
| 791 | |
| 792 | { |
| 793 | // Setting and getting quadpoints at out-of-bound index should fail |
| 794 | EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints)); |
| 795 | EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints)); |
| 796 | } |
| 797 | |
| 798 | FPDFPage_CloseAnnot(annot); |
| 799 | |
| 800 | // Retrieve the square annotation |
| 801 | FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2); |
| 802 | |
| 803 | { |
| 804 | // Check that attempting to set its quadpoints would fail |
| 805 | ASSERT_TRUE(squareAnnot); |
| 806 | EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot)); |
| 807 | EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot)); |
| 808 | EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints)); |
| 809 | } |
| 810 | |
| 811 | FPDFPage_CloseAnnot(squareAnnot); |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 812 | UnloadPage(page); |
| 813 | } |
| 814 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 815 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 816 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 817 | #define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP |
| 818 | #else |
| 819 | #define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP |
| 820 | #endif |
| 821 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) { |
Lei Zhang | e4cdac5 | 2019-04-30 16:45:57 +0000 | [diff] [blame] | 822 | #if defined(OS_MACOSX) |
Lei Zhang | 0f6b434 | 2020-02-25 20:00:39 +0000 | [diff] [blame] | 823 | static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a"; |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 824 | static const char kMd5ModifiedHighlight[] = |
Lei Zhang | 0f6b434 | 2020-02-25 20:00:39 +0000 | [diff] [blame] | 825 | "e64bf648f6e9354d1f3eedb47a2c9498"; |
| 826 | static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf"; |
Lei Zhang | e67bcc7 | 2019-04-30 18:55:58 +0000 | [diff] [blame] | 827 | #elif defined(OS_WIN) |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 828 | static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5"; |
| 829 | static const char kMd5ModifiedHighlight[] = |
| 830 | "66f3caef3a7d488a4fa1ad37fc06310e"; |
| 831 | static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563"; |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 832 | #else |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 833 | static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5"; |
| 834 | static const char kMd5ModifiedHighlight[] = |
| 835 | "66f3caef3a7d488a4fa1ad37fc06310e"; |
| 836 | static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563"; |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 837 | #endif |
| 838 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 839 | // Open a file with four annotations and load its first page. |
| 840 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 841 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 842 | ASSERT_TRUE(page); |
| 843 | EXPECT_EQ(4, FPDFPage_GetAnnotCount(page)); |
| 844 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 845 | // Check that the original file renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 846 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 847 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 848 | CompareBitmap(bitmap.get(), 612, 792, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 849 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 850 | |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 851 | FS_RECTF rect; |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 852 | FS_RECTF new_rect; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 853 | |
| 854 | // Retrieve the highlight annotation which has its AP stream already defined. |
| 855 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 856 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 857 | ASSERT_TRUE(annot); |
| 858 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 859 | |
| 860 | // Check that color cannot be set when an AP stream is defined already. |
| 861 | EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51, |
| 862 | 102, 153, 204)); |
| 863 | |
| 864 | // Verify its attachment points. |
| 865 | FS_QUADPOINTSF quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 866 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 867 | EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f); |
| 868 | EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f); |
| 869 | EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f); |
| 870 | EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f); |
| 871 | |
| 872 | // Check that updating the attachment points would succeed. |
| 873 | quadpoints.x1 -= 50.f; |
| 874 | quadpoints.x2 -= 50.f; |
| 875 | quadpoints.x3 -= 50.f; |
| 876 | quadpoints.x4 -= 50.f; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 877 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 878 | FS_QUADPOINTSF new_quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 879 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 880 | EXPECT_EQ(quadpoints.x1, new_quadpoints.x1); |
| 881 | EXPECT_EQ(quadpoints.y1, new_quadpoints.y1); |
| 882 | EXPECT_EQ(quadpoints.x4, new_quadpoints.x4); |
| 883 | EXPECT_EQ(quadpoints.y4, new_quadpoints.y4); |
| 884 | |
| 885 | // Check that updating quadpoints does not change the annotation's position. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 886 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 887 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 888 | CompareBitmap(bitmap.get(), 612, 792, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 889 | } |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 890 | |
| 891 | // Verify its annotation rectangle. |
| 892 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 893 | EXPECT_NEAR(67.7299f, rect.left, 0.001f); |
| 894 | EXPECT_NEAR(704.296f, rect.bottom, 0.001f); |
| 895 | EXPECT_NEAR(136.325f, rect.right, 0.001f); |
| 896 | EXPECT_NEAR(721.292f, rect.top, 0.001f); |
| 897 | |
| 898 | // Check that updating the rectangle would succeed. |
| 899 | rect.left -= 60.f; |
| 900 | rect.right -= 60.f; |
| 901 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 902 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 903 | EXPECT_EQ(rect.right, new_rect.right); |
| 904 | } |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 905 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 906 | // Check that updating the rectangle changes the annotation's position. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 907 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 908 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 909 | CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 910 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 911 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 912 | { |
| 913 | // Retrieve the square annotation which has its AP stream already defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 914 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 915 | ASSERT_TRUE(annot); |
| 916 | EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 917 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 918 | // Check that updating the rectangle would succeed. |
| 919 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 920 | rect.left += 70.f; |
| 921 | rect.right += 70.f; |
| 922 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 923 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 924 | EXPECT_EQ(rect.right, new_rect.right); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 925 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 926 | // Check that updating the rectangle changes the square annotation's |
| 927 | // position. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 928 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 929 | CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 930 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 931 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 932 | UnloadPage(page); |
| 933 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 934 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 935 | TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) { |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 936 | // Open a file with multiline markup annotations. |
| 937 | ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf")); |
| 938 | FPDF_PAGE page = LoadPage(0); |
| 939 | ASSERT_TRUE(page); |
| 940 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 941 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 942 | ASSERT_TRUE(annot); |
| 943 | |
| 944 | // This is a three line annotation. |
| 945 | EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get())); |
| 946 | } |
| 947 | UnloadPage(page); |
| 948 | |
| 949 | // null annotation should return 0 |
| 950 | EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr)); |
| 951 | } |
| 952 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 953 | TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) { |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 954 | // Open a file with 3 annotations on its first page. |
| 955 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 956 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 957 | ASSERT_TRUE(page); |
| 958 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 959 | |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 960 | FS_RECTF rect; |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 961 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 962 | // Check that the annotations have the expected rectangle coordinates. |
| 963 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 964 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 965 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 966 | EXPECT_NEAR(86.1971f, rect.left, 0.001f); |
| 967 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 968 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 969 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 970 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 971 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 972 | EXPECT_NEAR(149.8127f, rect.left, 0.001f); |
| 973 | } |
| 974 | |
| 975 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 976 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 977 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 978 | EXPECT_NEAR(351.8204f, rect.left, 0.001f); |
| 979 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 980 | |
| 981 | // Check that nothing happens when attempting to remove an annotation with an |
| 982 | // out-of-bound index. |
| 983 | EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4)); |
| 984 | EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1)); |
| 985 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 986 | |
| 987 | // Remove the second annotation. |
| 988 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1)); |
| 989 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 990 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 2)); |
| 991 | |
| 992 | // Save the document, closing the page and document. |
| 993 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 994 | UnloadPageNoEvents(page); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 995 | |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 996 | // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1?? |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 997 | // Open the saved document. |
| 998 | std::string new_file = GetString(); |
| 999 | FPDF_FILEACCESS file_access; |
| 1000 | memset(&file_access, 0, sizeof(file_access)); |
| 1001 | file_access.m_FileLen = new_file.size(); |
| 1002 | file_access.m_GetBlock = GetBlockFromString; |
| 1003 | file_access.m_Param = &new_file; |
| 1004 | FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 1005 | ASSERT_TRUE(new_doc); |
| 1006 | FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); |
| 1007 | ASSERT_TRUE(new_page); |
| 1008 | |
| 1009 | // Check that the saved document has 2 annotations on the first page. |
| 1010 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page)); |
| 1011 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1012 | // Check that the remaining 2 annotations are the original 1st and 3rd ones |
| 1013 | // by verifying their rectangle coordinates. |
| 1014 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1015 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1016 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 1017 | EXPECT_NEAR(86.1971f, rect.left, 0.001f); |
| 1018 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1019 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1020 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1021 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1022 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 1023 | EXPECT_NEAR(351.8204f, rect.left, 0.001f); |
| 1024 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1025 | FPDF_ClosePage(new_page); |
| 1026 | FPDF_CloseDocument(new_doc); |
| 1027 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1028 | |
Hui Yingst | 4a21df0 | 2020-05-13 03:15:44 +0000 | [diff] [blame] | 1029 | TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) { |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1030 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Hui Yingst | 4a21df0 | 2020-05-13 03:15:44 +0000 | [diff] [blame] | 1031 | #if defined(OS_LINUX) |
| 1032 | static const char kMd5ModifiedPath[] = "d76382fd57fafad0233f7549f871dafa"; |
| 1033 | static const char kMd5TwoPaths[] = "323151317b8cb62130546c7b8d70f622"; |
| 1034 | static const char kMd5NewAnnot[] = "9a3b02d876620d19787549ee1100b63c"; |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1035 | #else |
Hui Yingst | 4a21df0 | 2020-05-13 03:15:44 +0000 | [diff] [blame] | 1036 | static const char kMd5ModifiedPath[] = "c9ba60887a312370d9a32198aa53aca4"; |
| 1037 | static const char kMd5TwoPaths[] = "0768d56373094fcdf4ddf3f3364c006f"; |
| 1038 | static const char kMd5NewAnnot[] = "6f7e1c189bcfac90ffccf2a527857006"; |
| 1039 | #endif // defined(OS_LINUX) |
| 1040 | #else |
| 1041 | #if defined(OS_WIN) |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1042 | static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1"; |
| 1043 | static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6"; |
| 1044 | static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e"; |
Hui Yingst | 4a21df0 | 2020-05-13 03:15:44 +0000 | [diff] [blame] | 1045 | #elif defined(OS_MACOSX) |
| 1046 | static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e"; |
| 1047 | static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f"; |
| 1048 | static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1049 | #else |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1050 | static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517"; |
| 1051 | static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6"; |
| 1052 | static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1053 | #endif |
Hui Yingst | 4a21df0 | 2020-05-13 03:15:44 +0000 | [diff] [blame] | 1054 | #endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1055 | |
| 1056 | // Open a file with two annotations and load its first page. |
| 1057 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1058 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1059 | ASSERT_TRUE(page); |
| 1060 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 1061 | |
| 1062 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1063 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1064 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Hui Yingst | b4baceb | 2020-04-28 23:46:10 +0000 | [diff] [blame] | 1065 | CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1066 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1067 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1068 | { |
| 1069 | // Retrieve the stamp annotation which has its AP stream already defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1070 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1071 | ASSERT_TRUE(annot); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1072 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1073 | // Check that this annotation has one path object and retrieve it. |
| 1074 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 1075 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1076 | FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1); |
| 1077 | EXPECT_FALSE(path); |
| 1078 | path = FPDFAnnot_GetObject(annot.get(), 0); |
| 1079 | EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path)); |
| 1080 | EXPECT_TRUE(path); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1081 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1082 | // Modify the color of the path object. |
Lei Zhang | 3475b48 | 2019-05-13 18:30:57 +0000 | [diff] [blame] | 1083 | EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1084 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1085 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1086 | // Check that the page with the modified annotation renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1087 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1088 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1089 | CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1090 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 1091 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1092 | // Add a second path object to the same annotation. |
| 1093 | FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84); |
| 1094 | EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88)); |
Lei Zhang | 3475b48 | 2019-05-13 18:30:57 +0000 | [diff] [blame] | 1095 | EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100)); |
| 1096 | EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1097 | EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1)); |
| 1098 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot)); |
| 1099 | EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get())); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 1100 | |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 1101 | // The object is in the annontation, not in the page, so the page object |
| 1102 | // array should not change. |
| 1103 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
| 1104 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1105 | // Check that the page with an annotation with two paths renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1106 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1107 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1108 | CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1109 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 1110 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1111 | // Delete the newly added path object. |
| 1112 | EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1)); |
| 1113 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 1114 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1115 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 1116 | |
| 1117 | // Check that the page renders the same as before. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1118 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1119 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1120 | CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1121 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1122 | |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1123 | FS_RECTF rect; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1124 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1125 | { |
| 1126 | // Create another stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1127 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1128 | ASSERT_TRUE(annot); |
| 1129 | rect.left = 200.f; |
| 1130 | rect.bottom = 400.f; |
| 1131 | rect.right = 500.f; |
| 1132 | rect.top = 600.f; |
| 1133 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1134 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1135 | // Add a new path to the annotation. |
| 1136 | FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500); |
| 1137 | EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400)); |
| 1138 | EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600)); |
| 1139 | EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550)); |
| 1140 | EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450)); |
Lei Zhang | 3475b48 | 2019-05-13 18:30:57 +0000 | [diff] [blame] | 1141 | EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180)); |
| 1142 | EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1143 | EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1)); |
| 1144 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check)); |
| 1145 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 1146 | |
| 1147 | // Check that the annotation's bounding box came from its rectangle. |
| 1148 | FS_RECTF new_rect; |
| 1149 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 1150 | EXPECT_EQ(rect.left, new_rect.left); |
| 1151 | EXPECT_EQ(rect.bottom, new_rect.bottom); |
| 1152 | EXPECT_EQ(rect.right, new_rect.right); |
| 1153 | EXPECT_EQ(rect.top, new_rect.top); |
| 1154 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1155 | |
| 1156 | // Save the document, closing the page and document. |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1157 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1158 | UnloadPage(page); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1159 | |
| 1160 | // Open the saved document. |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1161 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1162 | page = LoadSavedPage(0); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1163 | VerifySavedRendering(page, 595, 842, kMd5NewAnnot); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1164 | |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1165 | // Check that the document has a correct count of annotations and objects. |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1166 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1167 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1168 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1169 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1170 | ASSERT_TRUE(annot); |
| 1171 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1172 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1173 | // Check that the new annotation's rectangle is as defined. |
| 1174 | FS_RECTF new_rect; |
| 1175 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 1176 | EXPECT_EQ(rect.left, new_rect.left); |
| 1177 | EXPECT_EQ(rect.bottom, new_rect.bottom); |
| 1178 | EXPECT_EQ(rect.right, new_rect.right); |
| 1179 | EXPECT_EQ(rect.top, new_rect.top); |
| 1180 | } |
| 1181 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1182 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1183 | CloseSavedDocument(); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1184 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1185 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1186 | TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) { |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1187 | // Open a file with an annotation and load its first page. |
| 1188 | ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1189 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1190 | ASSERT_TRUE(page); |
| 1191 | |
| 1192 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1193 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1194 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1195 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 1196 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1197 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1198 | { |
| 1199 | // Retrieve the annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1200 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1201 | ASSERT_TRUE(annot); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1202 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1203 | // Check that the original flag values are as expected. |
| 1204 | int flags = FPDFAnnot_GetFlags(annot.get()); |
| 1205 | EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 1206 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1207 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1208 | // Set the HIDDEN flag. |
| 1209 | flags |= FPDF_ANNOT_FLAG_HIDDEN; |
| 1210 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags)); |
| 1211 | flags = FPDFAnnot_GetFlags(annot.get()); |
| 1212 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 1213 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1214 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1215 | // Check that the page renders correctly without rendering the annotation. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1216 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1217 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Hui Yingst | b4baceb | 2020-04-28 23:46:10 +0000 | [diff] [blame] | 1218 | CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1219 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1220 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1221 | // Unset the HIDDEN flag. |
| 1222 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE)); |
| 1223 | EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get())); |
| 1224 | flags &= ~FPDF_ANNOT_FLAG_HIDDEN; |
| 1225 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags)); |
| 1226 | flags = FPDFAnnot_GetFlags(annot.get()); |
| 1227 | EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 1228 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1229 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1230 | // Check that the page renders correctly as before. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1231 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1232 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1233 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 1234 | } |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1235 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1236 | |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1237 | UnloadPage(page); |
| 1238 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1239 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1240 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 1241 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 1242 | #define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage |
| 1243 | #else |
| 1244 | #define MAYBE_AddAndModifyImage AddAndModifyImage |
| 1245 | #endif |
| 1246 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) { |
Lei Zhang | e4cdac5 | 2019-04-30 16:45:57 +0000 | [diff] [blame] | 1247 | #if defined(OS_MACOSX) |
Lei Zhang | 0f6b434 | 2020-02-25 20:00:39 +0000 | [diff] [blame] | 1248 | static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9"; |
| 1249 | static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99"; |
Lei Zhang | e67bcc7 | 2019-04-30 18:55:58 +0000 | [diff] [blame] | 1250 | #elif defined(OS_WIN) |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1251 | static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b"; |
| 1252 | static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1253 | #else |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1254 | static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935"; |
| 1255 | static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1256 | #endif |
| 1257 | |
| 1258 | // Open a file with two annotations and load its first page. |
| 1259 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1260 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1261 | ASSERT_TRUE(page); |
| 1262 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 1263 | |
| 1264 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1265 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1266 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Hui Yingst | b4baceb | 2020-04-28 23:46:10 +0000 | [diff] [blame] | 1267 | CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1268 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1269 | |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1270 | constexpr int kBitmapSize = 200; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1271 | FPDF_BITMAP image_bitmap; |
| 1272 | |
| 1273 | { |
| 1274 | // Create a stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1275 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1276 | ASSERT_TRUE(annot); |
| 1277 | FS_RECTF rect; |
| 1278 | rect.left = 200.f; |
| 1279 | rect.bottom = 600.f; |
| 1280 | rect.right = 400.f; |
| 1281 | rect.top = 800.f; |
| 1282 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 1283 | |
| 1284 | // Add a solid-color translucent image object to the new annotation. |
| 1285 | image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1); |
| 1286 | FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, |
| 1287 | 0xeeeecccc); |
| 1288 | EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap)); |
| 1289 | EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap)); |
| 1290 | FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document()); |
| 1291 | ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap)); |
| 1292 | ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0, |
| 1293 | kBitmapSize, 0, 0)); |
| 1294 | FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600); |
| 1295 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object)); |
| 1296 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1297 | |
| 1298 | // Check that the page renders correctly with the new image object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1299 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1300 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1301 | CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1302 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1303 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1304 | { |
| 1305 | // Retrieve the newly added stamp annotation and its image object. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1306 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1307 | ASSERT_TRUE(annot); |
| 1308 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 1309 | FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0); |
| 1310 | EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1311 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1312 | // Modify the image in the new annotation. |
| 1313 | FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, |
| 1314 | 0xff000000); |
| 1315 | ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap)); |
| 1316 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object)); |
| 1317 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1318 | |
| 1319 | // Save the document, closing the page and document. |
| 1320 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1321 | UnloadPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1322 | FPDFBitmap_Destroy(image_bitmap); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1323 | |
| 1324 | // Test that the saved document renders the modified image object correctly. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1325 | VerifySavedDocument(595, 842, kMd5ModifiedImage); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1326 | } |
| 1327 | |
Hui Yingst | 6cd754f | 2020-05-14 04:05:25 +0000 | [diff] [blame] | 1328 | TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) { |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1329 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Hui Yingst | 6cd754f | 2020-05-14 04:05:25 +0000 | [diff] [blame] | 1330 | #if defined(OS_LINUX) |
| 1331 | static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a"; |
| 1332 | static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c"; |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1333 | #else |
Hui Yingst | 6cd754f | 2020-05-14 04:05:25 +0000 | [diff] [blame] | 1334 | static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48"; |
| 1335 | static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc"; |
| 1336 | #endif // defined(OS_LINUX) |
| 1337 | #else |
| 1338 | #if defined(OS_WIN) |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1339 | static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6"; |
| 1340 | static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d"; |
Hui Yingst | 6cd754f | 2020-05-14 04:05:25 +0000 | [diff] [blame] | 1341 | #elif defined(OS_MACOSX) |
| 1342 | static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da"; |
| 1343 | static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1344 | #else |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1345 | static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306"; |
| 1346 | static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1347 | #endif |
Hui Yingst | 6cd754f | 2020-05-14 04:05:25 +0000 | [diff] [blame] | 1348 | #endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1349 | |
| 1350 | // Open a file with two annotations and load its first page. |
| 1351 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1352 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1353 | ASSERT_TRUE(page); |
| 1354 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 1355 | |
| 1356 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1357 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1358 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Hui Yingst | b4baceb | 2020-04-28 23:46:10 +0000 | [diff] [blame] | 1359 | CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1360 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1361 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1362 | { |
| 1363 | // Create a stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1364 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1365 | ASSERT_TRUE(annot); |
| 1366 | FS_RECTF rect; |
| 1367 | rect.left = 200.f; |
| 1368 | rect.bottom = 550.f; |
| 1369 | rect.right = 450.f; |
| 1370 | rect.top = 650.f; |
| 1371 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1372 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1373 | // Add a translucent text object to the new annotation. |
| 1374 | FPDF_PAGEOBJECT text_object = |
| 1375 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 1376 | EXPECT_TRUE(text_object); |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1377 | ScopedFPDFWideString text = |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1378 | GetFPDFWideString(L"I'm a translucent text laying on other text."); |
| 1379 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
Lei Zhang | 3475b48 | 2019-05-13 18:30:57 +0000 | [diff] [blame] | 1380 | EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1381 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600); |
| 1382 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object)); |
| 1383 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1384 | |
| 1385 | // Check that the page renders correctly with the new text object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1386 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1387 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1388 | CompareBitmap(bitmap.get(), 595, 842, kMd5NewText); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1389 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1390 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1391 | { |
| 1392 | // Retrieve the newly added stamp annotation and its text object. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1393 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1394 | ASSERT_TRUE(annot); |
| 1395 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 1396 | FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0); |
| 1397 | EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1398 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1399 | // Modify the text in the new annotation. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1400 | ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!"); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1401 | EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get())); |
| 1402 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object)); |
| 1403 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1404 | |
| 1405 | // Check that the page renders correctly with the modified text object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1406 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1407 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1408 | CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1409 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1410 | |
| 1411 | // Remove the new annotation, and check that the page renders as before. |
| 1412 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2)); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1413 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1414 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Hui Yingst | b4baceb | 2020-04-28 23:46:10 +0000 | [diff] [blame] | 1415 | CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1416 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1417 | |
| 1418 | UnloadPage(page); |
| 1419 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1420 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1421 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 1422 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 1423 | #define MAYBE_GetSetStringValue DISABLED_GetSetStringValue |
| 1424 | #else |
| 1425 | #define MAYBE_GetSetStringValue GetSetStringValue |
| 1426 | #endif |
| 1427 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_GetSetStringValue) { |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1428 | // Open a file with four annotations and load its first page. |
| 1429 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1430 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1431 | ASSERT_TRUE(page); |
| 1432 | |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1433 | static const wchar_t kNewDate[] = L"D:201706282359Z00'00'"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1434 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1435 | { |
| 1436 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1437 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1438 | ASSERT_TRUE(annot); |
| 1439 | |
| 1440 | // Check that a non-existent key does not exist. |
| 1441 | EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none")); |
| 1442 | |
| 1443 | // Check that the string value of a non-string dictionary entry is empty. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1444 | EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1445 | EXPECT_EQ(FPDF_OBJECT_REFERENCE, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1446 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP)); |
| 1447 | EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP, |
| 1448 | nullptr, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1449 | |
| 1450 | // Check that the string value of the hash is correct. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1451 | static const char kHashKey[] = "AAPL:Hash"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1452 | EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1453 | unsigned long length_bytes = |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1454 | FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1455 | ASSERT_EQ(66u, length_bytes); |
| 1456 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 1457 | EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), |
| 1458 | length_bytes)); |
| 1459 | EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad", |
| 1460 | GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1461 | |
| 1462 | // Check that the string value of the modified date is correct. |
| 1463 | EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1464 | length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, |
| 1465 | nullptr, 0); |
| 1466 | ASSERT_EQ(44u, length_bytes); |
| 1467 | buf = GetFPDFWideStringBuffer(length_bytes); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1468 | EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1469 | buf.data(), length_bytes)); |
| 1470 | EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1471 | |
| 1472 | // Update the date entry for the annotation. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1473 | ScopedFPDFWideString text = GetFPDFWideString(kNewDate); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1474 | EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM, |
| 1475 | text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1476 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1477 | |
| 1478 | // Save the document, closing the page and document. |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1479 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1480 | UnloadPage(page); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1481 | |
Lei Zhang | e4cdac5 | 2019-04-30 16:45:57 +0000 | [diff] [blame] | 1482 | #if defined(OS_MACOSX) |
Lei Zhang | 0f6b434 | 2020-02-25 20:00:39 +0000 | [diff] [blame] | 1483 | static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb"; |
Lei Zhang | e67bcc7 | 2019-04-30 18:55:58 +0000 | [diff] [blame] | 1484 | #elif defined(OS_WIN) |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1485 | static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1486 | #else |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1487 | static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1488 | #endif |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 1489 | |
| 1490 | // Open the saved annotation. |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1491 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1492 | page = LoadSavedPage(0); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1493 | VerifySavedRendering(page, 595, 842, kMd5); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1494 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1495 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0)); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1496 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1497 | // Check that the string value of the modified date is the newly-set value. |
| 1498 | EXPECT_EQ(FPDF_OBJECT_STRING, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1499 | FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1500 | unsigned long length_bytes = FPDFAnnot_GetStringValue( |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1501 | new_annot.get(), pdfium::annotation::kM, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1502 | ASSERT_EQ(44u, length_bytes); |
| 1503 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1504 | EXPECT_EQ(44u, |
| 1505 | FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1506 | buf.data(), length_bytes)); |
| 1507 | EXPECT_EQ(kNewDate, GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1508 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1509 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1510 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1511 | CloseSavedDocument(); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1512 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1513 | |
rycsmith | 3e78560 | 2019-03-05 21:48:36 +0000 | [diff] [blame] | 1514 | TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) { |
Mansi Awasthi | 0b5da67 | 2020-01-23 18:17:18 +0000 | [diff] [blame] | 1515 | // Open a file with four text annotations and load its first page. |
rycsmith | 3e78560 | 2019-03-05 21:48:36 +0000 | [diff] [blame] | 1516 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 1517 | FPDF_PAGE page = LoadPage(0); |
| 1518 | ASSERT_TRUE(page); |
| 1519 | { |
| 1520 | // First two annotations do not have "MaxLen" attribute. |
| 1521 | for (int i = 0; i < 2; i++) { |
| 1522 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i)); |
| 1523 | ASSERT_TRUE(annot); |
| 1524 | |
| 1525 | // Verify that no "MaxLen" key present. |
| 1526 | EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen")); |
| 1527 | |
| 1528 | float value; |
| 1529 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value)); |
| 1530 | } |
| 1531 | |
| 1532 | // Annotation in index 2 has "MaxLen" of 10. |
| 1533 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
| 1534 | ASSERT_TRUE(annot); |
| 1535 | |
| 1536 | // Verify that "MaxLen" key present. |
| 1537 | EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen")); |
| 1538 | |
| 1539 | float value; |
| 1540 | EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value)); |
| 1541 | EXPECT_FLOAT_EQ(10.0f, value); |
| 1542 | |
| 1543 | // Check bad inputs. |
| 1544 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value)); |
| 1545 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value)); |
| 1546 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr)); |
| 1547 | // Ask for key that exists but is not a number. |
| 1548 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value)); |
| 1549 | } |
| 1550 | |
| 1551 | UnloadPage(page); |
| 1552 | } |
| 1553 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1554 | TEST_F(FPDFAnnotEmbedderTest, GetSetAP) { |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1555 | // Open a file with four annotations and load its first page. |
| 1556 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1557 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1558 | ASSERT_TRUE(page); |
| 1559 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1560 | { |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1561 | static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a"; |
| 1562 | static constexpr size_t kExpectNormalAPLength = 73970; |
| 1563 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1564 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1565 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1566 | ASSERT_TRUE(annot); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1567 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1568 | // Check that the string value of an AP returns the expected length. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1569 | unsigned long normal_length_bytes = FPDFAnnot_GetAP( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1570 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1571 | ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1572 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1573 | // Check that the string value of an AP is not returned if the buffer is too |
| 1574 | // small. The result buffer should be overwritten with an empty string. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1575 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes); |
| 1576 | // Write in the buffer to verify it's not overwritten. |
| 1577 | memcpy(buf.data(), "abcdefgh", 8); |
| 1578 | EXPECT_EQ(kExpectNormalAPLength, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1579 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1580 | buf.data(), normal_length_bytes - 1)); |
| 1581 | EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8)); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1582 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1583 | // Check that the string value of an AP is returned through a buffer that is |
| 1584 | // the right size. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1585 | EXPECT_EQ(kExpectNormalAPLength, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1586 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1587 | buf.data(), normal_length_bytes)); |
| 1588 | EXPECT_EQ(kMd5NormalAP, |
| 1589 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), |
| 1590 | normal_length_bytes)); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1591 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1592 | // Check that the string value of an AP is returned through a buffer that is |
| 1593 | // larger than necessary. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1594 | buf = GetFPDFWideStringBuffer(normal_length_bytes + 2); |
| 1595 | EXPECT_EQ(kExpectNormalAPLength, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1596 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1597 | buf.data(), normal_length_bytes + 2)); |
| 1598 | EXPECT_EQ(kMd5NormalAP, |
| 1599 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), |
| 1600 | normal_length_bytes)); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1601 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1602 | // Check that getting an AP for a mode that does not have an AP returns an |
| 1603 | // empty string. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1604 | unsigned long rollover_length_bytes = FPDFAnnot_GetAP( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1605 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1606 | ASSERT_EQ(2u, rollover_length_bytes); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1607 | |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1608 | buf = GetFPDFWideStringBuffer(1000); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1609 | EXPECT_EQ(2u, |
| 1610 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1611 | buf.data(), 1000)); |
| 1612 | EXPECT_EQ(L"", GetPlatformWString(buf.data())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1613 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1614 | // Check that setting the AP for an invalid appearance mode fails. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1615 | ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap"); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1616 | EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1617 | EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1618 | ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1619 | EXPECT_FALSE(FPDFAnnot_SetAP( |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1620 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1621 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1622 | // Set the AP correctly now. |
| 1623 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1624 | ap_text.get())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1625 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1626 | // Check that the new annotation value is equal to the value we just set. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1627 | rollover_length_bytes = FPDFAnnot_GetAP( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1628 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1629 | ASSERT_EQ(24u, rollover_length_bytes); |
| 1630 | buf = GetFPDFWideStringBuffer(rollover_length_bytes); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1631 | EXPECT_EQ(24u, |
| 1632 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1633 | buf.data(), rollover_length_bytes)); |
| 1634 | EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1635 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1636 | // Check that the Normal AP was not touched when the Rollover AP was set. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1637 | buf = GetFPDFWideStringBuffer(normal_length_bytes); |
| 1638 | EXPECT_EQ(kExpectNormalAPLength, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1639 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1640 | buf.data(), normal_length_bytes)); |
| 1641 | EXPECT_EQ(kMd5NormalAP, |
| 1642 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), |
| 1643 | normal_length_bytes)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1644 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1645 | |
| 1646 | // Save the modified document, then reopen it. |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1647 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1648 | UnloadPage(page); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1649 | |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1650 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1651 | page = LoadSavedPage(0); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1652 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1653 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1654 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1655 | // Check that the new annotation value is equal to the value we set before |
| 1656 | // saving. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1657 | unsigned long rollover_length_bytes = FPDFAnnot_GetAP( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1658 | new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1659 | ASSERT_EQ(24u, rollover_length_bytes); |
| 1660 | std::vector<FPDF_WCHAR> buf = |
| 1661 | GetFPDFWideStringBuffer(rollover_length_bytes); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1662 | EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(), |
| 1663 | FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1664 | buf.data(), rollover_length_bytes)); |
| 1665 | EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1666 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1667 | |
| 1668 | // Close saved document. |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1669 | CloseSavedPage(page); |
| 1670 | CloseSavedDocument(); |
| 1671 | } |
| 1672 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1673 | TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) { |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1674 | // Open a file with four annotations and load its first page. |
| 1675 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1676 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1677 | ASSERT_TRUE(page); |
| 1678 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1679 | { |
| 1680 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1681 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1682 | ASSERT_TRUE(annot); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1683 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1684 | // Set Down AP. Normal AP is already set. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1685 | ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap"); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1686 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1687 | ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1688 | EXPECT_EQ(73970u, |
| 1689 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1690 | nullptr, 0)); |
| 1691 | EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1692 | nullptr, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1693 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1694 | // Check that setting the Down AP to null removes the Down entry but keeps |
| 1695 | // Normal intact. |
| 1696 | EXPECT_TRUE( |
| 1697 | FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr)); |
| 1698 | EXPECT_EQ(73970u, |
| 1699 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1700 | nullptr, 0)); |
| 1701 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1702 | nullptr, 0)); |
| 1703 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1704 | |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1705 | UnloadPage(page); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1708 | TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) { |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1709 | // Open a file with four annotations and load its first page. |
| 1710 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1711 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1712 | ASSERT_TRUE(page); |
| 1713 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1714 | { |
| 1715 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1716 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1717 | ASSERT_TRUE(annot); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1718 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1719 | // Set Down AP. Normal AP is already set. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1720 | ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap"); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1721 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1722 | ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1723 | EXPECT_EQ(73970u, |
| 1724 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1725 | nullptr, 0)); |
| 1726 | EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1727 | nullptr, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1728 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1729 | // Check that setting the Normal AP to null removes the whole AP dictionary. |
| 1730 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1731 | nullptr)); |
| 1732 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1733 | nullptr, 0)); |
| 1734 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1735 | nullptr, 0)); |
| 1736 | } |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1737 | |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1738 | UnloadPage(page); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1741 | TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) { |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1742 | // Open a file with annotations and load its first page. |
| 1743 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1744 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1745 | ASSERT_TRUE(page); |
Jane Liu | d1ed1ce | 2017-08-24 12:31:10 -0400 | [diff] [blame] | 1746 | EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1747 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1748 | { |
| 1749 | // Retrieve the highlight annotation which has its popup defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1750 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1751 | ASSERT_TRUE(annot); |
| 1752 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 1753 | EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get())); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1754 | static const char kPopupKey[] = "Popup"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1755 | ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey)); |
| 1756 | ASSERT_EQ(FPDF_OBJECT_REFERENCE, |
| 1757 | FPDFAnnot_GetValueType(annot.get(), kPopupKey)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1758 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1759 | // Retrieve and verify the popup of the highlight annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1760 | ScopedFPDFAnnotation popup( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1761 | FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey)); |
| 1762 | ASSERT_TRUE(popup); |
| 1763 | EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get())); |
| 1764 | EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get())); |
| 1765 | FS_RECTF rect; |
| 1766 | ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect)); |
| 1767 | EXPECT_NEAR(612.0f, rect.left, 0.001f); |
| 1768 | EXPECT_NEAR(578.792, rect.bottom, 0.001f); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1769 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1770 | // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail, |
| 1771 | // since "IRT" is not a key in |annot|'s dictionary. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1772 | static const char kIRTKey[] = "IRT"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1773 | ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey)); |
| 1774 | EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1775 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1776 | // Attempting to retrieve |annot|'s parent dictionary as an annotation |
| 1777 | // would fail, since its parent is not an annotation. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1778 | ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1779 | EXPECT_EQ(FPDF_OBJECT_REFERENCE, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1780 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP)); |
| 1781 | EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1782 | } |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1783 | |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1784 | UnloadPage(page); |
| 1785 | } |
| 1786 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1787 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) { |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1788 | // Open file with form text fields. |
| 1789 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1790 | FPDF_PAGE page = LoadPage(0); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1791 | ASSERT_TRUE(page); |
| 1792 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1793 | { |
| 1794 | // Retrieve the first annotation: user-editable text field. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1795 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1796 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1797 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1798 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1799 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1800 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
Mansi Awasthi | 0b5da67 | 2020-01-23 18:17:18 +0000 | [diff] [blame] | 1801 | EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1802 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1803 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1804 | { |
| 1805 | // Retrieve the second annotation: read-only text field. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1806 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1807 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1808 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1809 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1810 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1811 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
Mansi Awasthi | 0b5da67 | 2020-01-23 18:17:18 +0000 | [diff] [blame] | 1812 | EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD); |
| 1813 | } |
| 1814 | |
| 1815 | { |
| 1816 | // Retrieve the fourth annotation: user-editable password text field. |
| 1817 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3)); |
| 1818 | ASSERT_TRUE(annot); |
| 1819 | |
| 1820 | // Check that the flag values are as expected. |
| 1821 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
| 1822 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1823 | EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1824 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1825 | |
| 1826 | UnloadPage(page); |
| 1827 | } |
| 1828 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1829 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) { |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1830 | // Open file with form text fields. |
| 1831 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1832 | FPDF_PAGE page = LoadPage(0); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1833 | ASSERT_TRUE(page); |
| 1834 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1835 | { |
| 1836 | // Retrieve the first annotation: user-editable combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1837 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1838 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1839 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1840 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1841 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1842 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1843 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1844 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1845 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1846 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1847 | { |
| 1848 | // Retrieve the second annotation: regular combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1849 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1850 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1851 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1852 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1853 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1854 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1855 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1856 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1857 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1858 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1859 | { |
| 1860 | // Retrieve the third annotation: read-only combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1861 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1862 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1863 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1864 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1865 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1866 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1867 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1868 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1869 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1870 | |
| 1871 | UnloadPage(page); |
| 1872 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1873 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1874 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1875 | // Open file with form text fields. |
| 1876 | EXPECT_TRUE(OpenDocument("text_form.pdf")); |
| 1877 | FPDF_PAGE page = LoadPage(0); |
| 1878 | ASSERT_TRUE(page); |
| 1879 | |
| 1880 | // Attempt to get an annotation where no annotation exists on page. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1881 | static const FS_POINTF kOriginPoint = {0.0f, 0.0f}; |
| 1882 | EXPECT_FALSE( |
| 1883 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint)); |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 1884 | |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1885 | static const FS_POINTF kValidPoint = {120.0f, 120.0f}; |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 1886 | { |
| 1887 | // Verify there is an annotation. |
| 1888 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1889 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint)); |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 1890 | EXPECT_TRUE(annot); |
| 1891 | } |
| 1892 | |
| 1893 | // Try other bad inputs at a valid location. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1894 | EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint)); |
| 1895 | EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint)); |
| 1896 | EXPECT_FALSE( |
| 1897 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint)); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1898 | |
| 1899 | UnloadPage(page); |
| 1900 | } |
| 1901 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1902 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1903 | // Open file with form text fields. |
| 1904 | EXPECT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 1905 | FPDF_PAGE page = LoadPage(0); |
| 1906 | ASSERT_TRUE(page); |
| 1907 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1908 | { |
| 1909 | // Retrieve user-editable text field annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1910 | static const FS_POINTF kPoint = {105.0f, 118.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1911 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1912 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1913 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1914 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1915 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1916 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1917 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1918 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1919 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1920 | { |
| 1921 | // Retrieve read-only text field annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1922 | static const FS_POINTF kPoint = {105.0f, 202.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1923 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1924 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1925 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1926 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1927 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1928 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1929 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1930 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1931 | |
| 1932 | UnloadPage(page); |
| 1933 | } |
| 1934 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1935 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1936 | // Open file with form comboboxes. |
| 1937 | EXPECT_TRUE(OpenDocument("combobox_form.pdf")); |
| 1938 | FPDF_PAGE page = LoadPage(0); |
| 1939 | ASSERT_TRUE(page); |
| 1940 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1941 | { |
| 1942 | // Retrieve user-editable combobox annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1943 | static const FS_POINTF kPoint = {102.0f, 363.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1944 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1945 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1946 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1947 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1948 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1949 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1950 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1951 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1952 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1953 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1954 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1955 | { |
| 1956 | // Retrieve regular combobox annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1957 | static const FS_POINTF kPoint = {102.0f, 413.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1958 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1959 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1960 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1961 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1962 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1963 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1964 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1965 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1966 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1967 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1968 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1969 | { |
| 1970 | // Retrieve read-only combobox annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1971 | static const FS_POINTF kPoint = {102.0f, 513.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1972 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1973 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1974 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1975 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1976 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1977 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1978 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1979 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1980 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1981 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1982 | |
| 1983 | UnloadPage(page); |
| 1984 | } |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1985 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1986 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 1987 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 1988 | #define MAYBE_BUG_1206 DISABLED_BUG_1206 |
| 1989 | #else |
| 1990 | #define MAYBE_BUG_1206 BUG_1206 |
| 1991 | #endif |
| 1992 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_BUG_1206) { |
Lei Zhang | 992e7e2 | 2019-02-04 19:20:58 +0000 | [diff] [blame] | 1993 | static constexpr size_t kExpectedSize = 1609; |
| 1994 | static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc"; |
| 1995 | |
| 1996 | ASSERT_TRUE(OpenDocument("bug_1206.pdf")); |
| 1997 | |
| 1998 | FPDF_PAGE page = LoadPage(0); |
| 1999 | ASSERT_TRUE(page); |
| 2000 | |
| 2001 | ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 2002 | EXPECT_EQ(kExpectedSize, GetString().size()); |
| 2003 | ClearString(); |
| 2004 | |
| 2005 | for (size_t i = 0; i < 10; ++i) { |
| 2006 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 2007 | CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap); |
| 2008 | |
| 2009 | ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 2010 | // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be |
| 2011 | // equal, not bigger. |
| 2012 | EXPECT_LT(kExpectedSize, GetString().size()); |
| 2013 | ClearString(); |
| 2014 | } |
| 2015 | |
| 2016 | UnloadPage(page); |
| 2017 | } |
| 2018 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 2019 | TEST_F(FPDFAnnotEmbedderTest, BUG_1212) { |
| 2020 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| 2021 | FPDF_PAGE page = LoadPage(0); |
| 2022 | ASSERT_TRUE(page); |
| 2023 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(page)); |
| 2024 | |
| 2025 | static const char kTestKey[] = "test"; |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 2026 | static const wchar_t kData[] = L"\xf6\xe4"; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2027 | static const size_t kBufSize = 12; |
| 2028 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 2029 | |
| 2030 | { |
| 2031 | // Add a text annotation to the page. |
| 2032 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT)); |
| 2033 | ASSERT_TRUE(annot); |
| 2034 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 2035 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 2036 | |
| 2037 | // Make sure there is no test key, add set a value there, and read it back. |
| 2038 | std::fill(buf.begin(), buf.end(), 'x'); |
| 2039 | ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2040 | kBufSize)); |
| 2041 | EXPECT_EQ(L"", GetPlatformWString(buf.data())); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 2042 | |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 2043 | ScopedFPDFWideString text = GetFPDFWideString(kData); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 2044 | EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get())); |
| 2045 | |
| 2046 | std::fill(buf.begin(), buf.end(), 'x'); |
| 2047 | ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2048 | kBufSize)); |
| 2049 | EXPECT_EQ(kData, GetPlatformWString(buf.data())); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 2052 | { |
| 2053 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
| 2054 | ASSERT_TRUE(annot); |
Shikha Walia | 87ad417 | 2019-11-08 20:55:19 +0000 | [diff] [blame] | 2055 | const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f}; |
Shikha Walia | b54d7ad | 2019-11-06 02:06:33 +0000 | [diff] [blame] | 2056 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect)); |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 2057 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 2058 | EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get())); |
| 2059 | // Also do the same test for its appearance string. |
| 2060 | std::fill(buf.begin(), buf.end(), 'x'); |
| 2061 | ASSERT_EQ(2u, |
| 2062 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2063 | buf.data(), kBufSize)); |
| 2064 | EXPECT_EQ(L"", GetPlatformWString(buf.data())); |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 2065 | |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 2066 | ScopedFPDFWideString text = GetFPDFWideString(kData); |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 2067 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 2068 | text.get())); |
| 2069 | |
| 2070 | std::fill(buf.begin(), buf.end(), 'x'); |
| 2071 | ASSERT_EQ(6u, |
| 2072 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2073 | buf.data(), kBufSize)); |
| 2074 | EXPECT_EQ(kData, GetPlatformWString(buf.data())); |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 2077 | UnloadPage(page); |
| 2078 | |
| 2079 | { |
| 2080 | // Save a copy, open the copy, and check the annotation again. |
| 2081 | // Note that it renders the rotation. |
| 2082 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 2083 | ASSERT_TRUE(OpenSavedDocument()); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 2084 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 2085 | ASSERT_TRUE(saved_page); |
| 2086 | |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 2087 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page)); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 2088 | { |
| 2089 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0)); |
| 2090 | ASSERT_TRUE(annot); |
| 2091 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 2092 | |
| 2093 | std::fill(buf.begin(), buf.end(), 'x'); |
| 2094 | ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2095 | kBufSize)); |
| 2096 | EXPECT_EQ(kData, GetPlatformWString(buf.data())); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 2099 | { |
| 2100 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0)); |
| 2101 | ASSERT_TRUE(annot); |
| 2102 | // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason. |
| 2103 | // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 2104 | |
| 2105 | std::fill(buf.begin(), buf.end(), 'x'); |
| 2106 | ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2107 | kBufSize)); |
| 2108 | EXPECT_EQ(kData, GetPlatformWString(buf.data())); |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 2111 | CloseSavedPage(saved_page); |
| 2112 | CloseSavedDocument(); |
| 2113 | } |
| 2114 | } |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2115 | |
| 2116 | TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) { |
| 2117 | // Open a file with combobox widget annotations and load its first page. |
| 2118 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 2119 | FPDF_PAGE page = LoadPage(0); |
| 2120 | ASSERT_TRUE(page); |
| 2121 | |
| 2122 | { |
| 2123 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2124 | ASSERT_TRUE(annot); |
| 2125 | |
| 2126 | EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 2127 | |
| 2128 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2129 | ASSERT_TRUE(annot); |
| 2130 | |
| 2131 | EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
Lei Zhang | e7033c8 | 2019-02-26 19:30:49 +0000 | [diff] [blame] | 2132 | |
| 2133 | // Check bad form handle / annot. |
| 2134 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr)); |
| 2135 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr)); |
| 2136 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | UnloadPage(page); |
| 2140 | } |
| 2141 | |
| 2142 | TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) { |
| 2143 | // Open a file with listbox widget annotations and load its first page. |
| 2144 | ASSERT_TRUE(OpenDocument("listbox_form.pdf")); |
| 2145 | FPDF_PAGE page = LoadPage(0); |
| 2146 | ASSERT_TRUE(page); |
| 2147 | |
| 2148 | { |
| 2149 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2150 | ASSERT_TRUE(annot); |
| 2151 | |
| 2152 | EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 2153 | |
| 2154 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2155 | ASSERT_TRUE(annot); |
| 2156 | |
| 2157 | EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 2158 | } |
| 2159 | |
| 2160 | UnloadPage(page); |
| 2161 | } |
| 2162 | |
| 2163 | TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) { |
| 2164 | // Open a file with ink annotations and load its first page. |
| 2165 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 2166 | FPDF_PAGE page = LoadPage(0); |
| 2167 | ASSERT_TRUE(page); |
| 2168 | |
| 2169 | { |
| 2170 | // annotations do not have "Opt" array and will return -1 |
| 2171 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2172 | ASSERT_TRUE(annot); |
| 2173 | |
| 2174 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 2175 | |
| 2176 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2177 | ASSERT_TRUE(annot); |
| 2178 | |
| 2179 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 2180 | } |
| 2181 | |
| 2182 | UnloadPage(page); |
| 2183 | } |
| 2184 | |
| 2185 | TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) { |
| 2186 | // Open a file with combobox widget annotations and load its first page. |
| 2187 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 2188 | FPDF_PAGE page = LoadPage(0); |
| 2189 | ASSERT_TRUE(page); |
| 2190 | |
| 2191 | { |
| 2192 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2193 | ASSERT_TRUE(annot); |
| 2194 | |
| 2195 | int index = 0; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2196 | unsigned long length_bytes = |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2197 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2198 | ASSERT_EQ(8u, length_bytes); |
| 2199 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2200 | EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2201 | buf.data(), length_bytes)); |
| 2202 | EXPECT_EQ(L"Foo", GetPlatformWString(buf.data())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2203 | |
| 2204 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2205 | ASSERT_TRUE(annot); |
| 2206 | |
| 2207 | index = 0; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2208 | length_bytes = |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2209 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2210 | ASSERT_EQ(12u, length_bytes); |
| 2211 | buf = GetFPDFWideStringBuffer(length_bytes); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2212 | EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2213 | buf.data(), length_bytes)); |
| 2214 | EXPECT_EQ(L"Apple", GetPlatformWString(buf.data())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2215 | |
| 2216 | index = 25; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2217 | length_bytes = |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2218 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2219 | buf = GetFPDFWideStringBuffer(length_bytes); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2220 | EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2221 | buf.data(), length_bytes)); |
| 2222 | EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2223 | |
Lei Zhang | e7033c8 | 2019-02-26 19:30:49 +0000 | [diff] [blame] | 2224 | // Indices out of range |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2225 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1, |
| 2226 | nullptr, 0)); |
| 2227 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26, |
| 2228 | nullptr, 0)); |
Lei Zhang | e7033c8 | 2019-02-26 19:30:49 +0000 | [diff] [blame] | 2229 | |
| 2230 | // Check bad form handle / annot. |
| 2231 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0)); |
| 2232 | EXPECT_EQ(0u, |
| 2233 | FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0)); |
| 2234 | EXPECT_EQ(0u, |
| 2235 | FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0)); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | UnloadPage(page); |
| 2239 | } |
| 2240 | |
| 2241 | TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) { |
| 2242 | // Open a file with listbox widget annotations and load its first page. |
| 2243 | ASSERT_TRUE(OpenDocument("listbox_form.pdf")); |
| 2244 | FPDF_PAGE page = LoadPage(0); |
| 2245 | ASSERT_TRUE(page); |
| 2246 | |
| 2247 | { |
| 2248 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2249 | ASSERT_TRUE(annot); |
| 2250 | |
| 2251 | int index = 0; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2252 | unsigned long length_bytes = |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2253 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2254 | ASSERT_EQ(8u, length_bytes); |
| 2255 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2256 | EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2257 | buf.data(), length_bytes)); |
| 2258 | EXPECT_EQ(L"Foo", GetPlatformWString(buf.data())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2259 | |
| 2260 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2261 | ASSERT_TRUE(annot); |
| 2262 | |
| 2263 | index = 0; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2264 | length_bytes = |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2265 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2266 | ASSERT_EQ(12u, length_bytes); |
| 2267 | buf = GetFPDFWideStringBuffer(length_bytes); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2268 | EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2269 | buf.data(), length_bytes)); |
| 2270 | EXPECT_EQ(L"Apple", GetPlatformWString(buf.data())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2271 | |
| 2272 | index = 25; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2273 | length_bytes = |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2274 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2275 | ASSERT_EQ(18u, length_bytes); |
| 2276 | buf = GetFPDFWideStringBuffer(length_bytes); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2277 | EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2278 | buf.data(), length_bytes)); |
| 2279 | EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2280 | |
| 2281 | // indices out of range |
| 2282 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1, |
| 2283 | nullptr, 0)); |
| 2284 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26, |
| 2285 | nullptr, 0)); |
| 2286 | } |
| 2287 | |
| 2288 | UnloadPage(page); |
| 2289 | } |
| 2290 | |
| 2291 | TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) { |
| 2292 | // Open a file with ink annotations and load its first page. |
| 2293 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 2294 | FPDF_PAGE page = LoadPage(0); |
| 2295 | ASSERT_TRUE(page); |
| 2296 | |
| 2297 | { |
| 2298 | // annotations do not have "Opt" array and will return 0 |
| 2299 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2300 | ASSERT_TRUE(annot); |
| 2301 | |
| 2302 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0, |
| 2303 | nullptr, 0)); |
| 2304 | |
| 2305 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2306 | ASSERT_TRUE(annot); |
| 2307 | |
| 2308 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0, |
| 2309 | nullptr, 0)); |
| 2310 | } |
| 2311 | |
| 2312 | UnloadPage(page); |
| 2313 | } |
Ryan Smith | 09c23b1 | 2019-04-25 18:09:06 +0000 | [diff] [blame] | 2314 | |
Mansi Awasthi | 2acdf79 | 2020-05-12 08:48:04 +0000 | [diff] [blame] | 2315 | TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) { |
| 2316 | // Open a file with combobox widget annotations and load its first page. |
| 2317 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 2318 | FPDF_PAGE page = LoadPage(0); |
| 2319 | ASSERT_TRUE(page); |
| 2320 | |
| 2321 | { |
| 2322 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2323 | ASSERT_TRUE(annot); |
| 2324 | |
| 2325 | // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects. |
| 2326 | int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get()); |
| 2327 | ASSERT_EQ(3, count); |
| 2328 | for (int i = 0; i < count; i++) { |
| 2329 | EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i)); |
| 2330 | } |
| 2331 | |
| 2332 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2333 | ASSERT_TRUE(annot); |
| 2334 | |
| 2335 | // Checks for Combobox with Values (/V) object which is just a string. |
| 2336 | count = FPDFAnnot_GetOptionCount(form_handle(), annot.get()); |
| 2337 | ASSERT_EQ(26, count); |
| 2338 | for (int i = 0; i < count; i++) { |
| 2339 | EXPECT_EQ(i == 1, |
| 2340 | FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i)); |
| 2341 | } |
| 2342 | |
| 2343 | // Checks for index outside bound i.e. (index >= CountOption()). |
| 2344 | EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), |
| 2345 | /*index=*/26)); |
| 2346 | // Checks for negetive index. |
| 2347 | EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), |
| 2348 | /*index=*/-1)); |
| 2349 | |
| 2350 | // Checks for bad form handle/annot. |
| 2351 | EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0)); |
| 2352 | EXPECT_FALSE( |
| 2353 | FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0)); |
| 2354 | EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0)); |
| 2355 | } |
| 2356 | |
| 2357 | UnloadPage(page); |
| 2358 | } |
| 2359 | |
| 2360 | TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) { |
| 2361 | // Open a file with listbox widget annotations and load its first page. |
| 2362 | ASSERT_TRUE(OpenDocument("listbox_form.pdf")); |
| 2363 | FPDF_PAGE page = LoadPage(0); |
| 2364 | ASSERT_TRUE(page); |
| 2365 | |
| 2366 | { |
| 2367 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2368 | ASSERT_TRUE(annot); |
| 2369 | |
| 2370 | // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects. |
| 2371 | int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get()); |
| 2372 | ASSERT_EQ(3, count); |
| 2373 | for (int i = 0; i < count; i++) { |
| 2374 | EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i)); |
| 2375 | } |
| 2376 | |
| 2377 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2378 | ASSERT_TRUE(annot); |
| 2379 | |
| 2380 | // Checks for Listbox with Values (/V) object which is just a string. |
| 2381 | count = FPDFAnnot_GetOptionCount(form_handle(), annot.get()); |
| 2382 | ASSERT_EQ(26, count); |
| 2383 | for (int i = 0; i < count; i++) { |
| 2384 | EXPECT_EQ(i == 1, |
| 2385 | FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i)); |
| 2386 | } |
| 2387 | |
| 2388 | annot.reset(FPDFPage_GetAnnot(page, 3)); |
| 2389 | ASSERT_TRUE(annot); |
| 2390 | |
| 2391 | // Checks for Listbox with only Selected indices (/I) object which is an |
| 2392 | // array with multiple objects. |
| 2393 | count = FPDFAnnot_GetOptionCount(form_handle(), annot.get()); |
| 2394 | ASSERT_EQ(5, count); |
| 2395 | for (int i = 0; i < count; i++) { |
| 2396 | bool expected = (i == 1 || i == 3); |
| 2397 | EXPECT_EQ(expected, |
| 2398 | FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i)); |
| 2399 | } |
| 2400 | |
| 2401 | annot.reset(FPDFPage_GetAnnot(page, 4)); |
| 2402 | ASSERT_TRUE(annot); |
| 2403 | |
| 2404 | // Checks for Listbox with Values (/V) object which is an array with |
| 2405 | // multiple objects. |
| 2406 | count = FPDFAnnot_GetOptionCount(form_handle(), annot.get()); |
| 2407 | ASSERT_EQ(5, count); |
| 2408 | for (int i = 0; i < count; i++) { |
| 2409 | bool expected = (i == 2 || i == 4); |
| 2410 | EXPECT_EQ(expected, |
| 2411 | FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i)); |
| 2412 | } |
| 2413 | |
| 2414 | annot.reset(FPDFPage_GetAnnot(page, 5)); |
| 2415 | ASSERT_TRUE(annot); |
| 2416 | |
| 2417 | // Checks for Listbox with both Values (/V) and Selected Indices (/I) |
| 2418 | // objects conflict with different lengths. |
| 2419 | count = FPDFAnnot_GetOptionCount(form_handle(), annot.get()); |
| 2420 | ASSERT_EQ(5, count); |
| 2421 | for (int i = 0; i < count; i++) { |
| 2422 | bool expected = (i == 0 || i == 2); |
| 2423 | EXPECT_EQ(expected, |
| 2424 | FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i)); |
| 2425 | } |
| 2426 | } |
| 2427 | |
| 2428 | UnloadPage(page); |
| 2429 | } |
| 2430 | |
| 2431 | TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) { |
| 2432 | // Open a file with multiple form field annotations and load its first page. |
| 2433 | ASSERT_TRUE(OpenDocument("multiple_form_types.pdf")); |
| 2434 | FPDF_PAGE page = LoadPage(0); |
| 2435 | ASSERT_TRUE(page); |
| 2436 | |
| 2437 | { |
| 2438 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2439 | ASSERT_TRUE(annot); |
| 2440 | |
| 2441 | // Checks for link annotation. |
| 2442 | EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), |
| 2443 | /*index=*/0)); |
| 2444 | |
| 2445 | annot.reset(FPDFPage_GetAnnot(page, 3)); |
| 2446 | ASSERT_TRUE(annot); |
| 2447 | |
| 2448 | // Checks for text field annotation. |
| 2449 | EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), |
| 2450 | /*index=*/0)); |
| 2451 | } |
| 2452 | |
| 2453 | UnloadPage(page); |
| 2454 | } |
| 2455 | |
Ryan Smith | 09c23b1 | 2019-04-25 18:09:06 +0000 | [diff] [blame] | 2456 | TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) { |
| 2457 | // Open a file with combobox annotations and load its first page. |
| 2458 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 2459 | FPDF_PAGE page = LoadPage(0); |
| 2460 | ASSERT_TRUE(page); |
| 2461 | |
| 2462 | { |
| 2463 | // All 3 widgets have Tf font size 12. |
| 2464 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2465 | ASSERT_TRUE(annot); |
| 2466 | |
| 2467 | float value; |
| 2468 | ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value)); |
| 2469 | EXPECT_EQ(12.0, value); |
| 2470 | |
| 2471 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2472 | ASSERT_TRUE(annot); |
| 2473 | |
| 2474 | float value_two; |
| 2475 | ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two)); |
| 2476 | EXPECT_EQ(12.0, value_two); |
| 2477 | |
| 2478 | annot.reset(FPDFPage_GetAnnot(page, 2)); |
| 2479 | ASSERT_TRUE(annot); |
| 2480 | |
| 2481 | float value_three; |
| 2482 | ASSERT_TRUE( |
| 2483 | FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three)); |
| 2484 | EXPECT_EQ(12.0, value_three); |
| 2485 | } |
| 2486 | |
| 2487 | UnloadPage(page); |
| 2488 | } |
| 2489 | |
| 2490 | TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) { |
| 2491 | // Open a file with textfield annotations and load its first page. |
| 2492 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 2493 | FPDF_PAGE page = LoadPage(0); |
| 2494 | ASSERT_TRUE(page); |
| 2495 | |
| 2496 | { |
Mansi Awasthi | 0b5da67 | 2020-01-23 18:17:18 +0000 | [diff] [blame] | 2497 | // All 4 widgets have Tf font size 12. |
Ryan Smith | 09c23b1 | 2019-04-25 18:09:06 +0000 | [diff] [blame] | 2498 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2499 | ASSERT_TRUE(annot); |
| 2500 | |
| 2501 | float value; |
| 2502 | ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value)); |
| 2503 | EXPECT_EQ(12.0, value); |
| 2504 | |
| 2505 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2506 | ASSERT_TRUE(annot); |
| 2507 | |
| 2508 | float value_two; |
| 2509 | ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two)); |
| 2510 | EXPECT_EQ(12.0, value_two); |
| 2511 | |
| 2512 | annot.reset(FPDFPage_GetAnnot(page, 2)); |
| 2513 | ASSERT_TRUE(annot); |
| 2514 | |
| 2515 | float value_three; |
| 2516 | ASSERT_TRUE( |
| 2517 | FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three)); |
| 2518 | EXPECT_EQ(12.0, value_three); |
Mansi Awasthi | 0b5da67 | 2020-01-23 18:17:18 +0000 | [diff] [blame] | 2519 | |
| 2520 | float value_four; |
| 2521 | ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four)); |
| 2522 | EXPECT_EQ(12.0, value_four); |
Ryan Smith | 09c23b1 | 2019-04-25 18:09:06 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | UnloadPage(page); |
| 2526 | } |
| 2527 | |
| 2528 | TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) { |
| 2529 | // Open a file with ink annotations and load its first page. |
| 2530 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 2531 | FPDF_PAGE page = LoadPage(0); |
| 2532 | ASSERT_TRUE(page); |
| 2533 | |
| 2534 | { |
| 2535 | // Annotations that do not have variable text and will return -1. |
| 2536 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2537 | ASSERT_TRUE(annot); |
| 2538 | |
| 2539 | float value; |
| 2540 | ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value)); |
| 2541 | |
| 2542 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2543 | ASSERT_TRUE(annot); |
| 2544 | |
| 2545 | ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value)); |
| 2546 | } |
| 2547 | |
| 2548 | UnloadPage(page); |
| 2549 | } |
| 2550 | |
| 2551 | TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) { |
| 2552 | // Open a file with combobox annotations and load its first page. |
| 2553 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 2554 | FPDF_PAGE page = LoadPage(0); |
| 2555 | ASSERT_TRUE(page); |
| 2556 | |
| 2557 | { |
| 2558 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2559 | ASSERT_TRUE(annot); |
| 2560 | |
| 2561 | // Check bad form handle / annot. |
| 2562 | float value; |
| 2563 | ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value)); |
| 2564 | ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value)); |
| 2565 | ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value)); |
| 2566 | } |
| 2567 | |
| 2568 | UnloadPage(page); |
| 2569 | } |
| 2570 | |
| 2571 | TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) { |
| 2572 | // Open a file with textfield annotations and load its first page. |
| 2573 | ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf")); |
| 2574 | FPDF_PAGE page = LoadPage(0); |
| 2575 | ASSERT_TRUE(page); |
| 2576 | |
| 2577 | { |
| 2578 | // Obtain the first annotation, a text field with negative font size, -12. |
| 2579 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2580 | ASSERT_TRUE(annot); |
| 2581 | |
| 2582 | float value; |
| 2583 | ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value)); |
| 2584 | EXPECT_EQ(-12.0, value); |
| 2585 | } |
| 2586 | |
| 2587 | UnloadPage(page); |
| 2588 | } |
Ryan Smith | 23fdf89 | 2019-07-17 21:51:26 +0000 | [diff] [blame] | 2589 | |
| 2590 | TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) { |
| 2591 | // Open a file with checkbox and radiobuttons widget annotations and load its |
| 2592 | // first page. |
| 2593 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 2594 | FPDF_PAGE page = LoadPage(0); |
| 2595 | ASSERT_TRUE(page); |
| 2596 | |
| 2597 | { |
| 2598 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
| 2599 | ASSERT_TRUE(annot); |
| 2600 | ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2601 | } |
| 2602 | |
| 2603 | UnloadPage(page); |
| 2604 | } |
| 2605 | |
| 2606 | TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) { |
| 2607 | // Open a file with checkbox and radiobutton widget annotations and load its |
| 2608 | // first page. |
| 2609 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 2610 | FPDF_PAGE page = LoadPage(0); |
| 2611 | ASSERT_TRUE(page); |
| 2612 | |
| 2613 | { |
| 2614 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2615 | ASSERT_TRUE(annot); |
| 2616 | ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2617 | } |
| 2618 | |
| 2619 | UnloadPage(page); |
| 2620 | } |
| 2621 | |
| 2622 | TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) { |
| 2623 | // Open a file with checkbox and radiobutton widget annotations and load its |
| 2624 | // first page. |
| 2625 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 2626 | FPDF_PAGE page = LoadPage(0); |
| 2627 | ASSERT_TRUE(page); |
| 2628 | |
| 2629 | { |
| 2630 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5)); |
| 2631 | ASSERT_TRUE(annot); |
| 2632 | ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2633 | |
| 2634 | annot.reset(FPDFPage_GetAnnot(page, 6)); |
| 2635 | ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2636 | |
| 2637 | annot.reset(FPDFPage_GetAnnot(page, 7)); |
| 2638 | ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2639 | } |
| 2640 | |
| 2641 | UnloadPage(page); |
| 2642 | } |
| 2643 | |
| 2644 | TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) { |
| 2645 | // Open a file with checkbox and radiobutton widget annotations and load its |
| 2646 | // first page. |
| 2647 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 2648 | FPDF_PAGE page = LoadPage(0); |
| 2649 | ASSERT_TRUE(page); |
| 2650 | |
| 2651 | { |
| 2652 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
| 2653 | ASSERT_TRUE(annot); |
| 2654 | ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2655 | |
| 2656 | annot.reset(FPDFPage_GetAnnot(page, 3)); |
| 2657 | ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2658 | |
| 2659 | annot.reset(FPDFPage_GetAnnot(page, 4)); |
| 2660 | ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2661 | } |
| 2662 | |
| 2663 | UnloadPage(page); |
| 2664 | } |
| 2665 | |
| 2666 | TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) { |
| 2667 | // Open a file with checkbox and radiobuttons widget annotations and load its |
| 2668 | // first page. |
| 2669 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 2670 | FPDF_PAGE page = LoadPage(0); |
| 2671 | ASSERT_TRUE(page); |
| 2672 | |
| 2673 | { |
| 2674 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2675 | ASSERT_TRUE(annot); |
| 2676 | ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get())); |
| 2677 | ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr)); |
| 2678 | ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr)); |
| 2679 | } |
| 2680 | |
| 2681 | UnloadPage(page); |
| 2682 | } |
| 2683 | |
| 2684 | TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) { |
| 2685 | // Open a file with text widget annotations and load its first page. |
| 2686 | ASSERT_TRUE(OpenDocument("text_form.pdf")); |
| 2687 | FPDF_PAGE page = LoadPage(0); |
| 2688 | ASSERT_TRUE(page); |
| 2689 | |
| 2690 | { |
| 2691 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2692 | ASSERT_TRUE(annot); |
| 2693 | ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2694 | } |
| 2695 | |
| 2696 | UnloadPage(page); |
| 2697 | } |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2698 | |
Ankit Kumar | 8c7e4ad | 2020-02-07 08:56:49 +0000 | [diff] [blame] | 2699 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) { |
| 2700 | ASSERT_TRUE(OpenDocument("multiple_form_types.pdf")); |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2701 | FPDF_PAGE page = LoadPage(0); |
| 2702 | ASSERT_TRUE(page); |
| 2703 | |
Ankit Kumar | 8c7e4ad | 2020-02-07 08:56:49 +0000 | [diff] [blame] | 2704 | EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr)); |
| 2705 | |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2706 | { |
Ankit Kumar | 8c7e4ad | 2020-02-07 08:56:49 +0000 | [diff] [blame] | 2707 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2708 | ASSERT_TRUE(annot); |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2709 | EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get())); |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2710 | } |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2711 | |
Ankit Kumar | 8c7e4ad | 2020-02-07 08:56:49 +0000 | [diff] [blame] | 2712 | constexpr int kExpectedAnnotTypes[] = {-1, |
| 2713 | FPDF_FORMFIELD_COMBOBOX, |
| 2714 | FPDF_FORMFIELD_LISTBOX, |
| 2715 | FPDF_FORMFIELD_TEXTFIELD, |
| 2716 | FPDF_FORMFIELD_CHECKBOX, |
| 2717 | FPDF_FORMFIELD_RADIOBUTTON}; |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2718 | |
Lei Zhang | 4501a50 | 2020-05-18 16:52:59 +0000 | [diff] [blame] | 2719 | for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) { |
Ankit Kumar | 8c7e4ad | 2020-02-07 08:56:49 +0000 | [diff] [blame] | 2720 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i)); |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2721 | ASSERT_TRUE(annot); |
Ankit Kumar | 8c7e4ad | 2020-02-07 08:56:49 +0000 | [diff] [blame] | 2722 | EXPECT_EQ(kExpectedAnnotTypes[i], |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2723 | FPDFAnnot_GetFormFieldType(form_handle(), annot.get())); |
| 2724 | } |
| 2725 | UnloadPage(page); |
| 2726 | } |
| 2727 | |
| 2728 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) { |
| 2729 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 2730 | FPDF_PAGE page = LoadPage(0); |
| 2731 | ASSERT_TRUE(page); |
| 2732 | |
| 2733 | { |
| 2734 | EXPECT_EQ(0u, |
| 2735 | FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0)); |
| 2736 | |
| 2737 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2738 | ASSERT_TRUE(annot); |
| 2739 | |
| 2740 | EXPECT_EQ(0u, |
| 2741 | FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0)); |
| 2742 | |
| 2743 | unsigned long length_bytes = |
| 2744 | FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0); |
| 2745 | ASSERT_EQ(2u, length_bytes); |
| 2746 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 2747 | EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), |
| 2748 | buf.data(), length_bytes)); |
| 2749 | EXPECT_EQ(L"", GetPlatformWString(buf.data())); |
| 2750 | } |
| 2751 | { |
| 2752 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
| 2753 | ASSERT_TRUE(annot); |
| 2754 | |
| 2755 | unsigned long length_bytes = |
| 2756 | FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0); |
| 2757 | ASSERT_EQ(18u, length_bytes); |
| 2758 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 2759 | EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), |
| 2760 | buf.data(), length_bytes)); |
| 2761 | EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data())); |
| 2762 | } |
| 2763 | UnloadPage(page); |
| 2764 | } |
| 2765 | |
| 2766 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) { |
| 2767 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 2768 | FPDF_PAGE page = LoadPage(0); |
| 2769 | ASSERT_TRUE(page); |
| 2770 | |
| 2771 | { |
| 2772 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2773 | ASSERT_TRUE(annot); |
| 2774 | |
| 2775 | unsigned long length_bytes = |
| 2776 | FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0); |
| 2777 | ASSERT_EQ(2u, length_bytes); |
| 2778 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 2779 | EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), |
| 2780 | buf.data(), length_bytes)); |
| 2781 | EXPECT_EQ(L"", GetPlatformWString(buf.data())); |
| 2782 | } |
| 2783 | { |
| 2784 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
| 2785 | ASSERT_TRUE(annot); |
| 2786 | |
| 2787 | unsigned long length_bytes = |
| 2788 | FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0); |
| 2789 | ASSERT_EQ(14u, length_bytes); |
| 2790 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 2791 | EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), |
| 2792 | buf.data(), length_bytes)); |
| 2793 | EXPECT_EQ(L"Banana", GetPlatformWString(buf.data())); |
| 2794 | } |
| 2795 | UnloadPage(page); |
| 2796 | } |
| 2797 | |
| 2798 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) { |
| 2799 | ASSERT_TRUE(OpenDocument("text_form.pdf")); |
| 2800 | FPDF_PAGE page = LoadPage(0); |
| 2801 | ASSERT_TRUE(page); |
| 2802 | |
| 2803 | { |
| 2804 | EXPECT_EQ(0u, |
| 2805 | FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0)); |
| 2806 | |
| 2807 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2808 | ASSERT_TRUE(annot); |
| 2809 | |
| 2810 | EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0)); |
| 2811 | |
| 2812 | unsigned long length_bytes = |
| 2813 | FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0); |
| 2814 | ASSERT_EQ(18u, length_bytes); |
| 2815 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 2816 | EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), |
| 2817 | buf.data(), length_bytes)); |
| 2818 | EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data())); |
| 2819 | } |
| 2820 | UnloadPage(page); |
| 2821 | } |
| 2822 | |
| 2823 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) { |
| 2824 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 2825 | FPDF_PAGE page = LoadPage(0); |
| 2826 | ASSERT_TRUE(page); |
| 2827 | |
| 2828 | { |
| 2829 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2830 | ASSERT_TRUE(annot); |
| 2831 | |
| 2832 | unsigned long length_bytes = |
| 2833 | FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0); |
| 2834 | ASSERT_EQ(30u, length_bytes); |
| 2835 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 2836 | EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), |
| 2837 | buf.data(), length_bytes)); |
| 2838 | EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data())); |
| 2839 | } |
| 2840 | UnloadPage(page); |
| 2841 | } |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2842 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2843 | TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) { |
| 2844 | ASSERT_TRUE(OpenDocument("annots.pdf")); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2845 | FPDF_PAGE page = LoadPage(0); |
| 2846 | ASSERT_TRUE(page); |
| 2847 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2848 | // Verify widgets are by default focusable. |
| 2849 | const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET}; |
| 2850 | VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2851 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2852 | // Expected annot subtypes for page 0 of annots.pdf. |
| 2853 | const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = { |
| 2854 | FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, |
| 2855 | FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT, |
| 2856 | FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET, |
| 2857 | }; |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2858 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2859 | const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = { |
Ankit Kumar | 69cab67 | 2020-04-20 19:50:41 +0000 | [diff] [blame] | 2860 | FPDF_ANNOT_WIDGET}; |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2861 | VerifyAnnotationSubtypesAndFocusability(form_handle(), page, |
| 2862 | kExpectedAnnotSubtypes, |
| 2863 | kExpectedDefaultFocusableSubtypes); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2864 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2865 | // Make no annotation type focusable using the preferred method. |
| 2866 | ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0)); |
| 2867 | ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle())); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2868 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2869 | // Restore the focusable type count back to 1, then set it back to 0 using a |
| 2870 | // different method. |
| 2871 | SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes); |
| 2872 | ASSERT_TRUE( |
| 2873 | FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0)); |
| 2874 | ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle())); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2875 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2876 | VerifyAnnotationSubtypesAndFocusability(form_handle(), page, |
Ankit Kumar | 906ac57 | 2020-04-21 05:58:04 +0000 | [diff] [blame] | 2877 | kExpectedAnnotSubtypes, {}); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2878 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2879 | // Now make links focusable. |
| 2880 | const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK}; |
| 2881 | SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2882 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2883 | const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = { |
Ankit Kumar | 906ac57 | 2020-04-21 05:58:04 +0000 | [diff] [blame] | 2884 | FPDF_ANNOT_LINK}; |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2885 | VerifyAnnotationSubtypesAndFocusability(form_handle(), page, |
| 2886 | kExpectedAnnotSubtypes, |
| 2887 | kExpectedLinkocusableSubtypes); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2888 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2889 | // Test invalid parameters. |
| 2890 | EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes, |
Lei Zhang | 4501a50 | 2020-05-18 16:52:59 +0000 | [diff] [blame] | 2891 | pdfium::size(kDefaultSubtypes))); |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2892 | EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, |
Lei Zhang | 4501a50 | 2020-05-18 16:52:59 +0000 | [diff] [blame] | 2893 | pdfium::size(kDefaultSubtypes))); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2894 | EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr)); |
| 2895 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2896 | std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1); |
| 2897 | EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(), |
| 2898 | subtypes.size())); |
| 2899 | EXPECT_FALSE( |
| 2900 | FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size())); |
| 2901 | EXPECT_FALSE( |
| 2902 | FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0)); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2903 | |
| 2904 | UnloadPage(page); |
| 2905 | } |
Lei Zhang | 671aece | 2020-04-14 19:02:26 +0000 | [diff] [blame] | 2906 | |
Hui Yingst | 609f7d6 | 2020-04-23 23:14:13 +0000 | [diff] [blame] | 2907 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 2908 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 2909 | #define MAYBE_FocusableAnnotRendering DISABLED_FocusableAnnotRendering |
| 2910 | #else |
| 2911 | #define MAYBE_FocusableAnnotRendering FocusableAnnotRendering |
| 2912 | #endif |
| 2913 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_FocusableAnnotRendering) { |
Lei Zhang | 671aece | 2020-04-14 19:02:26 +0000 | [diff] [blame] | 2914 | ASSERT_TRUE(OpenDocument("annots.pdf")); |
| 2915 | FPDF_PAGE page = LoadPage(0); |
| 2916 | ASSERT_TRUE(page); |
| 2917 | |
| 2918 | { |
| 2919 | #if defined(OS_WIN) |
| 2920 | static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30"; |
| 2921 | #elif defined(OS_MACOSX) |
| 2922 | static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867"; |
| 2923 | #else |
| 2924 | static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654"; |
| 2925 | #endif |
| 2926 | // Check the initial rendering. |
| 2927 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 2928 | CompareBitmap(bitmap.get(), 612, 792, kMd5sum); |
| 2929 | } |
| 2930 | |
| 2931 | // Make links and highlights focusable. |
| 2932 | static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK, |
| 2933 | FPDF_ANNOT_HIGHLIGHT}; |
Lei Zhang | 4501a50 | 2020-05-18 16:52:59 +0000 | [diff] [blame] | 2934 | constexpr int kSubTypesCount = pdfium::size(kSubTypes); |
Lei Zhang | 671aece | 2020-04-14 19:02:26 +0000 | [diff] [blame] | 2935 | ASSERT_TRUE( |
| 2936 | FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount)); |
| 2937 | ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle())); |
| 2938 | std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount); |
| 2939 | ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), |
| 2940 | subtypes.size())); |
| 2941 | ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]); |
| 2942 | ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]); |
| 2943 | |
| 2944 | { |
| 2945 | #if defined(OS_WIN) |
| 2946 | static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f"; |
| 2947 | #elif defined(OS_MACOSX) |
| 2948 | static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346"; |
| 2949 | #else |
| 2950 | static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee"; |
| 2951 | #endif |
| 2952 | // Focus the first link and check the rendering. |
| 2953 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2954 | ASSERT_TRUE(annot); |
| 2955 | EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get())); |
| 2956 | EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get())); |
| 2957 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 2958 | CompareBitmap(bitmap.get(), 612, 792, kMd5sum); |
| 2959 | } |
| 2960 | |
| 2961 | { |
| 2962 | #if defined(OS_WIN) |
| 2963 | static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10"; |
| 2964 | #elif defined(OS_MACOSX) |
| 2965 | static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e"; |
| 2966 | #else |
| 2967 | static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110"; |
| 2968 | #endif |
| 2969 | // Focus the first highlight and check the rendering. |
| 2970 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4)); |
| 2971 | ASSERT_TRUE(annot); |
| 2972 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 2973 | EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get())); |
| 2974 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 2975 | CompareBitmap(bitmap.get(), 612, 792, kMd5sum); |
| 2976 | } |
| 2977 | |
| 2978 | UnloadPage(page); |
| 2979 | } |
Badhri Ravikumar | cd62891 | 2020-05-07 19:23:51 +0000 | [diff] [blame] | 2980 | |
| 2981 | TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) { |
| 2982 | ASSERT_TRUE(OpenDocument("annots.pdf")); |
| 2983 | FPDF_PAGE page = LoadPage(0); |
| 2984 | ASSERT_TRUE(page); |
| 2985 | { |
| 2986 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3)); |
| 2987 | ASSERT_TRUE(annot); |
| 2988 | EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get())); |
| 2989 | FPDF_LINK link_annot = FPDFAnnot_GetLink(annot.get()); |
| 2990 | ASSERT_TRUE(link_annot); |
| 2991 | |
| 2992 | FPDF_ACTION action = FPDFLink_GetAction(link_annot); |
| 2993 | ASSERT_TRUE(action); |
| 2994 | EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI), |
| 2995 | FPDFAction_GetType(action)); |
| 2996 | |
| 2997 | constexpr char kExpectedResult[] = |
| 2998 | "https://cs.chromium.org/chromium/src/third_party/pdfium/public/" |
| 2999 | "fpdf_text.h"; |
Lei Zhang | 4501a50 | 2020-05-18 16:52:59 +0000 | [diff] [blame] | 3000 | constexpr unsigned long kExpectedLength = pdfium::size(kExpectedResult); |
Badhri Ravikumar | cd62891 | 2020-05-07 19:23:51 +0000 | [diff] [blame] | 3001 | unsigned long bufsize = |
| 3002 | FPDFAction_GetURIPath(document(), action, nullptr, 0); |
| 3003 | ASSERT_EQ(kExpectedLength, bufsize); |
| 3004 | |
| 3005 | char buffer[1024]; |
| 3006 | EXPECT_EQ(bufsize, |
| 3007 | FPDFAction_GetURIPath(document(), action, buffer, bufsize)); |
| 3008 | EXPECT_STREQ(kExpectedResult, buffer); |
| 3009 | } |
| 3010 | |
| 3011 | { |
| 3012 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4)); |
| 3013 | ASSERT_TRUE(annot); |
| 3014 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 3015 | EXPECT_FALSE(FPDFAnnot_GetLink(annot.get())); |
| 3016 | } |
| 3017 | |
| 3018 | EXPECT_FALSE(FPDFAnnot_GetLink(nullptr)); |
| 3019 | |
| 3020 | UnloadPage(page); |
| 3021 | } |
Mansi Awasthi | 23bba0e | 2020-05-15 12:18:25 +0000 | [diff] [blame] | 3022 | |
| 3023 | TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) { |
| 3024 | // Open a file with radio button widget annotations and load its first page. |
| 3025 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 3026 | FPDF_PAGE page = LoadPage(0); |
| 3027 | ASSERT_TRUE(page); |
| 3028 | |
| 3029 | { |
| 3030 | // Checks for bad annot. |
| 3031 | EXPECT_EQ(-1, |
| 3032 | FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr)); |
| 3033 | |
| 3034 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3)); |
| 3035 | ASSERT_TRUE(annot); |
| 3036 | |
| 3037 | // Checks for bad form handle. |
| 3038 | EXPECT_EQ(-1, |
| 3039 | FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get())); |
| 3040 | |
| 3041 | EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get())); |
| 3042 | } |
| 3043 | |
| 3044 | UnloadPage(page); |
| 3045 | } |
| 3046 | |
| 3047 | TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) { |
| 3048 | // Open a file with checkbox widget annotations and load its first page. |
| 3049 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 3050 | FPDF_PAGE page = LoadPage(0); |
| 3051 | ASSERT_TRUE(page); |
| 3052 | |
| 3053 | { |
| 3054 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3055 | ASSERT_TRUE(annot); |
| 3056 | EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get())); |
| 3057 | } |
| 3058 | |
| 3059 | UnloadPage(page); |
| 3060 | } |
| 3061 | |
| 3062 | TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) { |
| 3063 | // Open a file with ink annotations and load its first page. |
| 3064 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 3065 | FPDF_PAGE page = LoadPage(0); |
| 3066 | ASSERT_TRUE(page); |
| 3067 | |
| 3068 | { |
| 3069 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3070 | ASSERT_TRUE(annot); |
| 3071 | EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get())); |
| 3072 | } |
| 3073 | |
| 3074 | UnloadPage(page); |
| 3075 | } |
| 3076 | |
| 3077 | TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) { |
| 3078 | // Open a file with radio button widget annotations and load its first page. |
| 3079 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 3080 | FPDF_PAGE page = LoadPage(0); |
| 3081 | ASSERT_TRUE(page); |
| 3082 | |
| 3083 | { |
| 3084 | // Checks for bad annot. |
| 3085 | EXPECT_EQ(-1, |
| 3086 | FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr)); |
| 3087 | |
| 3088 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3)); |
| 3089 | ASSERT_TRUE(annot); |
| 3090 | |
| 3091 | // Checks for bad form handle. |
| 3092 | EXPECT_EQ(-1, |
| 3093 | FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get())); |
| 3094 | |
| 3095 | EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get())); |
| 3096 | } |
| 3097 | |
| 3098 | UnloadPage(page); |
| 3099 | } |
| 3100 | |
| 3101 | TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) { |
| 3102 | // Open a file with checkbox widget annotations and load its first page. |
| 3103 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 3104 | FPDF_PAGE page = LoadPage(0); |
| 3105 | ASSERT_TRUE(page); |
| 3106 | |
| 3107 | { |
| 3108 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3109 | ASSERT_TRUE(annot); |
| 3110 | EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get())); |
| 3111 | } |
| 3112 | |
| 3113 | UnloadPage(page); |
| 3114 | } |
| 3115 | |
| 3116 | TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) { |
| 3117 | // Open a file with ink annotations and load its first page. |
| 3118 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 3119 | FPDF_PAGE page = LoadPage(0); |
| 3120 | ASSERT_TRUE(page); |
| 3121 | |
| 3122 | { |
| 3123 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3124 | ASSERT_TRUE(annot); |
| 3125 | EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get())); |
| 3126 | } |
| 3127 | |
| 3128 | UnloadPage(page); |
| 3129 | } |
| 3130 | |
| 3131 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) { |
| 3132 | // Open a file with radio button widget annotations and load its first page. |
| 3133 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 3134 | FPDF_PAGE page = LoadPage(0); |
| 3135 | ASSERT_TRUE(page); |
| 3136 | |
| 3137 | { |
| 3138 | // Checks for bad annot. |
| 3139 | EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue( |
| 3140 | form_handle(), /*annot=*/nullptr, |
| 3141 | /*buffer=*/nullptr, /*buflen=*/0)); |
| 3142 | |
| 3143 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6)); |
| 3144 | ASSERT_TRUE(annot); |
| 3145 | |
| 3146 | // Checks for bad form handle. |
| 3147 | EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue( |
| 3148 | /*hHandle=*/nullptr, annot.get(), |
| 3149 | /*buffer=*/nullptr, /*buflen=*/0)); |
| 3150 | |
| 3151 | unsigned long length_bytes = |
| 3152 | FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(), |
| 3153 | /*buffer=*/nullptr, /*buflen=*/0); |
| 3154 | ASSERT_EQ(14u, length_bytes); |
| 3155 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 3156 | EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(), |
| 3157 | buf.data(), length_bytes)); |
| 3158 | EXPECT_EQ(L"value2", GetPlatformWString(buf.data())); |
| 3159 | } |
| 3160 | |
| 3161 | UnloadPage(page); |
| 3162 | } |
| 3163 | |
| 3164 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) { |
| 3165 | // Open a file with checkbox widget annotations and load its first page. |
| 3166 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 3167 | FPDF_PAGE page = LoadPage(0); |
| 3168 | ASSERT_TRUE(page); |
| 3169 | |
| 3170 | { |
| 3171 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3172 | ASSERT_TRUE(annot); |
| 3173 | |
| 3174 | unsigned long length_bytes = |
| 3175 | FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(), |
| 3176 | /*buffer=*/nullptr, /*buflen=*/0); |
| 3177 | ASSERT_EQ(8u, length_bytes); |
| 3178 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 3179 | EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(), |
| 3180 | buf.data(), length_bytes)); |
| 3181 | EXPECT_EQ(L"Yes", GetPlatformWString(buf.data())); |
| 3182 | } |
| 3183 | |
| 3184 | UnloadPage(page); |
| 3185 | } |
| 3186 | |
| 3187 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) { |
| 3188 | // Open a file with ink annotations and load its first page. |
| 3189 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 3190 | FPDF_PAGE page = LoadPage(0); |
| 3191 | ASSERT_TRUE(page); |
| 3192 | |
| 3193 | { |
| 3194 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3195 | ASSERT_TRUE(annot); |
| 3196 | EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(), |
| 3197 | /*buffer=*/nullptr, |
| 3198 | /*buflen=*/0)); |
| 3199 | } |
| 3200 | |
| 3201 | UnloadPage(page); |
| 3202 | } |