/* -*- 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/. */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace ::com::sun::star; class SdModelTestBase : public UnoApiXmlTest { public: SdModelTestBase(OUString path) : UnoApiXmlTest(path) { } void createSdImpressDoc(const char* pName = nullptr, const char* pPassword = nullptr) { if (!pName) load(u"private:factory/simpress"_ustr); else loadFromFile(OUString::createFromAscii(pName), pPassword); uno::Reference xServiceInfo(mxComponent, uno::UNO_QUERY_THROW); CPPUNIT_ASSERT( xServiceInfo->supportsService(u"com.sun.star.presentation.PresentationDocument"_ustr)); CPPUNIT_ASSERT(!getSdDocShell()->GetMedium()->GetWarningError()); } void createSdDrawDoc(const char* pName = nullptr, const char* pPassword = nullptr) { if (!pName) load(u"private:factory/sdraw"_ustr); else loadFromFile(OUString::createFromAscii(pName), pPassword); uno::Reference xServiceInfo(mxComponent, uno::UNO_QUERY_THROW); CPPUNIT_ASSERT(xServiceInfo->supportsService(u"com.sun.star.drawing.DrawingDocument"_ustr)); CPPUNIT_ASSERT(!getSdDocShell()->GetMedium()->GetWarningError()); } sd::DrawDocShell* getSdDocShell() { SdXImpressDocument* pImpressDocument = dynamic_cast(mxComponent.get()); CPPUNIT_ASSERT(pImpressDocument); return pImpressDocument->GetDocShell(); } uno::Reference getPage(int nPage) { uno::Reference xDoc(mxComponent, uno::UNO_QUERY); CPPUNIT_ASSERT(xDoc.is()); uno::Reference xPage(xDoc->getDrawPages()->getByIndex(nPage), uno::UNO_QUERY_THROW); return xPage; } uno::Reference getShapeFromPage(int nShape, int nPage) { uno::Reference xPage(getPage(nPage)); uno::Reference xShape(getShape(nShape, xPage)); CPPUNIT_ASSERT_MESSAGE("Failed to load shape", xShape.is()); return xShape; } // very confusing ... UNO index-based access to pages is 0-based. This one is 1-based const SdrPage* GetPage(int nPage) { SdXImpressDocument* pXImpressDocument = dynamic_cast(mxComponent.get()); CPPUNIT_ASSERT(pXImpressDocument); SdDrawDocument* pDoc = pXImpressDocument->GetDoc(); CPPUNIT_ASSERT_MESSAGE("no document", pDoc != nullptr); const SdrPage* pPage = pDoc->GetPage(nPage); CPPUNIT_ASSERT_MESSAGE("no page", pPage != nullptr); return pPage; } uno::Reference getShape(int nShape, uno::Reference const& xPage) { uno::Reference xShape(xPage->getByIndex(nShape), uno::UNO_QUERY); CPPUNIT_ASSERT_MESSAGE("Failed to load shape", xShape.is()); return xShape; } uno::Reference getParagraphFromShape(int nPara, uno::Reference const& xShape) { uno::Reference xText = uno::Reference(xShape, uno::UNO_QUERY_THROW)->getText(); CPPUNIT_ASSERT_MESSAGE("Not a text shape", xText.is()); uno::Reference paraEnumAccess(xText, uno::UNO_QUERY); uno::Reference paraEnum(paraEnumAccess->createEnumeration()); for (int i = 0; i < nPara; ++i) paraEnum->nextElement(); uno::Reference xParagraph(paraEnum->nextElement(), uno::UNO_QUERY_THROW); return xParagraph; } uno::Reference getRunFromParagraph(int nRun, uno::Reference const& xParagraph) { uno::Reference runEnumAccess(xParagraph, uno::UNO_QUERY); uno::Reference runEnum = runEnumAccess->createEnumeration(); for (int i = 0; i < nRun; ++i) runEnum->nextElement(); uno::Reference xRun(runEnum->nextElement(), uno::UNO_QUERY); return xRun; } uno::Reference getTextFieldFromPage(int nRun, int nPara, int nShape, int nPage) { // get TextShape 1 from the first page uno::Reference xShape(getShapeFromPage(nShape, nPage)); // Get first paragraph uno::Reference xParagraph(getParagraphFromShape(nPara, xShape)); // first chunk of text uno::Reference xRun(getRunFromParagraph(nRun, xParagraph)); uno::Reference xPropSet(xRun, uno::UNO_QUERY_THROW); uno::Reference xField; xPropSet->getPropertyValue(u"TextField"_ustr) >>= xField; return xField; } }; /* vim:set shiftwidth=4 softtabstop=4 expandtab: */