LCOV - code coverage report
Current view: top level - writerperfect/qa/unit - WpftImportTestBase.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 69 78 88.5 %
Date: 2014-11-03 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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             :  */
       9             : 
      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>
      26             : 
      27             : #include <ucbhelper/content.hxx>
      28             : 
      29             : #include "WpftImportTestBase.hxx"
      30             : 
      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;
      40             : 
      41             : namespace writerperfect
      42             : {
      43             : namespace test
      44             : {
      45             : 
      46           8 : WpftImportTestBase::WpftImportTestBase(const rtl::OUString &rFactoryURL)
      47             :     : ::test::FiltersTest()
      48             :     , ::test::BootstrapFixture()
      49             :     , m_aFactoryURL(rFactoryURL)
      50             :     , m_xDesktop()
      51             :     , m_xFilter()
      52           8 :     , m_xTypeMap()
      53             : {
      54           8 : }
      55             : 
      56           8 : void WpftImportTestBase::setUp()
      57             : {
      58           8 :     ::test::BootstrapFixture::setUp();
      59             : 
      60           8 :     m_xDesktop = frame::theDesktop::get(m_xContext);
      61             : 
      62             :     const uno::Reference<document::XTypeDetection> xTypeDetection(
      63           8 :         m_xFactory->createInstanceWithContext("com.sun.star.document.TypeDetection", m_xContext),
      64           8 :         uno::UNO_QUERY_THROW);
      65           8 :     m_xTypeMap.set(xTypeDetection, uno::UNO_QUERY_THROW);
      66           8 : }
      67             : 
      68           8 : void WpftImportTestBase::tearDown()
      69             : {
      70           8 :     m_xDesktop->terminate();
      71             : 
      72           8 :     ::test::BootstrapFixture::tearDown();
      73           8 : }
      74             : 
      75         230 : bool WpftImportTestBase::load(const OUString &, const OUString &rURL, const OUString &,
      76             :                               unsigned int, unsigned int, unsigned int)
      77             : {
      78             :     // create an empty frame
      79             :     const uno::Reference<lang::XComponent> xDoc(
      80         460 :         m_xDesktop->loadComponentFromURL(m_aFactoryURL, "_blank", 0, uno::Sequence<beans::PropertyValue>()),
      81         460 :         uno::UNO_QUERY_THROW);
      82             : 
      83             :     // Find the model and frame. We need them later.
      84         460 :     uno::Reference<frame::XFrame> xFrame(xDoc, uno::UNO_QUERY);
      85         460 :     uno::Reference<frame::XModel> xModel(xDoc, uno::UNO_QUERY);
      86         460 :     uno::Reference<frame::XController> xController(xDoc, uno::UNO_QUERY);
      87             : 
      88         230 :     if (xFrame.is())
      89             :     {
      90           0 :         xController = xFrame->getController();
      91           0 :         xModel = xController->getModel();
      92             :     }
      93         230 :     else if (xModel.is())
      94             :     {
      95         230 :         xController = xModel->getCurrentController();
      96         230 :         xFrame = xController->getFrame();
      97             :     }
      98           0 :     else if (xController.is())
      99             :     {
     100           0 :         xFrame = xController->getFrame();
     101           0 :         xModel = xController->getModel();
     102             :     }
     103             : 
     104         230 :     if (!xFrame.is() || !xModel.is())
     105           0 :         throw uno::RuntimeException();
     106             : 
     107         230 :     bool result = false;
     108             : 
     109             :     // try to import the document (and load it into the prepared frame)
     110             :     try
     111             :     {
     112         230 :         const uno::Reference<document::XImporter> xImporter(m_xFilter, uno::UNO_QUERY_THROW);
     113             : 
     114         230 :         xImporter->setTargetDocument(xDoc);
     115             : 
     116         460 :         uno::Sequence<beans::PropertyValue> aDescriptor(3);
     117         460 :         ucbhelper::Content aContent(rURL, uno::Reference<ucb::XCommandEnvironment>(), m_xContext);
     118             : 
     119         230 :         aDescriptor[0].Name = "URL";
     120         230 :         aDescriptor[0].Value <<= rURL;
     121         230 :         aDescriptor[1].Name = "InputStream";
     122         230 :         aDescriptor[1].Value <<= aContent.openStream();
     123         230 :         aDescriptor[2].Name = "UCBContent";
     124         230 :         aDescriptor[2].Value <<= aContent.get();
     125             : 
     126         460 :         const uno::Reference<document::XExtendedFilterDetection> xDetector(m_xFilter, uno::UNO_QUERY_THROW);
     127             : 
     128         460 :         const rtl::OUString aTypeName(xDetector->detect(aDescriptor));
     129         230 :         if (aTypeName.isEmpty())
     130           4 :             throw lang::IllegalArgumentException();
     131             : 
     132         226 :         impl_detectFilterName(aDescriptor, aTypeName);
     133             : 
     134         226 :         xModel->lockControllers();
     135         226 :         result = m_xFilter->filter(aDescriptor);
     136         456 :         xModel->unlockControllers();
     137             :     }
     138           4 :     catch (const uno::Exception &)
     139             :     {
     140             :         // ignore
     141             :     }
     142             : 
     143             :     // close the opened document
     144             :     try
     145             :     {
     146         230 :         uno::Reference<util::XCloseable> xCloseable(xFrame, uno::UNO_QUERY);
     147         230 :         if (xCloseable.is())
     148         230 :             xCloseable->close(true);
     149             :         else
     150           0 :             xDoc->dispose();
     151             :     }
     152           0 :     catch (const uno::Exception &)
     153             :     {
     154             :         // ignore
     155             :     }
     156             : 
     157         460 :     return result;
     158             : }
     159             : 
     160          32 : void WpftImportTestBase::doTest(const rtl::OUString &rFilter, const rtl::OUString &rPath)
     161             : {
     162          32 :     m_xFilter.set(m_xFactory->createInstanceWithContext(rFilter, m_xContext), uno::UNO_QUERY_THROW);
     163          32 :     testDir(OUString(), getURLFromSrc(rPath), OUString());
     164          32 : }
     165             : 
     166         226 : void WpftImportTestBase::impl_detectFilterName(uno::Sequence<beans::PropertyValue> &rDescriptor, const rtl::OUString &rTypeName)
     167             : {
     168         226 :     const sal_Int32 nDescriptorLen = rDescriptor.getLength();
     169             : 
     170        1130 :     for (sal_Int32 n = 0; nDescriptorLen != n; ++n)
     171             :     {
     172         904 :         if ("FilterName" == rDescriptor[n].Name)
     173           0 :             return;
     174             :     }
     175             : 
     176         226 :     uno::Sequence<beans::PropertyValue> aTypes;
     177         226 :     if (m_xTypeMap->getByName(rTypeName) >>= aTypes)
     178             :     {
     179        4972 :         for (sal_Int32 n = 0; aTypes.getLength() != n; ++n)
     180             :         {
     181        2486 :             rtl::OUString aFilterName;
     182        2486 :             if (("PreferredFilter" == aTypes[n].Name) && (aTypes[n].Value >>= aFilterName))
     183             :             {
     184         226 :                 rDescriptor.realloc(nDescriptorLen + 1);
     185         226 :                 rDescriptor[nDescriptorLen].Name = "FilterName";
     186         226 :                 rDescriptor[nDescriptorLen].Value <<= aFilterName;
     187         226 :                 return;
     188             :             }
     189        2260 :         }
     190             :     }
     191             : 
     192         226 :     throw container::NoSuchElementException();
     193             : }
     194             : 
     195             : }
     196          24 : }
     197             : 
     198             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10