Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svtools / qa / unit / testHtmlWriter.cxx
blob702bf64464ab3a0806d950388a592c552c7e578d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 */
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>
18 namespace
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");
41 aHtml.end();
43 OString aString = extractFromStream(aStream);
44 CPPUNIT_ASSERT_EQUAL(OString("<abc/>"), aString);
48 SvMemoryStream aStream;
50 HtmlWriter aHtml(aStream);
51 aHtml.prettyPrint(false);
52 aHtml.single("abc");
54 OString aString = extractFromStream(aStream);
56 CPPUNIT_ASSERT_EQUAL(OString("<abc/>"), aString);
60 CPPUNIT_TEST_FIXTURE(Test, testSingleElementWithAttributes)
63 SvMemoryStream aStream;
65 HtmlWriter aHtml(aStream);
66 aHtml.prettyPrint(false);
67 aHtml.start("abc");
68 aHtml.attribute("x", "y");
69 aHtml.end();
71 OString aString = extractFromStream(aStream);
73 CPPUNIT_ASSERT_EQUAL(OString("<abc x=\"y\"/>"), aString);
77 SvMemoryStream aStream;
79 HtmlWriter aHtml(aStream);
80 aHtml.prettyPrint(false);
81 aHtml.start("abc");
82 aHtml.attribute("x", "y");
83 aHtml.attribute("q", "w");
84 aHtml.end();
86 OString aString = extractFromStream(aStream);
88 CPPUNIT_ASSERT_EQUAL(OString("<abc x=\"y\" q=\"w\"/>"), aString);
92 CPPUNIT_TEST_FIXTURE(Test, testSingleElementWithContent)
94 SvMemoryStream aStream;
96 HtmlWriter aHtml(aStream);
97 aHtml.prettyPrint(false);
98 aHtml.start("abc");
99 aHtml.end();
101 OString aString = extractFromStream(aStream);
103 CPPUNIT_ASSERT_EQUAL(OString("<abc/>"), aString);
106 CPPUNIT_TEST_FIXTURE(Test, testSingleElementWithContentAndAttributes)
108 SvMemoryStream aStream;
110 HtmlWriter aHtml(aStream);
111 aHtml.prettyPrint(false);
112 aHtml.start("abc");
113 aHtml.attribute("x", "y");
114 aHtml.attribute("q", "w");
115 aHtml.end();
117 OString aString = extractFromStream(aStream);
119 CPPUNIT_ASSERT_EQUAL(OString("<abc x=\"y\" q=\"w\"/>"), aString);
122 CPPUNIT_TEST_FIXTURE(Test, testNested)
124 SvMemoryStream aStream;
126 HtmlWriter aHtml(aStream);
127 aHtml.prettyPrint(false);
128 aHtml.start("abc");
129 aHtml.start("xyz");
130 aHtml.end();
131 aHtml.end();
133 OString aString = extractFromStream(aStream);
135 CPPUNIT_ASSERT_EQUAL(OString("<abc><xyz/></abc>"), aString);
138 CPPUNIT_TEST_FIXTURE(Test, testNamespace)
140 SvMemoryStream aStream;
142 HtmlWriter aHtml(aStream, "reqif-xhtml");
143 aHtml.prettyPrint(false);
144 aHtml.single("br");
146 OString aString = extractFromStream(aStream);
148 // This was <br/>, namespace request was ignored.
149 CPPUNIT_ASSERT_EQUAL(OString("<reqif-xhtml:br/>"), aString);
152 CPPUNIT_TEST_FIXTURE(Test, testAttributeValues)
154 SvMemoryStream aStream;
156 HtmlWriter aHtml(aStream);
157 aHtml.prettyPrint(false);
158 aHtml.start("abc");
159 aHtml.attribute("one", "one");
160 aHtml.attribute("two", u"two");
161 aHtml.attribute("three", sal_Int32(12));
162 aHtml.end();
164 OString aString = extractFromStream(aStream);
166 CPPUNIT_ASSERT_EQUAL(OString("<abc one=\"one\" two=\"two\" three=\"12\"/>"), aString);
169 CPPUNIT_TEST_FIXTURE(Test, testCharacters)
171 SvMemoryStream aStream;
173 HtmlWriter aHtml(aStream);
174 aHtml.prettyPrint(false);
175 aHtml.start("abc");
176 aHtml.characters("hello");
177 aHtml.end();
179 OString aString = extractFromStream(aStream);
181 CPPUNIT_ASSERT_EQUAL(OString("<abc>hello</abc>"), aString);
184 CPPUNIT_TEST_FIXTURE(Test, testExactElementEnd)
186 SvMemoryStream aStream;
188 HtmlWriter aHtml(aStream);
189 aHtml.prettyPrint(false);
190 aHtml.start("start");
191 aHtml.start("a");
192 CPPUNIT_ASSERT(aHtml.end("a"));
193 aHtml.start("b");
194 CPPUNIT_ASSERT(!aHtml.end("c"));
195 CPPUNIT_ASSERT(aHtml.end("start"));
197 OString aString = extractFromStream(aStream);
198 CPPUNIT_ASSERT_EQUAL(OString("<start><a/><b/></start>"), aString);
201 CPPUNIT_TEST_FIXTURE(Test, testAttributeValueEncode)
203 // Given a HTML writer:
204 SvMemoryStream aStream;
205 HtmlWriter aHtml(aStream);
206 aHtml.prettyPrint(false);
208 // When writing an attribute with a value that needs encoding:
209 aHtml.start("element");
210 aHtml.attribute("attribute", "a&b");
211 aHtml.end();
213 // Then make sure that the encoding is performed:
214 OString aString = extractFromStream(aStream);
215 // Without the accompanying fix in place, this test would have failed with:
216 // - Expected: <element attribute="a&amp;b"/>
217 // - Actual : <element attribute="a&b"/>
218 // i.e. attribute value was not encoded in HTML, but it was in e.g. XML.
219 CPPUNIT_ASSERT_EQUAL(OString("<element attribute=\"a&amp;b\"/>"), aString);
222 CPPUNIT_PLUGIN_IMPLEMENT();
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */