Bump version to 6.4-15
[LibreOffice.git] / writerperfect / qa / unit / TextImportTest.cxx
blob0b482a75c9a3098efe8758f14240525beb038ac5
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/container/XEnumeration.hpp>
12 #include <com/sun/star/container/XEnumerationAccess.hpp>
13 #include <com/sun/star/text/XTextDocument.hpp>
14 #include <com/sun/star/uno/XComponentContext.hpp>
16 #include <cppuhelper/supportsservice.hxx>
18 #include <rtl/ref.hxx>
20 #include <DocumentHandlerForOdt.hxx>
21 #include <ImportFilter.hxx>
22 #include "WpftFilterFixture.hxx"
23 #include "WpftLoader.hxx"
24 #include "wpftimport.hxx"
26 namespace
28 namespace uno = css::uno;
30 class TextImportFilter : public writerperfect::ImportFilter<OdtGenerator>
32 public:
33 explicit TextImportFilter(const uno::Reference<uno::XComponentContext>& rxContext)
34 : writerperfect::ImportFilter<OdtGenerator>(rxContext)
38 // XServiceInfo
39 virtual OUString SAL_CALL getImplementationName() override;
40 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
41 virtual uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
43 private:
44 virtual bool doDetectFormat(librevenge::RVNGInputStream& rInput, OUString& rTypeName) override;
45 virtual bool doImportDocument(weld::Window* pWindow, librevenge::RVNGInputStream& rInput,
46 OdtGenerator& rGenerator,
47 utl::MediaDescriptor& rDescriptor) override;
49 static void generate(librevenge::RVNGTextInterface& rDocument);
52 bool TextImportFilter::doImportDocument(weld::Window*, librevenge::RVNGInputStream&,
53 OdtGenerator& rGenerator, utl::MediaDescriptor&)
55 TextImportFilter::generate(rGenerator);
56 return true;
59 bool TextImportFilter::doDetectFormat(librevenge::RVNGInputStream&, OUString& rTypeName)
61 rTypeName = "WpftDummyText";
62 return true;
65 // XServiceInfo
66 OUString SAL_CALL TextImportFilter::getImplementationName()
68 return "org.libreoffice.comp.Wpft.QA.TextImportFilter";
71 sal_Bool SAL_CALL TextImportFilter::supportsService(const OUString& rServiceName)
73 return cppu::supportsService(this, rServiceName);
76 uno::Sequence<OUString> SAL_CALL TextImportFilter::getSupportedServiceNames()
78 return { "com.sun.star.document.ImportFilter", "com.sun.star.document.ExtendedTypeDetection" };
81 void TextImportFilter::generate(librevenge::RVNGTextInterface& rDocument)
83 using namespace librevenge;
85 rDocument.startDocument(RVNGPropertyList());
86 rDocument.openPageSpan(RVNGPropertyList());
87 rDocument.openParagraph(RVNGPropertyList());
88 rDocument.openSpan(RVNGPropertyList());
89 rDocument.insertText("My hovercraft is full of eels.");
90 rDocument.closeSpan();
91 rDocument.closeParagraph();
92 rDocument.closePageSpan();
93 rDocument.endDocument();
97 namespace
99 class TextImportTest : public writerperfect::test::WpftFilterFixture
101 public:
102 void test();
104 CPPUNIT_TEST_SUITE(TextImportTest);
105 CPPUNIT_TEST(test);
106 CPPUNIT_TEST_SUITE_END();
109 void TextImportTest::test()
111 using namespace css;
113 rtl::Reference<TextImportFilter> xFilter{ new TextImportFilter(m_xContext) };
114 writerperfect::test::WpftLoader aLoader(createDummyInput(), xFilter.get(),
115 "private:factory/swriter", m_xDesktop, m_xContext);
117 uno::Reference<text::XTextDocument> xDoc(aLoader.getDocument(), uno::UNO_QUERY);
118 CPPUNIT_ASSERT(xDoc.is());
119 uno::Reference<container::XEnumerationAccess> xParaAccess(xDoc->getText(), uno::UNO_QUERY);
120 CPPUNIT_ASSERT(xParaAccess.is());
121 uno::Reference<container::XEnumeration> xParas = xParaAccess->createEnumeration();
122 CPPUNIT_ASSERT(xParas.is());
123 CPPUNIT_ASSERT(xParas->hasMoreElements());
124 uno::Reference<container::XEnumerationAccess> xPortionsAccess(xParas->nextElement(),
125 uno::UNO_QUERY);
126 CPPUNIT_ASSERT(xPortionsAccess.is());
127 uno::Reference<container::XEnumeration> xPortions = xPortionsAccess->createEnumeration();
128 CPPUNIT_ASSERT(xPortions.is());
129 CPPUNIT_ASSERT(xPortions->hasMoreElements());
130 uno::Reference<beans::XPropertySet> xPortionProps(xPortions->nextElement(), uno::UNO_QUERY);
131 CPPUNIT_ASSERT(xPortionProps.is());
132 OUString aPortionType;
133 CPPUNIT_ASSERT(xPortionProps->getPropertyValue("TextPortionType") >>= aPortionType);
134 CPPUNIT_ASSERT_EQUAL(OUString("Text"), aPortionType);
135 uno::Reference<text::XTextRange> xPortion(xPortionProps, uno::UNO_QUERY);
136 CPPUNIT_ASSERT(xPortion.is());
137 CPPUNIT_ASSERT_EQUAL(OUString("My hovercraft is full of eels."), xPortion->getString());
138 CPPUNIT_ASSERT(!xPortions->hasMoreElements());
139 CPPUNIT_ASSERT(!xParas->hasMoreElements());
142 CPPUNIT_TEST_SUITE_REGISTRATION(TextImportTest);
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */