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 <test/bootstrapfixture.hxx>
11 #include <unotest/macros_test.hxx>
12 
13 #include <com/sun/star/document/XExtendedFilterDetection.hpp>
14 #include <com/sun/star/frame/Desktop.hpp>
15 #include <com/sun/star/lang/XServiceInfo.hpp>
16 
17 #include <comphelper/propertyvalue.hxx>
18 #include <unotools/mediadescriptor.hxx>
19 #include <unotools/streamwrap.hxx>
20 #include <tools/stream.hxx>
21 
22 namespace com::sun::star::io
23 {
24 class XInputStream;
25 }
26 
27 using namespace com::sun::star;
28 
29 namespace
30 {
31 /// Test class for PlainTextFilterDetect.
32 class TextFilterDetectTest : public test::BootstrapFixture, public unotest::MacrosTest
33 {
34     uno::Reference<lang::XComponent> mxComponent;
35 
36 public:
37     void setUp() override;
38     void tearDown() override;
getComponent()39     uno::Reference<lang::XComponent>& getComponent() { return mxComponent; }
40 };
41 
setUp()42 void TextFilterDetectTest::setUp()
43 {
44     test::BootstrapFixture::setUp();
45 
46     mxDesktop.set(frame::Desktop::create(mxComponentContext));
47 }
48 
tearDown()49 void TextFilterDetectTest::tearDown()
50 {
51     if (mxComponent.is())
52         mxComponent->dispose();
53 
54     test::BootstrapFixture::tearDown();
55 }
56 
57 constexpr OUStringLiteral DATA_DIRECTORY = u"/filter/qa/unit/data/";
58 
CPPUNIT_TEST_FIXTURE(TextFilterDetectTest,testTdf114428)59 CPPUNIT_TEST_FIXTURE(TextFilterDetectTest, testTdf114428)
60 {
61     uno::Reference<document::XExtendedFilterDetection> xDetect(
62         getMultiServiceFactory()->createInstance("com.sun.star.comp.filters.PlainTextFilterDetect"),
63         uno::UNO_QUERY);
64     OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "tdf114428.xhtml";
65     SvFileStream aStream(aURL, StreamMode::READ);
66     uno::Reference<io::XInputStream> xStream(new utl::OStreamWrapper(aStream));
67     uno::Sequence<beans::PropertyValue> aDescriptor
68         = { comphelper::makePropertyValue("DocumentService",
69                                           OUString("com.sun.star.text.TextDocument")),
70             comphelper::makePropertyValue("InputStream", xStream),
71             comphelper::makePropertyValue("TypeName", OUString("generic_HTML")) };
72     xDetect->detect(aDescriptor);
73     utl::MediaDescriptor aMediaDesc(aDescriptor);
74     OUString aFilterName = aMediaDesc.getUnpackedValueOrDefault("FilterName", OUString());
75     // This was empty, XML declaration caused HTML detect to not handle XHTML.
76     CPPUNIT_ASSERT_EQUAL(OUString("HTML (StarWriter)"), aFilterName);
77 }
78 
CPPUNIT_TEST_FIXTURE(TextFilterDetectTest,testEmptyFile)79 CPPUNIT_TEST_FIXTURE(TextFilterDetectTest, testEmptyFile)
80 {
81     // Given an empty file, with a pptx extension
82     OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "empty.pptx";
83 
84     // When loading the file
85     getComponent() = loadFromDesktop(aURL);
86 
87     // Then make sure it is opened in Impress.
88     uno::Reference<lang::XServiceInfo> xServiceInfo(getComponent(), uno::UNO_QUERY);
89     CPPUNIT_ASSERT(xServiceInfo.is());
90 
91     // Without the accompanying fix in place, this test would have failed, as it was opened in
92     // Writer instead.
93     CPPUNIT_ASSERT(xServiceInfo->supportsService("com.sun.star.presentation.PresentationDocument"));
94 
95     getComponent()->dispose();
96 
97     // Now also test ODT
98     aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "empty.odt";
99     getComponent() = loadFromDesktop(aURL);
100     xServiceInfo.set(getComponent(), uno::UNO_QUERY);
101     CPPUNIT_ASSERT(xServiceInfo.is());
102     // Make sure it opens in Writer.
103     CPPUNIT_ASSERT(xServiceInfo->supportsService("com.sun.star.text.TextDocument"));
104     getComponent()->dispose();
105 
106     // ... and ODP
107     aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "empty.odp";
108     getComponent() = loadFromDesktop(aURL);
109     xServiceInfo.set(getComponent(), uno::UNO_QUERY);
110     CPPUNIT_ASSERT(xServiceInfo.is());
111     // Without the accompanying fix in place, this test would have failed, as it was opened in
112     // Writer instead.
113     CPPUNIT_ASSERT(xServiceInfo->supportsService("com.sun.star.presentation.PresentationDocument"));
114     getComponent()->dispose();
115 }
116 }
117 
118 CPPUNIT_PLUGIN_IMPLEMENT();
119 
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
121