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