From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- sd/qa/unit/SVGExportTests.cxx | 352 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 352 insertions(+) create mode 100644 sd/qa/unit/SVGExportTests.cxx (limited to 'sd/qa/unit/SVGExportTests.cxx') diff --git a/sd/qa/unit/SVGExportTests.cxx b/sd/qa/unit/SVGExportTests.cxx new file mode 100644 index 000000000..4878eca27 --- /dev/null +++ b/sd/qa/unit/SVGExportTests.cxx @@ -0,0 +1,352 @@ +/* -*- 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 + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define SVG_SVG *[name()='svg'] +#define SVG_G *[name()='g'] +#define SVG_TEXT *[name()='text'] +#define SVG_TSPAN *[name()='tspan'] +#define SVG_DEFS *[name()='defs'] +#define SVG_IMAGE *[name()='image'] +#define SVG_USE *[name()='use'] +#define SVG_PATTERN *[name()='pattern'] +#define SVG_RECT *[name()='rect'] + +using namespace css; + +namespace +{ +bool isValidBitmapId(const OUString& sId) +{ + std::regex aRegEx("bitmap\\(\\d+\\)"); + return std::regex_match(sId.toUtf8().getStr(), aRegEx); +} + +BitmapChecksum getBitmapChecksumFromId(std::u16string_view sId) +{ + size_t nStart = sId.find(u"(") + 1; + size_t nCount = sId.find(u")") - nStart; + bool bIsValidRange = nStart > 0 && nStart != std::u16string_view::npos && nCount > 0; + CPPUNIT_ASSERT(bIsValidRange); + OUString sChecksum( sId.substr( nStart, nCount ) ); + return sChecksum.toUInt64(); +} + +bool isValidBackgroundPatternId(const OUString& sId) +{ + std::regex aRegEx( R"(bg\-pattern\.id\d+\.\d+)" ); + return std::regex_match(sId.toUtf8().getStr(), aRegEx); +} + +bool isValidTiledBackgroundId(const OUString& sId) +{ + std::regex aRegEx( R"(bg\-id\d+\.\d+)" ); + return std::regex_match(sId.toUtf8().getStr(), aRegEx); +} + +} + +class SdSVGFilterTest : public test::BootstrapFixture, public unotest::MacrosTest, public XmlTestTools +{ + class Resetter + { + private: + std::function m_Func; + + public: + Resetter(std::function const& rFunc) + : m_Func(rFunc) + { + } + ~Resetter() + { + try + { + m_Func(); + } + catch (...) // has to be reliable + { + CPPUNIT_FAIL("resetter failed with exception"); + } + } + }; + + uno::Reference mxComponent; + utl::TempFile maTempFile; + +protected: + void load(std::u16string_view pDir, const char* pName) + { + return loadURL(m_directories.getURLFromSrc(pDir) + OUString::createFromAscii(pName), pName); + } + + void loadURL(OUString const& rURL, const char* pName) + { + if (mxComponent.is()) + mxComponent->dispose(); + // Output name early, so in the case of a hang, the name of the hanging input file is visible. + if (pName) + std::cout << pName << ","; + mxComponent = loadFromDesktop(rURL); + } + + void save() + { + uno::Reference xStorable(mxComponent, uno::UNO_QUERY); + utl::MediaDescriptor aMediaDescriptor; + aMediaDescriptor["FilterName"] <<= OUString("impress_svg_Export"); + xStorable->storeToURL(maTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList()); + } + +public: + SdSVGFilterTest() + { + maTempFile.EnableKillingFile(); + } + + virtual void setUp() override + { + test::BootstrapFixture::setUp(); + + mxDesktop.set(css::frame::Desktop::create(comphelper::getComponentContext(getMultiServiceFactory()))); + } + + virtual void tearDown() override + { + if (mxComponent.is()) + mxComponent->dispose(); + + test::BootstrapFixture::tearDown(); + } + + void executeExport(const char* pName) + { + load( u"/sd/qa/unit/data/odp/", pName ); + save(); + } + + void testSVGExportTextDecorations() + { + executeExport( "svg-export-text-decorations.odp" ); + + xmlDocUniquePtr svgDoc = parseXml(maTempFile); + CPPUNIT_ASSERT(svgDoc); + + svgDoc->name = reinterpret_cast(xmlStrdup(reinterpret_cast(OUStringToOString(maTempFile.GetURL(), RTL_TEXTENCODING_UTF8).getStr()))); + + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG ), 1); + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_G[2] ), "class", "SlideGroup"); + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_G[2]/SVG_G/SVG_G/SVG_G ), "class", "Slide"); + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_G[2]/SVG_G/SVG_G/SVG_G/SVG_G/SVG_G[1] ), "class", "TitleText"); + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_G[2]/SVG_G/SVG_G/SVG_G/SVG_G/SVG_G[1]/SVG_G/SVG_TEXT ), "class", "SVGTextShape"); + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_G[2]/SVG_G/SVG_G/SVG_G/SVG_G/SVG_G[1]/SVG_G/SVG_TEXT/SVG_TSPAN ), "class", "TextParagraph"); + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_G[2]/SVG_G/SVG_G/SVG_G/SVG_G/SVG_G[1]/SVG_G/SVG_TEXT/SVG_TSPAN ), "text-decoration", "underline"); + + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_G[2]/SVG_G/SVG_G/SVG_G/SVG_G/SVG_G[2]/SVG_G/SVG_TEXT ), "class", "SVGTextShape"); + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_G[2]/SVG_G/SVG_G/SVG_G/SVG_G/SVG_G[2]/SVG_G/SVG_TEXT/SVG_TSPAN ), "class", "TextParagraph"); + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_G[2]/SVG_G/SVG_G/SVG_G/SVG_G/SVG_G[2]/SVG_G/SVG_TEXT/SVG_TSPAN ), "text-decoration", "line-through"); + } + + void testSVGExportJavascriptURL() + { + executeExport("textbox-link-javascript.odp"); + + xmlDocUniquePtr svgDoc = parseXml(maTempFile); + CPPUNIT_ASSERT(svgDoc); + + // There should be only one child (no link to javascript url) + assertXPathChildren(svgDoc, + SAL_STRINGIFY(/ SVG_SVG / SVG_G[2] / SVG_G / SVG_G / SVG_G / SVG_G + / SVG_G[3] / SVG_G), + 1); + } + + void testSVGExportSlideCustomBackground() + { + executeExport("slide-custom-background.odp"); + + xmlDocUniquePtr svgDoc = parseXml(maTempFile); + CPPUNIT_ASSERT(svgDoc); + + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_G[2]/SVG_G/SVG_G/SVG_G/SVG_G/SVG_DEFS ), "class", "SlideBackground"); + } + + void testSVGExportTextFieldsInMasterPage() + { + executeExport("text-fields.odp"); + + xmlDocUniquePtr svgDoc = parseXml(maTempFile); + CPPUNIT_ASSERT(svgDoc); + + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_DEFS[9]/SVG_G[2] ), "class", "Master_Slide"); + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_DEFS[9]/SVG_G[2]/SVG_G[2] ), "class", "BackgroundObjects"); + // Current Date Field + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_DEFS[9]/SVG_G[2]/SVG_G[2]/SVG_G[4] ), "class", "TextShape"); + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_DEFS[9]/SVG_G[2]/SVG_G[2]/SVG_G[4]/SVG_G/SVG_TEXT/SVG_TSPAN/SVG_TSPAN/SVG_TSPAN ), "class", "PlaceholderText"); + assertXPathContent(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_DEFS[9]/SVG_G[2]/SVG_G[2]/SVG_G[4]/SVG_G/SVG_TEXT/SVG_TSPAN/SVG_TSPAN/SVG_TSPAN ), ""); + // Current Time Field + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_DEFS[9]/SVG_G[2]/SVG_G[2]/SVG_G[5] ), "class", "TextShape"); + assertXPath(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_DEFS[9]/SVG_G[2]/SVG_G[2]/SVG_G[5]/SVG_G/SVG_TEXT/SVG_TSPAN/SVG_TSPAN/SVG_TSPAN ), "class", "PlaceholderText"); + assertXPathContent(svgDoc, SAL_STRINGIFY( /SVG_SVG/SVG_DEFS[9]/SVG_G[2]/SVG_G[2]/SVG_G[5]/SVG_G/SVG_TEXT/SVG_TSPAN/SVG_TSPAN/SVG_TSPAN ), "