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