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