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