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/.
10 #include <swmodeltestbase.hxx>
12 #include <com/sun/star/awt/Gradient.hpp>
13 #include <com/sun/star/drawing/FillStyle.hpp>
14 #include <rtl/byteseq.hxx>
16 #include <swmodule.hxx>
18 #include <usrpref.hxx>
20 #include <test/htmltesttools.hxx>
22 class HtmlExportTest
: public SwModelTestBase
, public HtmlTestTools
29 SwModelTestBase("/sw/qa/extras/htmlexport/data/", "HTML (StarWriter)"),
34 bool mustCalcLayoutOf(const char* filename
) SAL_OVERRIDE
36 return OString(filename
) != "fdo62336.docx";
39 bool mustTestImportOf(const char* filename
) const SAL_OVERRIDE
41 return OString(filename
) != "fdo62336.docx";
44 void preTest(const char* filename
) SAL_OVERRIDE
46 if (getTestName().indexOf("SkipImage") != -1)
47 setFilterOptions("SkipImages");
51 if (OString(filename
) == "charborder.odt")
53 // FIXME if padding-top gets exported as inches, not cms, we get rounding errors.
54 SwGlobals::ensure(); // make sure that SW_MOD() is not 0
55 SwMasterUsrPref
* pPref
= const_cast<SwMasterUsrPref
*>(SW_MOD()->GetUsrPref(false));
56 m_eUnit
= pPref
->GetMetric();
57 pPref
->SetMetric(FUNIT_CM
);
61 void postTest(const char* filename
) SAL_OVERRIDE
63 if (OString(filename
) == "charborder.odt")
65 SwMasterUsrPref
* pPref
= const_cast<SwMasterUsrPref
*>(SW_MOD()->GetUsrPref(false));
66 pPref
->SetMetric(m_eUnit
);
71 #define DECLARE_HTMLEXPORT_ROUNDTRIP_TEST(TestName, filename) DECLARE_SW_ROUNDTRIP_TEST(TestName, filename, HtmlExportTest)
73 DECLARE_HTMLEXPORT_ROUNDTRIP_TEST(testFdo81276
, "fdo81276.html")
75 uno::Reference
<container::XNameAccess
> xPageStyles(getStyles("PageStyles"));
76 uno::Reference
<beans::XPropertySet
> xStyle(xPageStyles
->getByName("HTML"), uno::UNO_QUERY
);
77 // some rounding going on here?
78 CPPUNIT_ASSERT(abs(sal_Int32(29700) - getProperty
<sal_Int32
>(xStyle
, "Width")) < 10);
79 CPPUNIT_ASSERT(abs(sal_Int32(21006) - getProperty
<sal_Int32
>(xStyle
, "Height")) < 10);
80 CPPUNIT_ASSERT(abs(sal_Int32(500) - getProperty
<sal_Int32
>(xStyle
, "LeftMargin")) < 10);
81 CPPUNIT_ASSERT(abs(sal_Int32(500) - getProperty
<sal_Int32
>(xStyle
, "RightMargin")) < 10);
82 CPPUNIT_ASSERT(abs(sal_Int32(2000) - getProperty
<sal_Int32
>(xStyle
, "TopMargin")) < 10);
83 CPPUNIT_ASSERT(abs(sal_Int32(500) - getProperty
<sal_Int32
>(xStyle
, "BottomMargin")) < 10);
86 DECLARE_HTMLEXPORT_ROUNDTRIP_TEST(testFdo62336
, "fdo62336.docx")
88 // The problem was essentially a crash during table export as docx/rtf/html
89 // If either of no-calc-layout or no-test-import is enabled, the crash does not occur
92 DECLARE_HTMLEXPORT_ROUNDTRIP_TEST(testFdo86857
, "fdo86857.html")
94 // problem was that background color on page style was not exported
95 uno::Reference
<container::XNameAccess
> xPageStyles(getStyles("PageStyles"));
96 uno::Reference
<beans::XPropertySet
> xStyle(xPageStyles
->getByName("HTML"), uno::UNO_QUERY
);
97 CPPUNIT_ASSERT_EQUAL(sal_Int32(0xff0000), getProperty
<sal_Int32
>(xStyle
, "BackColor"));
98 // check that table background color works, which still uses RES_BACKGROUND
99 uno::Reference
<text::XTextTablesSupplier
> xTablesSupplier(mxComponent
, uno::UNO_QUERY
);
100 uno::Reference
<container::XIndexAccess
> xTables(xTablesSupplier
->getTextTables(), uno::UNO_QUERY
);
101 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xTables
->getCount());
102 uno::Reference
<text::XTextTable
> xTable(xTables
->getByIndex(0), uno::UNO_QUERY
);
103 uno::Reference
<text::XTextRange
> xCell(xTable
->getCellByName("A1"), uno::UNO_QUERY
);
104 CPPUNIT_ASSERT_EQUAL(sal_Int32(0x66ffff), getProperty
<sal_Int32
>(xCell
, "BackColor"));
107 DECLARE_HTMLEXPORT_ROUNDTRIP_TEST(testCharacterBorder
, "charborder.odt")
110 uno::Reference
<beans::XPropertySet
> xRun(getRun(getParagraph(1),1), uno::UNO_QUERY
);
113 CPPUNIT_ASSERT_BORDER_EQUAL(table::BorderLine2(0x6666FF,12,12,12,3,37), getProperty
<table::BorderLine2
>(xRun
,"CharTopBorder"));
114 CPPUNIT_ASSERT_BORDER_EQUAL(table::BorderLine2(0xFF9900,0,99,0,2,99), getProperty
<table::BorderLine2
>(xRun
,"CharLeftBorder"));
115 CPPUNIT_ASSERT_BORDER_EQUAL(table::BorderLine2(0xFF0000,0,169,0,1,169), getProperty
<table::BorderLine2
>(xRun
,"CharBottomBorder"));
116 CPPUNIT_ASSERT_BORDER_EQUAL(table::BorderLine2(0x0000FF,0,169,0,0,169), getProperty
<table::BorderLine2
>(xRun
,"CharRightBorder"));
121 CPPUNIT_ASSERT_EQUAL(sal_Int32(450), getProperty
<sal_Int32
>(xRun
,"CharTopBorderDistance"));
122 CPPUNIT_ASSERT_EQUAL(sal_Int32(550), getProperty
<sal_Int32
>(xRun
,"CharLeftBorderDistance"));
123 CPPUNIT_ASSERT_EQUAL(sal_Int32(150), getProperty
<sal_Int32
>(xRun
,"CharBottomBorderDistance"));
124 CPPUNIT_ASSERT_EQUAL(sal_Int32(250), getProperty
<sal_Int32
>(xRun
,"CharRightBorderDistance"));
130 #define DECLARE_HTMLEXPORT_TEST(TestName, filename) DECLARE_SW_EXPORT_TEST(TestName, filename, HtmlExportTest)
132 DECLARE_HTMLEXPORT_TEST(testExportOfImages
, "textAndImage.docx")
134 htmlDocPtr pDoc
= parseHtml(maTempFile
);
135 CPPUNIT_ASSERT(pDoc
);
137 assertXPath(pDoc
, "/html/body", 1);
138 assertXPath(pDoc
, "/html/body/p/img", 1);
141 DECLARE_HTMLEXPORT_TEST(testExportOfImagesWithSkipImageEnabled
, "textAndImage.docx")
143 htmlDocPtr pDoc
= parseHtml(maTempFile
);
144 CPPUNIT_ASSERT(pDoc
);
146 assertXPath(pDoc
, "/html/body", 1);
147 assertXPath(pDoc
, "/html/body/p/img", 0);
150 DECLARE_HTMLEXPORT_TEST(testSkipImageEmbedded
, "skipimage-embedded.doc")
152 // Embedded spreadsheet was exported as image, so content was lost. Make
153 // sure it's exported as HTML instead.
154 htmlDocPtr pDoc
= parseHtml(maTempFile
);
155 CPPUNIT_ASSERT(pDoc
);
158 assertXPath(pDoc
, "//table", 1);
159 // This was 2, the HTML header was in the document two times.
160 assertXPath(pDoc
, "//meta[@name='generator']", 1);
161 // This was 0, <table> was directly under <p>, which caused errors in the parser.
162 assertXPath(pDoc
, "//span/table", 1);
165 DECLARE_HTMLEXPORT_TEST(testSkipImageEmbeddedDocument
, "skipimage-embedded-document.docx")
167 // Similar to testSkipImageEmbedded, but with an embedded Writer object,
168 // not a Calc one, and this time OOXML, not WW8.
169 htmlDocPtr pDoc
= parseHtml(maTempFile
);
170 CPPUNIT_ASSERT(pDoc
);
172 // This was 2, the HTML header was in the document two times.
173 assertXPath(pDoc
, "//meta[@name='generator']", 1);
174 // Text of embedded document was missing.
175 assertXPathContent(pDoc
, "/html/body/p/span/p/span", "Inner.");
178 DECLARE_HTMLEXPORT_TEST(testExportImageProperties
, "HTMLImage.odt")
180 htmlDocPtr pDoc
= parseHtml(maTempFile
);
181 CPPUNIT_ASSERT(pDoc
);
183 assertXPath(pDoc
, "/html/body", 1);
185 assertXPath(pDoc
, "/html/body/p/map/area", "shape", "poly");
186 assertXPath(pDoc
, "/html/body/p/map/area", "href", "http://www.microsoft.com/");
187 assertXPath(pDoc
, "/html/body/p/map/area", "target", "_self");
188 assertXPath(pDoc
, "/html/body/p/map/area", "alt", "microsoft");
190 assertXPath(pDoc
, "/html/body/p/a", 1);
191 assertXPath(pDoc
, "/html/body/p/a", "href", "http://www.google.com/");
193 assertXPath(pDoc
, "/html/body/p/a/font", 1);
194 assertXPath(pDoc
, "/html/body/p/a/font", "color", "#ff0000");
196 assertXPath(pDoc
, "/html/body/p/a/font/img", 1);
197 assertXPath(pDoc
, "/html/body/p/a/font/img", "name", "Text");
198 assertXPath(pDoc
, "/html/body/p/a/font/img", "alt", "Four colors");
199 assertXPath(pDoc
, "/html/body/p/a/font/img", "align", "middle");
201 // Probably the DPI in OSX is different and Twip -> Pixel conversion produces
202 // different results - so disable OSX for now.
204 // It would make sense to switch to use CSS and use "real world" units instead
205 // i.e. (style="margin: 0cm 1.5cm; width: 1cm; height: 1cm")
207 #if 0 // disabled as it depends that the system DPI is set to 96
208 assertXPath(pDoc
, "/html/body/p/a/font/img", "hspace", "38");
209 assertXPath(pDoc
, "/html/body/p/a/font/img", "vspace", "19");
210 assertXPath(pDoc
, "/html/body/p/a/font/img", "width", "222");
211 assertXPath(pDoc
, "/html/body/p/a/font/img", "height", "222");
212 assertXPath(pDoc
, "/html/body/p/a/font/img", "border", "3");
215 assertXPath(pDoc
, "/html/body/p/a/font/img", "usemap", "#map1");
218 DECLARE_HTMLEXPORT_TEST(testExportCheckboxRadioButtonState
, "checkbox-radiobutton.doc")
220 htmlDocPtr pDoc
= parseHtml(maTempFile
);
221 CPPUNIT_ASSERT(pDoc
);
223 assertXPath(pDoc
, "/html/body", 1);
224 assertXPath(pDoc
, "/html/body/p[1]/input", "type", "checkbox");
225 assertXPath(pDoc
, "/html/body/p[1]/input", "checked", "checked");
226 assertXPath(pDoc
, "/html/body/p[2]/input", "type", "checkbox");
227 assertXPathNoAttribute(pDoc
, "/html/body/p[2]/input", "checked");
228 assertXPath(pDoc
, "/html/body/form/p[1]/input", "type", "checkbox");
229 assertXPath(pDoc
, "/html/body/form/p[1]/input", "checked", "checked");
230 assertXPath(pDoc
, "/html/body/form/p[2]/input", "type", "checkbox");
231 assertXPathNoAttribute(pDoc
, "/html/body/form/p[2]/input", "checked");
232 assertXPath(pDoc
, "/html/body/form/p[3]/input", "type", "radio");
233 assertXPath(pDoc
, "/html/body/form/p[3]/input", "checked", "checked");
234 assertXPath(pDoc
, "/html/body/form/p[4]/input", "type", "radio");
235 assertXPathNoAttribute(pDoc
, "/html/body/form/p[4]/input", "checked");
238 DECLARE_HTMLEXPORT_TEST(testExportUrlEncoding
, "tdf76291.odt")
240 htmlDocPtr pDoc
= parseHtml(maTempFile
);
241 CPPUNIT_ASSERT(pDoc
);
243 // Test URI encoded hyperlink with Chinese characters
244 assertXPath(pDoc
, "/html/body/p/a", "href", "http://www.youtube.com/results?search_query=%E7%B2%B5%E8%AA%9Emv&sm=12");
247 DECLARE_HTMLEXPORT_TEST(testExportInternalUrl
, "tdf90905.odt")
249 htmlDocPtr pDoc
= parseHtml(maTempFile
);
250 CPPUNIT_ASSERT(pDoc
);
252 // Internal url should be valid
253 assertXPath(pDoc
, "/html/body/p[1]/a", "href", "#0.0.1.Text|outline");
254 assertXPath(pDoc
, "/html/body/p[2]/a", "href", "#bookmark");
257 CPPUNIT_PLUGIN_IMPLEMENT();
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */