/* -*- 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 namespace com::sun::star::io { class XInputStream; } using namespace com::sun::star; namespace { /// Test class for PlainTextFilterDetect. class TextFilterDetectTest : public test::BootstrapFixture, public unotest::MacrosTest { public: void setUp() override; }; void TextFilterDetectTest::setUp() { test::BootstrapFixture::setUp(); mxDesktop.set(frame::Desktop::create(mxComponentContext)); } constexpr OUStringLiteral DATA_DIRECTORY = u"/filter/qa/unit/data/"; CPPUNIT_TEST_FIXTURE(TextFilterDetectTest, testTdf114428) { uno::Reference xDetect( getMultiServiceFactory()->createInstance("com.sun.star.comp.filters.PlainTextFilterDetect"), uno::UNO_QUERY); OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "tdf114428.xhtml"; SvFileStream aStream(aURL, StreamMode::READ); uno::Reference xStream(new utl::OStreamWrapper(aStream)); uno::Sequence aDescriptor = { comphelper::makePropertyValue("DocumentService", OUString("com.sun.star.text.TextDocument")), comphelper::makePropertyValue("InputStream", xStream), comphelper::makePropertyValue("TypeName", OUString("generic_HTML")) }; xDetect->detect(aDescriptor); utl::MediaDescriptor aMediaDesc(aDescriptor); OUString aFilterName = aMediaDesc.getUnpackedValueOrDefault("FilterName", OUString()); // This was empty, XML declaration caused HTML detect to not handle XHTML. CPPUNIT_ASSERT_EQUAL(OUString("HTML (StarWriter)"), aFilterName); } CPPUNIT_TEST_FIXTURE(TextFilterDetectTest, testEmptyFile) { const OUString sDataDirectory = m_directories.getURLFromSrc(DATA_DIRECTORY); auto supportsService = [](const uno::Reference& x, const OUString& s) { return uno::Reference(x, uno::UNO_QUERY_THROW)->supportsService(s); }; // Given an empty file, with a pptx extension // When loading the file auto xComponent = loadFromDesktop(sDataDirectory + "empty.pptx"); // Then make sure it is opened in Impress. // Without the accompanying fix in place, this test would have failed, as it was opened in // Writer instead. CPPUNIT_ASSERT(supportsService(xComponent, "com.sun.star.presentation.PresentationDocument")); xComponent->dispose(); // Now also test ODT xComponent = loadFromDesktop(sDataDirectory + "empty.odt"); // Make sure it opens in Writer. CPPUNIT_ASSERT(supportsService(xComponent, "com.sun.star.text.TextDocument")); xComponent->dispose(); // ... and ODS xComponent = loadFromDesktop(sDataDirectory + "empty.ods"); // Make sure it opens in Calc. CPPUNIT_ASSERT(supportsService(xComponent, "com.sun.star.sheet.SpreadsheetDocument")); xComponent->dispose(); // ... and ODP xComponent = loadFromDesktop(sDataDirectory + "empty.odp"); // Without the accompanying fix in place, this test would have failed, as it was opened in // Writer instead. CPPUNIT_ASSERT(supportsService(xComponent, "com.sun.star.presentation.PresentationDocument")); xComponent->dispose(); // ... and DOC // Without the accompanying fix in place, this test would have failed, the import filter aborted // loading. xComponent = loadFromDesktop(sDataDirectory + "empty.doc"); CPPUNIT_ASSERT(supportsService(xComponent, "com.sun.star.text.TextDocument")); { uno::Reference xModel(xComponent, uno::UNO_QUERY); uno::Sequence aArgs = xModel->getArgs(); comphelper::SequenceAsHashMap aMap(aArgs); OUString aFilterName; aMap["FilterName"] >>= aFilterName; // Without the accompanying fix in place, this test would have failed with: // - Expected: MS Word 97 // - Actual : MS WinWord 6.0 // i.e. opening worked, but saving back failed instead of producing a WW8 binary file. CPPUNIT_ASSERT_EQUAL(OUString("MS Word 97"), aFilterName); } xComponent->dispose(); // Now test with default templates set SfxObjectFactory::SetStandardTemplate("com.sun.star.presentation.PresentationDocument", sDataDirectory + "impress.otp"); SfxObjectFactory::SetStandardTemplate("com.sun.star.text.TextDocument", sDataDirectory + "writer.ott"); SfxObjectFactory::SetStandardTemplate("com.sun.star.sheet.SpreadsheetDocument", sDataDirectory + "calc.ots"); xComponent = loadFromDesktop(sDataDirectory + "empty.pptx"); { uno::Reference xDoc(xComponent, uno::UNO_QUERY_THROW); uno::Reference xPages(xDoc->getDrawPages(), uno::UNO_SET_THROW); uno::Reference xPage(xPages->getByIndex(0), uno::UNO_QUERY_THROW); uno::Reference xBox(xPage->getByIndex(0), uno::UNO_QUERY_THROW); // Make sure the template's text was loaded CPPUNIT_ASSERT_EQUAL(OUString("Title of Impress template"), xBox->getString()); } xComponent->dispose(); xComponent = loadFromDesktop(sDataDirectory + "empty.odt"); { uno::Reference xDoc(xComponent, uno::UNO_QUERY_THROW); uno::Reference xEA(xDoc->getText(), uno::UNO_QUERY_THROW); uno::Reference xEnum(xEA->createEnumeration(), uno::UNO_SET_THROW); uno::Reference xParagraph(xEnum->nextElement(), uno::UNO_QUERY_THROW); // Make sure the template's text was loaded CPPUNIT_ASSERT_EQUAL(OUString(u"Writer template’s first line"), xParagraph->getString()); } xComponent->dispose(); xComponent = loadFromDesktop(sDataDirectory + "empty.ods"); { uno::Reference xDoc(xComponent, uno::UNO_QUERY_THROW); uno::Reference xRA(xDoc->getSheets(), uno::UNO_QUERY_THROW); uno::Reference xC(xRA->getCellByPosition(0, 0, 0), uno::UNO_QUERY_THROW); // Make sure the template's text was loaded CPPUNIT_ASSERT_EQUAL(OUString(u"Calc template’s first cell"), xC->getString()); } xComponent->dispose(); xComponent = loadFromDesktop(sDataDirectory + "empty.odp"); { uno::Reference xDoc(xComponent, uno::UNO_QUERY_THROW); uno::Reference xPages(xDoc->getDrawPages(), uno::UNO_SET_THROW); uno::Reference xPage(xPages->getByIndex(0), uno::UNO_QUERY_THROW); uno::Reference xBox(xPage->getByIndex(0), uno::UNO_QUERY_THROW); // Make sure the template's text was loaded CPPUNIT_ASSERT_EQUAL(OUString("Title of Impress template"), xBox->getString()); } xComponent->dispose(); xComponent = loadFromDesktop(sDataDirectory + "empty.doc"); { uno::Reference xDoc(xComponent, uno::UNO_QUERY_THROW); uno::Reference xEA(xDoc->getText(), uno::UNO_QUERY_THROW); uno::Reference xEnum(xEA->createEnumeration(), uno::UNO_SET_THROW); uno::Reference xParagraph(xEnum->nextElement(), uno::UNO_QUERY_THROW); // Make sure the template's text was loaded CPPUNIT_ASSERT_EQUAL(OUString(u"Writer template’s first line"), xParagraph->getString()); } xComponent->dispose(); } } CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */