/* -*- 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 #include #include #include #include #include #include #include #include #include #include using namespace css; void SwModelTestBase::paste(std::u16string_view aFilename, OUString aInstance, uno::Reference const& xTextRange) { uno::Reference xFilter(m_xSFactory->createInstance(aInstance), uno::UNO_QUERY_THROW); uno::Reference xImporter(xFilter, uno::UNO_QUERY_THROW); xImporter->setTargetDocument(mxComponent); std::unique_ptr pStream = utl::UcbStreamHelper::CreateStream( m_directories.getURLFromSrc(u"/sw/qa/extras/") + aFilename, StreamMode::STD_READ); CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, pStream->GetError()); uno::Reference xStream(new utl::OStreamWrapper(std::move(pStream))); uno::Sequence aDescriptor{ comphelper::makePropertyValue(u"InputStream"_ustr, xStream), comphelper::makePropertyValue(u"InsertMode"_ustr, true), comphelper::makePropertyValue(u"TextInsertModeRange"_ustr, xTextRange) }; CPPUNIT_ASSERT(xFilter->filter(aDescriptor)); } SwModelTestBase::SwModelTestBase(const OUString& pTestDocumentPath, const OUString& pFilter) : UnoApiXmlTest(pTestDocumentPath) , mbExported(false) , mpXmlBuffer(nullptr) , mpFilter(pFilter) { } void SwModelTestBase::executeLoadVerifyReloadVerify(const char* filename, const char* pPassword) { maTempFile.EnableKillingFile(false); header(); loadURL(createFileURL(OUString::createFromAscii(filename)), pPassword); verify(); saveAndReload(mpFilter, pPassword); verify(); maTempFile.EnableKillingFile(); } void SwModelTestBase::executeImportExport(const char* filename, const char* pPassword) { maTempFile.EnableKillingFile(false); header(); loadAndSave(filename, pPassword); maTempFile.EnableKillingFile(false); verify(); maTempFile.EnableKillingFile(); } void SwModelTestBase::dumpLayout(const uno::Reference& rComponent) { // create the xml writer mpXmlBuffer = xmlBufferCreate(); xmlTextWriterPtr pXmlWriter = xmlNewTextWriterMemory(mpXmlBuffer, 0); (void)xmlTextWriterStartDocument(pXmlWriter, nullptr, nullptr, nullptr); // create the dump SwXTextDocument* pTextDoc = dynamic_cast(rComponent.get()); CPPUNIT_ASSERT(pTextDoc); SwDoc* pDoc = pTextDoc->GetDocShell()->GetDoc(); SwRootFrame* pLayout = pDoc->getIDocumentLayoutAccess().GetCurrentLayout(); pLayout->dumpAsXml(pXmlWriter); // delete xml writer (void)xmlTextWriterEndDocument(pXmlWriter); xmlFreeTextWriter(pXmlWriter); } void SwModelTestBase::calcLayout() { getSwDoc()->getIDocumentLayoutAccess().GetCurrentViewShell()->CalcLayout(); } OUString SwModelTestBase::getBodyText() const { uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); uno::Reference xParaEnumAccess(xTextDocument->getText(), uno::UNO_QUERY); uno::Reference xParaEnum = xParaEnumAccess->createEnumeration(); OUStringBuffer aBuf; while (xParaEnum->hasMoreElements()) { uno::Reference xRangeEnumAccess(xParaEnum->nextElement(), uno::UNO_QUERY); uno::Reference xRangeEnum = xRangeEnumAccess->createEnumeration(); while (xRangeEnum->hasMoreElements()) { uno::Reference xRange(xRangeEnum->nextElement(), uno::UNO_QUERY); aBuf.append(xRange->getString()); } } return aBuf.makeStringAndClear(); } uno::Reference SwModelTestBase::getStyles(const OUString& aFamily) { uno::Reference xStyleFamiliesSupplier(mxComponent, uno::UNO_QUERY); uno::Reference xStyleFamilies = xStyleFamiliesSupplier->getStyleFamilies(); uno::Reference xStyleFamily(xStyleFamilies->getByName(aFamily), uno::UNO_QUERY); return xStyleFamily; } uno::Reference SwModelTestBase::getAutoStyles(const OUString& aFamily) { uno::Reference xAutoStylesSupplier(mxComponent, uno::UNO_QUERY); uno::Reference xAutoStyles(xAutoStylesSupplier->getAutoStyles()); uno::Reference xAutoStyleFamily(xAutoStyles->getByName(aFamily), uno::UNO_QUERY); return xAutoStyleFamily; } xmlDocUniquePtr SwModelTestBase::parseLayoutDump(const uno::Reference& xComponent) { if (xComponent) dumpLayout(xComponent); else dumpLayout(mxComponent); auto pBuffer = reinterpret_cast(xmlBufferContent(mpXmlBuffer)); SAL_INFO("sw.qa", "SwModelTestBase::parseLayoutDump: pBuffer is '" << pBuffer << "'"); xmlDocUniquePtr pXmlDoc(xmlParseMemory(pBuffer, xmlBufferLength(mpXmlBuffer))); // Discard dumped layout xmlBufferFree(mpXmlBuffer); mpXmlBuffer = nullptr; return pXmlDoc; } bool SwModelTestBase::hasProperty(const uno::Reference& obj, const OUString& name) const { uno::Reference properties(obj, uno::UNO_QUERY_THROW); return properties->getPropertySetInfo()->hasPropertyByName(name); } xml::AttributeData SwModelTestBase::getUserDefineAttribute(const uno::Any& obj, const OUString& name, const OUString& rValue) const { uno::Reference attrsCnt( getProperty(obj, u"UserDefinedAttributes"_ustr), uno::UNO_QUERY_THROW); xml::AttributeData aValue; attrsCnt->getByName(name) >>= aValue; if (!rValue.isEmpty()) CPPUNIT_ASSERT_EQUAL_MESSAGE("attribute of cell does not contain expected value", rValue, aValue.Value); return aValue; } int SwModelTestBase::getParagraphs(uno::Reference const& xText) { int nRet = 0; if (!xText.is()) return nRet; uno::Reference xParaEnumAccess(xText->getText(), uno::UNO_QUERY); uno::Reference xParaEnum = xParaEnumAccess->createEnumeration(); while (xParaEnum->hasMoreElements()) { xParaEnum->nextElement(); nRet++; } return nRet; } int SwModelTestBase::getParagraphs() { uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); return getParagraphs(xTextDocument->getText()); } uno::Reference SwModelTestBase::getParagraphOrTable(int number, uno::Reference const& xText) const { assert(number != 0); // this thing is 1-based uno::Reference paraEnumAccess; if (xText.is()) paraEnumAccess.set(xText, uno::UNO_QUERY); else { uno::Reference textDocument(mxComponent, uno::UNO_QUERY); paraEnumAccess.set(textDocument->getText(), uno::UNO_QUERY); } uno::Reference paraEnum = paraEnumAccess->createEnumeration(); for (int i = 1; i < number; ++i) paraEnum->nextElement(); uno::Reference const xElem(paraEnum->nextElement(), uno::UNO_QUERY_THROW); return xElem; } uno::Reference SwModelTestBase::getParagraph(int number, const OUString& content) const { uno::Reference const xParagraph(getParagraphOrTable(number), uno::UNO_QUERY_THROW); if (!content.isEmpty()) CPPUNIT_ASSERT_EQUAL_MESSAGE("paragraph does not have expected content", content, xParagraph->getString()); return xParagraph; } sal_Int16 SwModelTestBase::getNumberingTypeOfParagraph(int nPara) { sal_Int16 nNumberingType = -1; uno::Reference xPara(getParagraph(nPara)); uno::Reference properties(xPara, uno::UNO_QUERY); bool isNumber = false; properties->getPropertyValue(u"NumberingIsNumber"_ustr) >>= isNumber; if (isNumber) { uno::Reference xLevels( properties->getPropertyValue(u"NumberingRules"_ustr), uno::UNO_QUERY); sal_Int16 nNumberingLevel = -1; properties->getPropertyValue(u"NumberingLevel"_ustr) >>= nNumberingLevel; if (nNumberingLevel >= 0 && nNumberingLevel < xLevels->getCount()) { uno::Sequence aPropertyValue; xLevels->getByIndex(nNumberingLevel) >>= aPropertyValue; auto pProp = std::find_if( std::cbegin(aPropertyValue), std::cend(aPropertyValue), [](const beans::PropertyValue& rProp) { return rProp.Name == "NumberingType"; }); if (pProp != std::cend(aPropertyValue)) nNumberingType = pProp->Value.get(); } } return nNumberingType; } uno::Reference SwModelTestBase::getParagraphOfText(int number, uno::Reference const& xText, const OUString& content) const { uno::Reference const xParagraph(getParagraphOrTable(number, xText), uno::UNO_QUERY); CPPUNIT_ASSERT(xParagraph.is()); if (!content.isEmpty()) CPPUNIT_ASSERT_EQUAL_MESSAGE("paragraph does not contain expected content", content, xParagraph->getString()); return xParagraph; } uno::Reference SwModelTestBase::getParagraphAnchoredObject(int const index, uno::Reference const& xPara) const { uno::Reference xContentEnumAccess(xPara, uno::UNO_QUERY); uno::Reference xContentEnum = xContentEnumAccess->createContentEnumeration(u"com.sun.star.text.TextContent"_ustr); for (int i = 1; i < index; ++i) { xContentEnum->nextElement(); } return uno::Reference(xContentEnum->nextElement(), uno::UNO_QUERY); } uno::Reference SwModelTestBase::getRun(uno::Reference const& xParagraph, int number, const OUString& content) const { uno::Reference xRunEnumAccess(xParagraph, uno::UNO_QUERY); uno::Reference xRunEnum = xRunEnumAccess->createEnumeration(); for (int i = 1; i < number; ++i) xRunEnum->nextElement(); uno::Reference xRun(xRunEnum->nextElement(), uno::UNO_QUERY); if (!content.isEmpty()) CPPUNIT_ASSERT_EQUAL_MESSAGE("run does not contain expected content", content, xRun->getString()); return xRun; } OUString SwModelTestBase::getFormula(uno::Reference const& xRun) const { uno::Reference xContentEnumAccess(xRun, uno::UNO_QUERY); uno::Reference xContentEnum = xContentEnumAccess->createContentEnumeration(u""_ustr); uno::Reference xFormula(xContentEnum->nextElement(), uno::UNO_QUERY); return getProperty( getProperty>(xFormula, u"Model"_ustr), u"Formula"_ustr); } uno::Reference SwModelTestBase::getCell(uno::Reference const& xTableIfc, OUString const& rCell, OUString const& rContent) { uno::Reference const xTable(xTableIfc, uno::UNO_QUERY_THROW); uno::Reference const xCell(xTable->getCellByName(rCell), uno::UNO_SET_THROW); if (!rContent.isEmpty()) { uno::Reference const xCellText(xCell, uno::UNO_QUERY_THROW); CPPUNIT_ASSERT_EQUAL_MESSAGE("cell does not contain expected content", rContent, xCellText->getString()); } return xCell; } uno::Reference SwModelTestBase::getShape(int number) { uno::Reference xDrawPageSupplier(mxComponent, uno::UNO_QUERY); uno::Reference xDrawPage = xDrawPageSupplier->getDrawPage(); uno::Reference xShape(xDrawPage->getByIndex(number - 1), uno::UNO_QUERY); return xShape; } void SwModelTestBase::selectShape(int number) { SwXTextDocument* pXTextDocument = dynamic_cast(mxComponent.get()); uno::Reference xSelectionSupplier( pXTextDocument->getCurrentController(), uno::UNO_QUERY); xSelectionSupplier->select(uno::Any(getShape(number))); CPPUNIT_ASSERT(xSelectionSupplier->getSelection().hasValue()); SwDoc* pDoc = pXTextDocument->GetDocShell()->GetDoc(); SwView* pView = pDoc->GetDocShell()->GetView(); // Make sure SwTextShell is replaced with SwDrawShell right now, not after 120 ms, as set in the // SwView ctor. pView->StopShellTimer(); } uno::Reference SwModelTestBase::getShapeByName(std::u16string_view aName) { uno::Reference xRet; uno::Reference xDrawPageSupplier(mxComponent, uno::UNO_QUERY); uno::Reference xDrawPage = xDrawPageSupplier->getDrawPage(); for (sal_Int32 i = 0; i < xDrawPage->getCount(); ++i) { uno::Reference xShape(xDrawPage->getByIndex(i), uno::UNO_QUERY); if (xShape->getName() == aName) { xRet.set(xShape, uno::UNO_QUERY); break; } } return xRet; } uno::Reference SwModelTestBase::getTextFrameByName(const OUString& aName) { uno::Reference xTextFramesSupplier(mxComponent, uno::UNO_QUERY); uno::Reference xNameAccess = xTextFramesSupplier->getTextFrames(); uno::Reference xShape(xNameAccess->getByName(aName), uno::UNO_QUERY); return xShape; } void SwModelTestBase::header() {} void SwModelTestBase::loadURL(OUString const& rURL, const char* pPassword) { // Output name at load time, so in the case of a hang, the name of the hanging input file is visible. if (!isExported()) { std::cout << rURL << ":\n"; } UnoApiXmlTest::load(rURL, pPassword); CPPUNIT_ASSERT(!getSwDocShell()->GetMedium()->GetWarningError()); calcLayout(); } void SwModelTestBase::saveAndReload(const OUString& pFilter, const char* pPassword) { save(pFilter, pPassword); mbExported = true; loadURL(maTempFile.GetURL(), pPassword); } void SwModelTestBase::loadAndSave(const char* pName, const char* pPassword) { loadURL(createFileURL(OUString::createFromAscii(pName)), pPassword); save(mpFilter); mbExported = true; } void SwModelTestBase::loadAndReload(const char* pName) { loadURL(createFileURL(OUString::createFromAscii(pName))); saveAndReload(mpFilter); } int SwModelTestBase::getPages() const { uno::Reference xModel(mxComponent, uno::UNO_QUERY); uno::Reference xTextViewCursorSupplier( xModel->getCurrentController(), uno::UNO_QUERY); uno::Reference xCursor(xTextViewCursorSupplier->getViewCursor(), uno::UNO_QUERY); xCursor->jumpToLastPage(); return xCursor->getPage(); } int SwModelTestBase::getShapes() const { uno::Reference xDrawPageSupplier(mxComponent, uno::UNO_QUERY); uno::Reference xDraws = xDrawPageSupplier->getDrawPage(); return xDraws->getCount(); } void SwModelTestBase::createSwDoc(const char* pName, const char* pPassword) { if (!pName) loadURL(u"private:factory/swriter"_ustr); else loadURL(createFileURL(OUString::createFromAscii(pName)), pPassword); uno::Reference xServiceInfo(mxComponent, uno::UNO_QUERY_THROW); CPPUNIT_ASSERT(xServiceInfo->supportsService(u"com.sun.star.text.TextDocument"_ustr)); } void SwModelTestBase::createSwWebDoc(const char* pName) { if (!pName) loadURL(u"private:factory/swriter/web"_ustr); else loadURL(createFileURL(OUString::createFromAscii(pName))); uno::Reference xServiceInfo(mxComponent, uno::UNO_QUERY_THROW); CPPUNIT_ASSERT(xServiceInfo->supportsService(u"com.sun.star.text.WebDocument"_ustr)); } void SwModelTestBase::createSwGlobalDoc(const char* pName) { if (!pName) loadURL(u"private:factory/swriter/GlobalDocument"_ustr); else loadURL(createFileURL(OUString::createFromAscii(pName))); uno::Reference xServiceInfo(mxComponent, uno::UNO_QUERY_THROW); CPPUNIT_ASSERT(xServiceInfo->supportsService(u"com.sun.star.text.GlobalDocument"_ustr)); } SwDoc* SwModelTestBase::getSwDoc() { return getSwDocShell()->GetDoc(); } SwDocShell* SwModelTestBase::getSwDocShell() { SwXTextDocument* pTextDoc = dynamic_cast(mxComponent.get()); CPPUNIT_ASSERT(pTextDoc); return pTextDoc->GetDocShell(); } xmlDocUniquePtr SwModelTestBase::WrapReqifFromTempFile() { SvMemoryStream aStream; aStream.WriteOString("\n"); SvFileStream aFileStream(maTempFile.GetURL(), StreamMode::READ); aStream.WriteStream(aFileStream); aStream.WriteOString("\n"); aStream.Seek(0); xmlDocUniquePtr pXmlDoc = parseXmlStream(&aStream); // Make sure the output is well-formed. CPPUNIT_ASSERT(pXmlDoc); return pXmlDoc; } void SwModelTestBase::emulateTyping(SwXTextDocument& rTextDoc, const std::u16string_view& rStr) { for (const char16_t c : rStr) { rTextDoc.postKeyEvent(LOK_KEYEVENT_KEYINPUT, c, 0); rTextDoc.postKeyEvent(LOK_KEYEVENT_KEYUP, c, 0); Scheduler::ProcessEventsToIdle(); } } SwExportFormFieldsGuard::SwExportFormFieldsGuard() { m_pBatch = comphelper::ConfigurationChanges::create(); m_bValue = officecfg::Office::Common::Filter::PDF::Export::ExportFormFields::get(); officecfg::Office::Common::Filter::PDF::Export::ExportFormFields::set(true, m_pBatch); m_pBatch->commit(); } SwExportFormFieldsGuard::~SwExportFormFieldsGuard() { officecfg::Office::Common::Filter::PDF::Export::ExportFormFields::set(m_bValue, m_pBatch); m_pBatch->commit(); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */