Update git submodules
[LibreOffice.git] / svx / qa / unit / xoutdev.cxx
blob35eac21cda72f23674832737b1f094236a5d9d59
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 <cppunit/TestAssert.h>
11 #include <cppunit/TestFixture.h>
12 #include <cppunit/extensions/HelperMacros.h>
13 #include <unotest/bootstrapfixturebase.hxx>
15 #include <sal/types.h>
16 #include <sfx2/app.hxx>
17 #include <tools/stream.hxx>
18 #include <unotest/directories.hxx>
19 #include <unotools/tempfile.hxx>
20 #include <vcl/graph.hxx>
21 #include <vcl/graphicfilter.hxx>
22 #include <svx/xoutbmp.hxx>
23 #include <vcl/filter/PDFiumLibrary.hxx>
25 class XOutdevTest : public CppUnit::TestFixture
27 public:
28 virtual void setUp() override
30 CppUnit::TestFixture::setUp();
31 SfxApplication::GetOrCreate();
35 CPPUNIT_TEST_FIXTURE(XOutdevTest, testPdfGraphicExport)
37 auto pPdfium = vcl::pdf::PDFiumLibrary::get();
38 if (!pPdfium)
40 return;
43 // Import the graphic.
44 Graphic aGraphic;
45 test::Directories aDirectories;
46 OUString aURL = aDirectories.getURLFromSrc(u"svx/qa/unit/data/graphic.pdf");
47 SvFileStream aStream(aURL, StreamMode::READ);
48 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE,
49 GraphicFilter::GetGraphicFilter().ImportGraphic(aGraphic, aURL, aStream));
51 // Export it.
52 utl::TempFile aTempFile;
53 aTempFile.EnableKillingFile();
54 XOutFlags const eFlags = XOutFlags::DontExpandFilename | XOutFlags::DontAddExtension
55 | XOutFlags::UseNativeIfPossible;
56 OUString aTempURL = aTempFile.GetURL();
57 XOutBitmap::WriteGraphic(aGraphic, aTempURL, "pdf", eFlags);
59 // Assert that the output looks like a PDF.
60 SvStream* pStream = aTempFile.GetStream(StreamMode::READ);
61 CPPUNIT_ASSERT(pStream->TellEnd() > 5);
62 sal_uInt8 sFirstBytes[5];
63 pStream->ReadBytes(sFirstBytes, 5);
64 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt8>('%'), sFirstBytes[0]);
65 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt8>('P'), sFirstBytes[1]);
66 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt8>('D'), sFirstBytes[2]);
67 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt8>('F'), sFirstBytes[3]);
68 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt8>('-'), sFirstBytes[4]);
71 CPPUNIT_TEST_FIXTURE(XOutdevTest, testTdf60684)
73 Graphic aGraphic;
74 test::Directories aDirectories;
75 OUString aURL = aDirectories.getURLFromSrc(u"svx/qa/unit/data/tdf60684.jpg");
76 SvFileStream aStream(aURL, StreamMode::READ);
77 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE,
78 GraphicFilter::GetGraphicFilter().ImportGraphic(aGraphic, aURL, aStream));
80 // Export it.
81 utl::TempFile aTempFile;
82 aTempFile.EnableKillingFile();
83 XOutFlags const eFlags = XOutFlags::DontExpandFilename | XOutFlags::DontAddExtension
84 | XOutFlags::UseNativeIfPossible;
85 OUString aTempURL = aTempFile.GetURL();
86 XOutBitmap::WriteGraphic(aGraphic, aTempURL, "png", eFlags);
88 SvStream* pStream = aTempFile.GetStream(StreamMode::READ);
89 CPPUNIT_ASSERT(pStream->TellEnd() > 4);
90 sal_uInt8 sFirstBytes[4];
91 pStream->ReadBytes(sFirstBytes, 4);
93 //Checks if the file's header matches a PNG's expected header
94 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt8>('P'), sFirstBytes[1]);
95 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt8>('N'), sFirstBytes[2]);
96 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt8>('G'), sFirstBytes[3]);
99 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */