LCOV - code coverage report
Current view: top level - writerperfect/qa/unit - WpftImportTestBase.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 69 78 88.5 %
Date: 2015-06-13 12:38:46 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           4 : WpftImportTestBase::WpftImportTestBase(const rtl::OUString &rFactoryURL)
      47             :     : ::test::FiltersTest()
      48             :     , ::test::BootstrapFixture()
      49             :     , m_aFactoryURL(rFactoryURL)
      50             :     , m_xDesktop()
      51             :     , m_xFilter()
      52           4 :     , m_xTypeMap()
      53             : {
      54           4 : }
      55             : 
      56           4 : void WpftImportTestBase::setUp()
      57             : {
      58           4 :     ::test::BootstrapFixture::setUp();
      59             : 
      60           4 :     m_xDesktop = frame::theDesktop::get(m_xContext);
      61             : 
      62             :     const uno::Reference<document::XTypeDetection> xTypeDetection(
      63           4 :         m_xFactory->createInstanceWithContext("com.sun.star.document.TypeDetection", m_xContext),
      64           4 :         uno::UNO_QUERY_THROW);
      65           4 :     m_xTypeMap.set(xTypeDetection, uno::UNO_QUERY_THROW);
      66           4 : }
      67             : 
      68           4 : void WpftImportTestBase::tearDown()
      69             : {
      70           4 :     m_xDesktop->terminate();
      71             : 
      72           4 :     ::test::BootstrapFixture::tearDown();
      73           4 : }
      74             : 
      75         123 : bool WpftImportTestBase::load(const OUString &, const OUString &rURL, const OUString &,
      76             :                               SfxFilterFlags, SotClipboardFormatId, unsigned int)
      77             : {
      78             :     // create an empty frame
      79             :     const uno::Reference<lang::XComponent> xDoc(
      80         246 :         m_xDesktop->loadComponentFromURL(m_aFactoryURL, "_blank", 0, uno::Sequence<beans::PropertyValue>()),
      81         246 :         uno::UNO_QUERY_THROW);
      82             : 
      83             :     // Find the model and frame. We need them later.
      84         246 :     uno::Reference<frame::XFrame> xFrame(xDoc, uno::UNO_QUERY);
      85         246 :     uno::Reference<frame::XModel> xModel(xDoc, uno::UNO_QUERY);
      86         246 :     uno::Reference<frame::XController> xController(xDoc, uno::UNO_QUERY);
      87             : 
      88         123 :     if (xFrame.is())
      89             :     {
      90           0 :         xController = xFrame->getController();
      91           0 :         xModel = xController->getModel();
      92             :     }
      93         123 :     else if (xModel.is())
      94             :     {
      95         123 :         xController = xModel->getCurrentController();
      96         123 :         xFrame = xController->getFrame();
      97             :     }
      98           0 :     else if (xController.is())
      99             :     {
     100           0 :         xFrame = xController->getFrame();
     101           0 :         xModel = xController->getModel();
     102             :     }
     103             : 
     104         123 :     if (!xFrame.is() || !xModel.is())
     105           0 :         throw uno::RuntimeException();
     106             : 
     107         123 :     bool result = false;
     108             : 
     109             :     // try to import the document (and load it into the prepared frame)
     110             :     try
     111             :     {
     112         123 :         const uno::Reference<document::XImporter> xImporter(m_xFilter, uno::UNO_QUERY_THROW);
     113             : 
     114         123 :         xImporter->setTargetDocument(xDoc);
     115             : 
     116         246 :         uno::Sequence<beans::PropertyValue> aDescriptor(3);
     117         246 :         ucbhelper::Content aContent(rURL, uno::Reference<ucb::XCommandEnvironment>(), m_xContext);
     118             : 
     119         123 :         aDescriptor[0].Name = "URL";
     120         123 :         aDescriptor[0].Value <<= rURL;
     121         123 :         aDescriptor[1].Name = "InputStream";
     122         123 :         aDescriptor[1].Value <<= aContent.openStream();
     123         123 :         aDescriptor[2].Name = "UCBContent";
     124         123 :         aDescriptor[2].Value <<= aContent.get();
     125             : 
     126         246 :         const uno::Reference<document::XExtendedFilterDetection> xDetector(m_xFilter, uno::UNO_QUERY_THROW);
     127             : 
     128         246 :         const rtl::OUString aTypeName(xDetector->detect(aDescriptor));
     129         123 :         if (aTypeName.isEmpty())
     130           2 :             throw lang::IllegalArgumentException();
     131             : 
     132         121 :         impl_detectFilterName(aDescriptor, aTypeName);
     133             : 
     134         121 :         xModel->lockControllers();
     135         121 :         result = m_xFilter->filter(aDescriptor);
     136         244 :         xModel->unlockControllers();
     137             :     }
     138           2 :     catch (const uno::Exception &)
     139             :     {
     140             :         // ignore
     141             :     }
     142             : 
     143             :     // close the opened document
     144             :     try
     145             :     {
     146         123 :         uno::Reference<util::XCloseable> xCloseable(xFrame, uno::UNO_QUERY);
     147         123 :         if (xCloseable.is())
     148         123 :             xCloseable->close(true);
     149             :         else
     150           0 :             xDoc->dispose();
     151             :     }
     152           0 :     catch (const uno::Exception &)
     153             :     {
     154             :         // ignore
     155             :     }
     156             : 
     157         246 :     return result;
     158             : }
     159             : 
     160          18 : void WpftImportTestBase::doTest(const rtl::OUString &rFilter, const rtl::OUString &rPath)
     161             : {
     162          18 :     m_xFilter.set(m_xFactory->createInstanceWithContext(rFilter, m_xContext), uno::UNO_QUERY_THROW);
     163          18 :     testDir(OUString(), getURLFromSrc(rPath), OUString());
     164          18 : }
     165             : 
     166         121 : void WpftImportTestBase::impl_detectFilterName(uno::Sequence<beans::PropertyValue> &rDescriptor, const rtl::OUString &rTypeName)
     167             : {
     168         121 :     const sal_Int32 nDescriptorLen = rDescriptor.getLength();
     169             : 
     170         605 :     for (sal_Int32 n = 0; nDescriptorLen != n; ++n)
     171             :     {
     172         484 :         if ("FilterName" == rDescriptor[n].Name)
     173           0 :             return;
     174             :     }
     175             : 
     176         121 :     uno::Sequence<beans::PropertyValue> aTypes;
     177         121 :     if (m_xTypeMap->getByName(rTypeName) >>= aTypes)
     178             :     {
     179        1452 :         for (sal_Int32 n = 0; aTypes.getLength() != n; ++n)
     180             :         {
     181         726 :             rtl::OUString aFilterName;
     182         726 :             if (("PreferredFilter" == aTypes[n].Name) && (aTypes[n].Value >>= aFilterName))
     183             :             {
     184         121 :                 rDescriptor.realloc(nDescriptorLen + 1);
     185         121 :                 rDescriptor[nDescriptorLen].Name = "FilterName";
     186         121 :                 rDescriptor[nDescriptorLen].Value <<= aFilterName;
     187         121 :                 return;
     188             :             }
     189         605 :         }
     190             :     }
     191             : 
     192         121 :     throw container::NoSuchElementException();
     193             : }
     194             : 
     195             : }
     196          12 : }
     197             : 
     198             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11