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