summaryrefslogtreecommitdiff
path: root/writerperfect/qa/unit/WpftImportTestBase.cxx
blob: 740f55bdb61a4533497a1d78d998c25c43c90342 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* -*- 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 <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/container/NoSuchElementException.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/document/XExtendedFilterDetection.hpp>
#include <com/sun/star/document/XFilter.hpp>
#include <com/sun/star/document/XImporter.hpp>
#include <com/sun/star/document/XTypeDetection.hpp>
#include <com/sun/star/frame/theDesktop.hpp>
#include <com/sun/star/frame/XController.hpp>
#include <com/sun/star/frame/XFrame.hpp>
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/ucb/XContent.hpp>
#include <com/sun/star/util/XCloseable.hpp>

#include <ucbhelper/content.hxx>

#include "WpftImportTestBase.hxx"

namespace beans = com::sun::star::beans;
namespace container = com::sun::star::container;
namespace document = com::sun::star::document;
namespace frame = com::sun::star::frame;
namespace io = com::sun::star::io;
namespace lang = com::sun::star::lang;
namespace ucb = com::sun::star::ucb;
namespace uno = com::sun::star::uno;
namespace util = com::sun::star::util;

namespace writerperfect
{
namespace test
{

WpftImportTestBase::WpftImportTestBase(const rtl::OUString &rFactoryURL)
    : ::test::FiltersTest()
    , ::test::BootstrapFixture()
    , m_aFactoryURL(rFactoryURL)
    , m_xDesktop()
    , m_xFilter()
    , m_xTypeMap()
{
}

void WpftImportTestBase::setUp()
{
    ::test::BootstrapFixture::setUp();

    m_xDesktop = frame::theDesktop::get(m_xContext);

    const uno::Reference<document::XTypeDetection> xTypeDetection(
        m_xFactory->createInstanceWithContext("com.sun.star.document.TypeDetection", m_xContext),
        uno::UNO_QUERY_THROW);
    m_xTypeMap.set(xTypeDetection, uno::UNO_QUERY_THROW);
}

void WpftImportTestBase::tearDown()
{
    m_xDesktop->terminate();

    ::test::BootstrapFixture::tearDown();
}

bool WpftImportTestBase::load(const OUString &, const OUString &rURL, const OUString &,
                              unsigned int, SotClipboardFormatId, unsigned int)
{
    // create an empty frame
    const uno::Reference<lang::XComponent> xDoc(
        m_xDesktop->loadComponentFromURL(m_aFactoryURL, "_blank", 0, uno::Sequence<beans::PropertyValue>()),
        uno::UNO_QUERY_THROW);

    // Find the model and frame. We need them later.
    uno::Reference<frame::XFrame> xFrame(xDoc, uno::UNO_QUERY);
    uno::Reference<frame::XModel> xModel(xDoc, uno::UNO_QUERY);
    uno::Reference<frame::XController> xController(xDoc, uno::UNO_QUERY);

    if (xFrame.is())
    {
        xController = xFrame->getController();
        xModel = xController->getModel();
    }
    else if (xModel.is())
    {
        xController = xModel->getCurrentController();
        xFrame = xController->getFrame();
    }
    else if (xController.is())
    {
        xFrame = xController->getFrame();
        xModel = xController->getModel();
    }

    if (!xFrame.is() || !xModel.is())
        throw uno::RuntimeException();

    bool result = false;

    // try to import the document (and load it into the prepared frame)
    try
    {
        const uno::Reference<document::XImporter> xImporter(m_xFilter, uno::UNO_QUERY_THROW);

        xImporter->setTargetDocument(xDoc);

        uno::Sequence<beans::PropertyValue> aDescriptor(3);
        ucbhelper::Content aContent(rURL, uno::Reference<ucb::XCommandEnvironment>(), m_xContext);

        aDescriptor[0].Name = "URL";
        aDescriptor[0].Value <<= rURL;
        aDescriptor[1].Name = "InputStream";
        aDescriptor[1].Value <<= aContent.openStream();
        aDescriptor[2].Name = "UCBContent";
        aDescriptor[2].Value <<= aContent.get();

        const uno::Reference<document::XExtendedFilterDetection> xDetector(m_xFilter, uno::UNO_QUERY_THROW);

        const rtl::OUString aTypeName(xDetector->detect(aDescriptor));
        if (aTypeName.isEmpty())
            throw lang::IllegalArgumentException();

        impl_detectFilterName(aDescriptor, aTypeName);

        xModel->lockControllers();
        result = m_xFilter->filter(aDescriptor);
        xModel->unlockControllers();
    }
    catch (const uno::Exception &)
    {
        // ignore
    }

    // close the opened document
    try
    {
        uno::Reference<util::XCloseable> xCloseable(xFrame, uno::UNO_QUERY);
        if (xCloseable.is())
            xCloseable->close(true);
        else
            xDoc->dispose();
    }
    catch (const uno::Exception &)
    {
        // ignore
    }

    return result;
}

void WpftImportTestBase::doTest(const rtl::OUString &rFilter, const rtl::OUString &rPath)
{
    m_xFilter.set(m_xFactory->createInstanceWithContext(rFilter, m_xContext), uno::UNO_QUERY_THROW);
    testDir(OUString(), getURLFromSrc(rPath), OUString());
}

void WpftImportTestBase::impl_detectFilterName(uno::Sequence<beans::PropertyValue> &rDescriptor, const rtl::OUString &rTypeName)
{
    const sal_Int32 nDescriptorLen = rDescriptor.getLength();

    for (sal_Int32 n = 0; nDescriptorLen != n; ++n)
    {
        if ("FilterName" == rDescriptor[n].Name)
            return;
    }

    uno::Sequence<beans::PropertyValue> aTypes;
    if (m_xTypeMap->getByName(rTypeName) >>= aTypes)
    {
        for (sal_Int32 n = 0; aTypes.getLength() != n; ++n)
        {
            rtl::OUString aFilterName;
            if (("PreferredFilter" == aTypes[n].Name) && (aTypes[n].Value >>= aFilterName))
            {
                rDescriptor.realloc(nDescriptorLen + 1);
                rDescriptor[nDescriptorLen].Name = "FilterName";
                rDescriptor[nDescriptorLen].Value <<= aFilterName;
                return;
            }
        }
    }

    throw container::NoSuchElementException();
}

}
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */