remove assert looking for new compatibilityMode DOCX
[LibreOffice.git] / filter / qa / unit / textfilterdetect.cxx
blob8c13da5461fd2b58d812380931ef2c25e0a66780
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 <test/unoapi_test.hxx>
12 #include <com/sun/star/document/XExtendedFilterDetection.hpp>
13 #include <com/sun/star/lang/XServiceInfo.hpp>
14 #include <com/sun/star/document/XTypeDetection.hpp>
15 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
16 #include <com/sun/star/sheet/XCellRangesAccess.hpp>
17 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
18 #include <com/sun/star/text/XTextDocument.hpp>
20 #include <comphelper/configuration.hxx>
21 #include <comphelper/propertyvalue.hxx>
22 #include <officecfg/Office/Common.hxx>
23 #include <osl/file.hxx>
24 #include <sfx2/docfac.hxx>
25 #include <unotools/mediadescriptor.hxx>
26 #include <unotools/streamwrap.hxx>
27 #include <tools/stream.hxx>
29 namespace com::sun::star::io
31 class XInputStream;
34 using namespace com::sun::star;
36 namespace
38 bool supportsService(const uno::Reference<lang::XComponent>& x, const OUString& s)
40 return uno::Reference<lang::XServiceInfo>(x, uno::UNO_QUERY_THROW)->supportsService(s);
43 /// Test class for PlainTextFilterDetect.
44 class TextFilterDetectTest : public UnoApiTest
46 public:
47 TextFilterDetectTest()
48 : UnoApiTest(u"/filter/qa/unit/data/"_ustr)
53 CPPUNIT_TEST_FIXTURE(TextFilterDetectTest, testTdf114428)
55 uno::Reference<document::XExtendedFilterDetection> xDetect(
56 getMultiServiceFactory()->createInstance(
57 u"com.sun.star.comp.filters.PlainTextFilterDetect"_ustr),
58 uno::UNO_QUERY);
59 OUString aURL = createFileURL(u"tdf114428.xhtml");
60 SvFileStream aStream(aURL, StreamMode::READ);
61 uno::Reference<io::XInputStream> xStream(new utl::OStreamWrapper(aStream));
62 uno::Sequence<beans::PropertyValue> aDescriptor
63 = { comphelper::makePropertyValue(u"DocumentService"_ustr,
64 u"com.sun.star.text.TextDocument"_ustr),
65 comphelper::makePropertyValue(u"InputStream"_ustr, xStream),
66 comphelper::makePropertyValue(u"TypeName"_ustr, u"generic_HTML"_ustr) };
67 xDetect->detect(aDescriptor);
68 utl::MediaDescriptor aMediaDesc(aDescriptor);
69 OUString aFilterName = aMediaDesc.getUnpackedValueOrDefault(u"FilterName"_ustr, OUString());
70 // This was empty, XML declaration caused HTML detect to not handle XHTML.
71 CPPUNIT_ASSERT_EQUAL(u"HTML (StarWriter)"_ustr, aFilterName);
74 CPPUNIT_TEST_FIXTURE(TextFilterDetectTest, testEmptyFile)
76 // Given an empty file, with a pptx extension
77 // When loading the file
78 loadFromFile(u"empty.pptx");
80 // Then make sure it is opened in Impress.
81 // Without the accompanying fix in place, this test would have failed, as it was opened in
82 // Writer instead.
83 CPPUNIT_ASSERT(
84 supportsService(mxComponent, u"com.sun.star.presentation.PresentationDocument"_ustr));
86 // Now also test ODT
87 loadFromFile(u"empty.odt");
88 // Make sure it opens in Writer.
89 CPPUNIT_ASSERT(supportsService(mxComponent, u"com.sun.star.text.TextDocument"_ustr));
91 // ... and ODS
92 loadFromFile(u"empty.ods");
93 // Make sure it opens in Calc.
94 CPPUNIT_ASSERT(supportsService(mxComponent, u"com.sun.star.sheet.SpreadsheetDocument"_ustr));
96 // ... and ODP
97 loadFromFile(u"empty.odp");
98 // Without the accompanying fix in place, this test would have failed, as it was opened in
99 // Writer instead.
100 CPPUNIT_ASSERT(
101 supportsService(mxComponent, u"com.sun.star.presentation.PresentationDocument"_ustr));
103 // ... and DOC
104 // Without the accompanying fix in place, this test would have failed, the import filter aborted
105 // loading.
106 loadFromFile(u"empty.doc");
107 CPPUNIT_ASSERT(supportsService(mxComponent, u"com.sun.star.text.TextDocument"_ustr));
109 uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
110 uno::Sequence<beans::PropertyValue> aArgs = xModel->getArgs();
111 comphelper::SequenceAsHashMap aMap(aArgs);
112 OUString aFilterName;
113 aMap[u"FilterName"_ustr] >>= aFilterName;
114 // Without the accompanying fix in place, this test would have failed with:
115 // - Expected: MS Word 97
116 // - Actual : MS WinWord 6.0
117 // i.e. opening worked, but saving back failed instead of producing a WW8 binary file.
118 CPPUNIT_ASSERT_EQUAL(u"MS Word 97"_ustr, aFilterName);
121 // Now test with default templates set
123 SfxObjectFactory::SetStandardTemplate(u"com.sun.star.presentation.PresentationDocument"_ustr,
124 createFileURL(u"impress.otp"));
125 SfxObjectFactory::SetStandardTemplate(u"com.sun.star.text.TextDocument"_ustr,
126 createFileURL(u"writer.ott"));
127 SfxObjectFactory::SetStandardTemplate(u"com.sun.star.sheet.SpreadsheetDocument"_ustr,
128 createFileURL(u"calc.ots"));
130 loadFromFile(u"empty.pptx");
132 uno::Reference<drawing::XDrawPagesSupplier> xDoc(mxComponent, uno::UNO_QUERY_THROW);
133 uno::Reference<drawing::XDrawPages> xPages(xDoc->getDrawPages(), uno::UNO_SET_THROW);
134 uno::Reference<drawing::XDrawPage> xPage(xPages->getByIndex(0), uno::UNO_QUERY_THROW);
135 uno::Reference<text::XTextRange> xBox(xPage->getByIndex(0), uno::UNO_QUERY_THROW);
137 // Make sure the template's text was loaded
138 CPPUNIT_ASSERT_EQUAL(u"Title of Impress template"_ustr, xBox->getString());
141 loadFromFile(u"empty.odt");
143 uno::Reference<text::XTextDocument> xDoc(mxComponent, uno::UNO_QUERY_THROW);
144 uno::Reference<container::XEnumerationAccess> xEA(xDoc->getText(), uno::UNO_QUERY_THROW);
145 uno::Reference<container::XEnumeration> xEnum(xEA->createEnumeration(), uno::UNO_SET_THROW);
146 uno::Reference<text::XTextRange> xParagraph(xEnum->nextElement(), uno::UNO_QUERY_THROW);
148 // Make sure the template's text was loaded
149 CPPUNIT_ASSERT_EQUAL(u"Writer template’s first line"_ustr, xParagraph->getString());
152 loadFromFile(u"empty.ods");
154 uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, uno::UNO_QUERY_THROW);
155 uno::Reference<sheet::XCellRangesAccess> xRA(xDoc->getSheets(), uno::UNO_QUERY_THROW);
156 uno::Reference<text::XTextRange> xC(xRA->getCellByPosition(0, 0, 0), uno::UNO_QUERY_THROW);
158 // Make sure the template's text was loaded
159 CPPUNIT_ASSERT_EQUAL(u"Calc template’s first cell"_ustr, xC->getString());
162 loadFromFile(u"empty.odp");
164 uno::Reference<drawing::XDrawPagesSupplier> xDoc(mxComponent, uno::UNO_QUERY_THROW);
165 uno::Reference<drawing::XDrawPages> xPages(xDoc->getDrawPages(), uno::UNO_SET_THROW);
166 uno::Reference<drawing::XDrawPage> xPage(xPages->getByIndex(0), uno::UNO_QUERY_THROW);
167 uno::Reference<text::XTextRange> xBox(xPage->getByIndex(0), uno::UNO_QUERY_THROW);
169 // Make sure the template's text was loaded
170 CPPUNIT_ASSERT_EQUAL(u"Title of Impress template"_ustr, xBox->getString());
172 loadFromFile(u"empty.doc");
174 uno::Reference<text::XTextDocument> xDoc(mxComponent, uno::UNO_QUERY_THROW);
175 uno::Reference<container::XEnumerationAccess> xEA(xDoc->getText(), uno::UNO_QUERY_THROW);
176 uno::Reference<container::XEnumeration> xEnum(xEA->createEnumeration(), uno::UNO_SET_THROW);
177 uno::Reference<text::XTextRange> xParagraph(xEnum->nextElement(), uno::UNO_QUERY_THROW);
179 // Make sure the template's text was loaded
180 CPPUNIT_ASSERT_EQUAL(u"Writer template’s first line"_ustr, xParagraph->getString());
184 // The unit test fails on some Linux systems. Until it is found out why the file URLs are broken
185 // there, let it be Windows-only, since the original issue tested here was Windows-specific.
186 // See https://lists.freedesktop.org/archives/libreoffice/2023-December/091265.html for details.
187 #ifdef _WIN32
188 CPPUNIT_TEST_FIXTURE(TextFilterDetectTest, testHybridPDFFile)
190 // Make sure that file locking is ON
192 std::shared_ptr<comphelper::ConfigurationChanges> xChanges(
193 comphelper::ConfigurationChanges::create());
194 officecfg::Office::Common::Misc::UseDocumentSystemFileLocking::set(true, xChanges);
195 xChanges->commit();
198 // Given a hybrid PDF file
201 // Created in Writer
202 utl::TempFileNamed nonAsciiName(u"абв_αβγ_");
203 nonAsciiName.EnableKillingFile();
204 CPPUNIT_ASSERT_EQUAL(
205 osl::FileBase::E_None,
206 osl::File::copy(createFileURL(u"hybrid_writer.pdf"), nonAsciiName.GetURL()));
207 loadFromURL(nonAsciiName.GetURL());
208 // Make sure it opens in Writer.
209 // Without the accompanying fix in place, this test would have failed on Windows, as it was
210 // opened in Draw instead.
211 CPPUNIT_ASSERT(supportsService(mxComponent, "com.sun.star.text.TextDocument"));
215 // Created in Calc
216 utl::TempFileNamed nonAsciiName(u"абв_αβγ_");
217 nonAsciiName.EnableKillingFile();
218 CPPUNIT_ASSERT_EQUAL(
219 osl::FileBase::E_None,
220 osl::File::copy(createFileURL(u"hybrid_calc.pdf"), nonAsciiName.GetURL()));
221 loadFromURL(nonAsciiName.GetURL());
222 // Make sure it opens in Calc.
223 CPPUNIT_ASSERT(supportsService(mxComponent, "com.sun.star.sheet.SpreadsheetDocument"));
227 // Created in Impress
228 utl::TempFileNamed nonAsciiName(u"абв_αβγ_");
229 nonAsciiName.EnableKillingFile();
230 CPPUNIT_ASSERT_EQUAL(
231 osl::FileBase::E_None,
232 osl::File::copy(createFileURL(u"hybrid_impress.pdf"), nonAsciiName.GetURL()));
233 loadFromURL(nonAsciiName.GetURL());
234 // Make sure it opens in Impress.
235 CPPUNIT_ASSERT(
236 supportsService(mxComponent, "com.sun.star.presentation.PresentationDocument"));
239 #endif // _WIN32
241 CPPUNIT_TEST_FIXTURE(TextFilterDetectTest, testTdf163295)
243 // Given a file with a content of "<?xmlpwi" - yes, it's not an XML, and not a pwi file
244 auto xDetection(comphelper::getProcessServiceFactory()
245 ->createInstance(u"com.sun.star.document.TypeDetection"_ustr)
246 .queryThrow<document::XTypeDetection>());
247 OUString url = createFileURL(u"test_pseudo_pwi.xml");
248 css::uno::Sequence mediaDescriptor{ comphelper::makePropertyValue(u"URL"_ustr, url) };
249 OUString detection = xDetection->queryTypeByDescriptor(mediaDescriptor, true);
250 // Without the fix, this was "writer_PocketWord_File"
251 CPPUNIT_ASSERT_EQUAL(u"generic_Text"_ustr, detection);
255 CPPUNIT_PLUGIN_IMPLEMENT();
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */