tdf#137652 add xmlscript unittest & infrastructure

entire module was not covered by unittests and there was no way
to test some of the latest fixes done there, like f4a5893ece.

Now it is possible and a test for
https://gerrit.libreoffice.org/c/core/+/104619 was added

Removed remains of older unused tests.

Change-Id: Ide7bc3704efa1bb4bc62ed71bf4dd8b1ad4e800f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112569
Tested-by: Thorsten Behrens <thorsten.behrens@allotropia.de>
Reviewed-by: Thorsten Behrens <thorsten.behrens@allotropia.de>
This commit is contained in:
Vasily Melenchuk 2021-03-16 13:31:12 +03:00 committed by Thorsten Behrens
parent c47ad11f8c
commit ce135ba8a6
8 changed files with 282 additions and 265 deletions

View file

@ -0,0 +1,43 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
$(eval $(call gb_CppunitTest_CppunitTest,xmlscript_cppunit))
$(eval $(call gb_CppunitTest_add_exception_objects,xmlscript_cppunit, \
xmlscript/qa/cppunit/test \
))
$(eval $(call gb_CppunitTest_use_sdk_api,xmlscript_cppunit))
$(eval $(call gb_CppunitTest_use_ure,xmlscript_cppunit))
$(eval $(call gb_CppunitTest_use_vcl,xmlscript_cppunit))
$(eval $(call gb_CppunitTest_use_rdb,xmlscript_cppunit,services))
$(eval $(call gb_CppunitTest_use_custom_headers,xmlscript_cppunit,\
officecfg/registry \
))
$(eval $(call gb_CppunitTest_use_externals,xmlscript_cppunit,\
libxml2 \
))
$(eval $(call gb_CppunitTest_use_configuration,xmlscript_cppunit))
$(eval $(call gb_CppunitTest_use_libraries,xmlscript_cppunit, \
comphelper \
cppu \
cppuhelper \
fwk \
sal \
test \
unotest \
utl \
xmlscript \
))
# vim: set noet sw=4 ts=4:

View file

@ -14,4 +14,8 @@ $(eval $(call gb_Module_add_targets,xmlscript,\
Package_dtd \
))
$(eval $(call gb_Module_add_check_targets,xmlscript,\
CppunitTest_xmlscript_cppunit \
))
# vim: set noet sw=4 ts=4:

View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dlg:window PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "dialog.dtd">
<dlg:window xmlns:dlg="http://openoffice.org/2000/dialog" xmlns:script="http://openoffice.org/2000/script" dlg:id="TestDialog" dlg:left="116" dlg:top="56" dlg:width="175" dlg:height="123" dlg:closeable="true" dlg:moveable="true">
<dlg:bulletinboard>
<dlg:combobox dlg:id="ComboBox1" dlg:tab-index="0" dlg:left="9" dlg:top="15" dlg:width="92" dlg:height="15" dlg:spin="true">
<dlg:menupopup>
<dlg:menuitem dlg:value="Eintrag1"/>
<dlg:menuitem dlg:value=""/>
<dlg:menuitem dlg:value="Eintrag2"/>
</dlg:menupopup>
</dlg:combobox>
</dlg:bulletinboard>
</dlg:window>

View file

Before

Width:  |  Height:  |  Size: 2 KiB

After

Width:  |  Height:  |  Size: 2 KiB

View file

@ -0,0 +1,222 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <test/bootstrapfixture.hxx>
#include <test/xmltesttools.hxx>
#include <unotest/macros_test.hxx>
#include <unotools/tempfile.hxx>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/frame/DispatchHelper.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/io/XInputStreamProvider.hpp>
#include <com/sun/star/io/XOutputStream.hpp>
#include <comphelper/processfactory.hxx>
#include <comphelper/propertysequence.hxx>
#include <config_folders.h>
#include <osl/file.hxx>
#include <rtl/ustrbuf.hxx>
#include <rtl/byteseq.hxx>
#include <sal/log.hxx>
#include <xmlscript/xmldlg_imexp.hxx>
#include <xmlscript/xml_helper.hxx>
#include <cppuhelper/bootstrap.hxx>
using namespace ::cppu;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star;
/// Sample tests for import
class XmlScriptTest : public test::BootstrapFixture, public unotest::MacrosTest, public XmlTestTools
{
uno::Reference<lang::XComponent> mxComponent;
OUString maDataPath;
void testBasicElements();
void testEmptyPopupItems();
Reference<container::XNameContainer> importFile(std::u16string_view sFileName);
void exportToFile(std::u16string_view sFileName,
Reference<container::XNameContainer> const& xDialogModel);
public:
virtual void setUp() override;
virtual void tearDown() override;
virtual void registerNamespaces(xmlXPathContextPtr& pXmlXpathCtx) override
{
xmlXPathRegisterNs(pXmlXpathCtx, BAD_CAST("dlg"),
BAD_CAST("http://openoffice.org/2000/dialog"));
xmlXPathRegisterNs(pXmlXpathCtx, BAD_CAST("script"),
BAD_CAST("http://openoffice.org/2000/script"));
}
CPPUNIT_TEST_SUITE(XmlScriptTest);
CPPUNIT_TEST(testBasicElements);
CPPUNIT_TEST(testEmptyPopupItems);
CPPUNIT_TEST_SUITE_END();
};
void XmlScriptTest::setUp()
{
test::BootstrapFixture::setUp();
maDataPath = "/xmlscript/qa/cppunit/data/";
mxDesktop.set(frame::Desktop::create(mxComponentContext));
}
void XmlScriptTest::tearDown()
{
if (mxComponent.is())
mxComponent->dispose();
test::BootstrapFixture::tearDown();
}
Reference<container::XNameContainer> XmlScriptTest::importFile(std::u16string_view sFileName)
{
OUString sFullName = m_directories.getURLFromSrc(maDataPath) + sFileName;
osl::File aFile(sFullName);
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aFile.open(osl_File_OpenFlag_Read));
sal_uInt64 nBytes;
aFile.getSize(nBytes);
std::vector<sal_Int8> bytes(nBytes);
sal_uInt64 nBytesRead;
aFile.read(bytes.data(), nBytes, nBytesRead);
CPPUNIT_ASSERT_EQUAL_MESSAGE("File not read correctly", nBytes, nBytesRead);
aFile.close();
Reference<container::XNameContainer> xDialogModel(
mxComponentContext->getServiceManager()->createInstanceWithContext(
"com.sun.star.awt.UnoControlDialogModel", mxComponentContext),
UNO_QUERY);
::xmlscript::importDialogModel(::xmlscript::createInputStream(bytes), xDialogModel,
mxComponentContext, nullptr);
return xDialogModel;
}
void XmlScriptTest::exportToFile(std::u16string_view sURL,
Reference<container::XNameContainer> const& xDialogModel)
{
Reference<io::XInputStreamProvider> xProvider(
::xmlscript::exportDialogModel(xDialogModel, mxComponentContext, nullptr));
Reference<io::XInputStream> xStream(xProvider->createInputStream());
Sequence<sal_Int8> bytes;
sal_Int32 nRead = xStream->readBytes(bytes, xStream->available());
for (;;)
{
Sequence<sal_Int8> readBytes;
nRead = xStream->readBytes(readBytes, 1024);
if (!nRead)
break;
sal_Int32 nPos = bytes.getLength();
bytes.realloc(nPos + nRead);
memcpy(bytes.getArray() + nPos, readBytes.getConstArray(), static_cast<sal_uInt32>(nRead));
}
osl::File aFile(OUString() + sURL);
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, aFile.open(osl_File_OpenFlag_Write));
sal_uInt64 nBytesWritten;
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None,
aFile.write(bytes.getConstArray(), bytes.getLength(), nBytesWritten));
aFile.close();
}
void XmlScriptTest::testBasicElements()
{
// Import
Reference<container::XNameContainer> xModel(importFile(u"test.xml"));
CPPUNIT_ASSERT(xModel.is());
// Export
utl::TempFile aTempFile;
aTempFile.EnableKillingFile();
exportToFile(aTempFile.GetURL(), xModel);
// Parse & check
xmlDocUniquePtr pXmlDoc = parseXml(aTempFile);
CPPUNIT_ASSERT(pXmlDoc);
// Ensure we have all elements
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:button[1]", "id", "button1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:button[2]", "id", "button3");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:checkbox[1]", "id", "check1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:checkbox[2]", "id", "check2");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:checkbox[3]", "id", "check3");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:menulist[1]", "id", "list1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:menulist[2]", "id", "list2");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:combobox[1]", "id", "combo1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[1]/dlg:radio[1]", "id",
"radio1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[1]/dlg:radio[2]", "id",
"radio2");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[1]/dlg:radio[3]", "id",
"radio3");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:titledbox[1]", "id", "groupbox1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[2]/dlg:radio[1]", "id",
"radio5");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[2]/dlg:radio[2]", "id",
"radio7");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:radiogroup[2]/dlg:radio[3]", "id",
"radio8");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:text[1]", "id", "fixed1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:textfield[1]", "id", "field1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:textfield[2]", "id", "field2");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:img[1]", "id", "image1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:filecontrol[1]", "id", "file1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:datefield[1]", "id", "datefield1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:timefield[1]", "id", "time1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:patternfield[1]", "id", "pattern1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:currencyfield[1]", "id", "currency1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:numericfield[1]", "id", "numeric1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:fixedline[1]", "id", "fixedline1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:progressmeter[1]", "id", "progress1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:scrollbar[1]", "id", "scrollbar1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:formattedfield[1]", "id", "ffield0");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:formattedfield[2]", "id", "ffield1");
}
void XmlScriptTest::testEmptyPopupItems()
{
// Import
Reference<container::XNameContainer> xModel(importFile(u"EmptyPopupItems.xdl"));
CPPUNIT_ASSERT(xModel.is());
// Export
utl::TempFile aTempFile;
aTempFile.EnableKillingFile();
exportToFile(aTempFile.GetURL(), xModel);
// Parse & check
xmlDocUniquePtr pXmlDoc = parseXml(aTempFile);
CPPUNIT_ASSERT(pXmlDoc);
// Ensure we have 3 items in combobox after import/export and second one is empty
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:combobox/dlg:menupopup/dlg:menuitem[1]",
"value", "Eintrag1");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:combobox/dlg:menupopup/dlg:menuitem[2]",
"value", "");
assertXPath(pXmlDoc, "/dlg:window/dlg:bulletinboard/dlg:combobox/dlg:menupopup/dlg:menuitem[3]",
"value", "Eintrag2");
}
CPPUNIT_TEST_SUITE_REGISTRATION(XmlScriptTest);
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View file

@ -1,209 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <config_folders.h>
#include <stdio.h>
#include <osl/file.h>
#include <rtl/ustrbuf.hxx>
#include <sal/log.hxx>
#include <xmlscript/xmldlg_imexp.hxx>
#include <xmlscript/xml_helper.hxx>
#include <cppuhelper/servicefactory.hxx>
#include <cppuhelper/bootstrap.hxx>
#include <comphelper/processfactory.hxx>
#include <vcl/svapp.hxx>
#include <com/sun/star/awt/UnoControlDialog.hpp>
#include <com/sun/star/awt/Toolkit.hpp>
#include <com/sun/star/awt/XToolkit.hpp>
#include <com/sun/star/awt/XControlModel.hpp>
#include <com/sun/star/awt/XControl.hpp>
#include <com/sun/star/awt/XDialog.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/io/XActiveDataSource.hpp>
#include <com/sun/star/registry/XSimpleRegistry.hpp>
#include <com/sun/star/registry/XImplementationRegistration.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
using namespace ::cppu;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
Reference< XComponentContext > createInitialComponentContext(
OUString const & inst_dir )
{
Reference< XComponentContext > xContext;
try
{
OUString file_url;
oslFileError rc = osl_getFileURLFromSystemPath(
inst_dir.pData, &file_url.pData );
OSL_ASSERT( osl_File_E_None == rc );
OUString unorc = file_url + ("/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("louno") );
return defaultBootstrap_InitialComponentContext( unorc );
}
catch( const Exception& rExc )
{
SAL_WARN( "xmlscript", rExc );
}
return xContext;
}
Reference< container::XNameContainer > importFile(
char const * fname,
Reference< XComponentContext > const & xContext )
{
// create the input stream
FILE *f = ::fopen( fname, "rb" );
if (f)
{
::fseek( f, 0 ,SEEK_END );
int nLength = ::ftell( f );
::fseek( f, 0, SEEK_SET );
ByteSequence bytes( nLength );
::fread( bytes.getArray(), nLength, 1, f );
::fclose( f );
Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
"com.sun.star.awt.UnoControlDialogModel", xContext ), UNO_QUERY );
::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );
return xModel;
}
else
{
throw Exception( "### Cannot read file!" );
}
}
void exportToFile(
char const * fname,
Reference< container::XNameContainer > const & xModel,
Reference< XComponentContext > const & xContext )
{
Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
Reference< io::XInputStream > xStream( xProvider->createInputStream() );
Sequence< sal_Int8 > bytes;
sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
for (;;)
{
Sequence< sal_Int8 > readBytes;
nRead = xStream->readBytes( readBytes, 1024 );
if (! nRead)
break;
OSL_ASSERT( readBytes.getLength() >= nRead );
sal_Int32 nPos = bytes.getLength();
bytes.realloc( nPos + nRead );
memcpy( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
}
FILE * f = ::fopen( fname, "w" );
::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
::fclose( f );
}
class MyApp : public Application
{
public:
void Main();
};
MyApp aMyApp;
void MyApp::Main()
{
if (GetCommandLineParamCount() < 2)
{
OSL_FAIL( "usage: imexp inst_dir inputfile [outputfile]" );
return;
}
Reference< XComponentContext > xContext(
createInitialComponentContext( OUString( GetCommandLineParam( 0 ) ) ) );
Reference< lang::XMultiServiceFactory > xMSF(
xContext->getServiceManager(), UNO_QUERY );
try
{
::comphelper::setProcessServiceFactory( xMSF );
Reference< awt::XToolkit> xToolkit = awt::Toolkit::create( xContext );
// import dialogs
OString aParam1( OUStringToOString(
OUString( GetCommandLineParam( 1 ) ),
RTL_TEXTENCODING_ASCII_US ) );
Reference< container::XNameContainer > xModel(
importFile( aParam1.getStr(), xContext ) );
OSL_ASSERT( xModel.is() );
Reference< awt::XUnoControlDialog > xDlg = UnoControlDialog::create( xContext );
xDlg->setModel( xModel );
xDlg->createPeer( xToolkit, 0 );
xDlg->execute();
if (GetCommandLineParamCount() == 3)
{
// write modified dialogs
OString aParam2( OUStringToOString(
OUString( GetCommandLineParam( 2 ) ), RTL_TEXTENCODING_ASCII_US ) );
exportToFile( aParam2.getStr(), xModel, xContext );
}
}
catch (const xml::sax::SAXException & rExc)
{
OUString aStr( rExc );
uno::Exception exc;
if (rExc.WrappedException >>= exc)
{
aStr += " >>> ";
aStr += exc;
}
SAL_WARN( "xmlscript", aStr );
}
catch (const uno::Exception & rExc)
{
SAL_WARN( "xmlscript", rExc );
}
Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
if (xComp.is())
{
xComp->dispose();
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View file

@ -1,56 +0,0 @@
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed
# with this work for additional information regarding copyright
# ownership. The ASF licenses this file to you under the Apache
# License, Version 2.0 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.apache.org/licenses/LICENSE-2.0 .
#
PRJ=..
PRJNAME=xmlscript
TARGET=imexp
TARGETTYPE=GUI
LIBTARGET=NO
ENABLE_EXCEPTIONS=TRUE
# --- Settings -----------------------------------------------------
.INCLUDE : settings.mk
# --- Files --------------------------------------------------------
CXXFILES= \
imexp.cxx
OBJFILES= \
$(OBJ)$/imexp.obj
APP1TARGET=$(TARGET)
APP1OBJS=$(OBJFILES)
APP1STDLIBS= \
$(TOOLSLIB) \
$(SOTLIB) \
$(SVTOOLLIB) \
$(COMPHELPERLIB) \
$(VCLLIB) \
$(CPPULIB) \
$(CPPUHELPERLIB) \
$(SALLIB) \
$(XMLSCRIPTLIB)
APP1DEF= $(MISC)$/imexp.def
# --- Targets ------------------------------------------------------
.INCLUDE : target.mk