1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 #include <cppunit/TestFixture.h>
12 #include <cppunit/plugin/TestPlugIn.h>
14 #include <unotest/bootstrapfixturebase.hxx>
15 #include <tools/stream.hxx>
16 #include <svtools/HtmlWriter.hxx>
20 OString
extractFromStream(SvMemoryStream
& rStream
)
22 rStream
.WriteChar('\0');
23 rStream
.FlushBuffer();
24 rStream
.Seek(STREAM_SEEK_TO_BEGIN
);
25 return static_cast<const char*>(rStream
.GetData());
29 class Test
: public CppUnit::TestFixture
33 CPPUNIT_TEST_FIXTURE(Test
, testSingleElement
)
36 SvMemoryStream aStream
;
38 HtmlWriter
aHtml(aStream
);
39 aHtml
.prettyPrint(false);
40 aHtml
.start("abc"_ostr
);
43 OString aString
= extractFromStream(aStream
);
44 CPPUNIT_ASSERT_EQUAL("<abc/>"_ostr
, aString
);
48 SvMemoryStream aStream
;
50 HtmlWriter
aHtml(aStream
);
51 aHtml
.prettyPrint(false);
52 aHtml
.single("abc"_ostr
);
54 OString aString
= extractFromStream(aStream
);
56 CPPUNIT_ASSERT_EQUAL("<abc/>"_ostr
, aString
);
60 CPPUNIT_TEST_FIXTURE(Test
, testSingleElementWithAttributes
)
63 SvMemoryStream aStream
;
65 HtmlWriter
aHtml(aStream
);
66 aHtml
.prettyPrint(false);
67 aHtml
.start("abc"_ostr
);
68 aHtml
.attribute("x", "y");
71 OString aString
= extractFromStream(aStream
);
73 CPPUNIT_ASSERT_EQUAL("<abc x=\"y\"/>"_ostr
, aString
);
77 SvMemoryStream aStream
;
79 HtmlWriter
aHtml(aStream
);
80 aHtml
.prettyPrint(false);
81 aHtml
.start("abc"_ostr
);
82 aHtml
.attribute("x", "y");
83 aHtml
.attribute("q", "w");
86 OString aString
= extractFromStream(aStream
);
88 CPPUNIT_ASSERT_EQUAL("<abc x=\"y\" q=\"w\"/>"_ostr
, aString
);
92 CPPUNIT_TEST_FIXTURE(Test
, testSingleElementWithContent
)
94 SvMemoryStream aStream
;
96 HtmlWriter
aHtml(aStream
);
97 aHtml
.prettyPrint(false);
98 aHtml
.start("abc"_ostr
);
101 OString aString
= extractFromStream(aStream
);
103 CPPUNIT_ASSERT_EQUAL("<abc/>"_ostr
, aString
);
106 CPPUNIT_TEST_FIXTURE(Test
, testSingleElementWithContentAndAttributes
)
108 SvMemoryStream aStream
;
110 HtmlWriter
aHtml(aStream
);
111 aHtml
.prettyPrint(false);
112 aHtml
.start("abc"_ostr
);
113 aHtml
.attribute("x", "y");
114 aHtml
.attribute("q", "w");
117 OString aString
= extractFromStream(aStream
);
119 CPPUNIT_ASSERT_EQUAL("<abc x=\"y\" q=\"w\"/>"_ostr
, aString
);
122 CPPUNIT_TEST_FIXTURE(Test
, testNested
)
124 SvMemoryStream aStream
;
126 HtmlWriter
aHtml(aStream
);
127 aHtml
.prettyPrint(false);
128 aHtml
.start("abc"_ostr
);
129 aHtml
.start("xyz"_ostr
);
133 OString aString
= extractFromStream(aStream
);
135 CPPUNIT_ASSERT_EQUAL("<abc><xyz/></abc>"_ostr
, aString
);
138 CPPUNIT_TEST_FIXTURE(Test
, testNamespace
)
140 SvMemoryStream aStream
;
142 HtmlWriter
aHtml(aStream
, "reqif-xhtml");
143 aHtml
.prettyPrint(false);
144 aHtml
.single("br"_ostr
);
146 OString aString
= extractFromStream(aStream
);
148 // This was <br/>, namespace request was ignored.
149 CPPUNIT_ASSERT_EQUAL("<reqif-xhtml:br/>"_ostr
, aString
);
152 CPPUNIT_TEST_FIXTURE(Test
, testAttributeValues
)
154 SvMemoryStream aStream
;
156 HtmlWriter
aHtml(aStream
);
157 aHtml
.prettyPrint(false);
158 aHtml
.start("abc"_ostr
);
159 aHtml
.attribute("one", "one");
160 aHtml
.attribute("two", u
"two"_ustr
);
161 aHtml
.attribute("three", sal_Int32(12));
164 OString aString
= extractFromStream(aStream
);
166 CPPUNIT_ASSERT_EQUAL("<abc one=\"one\" two=\"two\" three=\"12\"/>"_ostr
, aString
);
169 CPPUNIT_TEST_FIXTURE(Test
, testCharacters
)
171 SvMemoryStream aStream
;
173 HtmlWriter
aHtml(aStream
);
174 aHtml
.prettyPrint(false);
175 aHtml
.start("abc"_ostr
);
176 aHtml
.characters("hello");
178 aHtml
.characters(" "); // Should not try to close a not opened tag
179 aHtml
.start("abc"_ostr
);
180 aHtml
.characters("world"); // Should close opening tag
183 OString aString
= extractFromStream(aStream
);
185 CPPUNIT_ASSERT_EQUAL("<abc>hello</abc> <abc>world</abc>"_ostr
, aString
);
188 CPPUNIT_TEST_FIXTURE(Test
, testExactElementEnd
)
190 SvMemoryStream aStream
;
192 HtmlWriter
aHtml(aStream
);
193 aHtml
.prettyPrint(false);
194 aHtml
.start("start"_ostr
);
195 aHtml
.start("a"_ostr
);
196 CPPUNIT_ASSERT(aHtml
.end("a"_ostr
));
197 aHtml
.start("b"_ostr
);
198 CPPUNIT_ASSERT(!aHtml
.end("c"_ostr
));
199 CPPUNIT_ASSERT(aHtml
.end("start"_ostr
));
201 OString aString
= extractFromStream(aStream
);
202 CPPUNIT_ASSERT_EQUAL("<start><a/><b/></start>"_ostr
, aString
);
205 CPPUNIT_TEST_FIXTURE(Test
, testAttributeValueEncode
)
207 // Given a HTML writer:
208 SvMemoryStream aStream
;
209 HtmlWriter
aHtml(aStream
);
210 aHtml
.prettyPrint(false);
212 // When writing an attribute with a value that needs encoding:
213 aHtml
.start("element"_ostr
);
214 aHtml
.attribute("attribute", "a&b");
217 // Then make sure that the encoding is performed:
218 OString aString
= extractFromStream(aStream
);
219 // Without the accompanying fix in place, this test would have failed with:
220 // - Expected: <element attribute="a&b"/>
221 // - Actual : <element attribute="a&b"/>
222 // i.e. attribute value was not encoded in HTML, but it was in e.g. XML.
223 CPPUNIT_ASSERT_EQUAL("<element attribute=\"a&b\"/>"_ostr
, aString
);
226 CPPUNIT_PLUGIN_IMPLEMENT();
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */