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
cfx_xmlinstruction_unittest.cpp
Go to the documentation of this file.
1// Copyright 2018 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/fxcrt/xml/cfx_xmlinstruction.h"
6
7#include <memory>
8
9#include "core/fxcrt/cfx_read_only_span_stream.h"
10#include "core/fxcrt/xml/cfx_xmldocument.h"
11#include "core/fxcrt/xml/cfx_xmlelement.h"
12#include "core/fxcrt/xml/cfx_xmlparser.h"
13#include "testing/gtest/include/gtest/gtest.h"
14#include "testing/string_write_stream.h"
15
17 CFX_XMLInstruction node(L"acrobat");
19}
20
22 CFX_XMLInstruction node(L"acrobat");
23 EXPECT_TRUE(node.IsAcrobat());
24 EXPECT_FALSE(node.IsOriginalXFAVersion());
25}
26
28 CFX_XMLInstruction node(L"originalXFAVersion");
29 EXPECT_TRUE(node.IsOriginalXFAVersion());
30 EXPECT_FALSE(node.IsAcrobat());
31}
32
34 CFX_XMLInstruction node(L"acrobat");
35 EXPECT_EQ(0U, node.GetTargetData().size());
36
37 node.AppendData(L"firstString");
38 node.AppendData(L"secondString");
39
40 auto& data = node.GetTargetData();
41 ASSERT_EQ(2U, data.size());
42 EXPECT_EQ(L"firstString", data[0]);
43 EXPECT_EQ(L"secondString", data[1]);
44}
45
48
49 CFX_XMLInstruction node(L"acrobat");
50 node.AppendData(L"firstString");
51 node.AppendData(L"secondString");
52
53 CFX_XMLNode* clone = node.Clone(&doc);
54 EXPECT_TRUE(clone != nullptr);
55
57 CFX_XMLInstruction* inst = ToXMLInstruction(clone);
58 EXPECT_TRUE(inst->IsAcrobat());
59
60 auto& data = inst->GetTargetData();
61 ASSERT_EQ(2U, data.size());
62 EXPECT_EQ(L"firstString", data[0]);
63 EXPECT_EQ(L"secondString", data[1]);
64}
65
67 auto stream = pdfium::MakeRetain<StringWriteStream>();
68 CFX_XMLInstruction node(L"xml");
69 node.Save(stream);
70 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", stream->ToString());
71}
72
74 auto stream = pdfium::MakeRetain<StringWriteStream>();
75 CFX_XMLInstruction node(L"acrobat");
76 node.AppendData(L"http://www.xfa.org/schema/xfa-template/3.3/");
77 node.AppendData(L"Display:1");
78
79 node.Save(stream);
80 EXPECT_EQ(
81 "<?acrobat http://www.xfa.org/schema/xfa-template/3.3/ Display:1 ?>\n",
82 stream->ToString());
83}
84
86 static const char input[] =
87 "<?acrobat http://www.xfa.org/schema/xfa-template/3.3/ Display:1 ?>\n"
88 "<node></node>";
89
90 auto in_stream =
91 pdfium::MakeRetain<CFX_ReadOnlySpanStream>(pdfium::as_byte_span(input));
92
93 CFX_XMLParser parser(in_stream);
94 std::unique_ptr<CFX_XMLDocument> doc = parser.Parse();
95 ASSERT_TRUE(doc != nullptr);
96
97 CFX_XMLElement* root = doc->GetRoot();
98 ASSERT_TRUE(root->GetFirstChild() != nullptr);
99 ASSERT_EQ(CFX_XMLNode::Type::kInstruction, root->GetFirstChild()->GetType());
100
101 CFX_XMLInstruction* node = ToXMLInstruction(root->GetFirstChild());
102 ASSERT_TRUE(node != nullptr);
103 EXPECT_TRUE(node->IsAcrobat());
104
105 auto& data = node->GetTargetData();
106 ASSERT_EQ(2U, data.size());
107 EXPECT_EQ(L"http://www.xfa.org/schema/xfa-template/3.3/", data[0]);
108 EXPECT_EQ(L"Display:1", data[1]);
109
110 auto out_stream = pdfium::MakeRetain<StringWriteStream>();
111 node->Save(out_stream);
112 EXPECT_EQ(
113 "<?acrobat http://www.xfa.org/schema/xfa-template/3.3/ Display:1 ?>\n",
114 out_stream->ToString());
115}
116
118 static const char input[] =
119 "<node>\n"
120 "<?acrobat http://www.xfa.org/schema/xfa-template/3.3/ Display:1 ?>\n"
121 "</node>";
122
123 auto in_stream =
124 pdfium::MakeRetain<CFX_ReadOnlySpanStream>(pdfium::as_byte_span(input));
125
126 CFX_XMLParser parser(in_stream);
127 std::unique_ptr<CFX_XMLDocument> doc = parser.Parse();
128 ASSERT_TRUE(doc != nullptr);
129
130 CFX_XMLElement* root = doc->GetRoot();
131 ASSERT_TRUE(root->GetFirstChild() != nullptr);
132 ASSERT_TRUE(root->GetFirstChild()->GetType() == CFX_XMLNode::Type::kElement);
133
134 CFX_XMLElement* node = ToXMLElement(root->GetFirstChild());
135 EXPECT_EQ(L"node", node->GetName());
136
137 CFX_XMLInstruction* instruction = nullptr;
138 for (auto* elem = node->GetFirstChild(); elem && !instruction;
139 elem = elem->GetNextSibling()) {
140 instruction = ToXMLInstruction(elem);
141 }
142 ASSERT_TRUE(instruction != nullptr);
143 EXPECT_TRUE(instruction->IsAcrobat());
144
145 auto& data = instruction->GetTargetData();
146 ASSERT_EQ(2U, data.size());
147 EXPECT_EQ(L"http://www.xfa.org/schema/xfa-template/3.3/", data[0]);
148 EXPECT_EQ(L"Display:1", data[1]);
149
150 auto out_stream = pdfium::MakeRetain<StringWriteStream>();
151 node->Save(out_stream);
152 EXPECT_EQ(
153 "<node>\n\n"
154 "<?acrobat http://www.xfa.org/schema/xfa-template/3.3/ Display:1 ?>\n\n"
155 "</node>\n",
156 out_stream->ToString());
157}
CFX_XMLInstruction * ToXMLInstruction(CFX_XMLNode *pNode)
void Save(const RetainPtr< IFX_RetainableWriteStream > &pXMLStream) override
const WideString & GetName() const
void AppendData(const WideString &wsData)
CFX_XMLInstruction(const WideString &wsTarget)
Type GetType() const override
bool IsOriginalXFAVersion() const
CFX_XMLNode * Clone(CFX_XMLDocument *doc) override
void Save(const RetainPtr< IFX_RetainableWriteStream > &pXMLStream) override
virtual Type GetType() const =0
TEST(FXCRYPT, MD5GenerateEmtpyData)