Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cpdf_annotlist_unittest.cpp
Go to the documentation of this file.
1// Copyright 2023 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "core/fpdfdoc/cpdf_annotlist.h"
6
7#include <stdint.h>
8
9#include <initializer_list>
10#include <memory>
11
12#include "constants/annotation_common.h"
13#include "core/fpdfapi/page/cpdf_page.h"
14#include "core/fpdfapi/page/test_with_page_module.h"
15#include "core/fpdfapi/parser/cpdf_array.h"
16#include "core/fpdfapi/parser/cpdf_dictionary.h"
17#include "core/fpdfapi/parser/cpdf_name.h"
18#include "core/fpdfapi/parser/cpdf_string.h"
19#include "core/fpdfapi/parser/cpdf_test_document.h"
20#include "core/fpdfdoc/cpdf_annot.h"
21#include "core/fxcrt/bytestring.h"
22#include "core/fxcrt/compiler_specific.h"
23#include "core/fxcrt/retain_ptr.h"
24#include "core/fxcrt/widestring.h"
25#include "testing/gtest/include/gtest/gtest.h"
26
27namespace {
28
29class CPDFAnnotListTest : public TestWithPageModule {
30 public:
31 void SetUp() override {
33
34 document_ = std::make_unique<CPDF_TestDocument>();
35 document_->SetRoot(pdfium::MakeRetain<CPDF_Dictionary>());
36 page_ = pdfium::MakeRetain<CPDF_Page>(
37 document_.get(), pdfium::MakeRetain<CPDF_Dictionary>());
38 }
39
40 void TearDown() override {
41 page_.Reset();
42 document_.reset();
43
45 }
46
47 protected:
48 void AddTextAnnotation(const ByteString& contents) {
49 RetainPtr<CPDF_Dictionary> annotation =
50 page_->GetOrCreateAnnotsArray()->AppendNew<CPDF_Dictionary>();
51 annotation->SetNewFor<CPDF_Name>(pdfium::annotation::kSubtype, "Text");
52 annotation->SetNewFor<CPDF_String>(pdfium::annotation::kContents, contents);
53 }
54
55 std::unique_ptr<CPDF_TestDocument> document_;
56 RetainPtr<CPDF_Page> page_;
57};
58
59ByteString MakeByteString(std::initializer_list<uint8_t> bytes) {
60 // SAFETY: compiler determines size of initializer_list.
61 return UNSAFE_BUFFERS(ByteString(std::data(bytes), std::size(bytes)));
62}
63
64ByteString GetRawContents(const CPDF_Annot* annotation) {
67}
68
69WideString GetDecodedContents(const CPDF_Annot* annotation) {
72}
73
74} // namespace
75
77 const ByteString kContents = MakeByteString({'A', 'a', 0xE4, 0xA0});
78 AddTextAnnotation(kContents);
79
80 CPDF_AnnotList list(page_);
81
82 ASSERT_EQ(2u, list.Count());
83 EXPECT_EQ(kContents, GetRawContents(list.GetAt(1)));
84 EXPECT_EQ(WideString::FromUTF8("Aaä€"), GetDecodedContents(list.GetAt(1)));
85}
86
88 const ByteString kContents =
89 MakeByteString({0xFE, 0xFF, 0x00, 'A', 0x00, 'a', 0x00, 0xE4, 0x20, 0xAC,
90 0xD8, 0x3C, 0xDF, 0xA8});
91 AddTextAnnotation(kContents);
92
93 CPDF_AnnotList list(page_);
94
95 ASSERT_EQ(2u, list.Count());
96 EXPECT_EQ(kContents, GetRawContents(list.GetAt(1)));
97
98 EXPECT_EQ(WideString::FromUTF8("Aaä€🎨"), GetDecodedContents(list.GetAt(1)));
99}
100
102 AddTextAnnotation("");
103
104 CPDF_AnnotList list(page_);
105
106 EXPECT_EQ(1u, list.Count());
107}
108
110 const ByteString kContents = MakeByteString({0xFE, 0xFF});
111 AddTextAnnotation(kContents);
112
113 CPDF_AnnotList list(page_);
114
115 EXPECT_EQ(1u, list.Count());
116}
117
119 const ByteString kContents =
120 MakeByteString({0xFE, 0xFF, 0x00, 0x1B, 'j', 'a', 0x00, 0x1B});
121 AddTextAnnotation(kContents);
122
123 CPDF_AnnotList list(page_);
124
125 EXPECT_EQ(1u, list.Count());
126}
fxcrt::ByteString ByteString
Definition bytestring.h:180
const CPDF_Dictionary * GetAnnotDict() const
Definition cpdf_annot.h:83
WideString GetUnicodeTextFor(const ByteString &key) const
ByteString GetByteStringFor(const ByteString &key) const
std::map< ByteString, RetainPtr< CPDF_Object >, std::less<> > DictMap
static WideString FromUTF8(ByteStringView str)
#define UNSAFE_BUFFERS(...)
TEST_F(CPDFAnnotListTest, CreatePopupAnnotFromPdfEncoded)
fxcrt::WideString WideString
Definition widestring.h:207