Bump version to 5.0-14
[LibreOffice.git] / writerperfect / qa / unit / WpftImportTestBase.cxx
blob874b4d6daac6b723849d4ba26a2df77b24fd00cd
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/PropertyValue.hpp>
11 #include <com/sun/star/container/NoSuchElementException.hpp>
12 #include <com/sun/star/container/XNameAccess.hpp>
13 #include <com/sun/star/document/XExtendedFilterDetection.hpp>
14 #include <com/sun/star/document/XFilter.hpp>
15 #include <com/sun/star/document/XImporter.hpp>
16 #include <com/sun/star/document/XTypeDetection.hpp>
17 #include <com/sun/star/frame/theDesktop.hpp>
18 #include <com/sun/star/frame/XController.hpp>
19 #include <com/sun/star/frame/XFrame.hpp>
20 #include <com/sun/star/frame/XModel.hpp>
21 #include <com/sun/star/io/XInputStream.hpp>
22 #include <com/sun/star/lang/IllegalArgumentException.hpp>
23 #include <com/sun/star/lang/XComponent.hpp>
24 #include <com/sun/star/ucb/XContent.hpp>
25 #include <com/sun/star/util/XCloseable.hpp>
27 #include <ucbhelper/content.hxx>
29 #include "WpftImportTestBase.hxx"
31 namespace beans = com::sun::star::beans;
32 namespace container = com::sun::star::container;
33 namespace document = com::sun::star::document;
34 namespace frame = com::sun::star::frame;
35 namespace io = com::sun::star::io;
36 namespace lang = com::sun::star::lang;
37 namespace ucb = com::sun::star::ucb;
38 namespace uno = com::sun::star::uno;
39 namespace util = com::sun::star::util;
41 namespace writerperfect
43 namespace test
46 WpftImportTestBase::WpftImportTestBase(const rtl::OUString &rFactoryURL)
47 : ::test::FiltersTest()
48 , ::test::BootstrapFixture()
49 , m_aFactoryURL(rFactoryURL)
50 , m_xDesktop()
51 , m_xFilter()
52 , m_xTypeMap()
56 void WpftImportTestBase::setUp()
58 ::test::BootstrapFixture::setUp();
60 m_xDesktop = frame::theDesktop::get(m_xContext);
62 const uno::Reference<document::XTypeDetection> xTypeDetection(
63 m_xFactory->createInstanceWithContext("com.sun.star.document.TypeDetection", m_xContext),
64 uno::UNO_QUERY_THROW);
65 m_xTypeMap.set(xTypeDetection, uno::UNO_QUERY_THROW);
68 void WpftImportTestBase::tearDown()
70 m_xDesktop->terminate();
72 ::test::BootstrapFixture::tearDown();
75 bool WpftImportTestBase::load(const OUString &, const OUString &rURL, const OUString &,
76 SfxFilterFlags, SotClipboardFormatId, unsigned int)
78 // create an empty frame
79 const uno::Reference<lang::XComponent> xDoc(
80 m_xDesktop->loadComponentFromURL(m_aFactoryURL, "_blank", 0, uno::Sequence<beans::PropertyValue>()),
81 uno::UNO_QUERY_THROW);
83 // Find the model and frame. We need them later.
84 uno::Reference<frame::XFrame> xFrame(xDoc, uno::UNO_QUERY);
85 uno::Reference<frame::XModel> xModel(xDoc, uno::UNO_QUERY);
86 uno::Reference<frame::XController> xController(xDoc, uno::UNO_QUERY);
88 if (xFrame.is())
90 xController = xFrame->getController();
91 xModel = xController->getModel();
93 else if (xModel.is())
95 xController = xModel->getCurrentController();
96 xFrame = xController->getFrame();
98 else if (xController.is())
100 xFrame = xController->getFrame();
101 xModel = xController->getModel();
104 if (!xFrame.is() || !xModel.is())
105 throw uno::RuntimeException();
107 bool result = false;
109 // try to import the document (and load it into the prepared frame)
112 const uno::Reference<document::XImporter> xImporter(m_xFilter, uno::UNO_QUERY_THROW);
114 xImporter->setTargetDocument(xDoc);
116 uno::Sequence<beans::PropertyValue> aDescriptor(3);
117 ucbhelper::Content aContent(rURL, uno::Reference<ucb::XCommandEnvironment>(), m_xContext);
119 aDescriptor[0].Name = "URL";
120 aDescriptor[0].Value <<= rURL;
121 aDescriptor[1].Name = "InputStream";
122 aDescriptor[1].Value <<= aContent.openStream();
123 aDescriptor[2].Name = "UCBContent";
124 aDescriptor[2].Value <<= aContent.get();
126 const uno::Reference<document::XExtendedFilterDetection> xDetector(m_xFilter, uno::UNO_QUERY_THROW);
128 const rtl::OUString aTypeName(xDetector->detect(aDescriptor));
129 if (aTypeName.isEmpty())
130 throw lang::IllegalArgumentException();
132 impl_detectFilterName(aDescriptor, aTypeName);
134 xModel->lockControllers();
135 result = m_xFilter->filter(aDescriptor);
136 xModel->unlockControllers();
138 catch (const uno::Exception &)
140 // ignore
143 // close the opened document
146 uno::Reference<util::XCloseable> xCloseable(xFrame, uno::UNO_QUERY);
147 if (xCloseable.is())
148 xCloseable->close(true);
149 else
150 xDoc->dispose();
152 catch (const uno::Exception &)
154 // ignore
157 return result;
160 void WpftImportTestBase::doTest(const rtl::OUString &rFilter, const rtl::OUString &rPath)
162 m_xFilter.set(m_xFactory->createInstanceWithContext(rFilter, m_xContext), uno::UNO_QUERY_THROW);
163 testDir(OUString(), getURLFromSrc(rPath), OUString());
166 void WpftImportTestBase::impl_detectFilterName(uno::Sequence<beans::PropertyValue> &rDescriptor, const rtl::OUString &rTypeName)
168 const sal_Int32 nDescriptorLen = rDescriptor.getLength();
170 for (sal_Int32 n = 0; nDescriptorLen != n; ++n)
172 if ("FilterName" == rDescriptor[n].Name)
173 return;
176 uno::Sequence<beans::PropertyValue> aTypes;
177 if (m_xTypeMap->getByName(rTypeName) >>= aTypes)
179 for (sal_Int32 n = 0; aTypes.getLength() != n; ++n)
181 rtl::OUString aFilterName;
182 if (("PreferredFilter" == aTypes[n].Name) && (aTypes[n].Value >>= aFilterName))
184 rDescriptor.realloc(nDescriptorLen + 1);
185 rDescriptor[nDescriptorLen].Name = "FilterName";
186 rDescriptor[nDescriptorLen].Value <<= aFilterName;
187 return;
192 throw container::NoSuchElementException();
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */