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 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 5 | #include <algorithm> |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 6 | #include <cwchar> |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 7 | #include <memory> |
| 8 | #include <string> |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Lei Zhang | e4cdac5 | 2019-04-30 16:45:57 +0000 | [diff] [blame] | 11 | #include "build/build_config.h" |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 12 | #include "constants/annotation_common.h" |
Ankit Kumar | 8c7e4ad | 2020-02-07 08:56:49 +0000 | [diff] [blame] | 13 | #include "core/fxcrt/fx_memory.h" |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 14 | #include "core/fxcrt/fx_system.h" |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 15 | #include "public/cpp/fpdf_scopers.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 16 | #include "public/fpdf_annot.h" |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 17 | #include "public/fpdf_edit.h" |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 18 | #include "public/fpdf_formfill.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 19 | #include "public/fpdfview.h" |
| 20 | #include "testing/embedder_test.h" |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 21 | #include "testing/fx_string_testhelpers.h" |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 22 | #include "testing/gmock/include/gmock/gmock-matchers.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 23 | #include "testing/gtest/include/gtest/gtest.h" |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 24 | #include "testing/utils/hash.h" |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 25 | #include "third_party/base/span.h" |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | void VerifyFocusableAnnotSubtypes( |
| 30 | FPDF_FORMHANDLE form_handle, |
| 31 | pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) { |
| 32 | ASSERT_EQ(static_cast<int>(expected_subtypes.size()), |
| 33 | FPDFAnnot_GetFocusableSubtypesCount(form_handle)); |
| 34 | |
| 35 | std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes( |
| 36 | expected_subtypes.size()); |
| 37 | ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes( |
| 38 | form_handle, actual_subtypes.data(), actual_subtypes.size())); |
| 39 | for (size_t i = 0; i < expected_subtypes.size(); ++i) |
| 40 | ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]); |
| 41 | } |
| 42 | |
| 43 | void SetAndVerifyFocusableAnnotSubtypes( |
| 44 | FPDF_FORMHANDLE form_handle, |
| 45 | pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) { |
| 46 | ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(), |
| 47 | subtypes.size())); |
| 48 | VerifyFocusableAnnotSubtypes(form_handle, subtypes); |
| 49 | } |
| 50 | |
| 51 | void VerifyAnnotationSubtypesAndFocusability( |
| 52 | FPDF_FORMHANDLE form_handle, |
| 53 | FPDF_PAGE page, |
| 54 | pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes, |
| 55 | pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) { |
| 56 | ASSERT_EQ(static_cast<int>(expected_subtypes.size()), |
| 57 | FPDFPage_GetAnnotCount(page)); |
| 58 | for (size_t i = 0; i < expected_subtypes.size(); ++i) { |
| 59 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i)); |
| 60 | ASSERT_TRUE(annot); |
| 61 | EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get())); |
| 62 | |
| 63 | bool expected_focusable = pdfium::ContainsValue(expected_focusable_subtypes, |
| 64 | expected_subtypes[i]); |
| 65 | EXPECT_EQ(expected_focusable, |
| 66 | FORM_SetFocusedAnnot(form_handle, annot.get())); |
| 67 | |
| 68 | // Kill the focus so the next test starts in an unfocused state. |
| 69 | FORM_ForceToKillFocus(form_handle); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | } // namespace |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 74 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 75 | class FPDFAnnotEmbedderTest : public EmbedderTest {}; |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 76 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 77 | TEST_F(FPDFAnnotEmbedderTest, BadParams) { |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 78 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| 79 | FPDF_PAGE page = LoadPage(0); |
| 80 | ASSERT_TRUE(page); |
| 81 | |
| 82 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr)); |
| 83 | |
| 84 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0)); |
| 85 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1)); |
| 86 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1)); |
| 87 | EXPECT_FALSE(FPDFPage_GetAnnot(page, -1)); |
| 88 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 1)); |
| 89 | |
| 90 | EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr)); |
| 91 | |
| 92 | EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr)); |
| 93 | |
| 94 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0)); |
| 95 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1)); |
| 96 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1)); |
| 97 | |
| 98 | EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo")); |
| 99 | |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 100 | static const wchar_t kContents[] = L"Bar"; |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 101 | ScopedFPDFWideString text = GetFPDFWideString(kContents); |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 102 | EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get())); |
| 103 | |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 104 | FPDF_WCHAR buffer[64]; |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 105 | EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0)); |
| 106 | EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0)); |
| 107 | EXPECT_EQ(0u, |
| 108 | FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer))); |
| 109 | |
| 110 | UnloadPage(page); |
| 111 | } |
| 112 | |
Lei Zhang | 3d9a097 | 2019-03-04 19:34:09 +0000 | [diff] [blame] | 113 | TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) { |
| 114 | ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf")); |
| 115 | FPDF_PAGE page = LoadPage(0); |
| 116 | ASSERT_TRUE(page); |
| 117 | |
| 118 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Lei Zhang | 98dc8c0 | 2019-03-04 19:40:30 +0000 | [diff] [blame] | 119 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | 3d9a097 | 2019-03-04 19:34:09 +0000 | [diff] [blame] | 120 | |
| 121 | UnloadPage(page); |
| 122 | } |
| 123 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 124 | TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) { |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 125 | // Open a file with one annotation and load its first page. |
| 126 | ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 127 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 128 | ASSERT_TRUE(page); |
| 129 | |
| 130 | // This annotation has a malformed appearance stream, which does not have its |
| 131 | // normal appearance defined, only its rollover appearance. In this case, its |
| 132 | // normal appearance should be generated, allowing the highlight annotation to |
| 133 | // still display. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 134 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | a98e366 | 2018-02-07 20:28:35 +0000 | [diff] [blame] | 135 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 136 | |
| 137 | UnloadPage(page); |
| 138 | } |
| 139 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 140 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 141 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 142 | #define MAYBE_RenderMultilineMarkupAnnotWithoutAP \ |
| 143 | DISABLED_RenderMultilineMarkupAnnotWithoutAP |
| 144 | #else |
| 145 | #define MAYBE_RenderMultilineMarkupAnnotWithoutAP \ |
| 146 | RenderMultilineMarkupAnnotWithoutAP |
| 147 | #endif |
| 148 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_RenderMultilineMarkupAnnotWithoutAP) { |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 149 | static const char kMd5[] = "76512832d88017668d9acc7aacd13dae"; |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 150 | // Open a file with multiline markup annotations. |
Ralf Sippl | b3a5240 | 2018-03-19 23:30:28 +0000 | [diff] [blame] | 151 | ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf")); |
| 152 | FPDF_PAGE page = LoadPage(0); |
| 153 | ASSERT_TRUE(page); |
| 154 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 155 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 156 | CompareBitmap(bitmap.get(), 595, 842, kMd5); |
Ralf Sippl | b3a5240 | 2018-03-19 23:30:28 +0000 | [diff] [blame] | 157 | |
| 158 | UnloadPage(page); |
| 159 | } |
| 160 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 161 | TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) { |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 162 | // Open a file with one annotation and load its first page. |
| 163 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 164 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 165 | ASSERT_TRUE(page); |
| 166 | |
| 167 | // Check that there is a total of 1 annotation on its first page. |
| 168 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 169 | |
| 170 | // Check that the annotation is of type "highlight". |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 171 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 172 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 173 | ASSERT_TRUE(annot); |
| 174 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 175 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 176 | // Check that the annotation color is yellow. |
| 177 | unsigned int R; |
| 178 | unsigned int G; |
| 179 | unsigned int B; |
| 180 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 181 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 182 | &G, &B, &A)); |
| 183 | EXPECT_EQ(255u, R); |
| 184 | EXPECT_EQ(255u, G); |
| 185 | EXPECT_EQ(0u, B); |
| 186 | EXPECT_EQ(255u, A); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 187 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 188 | // Check that the author is correct. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 189 | static const char kAuthorKey[] = "T"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 190 | EXPECT_EQ(FPDF_OBJECT_STRING, |
| 191 | FPDFAnnot_GetValueType(annot.get(), kAuthorKey)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 192 | unsigned long length_bytes = |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 193 | FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 194 | ASSERT_EQ(28u, length_bytes); |
| 195 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 196 | EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(), |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 197 | length_bytes)); |
| 198 | EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 199 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 200 | // Check that the content is correct. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 201 | EXPECT_EQ( |
| 202 | FPDF_OBJECT_STRING, |
| 203 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 204 | length_bytes = FPDFAnnot_GetStringValue( |
| 205 | annot.get(), pdfium::annotation::kContents, nullptr, 0); |
| 206 | ASSERT_EQ(2690u, length_bytes); |
| 207 | buf = GetFPDFWideStringBuffer(length_bytes); |
| 208 | EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(), |
| 209 | pdfium::annotation::kContents, |
| 210 | buf.data(), length_bytes)); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 211 | static const wchar_t kContents[] = |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 212 | L"This is a note for that highlight annotation. Very long highlight " |
| 213 | "annotation. Long long long Long long longLong long longLong long " |
| 214 | "longLong long longLong long longLong long longLong long longLong long " |
| 215 | "longLong long longLong long longLong long longLong long longLong long " |
| 216 | "longLong long longLong long longLong long longLong long longLong long " |
| 217 | "longLong long longLong long longLong long longLong long longLong long " |
| 218 | "longLong long longLong long longLong long longLong long longLong long " |
| 219 | "longLong long longLong long longLong long longLong long longLong long " |
| 220 | "longLong long longLong long longLong long longLong long longLong long " |
| 221 | "longLong long longLong long longLong long longLong long longLong long " |
| 222 | "longLong long longLong long longLong long longLong long longLong long " |
| 223 | "longLong long longLong long longLong long longLong long longLong long " |
| 224 | "longLong long longLong long longLong long longLong long longLong long " |
| 225 | "longLong long longLong long longLong long longLong long longLong long " |
| 226 | "longLong long longLong long longLong long longLong long longLong long " |
| 227 | "longLong long longLong long longLong long longLong long longLong long " |
| 228 | "longLong long longLong long longLong long longLong long longLong long " |
| 229 | "longLong long longLong long longLong long longLong long longLong long " |
| 230 | "longLong long longLong long longLong long longLong long longLong long " |
| 231 | "longLong long long. END"; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 232 | EXPECT_EQ(kContents, GetPlatformWString(buf.data())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 233 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 234 | // Check that the quadpoints are correct. |
| 235 | FS_QUADPOINTSF quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 236 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 237 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 238 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 239 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 240 | EXPECT_EQ(706.264465f, quadpoints.y4); |
| 241 | } |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 242 | UnloadPageNoEvents(page); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 243 | } |
| 244 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 245 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 246 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 247 | #define MAYBE_ExtractInkMultiple DISABLED_ExtractInkMultiple |
| 248 | #else |
| 249 | #define MAYBE_ExtractInkMultiple ExtractInkMultiple |
| 250 | #endif |
| 251 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_ExtractInkMultiple) { |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 252 | // Open a file with three annotations and load its first page. |
| 253 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 254 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 255 | ASSERT_TRUE(page); |
| 256 | |
| 257 | // Check that there is a total of 3 annotation on its first page. |
| 258 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 259 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 260 | { |
| 261 | // Check that the third annotation is of type "ink". |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 262 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 263 | ASSERT_TRUE(annot); |
| 264 | EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 265 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 266 | // Check that the annotation color is blue with opacity. |
| 267 | unsigned int R; |
| 268 | unsigned int G; |
| 269 | unsigned int B; |
| 270 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 271 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 272 | &G, &B, &A)); |
| 273 | EXPECT_EQ(0u, R); |
| 274 | EXPECT_EQ(0u, G); |
| 275 | EXPECT_EQ(255u, B); |
| 276 | EXPECT_EQ(76u, A); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 277 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 278 | // Check that there is no content. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 279 | EXPECT_EQ(2u, FPDFAnnot_GetStringValue( |
| 280 | annot.get(), pdfium::annotation::kContents, nullptr, 0)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 281 | |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 282 | // Check that the rectangle coordinates are correct. |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 283 | // Note that upon rendering, the rectangle coordinates will be adjusted. |
| 284 | FS_RECTF rect; |
| 285 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 286 | EXPECT_EQ(351.820404f, rect.left); |
| 287 | EXPECT_EQ(583.830688f, rect.bottom); |
| 288 | EXPECT_EQ(475.336090f, rect.right); |
| 289 | EXPECT_EQ(681.535034f, rect.top); |
| 290 | } |
Tom Sepez | ef43c26 | 2018-11-07 16:41:32 +0000 | [diff] [blame] | 291 | { |
| 292 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 293 | CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a"); |
| 294 | } |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 295 | UnloadPageNoEvents(page); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 296 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 297 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 298 | TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 299 | // Open a file with one annotation and load its first page. |
| 300 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 301 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 302 | ASSERT_TRUE(page); |
| 303 | |
| 304 | // Add an annotation with an illegal subtype. |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 305 | ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 306 | |
| 307 | UnloadPage(page); |
| 308 | } |
| 309 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 310 | TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 311 | // Open a file with no annotation and load its first page. |
| 312 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 313 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 314 | ASSERT_TRUE(page); |
| 315 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(page)); |
| 316 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 317 | { |
| 318 | // Add a text annotation to the page. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 319 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 320 | ASSERT_TRUE(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 321 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 322 | // Check that there is now 1 annotations on this page. |
| 323 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 324 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 325 | // Check that the subtype of the annotation is correct. |
| 326 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 327 | } |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 328 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 329 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 330 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 331 | ASSERT_TRUE(annot); |
| 332 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 333 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 334 | // Set the color of the annotation. |
| 335 | ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51, |
| 336 | 102, 153, 204)); |
| 337 | // Check that the color has been set correctly. |
| 338 | unsigned int R; |
| 339 | unsigned int G; |
| 340 | unsigned int B; |
| 341 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 342 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 343 | &G, &B, &A)); |
| 344 | EXPECT_EQ(51u, R); |
| 345 | EXPECT_EQ(102u, G); |
| 346 | EXPECT_EQ(153u, B); |
| 347 | EXPECT_EQ(204u, A); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 348 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 349 | // Change the color of the annotation. |
| 350 | ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204, |
| 351 | 153, 102, 51)); |
| 352 | // Check that the color has been set correctly. |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 353 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 354 | &G, &B, &A)); |
| 355 | EXPECT_EQ(204u, R); |
| 356 | EXPECT_EQ(153u, G); |
| 357 | EXPECT_EQ(102u, B); |
| 358 | EXPECT_EQ(51u, A); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 359 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 360 | // Set the annotation rectangle. |
| 361 | FS_RECTF rect; |
| 362 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 363 | EXPECT_EQ(0.f, rect.left); |
| 364 | EXPECT_EQ(0.f, rect.right); |
| 365 | rect.left = 35; |
| 366 | rect.bottom = 150; |
| 367 | rect.right = 53; |
| 368 | rect.top = 165; |
| 369 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 370 | // Check that the annotation rectangle has been set correctly. |
| 371 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 372 | EXPECT_EQ(35.f, rect.left); |
| 373 | EXPECT_EQ(150.f, rect.bottom); |
| 374 | EXPECT_EQ(53.f, rect.right); |
| 375 | EXPECT_EQ(165.f, rect.top); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 376 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 377 | // Set the content of the annotation. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 378 | static const wchar_t kContents[] = L"Hello! This is a customized content."; |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 379 | ScopedFPDFWideString text = GetFPDFWideString(kContents); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 380 | ASSERT_TRUE(FPDFAnnot_SetStringValue( |
| 381 | annot.get(), pdfium::annotation::kContents, text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 382 | // Check that the content has been set correctly. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 383 | unsigned long length_bytes = FPDFAnnot_GetStringValue( |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 384 | annot.get(), pdfium::annotation::kContents, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 385 | ASSERT_EQ(74u, length_bytes); |
| 386 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 387 | EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(), |
| 388 | pdfium::annotation::kContents, |
| 389 | buf.data(), length_bytes)); |
| 390 | EXPECT_EQ(kContents, GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 391 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 392 | UnloadPage(page); |
| 393 | } |
| 394 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 395 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 396 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 397 | #define MAYBE_AddAndSaveUnderlineAnnotation \ |
| 398 | DISABLED_AddAndSaveUnderlineAnnotation |
| 399 | #else |
| 400 | #define MAYBE_AddAndSaveUnderlineAnnotation AddAndSaveUnderlineAnnotation |
| 401 | #endif |
| 402 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndSaveUnderlineAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 403 | // Open a file with one annotation and load its first page. |
| 404 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 405 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 406 | ASSERT_TRUE(page); |
| 407 | |
| 408 | // Check that there is a total of one annotation on its first page, and verify |
| 409 | // its quadpoints. |
| 410 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 411 | FS_QUADPOINTSF quadpoints; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 412 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 413 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 414 | ASSERT_TRUE(annot); |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 415 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 416 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 417 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 418 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 419 | EXPECT_EQ(706.264465f, quadpoints.y4); |
| 420 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 421 | |
| 422 | // Add an underline annotation to the page and set its quadpoints. |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 423 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 424 | ScopedFPDFAnnotation annot( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 425 | FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE)); |
| 426 | ASSERT_TRUE(annot); |
| 427 | quadpoints.x1 = 140.802643f; |
| 428 | quadpoints.x3 = 140.802643f; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 429 | ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 430 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 431 | |
| 432 | // Save the document, closing the page and document. |
| 433 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 434 | UnloadPage(page); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 435 | |
| 436 | // Open the saved document. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 437 | static const char kMd5[] = "dba153419f67b7c0c0e3d22d3e8910d5"; |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 438 | |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 439 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 440 | page = LoadSavedPage(0); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 441 | VerifySavedRendering(page, 612, 792, kMd5); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 442 | |
| 443 | // Check that the saved document has 2 annotations on the first page |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 444 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 445 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 446 | { |
| 447 | // Check that the second annotation is an underline annotation and verify |
| 448 | // its quadpoints. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 449 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 450 | ASSERT_TRUE(new_annot); |
| 451 | EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get())); |
| 452 | FS_QUADPOINTSF new_quadpoints; |
| 453 | ASSERT_TRUE( |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 454 | FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 455 | EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f); |
| 456 | EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f); |
| 457 | EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f); |
| 458 | EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f); |
| 459 | } |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 460 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 461 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 462 | CloseSavedDocument(); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 463 | } |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 464 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 465 | TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) { |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 466 | // Open a file with four annotations and load its first page. |
| 467 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
| 468 | FPDF_PAGE page = LoadPage(0); |
| 469 | ASSERT_TRUE(page); |
| 470 | EXPECT_EQ(4, FPDFPage_GetAnnotCount(page)); |
| 471 | |
| 472 | // Retrieve the highlight annotation. |
| 473 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 474 | ASSERT_TRUE(annot); |
| 475 | ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot)); |
| 476 | |
| 477 | FS_QUADPOINTSF quadpoints; |
| 478 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints)); |
| 479 | |
| 480 | { |
| 481 | // Verify the current one set of quadpoints. |
| 482 | ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 483 | |
| 484 | EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f); |
| 485 | EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f); |
| 486 | EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f); |
| 487 | EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f); |
| 488 | } |
| 489 | |
| 490 | { |
| 491 | // Update the quadpoints. |
| 492 | FS_QUADPOINTSF new_quadpoints = quadpoints; |
| 493 | new_quadpoints.y1 -= 20.f; |
| 494 | new_quadpoints.y2 -= 20.f; |
| 495 | new_quadpoints.y3 -= 20.f; |
| 496 | new_quadpoints.y4 -= 20.f; |
| 497 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints)); |
| 498 | |
| 499 | // Verify added quadpoint set |
| 500 | ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 501 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints)); |
| 502 | EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f); |
| 503 | EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f); |
| 504 | EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f); |
| 505 | EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f); |
| 506 | } |
| 507 | |
| 508 | { |
| 509 | // Append a new set of quadpoints. |
| 510 | FS_QUADPOINTSF new_quadpoints = quadpoints; |
| 511 | new_quadpoints.y1 += 20.f; |
| 512 | new_quadpoints.y2 += 20.f; |
| 513 | new_quadpoints.y3 += 20.f; |
| 514 | new_quadpoints.y4 += 20.f; |
| 515 | ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints)); |
| 516 | |
| 517 | // Verify added quadpoint set |
| 518 | ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 519 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints)); |
| 520 | EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f); |
| 521 | EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f); |
| 522 | EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f); |
| 523 | EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f); |
| 524 | } |
| 525 | |
| 526 | { |
| 527 | // Setting and getting quadpoints at out-of-bound index should fail |
| 528 | EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints)); |
| 529 | EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints)); |
| 530 | } |
| 531 | |
| 532 | FPDFPage_CloseAnnot(annot); |
| 533 | |
| 534 | // Retrieve the square annotation |
| 535 | FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2); |
| 536 | |
| 537 | { |
| 538 | // Check that attempting to set its quadpoints would fail |
| 539 | ASSERT_TRUE(squareAnnot); |
| 540 | EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot)); |
| 541 | EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot)); |
| 542 | EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints)); |
| 543 | } |
| 544 | |
| 545 | FPDFPage_CloseAnnot(squareAnnot); |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 546 | UnloadPage(page); |
| 547 | } |
| 548 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 549 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 550 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 551 | #define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP |
| 552 | #else |
| 553 | #define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP |
| 554 | #endif |
| 555 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) { |
Lei Zhang | e4cdac5 | 2019-04-30 16:45:57 +0000 | [diff] [blame] | 556 | #if defined(OS_MACOSX) |
Lei Zhang | 0f6b434 | 2020-02-25 20:00:39 +0000 | [diff] [blame] | 557 | static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a"; |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 558 | static const char kMd5ModifiedHighlight[] = |
Lei Zhang | 0f6b434 | 2020-02-25 20:00:39 +0000 | [diff] [blame] | 559 | "e64bf648f6e9354d1f3eedb47a2c9498"; |
| 560 | static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf"; |
Lei Zhang | e67bcc7 | 2019-04-30 18:55:58 +0000 | [diff] [blame] | 561 | #elif defined(OS_WIN) |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 562 | static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5"; |
| 563 | static const char kMd5ModifiedHighlight[] = |
| 564 | "66f3caef3a7d488a4fa1ad37fc06310e"; |
| 565 | static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563"; |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 566 | #else |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 567 | static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5"; |
| 568 | static const char kMd5ModifiedHighlight[] = |
| 569 | "66f3caef3a7d488a4fa1ad37fc06310e"; |
| 570 | static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563"; |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 571 | #endif |
| 572 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 573 | // Open a file with four annotations and load its first page. |
| 574 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 575 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 576 | ASSERT_TRUE(page); |
| 577 | EXPECT_EQ(4, FPDFPage_GetAnnotCount(page)); |
| 578 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 579 | // Check that the original file renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 580 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 581 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 582 | CompareBitmap(bitmap.get(), 612, 792, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 583 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 584 | |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 585 | FS_RECTF rect; |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 586 | FS_RECTF new_rect; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 587 | |
| 588 | // Retrieve the highlight annotation which has its AP stream already defined. |
| 589 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 590 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 591 | ASSERT_TRUE(annot); |
| 592 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 593 | |
| 594 | // Check that color cannot be set when an AP stream is defined already. |
| 595 | EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51, |
| 596 | 102, 153, 204)); |
| 597 | |
| 598 | // Verify its attachment points. |
| 599 | FS_QUADPOINTSF quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 600 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 601 | EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f); |
| 602 | EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f); |
| 603 | EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f); |
| 604 | EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f); |
| 605 | |
| 606 | // Check that updating the attachment points would succeed. |
| 607 | quadpoints.x1 -= 50.f; |
| 608 | quadpoints.x2 -= 50.f; |
| 609 | quadpoints.x3 -= 50.f; |
| 610 | quadpoints.x4 -= 50.f; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 611 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 612 | FS_QUADPOINTSF new_quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 613 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 614 | EXPECT_EQ(quadpoints.x1, new_quadpoints.x1); |
| 615 | EXPECT_EQ(quadpoints.y1, new_quadpoints.y1); |
| 616 | EXPECT_EQ(quadpoints.x4, new_quadpoints.x4); |
| 617 | EXPECT_EQ(quadpoints.y4, new_quadpoints.y4); |
| 618 | |
| 619 | // Check that updating quadpoints does not change the annotation's position. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 620 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 621 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 622 | CompareBitmap(bitmap.get(), 612, 792, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 623 | } |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 624 | |
| 625 | // Verify its annotation rectangle. |
| 626 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 627 | EXPECT_NEAR(67.7299f, rect.left, 0.001f); |
| 628 | EXPECT_NEAR(704.296f, rect.bottom, 0.001f); |
| 629 | EXPECT_NEAR(136.325f, rect.right, 0.001f); |
| 630 | EXPECT_NEAR(721.292f, rect.top, 0.001f); |
| 631 | |
| 632 | // Check that updating the rectangle would succeed. |
| 633 | rect.left -= 60.f; |
| 634 | rect.right -= 60.f; |
| 635 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 636 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 637 | EXPECT_EQ(rect.right, new_rect.right); |
| 638 | } |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 639 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 640 | // Check that updating the rectangle changes the annotation's position. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 641 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 642 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 643 | CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 644 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 645 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 646 | { |
| 647 | // Retrieve the square annotation which has its AP stream already defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 648 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 649 | ASSERT_TRUE(annot); |
| 650 | EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 651 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 652 | // Check that updating the rectangle would succeed. |
| 653 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 654 | rect.left += 70.f; |
| 655 | rect.right += 70.f; |
| 656 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 657 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 658 | EXPECT_EQ(rect.right, new_rect.right); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 659 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 660 | // Check that updating the rectangle changes the square annotation's |
| 661 | // position. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 662 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 663 | CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 664 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 665 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 666 | UnloadPage(page); |
| 667 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 668 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 669 | TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) { |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 670 | // Open a file with multiline markup annotations. |
| 671 | ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf")); |
| 672 | FPDF_PAGE page = LoadPage(0); |
| 673 | ASSERT_TRUE(page); |
| 674 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 675 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 676 | ASSERT_TRUE(annot); |
| 677 | |
| 678 | // This is a three line annotation. |
| 679 | EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get())); |
| 680 | } |
| 681 | UnloadPage(page); |
| 682 | |
| 683 | // null annotation should return 0 |
| 684 | EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr)); |
| 685 | } |
| 686 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 687 | TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) { |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 688 | // Open a file with 3 annotations on its first page. |
| 689 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 690 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 691 | ASSERT_TRUE(page); |
| 692 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 693 | |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 694 | FS_RECTF rect; |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 695 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 696 | // Check that the annotations have the expected rectangle coordinates. |
| 697 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 698 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 699 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 700 | EXPECT_NEAR(86.1971f, rect.left, 0.001f); |
| 701 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 702 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 703 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 704 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 705 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 706 | EXPECT_NEAR(149.8127f, rect.left, 0.001f); |
| 707 | } |
| 708 | |
| 709 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 710 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 711 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 712 | EXPECT_NEAR(351.8204f, rect.left, 0.001f); |
| 713 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 714 | |
| 715 | // Check that nothing happens when attempting to remove an annotation with an |
| 716 | // out-of-bound index. |
| 717 | EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4)); |
| 718 | EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1)); |
| 719 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 720 | |
| 721 | // Remove the second annotation. |
| 722 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1)); |
| 723 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 724 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 2)); |
| 725 | |
| 726 | // Save the document, closing the page and document. |
| 727 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 728 | UnloadPageNoEvents(page); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 729 | |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 730 | // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1?? |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 731 | // Open the saved document. |
| 732 | std::string new_file = GetString(); |
| 733 | FPDF_FILEACCESS file_access; |
| 734 | memset(&file_access, 0, sizeof(file_access)); |
| 735 | file_access.m_FileLen = new_file.size(); |
| 736 | file_access.m_GetBlock = GetBlockFromString; |
| 737 | file_access.m_Param = &new_file; |
| 738 | FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 739 | ASSERT_TRUE(new_doc); |
| 740 | FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); |
| 741 | ASSERT_TRUE(new_page); |
| 742 | |
| 743 | // Check that the saved document has 2 annotations on the first page. |
| 744 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page)); |
| 745 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 746 | // Check that the remaining 2 annotations are the original 1st and 3rd ones |
| 747 | // by verifying their rectangle coordinates. |
| 748 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 749 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 750 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 751 | EXPECT_NEAR(86.1971f, rect.left, 0.001f); |
| 752 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 753 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 754 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 755 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 756 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 757 | EXPECT_NEAR(351.8204f, rect.left, 0.001f); |
| 758 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 759 | FPDF_ClosePage(new_page); |
| 760 | FPDF_CloseDocument(new_doc); |
| 761 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 762 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 763 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 764 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 765 | #define MAYBE_AddAndModifyPath DISABLED_AddAndModifyPath |
| 766 | #else |
| 767 | #define MAYBE_AddAndModifyPath AddAndModifyPath |
| 768 | #endif |
| 769 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyPath) { |
Lei Zhang | e4cdac5 | 2019-04-30 16:45:57 +0000 | [diff] [blame] | 770 | #if defined(OS_MACOSX) |
Lei Zhang | 0f6b434 | 2020-02-25 20:00:39 +0000 | [diff] [blame] | 771 | static const char kMd5Original[] = "80d7b6cc7b13a78d77a6151bc846e80b"; |
| 772 | static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e"; |
| 773 | static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f"; |
| 774 | static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d"; |
Lei Zhang | e67bcc7 | 2019-04-30 18:55:58 +0000 | [diff] [blame] | 775 | #elif defined(OS_WIN) |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 776 | static const char kMd5Original[] = "6aa001a77ec05d0f1b0d1d22e28744d4"; |
| 777 | static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1"; |
| 778 | static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6"; |
| 779 | static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 780 | #else |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 781 | static const char kMd5Original[] = "b42cef463483e668eaf4055a65e4f1f5"; |
| 782 | static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517"; |
| 783 | static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6"; |
| 784 | static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 785 | #endif |
| 786 | |
| 787 | // Open a file with two annotations and load its first page. |
| 788 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 789 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 790 | ASSERT_TRUE(page); |
| 791 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 792 | |
| 793 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 794 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 795 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 796 | CompareBitmap(bitmap.get(), 595, 842, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 797 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 798 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 799 | { |
| 800 | // Retrieve the stamp annotation which has its AP stream already defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 801 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 802 | ASSERT_TRUE(annot); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 803 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 804 | // Check that this annotation has one path object and retrieve it. |
| 805 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 806 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 807 | FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1); |
| 808 | EXPECT_FALSE(path); |
| 809 | path = FPDFAnnot_GetObject(annot.get(), 0); |
| 810 | EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path)); |
| 811 | EXPECT_TRUE(path); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 812 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 813 | // Modify the color of the path object. |
Lei Zhang | 3475b48 | 2019-05-13 18:30:57 +0000 | [diff] [blame] | 814 | EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 815 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 816 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 817 | // Check that the page with the modified annotation renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 818 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 819 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 820 | CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 821 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 822 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 823 | // Add a second path object to the same annotation. |
| 824 | FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84); |
| 825 | EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88)); |
Lei Zhang | 3475b48 | 2019-05-13 18:30:57 +0000 | [diff] [blame] | 826 | EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100)); |
| 827 | EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 828 | EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1)); |
| 829 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot)); |
| 830 | EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get())); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 831 | |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 832 | // The object is in the annontation, not in the page, so the page object |
| 833 | // array should not change. |
| 834 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
| 835 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 836 | // Check that the page with an annotation with two paths renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 837 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 838 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 839 | CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 840 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 841 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 842 | // Delete the newly added path object. |
| 843 | EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1)); |
| 844 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 845 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 846 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 847 | |
| 848 | // Check that the page renders the same as before. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 849 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 850 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 851 | CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 852 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 853 | |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 854 | FS_RECTF rect; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 855 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 856 | { |
| 857 | // Create another stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 858 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 859 | ASSERT_TRUE(annot); |
| 860 | rect.left = 200.f; |
| 861 | rect.bottom = 400.f; |
| 862 | rect.right = 500.f; |
| 863 | rect.top = 600.f; |
| 864 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 865 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 866 | // Add a new path to the annotation. |
| 867 | FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500); |
| 868 | EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400)); |
| 869 | EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600)); |
| 870 | EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550)); |
| 871 | EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450)); |
Lei Zhang | 3475b48 | 2019-05-13 18:30:57 +0000 | [diff] [blame] | 872 | EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180)); |
| 873 | EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 874 | EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1)); |
| 875 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check)); |
| 876 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 877 | |
| 878 | // Check that the annotation's bounding box came from its rectangle. |
| 879 | FS_RECTF new_rect; |
| 880 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 881 | EXPECT_EQ(rect.left, new_rect.left); |
| 882 | EXPECT_EQ(rect.bottom, new_rect.bottom); |
| 883 | EXPECT_EQ(rect.right, new_rect.right); |
| 884 | EXPECT_EQ(rect.top, new_rect.top); |
| 885 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 886 | |
| 887 | // Save the document, closing the page and document. |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 888 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 889 | UnloadPage(page); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 890 | |
| 891 | // Open the saved document. |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 892 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 893 | page = LoadSavedPage(0); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 894 | VerifySavedRendering(page, 595, 842, kMd5NewAnnot); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 895 | |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 896 | // Check that the document has a correct count of annotations and objects. |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 897 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 898 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 899 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 900 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 901 | ASSERT_TRUE(annot); |
| 902 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 903 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 904 | // Check that the new annotation's rectangle is as defined. |
| 905 | FS_RECTF new_rect; |
| 906 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 907 | EXPECT_EQ(rect.left, new_rect.left); |
| 908 | EXPECT_EQ(rect.bottom, new_rect.bottom); |
| 909 | EXPECT_EQ(rect.right, new_rect.right); |
| 910 | EXPECT_EQ(rect.top, new_rect.top); |
| 911 | } |
| 912 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 913 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 914 | CloseSavedDocument(); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 915 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 916 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 917 | TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) { |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 918 | // Open a file with an annotation and load its first page. |
| 919 | ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 920 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 921 | ASSERT_TRUE(page); |
| 922 | |
| 923 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 924 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 925 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 926 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 927 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 928 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 929 | { |
| 930 | // Retrieve the annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 931 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 932 | ASSERT_TRUE(annot); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 933 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 934 | // Check that the original flag values are as expected. |
| 935 | int flags = FPDFAnnot_GetFlags(annot.get()); |
| 936 | EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 937 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 938 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 939 | // Set the HIDDEN flag. |
| 940 | flags |= FPDF_ANNOT_FLAG_HIDDEN; |
| 941 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags)); |
| 942 | flags = FPDFAnnot_GetFlags(annot.get()); |
| 943 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 944 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 945 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 946 | // Check that the page renders correctly without rendering the annotation. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 947 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 948 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 949 | CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3"); |
| 950 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 951 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 952 | // Unset the HIDDEN flag. |
| 953 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE)); |
| 954 | EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get())); |
| 955 | flags &= ~FPDF_ANNOT_FLAG_HIDDEN; |
| 956 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags)); |
| 957 | flags = FPDFAnnot_GetFlags(annot.get()); |
| 958 | EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 959 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 960 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 961 | // Check that the page renders correctly as before. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 962 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 963 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 964 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 965 | } |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 966 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 967 | |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 968 | UnloadPage(page); |
| 969 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 970 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 971 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 972 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 973 | #define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage |
| 974 | #else |
| 975 | #define MAYBE_AddAndModifyImage AddAndModifyImage |
| 976 | #endif |
| 977 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) { |
Lei Zhang | e4cdac5 | 2019-04-30 16:45:57 +0000 | [diff] [blame] | 978 | #if defined(OS_MACOSX) |
Lei Zhang | 0f6b434 | 2020-02-25 20:00:39 +0000 | [diff] [blame] | 979 | static const char kMd5Original[] = "80d7b6cc7b13a78d77a6151bc846e80b"; |
| 980 | static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9"; |
| 981 | static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99"; |
Lei Zhang | e67bcc7 | 2019-04-30 18:55:58 +0000 | [diff] [blame] | 982 | #elif defined(OS_WIN) |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 983 | static const char kMd5Original[] = "6aa001a77ec05d0f1b0d1d22e28744d4"; |
| 984 | static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b"; |
| 985 | static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 986 | #else |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 987 | static const char kMd5Original[] = "b42cef463483e668eaf4055a65e4f1f5"; |
| 988 | static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935"; |
| 989 | static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 990 | #endif |
| 991 | |
| 992 | // Open a file with two annotations and load its first page. |
| 993 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 994 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 995 | ASSERT_TRUE(page); |
| 996 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 997 | |
| 998 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 999 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1000 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1001 | CompareBitmap(bitmap.get(), 595, 842, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1002 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1003 | |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1004 | constexpr int kBitmapSize = 200; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1005 | FPDF_BITMAP image_bitmap; |
| 1006 | |
| 1007 | { |
| 1008 | // Create a stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1009 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1010 | ASSERT_TRUE(annot); |
| 1011 | FS_RECTF rect; |
| 1012 | rect.left = 200.f; |
| 1013 | rect.bottom = 600.f; |
| 1014 | rect.right = 400.f; |
| 1015 | rect.top = 800.f; |
| 1016 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 1017 | |
| 1018 | // Add a solid-color translucent image object to the new annotation. |
| 1019 | image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1); |
| 1020 | FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, |
| 1021 | 0xeeeecccc); |
| 1022 | EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap)); |
| 1023 | EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap)); |
| 1024 | FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document()); |
| 1025 | ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap)); |
| 1026 | ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0, |
| 1027 | kBitmapSize, 0, 0)); |
| 1028 | FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600); |
| 1029 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object)); |
| 1030 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1031 | |
| 1032 | // Check that the page renders correctly with the new image object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1033 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1034 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1035 | CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1036 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1037 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1038 | { |
| 1039 | // Retrieve the newly added stamp annotation and its image object. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1040 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1041 | ASSERT_TRUE(annot); |
| 1042 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 1043 | FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0); |
| 1044 | EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1045 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1046 | // Modify the image in the new annotation. |
| 1047 | FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, |
| 1048 | 0xff000000); |
| 1049 | ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap)); |
| 1050 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object)); |
| 1051 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1052 | |
| 1053 | // Save the document, closing the page and document. |
| 1054 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1055 | UnloadPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1056 | FPDFBitmap_Destroy(image_bitmap); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1057 | |
| 1058 | // Test that the saved document renders the modified image object correctly. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1059 | VerifySavedDocument(595, 842, kMd5ModifiedImage); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1060 | } |
| 1061 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1062 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 1063 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 1064 | #define MAYBE_AddAndModifyText DISABLED_AddAndModifyText |
| 1065 | #else |
| 1066 | #define MAYBE_AddAndModifyText AddAndModifyText |
| 1067 | #endif |
| 1068 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyText) { |
Lei Zhang | e4cdac5 | 2019-04-30 16:45:57 +0000 | [diff] [blame] | 1069 | #if defined(OS_MACOSX) |
Lei Zhang | 0f6b434 | 2020-02-25 20:00:39 +0000 | [diff] [blame] | 1070 | static const char kMd5Original[] = "80d7b6cc7b13a78d77a6151bc846e80b"; |
| 1071 | static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da"; |
| 1072 | static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188"; |
Lei Zhang | e67bcc7 | 2019-04-30 18:55:58 +0000 | [diff] [blame] | 1073 | #elif defined(OS_WIN) |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1074 | static const char kMd5Original[] = "6aa001a77ec05d0f1b0d1d22e28744d4"; |
| 1075 | static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6"; |
| 1076 | static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1077 | #else |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1078 | static const char kMd5Original[] = "b42cef463483e668eaf4055a65e4f1f5"; |
| 1079 | static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306"; |
| 1080 | static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1081 | #endif |
| 1082 | |
| 1083 | // Open a file with two annotations and load its first page. |
| 1084 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1085 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1086 | ASSERT_TRUE(page); |
| 1087 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 1088 | |
| 1089 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1090 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1091 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1092 | CompareBitmap(bitmap.get(), 595, 842, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1093 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1094 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1095 | { |
| 1096 | // Create a stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1097 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1098 | ASSERT_TRUE(annot); |
| 1099 | FS_RECTF rect; |
| 1100 | rect.left = 200.f; |
| 1101 | rect.bottom = 550.f; |
| 1102 | rect.right = 450.f; |
| 1103 | rect.top = 650.f; |
| 1104 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1105 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1106 | // Add a translucent text object to the new annotation. |
| 1107 | FPDF_PAGEOBJECT text_object = |
| 1108 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 1109 | EXPECT_TRUE(text_object); |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1110 | ScopedFPDFWideString text = |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1111 | GetFPDFWideString(L"I'm a translucent text laying on other text."); |
| 1112 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
Lei Zhang | 3475b48 | 2019-05-13 18:30:57 +0000 | [diff] [blame] | 1113 | EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1114 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600); |
| 1115 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object)); |
| 1116 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1117 | |
| 1118 | // Check that the page renders correctly with the new text object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1119 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1120 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1121 | CompareBitmap(bitmap.get(), 595, 842, kMd5NewText); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1122 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1123 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1124 | { |
| 1125 | // Retrieve the newly added stamp annotation and its text object. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1126 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1127 | ASSERT_TRUE(annot); |
| 1128 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 1129 | FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0); |
| 1130 | EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1131 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1132 | // Modify the text in the new annotation. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1133 | ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!"); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1134 | EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get())); |
| 1135 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object)); |
| 1136 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1137 | |
| 1138 | // Check that the page renders correctly with the modified text object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1139 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1140 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1141 | CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1142 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1143 | |
| 1144 | // Remove the new annotation, and check that the page renders as before. |
| 1145 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2)); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1146 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1147 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1148 | CompareBitmap(bitmap.get(), 595, 842, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1149 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1150 | |
| 1151 | UnloadPage(page); |
| 1152 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1153 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1154 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 1155 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 1156 | #define MAYBE_GetSetStringValue DISABLED_GetSetStringValue |
| 1157 | #else |
| 1158 | #define MAYBE_GetSetStringValue GetSetStringValue |
| 1159 | #endif |
| 1160 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_GetSetStringValue) { |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1161 | // Open a file with four annotations and load its first page. |
| 1162 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1163 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1164 | ASSERT_TRUE(page); |
| 1165 | |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1166 | static const wchar_t kNewDate[] = L"D:201706282359Z00'00'"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1167 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1168 | { |
| 1169 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1170 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1171 | ASSERT_TRUE(annot); |
| 1172 | |
| 1173 | // Check that a non-existent key does not exist. |
| 1174 | EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none")); |
| 1175 | |
| 1176 | // 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] | 1177 | EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1178 | EXPECT_EQ(FPDF_OBJECT_REFERENCE, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1179 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP)); |
| 1180 | EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP, |
| 1181 | nullptr, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1182 | |
| 1183 | // Check that the string value of the hash is correct. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1184 | static const char kHashKey[] = "AAPL:Hash"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1185 | EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1186 | unsigned long length_bytes = |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1187 | FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1188 | ASSERT_EQ(66u, length_bytes); |
| 1189 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 1190 | EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), |
| 1191 | length_bytes)); |
| 1192 | EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad", |
| 1193 | GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1194 | |
| 1195 | // Check that the string value of the modified date is correct. |
| 1196 | EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1197 | length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, |
| 1198 | nullptr, 0); |
| 1199 | ASSERT_EQ(44u, length_bytes); |
| 1200 | buf = GetFPDFWideStringBuffer(length_bytes); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1201 | EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1202 | buf.data(), length_bytes)); |
| 1203 | EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1204 | |
| 1205 | // Update the date entry for the annotation. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1206 | ScopedFPDFWideString text = GetFPDFWideString(kNewDate); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1207 | EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM, |
| 1208 | text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1209 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1210 | |
| 1211 | // Save the document, closing the page and document. |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1212 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1213 | UnloadPage(page); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1214 | |
Lei Zhang | e4cdac5 | 2019-04-30 16:45:57 +0000 | [diff] [blame] | 1215 | #if defined(OS_MACOSX) |
Lei Zhang | 0f6b434 | 2020-02-25 20:00:39 +0000 | [diff] [blame] | 1216 | static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb"; |
Lei Zhang | e67bcc7 | 2019-04-30 18:55:58 +0000 | [diff] [blame] | 1217 | #elif defined(OS_WIN) |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1218 | static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1219 | #else |
Lei Zhang | a2b7073 | 2019-06-25 08:34:22 +0000 | [diff] [blame] | 1220 | static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1221 | #endif |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 1222 | |
| 1223 | // Open the saved annotation. |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1224 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1225 | page = LoadSavedPage(0); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1226 | VerifySavedRendering(page, 595, 842, kMd5); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1227 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1228 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0)); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1229 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1230 | // Check that the string value of the modified date is the newly-set value. |
| 1231 | EXPECT_EQ(FPDF_OBJECT_STRING, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1232 | FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM)); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1233 | unsigned long length_bytes = FPDFAnnot_GetStringValue( |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1234 | new_annot.get(), pdfium::annotation::kM, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1235 | ASSERT_EQ(44u, length_bytes); |
| 1236 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1237 | EXPECT_EQ(44u, |
| 1238 | FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1239 | buf.data(), length_bytes)); |
| 1240 | EXPECT_EQ(kNewDate, GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1241 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1242 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1243 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1244 | CloseSavedDocument(); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1245 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1246 | |
rycsmith | 3e78560 | 2019-03-05 21:48:36 +0000 | [diff] [blame] | 1247 | TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) { |
Mansi Awasthi | 0b5da67 | 2020-01-23 18:17:18 +0000 | [diff] [blame] | 1248 | // Open a file with four text annotations and load its first page. |
rycsmith | 3e78560 | 2019-03-05 21:48:36 +0000 | [diff] [blame] | 1249 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 1250 | FPDF_PAGE page = LoadPage(0); |
| 1251 | ASSERT_TRUE(page); |
| 1252 | { |
| 1253 | // First two annotations do not have "MaxLen" attribute. |
| 1254 | for (int i = 0; i < 2; i++) { |
| 1255 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i)); |
| 1256 | ASSERT_TRUE(annot); |
| 1257 | |
| 1258 | // Verify that no "MaxLen" key present. |
| 1259 | EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen")); |
| 1260 | |
| 1261 | float value; |
| 1262 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value)); |
| 1263 | } |
| 1264 | |
| 1265 | // Annotation in index 2 has "MaxLen" of 10. |
| 1266 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
| 1267 | ASSERT_TRUE(annot); |
| 1268 | |
| 1269 | // Verify that "MaxLen" key present. |
| 1270 | EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen")); |
| 1271 | |
| 1272 | float value; |
| 1273 | EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value)); |
| 1274 | EXPECT_FLOAT_EQ(10.0f, value); |
| 1275 | |
| 1276 | // Check bad inputs. |
| 1277 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value)); |
| 1278 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value)); |
| 1279 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr)); |
| 1280 | // Ask for key that exists but is not a number. |
| 1281 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value)); |
| 1282 | } |
| 1283 | |
| 1284 | UnloadPage(page); |
| 1285 | } |
| 1286 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1287 | TEST_F(FPDFAnnotEmbedderTest, GetSetAP) { |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1288 | // Open a file with four annotations and load its first page. |
| 1289 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1290 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1291 | ASSERT_TRUE(page); |
| 1292 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1293 | { |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1294 | static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a"; |
| 1295 | static constexpr size_t kExpectNormalAPLength = 73970; |
| 1296 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1297 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1298 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1299 | ASSERT_TRUE(annot); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1300 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1301 | // Check that the string value of an AP returns the expected length. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1302 | unsigned long normal_length_bytes = FPDFAnnot_GetAP( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1303 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1304 | ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1305 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1306 | // Check that the string value of an AP is not returned if the buffer is too |
| 1307 | // small. The result buffer should be overwritten with an empty string. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1308 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes); |
| 1309 | // Write in the buffer to verify it's not overwritten. |
| 1310 | memcpy(buf.data(), "abcdefgh", 8); |
| 1311 | EXPECT_EQ(kExpectNormalAPLength, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1312 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1313 | buf.data(), normal_length_bytes - 1)); |
| 1314 | EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8)); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1315 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1316 | // Check that the string value of an AP is returned through a buffer that is |
| 1317 | // the right size. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1318 | EXPECT_EQ(kExpectNormalAPLength, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1319 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1320 | buf.data(), normal_length_bytes)); |
| 1321 | EXPECT_EQ(kMd5NormalAP, |
| 1322 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), |
| 1323 | normal_length_bytes)); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1324 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1325 | // Check that the string value of an AP is returned through a buffer that is |
| 1326 | // larger than necessary. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1327 | buf = GetFPDFWideStringBuffer(normal_length_bytes + 2); |
| 1328 | EXPECT_EQ(kExpectNormalAPLength, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1329 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1330 | buf.data(), normal_length_bytes + 2)); |
| 1331 | EXPECT_EQ(kMd5NormalAP, |
| 1332 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), |
| 1333 | normal_length_bytes)); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1334 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1335 | // Check that getting an AP for a mode that does not have an AP returns an |
| 1336 | // empty string. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1337 | unsigned long rollover_length_bytes = FPDFAnnot_GetAP( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1338 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1339 | ASSERT_EQ(2u, rollover_length_bytes); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1340 | |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1341 | buf = GetFPDFWideStringBuffer(1000); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1342 | EXPECT_EQ(2u, |
| 1343 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1344 | buf.data(), 1000)); |
| 1345 | EXPECT_EQ(L"", GetPlatformWString(buf.data())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1346 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1347 | // Check that setting the AP for an invalid appearance mode fails. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1348 | ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap"); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1349 | EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1350 | EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1351 | ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1352 | EXPECT_FALSE(FPDFAnnot_SetAP( |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1353 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1354 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1355 | // Set the AP correctly now. |
| 1356 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1357 | ap_text.get())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1358 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1359 | // 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] | 1360 | rollover_length_bytes = FPDFAnnot_GetAP( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1361 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1362 | ASSERT_EQ(24u, rollover_length_bytes); |
| 1363 | buf = GetFPDFWideStringBuffer(rollover_length_bytes); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1364 | EXPECT_EQ(24u, |
| 1365 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1366 | buf.data(), rollover_length_bytes)); |
| 1367 | EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1368 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1369 | // 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] | 1370 | buf = GetFPDFWideStringBuffer(normal_length_bytes); |
| 1371 | EXPECT_EQ(kExpectNormalAPLength, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1372 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1373 | buf.data(), normal_length_bytes)); |
| 1374 | EXPECT_EQ(kMd5NormalAP, |
| 1375 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), |
| 1376 | normal_length_bytes)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1377 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1378 | |
| 1379 | // Save the modified document, then reopen it. |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1380 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1381 | UnloadPage(page); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1382 | |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1383 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1384 | page = LoadSavedPage(0); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1385 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1386 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1387 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1388 | // Check that the new annotation value is equal to the value we set before |
| 1389 | // saving. |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1390 | unsigned long rollover_length_bytes = FPDFAnnot_GetAP( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1391 | new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1392 | ASSERT_EQ(24u, rollover_length_bytes); |
| 1393 | std::vector<FPDF_WCHAR> buf = |
| 1394 | GetFPDFWideStringBuffer(rollover_length_bytes); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1395 | EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(), |
| 1396 | FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1397 | buf.data(), rollover_length_bytes)); |
| 1398 | EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1399 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1400 | |
| 1401 | // Close saved document. |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1402 | CloseSavedPage(page); |
| 1403 | CloseSavedDocument(); |
| 1404 | } |
| 1405 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1406 | TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) { |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1407 | // Open a file with four annotations and load its first page. |
| 1408 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1409 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1410 | ASSERT_TRUE(page); |
| 1411 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1412 | { |
| 1413 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1414 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1415 | ASSERT_TRUE(annot); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1416 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1417 | // Set Down AP. Normal AP is already set. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1418 | ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap"); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1419 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1420 | ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1421 | EXPECT_EQ(73970u, |
| 1422 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1423 | nullptr, 0)); |
| 1424 | EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1425 | nullptr, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1426 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1427 | // Check that setting the Down AP to null removes the Down entry but keeps |
| 1428 | // Normal intact. |
| 1429 | EXPECT_TRUE( |
| 1430 | FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr)); |
| 1431 | EXPECT_EQ(73970u, |
| 1432 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1433 | nullptr, 0)); |
| 1434 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1435 | nullptr, 0)); |
| 1436 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1437 | |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1438 | UnloadPage(page); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1441 | TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) { |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1442 | // Open a file with four annotations and load its first page. |
| 1443 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1444 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1445 | ASSERT_TRUE(page); |
| 1446 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1447 | { |
| 1448 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1449 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1450 | ASSERT_TRUE(annot); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1451 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1452 | // Set Down AP. Normal AP is already set. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1453 | ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap"); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1454 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1455 | ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1456 | EXPECT_EQ(73970u, |
| 1457 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1458 | nullptr, 0)); |
| 1459 | EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1460 | nullptr, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1461 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1462 | // Check that setting the Normal AP to null removes the whole AP dictionary. |
| 1463 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1464 | nullptr)); |
| 1465 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1466 | nullptr, 0)); |
| 1467 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1468 | nullptr, 0)); |
| 1469 | } |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1470 | |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1471 | UnloadPage(page); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1474 | TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) { |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1475 | // Open a file with annotations and load its first page. |
| 1476 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1477 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1478 | ASSERT_TRUE(page); |
Jane Liu | d1ed1ce | 2017-08-24 12:31:10 -0400 | [diff] [blame] | 1479 | EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1480 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1481 | { |
| 1482 | // Retrieve the highlight annotation which has its popup defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1483 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1484 | ASSERT_TRUE(annot); |
| 1485 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 1486 | EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get())); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1487 | static const char kPopupKey[] = "Popup"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1488 | ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey)); |
| 1489 | ASSERT_EQ(FPDF_OBJECT_REFERENCE, |
| 1490 | FPDFAnnot_GetValueType(annot.get(), kPopupKey)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1491 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1492 | // Retrieve and verify the popup of the highlight annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1493 | ScopedFPDFAnnotation popup( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1494 | FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey)); |
| 1495 | ASSERT_TRUE(popup); |
| 1496 | EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get())); |
| 1497 | EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get())); |
| 1498 | FS_RECTF rect; |
| 1499 | ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect)); |
| 1500 | EXPECT_NEAR(612.0f, rect.left, 0.001f); |
| 1501 | EXPECT_NEAR(578.792, rect.bottom, 0.001f); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1502 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1503 | // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail, |
| 1504 | // since "IRT" is not a key in |annot|'s dictionary. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1505 | static const char kIRTKey[] = "IRT"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1506 | ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey)); |
| 1507 | EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1508 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1509 | // Attempting to retrieve |annot|'s parent dictionary as an annotation |
| 1510 | // would fail, since its parent is not an annotation. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1511 | ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1512 | EXPECT_EQ(FPDF_OBJECT_REFERENCE, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1513 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP)); |
| 1514 | EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1515 | } |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1516 | |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1517 | UnloadPage(page); |
| 1518 | } |
| 1519 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1520 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) { |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1521 | // Open file with form text fields. |
| 1522 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1523 | FPDF_PAGE page = LoadPage(0); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1524 | ASSERT_TRUE(page); |
| 1525 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1526 | { |
| 1527 | // Retrieve the first annotation: user-editable text field. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1528 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1529 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1530 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1531 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1532 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1533 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
Mansi Awasthi | 0b5da67 | 2020-01-23 18:17:18 +0000 | [diff] [blame] | 1534 | EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1535 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1536 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1537 | { |
| 1538 | // Retrieve the second annotation: read-only text field. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1539 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1540 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1541 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1542 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1543 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1544 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
Mansi Awasthi | 0b5da67 | 2020-01-23 18:17:18 +0000 | [diff] [blame] | 1545 | EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD); |
| 1546 | } |
| 1547 | |
| 1548 | { |
| 1549 | // Retrieve the fourth annotation: user-editable password text field. |
| 1550 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3)); |
| 1551 | ASSERT_TRUE(annot); |
| 1552 | |
| 1553 | // Check that the flag values are as expected. |
| 1554 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
| 1555 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1556 | EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1557 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1558 | |
| 1559 | UnloadPage(page); |
| 1560 | } |
| 1561 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1562 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) { |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1563 | // Open file with form text fields. |
| 1564 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1565 | FPDF_PAGE page = LoadPage(0); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1566 | ASSERT_TRUE(page); |
| 1567 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1568 | { |
| 1569 | // Retrieve the first annotation: user-editable combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1570 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1571 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1572 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1573 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1574 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1575 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1576 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1577 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1578 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1579 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1580 | { |
| 1581 | // Retrieve the second annotation: regular combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1582 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1583 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1584 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1585 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1586 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1587 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1588 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1589 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1590 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1591 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1592 | { |
| 1593 | // Retrieve the third annotation: read-only combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1594 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1595 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1596 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1597 | // Check that the flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1598 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1599 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1600 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1601 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1602 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1603 | |
| 1604 | UnloadPage(page); |
| 1605 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1606 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1607 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1608 | // Open file with form text fields. |
| 1609 | EXPECT_TRUE(OpenDocument("text_form.pdf")); |
| 1610 | FPDF_PAGE page = LoadPage(0); |
| 1611 | ASSERT_TRUE(page); |
| 1612 | |
| 1613 | // Attempt to get an annotation where no annotation exists on page. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1614 | static const FS_POINTF kOriginPoint = {0.0f, 0.0f}; |
| 1615 | EXPECT_FALSE( |
| 1616 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint)); |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 1617 | |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1618 | static const FS_POINTF kValidPoint = {120.0f, 120.0f}; |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 1619 | { |
| 1620 | // Verify there is an annotation. |
| 1621 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1622 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint)); |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 1623 | EXPECT_TRUE(annot); |
| 1624 | } |
| 1625 | |
| 1626 | // Try other bad inputs at a valid location. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1627 | EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint)); |
| 1628 | EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint)); |
| 1629 | EXPECT_FALSE( |
| 1630 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint)); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1631 | |
| 1632 | UnloadPage(page); |
| 1633 | } |
| 1634 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1635 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1636 | // Open file with form text fields. |
| 1637 | EXPECT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 1638 | FPDF_PAGE page = LoadPage(0); |
| 1639 | ASSERT_TRUE(page); |
| 1640 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1641 | { |
| 1642 | // Retrieve user-editable text field annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1643 | static const FS_POINTF kPoint = {105.0f, 118.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1644 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1645 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1646 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1647 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1648 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1649 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1650 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1651 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1652 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1653 | { |
| 1654 | // Retrieve read-only text field annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1655 | static const FS_POINTF kPoint = {105.0f, 202.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1656 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1657 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1658 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1659 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1660 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1661 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1662 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1663 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1664 | |
| 1665 | UnloadPage(page); |
| 1666 | } |
| 1667 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1668 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1669 | // Open file with form comboboxes. |
| 1670 | EXPECT_TRUE(OpenDocument("combobox_form.pdf")); |
| 1671 | FPDF_PAGE page = LoadPage(0); |
| 1672 | ASSERT_TRUE(page); |
| 1673 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1674 | { |
| 1675 | // Retrieve user-editable combobox annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1676 | static const FS_POINTF kPoint = {102.0f, 363.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1677 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1678 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1679 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1680 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1681 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1682 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1683 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1684 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1685 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1686 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1687 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1688 | { |
| 1689 | // Retrieve regular combobox annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1690 | static const FS_POINTF kPoint = {102.0f, 413.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1691 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1692 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1693 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1694 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1695 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1696 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1697 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1698 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1699 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1700 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1701 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1702 | { |
| 1703 | // Retrieve read-only combobox annotation. |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1704 | static const FS_POINTF kPoint = {102.0f, 513.0f}; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1705 | ScopedFPDFAnnotation annot( |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 1706 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1707 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1708 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1709 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | a9d33bd | 2019-07-31 05:37:31 +0000 | [diff] [blame] | 1710 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1711 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1712 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1713 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1714 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1715 | |
| 1716 | UnloadPage(page); |
| 1717 | } |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1718 | |
Lei Zhang | 03e5e68 | 2019-09-16 19:45:55 +0000 | [diff] [blame] | 1719 | // TODO(crbug.com/pdfium/11): Fix this test and enable. |
| 1720 | #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_) |
| 1721 | #define MAYBE_BUG_1206 DISABLED_BUG_1206 |
| 1722 | #else |
| 1723 | #define MAYBE_BUG_1206 BUG_1206 |
| 1724 | #endif |
| 1725 | TEST_F(FPDFAnnotEmbedderTest, MAYBE_BUG_1206) { |
Lei Zhang | 992e7e2 | 2019-02-04 19:20:58 +0000 | [diff] [blame] | 1726 | static constexpr size_t kExpectedSize = 1609; |
| 1727 | static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc"; |
| 1728 | |
| 1729 | ASSERT_TRUE(OpenDocument("bug_1206.pdf")); |
| 1730 | |
| 1731 | FPDF_PAGE page = LoadPage(0); |
| 1732 | ASSERT_TRUE(page); |
| 1733 | |
| 1734 | ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 1735 | EXPECT_EQ(kExpectedSize, GetString().size()); |
| 1736 | ClearString(); |
| 1737 | |
| 1738 | for (size_t i = 0; i < 10; ++i) { |
| 1739 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 1740 | CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap); |
| 1741 | |
| 1742 | ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 1743 | // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be |
| 1744 | // equal, not bigger. |
| 1745 | EXPECT_LT(kExpectedSize, GetString().size()); |
| 1746 | ClearString(); |
| 1747 | } |
| 1748 | |
| 1749 | UnloadPage(page); |
| 1750 | } |
| 1751 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1752 | TEST_F(FPDFAnnotEmbedderTest, BUG_1212) { |
| 1753 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| 1754 | FPDF_PAGE page = LoadPage(0); |
| 1755 | ASSERT_TRUE(page); |
| 1756 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(page)); |
| 1757 | |
| 1758 | static const char kTestKey[] = "test"; |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1759 | static const wchar_t kData[] = L"\xf6\xe4"; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1760 | static const size_t kBufSize = 12; |
| 1761 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1762 | |
| 1763 | { |
| 1764 | // Add a text annotation to the page. |
| 1765 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT)); |
| 1766 | ASSERT_TRUE(annot); |
| 1767 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 1768 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 1769 | |
| 1770 | // Make sure there is no test key, add set a value there, and read it back. |
| 1771 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1772 | ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1773 | kBufSize)); |
| 1774 | EXPECT_EQ(L"", GetPlatformWString(buf.data())); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1775 | |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1776 | ScopedFPDFWideString text = GetFPDFWideString(kData); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1777 | EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get())); |
| 1778 | |
| 1779 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1780 | ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1781 | kBufSize)); |
| 1782 | EXPECT_EQ(kData, GetPlatformWString(buf.data())); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1785 | { |
| 1786 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
| 1787 | ASSERT_TRUE(annot); |
Shikha Walia | 87ad417 | 2019-11-08 20:55:19 +0000 | [diff] [blame] | 1788 | 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] | 1789 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect)); |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1790 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 1791 | EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get())); |
| 1792 | // Also do the same test for its appearance string. |
| 1793 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1794 | ASSERT_EQ(2u, |
| 1795 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1796 | buf.data(), kBufSize)); |
| 1797 | EXPECT_EQ(L"", GetPlatformWString(buf.data())); |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1798 | |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 1799 | ScopedFPDFWideString text = GetFPDFWideString(kData); |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1800 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 1801 | text.get())); |
| 1802 | |
| 1803 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1804 | ASSERT_EQ(6u, |
| 1805 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1806 | buf.data(), kBufSize)); |
| 1807 | EXPECT_EQ(kData, GetPlatformWString(buf.data())); |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1810 | UnloadPage(page); |
| 1811 | |
| 1812 | { |
| 1813 | // Save a copy, open the copy, and check the annotation again. |
| 1814 | // Note that it renders the rotation. |
| 1815 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1816 | ASSERT_TRUE(OpenSavedDocument()); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1817 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 1818 | ASSERT_TRUE(saved_page); |
| 1819 | |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1820 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page)); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1821 | { |
| 1822 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0)); |
| 1823 | ASSERT_TRUE(annot); |
| 1824 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 1825 | |
| 1826 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1827 | ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1828 | kBufSize)); |
| 1829 | EXPECT_EQ(kData, GetPlatformWString(buf.data())); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1832 | { |
| 1833 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0)); |
| 1834 | ASSERT_TRUE(annot); |
| 1835 | // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason. |
| 1836 | // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 1837 | |
| 1838 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1839 | ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1840 | kBufSize)); |
| 1841 | EXPECT_EQ(kData, GetPlatformWString(buf.data())); |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1844 | CloseSavedPage(saved_page); |
| 1845 | CloseSavedDocument(); |
| 1846 | } |
| 1847 | } |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1848 | |
| 1849 | TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) { |
| 1850 | // Open a file with combobox widget annotations and load its first page. |
| 1851 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 1852 | FPDF_PAGE page = LoadPage(0); |
| 1853 | ASSERT_TRUE(page); |
| 1854 | |
| 1855 | { |
| 1856 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 1857 | ASSERT_TRUE(annot); |
| 1858 | |
| 1859 | EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 1860 | |
| 1861 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 1862 | ASSERT_TRUE(annot); |
| 1863 | |
| 1864 | EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
Lei Zhang | e7033c8 | 2019-02-26 19:30:49 +0000 | [diff] [blame] | 1865 | |
| 1866 | // Check bad form handle / annot. |
| 1867 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr)); |
| 1868 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr)); |
| 1869 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | UnloadPage(page); |
| 1873 | } |
| 1874 | |
| 1875 | TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) { |
| 1876 | // Open a file with listbox widget annotations and load its first page. |
| 1877 | ASSERT_TRUE(OpenDocument("listbox_form.pdf")); |
| 1878 | FPDF_PAGE page = LoadPage(0); |
| 1879 | ASSERT_TRUE(page); |
| 1880 | |
| 1881 | { |
| 1882 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 1883 | ASSERT_TRUE(annot); |
| 1884 | |
| 1885 | EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 1886 | |
| 1887 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 1888 | ASSERT_TRUE(annot); |
| 1889 | |
| 1890 | EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 1891 | } |
| 1892 | |
| 1893 | UnloadPage(page); |
| 1894 | } |
| 1895 | |
| 1896 | TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) { |
| 1897 | // Open a file with ink annotations and load its first page. |
| 1898 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 1899 | FPDF_PAGE page = LoadPage(0); |
| 1900 | ASSERT_TRUE(page); |
| 1901 | |
| 1902 | { |
| 1903 | // annotations do not have "Opt" array and will return -1 |
| 1904 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 1905 | ASSERT_TRUE(annot); |
| 1906 | |
| 1907 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 1908 | |
| 1909 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 1910 | ASSERT_TRUE(annot); |
| 1911 | |
| 1912 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 1913 | } |
| 1914 | |
| 1915 | UnloadPage(page); |
| 1916 | } |
| 1917 | |
| 1918 | TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) { |
| 1919 | // Open a file with combobox widget annotations and load its first page. |
| 1920 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 1921 | FPDF_PAGE page = LoadPage(0); |
| 1922 | ASSERT_TRUE(page); |
| 1923 | |
| 1924 | { |
| 1925 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 1926 | ASSERT_TRUE(annot); |
| 1927 | |
| 1928 | int index = 0; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1929 | unsigned long length_bytes = |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1930 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1931 | ASSERT_EQ(8u, length_bytes); |
| 1932 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1933 | EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1934 | buf.data(), length_bytes)); |
| 1935 | EXPECT_EQ(L"Foo", GetPlatformWString(buf.data())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1936 | |
| 1937 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 1938 | ASSERT_TRUE(annot); |
| 1939 | |
| 1940 | index = 0; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1941 | length_bytes = |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1942 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1943 | ASSERT_EQ(12u, length_bytes); |
| 1944 | buf = GetFPDFWideStringBuffer(length_bytes); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1945 | EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1946 | buf.data(), length_bytes)); |
| 1947 | EXPECT_EQ(L"Apple", GetPlatformWString(buf.data())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1948 | |
| 1949 | index = 25; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1950 | length_bytes = |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1951 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1952 | buf = GetFPDFWideStringBuffer(length_bytes); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1953 | EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1954 | buf.data(), length_bytes)); |
| 1955 | EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1956 | |
Lei Zhang | e7033c8 | 2019-02-26 19:30:49 +0000 | [diff] [blame] | 1957 | // Indices out of range |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1958 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1, |
| 1959 | nullptr, 0)); |
| 1960 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26, |
| 1961 | nullptr, 0)); |
Lei Zhang | e7033c8 | 2019-02-26 19:30:49 +0000 | [diff] [blame] | 1962 | |
| 1963 | // Check bad form handle / annot. |
| 1964 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0)); |
| 1965 | EXPECT_EQ(0u, |
| 1966 | FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0)); |
| 1967 | EXPECT_EQ(0u, |
| 1968 | FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0)); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | UnloadPage(page); |
| 1972 | } |
| 1973 | |
| 1974 | TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) { |
| 1975 | // Open a file with listbox widget annotations and load its first page. |
| 1976 | ASSERT_TRUE(OpenDocument("listbox_form.pdf")); |
| 1977 | FPDF_PAGE page = LoadPage(0); |
| 1978 | ASSERT_TRUE(page); |
| 1979 | |
| 1980 | { |
| 1981 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 1982 | ASSERT_TRUE(annot); |
| 1983 | |
| 1984 | int index = 0; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1985 | unsigned long length_bytes = |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1986 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1987 | ASSERT_EQ(8u, length_bytes); |
| 1988 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1989 | EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1990 | buf.data(), length_bytes)); |
| 1991 | EXPECT_EQ(L"Foo", GetPlatformWString(buf.data())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1992 | |
| 1993 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 1994 | ASSERT_TRUE(annot); |
| 1995 | |
| 1996 | index = 0; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1997 | length_bytes = |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1998 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 1999 | ASSERT_EQ(12u, length_bytes); |
| 2000 | buf = GetFPDFWideStringBuffer(length_bytes); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2001 | EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2002 | buf.data(), length_bytes)); |
| 2003 | EXPECT_EQ(L"Apple", GetPlatformWString(buf.data())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2004 | |
| 2005 | index = 25; |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2006 | length_bytes = |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2007 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2008 | ASSERT_EQ(18u, length_bytes); |
| 2009 | buf = GetFPDFWideStringBuffer(length_bytes); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2010 | EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 2011 | buf.data(), length_bytes)); |
| 2012 | EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 2013 | |
| 2014 | // indices out of range |
| 2015 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1, |
| 2016 | nullptr, 0)); |
| 2017 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26, |
| 2018 | nullptr, 0)); |
| 2019 | } |
| 2020 | |
| 2021 | UnloadPage(page); |
| 2022 | } |
| 2023 | |
| 2024 | TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) { |
| 2025 | // Open a file with ink annotations and load its first page. |
| 2026 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 2027 | FPDF_PAGE page = LoadPage(0); |
| 2028 | ASSERT_TRUE(page); |
| 2029 | |
| 2030 | { |
| 2031 | // annotations do not have "Opt" array and will return 0 |
| 2032 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2033 | ASSERT_TRUE(annot); |
| 2034 | |
| 2035 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0, |
| 2036 | nullptr, 0)); |
| 2037 | |
| 2038 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2039 | ASSERT_TRUE(annot); |
| 2040 | |
| 2041 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0, |
| 2042 | nullptr, 0)); |
| 2043 | } |
| 2044 | |
| 2045 | UnloadPage(page); |
| 2046 | } |
Ryan Smith | 09c23b1 | 2019-04-25 18:09:06 +0000 | [diff] [blame] | 2047 | |
| 2048 | TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) { |
| 2049 | // Open a file with combobox annotations and load its first page. |
| 2050 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 2051 | FPDF_PAGE page = LoadPage(0); |
| 2052 | ASSERT_TRUE(page); |
| 2053 | |
| 2054 | { |
| 2055 | // All 3 widgets have Tf font size 12. |
| 2056 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2057 | ASSERT_TRUE(annot); |
| 2058 | |
| 2059 | float value; |
| 2060 | ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value)); |
| 2061 | EXPECT_EQ(12.0, value); |
| 2062 | |
| 2063 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2064 | ASSERT_TRUE(annot); |
| 2065 | |
| 2066 | float value_two; |
| 2067 | ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two)); |
| 2068 | EXPECT_EQ(12.0, value_two); |
| 2069 | |
| 2070 | annot.reset(FPDFPage_GetAnnot(page, 2)); |
| 2071 | ASSERT_TRUE(annot); |
| 2072 | |
| 2073 | float value_three; |
| 2074 | ASSERT_TRUE( |
| 2075 | FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three)); |
| 2076 | EXPECT_EQ(12.0, value_three); |
| 2077 | } |
| 2078 | |
| 2079 | UnloadPage(page); |
| 2080 | } |
| 2081 | |
| 2082 | TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) { |
| 2083 | // Open a file with textfield annotations and load its first page. |
| 2084 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 2085 | FPDF_PAGE page = LoadPage(0); |
| 2086 | ASSERT_TRUE(page); |
| 2087 | |
| 2088 | { |
Mansi Awasthi | 0b5da67 | 2020-01-23 18:17:18 +0000 | [diff] [blame] | 2089 | // All 4 widgets have Tf font size 12. |
Ryan Smith | 09c23b1 | 2019-04-25 18:09:06 +0000 | [diff] [blame] | 2090 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2091 | ASSERT_TRUE(annot); |
| 2092 | |
| 2093 | float value; |
| 2094 | ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value)); |
| 2095 | EXPECT_EQ(12.0, value); |
| 2096 | |
| 2097 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2098 | ASSERT_TRUE(annot); |
| 2099 | |
| 2100 | float value_two; |
| 2101 | ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two)); |
| 2102 | EXPECT_EQ(12.0, value_two); |
| 2103 | |
| 2104 | annot.reset(FPDFPage_GetAnnot(page, 2)); |
| 2105 | ASSERT_TRUE(annot); |
| 2106 | |
| 2107 | float value_three; |
| 2108 | ASSERT_TRUE( |
| 2109 | FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three)); |
| 2110 | EXPECT_EQ(12.0, value_three); |
Mansi Awasthi | 0b5da67 | 2020-01-23 18:17:18 +0000 | [diff] [blame] | 2111 | |
| 2112 | float value_four; |
| 2113 | ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four)); |
| 2114 | EXPECT_EQ(12.0, value_four); |
Ryan Smith | 09c23b1 | 2019-04-25 18:09:06 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
| 2117 | UnloadPage(page); |
| 2118 | } |
| 2119 | |
| 2120 | TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) { |
| 2121 | // Open a file with ink annotations and load its first page. |
| 2122 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 2123 | FPDF_PAGE page = LoadPage(0); |
| 2124 | ASSERT_TRUE(page); |
| 2125 | |
| 2126 | { |
| 2127 | // Annotations that do not have variable text and will return -1. |
| 2128 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2129 | ASSERT_TRUE(annot); |
| 2130 | |
| 2131 | float value; |
| 2132 | ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value)); |
| 2133 | |
| 2134 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 2135 | ASSERT_TRUE(annot); |
| 2136 | |
| 2137 | ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value)); |
| 2138 | } |
| 2139 | |
| 2140 | UnloadPage(page); |
| 2141 | } |
| 2142 | |
| 2143 | TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) { |
| 2144 | // Open a file with combobox annotations and load its first page. |
| 2145 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 2146 | FPDF_PAGE page = LoadPage(0); |
| 2147 | ASSERT_TRUE(page); |
| 2148 | |
| 2149 | { |
| 2150 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2151 | ASSERT_TRUE(annot); |
| 2152 | |
| 2153 | // Check bad form handle / annot. |
| 2154 | float value; |
| 2155 | ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value)); |
| 2156 | ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value)); |
| 2157 | ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value)); |
| 2158 | } |
| 2159 | |
| 2160 | UnloadPage(page); |
| 2161 | } |
| 2162 | |
| 2163 | TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) { |
| 2164 | // Open a file with textfield annotations and load its first page. |
| 2165 | ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf")); |
| 2166 | FPDF_PAGE page = LoadPage(0); |
| 2167 | ASSERT_TRUE(page); |
| 2168 | |
| 2169 | { |
| 2170 | // Obtain the first annotation, a text field with negative font size, -12. |
| 2171 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2172 | ASSERT_TRUE(annot); |
| 2173 | |
| 2174 | float value; |
| 2175 | ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value)); |
| 2176 | EXPECT_EQ(-12.0, value); |
| 2177 | } |
| 2178 | |
| 2179 | UnloadPage(page); |
| 2180 | } |
Ryan Smith | 23fdf89 | 2019-07-17 21:51:26 +0000 | [diff] [blame] | 2181 | |
| 2182 | TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) { |
| 2183 | // Open a file with checkbox and radiobuttons widget annotations and load its |
| 2184 | // first page. |
| 2185 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 2186 | FPDF_PAGE page = LoadPage(0); |
| 2187 | ASSERT_TRUE(page); |
| 2188 | |
| 2189 | { |
| 2190 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
| 2191 | ASSERT_TRUE(annot); |
| 2192 | ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2193 | } |
| 2194 | |
| 2195 | UnloadPage(page); |
| 2196 | } |
| 2197 | |
| 2198 | TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) { |
| 2199 | // Open a file with checkbox and radiobutton widget annotations and load its |
| 2200 | // first page. |
| 2201 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 2202 | FPDF_PAGE page = LoadPage(0); |
| 2203 | ASSERT_TRUE(page); |
| 2204 | |
| 2205 | { |
| 2206 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2207 | ASSERT_TRUE(annot); |
| 2208 | ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2209 | } |
| 2210 | |
| 2211 | UnloadPage(page); |
| 2212 | } |
| 2213 | |
| 2214 | TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) { |
| 2215 | // Open a file with checkbox and radiobutton widget annotations and load its |
| 2216 | // first page. |
| 2217 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 2218 | FPDF_PAGE page = LoadPage(0); |
| 2219 | ASSERT_TRUE(page); |
| 2220 | |
| 2221 | { |
| 2222 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5)); |
| 2223 | ASSERT_TRUE(annot); |
| 2224 | ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2225 | |
| 2226 | annot.reset(FPDFPage_GetAnnot(page, 6)); |
| 2227 | ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2228 | |
| 2229 | annot.reset(FPDFPage_GetAnnot(page, 7)); |
| 2230 | ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2231 | } |
| 2232 | |
| 2233 | UnloadPage(page); |
| 2234 | } |
| 2235 | |
| 2236 | TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) { |
| 2237 | // Open a file with checkbox and radiobutton widget annotations and load its |
| 2238 | // first page. |
| 2239 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 2240 | FPDF_PAGE page = LoadPage(0); |
| 2241 | ASSERT_TRUE(page); |
| 2242 | |
| 2243 | { |
| 2244 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
| 2245 | ASSERT_TRUE(annot); |
| 2246 | ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2247 | |
| 2248 | annot.reset(FPDFPage_GetAnnot(page, 3)); |
| 2249 | ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2250 | |
| 2251 | annot.reset(FPDFPage_GetAnnot(page, 4)); |
| 2252 | ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2253 | } |
| 2254 | |
| 2255 | UnloadPage(page); |
| 2256 | } |
| 2257 | |
| 2258 | TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) { |
| 2259 | // Open a file with checkbox and radiobuttons widget annotations and load its |
| 2260 | // first page. |
| 2261 | ASSERT_TRUE(OpenDocument("click_form.pdf")); |
| 2262 | FPDF_PAGE page = LoadPage(0); |
| 2263 | ASSERT_TRUE(page); |
| 2264 | |
| 2265 | { |
| 2266 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2267 | ASSERT_TRUE(annot); |
| 2268 | ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get())); |
| 2269 | ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr)); |
| 2270 | ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr)); |
| 2271 | } |
| 2272 | |
| 2273 | UnloadPage(page); |
| 2274 | } |
| 2275 | |
| 2276 | TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) { |
| 2277 | // Open a file with text widget annotations and load its first page. |
| 2278 | ASSERT_TRUE(OpenDocument("text_form.pdf")); |
| 2279 | FPDF_PAGE page = LoadPage(0); |
| 2280 | ASSERT_TRUE(page); |
| 2281 | |
| 2282 | { |
| 2283 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2284 | ASSERT_TRUE(annot); |
| 2285 | ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get())); |
| 2286 | } |
| 2287 | |
| 2288 | UnloadPage(page); |
| 2289 | } |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2290 | |
Ankit Kumar | 8c7e4ad | 2020-02-07 08:56:49 +0000 | [diff] [blame] | 2291 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) { |
| 2292 | ASSERT_TRUE(OpenDocument("multiple_form_types.pdf")); |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2293 | FPDF_PAGE page = LoadPage(0); |
| 2294 | ASSERT_TRUE(page); |
| 2295 | |
Ankit Kumar | 8c7e4ad | 2020-02-07 08:56:49 +0000 | [diff] [blame] | 2296 | EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr)); |
| 2297 | |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2298 | { |
Ankit Kumar | 8c7e4ad | 2020-02-07 08:56:49 +0000 | [diff] [blame] | 2299 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2300 | ASSERT_TRUE(annot); |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2301 | EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get())); |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2302 | } |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2303 | |
Ankit Kumar | 8c7e4ad | 2020-02-07 08:56:49 +0000 | [diff] [blame] | 2304 | constexpr int kExpectedAnnotTypes[] = {-1, |
| 2305 | FPDF_FORMFIELD_COMBOBOX, |
| 2306 | FPDF_FORMFIELD_LISTBOX, |
| 2307 | FPDF_FORMFIELD_TEXTFIELD, |
| 2308 | FPDF_FORMFIELD_CHECKBOX, |
| 2309 | FPDF_FORMFIELD_RADIOBUTTON}; |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2310 | |
Ankit Kumar | 8c7e4ad | 2020-02-07 08:56:49 +0000 | [diff] [blame] | 2311 | for (size_t i = 0; i < FX_ArraySize(kExpectedAnnotTypes); ++i) { |
| 2312 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i)); |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2313 | ASSERT_TRUE(annot); |
Ankit Kumar | 8c7e4ad | 2020-02-07 08:56:49 +0000 | [diff] [blame] | 2314 | EXPECT_EQ(kExpectedAnnotTypes[i], |
Mansi Awasthi | 07bf7e6 | 2020-01-24 10:34:17 +0000 | [diff] [blame] | 2315 | FPDFAnnot_GetFormFieldType(form_handle(), annot.get())); |
| 2316 | } |
| 2317 | UnloadPage(page); |
| 2318 | } |
| 2319 | |
| 2320 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) { |
| 2321 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 2322 | FPDF_PAGE page = LoadPage(0); |
| 2323 | ASSERT_TRUE(page); |
| 2324 | |
| 2325 | { |
| 2326 | EXPECT_EQ(0u, |
| 2327 | FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0)); |
| 2328 | |
| 2329 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2330 | ASSERT_TRUE(annot); |
| 2331 | |
| 2332 | EXPECT_EQ(0u, |
| 2333 | FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0)); |
| 2334 | |
| 2335 | unsigned long length_bytes = |
| 2336 | FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0); |
| 2337 | ASSERT_EQ(2u, length_bytes); |
| 2338 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 2339 | EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), |
| 2340 | buf.data(), length_bytes)); |
| 2341 | EXPECT_EQ(L"", GetPlatformWString(buf.data())); |
| 2342 | } |
| 2343 | { |
| 2344 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
| 2345 | ASSERT_TRUE(annot); |
| 2346 | |
| 2347 | unsigned long length_bytes = |
| 2348 | FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0); |
| 2349 | ASSERT_EQ(18u, length_bytes); |
| 2350 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 2351 | EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), |
| 2352 | buf.data(), length_bytes)); |
| 2353 | EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data())); |
| 2354 | } |
| 2355 | UnloadPage(page); |
| 2356 | } |
| 2357 | |
| 2358 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) { |
| 2359 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 2360 | FPDF_PAGE page = LoadPage(0); |
| 2361 | ASSERT_TRUE(page); |
| 2362 | |
| 2363 | { |
| 2364 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2365 | ASSERT_TRUE(annot); |
| 2366 | |
| 2367 | unsigned long length_bytes = |
| 2368 | FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0); |
| 2369 | ASSERT_EQ(2u, length_bytes); |
| 2370 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 2371 | EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), |
| 2372 | buf.data(), length_bytes)); |
| 2373 | EXPECT_EQ(L"", GetPlatformWString(buf.data())); |
| 2374 | } |
| 2375 | { |
| 2376 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
| 2377 | ASSERT_TRUE(annot); |
| 2378 | |
| 2379 | unsigned long length_bytes = |
| 2380 | FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0); |
| 2381 | ASSERT_EQ(14u, length_bytes); |
| 2382 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 2383 | EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), |
| 2384 | buf.data(), length_bytes)); |
| 2385 | EXPECT_EQ(L"Banana", GetPlatformWString(buf.data())); |
| 2386 | } |
| 2387 | UnloadPage(page); |
| 2388 | } |
| 2389 | |
| 2390 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) { |
| 2391 | ASSERT_TRUE(OpenDocument("text_form.pdf")); |
| 2392 | FPDF_PAGE page = LoadPage(0); |
| 2393 | ASSERT_TRUE(page); |
| 2394 | |
| 2395 | { |
| 2396 | EXPECT_EQ(0u, |
| 2397 | FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0)); |
| 2398 | |
| 2399 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2400 | ASSERT_TRUE(annot); |
| 2401 | |
| 2402 | EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0)); |
| 2403 | |
| 2404 | unsigned long length_bytes = |
| 2405 | FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0); |
| 2406 | ASSERT_EQ(18u, length_bytes); |
| 2407 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 2408 | EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), |
| 2409 | buf.data(), length_bytes)); |
| 2410 | EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data())); |
| 2411 | } |
| 2412 | UnloadPage(page); |
| 2413 | } |
| 2414 | |
| 2415 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) { |
| 2416 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 2417 | FPDF_PAGE page = LoadPage(0); |
| 2418 | ASSERT_TRUE(page); |
| 2419 | |
| 2420 | { |
| 2421 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2422 | ASSERT_TRUE(annot); |
| 2423 | |
| 2424 | unsigned long length_bytes = |
| 2425 | FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0); |
| 2426 | ASSERT_EQ(30u, length_bytes); |
| 2427 | std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes); |
| 2428 | EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), |
| 2429 | buf.data(), length_bytes)); |
| 2430 | EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data())); |
| 2431 | } |
| 2432 | UnloadPage(page); |
| 2433 | } |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2434 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2435 | TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) { |
| 2436 | ASSERT_TRUE(OpenDocument("annots.pdf")); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2437 | FPDF_PAGE page = LoadPage(0); |
| 2438 | ASSERT_TRUE(page); |
| 2439 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2440 | // Verify widgets are by default focusable. |
| 2441 | const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET}; |
| 2442 | VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2443 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2444 | // Expected annot subtypes for page 0 of annots.pdf. |
| 2445 | const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = { |
| 2446 | FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, |
| 2447 | FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT, |
| 2448 | FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET, |
| 2449 | }; |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2450 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2451 | const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = { |
Ankit Kumar | 69cab67 | 2020-04-20 19:50:41 +0000 | [diff] [blame] | 2452 | FPDF_ANNOT_WIDGET}; |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2453 | VerifyAnnotationSubtypesAndFocusability(form_handle(), page, |
| 2454 | kExpectedAnnotSubtypes, |
| 2455 | kExpectedDefaultFocusableSubtypes); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2456 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2457 | // Make no annotation type focusable using the preferred method. |
| 2458 | ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0)); |
| 2459 | ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle())); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2460 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2461 | // Restore the focusable type count back to 1, then set it back to 0 using a |
| 2462 | // different method. |
| 2463 | SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes); |
| 2464 | ASSERT_TRUE( |
| 2465 | FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0)); |
| 2466 | ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle())); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2467 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2468 | VerifyAnnotationSubtypesAndFocusability(form_handle(), page, |
Ankit Kumar | 906ac57 | 2020-04-21 05:58:04 +0000 | [diff] [blame^] | 2469 | kExpectedAnnotSubtypes, {}); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2470 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2471 | // Now make links focusable. |
| 2472 | const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK}; |
| 2473 | SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2474 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2475 | const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = { |
Ankit Kumar | 906ac57 | 2020-04-21 05:58:04 +0000 | [diff] [blame^] | 2476 | FPDF_ANNOT_LINK}; |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2477 | VerifyAnnotationSubtypesAndFocusability(form_handle(), page, |
| 2478 | kExpectedAnnotSubtypes, |
| 2479 | kExpectedLinkocusableSubtypes); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2480 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2481 | // Test invalid parameters. |
| 2482 | EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes, |
| 2483 | FX_ArraySize(kDefaultSubtypes))); |
| 2484 | EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, |
| 2485 | FX_ArraySize(kDefaultSubtypes))); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2486 | EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr)); |
| 2487 | |
Lei Zhang | 9b44400 | 2020-04-17 17:35:23 +0000 | [diff] [blame] | 2488 | std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1); |
| 2489 | EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(), |
| 2490 | subtypes.size())); |
| 2491 | EXPECT_FALSE( |
| 2492 | FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size())); |
| 2493 | EXPECT_FALSE( |
| 2494 | FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0)); |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 2495 | |
| 2496 | UnloadPage(page); |
| 2497 | } |
Lei Zhang | 671aece | 2020-04-14 19:02:26 +0000 | [diff] [blame] | 2498 | |
| 2499 | TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) { |
| 2500 | ASSERT_TRUE(OpenDocument("annots.pdf")); |
| 2501 | FPDF_PAGE page = LoadPage(0); |
| 2502 | ASSERT_TRUE(page); |
| 2503 | |
| 2504 | { |
| 2505 | #if defined(OS_WIN) |
| 2506 | static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30"; |
| 2507 | #elif defined(OS_MACOSX) |
| 2508 | static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867"; |
| 2509 | #else |
| 2510 | static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654"; |
| 2511 | #endif |
| 2512 | // Check the initial rendering. |
| 2513 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 2514 | CompareBitmap(bitmap.get(), 612, 792, kMd5sum); |
| 2515 | } |
| 2516 | |
| 2517 | // Make links and highlights focusable. |
| 2518 | static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK, |
| 2519 | FPDF_ANNOT_HIGHLIGHT}; |
| 2520 | constexpr int kSubTypesCount = FX_ArraySize(kSubTypes); |
| 2521 | ASSERT_TRUE( |
| 2522 | FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount)); |
| 2523 | ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle())); |
| 2524 | std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount); |
| 2525 | ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), |
| 2526 | subtypes.size())); |
| 2527 | ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]); |
| 2528 | ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]); |
| 2529 | |
| 2530 | { |
| 2531 | #if defined(OS_WIN) |
| 2532 | static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f"; |
| 2533 | #elif defined(OS_MACOSX) |
| 2534 | static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346"; |
| 2535 | #else |
| 2536 | static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee"; |
| 2537 | #endif |
| 2538 | // Focus the first link and check the rendering. |
| 2539 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 2540 | ASSERT_TRUE(annot); |
| 2541 | EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get())); |
| 2542 | EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get())); |
| 2543 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 2544 | CompareBitmap(bitmap.get(), 612, 792, kMd5sum); |
| 2545 | } |
| 2546 | |
| 2547 | { |
| 2548 | #if defined(OS_WIN) |
| 2549 | static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10"; |
| 2550 | #elif defined(OS_MACOSX) |
| 2551 | static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e"; |
| 2552 | #else |
| 2553 | static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110"; |
| 2554 | #endif |
| 2555 | // Focus the first highlight and check the rendering. |
| 2556 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4)); |
| 2557 | ASSERT_TRUE(annot); |
| 2558 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 2559 | EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get())); |
| 2560 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 2561 | CompareBitmap(bitmap.get(), 612, 792, kMd5sum); |
| 2562 | } |
| 2563 | |
| 2564 | UnloadPage(page); |
| 2565 | } |