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