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);
43 OString aString
= extractFromStream(aStream
);
44 CPPUNIT_ASSERT_EQUAL(OString("<abc/>"), aString
);
48 SvMemoryStream aStream
;
50 HtmlWriter
aHtml(aStream
);
51 aHtml
.prettyPrint(false);
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);
68 aHtml
.attribute("x", "y");
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);
82 aHtml
.attribute("x", "y");
83 aHtml
.attribute("q", "w");
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);
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);
113 aHtml
.attribute("x", "y");
114 aHtml
.attribute("q", "w");
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);
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);
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);
159 aHtml
.attribute("one", "one");
160 aHtml
.attribute("two", u
"two");
161 aHtml
.attribute("three", sal_Int32(12));
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);
176 aHtml
.characters("hello");
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");
192 CPPUNIT_ASSERT(aHtml
.end("a"));
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");
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&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&b\"/>"), aString
);
222 CPPUNIT_PLUGIN_IMPLEMENT();
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */