/* -*- 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 using namespace ::com::sun::star; namespace { char const DATA_DIRECTORY[] = "/writerperfect/qa/unit/data/writer/epubexport/"; /// Tests the EPUB export filter. class EPUBExportTest : public test::BootstrapFixture, public unotest::MacrosTest, public XmlTestTools { uno::Reference mxComponentContext; uno::Reference mxComponent; utl::TempFile maTempFile; public: void setUp() override; void tearDown() override; void registerNamespaces(xmlXPathContextPtr &pXmlXpathCtx) override; void createDoc(const OUString &rFile, const uno::Sequence &rFilterData); void testOutlineLevel(); void testMimetype(); void testEPUB2(); CPPUNIT_TEST_SUITE(EPUBExportTest); CPPUNIT_TEST(testOutlineLevel); CPPUNIT_TEST(testMimetype); CPPUNIT_TEST(testEPUB2); CPPUNIT_TEST_SUITE_END(); }; void EPUBExportTest::setUp() { test::BootstrapFixture::setUp(); mxComponentContext.set(comphelper::getComponentContext(getMultiServiceFactory())); mxDesktop.set(frame::Desktop::create(mxComponentContext)); } void EPUBExportTest::tearDown() { if (mxComponent.is()) mxComponent->dispose(); test::BootstrapFixture::tearDown(); } void EPUBExportTest::registerNamespaces(xmlXPathContextPtr &pXmlXpathCtx) { xmlXPathRegisterNs(pXmlXpathCtx, BAD_CAST("opf"), BAD_CAST("http://www.idpf.org/2007/opf")); } void EPUBExportTest::createDoc(const OUString &rFile, const uno::Sequence &rFilterData) { // Import the bugdoc and export as EPUB. OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + rFile; mxComponent = loadFromDesktop(aURL); CPPUNIT_ASSERT(mxComponent.is()); uno::Reference xStorable(mxComponent, uno::UNO_QUERY); maTempFile.EnableKillingFile(); utl::MediaDescriptor aMediaDescriptor; aMediaDescriptor["FilterName"] <<= OUString("EPUB"); aMediaDescriptor["FilterData"] <<= rFilterData; xStorable->storeToURL(maTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList()); } void EPUBExportTest::testOutlineLevel() { createDoc("outline-level.fodt", {}); // Make sure that the output is split into two. uno::Reference xNameAccess = packages::zip::ZipFileAccess::createWithURL(mxComponentContext, maTempFile.GetURL()); CPPUNIT_ASSERT(xNameAccess->hasByName("OEBPS/sections/section0001.xhtml")); // This failed, output was a single section. CPPUNIT_ASSERT(xNameAccess->hasByName("OEBPS/sections/section0002.xhtml")); CPPUNIT_ASSERT(!xNameAccess->hasByName("OEBPS/sections/section0003.xhtml")); } void EPUBExportTest::testMimetype() { createDoc("hello.fodt", {}); // Check that the mime type is written uncompressed at the expected location. SvFileStream aFileStream(maTempFile.GetURL(), StreamMode::READ); SvMemoryStream aMemoryStream; aMemoryStream.WriteStream(aFileStream); OString aExpected("application/epub+zip"); CPPUNIT_ASSERT(aMemoryStream.GetSize() > static_cast(38 + aExpected.getLength())); OString aActual(static_cast(aMemoryStream.GetBuffer()) + 38, aExpected.getLength()); // This failed: actual data was some garbage, not the uncompressed mime type. CPPUNIT_ASSERT_EQUAL(aExpected, aActual); } void EPUBExportTest::testEPUB2() { uno::Sequence aFilterData(comphelper::InitPropertySequence( { // Explicitly request EPUB2. {"EPUBVersion", uno::makeAny(static_cast(20))} })); createDoc("hello.fodt", aFilterData); uno::Reference xNameAccess = packages::zip::ZipFileAccess::createWithURL(mxComponentContext, maTempFile.GetURL()); uno::Reference xInputStream(xNameAccess->getByName("OEBPS/content.opf"), uno::UNO_QUERY); std::shared_ptr pStream(utl::UcbStreamHelper::CreateStream(xInputStream, true)); xmlDocPtr pXmlDoc = parseXmlStream(pStream.get()); // This was 3.0, EPUBVersion filter option was ignored and we always emitted EPUB3. assertXPath(pXmlDoc, "/opf:package", "version", "2.0"); xmlFreeDoc(pXmlDoc); } CPPUNIT_TEST_SUITE_REGISTRATION(EPUBExportTest); } CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */