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]", "id", "button1");
151 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:button[2]", "id", "button3");
152 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:checkbox[1]", "id", "check1");
153 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:checkbox[2]", "id", "check2");
154 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:checkbox[3]", "id", "check3");
155 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:menulist[1]", "id", "list1");
156 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:menulist[2]", "id", "list2");
157 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:combobox[1]", "id", "combo1");
158 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[1]/dlg:radio[1]", "id",
160 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[1]/dlg:radio[2]", "id",
162 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[1]/dlg:radio[3]", "id",
164 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:titledbox[1]", "id", "groupbox1");
165 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[2]/dlg:radio[1]", "id",
167 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[2]/dlg:radio[2]", "id",
169 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[2]/dlg:radio[3]", "id",
171 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:text[1]", "id", "fixed1");
172 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:textfield[1]", "id", "field1");
173 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:textfield[2]", "id", "field2");
174 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:img[1]", "id", "image1");
175 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:filecontrol[1]", "id", "file1");
176 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:datefield[1]", "id", "datefield1");
177 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:timefield[1]", "id", "time1");
178 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:patternfield[1]", "id", "pattern1");
179 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:currencyfield[1]", "id", "currency1");
180 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:numericfield[1]", "id", "numeric1");
181 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:fixedline[1]", "id", "fixedline1");
182 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:progressmeter[1]", "id", "progress1");
183 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:scrollbar[1]", "id", "scrollbar1");
184 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:formattedfield[1]", "id", "ffield0");
185 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:formattedfield[2]", "id", "ffield1");
187 Reference
<lang::XComponent
> xDialogModelComp(xModel
, UNO_QUERY
);
188 if (xDialogModelComp
)
189 xDialogModelComp
->dispose();
192 void XmlScriptTest::testEmptyPopupItems()
195 Reference
<container::XNameContainer
> xModel(importFile(u
"EmptyPopupItems.xdl"));
196 CPPUNIT_ASSERT(xModel
.is());
199 utl::TempFileNamed aTempFile
;
200 aTempFile
.EnableKillingFile();
201 exportToFile(aTempFile
.GetURL(), xModel
);
204 xmlDocUniquePtr pXmlDoc
= parseXml(aTempFile
);
205 CPPUNIT_ASSERT(pXmlDoc
);
207 // Ensure we have 3 items in combobox after import/export and second one is empty
208 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:combobox/dlg:menupopup/dlg:menuitem[1]",
209 "value", "Eintrag1");
210 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:combobox/dlg:menupopup/dlg:menuitem[2]",
212 assertXPath(pXmlDoc
, "/dlg:window/dlg:bulletinboard/dlg:combobox/dlg:menupopup/dlg:menuitem[3]",
213 "value", "Eintrag2");
216 CPPUNIT_TEST_SUITE_REGISTRATION(XmlScriptTest
);
218 CPPUNIT_PLUGIN_IMPLEMENT();
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */