Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 1 | // Copyright 2017 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tom Sepez | 204ab05 | 2020-06-12 21:33:48 +0000 | [diff] [blame] | 5 | #include "public/fpdf_annot.h" |
| 6 | |
| 7 | #include <limits.h> |
| 8 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 9 | #include <algorithm> |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 10 | #include <cwchar> |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 11 | #include <memory> |
| 12 | #include <string> |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
Lei Zhang | e4cdac5 | 2019-04-30 16:45:57 +0000 | [diff] [blame] | 15 | #include "build/build_config.h" |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 16 | #include "constants/annotation_common.h" |
Tom Sepez | 204ab05 | 2020-06-12 21:33:48 +0000 | [diff] [blame] | 17 | #include "core/fpdfapi/page/cpdf_annotcontext.h" |
| 18 | #include "core/fpdfapi/page/cpdf_pagemodule.h" |
| 19 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 20 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 21 | #include "core/fxcrt/fx_system.h" |
Tom Sepez | 204ab05 | 2020-06-12 21:33:48 +0000 | [diff] [blame] | 22 | #include "fpdfsdk/cpdfsdk_helpers.h" |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 23 | #include "public/cpp/fpdf_scopers.h" |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 24 | #include "public/fpdf_edit.h" |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 25 | #include "public/fpdf_formfill.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 26 | #include "public/fpdfview.h" |
| 27 | #include "testing/embedder_test.h" |
Hui Yingst | b4baceb | 2020-04-28 23:46:10 +0000 | [diff] [blame] | 28 | #include "testing/embedder_test_constants.h" |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 29 | #include "testing/fx_string_testhelpers.h" |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 30 | #include "testing/gmock/include/gmock/gmock-matchers.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 31 | #include "testing/gtest/include/gtest/gtest.h" |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 32 | #include "testing/utils/hash.h" |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 33 | #include "third_party/base/span.h" |
Lei Zhang | c9b7299 | 2020-04-22 01:03:52 +0000 | [diff] [blame] | 34 | #include "third_party/base/stl_util.h" |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 35 | |
Hui Yingst | 30bfcc5 | 2020-07-27 23:54:40 +0000 | [diff] [blame] | 36 | using pdfium::kAnnotationStampWithApChecksum; |
| 37 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 38 | namespace { |
| 39 | |
Tom Sepez | 204ab05 | 2020-06-12 21:33:48 +0000 | [diff] [blame] | 40 | const wchar_t kStreamData[] = |
| 41 | L"/GS gs 0.0 0.0 0.0 RG 4 w 211.8 747.6 m 211.8 744.8 " |
| 42 | L"212.6 743.0 214.2 740.8 " |
| 43 | L"c 215.4 739.0 216.8 737.1 218.9 736.1 c 220.8 735.1 221.4 733.0 " |
| 44 | L"223.7 732.4 c 232.6 729.9 242.0 730.8 251.2 730.8 c 257.5 730.8 " |
| 45 | L"263.0 732.9 269.0 734.4 c S"; |
| 46 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 47 | void VerifyFocusableAnnotSubtypes( |
| 48 | FPDF_FORMHANDLE form_handle, |
| 49 | pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) { |
| 50 | ASSERT_EQ(static_cast<int>(expected_subtypes.size()), |
| 51 | FPDFAnnot_GetFocusableSubtypesCount(form_handle)); |
| 52 | |
| 53 | std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes( |
| 54 | expected_subtypes.size()); |
| 55 | ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes( |
| 56 | form_handle, actual_subtypes.data(), actual_subtypes.size())); |
| 57 | for (size_t i = 0; i < expected_subtypes.size(); ++i) |
| 58 | ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]); |
| 59 | } |
| 60 | |
| 61 | void SetAndVerifyFocusableAnnotSubtypes( |
| 62 | FPDF_FORMHANDLE form_handle, |
| 63 | pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) { |
| 64 | ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(), |
| 65 | subtypes.size())); |
| 66 | VerifyFocusableAnnotSubtypes(form_handle, subtypes); |
| 67 | } |
| 68 | |
| 69 | void VerifyAnnotationSubtypesAndFocusability( |
| 70 | FPDF_FORMHANDLE form_handle, |
| 71 | FPDF_PAGE page, |
| 72 | pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes, |
| 73 | pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) { |
| 74 | ASSERT_EQ(static_cast<int>(expected_subtypes.size()), |
| 75 | FPDFPage_GetAnnotCount(page)); |
| 76 | for (size_t i = 0; i < expected_subtypes.size(); ++i) { |
| 77 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i)); |
| 78 | ASSERT_TRUE(annot); |
| 79 | EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get())); |
| 80 | |
Lei Zhang | f245ae6 | 2020-05-15 21:49:46 +0000 | [diff] [blame] | 81 | bool expected_focusable = |
| 82 | pdfium::Contains(expected_focusable_subtypes, expected_subtypes[i]); |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 83 | EXPECT_EQ(expected_focusable, |
| 84 | FORM_SetFocusedAnnot(form_handle, annot.get())); |
| 85 | |
| 86 | // Kill the focus so the next test starts in an unfocused state. |
| 87 | FORM_ForceToKillFocus(form_handle); |
| 88 | } |
| 89 | } |
| 90 | |
Lei Zhang | 306874b | 2021-04-16 00:33:36 +0000 | [diff] [blame] | 91 | void VerifyUriActionInLink(FPDF_DOCUMENT doc, |
| 92 | FPDF_LINK link, |
| 93 | const std::string& expected_uri) { |
| 94 | ASSERT_TRUE(link); |
| 95 | |
| 96 | FPDF_ACTION action = FPDFLink_GetAction(link); |
| 97 | ASSERT_TRUE(action); |
| 98 | EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI), |
| 99 | FPDFAction_GetType(action)); |
| 100 | |
| 101 | unsigned long bufsize = FPDFAction_GetURIPath(doc, action, nullptr, 0); |
| 102 | ASSERT_EQ(expected_uri.size() + 1, bufsize); |
| 103 | |
| 104 | std::vector<char> buffer(bufsize); |
| 105 | EXPECT_EQ(bufsize, |
| 106 | FPDFAction_GetURIPath(doc, action, buffer.data(), bufsize)); |
| 107 | EXPECT_STREQ(expected_uri.c_str(), buffer.data()); |
| 108 | } |
| 109 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 110 | } // namespace |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 111 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 112 | class FPDFAnnotEmbedderTest : public EmbedderTest {}; |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 113 | |
Tom Sepez | 204ab05 | 2020-06-12 21:33:48 +0000 | [diff] [blame] | 114 | TEST_F(FPDFAnnotEmbedderTest, SetAP) { |
| 115 | ScopedFPDFDocument doc(FPDF_CreateNewDocument()); |
| 116 | ASSERT_TRUE(doc); |
| 117 | ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100)); |
| 118 | ASSERT_TRUE(page); |
| 119 | ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData); |
| 120 | ASSERT_TRUE(ap_stream); |
| 121 | |
| 122 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK)); |
| 123 | ASSERT_TRUE(annot); |
| 124 | |
| 125 | // Negative case: FPDFAnnot_SetAP() should fail if bounding rect is not yet |
| 126 | // set on the annotation. |
| 127 | EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 128 | ap_stream.get())); |
| 129 | |
| 130 | const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f}; |
| 131 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect)); |
| 132 | |
| 133 | ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, |
| 134 | /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/255)); |
| 135 | |
| 136 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 137 | ap_stream.get())); |
| 138 | |
| 139 | // Verify that appearance stream is created as form XObject |
| 140 | CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get()); |
| 141 | ASSERT_TRUE(context); |
| 142 | CPDF_Dictionary* annot_dict = context->GetAnnotDict(); |
| 143 | ASSERT_TRUE(annot_dict); |
| 144 | CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP); |
| 145 | ASSERT_TRUE(ap_dict); |
| 146 | CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N"); |
| 147 | ASSERT_TRUE(stream_dict); |
| 148 | // Check for non-existence of resources dictionary in case of opaque color |
| 149 | CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources"); |
| 150 | ASSERT_FALSE(resources_dict); |
| 151 | ByteString type = stream_dict->GetStringFor(pdfium::annotation::kType); |
| 152 | EXPECT_EQ("XObject", type); |
| 153 | ByteString sub_type = stream_dict->GetStringFor(pdfium::annotation::kSubtype); |
| 154 | EXPECT_EQ("Form", sub_type); |
| 155 | |
| 156 | // Check that the appearance stream is same as we just set. |
| 157 | const uint32_t kStreamDataSize = |
| 158 | pdfium::size(kStreamData) * sizeof(FPDF_WCHAR); |
| 159 | unsigned long normal_length_bytes = FPDFAnnot_GetAP( |
| 160 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0); |
| 161 | ASSERT_EQ(kStreamDataSize, normal_length_bytes); |
| 162 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes); |
| 163 | EXPECT_EQ(kStreamDataSize, |
| 164 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 165 | buf.data(), normal_length_bytes)); |
| 166 | EXPECT_EQ(kStreamData, GetPlatformWString(buf.data())); |
| 167 | } |
| 168 | |
| 169 | TEST_F(FPDFAnnotEmbedderTest, SetAPWithOpacity) { |
| 170 | ScopedFPDFDocument doc(FPDF_CreateNewDocument()); |
| 171 | ASSERT_TRUE(doc); |
| 172 | ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100)); |
| 173 | ASSERT_TRUE(page); |
| 174 | ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData); |
| 175 | ASSERT_TRUE(ap_stream); |
| 176 | |
| 177 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK)); |
| 178 | ASSERT_TRUE(annot); |
| 179 | |
| 180 | ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, |
| 181 | /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/102)); |
| 182 | |
| 183 | const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f}; |
| 184 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect)); |
| 185 | |
| 186 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 187 | ap_stream.get())); |
| 188 | |
| 189 | CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get()); |
| 190 | ASSERT_TRUE(context); |
| 191 | CPDF_Dictionary* annot_dict = context->GetAnnotDict(); |
| 192 | ASSERT_TRUE(annot_dict); |
| 193 | CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP); |
| 194 | ASSERT_TRUE(ap_dict); |
| 195 | CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N"); |
| 196 | ASSERT_TRUE(stream_dict); |
| 197 | CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources"); |
| 198 | ASSERT_TRUE(stream_dict); |
| 199 | CPDF_Dictionary* extGState_dict = resources_dict->GetDictFor("ExtGState"); |
| 200 | ASSERT_TRUE(extGState_dict); |
| 201 | CPDF_Dictionary* gs_dict = extGState_dict->GetDictFor("GS"); |
| 202 | ASSERT_TRUE(gs_dict); |
| 203 | ByteString type = gs_dict->GetStringFor(pdfium::annotation::kType); |
| 204 | EXPECT_EQ("ExtGState", type); |
| 205 | float opacity = gs_dict->GetNumberFor("CA"); |
| 206 | // Opacity value of 102 is represented as 0.4f (=104/255) in /CA entry. |
| 207 | EXPECT_FLOAT_EQ(0.4f, opacity); |
| 208 | ByteString blend_mode = gs_dict->GetStringFor("BM"); |
| 209 | EXPECT_EQ("Normal", blend_mode); |
| 210 | bool alpha_source_flag = gs_dict->GetBooleanFor("AIS", true); |
| 211 | EXPECT_FALSE(alpha_source_flag); |
| 212 | } |
| 213 | |
| 214 | TEST_F(FPDFAnnotEmbedderTest, InkListAPIValidations) { |
| 215 | ScopedFPDFDocument doc(FPDF_CreateNewDocument()); |
| 216 | ASSERT_TRUE(doc); |
| 217 | ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100)); |
| 218 | ASSERT_TRUE(page); |
| 219 | |
| 220 | // Create a new ink annotation. |
| 221 | ScopedFPDFAnnotation ink_annot( |
| 222 | FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK)); |
| 223 | ASSERT_TRUE(ink_annot); |
| 224 | CPDF_AnnotContext* context = |
| 225 | CPDFAnnotContextFromFPDFAnnotation(ink_annot.get()); |
| 226 | ASSERT_TRUE(context); |
| 227 | CPDF_Dictionary* annot_dict = context->GetAnnotDict(); |
| 228 | ASSERT_TRUE(annot_dict); |
| 229 | |
| 230 | static constexpr FS_POINTF kFirstInkStroke[] = { |
| 231 | {80.0f, 90.0f}, {81.0f, 91.0f}, {82.0f, 92.0f}, |
| 232 | {83.0f, 93.0f}, {84.0f, 94.0f}, {85.0f, 95.0f}}; |
| 233 | static constexpr size_t kFirstStrokePointCount = |
| 234 | pdfium::size(kFirstInkStroke); |
| 235 | |
| 236 | static constexpr FS_POINTF kSecondInkStroke[] = { |
| 237 | {70.0f, 90.0f}, {71.0f, 91.0f}, {72.0f, 92.0f}}; |
| 238 | static constexpr size_t kSecondStrokePointCount = |
| 239 | pdfium::size(kSecondInkStroke); |
| 240 | |
| 241 | static constexpr FS_POINTF kThirdInkStroke[] = {{60.0f, 90.0f}, |
| 242 | {61.0f, 91.0f}, |
| 243 | {62.0f, 92.0f}, |
| 244 | {63.0f, 93.0f}, |
| 245 | {64.0f, 94.0f}}; |
| 246 | static constexpr size_t kThirdStrokePointCount = |
| 247 | pdfium::size(kThirdInkStroke); |
| 248 | |
| 249 | // Negative test: |annot| is passed as nullptr. |
| 250 | EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(nullptr, kFirstInkStroke, |
| 251 | kFirstStrokePointCount)); |
| 252 | |
| 253 | // Negative test: |annot| is not ink annotation. |
| 254 | // Create a new highlight annotation. |
| 255 | ScopedFPDFAnnotation highlight_annot( |
| 256 | FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT)); |
| 257 | ASSERT_TRUE(highlight_annot); |
| 258 | EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(highlight_annot.get(), kFirstInkStroke, |
| 259 | kFirstStrokePointCount)); |
| 260 | |
| 261 | // Negative test: passing |point_count| as 0. |
| 262 | EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke, 0)); |
| 263 | |
| 264 | // Negative test: passing |points| array as nullptr. |
| 265 | EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), nullptr, |
| 266 | kFirstStrokePointCount)); |
| 267 | |
| 268 | // Negative test: passing |point_count| more than ULONG_MAX/2. |
| 269 | EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke, |
| 270 | ULONG_MAX / 2 + 1)); |
| 271 | |
| 272 | // InkStroke should get added to ink annotation. Also inklist should get |
| 273 | // created. |
| 274 | EXPECT_EQ(0, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke, |
| 275 | kFirstStrokePointCount)); |
| 276 | |
| 277 | CPDF_Array* inklist = annot_dict->GetArrayFor("InkList"); |
| 278 | ASSERT_TRUE(inklist); |
| 279 | EXPECT_EQ(1u, inklist->size()); |
| 280 | EXPECT_EQ(kFirstStrokePointCount * 2, inklist->GetArrayAt(0)->size()); |
| 281 | |
| 282 | // Adding another inkStroke to ink annotation with all valid paremeters. |
| 283 | // InkList already exists in ink_annot. |
| 284 | EXPECT_EQ(1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke, |
| 285 | kSecondStrokePointCount)); |
| 286 | EXPECT_EQ(2u, inklist->size()); |
| 287 | EXPECT_EQ(kSecondStrokePointCount * 2, inklist->GetArrayAt(1)->size()); |
| 288 | |
| 289 | // Adding one more InkStroke to the ink annotation. |point_count| passed is |
| 290 | // less than the data available in |buffer|. |
| 291 | EXPECT_EQ(2, FPDFAnnot_AddInkStroke(ink_annot.get(), kThirdInkStroke, |
| 292 | kThirdStrokePointCount - 1)); |
| 293 | EXPECT_EQ(3u, inklist->size()); |
| 294 | EXPECT_EQ((kThirdStrokePointCount - 1) * 2, inklist->GetArrayAt(2)->size()); |
| 295 | } |
| 296 | |
| 297 | TEST_F(FPDFAnnotEmbedderTest, RemoveInkList) { |
| 298 | ScopedFPDFDocument doc(FPDF_CreateNewDocument()); |
| 299 | ASSERT_TRUE(doc); |
| 300 | ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100)); |
| 301 | ASSERT_TRUE(page); |
| 302 | |
| 303 | // Negative test: |annot| is passed as nullptr. |
| 304 | EXPECT_FALSE(FPDFAnnot_RemoveInkList(nullptr)); |
| 305 | |
| 306 | // Negative test: |annot| is not ink annotation. |
| 307 | // Create a new highlight annotation. |
| 308 | ScopedFPDFAnnotation highlight_annot( |
| 309 | FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT)); |
| 310 | ASSERT_TRUE(highlight_annot); |
| 311 | EXPECT_FALSE(FPDFAnnot_RemoveInkList(highlight_annot.get())); |
| 312 | |
| 313 | // Create a new ink annotation. |
| 314 | ScopedFPDFAnnotation ink_annot( |
| 315 | FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK)); |
| 316 | ASSERT_TRUE(ink_annot); |
| 317 | CPDF_AnnotContext* context = |
| 318 | CPDFAnnotContextFromFPDFAnnotation(ink_annot.get()); |
| 319 | ASSERT_TRUE(context); |
| 320 | CPDF_Dictionary* annot_dict = context->GetAnnotDict(); |
| 321 | ASSERT_TRUE(annot_dict); |
| 322 | |
| 323 | static constexpr FS_POINTF kInkStroke[] = {{80.0f, 90.0f}, {81.0f, 91.0f}, |
| 324 | {82.0f, 92.0f}, {83.0f, 93.0f}, |
| 325 | {84.0f, 94.0f}, {85.0f, 95.0f}}; |
| 326 | static constexpr size_t kPointCount = pdfium::size(kInkStroke); |
| 327 | |
| 328 | // InkStroke should get added to ink annotation. Also inklist should get |
| 329 | // created. |
| 330 | EXPECT_EQ(0, |
| 331 | FPDFAnnot_AddInkStroke(ink_annot.get(), kInkStroke, kPointCount)); |
| 332 | |
| 333 | CPDF_Array* inklist = annot_dict->GetArrayFor("InkList"); |
| 334 | ASSERT_TRUE(inklist); |
| 335 | ASSERT_EQ(1u, inklist->size()); |
| 336 | EXPECT_EQ(kPointCount * 2, inklist->GetArrayAt(0)->size()); |
| 337 | |
| 338 | // Remove inklist. |
| 339 | EXPECT_TRUE(FPDFAnnot_RemoveInkList(ink_annot.get())); |
| 340 | EXPECT_FALSE(annot_dict->KeyExist("InkList")); |
| 341 | } |
| 342 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 343 | TEST_F(FPDFAnnotEmbedderTest, BadParams) { |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 344 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| 345 | FPDF_PAGE page = LoadPage(0); |
| 346 | ASSERT_TRUE(page); |
| 347 | |
| 348 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr)); |
| 349 | |
| 350 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0)); |
| 351 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1)); |
| 352 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1)); |
| 353 | EXPECT_FALSE(FPDFPage_GetAnnot(page, -1)); |
| 354 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 1)); |
| 355 | |
| 356 | EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr)); |
| 357 | |
| 358 | EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr)); |
| 359 | |
| 360 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0)); |
| 361 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1)); |
| 362 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1)); |
| 363 | |
| 364 | EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo")); |
| 365 | |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 366 | static const wchar_t kContents[] = L"Bar"; |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 367 | ScopedFPDFWideString text = GetFPDFWideString(kContents); |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 368 | EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get())); |
| 369 | |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 370 | FPDF_WCHAR buffer[64]; |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 371 | EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0)); |
| 372 | EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0)); |
| 373 | EXPECT_EQ(0u, |
| 374 | FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer))); |
| 375 | |
| 376 | UnloadPage(page); |
| 377 | } |
| 378 | |
Lei Zhang | 3d9a097 | 2019-03-04 19:34:09 +0000 | [diff] [blame] | 379 | TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) { |
| 380 | ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf")); |
| 381 | FPDF_PAGE page = LoadPage(0); |
| 382 | ASSERT_TRUE(page); |
| 383 | |
| 384 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Lei Zhang | 98dc8c0 | 2019-03-04 19:40:30 +0000 | [diff] [blame] | 385 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | 3d9a097 | 2019-03-04 19:34:09 +0000 | [diff] [blame] | 386 | |
| 387 | UnloadPage(page); |
| 388 | } |
| 389 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 390 | TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) { |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 391 | // Open a file with one annotation and load its first page. |
| 392 | ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 393 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 394 | ASSERT_TRUE(page); |
| 395 | |
| 396 | // This annotation has a malformed appearance stream, which does not have its |
| 397 | // normal appearance defined, only its rollover appearance. In this case, its |
| 398 | // normal appearance should be generated, allowing the highlight annotation to |
| 399 | // still display. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 400 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | a98e366 | 2018-02-07 20:28:35 +0000 | [diff] [blame] | 401 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 402 | |
| 403 | UnloadPage(page); |
| 404 | } |
| 405 | |
Hui Yingst | d5b6f63 | 2020-07-22 01:31:59 +0000 | [diff] [blame] | 406 | TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) { |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 407 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Hui Yingst | d5b6f63 | 2020-07-22 01:31:59 +0000 | [diff] [blame] | 408 | static const char kChecksum[] = "ec1f4ccbd0aecfdea6d53893387a0101"; |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 409 | #else |
Hui Yingst | d5b6f63 | 2020-07-22 01:31:59 +0000 | [diff] [blame] | 410 | static const char kChecksum[] = "76512832d88017668d9acc7aacd13dae"; |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 411 | #endif |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 412 | // Open a file with multiline markup annotations. |
Ralf Sippl | b3a5240 | 2018-03-19 23:30:28 +0000 | [diff] [blame] | 413 | ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf")); |
| 414 | FPDF_PAGE page = LoadPage(0); |
| 415 | ASSERT_TRUE(page); |
| 416 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 417 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Hui Yingst | d5b6f63 | 2020-07-22 01:31:59 +0000 | [diff] [blame] | 418 | CompareBitmap(bitmap.get(), 595, 842, kChecksum); |
Ralf Sippl | b3a5240 | 2018-03-19 23:30:28 +0000 | [diff] [blame] | 419 | |
| 420 | UnloadPage(page); |
| 421 | } |
| 422 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 423 | TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) { |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 424 | // Open a file with one annotation and load its first page. |
| 425 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 426 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 427 | ASSERT_TRUE(page); |
| 428 | |
| 429 | // Check that there is a total of 1 annotation on its first page. |
| 430 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 431 | |
| 432 | // Check that the annotation is of type "highlight". |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 433 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 434 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 435 | ASSERT_TRUE(annot); |
| 436 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 437 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 438 | // Check that the annotation color is yellow. |
| 439 | unsigned int R; |
| 440 | unsigned int G; |
| 441 | unsigned int B; |
| 442 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 443 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 444 | &G, &B, &A)); |
| 445 | EXPECT_EQ(255u, R); |
| 446 | EXPECT_EQ(255u, G); |
| 447 | EXPECT_EQ(0u, B); |
| 448 | EXPECT_EQ(255u, A); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 449 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 450 | // Check that the author is correct. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 451 | static const char kAuthorKey[] = "T"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 452 | EXPECT_EQ(FPDF_OBJECT_STRING, |
| 453 | FPDFAnnot_GetValueType(annot.get(), kAuthorKey)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 454 | unsigned long length_bytes = |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 455 | FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 456 | ASSERT_EQ(28u, length_bytes); |
| 457 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 458 | EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(), |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 459 | length_bytes)); |
| 460 | EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 461 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 462 | // Check that the content is correct. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 463 | EXPECT_EQ( |
| 464 | FPDF_OBJECT_STRING, |
| 465 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 466 | length_bytes = FPDFAnnot_GetStringValue( |
| 467 | annot.get(), pdfium::annotation::kContents, nullptr, 0); |
| 468 | ASSERT_EQ(2690u, length_bytes); |
| 469 | buf = GetFPDFWideStringBuffer(length_bytes); |
| 470 | EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(), |
| 471 | pdfium::annotation::kContents, |
| 472 | buf.data(), length_bytes)); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 473 | static const wchar_t kContents[] = |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 474 | L"This is a note for that highlight annotation. Very long highlight " |
| 475 | "annotation. Long long long Long 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 longLong long longLong long longLong long longLong long " |
| 493 | "longLong long long. END"; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 494 | EXPECT_EQ(kContents, GetPlatformWString(buf.data())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 495 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 496 | // Check that the quadpoints are correct. |
| 497 | FS_QUADPOINTSF quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 498 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 499 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 500 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 501 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 502 | EXPECT_EQ(706.264465f, quadpoints.y4); |
| 503 | } |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 504 | UnloadPageNoEvents(page); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 505 | } |
| 506 | |
Hui Yingst | 9b6b154 | 2020-07-27 16:11:12 +0000 | [diff] [blame] | 507 | TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) { |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 508 | // Open a file with three annotations and load its first page. |
| 509 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 510 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 511 | ASSERT_TRUE(page); |
| 512 | |
| 513 | // Check that there is a total of 3 annotation on its first page. |
| 514 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 515 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 516 | { |
| 517 | // Check that the third annotation is of type "ink". |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 518 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 519 | ASSERT_TRUE(annot); |
| 520 | EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 521 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 522 | // Check that the annotation color is blue with opacity. |
| 523 | unsigned int R; |
| 524 | unsigned int G; |
| 525 | unsigned int B; |
| 526 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 527 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 528 | &G, &B, &A)); |
| 529 | EXPECT_EQ(0u, R); |
| 530 | EXPECT_EQ(0u, G); |
| 531 | EXPECT_EQ(255u, B); |
| 532 | EXPECT_EQ(76u, A); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 533 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 534 | // Check that there is no content. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 535 | EXPECT_EQ(2u, FPDFAnnot_GetStringValue( |
| 536 | annot.get(), pdfium::annotation::kContents, nullptr, 0)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 537 | |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 538 | // Check that the rectangle coordinates are correct. |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 539 | // Note that upon rendering, the rectangle coordinates will be adjusted. |
| 540 | FS_RECTF rect; |
| 541 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 542 | EXPECT_EQ(351.820404f, rect.left); |
| 543 | EXPECT_EQ(583.830688f, rect.bottom); |
| 544 | EXPECT_EQ(475.336090f, rect.right); |
| 545 | EXPECT_EQ(681.535034f, rect.top); |
| 546 | } |
Tom Sepez | ef43c26 | 2018-11-07 16:41:32 +0000 | [diff] [blame] | 547 | { |
Hui Yingst | 57daee8 | 2020-10-09 05:47:20 +0000 | [diff] [blame] | 548 | #if defined(_SKIA_SUPPORT_) && defined(OS_APPLE) |
| 549 | static constexpr char kExpectedHash[] = "fad91b9c968fe8019a774f5e2419b8fc"; |
| 550 | #elif defined(_SKIA_SUPPORT_PATHS_) && defined(OS_APPLE) |
Hui Yingst | 9b6b154 | 2020-07-27 16:11:12 +0000 | [diff] [blame] | 551 | static constexpr char kExpectedHash[] = "acddfe688a117ead56af7b249a2cf8a1"; |
Hui Yingst | 57daee8 | 2020-10-09 05:47:20 +0000 | [diff] [blame] | 552 | #elif defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Hui Yingst | 9b6b154 | 2020-07-27 16:11:12 +0000 | [diff] [blame] | 553 | static constexpr char kExpectedHash[] = "1fb0dd8dd5f0b9bb8d076e48eb59296d"; |
Hui Yingst | 57daee8 | 2020-10-09 05:47:20 +0000 | [diff] [blame] | 554 | #elif defined(OS_WIN) |
Lei Zhang | 430b532 | 2020-07-06 22:23:36 +0000 | [diff] [blame] | 555 | static constexpr char kExpectedHash[] = "49d0a81c636531a337429325273d0508"; |
| 556 | #else |
| 557 | static constexpr char kExpectedHash[] = "354002e1c4386d38fdde29ef8d61074a"; |
Hui Yingst | 57daee8 | 2020-10-09 05:47:20 +0000 | [diff] [blame] | 558 | #endif |
Tom Sepez | ef43c26 | 2018-11-07 16:41:32 +0000 | [diff] [blame] | 559 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 430b532 | 2020-07-06 22:23:36 +0000 | [diff] [blame] | 560 | CompareBitmap(bitmap.get(), 612, 792, kExpectedHash); |
Tom Sepez | ef43c26 | 2018-11-07 16:41:32 +0000 | [diff] [blame] | 561 | } |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 562 | UnloadPageNoEvents(page); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 563 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 564 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 565 | TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 566 | // Open a file with one annotation and load its first page. |
| 567 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 568 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 569 | ASSERT_TRUE(page); |
| 570 | |
| 571 | // Add an annotation with an illegal subtype. |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 572 | ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 573 | |
| 574 | UnloadPage(page); |
| 575 | } |
| 576 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 577 | TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 578 | // Open a file with no annotation and load its first page. |
| 579 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 580 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 581 | ASSERT_TRUE(page); |
| 582 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(page)); |
| 583 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 584 | { |
| 585 | // Add a text annotation to the page. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 586 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 587 | ASSERT_TRUE(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 588 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 589 | // Check that there is now 1 annotations on this page. |
| 590 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 591 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 592 | // Check that the subtype of the annotation is correct. |
| 593 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 594 | } |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 595 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 596 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 597 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 598 | ASSERT_TRUE(annot); |
| 599 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 600 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 601 | // Set the color of the annotation. |
| 602 | ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51, |
| 603 | 102, 153, 204)); |
| 604 | // Check that the color has been set correctly. |
| 605 | unsigned int R; |
| 606 | unsigned int G; |
| 607 | unsigned int B; |
| 608 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 609 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 610 | &G, &B, &A)); |
| 611 | EXPECT_EQ(51u, R); |
| 612 | EXPECT_EQ(102u, G); |
| 613 | EXPECT_EQ(153u, B); |
| 614 | EXPECT_EQ(204u, A); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 615 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 616 | // Change the color of the annotation. |
| 617 | ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204, |
| 618 | 153, 102, 51)); |
| 619 | // Check that the color has been set correctly. |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 620 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 621 | &G, &B, &A)); |
| 622 | EXPECT_EQ(204u, R); |
| 623 | EXPECT_EQ(153u, G); |
| 624 | EXPECT_EQ(102u, B); |
| 625 | EXPECT_EQ(51u, A); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 626 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 627 | // Set the annotation rectangle. |
| 628 | FS_RECTF rect; |
| 629 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 630 | EXPECT_EQ(0.f, rect.left); |
| 631 | EXPECT_EQ(0.f, rect.right); |
| 632 | rect.left = 35; |
| 633 | rect.bottom = 150; |
| 634 | rect.right = 53; |
| 635 | rect.top = 165; |
| 636 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 637 | // Check that the annotation rectangle has been set correctly. |
| 638 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 639 | EXPECT_EQ(35.f, rect.left); |
| 640 | EXPECT_EQ(150.f, rect.bottom); |
| 641 | EXPECT_EQ(53.f, rect.right); |
| 642 | EXPECT_EQ(165.f, rect.top); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 643 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 644 | // Set the content of the annotation. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 645 | static const wchar_t kContents[] = L"Hello! This is a customized content."; |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 646 | ScopedFPDFWideString text = GetFPDFWideString(kContents); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 647 | ASSERT_TRUE(FPDFAnnot_SetStringValue( |
| 648 | annot.get(), pdfium::annotation::kContents, text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 649 | // Check that the content has been set correctly. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 650 | unsigned long length_bytes = FPDFAnnot_GetStringValue( |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 651 | annot.get(), pdfium::annotation::kContents, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 652 | ASSERT_EQ(74u, length_bytes); |
| 653 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 654 | EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(), |
| 655 | pdfium::annotation::kContents, |
| 656 | buf.data(), length_bytes)); |
| 657 | EXPECT_EQ(kContents, GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 658 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 659 | UnloadPage(page); |
| 660 | } |
| 661 | |
Lei Zhang | 81395aa | 2021-04-16 00:40:46 +0000 | [diff] [blame^] | 662 | TEST_F(FPDFAnnotEmbedderTest, AddAndSaveLinkAnnotation) { |
| 663 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| 664 | FPDF_PAGE page = LoadPage(0); |
| 665 | ASSERT_TRUE(page); |
| 666 | { |
| 667 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 668 | CompareBitmap(bitmap.get(), 200, 200, pdfium::kHelloWorldChecksum); |
| 669 | } |
| 670 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(page)); |
| 671 | |
| 672 | constexpr char kUri[] = "https://pdfium.org/"; |
| 673 | |
| 674 | { |
| 675 | // Add a link annotation to the page and set its URI. |
| 676 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_LINK)); |
| 677 | ASSERT_TRUE(annot); |
| 678 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 679 | EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get())); |
| 680 | EXPECT_TRUE(FPDFAnnot_SetURI(annot.get(), kUri)); |
| 681 | VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri); |
| 682 | |
| 683 | // Negative tests: |
| 684 | EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, nullptr)); |
| 685 | VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri); |
| 686 | EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), nullptr)); |
| 687 | VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri); |
| 688 | EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, kUri)); |
| 689 | VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri); |
| 690 | |
| 691 | // Position the link on top of "Hello, world!" without a border. |
| 692 | const FS_RECTF kRect = {19.0f, 48.0f, 85.0f, 60.0f}; |
| 693 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &kRect)); |
| 694 | EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/0.0f, |
| 695 | /*vertical_radius=*/0.0f, |
| 696 | /*border_width=*/0.0f)); |
| 697 | |
| 698 | VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0), |
| 699 | kUri); |
| 700 | } |
| 701 | |
| 702 | { |
| 703 | // Add an ink annotation to the page. Trying to add a link to it fails. |
| 704 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK)); |
| 705 | ASSERT_TRUE(annot); |
| 706 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 707 | EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get())); |
| 708 | EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), kUri)); |
| 709 | } |
| 710 | |
| 711 | // Remove the ink annotation added above for negative testing. |
| 712 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1)); |
| 713 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 714 | |
| 715 | // Save the document, closing the page. |
| 716 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 717 | UnloadPage(page); |
| 718 | |
| 719 | // Reopen the document and make sure it still renders the same. Since the link |
| 720 | // does not have a border, it does not affect the rendering. |
| 721 | ASSERT_TRUE(OpenSavedDocument()); |
| 722 | page = LoadSavedPage(0); |
| 723 | ASSERT_TRUE(page); |
| 724 | VerifySavedRendering(page, 200, 200, pdfium::kHelloWorldChecksum); |
| 725 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 726 | |
| 727 | { |
| 728 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 729 | ASSERT_TRUE(annot); |
| 730 | EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get())); |
| 731 | VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri); |
| 732 | VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0), |
| 733 | kUri); |
| 734 | } |
| 735 | |
| 736 | CloseSavedPage(page); |
| 737 | CloseSavedDocument(); |
| 738 | } |
| 739 | |
Hui Yingst | b349032 | 2020-07-22 00:53:29 +0000 | [diff] [blame] | 740 | TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 741 | // Open a file with one annotation and load its first page. |
| 742 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 743 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 744 | ASSERT_TRUE(page); |
| 745 | |
| 746 | // Check that there is a total of one annotation on its first page, and verify |
| 747 | // its quadpoints. |
| 748 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 749 | FS_QUADPOINTSF quadpoints; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 750 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 751 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 752 | ASSERT_TRUE(annot); |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 753 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 754 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 755 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 756 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 757 | EXPECT_EQ(706.264465f, quadpoints.y4); |
| 758 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 759 | |
| 760 | // Add an underline annotation to the page and set its quadpoints. |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 761 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 762 | ScopedFPDFAnnotation annot( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 763 | FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE)); |
| 764 | ASSERT_TRUE(annot); |
| 765 | quadpoints.x1 = 140.802643f; |
| 766 | quadpoints.x3 = 140.802643f; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 767 | ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 768 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 769 | |
Lei Zhang | ec61814 | 2021-04-13 17:36:46 +0000 | [diff] [blame] | 770 | // Save the document and close the page. |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 771 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 772 | UnloadPage(page); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 773 | |
| 774 | // Open the saved document. |
Hui Yingst | 57daee8 | 2020-10-09 05:47:20 +0000 | [diff] [blame] | 775 | #if defined(_SKIA_SUPPORT_) && defined(OS_APPLE) |
| 776 | static const char kChecksum[] = "899387ae792390cd0d83cf7e2bbebfb5"; |
| 777 | #elif defined(_SKIA_SUPPORT_PATHS_) && defined(OS_APPLE) |
| 778 | static const char kChecksum[] = "e40e235ee35f47ff28dda009aaaf36df"; |
| 779 | #elif defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Hui Yingst | b349032 | 2020-07-22 00:53:29 +0000 | [diff] [blame] | 780 | static const char kChecksum[] = "798fa41303381c9ba6d99092f5cd4d2b"; |
| 781 | #else |
| 782 | static const char kChecksum[] = "dba153419f67b7c0c0e3d22d3e8910d5"; |
| 783 | #endif |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 784 | |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 785 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 786 | page = LoadSavedPage(0); |
Lei Zhang | ec61814 | 2021-04-13 17:36:46 +0000 | [diff] [blame] | 787 | ASSERT_TRUE(page); |
Hui Yingst | b349032 | 2020-07-22 00:53:29 +0000 | [diff] [blame] | 788 | VerifySavedRendering(page, 612, 792, kChecksum); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 789 | |
| 790 | // Check that the saved document has 2 annotations on the first page |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 791 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 792 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 793 | { |
| 794 | // Check that the second annotation is an underline annotation and verify |
| 795 | // its quadpoints. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 796 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 797 | ASSERT_TRUE(new_annot); |
| 798 | EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get())); |
| 799 | FS_QUADPOINTSF new_quadpoints; |
| 800 | ASSERT_TRUE( |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 801 | FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 802 | EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f); |
| 803 | EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f); |
| 804 | EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f); |
| 805 | EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f); |
| 806 | } |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 807 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 808 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 809 | CloseSavedDocument(); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 810 | } |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 811 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 812 | TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) { |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 813 | // Open a file with four annotations and load its first page. |
| 814 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
| 815 | FPDF_PAGE page = LoadPage(0); |
| 816 | ASSERT_TRUE(page); |
| 817 | EXPECT_EQ(4, FPDFPage_GetAnnotCount(page)); |
| 818 | |
| 819 | // Retrieve the highlight annotation. |
| 820 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 821 | ASSERT_TRUE(annot); |
| 822 | ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot)); |
| 823 | |
| 824 | FS_QUADPOINTSF quadpoints; |
| 825 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints)); |
| 826 | |
| 827 | { |
| 828 | // Verify the current one set of quadpoints. |
| 829 | ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 830 | |
| 831 | EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f); |
| 832 | EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f); |
| 833 | EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f); |
| 834 | EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f); |
| 835 | } |
| 836 | |
| 837 | { |
| 838 | // Update the quadpoints. |
| 839 | FS_QUADPOINTSF new_quadpoints = quadpoints; |
| 840 | new_quadpoints.y1 -= 20.f; |
| 841 | new_quadpoints.y2 -= 20.f; |
| 842 | new_quadpoints.y3 -= 20.f; |
| 843 | new_quadpoints.y4 -= 20.f; |
| 844 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints)); |
| 845 | |
| 846 | // Verify added quadpoint set |
| 847 | ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 848 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints)); |
| 849 | EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f); |
| 850 | EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f); |
| 851 | EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f); |
| 852 | EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f); |
| 853 | } |
| 854 | |
| 855 | { |
| 856 | // Append a new set of quadpoints. |
| 857 | FS_QUADPOINTSF new_quadpoints = quadpoints; |
| 858 | new_quadpoints.y1 += 20.f; |
| 859 | new_quadpoints.y2 += 20.f; |
| 860 | new_quadpoints.y3 += 20.f; |
| 861 | new_quadpoints.y4 += 20.f; |
| 862 | ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints)); |
| 863 | |
| 864 | // Verify added quadpoint set |
| 865 | ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 866 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints)); |
| 867 | EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f); |
| 868 | EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f); |
| 869 | EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f); |
| 870 | EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f); |
| 871 | } |
| 872 | |
| 873 | { |
| 874 | // Setting and getting quadpoints at out-of-bound index should fail |
| 875 | EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints)); |
| 876 | EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints)); |
| 877 | } |
| 878 | |
| 879 | FPDFPage_CloseAnnot(annot); |
| 880 | |
| 881 | // Retrieve the square annotation |
| 882 | FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2); |
| 883 | |
| 884 | { |
| 885 | // Check that attempting to set its quadpoints would fail |
| 886 | ASSERT_TRUE(squareAnnot); |
| 887 | EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot)); |
| 888 | EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot)); |
| 889 | EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints)); |
| 890 | } |
| 891 | |
| 892 | FPDFPage_CloseAnnot(squareAnnot); |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 893 | UnloadPage(page); |
| 894 | } |
| 895 | |
Hui Yingst | b64cd12 | 2020-07-27 16:01:02 +0000 | [diff] [blame] | 896 | // TODO(crbug.com/pdfium/1569): Fix this issue and enable the test for Skia. |
| 897 | #if defined(_SKIA_SUPPORT_) |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 898 | #define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP |
| 899 | #else |
| 900 | #define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP |
| 901 | #endif |
| 902 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) { |
Hui Yingst | b64cd12 | 2020-07-27 16:01:02 +0000 | [diff] [blame] | 903 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 904 | static const char kMd5Original[] = "00e70eb543c2a6e8f8aafb4ee951d9bf"; |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 905 | static const char kMd5ModifiedHighlight[] = |
Hui Yingst | b64cd12 | 2020-07-27 16:01:02 +0000 | [diff] [blame] | 906 | "7638c4a8fe4aabbf8704e198f49b3198"; |
| 907 | static const char kMd5ModifiedSquare[] = "54f507af6af63de877b9cafdab1bbdaa"; |
| 908 | #else |
| 909 | #if defined(OS_WIN) |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 910 | static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5"; |
| 911 | static const char kMd5ModifiedHighlight[] = |
| 912 | "66f3caef3a7d488a4fa1ad37fc06310e"; |
| 913 | static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563"; |
Lei Zhang | 0c03d63 | 2020-07-30 17:05:36 +0000 | [diff] [blame] | 914 | #elif defined(OS_APPLE) |
Hui Yingst | b64cd12 | 2020-07-27 16:01:02 +0000 | [diff] [blame] | 915 | static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a"; |
| 916 | static const char kMd5ModifiedHighlight[] = |
| 917 | "e64bf648f6e9354d1f3eedb47a2c9498"; |
| 918 | static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf"; |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 919 | #else |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 920 | static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5"; |
| 921 | static const char kMd5ModifiedHighlight[] = |
| 922 | "66f3caef3a7d488a4fa1ad37fc06310e"; |
| 923 | static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563"; |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 924 | #endif |
Hui Yingst | b64cd12 | 2020-07-27 16:01:02 +0000 | [diff] [blame] | 925 | #endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 926 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 927 | // Open a file with four annotations and load its first page. |
| 928 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 929 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 930 | ASSERT_TRUE(page); |
| 931 | EXPECT_EQ(4, FPDFPage_GetAnnotCount(page)); |
| 932 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 933 | // Check that the original file renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 934 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 935 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 936 | CompareBitmap(bitmap.get(), 612, 792, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 937 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 938 | |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 939 | FS_RECTF rect; |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 940 | FS_RECTF new_rect; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 941 | |
| 942 | // Retrieve the highlight annotation which has its AP stream already defined. |
| 943 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 944 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 945 | ASSERT_TRUE(annot); |
| 946 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 947 | |
| 948 | // Check that color cannot be set when an AP stream is defined already. |
| 949 | EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51, |
| 950 | 102, 153, 204)); |
| 951 | |
| 952 | // Verify its attachment points. |
| 953 | FS_QUADPOINTSF quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 954 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 955 | EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f); |
| 956 | EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f); |
| 957 | EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f); |
| 958 | EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f); |
| 959 | |
| 960 | // Check that updating the attachment points would succeed. |
| 961 | quadpoints.x1 -= 50.f; |
| 962 | quadpoints.x2 -= 50.f; |
| 963 | quadpoints.x3 -= 50.f; |
| 964 | quadpoints.x4 -= 50.f; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 965 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 966 | FS_QUADPOINTSF new_quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 967 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 968 | EXPECT_EQ(quadpoints.x1, new_quadpoints.x1); |
| 969 | EXPECT_EQ(quadpoints.y1, new_quadpoints.y1); |
| 970 | EXPECT_EQ(quadpoints.x4, new_quadpoints.x4); |
| 971 | EXPECT_EQ(quadpoints.y4, new_quadpoints.y4); |
| 972 | |
| 973 | // Check that updating quadpoints does not change the annotation's position. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 974 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 975 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 976 | CompareBitmap(bitmap.get(), 612, 792, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 977 | } |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 978 | |
| 979 | // Verify its annotation rectangle. |
| 980 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 981 | EXPECT_NEAR(67.7299f, rect.left, 0.001f); |
| 982 | EXPECT_NEAR(704.296f, rect.bottom, 0.001f); |
| 983 | EXPECT_NEAR(136.325f, rect.right, 0.001f); |
| 984 | EXPECT_NEAR(721.292f, rect.top, 0.001f); |
| 985 | |
| 986 | // Check that updating the rectangle would succeed. |
| 987 | rect.left -= 60.f; |
| 988 | rect.right -= 60.f; |
| 989 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 990 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 991 | EXPECT_EQ(rect.right, new_rect.right); |
| 992 | } |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 993 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 994 | // Check that updating the rectangle changes the annotation's position. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 995 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 996 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 997 | CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 998 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 999 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1000 | { |
| 1001 | // Retrieve the square annotation which has its AP stream already defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1002 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1003 | ASSERT_TRUE(annot); |
| 1004 | EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 1005 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1006 | // Check that updating the rectangle would succeed. |
| 1007 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 1008 | rect.left += 70.f; |
| 1009 | rect.right += 70.f; |
| 1010 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 1011 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 1012 | EXPECT_EQ(rect.right, new_rect.right); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 1013 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1014 | // Check that updating the rectangle changes the square annotation's |
| 1015 | // position. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1016 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1017 | CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1018 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 1019 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 1020 | UnloadPage(page); |
| 1021 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1022 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1023 | TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) { |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 1024 | // Open a file with multiline markup annotations. |
| 1025 | ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf")); |
| 1026 | FPDF_PAGE page = LoadPage(0); |
| 1027 | ASSERT_TRUE(page); |
| 1028 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1029 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 1030 | ASSERT_TRUE(annot); |
| 1031 | |
| 1032 | // This is a three line annotation. |
| 1033 | EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get())); |
| 1034 | } |
| 1035 | UnloadPage(page); |
| 1036 | |
| 1037 | // null annotation should return 0 |
| 1038 | EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr)); |
| 1039 | } |
| 1040 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1041 | TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) { |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1042 | // Open a file with 3 annotations on its first page. |
| 1043 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 1044 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1045 | ASSERT_TRUE(page); |
| 1046 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 1047 | |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 1048 | FS_RECTF rect; |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1049 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1050 | // Check that the annotations have the expected rectangle coordinates. |
| 1051 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1052 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1053 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 1054 | EXPECT_NEAR(86.1971f, rect.left, 0.001f); |
| 1055 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1056 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1057 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1058 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1059 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 1060 | EXPECT_NEAR(149.8127f, rect.left, 0.001f); |
| 1061 | } |
| 1062 | |
| 1063 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1064 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1065 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 1066 | EXPECT_NEAR(351.8204f, rect.left, 0.001f); |
| 1067 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1068 | |
| 1069 | // Check that nothing happens when attempting to remove an annotation with an |
| 1070 | // out-of-bound index. |
| 1071 | EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4)); |
| 1072 | EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1)); |
| 1073 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 1074 | |
| 1075 | // Remove the second annotation. |
| 1076 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1)); |
| 1077 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 1078 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 2)); |
| 1079 | |
Lei Zhang | ec61814 | 2021-04-13 17:36:46 +0000 | [diff] [blame] | 1080 | // Save the document and close the page. |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1081 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 1082 | UnloadPageNoEvents(page); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1083 | |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1084 | // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1?? |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1085 | // Open the saved document. |
| 1086 | std::string new_file = GetString(); |
| 1087 | FPDF_FILEACCESS file_access; |
| 1088 | memset(&file_access, 0, sizeof(file_access)); |
| 1089 | file_access.m_FileLen = new_file.size(); |
| 1090 | file_access.m_GetBlock = GetBlockFromString; |
| 1091 | file_access.m_Param = &new_file; |
| 1092 | FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 1093 | ASSERT_TRUE(new_doc); |
| 1094 | FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); |
| 1095 | ASSERT_TRUE(new_page); |
| 1096 | |
| 1097 | // Check that the saved document has 2 annotations on the first page. |
| 1098 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page)); |
| 1099 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1100 | // Check that the remaining 2 annotations are the original 1st and 3rd ones |
| 1101 | // by verifying their rectangle coordinates. |
| 1102 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1103 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1104 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 1105 | EXPECT_NEAR(86.1971f, rect.left, 0.001f); |
| 1106 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1107 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1108 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1109 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1110 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 1111 | EXPECT_NEAR(351.8204f, rect.left, 0.001f); |
| 1112 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1113 | FPDF_ClosePage(new_page); |
| 1114 | FPDF_CloseDocument(new_doc); |
| 1115 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1116 | |
Hui Yingst | 4a21df0 | 2020-05-13 03:15:44 +0000 | [diff] [blame] | 1117 | TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) { |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1118 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Lei Zhang | fe8e758 | 2020-08-05 19:23:16 +0000 | [diff] [blame] | 1119 | #if defined(OS_LINUX) || defined(OS_CHROMEOS) |
Hui Yingst | 4a21df0 | 2020-05-13 03:15:44 +0000 | [diff] [blame] | 1120 | static const char kMd5ModifiedPath[] = "d76382fd57fafad0233f7549f871dafa"; |
| 1121 | static const char kMd5TwoPaths[] = "323151317b8cb62130546c7b8d70f622"; |
| 1122 | static const char kMd5NewAnnot[] = "9a3b02d876620d19787549ee1100b63c"; |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1123 | #else |
Hui Yingst | 4a21df0 | 2020-05-13 03:15:44 +0000 | [diff] [blame] | 1124 | static const char kMd5ModifiedPath[] = "c9ba60887a312370d9a32198aa53aca4"; |
| 1125 | static const char kMd5TwoPaths[] = "0768d56373094fcdf4ddf3f3364c006f"; |
| 1126 | static const char kMd5NewAnnot[] = "6f7e1c189bcfac90ffccf2a527857006"; |
Lei Zhang | fe8e758 | 2020-08-05 19:23:16 +0000 | [diff] [blame] | 1127 | #endif // defined(OS_LINUX) || defined(OS_CHROMEOS) |
Hui Yingst | 4a21df0 | 2020-05-13 03:15:44 +0000 | [diff] [blame] | 1128 | #else |
| 1129 | #if defined(OS_WIN) |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1130 | static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1"; |
| 1131 | static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6"; |
| 1132 | static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e"; |
Lei Zhang | 0c03d63 | 2020-07-30 17:05:36 +0000 | [diff] [blame] | 1133 | #elif defined(OS_APPLE) |
Hui Yingst | 4a21df0 | 2020-05-13 03:15:44 +0000 | [diff] [blame] | 1134 | static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e"; |
| 1135 | static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f"; |
| 1136 | static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1137 | #else |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1138 | static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517"; |
| 1139 | static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6"; |
| 1140 | static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1141 | #endif |
Hui Yingst | 4a21df0 | 2020-05-13 03:15:44 +0000 | [diff] [blame] | 1142 | #endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1143 | |
| 1144 | // Open a file with two annotations and load its first page. |
| 1145 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1146 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1147 | ASSERT_TRUE(page); |
| 1148 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 1149 | |
| 1150 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1151 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1152 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Hui Yingst | b4baceb | 2020-04-28 23:46:10 +0000 | [diff] [blame] | 1153 | CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1154 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1155 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1156 | { |
| 1157 | // Retrieve the stamp annotation which has its AP stream already defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1158 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1159 | ASSERT_TRUE(annot); |
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 this annotation has one path object and retrieve it. |
| 1162 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 1163 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1164 | FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1); |
| 1165 | EXPECT_FALSE(path); |
| 1166 | path = FPDFAnnot_GetObject(annot.get(), 0); |
| 1167 | EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path)); |
| 1168 | EXPECT_TRUE(path); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1169 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1170 | // Modify the color of the path object. |
Lei Zhang | 3475b48 | 2019-05-13 18:30:57 +0000 | [diff] [blame] | 1171 | EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1172 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1173 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1174 | // Check that the page with the modified annotation renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1175 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1176 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1177 | CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1178 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 1179 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1180 | // Add a second path object to the same annotation. |
| 1181 | FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84); |
| 1182 | EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88)); |
Lei Zhang | 3475b48 | 2019-05-13 18:30:57 +0000 | [diff] [blame] | 1183 | EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100)); |
| 1184 | EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1185 | EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1)); |
| 1186 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot)); |
| 1187 | EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get())); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 1188 | |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 1189 | // The object is in the annontation, not in the page, so the page object |
| 1190 | // array should not change. |
| 1191 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
| 1192 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1193 | // Check that the page with an annotation with two paths renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1194 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1195 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1196 | CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1197 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 1198 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1199 | // Delete the newly added path object. |
| 1200 | EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1)); |
| 1201 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 1202 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1203 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 1204 | |
| 1205 | // Check that the page renders the same as before. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1206 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1207 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1208 | CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1209 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1210 | |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1211 | FS_RECTF rect; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1212 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1213 | { |
| 1214 | // Create another stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1215 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1216 | ASSERT_TRUE(annot); |
| 1217 | rect.left = 200.f; |
| 1218 | rect.bottom = 400.f; |
| 1219 | rect.right = 500.f; |
| 1220 | rect.top = 600.f; |
| 1221 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1222 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1223 | // Add a new path to the annotation. |
| 1224 | FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500); |
| 1225 | EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400)); |
| 1226 | EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600)); |
| 1227 | EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550)); |
| 1228 | EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450)); |
Lei Zhang | 3475b48 | 2019-05-13 18:30:57 +0000 | [diff] [blame] | 1229 | EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180)); |
| 1230 | EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1231 | EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1)); |
| 1232 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check)); |
| 1233 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 1234 | |
| 1235 | // Check that the annotation's bounding box came from its rectangle. |
| 1236 | FS_RECTF new_rect; |
| 1237 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 1238 | EXPECT_EQ(rect.left, new_rect.left); |
| 1239 | EXPECT_EQ(rect.bottom, new_rect.bottom); |
| 1240 | EXPECT_EQ(rect.right, new_rect.right); |
| 1241 | EXPECT_EQ(rect.top, new_rect.top); |
| 1242 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1243 | |
Lei Zhang | ec61814 | 2021-04-13 17:36:46 +0000 | [diff] [blame] | 1244 | // Save the document and close the page. |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1245 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1246 | UnloadPage(page); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1247 | |
| 1248 | // Open the saved document. |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1249 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1250 | page = LoadSavedPage(0); |
Lei Zhang | ec61814 | 2021-04-13 17:36:46 +0000 | [diff] [blame] | 1251 | ASSERT_TRUE(page); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1252 | VerifySavedRendering(page, 595, 842, kMd5NewAnnot); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1253 | |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1254 | // Check that the document has a correct count of annotations and objects. |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1255 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1256 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1257 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1258 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1259 | ASSERT_TRUE(annot); |
| 1260 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 1261 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1262 | // Check that the new annotation's rectangle is as defined. |
| 1263 | FS_RECTF new_rect; |
| 1264 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 1265 | EXPECT_EQ(rect.left, new_rect.left); |
| 1266 | EXPECT_EQ(rect.bottom, new_rect.bottom); |
| 1267 | EXPECT_EQ(rect.right, new_rect.right); |
| 1268 | EXPECT_EQ(rect.top, new_rect.top); |
| 1269 | } |
| 1270 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1271 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1272 | CloseSavedDocument(); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 1273 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1274 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1275 | TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) { |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1276 | // Open a file with an annotation and load its first page. |
| 1277 | ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1278 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1279 | ASSERT_TRUE(page); |
| 1280 | |
| 1281 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1282 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1283 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1284 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 1285 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1286 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1287 | { |
| 1288 | // Retrieve the annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1289 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1290 | ASSERT_TRUE(annot); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1291 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1292 | // Check that the original flag values are as expected. |
| 1293 | int flags = FPDFAnnot_GetFlags(annot.get()); |
| 1294 | EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 1295 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1296 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1297 | // Set the HIDDEN flag. |
| 1298 | flags |= FPDF_ANNOT_FLAG_HIDDEN; |
| 1299 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags)); |
| 1300 | flags = FPDFAnnot_GetFlags(annot.get()); |
| 1301 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 1302 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
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 | // Check that the page renders correctly without rendering the annotation. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1305 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1306 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Hui Yingst | b4baceb | 2020-04-28 23:46:10 +0000 | [diff] [blame] | 1307 | CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1308 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1309 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1310 | // Unset the HIDDEN flag. |
| 1311 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE)); |
| 1312 | EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get())); |
| 1313 | flags &= ~FPDF_ANNOT_FLAG_HIDDEN; |
| 1314 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags)); |
| 1315 | flags = FPDFAnnot_GetFlags(annot.get()); |
| 1316 | EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 1317 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1318 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1319 | // Check that the page renders correctly as before. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1320 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1321 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1322 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 1323 | } |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1324 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1325 | |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 1326 | UnloadPage(page); |
| 1327 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1328 | |
Hui Yingst | dd9a3f6 | 2020-06-16 18:14:21 +0000 | [diff] [blame] | 1329 | // TODO(crbug.com/pdfium/1541): Fix this test and enable. |
| 1330 | #if defined(_SKIA_SUPPORT_) |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1331 | #define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage |
| 1332 | #else |
| 1333 | #define MAYBE_AddAndModifyImage AddAndModifyImage |
| 1334 | #endif |
| 1335 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) { |
Hui Yingst | dd9a3f6 | 2020-06-16 18:14:21 +0000 | [diff] [blame] | 1336 | #if defined(_SKIA_SUPPORT_PATHS_) |
Lei Zhang | fe8e758 | 2020-08-05 19:23:16 +0000 | [diff] [blame] | 1337 | #if defined(OS_LINUX) || defined(OS_CHROMEOS) |
Hui Yingst | dd9a3f6 | 2020-06-16 18:14:21 +0000 | [diff] [blame] | 1338 | static const char kMd5NewImage[] = "26a8eb30937226a677839379e0d7ae1a"; |
| 1339 | static const char kMd5ModifiedImage[] = "2985114b32ba1a96be78ee643fe31aa5"; |
| 1340 | #else |
| 1341 | static const char kMd5NewImage[] = "14012ab500b4671fa73dd760129a8a93"; |
| 1342 | static const char kMd5ModifiedImage[] = "5f97f98f58ed04dc393f31460485f1a2"; |
Lei Zhang | fe8e758 | 2020-08-05 19:23:16 +0000 | [diff] [blame] | 1343 | #endif // defined(OS_LINUX) || defined(OS_CHROMEOS) |
Hui Yingst | dd9a3f6 | 2020-06-16 18:14:21 +0000 | [diff] [blame] | 1344 | #else |
Lei Zhang | 0c03d63 | 2020-07-30 17:05:36 +0000 | [diff] [blame] | 1345 | #if defined(OS_APPLE) |
Lei Zhang | 0f6b434 | 2020-02-25 20:00:39 +0000 | [diff] [blame] | 1346 | static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9"; |
| 1347 | static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99"; |
Lei Zhang | e67bcc7 | 2019-04-30 18:55:58 +0000 | [diff] [blame] | 1348 | #elif defined(OS_WIN) |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1349 | static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b"; |
| 1350 | static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1351 | #else |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1352 | static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935"; |
| 1353 | static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1354 | #endif |
Hui Yingst | dd9a3f6 | 2020-06-16 18:14:21 +0000 | [diff] [blame] | 1355 | #endif // defined(_SKIA_SUPPORT_PATHS_) |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1356 | |
| 1357 | // Open a file with two annotations and load its first page. |
| 1358 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1359 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1360 | ASSERT_TRUE(page); |
| 1361 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 1362 | |
| 1363 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1364 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1365 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Hui Yingst | b4baceb | 2020-04-28 23:46:10 +0000 | [diff] [blame] | 1366 | CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1367 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1368 | |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1369 | constexpr int kBitmapSize = 200; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1370 | FPDF_BITMAP image_bitmap; |
| 1371 | |
| 1372 | { |
| 1373 | // Create a stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1374 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1375 | ASSERT_TRUE(annot); |
| 1376 | FS_RECTF rect; |
| 1377 | rect.left = 200.f; |
| 1378 | rect.bottom = 600.f; |
| 1379 | rect.right = 400.f; |
| 1380 | rect.top = 800.f; |
| 1381 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 1382 | |
| 1383 | // Add a solid-color translucent image object to the new annotation. |
| 1384 | image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1); |
| 1385 | FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, |
| 1386 | 0xeeeecccc); |
| 1387 | EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap)); |
| 1388 | EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap)); |
| 1389 | FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document()); |
| 1390 | ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap)); |
| 1391 | ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0, |
| 1392 | kBitmapSize, 0, 0)); |
| 1393 | FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600); |
| 1394 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object)); |
| 1395 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1396 | |
| 1397 | // Check that the page renders correctly with the new image object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1398 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1399 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1400 | CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1401 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1402 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1403 | { |
| 1404 | // Retrieve the newly added stamp annotation and its image object. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1405 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1406 | ASSERT_TRUE(annot); |
| 1407 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 1408 | FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0); |
| 1409 | EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1410 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1411 | // Modify the image in the new annotation. |
| 1412 | FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, |
| 1413 | 0xff000000); |
| 1414 | ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap)); |
| 1415 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object)); |
| 1416 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1417 | |
Lei Zhang | ec61814 | 2021-04-13 17:36:46 +0000 | [diff] [blame] | 1418 | // Save the document and close the page. |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1419 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1420 | UnloadPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1421 | FPDFBitmap_Destroy(image_bitmap); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1422 | |
| 1423 | // Test that the saved document renders the modified image object correctly. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1424 | VerifySavedDocument(595, 842, kMd5ModifiedImage); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1425 | } |
| 1426 | |
Hui Yingst | 6cd754f | 2020-05-14 04:05:25 +0000 | [diff] [blame] | 1427 | TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) { |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1428 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Lei Zhang | fe8e758 | 2020-08-05 19:23:16 +0000 | [diff] [blame] | 1429 | #if defined(OS_LINUX) || defined(OS_CHROMEOS) |
Hui Yingst | 6cd754f | 2020-05-14 04:05:25 +0000 | [diff] [blame] | 1430 | static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a"; |
| 1431 | static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c"; |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1432 | #else |
Hui Yingst | 6cd754f | 2020-05-14 04:05:25 +0000 | [diff] [blame] | 1433 | static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48"; |
| 1434 | static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc"; |
Lei Zhang | fe8e758 | 2020-08-05 19:23:16 +0000 | [diff] [blame] | 1435 | #endif // defined(OS_LINUX) || defined(OS_CHROMEOS) |
Hui Yingst | 6cd754f | 2020-05-14 04:05:25 +0000 | [diff] [blame] | 1436 | #else |
| 1437 | #if defined(OS_WIN) |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1438 | static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6"; |
| 1439 | static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d"; |
Lei Zhang | 0c03d63 | 2020-07-30 17:05:36 +0000 | [diff] [blame] | 1440 | #elif defined(OS_APPLE) |
Hui Yingst | 6cd754f | 2020-05-14 04:05:25 +0000 | [diff] [blame] | 1441 | static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da"; |
| 1442 | static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1443 | #else |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1444 | static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306"; |
| 1445 | static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1446 | #endif |
Hui Yingst | 6cd754f | 2020-05-14 04:05:25 +0000 | [diff] [blame] | 1447 | #endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1448 | |
| 1449 | // Open a file with two annotations and load its first page. |
| 1450 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1451 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1452 | ASSERT_TRUE(page); |
| 1453 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 1454 | |
| 1455 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1456 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1457 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Hui Yingst | b4baceb | 2020-04-28 23:46:10 +0000 | [diff] [blame] | 1458 | CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1459 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1460 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1461 | { |
| 1462 | // Create a stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1463 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1464 | ASSERT_TRUE(annot); |
| 1465 | FS_RECTF rect; |
| 1466 | rect.left = 200.f; |
| 1467 | rect.bottom = 550.f; |
| 1468 | rect.right = 450.f; |
| 1469 | rect.top = 650.f; |
| 1470 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1471 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1472 | // Add a translucent text object to the new annotation. |
| 1473 | FPDF_PAGEOBJECT text_object = |
| 1474 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 1475 | EXPECT_TRUE(text_object); |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1476 | ScopedFPDFWideString text = |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1477 | GetFPDFWideString(L"I'm a translucent text laying on other text."); |
| 1478 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
Lei Zhang | 3475b48 | 2019-05-13 18:30:57 +0000 | [diff] [blame] | 1479 | EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1480 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600); |
| 1481 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object)); |
| 1482 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1483 | |
| 1484 | // Check that the page renders correctly with the new text object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1485 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1486 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1487 | CompareBitmap(bitmap.get(), 595, 842, kMd5NewText); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1488 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1489 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1490 | { |
| 1491 | // Retrieve the newly added stamp annotation and its text object. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1492 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1493 | ASSERT_TRUE(annot); |
| 1494 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 1495 | FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0); |
| 1496 | EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1497 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1498 | // Modify the text in the new annotation. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1499 | ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!"); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1500 | EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get())); |
| 1501 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object)); |
| 1502 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1503 | |
| 1504 | // Check that the page renders correctly with the modified text object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1505 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1506 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1507 | CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1508 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1509 | |
| 1510 | // Remove the new annotation, and check that the page renders as before. |
| 1511 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2)); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1512 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1513 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Hui Yingst | b4baceb | 2020-04-28 23:46:10 +0000 | [diff] [blame] | 1514 | CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1515 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1516 | |
| 1517 | UnloadPage(page); |
| 1518 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1519 | |
Hui Yingst | 9b6b154 | 2020-07-27 16:11:12 +0000 | [diff] [blame] | 1520 | TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) { |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1521 | // Open a file with four annotations and load its first page. |
| 1522 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1523 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1524 | ASSERT_TRUE(page); |
| 1525 | |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1526 | static const wchar_t kNewDate[] = L"D:201706282359Z00'00'"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1527 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1528 | { |
| 1529 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1530 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1531 | ASSERT_TRUE(annot); |
| 1532 | |
| 1533 | // Check that a non-existent key does not exist. |
| 1534 | EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none")); |
| 1535 | |
| 1536 | // 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] | 1537 | EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1538 | EXPECT_EQ(FPDF_OBJECT_REFERENCE, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1539 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP)); |
| 1540 | EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP, |
| 1541 | nullptr, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1542 | |
| 1543 | // Check that the string value of the hash is correct. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1544 | static const char kHashKey[] = "AAPL:Hash"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1545 | EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1546 | unsigned long length_bytes = |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1547 | FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1548 | ASSERT_EQ(66u, length_bytes); |
| 1549 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 1550 | EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), |
| 1551 | length_bytes)); |
| 1552 | EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad", |
| 1553 | GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1554 | |
| 1555 | // Check that the string value of the modified date is correct. |
| 1556 | EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1557 | length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, |
| 1558 | nullptr, 0); |
| 1559 | ASSERT_EQ(44u, length_bytes); |
| 1560 | buf = GetFPDFWideStringBuffer(length_bytes); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1561 | EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1562 | buf.data(), length_bytes)); |
| 1563 | EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1564 | |
| 1565 | // Update the date entry for the annotation. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1566 | ScopedFPDFWideString text = GetFPDFWideString(kNewDate); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1567 | EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM, |
| 1568 | text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1569 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1570 | |
Lei Zhang | ec61814 | 2021-04-13 17:36:46 +0000 | [diff] [blame] | 1571 | // Save the document and close the page. |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1572 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1573 | UnloadPage(page); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1574 | |
Hui Yingst | 9b6b154 | 2020-07-27 16:11:12 +0000 | [diff] [blame] | 1575 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Lei Zhang | fe8e758 | 2020-08-05 19:23:16 +0000 | [diff] [blame] | 1576 | #if defined(OS_LINUX) || defined(OS_CHROMEOS) |
Hui Yingst | 9b6b154 | 2020-07-27 16:11:12 +0000 | [diff] [blame] | 1577 | static const char kMd5[] = "7a2b712ca88d7b71f125ea3f9c88e57a"; |
| 1578 | #else |
| 1579 | static const char kMd5[] = "626d25c5aa5baf67d22d9a0e1c23f6aa"; |
Lei Zhang | fe8e758 | 2020-08-05 19:23:16 +0000 | [diff] [blame] | 1580 | #endif // defined(OS_LINUX) || defined(OS_CHROMEOS) |
Hui Yingst | 9b6b154 | 2020-07-27 16:11:12 +0000 | [diff] [blame] | 1581 | #else |
Lei Zhang | 0c03d63 | 2020-07-30 17:05:36 +0000 | [diff] [blame] | 1582 | #if defined(OS_APPLE) |
Lei Zhang | 0f6b434 | 2020-02-25 20:00:39 +0000 | [diff] [blame] | 1583 | static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb"; |
Lei Zhang | e67bcc7 | 2019-04-30 18:55:58 +0000 | [diff] [blame] | 1584 | #elif defined(OS_WIN) |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1585 | static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1586 | #else |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1587 | static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1588 | #endif |
Hui Yingst | 9b6b154 | 2020-07-27 16:11:12 +0000 | [diff] [blame] | 1589 | #endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 1590 | |
| 1591 | // Open the saved annotation. |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1592 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1593 | page = LoadSavedPage(0); |
Lei Zhang | ec61814 | 2021-04-13 17:36:46 +0000 | [diff] [blame] | 1594 | ASSERT_TRUE(page); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1595 | VerifySavedRendering(page, 595, 842, kMd5); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1596 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1597 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0)); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1598 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1599 | // Check that the string value of the modified date is the newly-set value. |
| 1600 | EXPECT_EQ(FPDF_OBJECT_STRING, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1601 | FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1602 | unsigned long length_bytes = FPDFAnnot_GetStringValue( |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1603 | new_annot.get(), pdfium::annotation::kM, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1604 | ASSERT_EQ(44u, length_bytes); |
| 1605 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1606 | EXPECT_EQ(44u, |
| 1607 | FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1608 | buf.data(), length_bytes)); |
| 1609 | EXPECT_EQ(kNewDate, GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1610 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1611 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1612 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1613 | CloseSavedDocument(); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1614 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1615 | |
rycsmith | 3e78560 | 2019-03-05 21:48:36 +0000 | [diff] [blame] | 1616 | TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) { |
Mansi Awasthi | 0b5da67 | 2020-01-23 18:17:18 +0000 | [diff] [blame] | 1617 | // Open a file with four text annotations and load its first page. |
rycsmith | 3e78560 | 2019-03-05 21:48:36 +0000 | [diff] [blame] | 1618 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 1619 | FPDF_PAGE page = LoadPage(0); |
| 1620 | ASSERT_TRUE(page); |
| 1621 | { |
| 1622 | // First two annotations do not have "MaxLen" attribute. |
| 1623 | for (int i = 0; i < 2; i++) { |
| 1624 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i)); |
| 1625 | ASSERT_TRUE(annot); |
| 1626 | |
| 1627 | // Verify that no "MaxLen" key present. |
| 1628 | EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen")); |
| 1629 | |
| 1630 | float value; |
| 1631 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value)); |
| 1632 | } |
| 1633 | |
| 1634 | // Annotation in index 2 has "MaxLen" of 10. |
| 1635 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
| 1636 | ASSERT_TRUE(annot); |
| 1637 | |
| 1638 | // Verify that "MaxLen" key present. |
| 1639 | EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen")); |
| 1640 | |
| 1641 | float value; |
| 1642 | EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value)); |
| 1643 | EXPECT_FLOAT_EQ(10.0f, value); |
| 1644 | |
| 1645 | // Check bad inputs. |
| 1646 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value)); |
| 1647 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value)); |
| 1648 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr)); |
| 1649 | // Ask for key that exists but is not a number. |
| 1650 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value)); |
| 1651 | } |
| 1652 | |
| 1653 | UnloadPage(page); |
| 1654 | } |
| 1655 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1656 | TEST_F(FPDFAnnotEmbedderTest, GetSetAP) { |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1657 | // Open a file with four annotations and load its first page. |
| 1658 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1659 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1660 | ASSERT_TRUE(page); |
| 1661 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1662 | { |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1663 | static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a"; |
| 1664 | static constexpr size_t kExpectNormalAPLength = 73970; |
| 1665 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1666 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1667 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1668 | ASSERT_TRUE(annot); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1669 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1670 | // Check that the string value of an AP returns the expected length. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1671 | unsigned long normal_length_bytes = FPDFAnnot_GetAP( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1672 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1673 | ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1674 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1675 | // Check that the string value of an AP is not returned if the buffer is too |
| 1676 | // small. The result buffer should be overwritten with an empty string. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1677 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes); |
| 1678 | // Write in the buffer to verify it's not overwritten. |
| 1679 | memcpy(buf.data(), "abcdefgh", 8); |
| 1680 | EXPECT_EQ(kExpectNormalAPLength, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1681 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1682 | buf.data(), normal_length_bytes - 1)); |
| 1683 | EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8)); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1684 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1685 | // Check that the string value of an AP is returned through a buffer that is |
| 1686 | // the right size. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1687 | EXPECT_EQ(kExpectNormalAPLength, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1688 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1689 | buf.data(), normal_length_bytes)); |
| 1690 | EXPECT_EQ(kMd5NormalAP, |
| 1691 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), |
| 1692 | normal_length_bytes)); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1693 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1694 | // Check that the string value of an AP is returned through a buffer that is |
| 1695 | // larger than necessary. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1696 | buf = GetFPDFWideStringBuffer(normal_length_bytes + 2); |
| 1697 | EXPECT_EQ(kExpectNormalAPLength, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1698 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1699 | buf.data(), normal_length_bytes + 2)); |
| 1700 | EXPECT_EQ(kMd5NormalAP, |
| 1701 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), |
| 1702 | normal_length_bytes)); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1703 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1704 | // Check that getting an AP for a mode that does not have an AP returns an |
| 1705 | // empty string. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1706 | unsigned long rollover_length_bytes = FPDFAnnot_GetAP( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1707 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1708 | ASSERT_EQ(2u, rollover_length_bytes); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1709 | |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1710 | buf = GetFPDFWideStringBuffer(1000); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1711 | EXPECT_EQ(2u, |
| 1712 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1713 | buf.data(), 1000)); |
| 1714 | EXPECT_EQ(L"", GetPlatformWString(buf.data())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1715 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1716 | // Check that setting the AP for an invalid appearance mode fails. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1717 | ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap"); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1718 | EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1719 | EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1720 | ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1721 | EXPECT_FALSE(FPDFAnnot_SetAP( |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1722 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1723 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1724 | // Set the AP correctly now. |
| 1725 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1726 | ap_text.get())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1727 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1728 | // 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] | 1729 | rollover_length_bytes = FPDFAnnot_GetAP( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1730 | 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 | buf = GetFPDFWideStringBuffer(rollover_length_bytes); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1733 | EXPECT_EQ(24u, |
| 1734 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1735 | buf.data(), rollover_length_bytes)); |
| 1736 | EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1737 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1738 | // 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] | 1739 | buf = GetFPDFWideStringBuffer(normal_length_bytes); |
| 1740 | EXPECT_EQ(kExpectNormalAPLength, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1741 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1742 | buf.data(), normal_length_bytes)); |
| 1743 | EXPECT_EQ(kMd5NormalAP, |
| 1744 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), |
| 1745 | normal_length_bytes)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1746 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1747 | |
| 1748 | // Save the modified document, then reopen it. |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1749 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1750 | UnloadPage(page); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1751 | |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1752 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1753 | page = LoadSavedPage(0); |
Lei Zhang | ec61814 | 2021-04-13 17:36:46 +0000 | [diff] [blame] | 1754 | ASSERT_TRUE(page); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1755 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1756 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1757 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1758 | // Check that the new annotation value is equal to the value we set before |
| 1759 | // saving. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1760 | unsigned long rollover_length_bytes = FPDFAnnot_GetAP( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1761 | new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1762 | ASSERT_EQ(24u, rollover_length_bytes); |
| 1763 | std::vector<FPDF_WCHAR> buf = |
| 1764 | GetFPDFWideStringBuffer(rollover_length_bytes); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1765 | EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(), |
| 1766 | FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1767 | buf.data(), rollover_length_bytes)); |
| 1768 | EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1769 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1770 | |
| 1771 | // Close saved document. |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1772 | CloseSavedPage(page); |
| 1773 | CloseSavedDocument(); |
| 1774 | } |
| 1775 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1776 | TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) { |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1777 | // Open a file with four annotations and load its first page. |
| 1778 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1779 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1780 | ASSERT_TRUE(page); |
| 1781 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1782 | { |
| 1783 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1784 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1785 | ASSERT_TRUE(annot); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1786 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1787 | // Set Down AP. Normal AP is already set. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1788 | ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap"); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1789 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1790 | ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1791 | EXPECT_EQ(73970u, |
| 1792 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1793 | nullptr, 0)); |
| 1794 | EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1795 | nullptr, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1796 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1797 | // Check that setting the Down AP to null removes the Down entry but keeps |
| 1798 | // Normal intact. |
| 1799 | EXPECT_TRUE( |
| 1800 | FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr)); |
| 1801 | EXPECT_EQ(73970u, |
| 1802 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1803 | nullptr, 0)); |
| 1804 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1805 | nullptr, 0)); |
| 1806 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1807 | |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1808 | UnloadPage(page); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1811 | TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) { |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1812 | // Open a file with four annotations and load its first page. |
| 1813 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1814 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1815 | ASSERT_TRUE(page); |
| 1816 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1817 | { |
| 1818 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1819 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1820 | ASSERT_TRUE(annot); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1821 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1822 | // Set Down AP. Normal AP is already set. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1823 | ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap"); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1824 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1825 | ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1826 | EXPECT_EQ(73970u, |
| 1827 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1828 | nullptr, 0)); |
| 1829 | EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1830 | nullptr, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1831 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1832 | // Check that setting the Normal AP to null removes the whole AP dictionary. |
| 1833 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1834 | nullptr)); |
| 1835 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1836 | nullptr, 0)); |
| 1837 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1838 | nullptr, 0)); |
| 1839 | } |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1840 | |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1841 | UnloadPage(page); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1844 | TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) { |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1845 | // Open a file with annotations and load its first page. |
| 1846 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1847 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1848 | ASSERT_TRUE(page); |
Jane Liu | d1ed1ce | 2017-08-24 12:31:10 -0400 | [diff] [blame] | 1849 | EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1850 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1851 | { |
| 1852 | // Retrieve the highlight annotation which has its popup defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1853 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1854 | ASSERT_TRUE(annot); |
| 1855 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 1856 | EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get())); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1857 | static const char kPopupKey[] = "Popup"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1858 | ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey)); |
| 1859 | ASSERT_EQ(FPDF_OBJECT_REFERENCE, |
| 1860 | FPDFAnnot_GetValueType(annot.get(), kPopupKey)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1861 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1862 | // Retrieve and verify the popup of the highlight annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1863 | ScopedFPDFAnnotation popup( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1864 | FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey)); |
| 1865 | ASSERT_TRUE(popup); |
| 1866 | EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get())); |
| 1867 | EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get())); |
| 1868 | FS_RECTF rect; |
| 1869 | ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect)); |
| 1870 | EXPECT_NEAR(612.0f, rect.left, 0.001f); |
| 1871 | EXPECT_NEAR(578.792, rect.bottom, 0.001f); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1872 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1873 | // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail, |
| 1874 | // since "IRT" is not a key in |annot|'s dictionary. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1875 | static const char kIRTKey[] = "IRT"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1876 | ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey)); |
| 1877 | EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1878 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1879 | // Attempting to retrieve |annot|'s parent dictionary as an annotation |
| 1880 | // would fail, since its parent is not an annotation. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1881 | ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1882 | EXPECT_EQ(FPDF_OBJECT_REFERENCE, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1883 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP)); |
| 1884 | EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1885 | } |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1886 | |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1887 | UnloadPage(page); |
| 1888 | } |
| 1889 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1890 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) { |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1891 | // Open file with form text fields. |
| 1892 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1893 | FPDF_PAGE page = LoadPage(0); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1894 | ASSERT_TRUE(page); |
| 1895 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1896 | { |
| 1897 | // Retrieve the first annotation: user-editable text field. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1898 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1899 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1900 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1901 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1902 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1903 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
Mansi Awasthi | 0b5da67 | 2020-01-23 18:17:18 +0000 | [diff] [blame] | 1904 | EXPECT_FALSE(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 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1907 | { |
| 1908 | // Retrieve the second annotation: read-only text field. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1909 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1910 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1911 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1912 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1913 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1914 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
Mansi Awasthi | 0b5da67 | 2020-01-23 18:17:18 +0000 | [diff] [blame] | 1915 | EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD); |
| 1916 | } |
| 1917 | |
| 1918 | { |
| 1919 | // Retrieve the fourth annotation: user-editable password text field. |
| 1920 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3)); |
| 1921 | ASSERT_TRUE(annot); |
| 1922 | |
| 1923 | // Check that the flag values are as expected. |
| 1924 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
| 1925 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1926 | EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1927 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1928 | |
| 1929 | UnloadPage(page); |
| 1930 | } |
| 1931 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1932 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) { |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1933 | // Open file with form text fields. |
| 1934 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1935 | FPDF_PAGE page = LoadPage(0); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1936 | ASSERT_TRUE(page); |
| 1937 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1938 | { |
| 1939 | // Retrieve the first annotation: user-editable combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1940 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1941 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1942 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1943 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1944 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1945 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1946 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1947 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1948 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1949 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1950 | { |
| 1951 | // Retrieve the second annotation: regular combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1952 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1953 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1954 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1955 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1956 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1957 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1958 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1959 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1960 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1961 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1962 | { |
| 1963 | // Retrieve the third annotation: read-only combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1964 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1965 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1966 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1967 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1968 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1969 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1970 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1971 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1972 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1973 | |
| 1974 | UnloadPage(page); |
| 1975 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1976 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1977 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1978 | // Open file with form text fields. |
Daniel Hosseinian | 5af51b6 | 2020-07-18 00:53:43 +0000 | [diff] [blame] | 1979 | ASSERT_TRUE(OpenDocument("text_form.pdf")); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1980 | FPDF_PAGE page = LoadPage(0); |
| 1981 | ASSERT_TRUE(page); |
| 1982 | |
| 1983 | // Attempt to get an annotation where no annotation exists on page. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1984 | static const FS_POINTF kOriginPoint = {0.0f, 0.0f}; |
| 1985 | EXPECT_FALSE( |
| 1986 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint)); |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 1987 | |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1988 | static const FS_POINTF kValidPoint = {120.0f, 120.0f}; |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 1989 | { |
| 1990 | // Verify there is an annotation. |
| 1991 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1992 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint)); |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 1993 | EXPECT_TRUE(annot); |
| 1994 | } |
| 1995 | |
| 1996 | // Try other bad inputs at a valid location. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1997 | EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint)); |
| 1998 | EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint)); |
| 1999 | EXPECT_FALSE( |
| 2000 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint)); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 2001 | |
| 2002 | UnloadPage(page); |
| 2003 | } |
| 2004 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 2005 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 2006 | // Open file with form text fields. |
Daniel Hosseinian | 5af51b6 | 2020-07-18 00:53:43 +0000 | [diff] [blame] | 2007 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 2008 | FPDF_PAGE page = LoadPage(0); |
| 2009 | ASSERT_TRUE(page); |
| 2010 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2011 | { |
| 2012 | // Retrieve user-editable text field annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 2013 | static const FS_POINTF kPoint = {105.0f, 118.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 2014 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 2015 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2016 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 2017 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2018 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 2019 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2020 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 2021 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 2022 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2023 | { |
| 2024 | // Retrieve read-only text field annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 2025 | static const FS_POINTF kPoint = {105.0f, 202.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 2026 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 2027 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2028 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 2029 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2030 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 2031 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2032 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 2033 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 2034 | |
| 2035 | UnloadPage(page); |
| 2036 | } |
| 2037 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 2038 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 2039 | // Open file with form comboboxes. |
Daniel Hosseinian | 5af51b6 | 2020-07-18 00:53:43 +0000 | [diff] [blame] | 2040 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 2041 | FPDF_PAGE page = LoadPage(0); |
| 2042 | ASSERT_TRUE(page); |
| 2043 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2044 | { |
| 2045 | // Retrieve user-editable combobox annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 2046 | static const FS_POINTF kPoint = {102.0f, 363.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 2047 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 2048 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2049 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 2050 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2051 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 2052 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2053 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 2054 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 2055 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 2056 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 2057 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2058 | { |
| 2059 | // Retrieve regular combobox annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 2060 | static const FS_POINTF kPoint = {102.0f, 413.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 2061 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 2062 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2063 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 2064 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2065 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 2066 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2067 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 2068 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 2069 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 2070 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 2071 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2072 | { |
| 2073 | // Retrieve read-only combobox annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 2074 | static const FS_POINTF kPoint = {102.0f, 513.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 2075 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 2076 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2077 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 2078 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2079 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 2080 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2081 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 2082 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 2083 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 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 |
| 2095 | static constexpr size_t kExpectedSize = 1609; |
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_) |
| 3015 | static const char kMd5sum[] = "4ca14c670396711194b40ecc2514969b"; |
| 3016 | #else |
Lei Zhang | 671aece | 2020-04-14 19:02:26 +0000 | [diff] [blame] | 3017 | #if defined(OS_WIN) |
| 3018 | static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30"; |
Lei Zhang | 0c03d63 | 2020-07-30 17:05:36 +0000 | [diff] [blame] | 3019 | #elif defined(OS_APPLE) |
Lei Zhang | 671aece | 2020-04-14 19:02:26 +0000 | [diff] [blame] | 3020 | static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867"; |
| 3021 | #else |
| 3022 | static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654"; |
| 3023 | #endif |
Hui Yingst | ea3816d | 2020-07-28 22:35:11 +0000 | [diff] [blame] | 3024 | #endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Lei Zhang | 671aece | 2020-04-14 19:02:26 +0000 | [diff] [blame] | 3025 | // Check the initial rendering. |
| 3026 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 3027 | CompareBitmap(bitmap.get(), 612, 792, kMd5sum); |
| 3028 | } |
| 3029 | |
| 3030 | // Make links and highlights focusable. |
| 3031 | static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK, |
| 3032 | FPDF_ANNOT_HIGHLIGHT}; |
Lei Zhang | 4501a50 | 2020-05-18 16:52:59 +0000 | [diff] [blame] | 3033 | constexpr int kSubTypesCount = pdfium::size(kSubTypes); |
Lei Zhang | 671aece | 2020-04-14 19:02:26 +0000 | [diff] [blame] | 3034 | ASSERT_TRUE( |
| 3035 | FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount)); |
| 3036 | ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle())); |
| 3037 | std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount); |
| 3038 | ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), |
| 3039 | subtypes.size())); |
| 3040 | ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]); |
| 3041 | ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]); |
| 3042 | |
| 3043 | { |
Hui Yingst | ea3816d | 2020-07-28 22:35:11 +0000 | [diff] [blame] | 3044 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 3045 | static const char kMd5sum[] = "48cb60b3f9bc364c73582aff3418451e"; |
| 3046 | #else |
Lei Zhang | 671aece | 2020-04-14 19:02:26 +0000 | [diff] [blame] | 3047 | #if defined(OS_WIN) |
| 3048 | static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f"; |
Lei Zhang | 0c03d63 | 2020-07-30 17:05:36 +0000 | [diff] [blame] | 3049 | #elif defined(OS_APPLE) |
Lei Zhang | 671aece | 2020-04-14 19:02:26 +0000 | [diff] [blame] | 3050 | static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346"; |
| 3051 | #else |
| 3052 | static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee"; |
| 3053 | #endif |
Hui Yingst | ea3816d | 2020-07-28 22:35:11 +0000 | [diff] [blame] | 3054 | #endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Lei Zhang | 671aece | 2020-04-14 19:02:26 +0000 | [diff] [blame] | 3055 | // Focus the first link and check the rendering. |
| 3056 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3057 | ASSERT_TRUE(annot); |
| 3058 | EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get())); |
| 3059 | EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get())); |
| 3060 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 3061 | CompareBitmap(bitmap.get(), 612, 792, kMd5sum); |
| 3062 | } |
| 3063 | |
| 3064 | { |
Hui Yingst | ea3816d | 2020-07-28 22:35:11 +0000 | [diff] [blame] | 3065 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 3066 | static const char kMd5sum[] = "d09869ff0a209daf179da2d7a58142b1"; |
| 3067 | #else |
Lei Zhang | 671aece | 2020-04-14 19:02:26 +0000 | [diff] [blame] | 3068 | #if defined(OS_WIN) |
| 3069 | static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10"; |
Lei Zhang | 0c03d63 | 2020-07-30 17:05:36 +0000 | [diff] [blame] | 3070 | #elif defined(OS_APPLE) |
Lei Zhang | 671aece | 2020-04-14 19:02:26 +0000 | [diff] [blame] | 3071 | static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e"; |
| 3072 | #else |
| 3073 | static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110"; |
| 3074 | #endif |
Hui Yingst | ea3816d | 2020-07-28 22:35:11 +0000 | [diff] [blame] | 3075 | #endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
Lei Zhang | 671aece | 2020-04-14 19:02:26 +0000 | [diff] [blame] | 3076 | // Focus the first highlight and check the rendering. |
| 3077 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4)); |
| 3078 | ASSERT_TRUE(annot); |
| 3079 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 3080 | EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get())); |
| 3081 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 3082 | CompareBitmap(bitmap.get(), 612, 792, kMd5sum); |
| 3083 | } |
| 3084 | |
| 3085 | UnloadPage(page); |
| 3086 | } |
Badhri Ravikumar | cd62891 | 2020-05-07 19:23:51 +0000 | [diff] [blame] | 3087 | |
| 3088 | TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) { |
| 3089 | ASSERT_TRUE(OpenDocument("annots.pdf")); |
| 3090 | FPDF_PAGE page = LoadPage(0); |
| 3091 | ASSERT_TRUE(page); |
| 3092 | { |
Badhri Ravikumar | cd62891 | 2020-05-07 19:23:51 +0000 | [diff] [blame] | 3093 | constexpr char kExpectedResult[] = |
| 3094 | "https://cs.chromium.org/chromium/src/third_party/pdfium/public/" |
| 3095 | "fpdf_text.h"; |
Badhri Ravikumar | cd62891 | 2020-05-07 19:23:51 +0000 | [diff] [blame] | 3096 | |
Lei Zhang | 306874b | 2021-04-16 00:33:36 +0000 | [diff] [blame] | 3097 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3)); |
| 3098 | ASSERT_TRUE(annot); |
| 3099 | EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get())); |
| 3100 | VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), |
| 3101 | kExpectedResult); |
Badhri Ravikumar | cd62891 | 2020-05-07 19:23:51 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | { |
| 3105 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4)); |
| 3106 | ASSERT_TRUE(annot); |
| 3107 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 3108 | EXPECT_FALSE(FPDFAnnot_GetLink(annot.get())); |
| 3109 | } |
| 3110 | |
| 3111 | EXPECT_FALSE(FPDFAnnot_GetLink(nullptr)); |
| 3112 | |
| 3113 | UnloadPage(page); |
| 3114 | } |
Mansi Awasthi | 23bba0e | 2020-05-15 12:18:25 +0000 | [diff] [blame] | 3115 | |
| 3116 | TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) { |
| 3117 | // Open a file with radio button widget annotations and load its first page. |
| 3118 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 3119 | FPDF_PAGE page = LoadPage(0); |
| 3120 | ASSERT_TRUE(page); |
| 3121 | |
| 3122 | { |
| 3123 | // Checks for bad annot. |
| 3124 | EXPECT_EQ(-1, |
| 3125 | FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr)); |
| 3126 | |
| 3127 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3)); |
| 3128 | ASSERT_TRUE(annot); |
| 3129 | |
| 3130 | // Checks for bad form handle. |
| 3131 | EXPECT_EQ(-1, |
| 3132 | FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get())); |
| 3133 | |
| 3134 | EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get())); |
| 3135 | } |
| 3136 | |
| 3137 | UnloadPage(page); |
| 3138 | } |
| 3139 | |
| 3140 | TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) { |
| 3141 | // Open a file with checkbox widget annotations and load its first page. |
| 3142 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 3143 | FPDF_PAGE page = LoadPage(0); |
| 3144 | ASSERT_TRUE(page); |
| 3145 | |
| 3146 | { |
| 3147 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3148 | ASSERT_TRUE(annot); |
| 3149 | EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get())); |
| 3150 | } |
| 3151 | |
| 3152 | UnloadPage(page); |
| 3153 | } |
| 3154 | |
| 3155 | TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) { |
| 3156 | // Open a file with ink annotations and load its first page. |
| 3157 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 3158 | FPDF_PAGE page = LoadPage(0); |
| 3159 | ASSERT_TRUE(page); |
| 3160 | |
| 3161 | { |
| 3162 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3163 | ASSERT_TRUE(annot); |
| 3164 | EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get())); |
| 3165 | } |
| 3166 | |
| 3167 | UnloadPage(page); |
| 3168 | } |
| 3169 | |
| 3170 | TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) { |
| 3171 | // Open a file with radio button widget annotations and load its first page. |
| 3172 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 3173 | FPDF_PAGE page = LoadPage(0); |
| 3174 | ASSERT_TRUE(page); |
| 3175 | |
| 3176 | { |
| 3177 | // Checks for bad annot. |
| 3178 | EXPECT_EQ(-1, |
| 3179 | FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr)); |
| 3180 | |
| 3181 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3)); |
| 3182 | ASSERT_TRUE(annot); |
| 3183 | |
| 3184 | // Checks for bad form handle. |
| 3185 | EXPECT_EQ(-1, |
| 3186 | FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get())); |
| 3187 | |
| 3188 | EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get())); |
| 3189 | } |
| 3190 | |
| 3191 | UnloadPage(page); |
| 3192 | } |
| 3193 | |
| 3194 | TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) { |
| 3195 | // Open a file with checkbox widget annotations and load its first page. |
| 3196 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 3197 | FPDF_PAGE page = LoadPage(0); |
| 3198 | ASSERT_TRUE(page); |
| 3199 | |
| 3200 | { |
| 3201 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3202 | ASSERT_TRUE(annot); |
| 3203 | EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get())); |
| 3204 | } |
| 3205 | |
| 3206 | UnloadPage(page); |
| 3207 | } |
| 3208 | |
| 3209 | TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) { |
| 3210 | // Open a file with ink annotations and load its first page. |
| 3211 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 3212 | FPDF_PAGE page = LoadPage(0); |
| 3213 | ASSERT_TRUE(page); |
| 3214 | |
| 3215 | { |
| 3216 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3217 | ASSERT_TRUE(annot); |
| 3218 | EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get())); |
| 3219 | } |
| 3220 | |
| 3221 | UnloadPage(page); |
| 3222 | } |
| 3223 | |
| 3224 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) { |
| 3225 | // Open a file with radio button widget annotations and load its first page. |
| 3226 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 3227 | FPDF_PAGE page = LoadPage(0); |
| 3228 | ASSERT_TRUE(page); |
| 3229 | |
| 3230 | { |
| 3231 | // Checks for bad annot. |
| 3232 | EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue( |
| 3233 | form_handle(), /*annot=*/nullptr, |
| 3234 | /*buffer=*/nullptr, /*buflen=*/0)); |
| 3235 | |
| 3236 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6)); |
| 3237 | ASSERT_TRUE(annot); |
| 3238 | |
| 3239 | // Checks for bad form handle. |
| 3240 | EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue( |
| 3241 | /*hHandle=*/nullptr, annot.get(), |
| 3242 | /*buffer=*/nullptr, /*buflen=*/0)); |
| 3243 | |
| 3244 | unsigned long length_bytes = |
| 3245 | FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(), |
| 3246 | /*buffer=*/nullptr, /*buflen=*/0); |
| 3247 | ASSERT_EQ(14u, length_bytes); |
| 3248 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 3249 | EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(), |
| 3250 | buf.data(), length_bytes)); |
| 3251 | EXPECT_EQ(L"value2", GetPlatformWString(buf.data())); |
| 3252 | } |
| 3253 | |
| 3254 | UnloadPage(page); |
| 3255 | } |
| 3256 | |
| 3257 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) { |
| 3258 | // Open a file with checkbox widget annotations and load its first page. |
| 3259 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 3260 | FPDF_PAGE page = LoadPage(0); |
| 3261 | ASSERT_TRUE(page); |
| 3262 | |
| 3263 | { |
| 3264 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3265 | ASSERT_TRUE(annot); |
| 3266 | |
| 3267 | unsigned long length_bytes = |
| 3268 | FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(), |
| 3269 | /*buffer=*/nullptr, /*buflen=*/0); |
| 3270 | ASSERT_EQ(8u, length_bytes); |
| 3271 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 3272 | EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(), |
| 3273 | buf.data(), length_bytes)); |
| 3274 | EXPECT_EQ(L"Yes", GetPlatformWString(buf.data())); |
| 3275 | } |
| 3276 | |
| 3277 | UnloadPage(page); |
| 3278 | } |
| 3279 | |
| 3280 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) { |
| 3281 | // Open a file with ink annotations and load its first page. |
| 3282 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 3283 | FPDF_PAGE page = LoadPage(0); |
| 3284 | ASSERT_TRUE(page); |
| 3285 | |
| 3286 | { |
| 3287 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3288 | ASSERT_TRUE(annot); |
| 3289 | EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(), |
| 3290 | /*buffer=*/nullptr, |
| 3291 | /*buflen=*/0)); |
| 3292 | } |
| 3293 | |
| 3294 | UnloadPage(page); |
| 3295 | } |
Miklos Vajna | d1a24fb | 2020-10-26 18:07:13 +0000 | [diff] [blame] | 3296 | |
| 3297 | TEST_F(FPDFAnnotEmbedderTest, Redactannotation) { |
| 3298 | ASSERT_TRUE(OpenDocument("redact_annot.pdf")); |
| 3299 | FPDF_PAGE page = LoadPage(0); |
| 3300 | ASSERT_TRUE(page); |
| 3301 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 3302 | |
| 3303 | { |
| 3304 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3305 | ASSERT_TRUE(annot); |
| 3306 | EXPECT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(annot.get())); |
| 3307 | } |
| 3308 | |
| 3309 | UnloadPage(page); |
| 3310 | } |
Miklos Vajna | 09ecef6 | 2020-11-10 21:50:38 +0000 | [diff] [blame] | 3311 | |
| 3312 | TEST_F(FPDFAnnotEmbedderTest, PolygonAnnotation) { |
| 3313 | ASSERT_TRUE(OpenDocument("polygon_annot.pdf")); |
| 3314 | FPDF_PAGE page = LoadPage(0); |
| 3315 | ASSERT_TRUE(page); |
| 3316 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 3317 | |
| 3318 | { |
| 3319 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3320 | ASSERT_TRUE(annot); |
| 3321 | |
Miklos Vajna | 8f7b1ae | 2020-11-17 16:53:14 +0000 | [diff] [blame] | 3322 | // FPDFAnnot_GetVertices() positive testing. |
Miklos Vajna | 09ecef6 | 2020-11-10 21:50:38 +0000 | [diff] [blame] | 3323 | unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0); |
| 3324 | const size_t kExpectedSize = 3; |
| 3325 | ASSERT_EQ(kExpectedSize, size); |
| 3326 | std::vector<FS_POINTF> vertices_buffer(size); |
| 3327 | EXPECT_EQ(size, |
| 3328 | FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size)); |
| 3329 | EXPECT_FLOAT_EQ(159.0f, vertices_buffer[0].x); |
| 3330 | EXPECT_FLOAT_EQ(296.0f, vertices_buffer[0].y); |
| 3331 | EXPECT_FLOAT_EQ(350.0f, vertices_buffer[1].x); |
| 3332 | EXPECT_FLOAT_EQ(411.0f, vertices_buffer[1].y); |
| 3333 | EXPECT_FLOAT_EQ(472.0f, vertices_buffer[2].x); |
| 3334 | EXPECT_FLOAT_EQ(243.42f, vertices_buffer[2].y); |
| 3335 | |
| 3336 | // FPDFAnnot_GetVertices() negative testing. |
| 3337 | EXPECT_EQ(0U, FPDFAnnot_GetVertices(nullptr, nullptr, 0)); |
| 3338 | |
| 3339 | // vertices_buffer is not overwritten if it is too small. |
| 3340 | vertices_buffer.resize(1); |
| 3341 | vertices_buffer[0].x = 42; |
| 3342 | vertices_buffer[0].y = 43; |
| 3343 | size = FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), |
| 3344 | vertices_buffer.size()); |
| 3345 | EXPECT_EQ(kExpectedSize, size); |
| 3346 | EXPECT_FLOAT_EQ(42, vertices_buffer[0].x); |
| 3347 | EXPECT_FLOAT_EQ(43, vertices_buffer[0].y); |
| 3348 | } |
| 3349 | |
| 3350 | { |
| 3351 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
| 3352 | ASSERT_TRUE(annot); |
| 3353 | |
| 3354 | // This has an odd number of elements in the vertices array, ignore the last |
| 3355 | // element. |
| 3356 | unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0); |
| 3357 | const size_t kExpectedSize = 3; |
| 3358 | ASSERT_EQ(kExpectedSize, size); |
| 3359 | std::vector<FS_POINTF> vertices_buffer(size); |
| 3360 | EXPECT_EQ(size, |
| 3361 | FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size)); |
| 3362 | EXPECT_FLOAT_EQ(259.0f, vertices_buffer[0].x); |
| 3363 | EXPECT_FLOAT_EQ(396.0f, vertices_buffer[0].y); |
| 3364 | EXPECT_FLOAT_EQ(450.0f, vertices_buffer[1].x); |
| 3365 | EXPECT_FLOAT_EQ(511.0f, vertices_buffer[1].y); |
| 3366 | EXPECT_FLOAT_EQ(572.0f, vertices_buffer[2].x); |
| 3367 | EXPECT_FLOAT_EQ(343.0f, vertices_buffer[2].y); |
| 3368 | } |
| 3369 | |
| 3370 | { |
| 3371 | // Wrong annotation type. |
| 3372 | ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK)); |
| 3373 | EXPECT_EQ(0U, FPDFAnnot_GetVertices(ink_annot.get(), nullptr, 0)); |
| 3374 | } |
| 3375 | |
| 3376 | UnloadPage(page); |
| 3377 | } |
Miklos Vajna | 8f7b1ae | 2020-11-17 16:53:14 +0000 | [diff] [blame] | 3378 | |
| 3379 | TEST_F(FPDFAnnotEmbedderTest, InkAnnotation) { |
| 3380 | ASSERT_TRUE(OpenDocument("ink_annot.pdf")); |
| 3381 | FPDF_PAGE page = LoadPage(0); |
| 3382 | ASSERT_TRUE(page); |
| 3383 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 3384 | |
| 3385 | { |
| 3386 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3387 | ASSERT_TRUE(annot); |
| 3388 | |
| 3389 | // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() positive |
| 3390 | // testing. |
| 3391 | unsigned long size = FPDFAnnot_GetInkListCount(annot.get()); |
| 3392 | const size_t kExpectedSize = 1; |
| 3393 | ASSERT_EQ(kExpectedSize, size); |
| 3394 | const unsigned long kPathIndex = 0; |
| 3395 | unsigned long path_size = |
| 3396 | FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0); |
| 3397 | const size_t kExpectedPathSize = 3; |
| 3398 | ASSERT_EQ(kExpectedPathSize, path_size); |
| 3399 | std::vector<FS_POINTF> path_buffer(path_size); |
| 3400 | EXPECT_EQ(path_size, |
| 3401 | FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, |
| 3402 | path_buffer.data(), path_size)); |
| 3403 | EXPECT_FLOAT_EQ(159.0f, path_buffer[0].x); |
| 3404 | EXPECT_FLOAT_EQ(296.0f, path_buffer[0].y); |
| 3405 | EXPECT_FLOAT_EQ(350.0f, path_buffer[1].x); |
| 3406 | EXPECT_FLOAT_EQ(411.0f, path_buffer[1].y); |
| 3407 | EXPECT_FLOAT_EQ(472.0f, path_buffer[2].x); |
| 3408 | EXPECT_FLOAT_EQ(243.42f, path_buffer[2].y); |
| 3409 | |
| 3410 | // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() negative |
| 3411 | // testing. |
| 3412 | EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(nullptr)); |
| 3413 | EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 0, nullptr, 0)); |
| 3414 | |
| 3415 | // out of bounds path_index. |
| 3416 | EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 42, nullptr, 0)); |
| 3417 | |
| 3418 | // path_buffer is not overwritten if it is too small. |
| 3419 | path_buffer.resize(1); |
| 3420 | path_buffer[0].x = 42; |
| 3421 | path_buffer[0].y = 43; |
| 3422 | path_size = FPDFAnnot_GetInkListPath( |
| 3423 | annot.get(), kPathIndex, path_buffer.data(), path_buffer.size()); |
| 3424 | EXPECT_EQ(kExpectedSize, size); |
| 3425 | EXPECT_FLOAT_EQ(42, path_buffer[0].x); |
| 3426 | EXPECT_FLOAT_EQ(43, path_buffer[0].y); |
| 3427 | } |
| 3428 | |
| 3429 | { |
| 3430 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
| 3431 | ASSERT_TRUE(annot); |
| 3432 | |
| 3433 | // This has an odd number of elements in the path array, ignore the last |
| 3434 | // element. |
| 3435 | unsigned long size = FPDFAnnot_GetInkListCount(annot.get()); |
| 3436 | const size_t kExpectedSize = 1; |
| 3437 | ASSERT_EQ(kExpectedSize, size); |
| 3438 | const unsigned long kPathIndex = 0; |
| 3439 | unsigned long path_size = |
| 3440 | FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0); |
| 3441 | const size_t kExpectedPathSize = 3; |
| 3442 | ASSERT_EQ(kExpectedPathSize, path_size); |
| 3443 | std::vector<FS_POINTF> path_buffer(path_size); |
| 3444 | EXPECT_EQ(path_size, |
| 3445 | FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, |
| 3446 | path_buffer.data(), path_size)); |
| 3447 | EXPECT_FLOAT_EQ(259.0f, path_buffer[0].x); |
| 3448 | EXPECT_FLOAT_EQ(396.0f, path_buffer[0].y); |
| 3449 | EXPECT_FLOAT_EQ(450.0f, path_buffer[1].x); |
| 3450 | EXPECT_FLOAT_EQ(511.0f, path_buffer[1].y); |
| 3451 | EXPECT_FLOAT_EQ(572.0f, path_buffer[2].x); |
| 3452 | EXPECT_FLOAT_EQ(343.0f, path_buffer[2].y); |
| 3453 | } |
| 3454 | |
| 3455 | { |
| 3456 | // Wrong annotation type. |
| 3457 | ScopedFPDFAnnotation polygon_annot( |
| 3458 | FPDFPage_CreateAnnot(page, FPDF_ANNOT_POLYGON)); |
| 3459 | EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(polygon_annot.get())); |
| 3460 | const unsigned long kPathIndex = 0; |
| 3461 | EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(polygon_annot.get(), kPathIndex, |
| 3462 | nullptr, 0)); |
| 3463 | } |
| 3464 | |
| 3465 | UnloadPage(page); |
| 3466 | } |
Miklos Vajna | 30f45a6 | 2020-12-04 19:12:31 +0000 | [diff] [blame] | 3467 | |
| 3468 | TEST_F(FPDFAnnotEmbedderTest, LineAnnotation) { |
| 3469 | ASSERT_TRUE(OpenDocument("line_annot.pdf")); |
| 3470 | FPDF_PAGE page = LoadPage(0); |
| 3471 | ASSERT_TRUE(page); |
| 3472 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 3473 | |
| 3474 | { |
| 3475 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3476 | ASSERT_TRUE(annot); |
| 3477 | |
| 3478 | // FPDFAnnot_GetVertices() positive testing. |
| 3479 | FS_POINTF start; |
| 3480 | FS_POINTF end; |
| 3481 | ASSERT_TRUE(FPDFAnnot_GetLine(annot.get(), &start, &end)); |
| 3482 | EXPECT_FLOAT_EQ(159.0f, start.x); |
| 3483 | EXPECT_FLOAT_EQ(296.0f, start.y); |
| 3484 | EXPECT_FLOAT_EQ(472.0f, end.x); |
| 3485 | EXPECT_FLOAT_EQ(243.42f, end.y); |
| 3486 | |
| 3487 | // FPDFAnnot_GetVertices() negative testing. |
| 3488 | EXPECT_FALSE(FPDFAnnot_GetLine(nullptr, nullptr, nullptr)); |
| 3489 | EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), nullptr, nullptr)); |
| 3490 | } |
| 3491 | |
| 3492 | { |
| 3493 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
| 3494 | ASSERT_TRUE(annot); |
| 3495 | |
| 3496 | // Too few elements in the line array. |
| 3497 | FS_POINTF start; |
| 3498 | FS_POINTF end; |
| 3499 | EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), &start, &end)); |
| 3500 | } |
| 3501 | |
| 3502 | { |
| 3503 | // Wrong annotation type. |
| 3504 | ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK)); |
| 3505 | FS_POINTF start; |
| 3506 | FS_POINTF end; |
| 3507 | EXPECT_FALSE(FPDFAnnot_GetLine(ink_annot.get(), &start, &end)); |
| 3508 | } |
| 3509 | |
| 3510 | UnloadPage(page); |
| 3511 | } |
Miklos Vajna | 305d2ed | 2020-12-15 17:28:49 +0000 | [diff] [blame] | 3512 | |
| 3513 | TEST_F(FPDFAnnotEmbedderTest, AnnotationBorder) { |
| 3514 | ASSERT_TRUE(OpenDocument("line_annot.pdf")); |
| 3515 | FPDF_PAGE page = LoadPage(0); |
| 3516 | ASSERT_TRUE(page); |
| 3517 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 3518 | |
| 3519 | { |
| 3520 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 3521 | ASSERT_TRUE(annot); |
| 3522 | |
| 3523 | // FPDFAnnot_GetBorder() positive testing. |
| 3524 | float horizontal_radius; |
| 3525 | float vertical_radius; |
| 3526 | float border_width; |
| 3527 | ASSERT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius, |
| 3528 | &vertical_radius, &border_width)); |
| 3529 | EXPECT_FLOAT_EQ(0.25f, horizontal_radius); |
| 3530 | EXPECT_FLOAT_EQ(0.5f, vertical_radius); |
| 3531 | EXPECT_FLOAT_EQ(2.0f, border_width); |
| 3532 | |
| 3533 | // FPDFAnnot_GetBorder() negative testing. |
| 3534 | EXPECT_FALSE(FPDFAnnot_GetBorder(nullptr, nullptr, nullptr, nullptr)); |
| 3535 | EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), nullptr, nullptr, nullptr)); |
| 3536 | } |
| 3537 | |
| 3538 | { |
| 3539 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
| 3540 | ASSERT_TRUE(annot); |
| 3541 | |
| 3542 | // Too few elements in the border array. |
| 3543 | float horizontal_radius; |
| 3544 | float vertical_radius; |
| 3545 | float border_width; |
| 3546 | EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius, |
| 3547 | &vertical_radius, &border_width)); |
Lei Zhang | 21ea665 | 2021-04-15 23:24:46 +0000 | [diff] [blame] | 3548 | |
| 3549 | // FPDFAnnot_SetBorder() positive testing. |
| 3550 | EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f, |
| 3551 | /*vertical_radius=*/3.5f, |
| 3552 | /*border_width=*/4.0f)); |
| 3553 | |
| 3554 | EXPECT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius, |
| 3555 | &vertical_radius, &border_width)); |
| 3556 | EXPECT_FLOAT_EQ(2.0f, horizontal_radius); |
| 3557 | EXPECT_FLOAT_EQ(3.5f, vertical_radius); |
| 3558 | EXPECT_FLOAT_EQ(4.0f, border_width); |
| 3559 | |
| 3560 | // FPDFAnnot_SetBorder() negative testing. |
| 3561 | EXPECT_FALSE(FPDFAnnot_SetBorder(nullptr, /*horizontal_radius=*/1.0f, |
| 3562 | /*vertical_radius=*/2.5f, |
| 3563 | /*border_width=*/3.0f)); |
Miklos Vajna | 305d2ed | 2020-12-15 17:28:49 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | UnloadPage(page); |
| 3567 | } |