Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmlscript / qa / cppunit / test.cxx
blobf29c9bb7ba5990d332ce7909c2864d1a3b62ccbb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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
44 OUString maDataPath;
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);
53 public:
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));
82 sal_uInt64 nBytes;
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);
88 aFile.close();
90 Reference<container::XNameContainer> xDialogModel(
91 mxComponentContext->getServiceManager()->createInstanceWithContext(
92 "com.sun.star.awt.UnoControlDialogModel", mxComponentContext),
93 UNO_QUERY);
95 ::xmlscript::importDialogModel(::xmlscript::createInputStream(std::move(bytes)), xDialogModel,
96 mxComponentContext, nullptr);
98 Reference<lang::XComponent> xDialogModelComp(xDialogModel, UNO_QUERY);
99 if (xDialogModelComp)
100 xDialogModelComp->dispose();
102 return xDialogModel;
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());
114 for (;;)
116 Sequence<sal_Int8> readBytes;
117 nRead = xStream->readBytes(readBytes, 1024);
118 if (!nRead)
119 break;
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));
131 aFile.close();
134 void XmlScriptTest::testBasicElements()
136 // Import
137 Reference<container::XNameContainer> xModel(importFile(u"test.xml"));
138 CPPUNIT_ASSERT(xModel.is());
140 // Export
141 utl::TempFileNamed aTempFile;
142 aTempFile.EnableKillingFile();
143 exportToFile(aTempFile.GetURL(), xModel);
145 // Parse & check
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,
165 "groupbox1");
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,
174 "field1");
175 assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:textfield[2]"_ostr, "id"_ostr,
176 "field2");
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,
179 "file1");
180 assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:datefield[1]"_ostr, "id"_ostr,
181 "datefield1");
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,
184 "pattern1");
185 assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:currencyfield[1]"_ostr, "id"_ostr,
186 "currency1");
187 assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:numericfield[1]"_ostr, "id"_ostr,
188 "numeric1");
189 assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:fixedline[1]"_ostr, "id"_ostr,
190 "fixedline1");
191 assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:progressmeter[1]"_ostr, "id"_ostr,
192 "progress1");
193 assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:scrollbar[1]"_ostr, "id"_ostr,
194 "scrollbar1");
195 assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:formattedfield[1]"_ostr, "id"_ostr,
196 "ffield0");
197 assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:formattedfield[2]"_ostr, "id"_ostr,
198 "ffield1");
200 Reference<lang::XComponent> xDialogModelComp(xModel, UNO_QUERY);
201 if (xDialogModelComp)
202 xDialogModelComp->dispose();
205 void XmlScriptTest::testEmptyPopupItems()
207 // Import
208 Reference<container::XNameContainer> xModel(importFile(u"EmptyPopupItems.xdl"));
209 CPPUNIT_ASSERT(xModel.is());
211 // Export
212 utl::TempFileNamed aTempFile;
213 aTempFile.EnableKillingFile();
214 exportToFile(aTempFile.GetURL(), xModel);
216 // Parse & check
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
221 assertXPath(pXmlDoc,
222 "/dlg:window/dlg:bulletinboard/dlg:combobox/dlg:menupopup/dlg:menuitem[1]"_ostr,
223 "value"_ostr, "Eintrag1");
224 assertXPath(pXmlDoc,
225 "/dlg:window/dlg:bulletinboard/dlg:combobox/dlg:menupopup/dlg:menuitem[2]"_ostr,
226 "value"_ostr, "");
227 assertXPath(pXmlDoc,
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: */