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