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 <test/bootstrapfixture.hxx>
11 #include <test/xmltesttools.hxx>
12 #include <unotest/macros_test.hxx>
13 #include <unotools/tempfile.hxx>
15 #include <com/sun/star/frame/Desktop.hpp>
16 #include <com/sun/star/frame/DispatchHelper.hpp>
17 #include <com/sun/star/beans/XPropertySet.hpp>
18 #include <com/sun/star/container/XNameContainer.hpp>
19 #include <com/sun/star/io/XInputStream.hpp>
20 #include <com/sun/star/io/XInputStreamProvider.hpp>
21 #include <com/sun/star/io/XOutputStream.hpp>
23 #include <comphelper/processfactory.hxx>
24 #include <comphelper/propertysequence.hxx>
26 #include <config_folders.h>
27 #include <osl/file.hxx>
28 #include <rtl/ustrbuf.hxx>
29 #include <rtl/byteseq.hxx>
30 #include <sal/log.hxx>
32 #include <xmlscript/xmldlg_imexp.hxx>
33 #include <xmlscript/xml_helper.hxx>
34 #include <cppuhelper/bootstrap.hxx>
36 using namespace ::cppu
;
37 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star
;
41 /// Sample tests for import
42 class XmlScriptTest
: public test::BootstrapFixture
, public unotest::MacrosTest
, public XmlTestTools
46 void testBasicElements();
47 void testEmptyPopupItems();
49 Reference
<container::XNameContainer
> importFile(std::u16string_view sFileName
);
50 void exportToFile(std::u16string_view sFileName
,
51 Reference
<container::XNameContainer
> const& xDialogModel
);
54 virtual void setUp() override
;
56 virtual void registerNamespaces(xmlXPathContextPtr
& pXmlXpathCtx
) override
58 xmlXPathRegisterNs(pXmlXpathCtx
, BAD_CAST("dlg"),
59 BAD_CAST("http://openoffice.org/2000/dialog"));
60 xmlXPathRegisterNs(pXmlXpathCtx
, BAD_CAST("script"),
61 BAD_CAST("http://openoffice.org/2000/script"));
63 CPPUNIT_TEST_SUITE(XmlScriptTest
);
64 CPPUNIT_TEST(testBasicElements
);
65 CPPUNIT_TEST(testEmptyPopupItems
);
66 CPPUNIT_TEST_SUITE_END();
69 void XmlScriptTest::setUp()
71 test::BootstrapFixture::setUp();
72 maDataPath
= "/xmlscript/qa/cppunit/data/";
74 mxDesktop
.set(frame::Desktop::create(mxComponentContext
));
77 Reference
<container::XNameContainer
> XmlScriptTest::importFile(std::u16string_view sFileName
)
79 OUString sFullName
= m_directories
.getURLFromSrc(maDataPath
) + sFileName
;
80 osl::File
aFile(sFullName
);
81 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, aFile
.open(osl_File_OpenFlag_Read
));
83 aFile
.getSize(nBytes
);
84 std::vector
<sal_Int8
> bytes(nBytes
);
85 sal_uInt64 nBytesRead
;
86 aFile
.read(bytes
.data(), nBytes
, nBytesRead
);
87 CPPUNIT_ASSERT_EQUAL_MESSAGE("File not read correctly", nBytes
, nBytesRead
);
90 Reference
<container::XNameContainer
> xDialogModel(
91 mxComponentContext
->getServiceManager()->createInstanceWithContext(
92 "com.sun.star.awt.UnoControlDialogModel", mxComponentContext
),
95 ::xmlscript::importDialogModel(::xmlscript::createInputStream(std::move(bytes
)), xDialogModel
,
96 mxComponentContext
, nullptr);
98 Reference
<lang::XComponent
> xDialogModelComp(xDialogModel
, UNO_QUERY
);
100 xDialogModelComp
->dispose();
105 void XmlScriptTest::exportToFile(std::u16string_view sURL
,
106 Reference
<container::XNameContainer
> const& xDialogModel
)
108 Reference
<io::XInputStreamProvider
> xProvider(
109 ::xmlscript::exportDialogModel(xDialogModel
, mxComponentContext
, nullptr));
110 Reference
<io::XInputStream
> xStream(xProvider
->createInputStream());
112 Sequence
<sal_Int8
> bytes
;
113 sal_Int32 nRead
= xStream
->readBytes(bytes
, xStream
->available());
116 Sequence
<sal_Int8
> readBytes
;
117 nRead
= xStream
->readBytes(readBytes
, 1024);
121 sal_Int32 nPos
= bytes
.getLength();
122 bytes
.realloc(nPos
+ nRead
);
123 memcpy(bytes
.getArray() + nPos
, readBytes
.getConstArray(), static_cast<sal_uInt32
>(nRead
));
126 osl::File
aFile(OUString
{ sURL
});
127 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, aFile
.open(osl_File_OpenFlag_Write
));
128 sal_uInt64 nBytesWritten
;
129 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
,
130 aFile
.write(bytes
.getConstArray(), bytes
.getLength(), nBytesWritten
));
134 void XmlScriptTest::testBasicElements()
137 Reference
<container::XNameContainer
> xModel(importFile(u
"test.xml"));
138 CPPUNIT_ASSERT(xModel
.is());
141 utl::TempFileNamed aTempFile
;
142 aTempFile
.EnableKillingFile();
143 exportToFile(aTempFile
.GetURL(), xModel
);
146 xmlDocUniquePtr pXmlDoc
= parseXml(aTempFile
);
147 CPPUNIT_ASSERT(pXmlDoc
);
149 // Ensure we have all elements
150 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:button[1]"_ostr
, "id"_ostr
, "button1");
151 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:button[2]"_ostr
, "id"_ostr
, "button3");
152 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:checkbox[1]"_ostr
, "id"_ostr
, "check1");
153 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:checkbox[2]"_ostr
, "id"_ostr
, "check2");
154 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:checkbox[3]"_ostr
, "id"_ostr
, "check3");
155 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:menulist[1]"_ostr
, "id"_ostr
, "list1");
156 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:menulist[2]"_ostr
, "id"_ostr
, "list2");
157 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:combobox[1]"_ostr
, "id"_ostr
, "combo1");
158 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[1]/dlg:radio[1]"_ostr
,
159 "id"_ostr
, "radio1");
160 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[1]/dlg:radio[2]"_ostr
,
161 "id"_ostr
, "radio2");
162 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[1]/dlg:radio[3]"_ostr
,
163 "id"_ostr
, "radio3");
164 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:titledbox[1]"_ostr
, "id"_ostr
,
166 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[2]/dlg:radio[1]"_ostr
,
167 "id"_ostr
, "radio5");
168 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[2]/dlg:radio[2]"_ostr
,
169 "id"_ostr
, "radio7");
170 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[2]/dlg:radio[3]"_ostr
,
171 "id"_ostr
, "radio8");
172 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:text[1]"_ostr
, "id"_ostr
, "fixed1");
173 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:textfield[1]"_ostr
, "id"_ostr
,
175 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:textfield[2]"_ostr
, "id"_ostr
,
177 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:img[1]"_ostr
, "id"_ostr
, "image1");
178 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:filecontrol[1]"_ostr
, "id"_ostr
,
180 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:datefield[1]"_ostr
, "id"_ostr
,
182 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:timefield[1]"_ostr
, "id"_ostr
, "time1");
183 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:patternfield[1]"_ostr
, "id"_ostr
,
185 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:currencyfield[1]"_ostr
, "id"_ostr
,
187 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:numericfield[1]"_ostr
, "id"_ostr
,
189 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:fixedline[1]"_ostr
, "id"_ostr
,
191 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:progressmeter[1]"_ostr
, "id"_ostr
,
193 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:scrollbar[1]"_ostr
, "id"_ostr
,
195 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:formattedfield[1]"_ostr
, "id"_ostr
,
197 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:formattedfield[2]"_ostr
, "id"_ostr
,
200 Reference
<lang::XComponent
> xDialogModelComp(xModel
, UNO_QUERY
);
201 if (xDialogModelComp
)
202 xDialogModelComp
->dispose();
205 void XmlScriptTest::testEmptyPopupItems()
208 Reference
<container::XNameContainer
> xModel(importFile(u
"EmptyPopupItems.xdl"));
209 CPPUNIT_ASSERT(xModel
.is());
212 utl::TempFileNamed aTempFile
;
213 aTempFile
.EnableKillingFile();
214 exportToFile(aTempFile
.GetURL(), xModel
);
217 xmlDocUniquePtr pXmlDoc
= parseXml(aTempFile
);
218 CPPUNIT_ASSERT(pXmlDoc
);
220 // Ensure we have 3 items in combobox after import/export and second one is empty
222 "/dlg:window/dlg:bulletinboard/dlg:combobox/dlg:menupopup/dlg:menuitem[1]"_ostr
,
223 "value"_ostr
, "Eintrag1");
225 "/dlg:window/dlg:bulletinboard/dlg:combobox/dlg:menupopup/dlg:menuitem[2]"_ostr
,
228 "/dlg:window/dlg:bulletinboard/dlg:combobox/dlg:menupopup/dlg:menuitem[3]"_ostr
,
229 "value"_ostr
, "Eintrag2");
232 CPPUNIT_TEST_SUITE_REGISTRATION(XmlScriptTest
);
234 CPPUNIT_PLUGIN_IMPLEMENT();
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */