Avoid potential negative array index access to cached text.
[LibreOffice.git] / writerperfect / qa / unit / DrawingImportTest.cxx
blob8af2ec8d62b72a7bafdb3154fcada1d1a03223aa
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 <com/sun/star/beans/XPropertySet.hpp>
11 #include <com/sun/star/drawing/XDrawPages.hpp>
12 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
13 #include <com/sun/star/drawing/XShapeDescriptor.hpp>
14 #include <com/sun/star/drawing/XShapes.hpp>
15 #include <com/sun/star/text/XText.hpp>
16 #include <com/sun/star/uno/XComponentContext.hpp>
18 #include <cppuhelper/supportsservice.hxx>
20 #include <rtl/ref.hxx>
22 #include <DocumentHandlerForOdg.hxx>
23 #include <ImportFilter.hxx>
24 #include "WpftFilterFixture.hxx"
25 #include "WpftLoader.hxx"
26 #include "wpftimport.hxx"
28 namespace
30 namespace uno = css::uno;
32 class DrawingImportFilter : public writerperfect::ImportFilter<OdgGenerator>
34 public:
35 explicit DrawingImportFilter(const uno::Reference<uno::XComponentContext>& rxContext)
36 : writerperfect::ImportFilter<OdgGenerator>(rxContext)
40 // XServiceInfo
41 virtual OUString SAL_CALL getImplementationName() override;
42 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
43 virtual uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
45 private:
46 virtual bool doDetectFormat(librevenge::RVNGInputStream& rInput, OUString& rTypeName) override;
47 virtual bool doImportDocument(weld::Window* pWindow, librevenge::RVNGInputStream& rInput,
48 OdgGenerator& rGenerator,
49 utl::MediaDescriptor& rDescriptor) override;
51 static void generate(librevenge::RVNGDrawingInterface& rDocument);
54 bool DrawingImportFilter::doImportDocument(weld::Window*, librevenge::RVNGInputStream&,
55 OdgGenerator& rGenerator, utl::MediaDescriptor&)
57 DrawingImportFilter::generate(rGenerator);
58 return true;
61 bool DrawingImportFilter::doDetectFormat(librevenge::RVNGInputStream&, OUString& rTypeName)
63 rTypeName = "WpftDummyDrawing";
64 return true;
67 // XServiceInfo
68 OUString SAL_CALL DrawingImportFilter::getImplementationName()
70 return "org.libreoffice.comp.Wpft.QA.DrawingImportFilter";
73 sal_Bool SAL_CALL DrawingImportFilter::supportsService(const OUString& rServiceName)
75 return cppu::supportsService(this, rServiceName);
78 uno::Sequence<OUString> SAL_CALL DrawingImportFilter::getSupportedServiceNames()
80 return { "com.sun.star.document.ImportFilter", "com.sun.star.document.ExtendedTypeDetection" };
83 void DrawingImportFilter::generate(librevenge::RVNGDrawingInterface& rDocument)
85 using namespace librevenge;
87 rDocument.startDocument(RVNGPropertyList());
88 RVNGPropertyList aProps;
89 aProps.insert("svg:width", 800);
90 aProps.insert("svg:height", 600);
91 rDocument.startPage(aProps);
92 aProps.clear();
93 aProps.insert("svg:x", 50);
94 aProps.insert("svg:y", 50);
95 aProps.insert("svg:width", 200);
96 aProps.insert("svg:height", 100);
97 rDocument.startTextObject(aProps);
98 rDocument.openParagraph(RVNGPropertyList());
99 rDocument.openSpan(RVNGPropertyList());
100 rDocument.insertText("My hovercraft is full of eels.");
101 rDocument.closeSpan();
102 rDocument.closeParagraph();
103 rDocument.endTextObject();
104 rDocument.endPage();
105 rDocument.endDocument();
109 namespace
111 class DrawingImportTest : public writerperfect::test::WpftFilterFixture
113 public:
114 void test();
116 CPPUNIT_TEST_SUITE(DrawingImportTest);
117 CPPUNIT_TEST(test);
118 CPPUNIT_TEST_SUITE_END();
121 void DrawingImportTest::test()
123 using namespace css;
125 rtl::Reference<DrawingImportFilter> xFilter{ new DrawingImportFilter(m_xContext) };
126 writerperfect::test::WpftLoader aLoader(createDummyInput(), xFilter, "private:factory/sdraw",
127 m_xDesktop, m_xContext);
129 uno::Reference<drawing::XDrawPagesSupplier> xDoc(aLoader.getDocument(), uno::UNO_QUERY);
130 CPPUNIT_ASSERT(xDoc.is());
131 uno::Reference<drawing::XDrawPages> xPages = xDoc->getDrawPages();
132 CPPUNIT_ASSERT(xPages.is());
133 auto aPage = xPages->getByIndex(0);
134 uno::Reference<beans::XPropertySet> xPageProps;
135 CPPUNIT_ASSERT(aPage >>= xPageProps);
136 CPPUNIT_ASSERT(xPageProps.is());
137 sal_Int32 nProp = 0;
138 CPPUNIT_ASSERT(xPageProps->getPropertyValue("Height") >>= nProp);
139 CPPUNIT_ASSERT_EQUAL(sal_Int32(600), nProp);
140 CPPUNIT_ASSERT(xPageProps->getPropertyValue("Width") >>= nProp);
141 CPPUNIT_ASSERT_EQUAL(sal_Int32(800), nProp);
142 uno::Reference<drawing::XShapes> xShapes(xPageProps, uno::UNO_QUERY);
143 CPPUNIT_ASSERT(xShapes.is());
144 auto aShape = xShapes->getByIndex(0);
145 uno::Reference<drawing::XShapeDescriptor> xShapeDesc;
146 CPPUNIT_ASSERT(aShape >>= xShapeDesc);
147 CPPUNIT_ASSERT(xShapeDesc.is());
148 CPPUNIT_ASSERT_EQUAL(OUString("com.sun.star.drawing.TextShape"), xShapeDesc->getShapeType());
149 uno::Reference<text::XText> xText(xShapeDesc, uno::UNO_QUERY);
150 CPPUNIT_ASSERT_EQUAL(OUString("My hovercraft is full of eels."), xText->getString());
153 CPPUNIT_TEST_SUITE_REGISTRATION(DrawingImportTest);
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */