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/TestCase.h>
12 #include <cppunit/TestFixture.h>
13 #include <cppunit/TestSuite.h>
14 #include <cppunit/extensions/HelperMacros.h>
15 #include <cppunit/plugin/TestPlugIn.h>
17 #include <unotest/bootstrapfixturebase.hxx>
18 #include <tools/stream.hxx>
19 #include <svtools/HtmlWriter.hxx>
24 OString
extractFromStream(SvMemoryStream
& rStream
)
26 rStream
.WriteChar('\0');
28 rStream
.Seek(STREAM_SEEK_TO_BEGIN
);
29 return OString(static_cast<const sal_Char
*>(rStream
.GetData()));
34 class Test
: public CppUnit::TestFixture
38 CPPUNIT_TEST_FIXTURE(Test
, testSingleElement
)
41 SvMemoryStream aStream
;
43 HtmlWriter
aHtml(aStream
);
44 aHtml
.prettyPrint(false);
48 OString aString
= extractFromStream(aStream
);
49 CPPUNIT_ASSERT_EQUAL(OString("<abc/>"), aString
);
53 SvMemoryStream aStream
;
55 HtmlWriter
aHtml(aStream
);
56 aHtml
.prettyPrint(false);
59 OString aString
= extractFromStream(aStream
);
61 CPPUNIT_ASSERT_EQUAL(OString("<abc/>"), aString
);
65 CPPUNIT_TEST_FIXTURE(Test
, testSingleElementWithAttributes
)
68 SvMemoryStream aStream
;
70 HtmlWriter
aHtml(aStream
);
71 aHtml
.prettyPrint(false);
73 aHtml
.attribute("x", "y");
76 OString aString
= extractFromStream(aStream
);
78 CPPUNIT_ASSERT_EQUAL(OString("<abc x=\"y\"/>"), aString
);
82 SvMemoryStream aStream
;
84 HtmlWriter
aHtml(aStream
);
85 aHtml
.prettyPrint(false);
87 aHtml
.attribute("x", "y");
88 aHtml
.attribute("q", "w");
91 OString aString
= extractFromStream(aStream
);
93 CPPUNIT_ASSERT_EQUAL(OString("<abc x=\"y\" q=\"w\"/>"), aString
);
97 CPPUNIT_TEST_FIXTURE(Test
, testSingleElementWithContent
)
99 SvMemoryStream aStream
;
101 HtmlWriter
aHtml(aStream
);
102 aHtml
.prettyPrint(false);
106 OString aString
= extractFromStream(aStream
);
108 CPPUNIT_ASSERT_EQUAL(OString("<abc/>"), aString
);
111 CPPUNIT_TEST_FIXTURE(Test
, testSingleElementWithContentAndAttributes
)
113 SvMemoryStream aStream
;
115 HtmlWriter
aHtml(aStream
);
116 aHtml
.prettyPrint(false);
118 aHtml
.attribute("x", "y");
119 aHtml
.attribute("q", "w");
122 OString aString
= extractFromStream(aStream
);
124 CPPUNIT_ASSERT_EQUAL(OString("<abc x=\"y\" q=\"w\"/>"), aString
);
127 CPPUNIT_TEST_FIXTURE(Test
, testNested
)
129 SvMemoryStream aStream
;
131 HtmlWriter
aHtml(aStream
);
132 aHtml
.prettyPrint(false);
138 OString aString
= extractFromStream(aStream
);
140 CPPUNIT_ASSERT_EQUAL(OString("<abc><xyz/></abc>"), aString
);
143 CPPUNIT_TEST_FIXTURE(Test
, testNamespace
)
145 SvMemoryStream aStream
;
147 HtmlWriter
aHtml(aStream
, "reqif-xhtml");
148 aHtml
.prettyPrint(false);
151 OString aString
= extractFromStream(aStream
);
153 // This was <br/>, namespace request was ignored.
154 CPPUNIT_ASSERT_EQUAL(OString("<reqif-xhtml:br/>"), aString
);
157 CPPUNIT_TEST_FIXTURE(Test
, testAttributeValues
)
159 SvMemoryStream aStream
;
161 HtmlWriter
aHtml(aStream
);
162 aHtml
.prettyPrint(false);
164 aHtml
.attribute("one", OString("one"));
165 aHtml
.attribute("two", OUString("two"));
166 aHtml
.attribute("three", sal_Int32(12));
169 OString aString
= extractFromStream(aStream
);
171 CPPUNIT_ASSERT_EQUAL(OString("<abc one=\"one\" two=\"two\" three=\"12\"/>"), aString
);
174 CPPUNIT_TEST_FIXTURE(Test
, testCharacters
)
176 SvMemoryStream aStream
;
178 HtmlWriter
aHtml(aStream
);
179 aHtml
.prettyPrint(false);
181 aHtml
.characters("hello");
184 OString aString
= extractFromStream(aStream
);
186 CPPUNIT_ASSERT_EQUAL(OString("<abc>hello</abc>"), aString
);
189 CPPUNIT_PLUGIN_IMPLEMENT();
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */