summaryrefslogtreecommitdiff
path: root/vcl/qa/cppunit/GraphicDescriptorTest.cxx
blob: 9bbe10a5cea7e55d5519e4e4ffec7098cec820b9 (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
/* -*- 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 <unotest/bootstrapfixturebase.hxx>

#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>

#include <com/sun/star/uno/Sequence.hxx>
#include <com/sun/star/beans/PropertyValue.hpp>

#include <vcl/graphicfilter.hxx>
#include <tools/stream.hxx>

using namespace css;

namespace
{
class GraphicDescriptorTest : public test::BootstrapFixtureBase
{
    OUString getFullUrl(std::u16string_view sFileName)
    {
        return m_directories.getURLFromSrc(u"/vcl/qa/cppunit/data/") + sFileName;
    }
    void testDetectPNG();
    void testDetectJPG();
    void testDetectGIF();
    void testDetectBMP();
    void testDetectWEBP();

    CPPUNIT_TEST_SUITE(GraphicDescriptorTest);
    CPPUNIT_TEST(testDetectPNG);
    CPPUNIT_TEST(testDetectJPG);
    CPPUNIT_TEST(testDetectGIF);
    CPPUNIT_TEST(testDetectBMP);
    CPPUNIT_TEST(testDetectWEBP);
    CPPUNIT_TEST_SUITE_END();
};

BitmapEx createBitmap()
{
    Bitmap aBitmap(Size(100, 100), 24);
    aBitmap.Erase(COL_LIGHTRED);

    return BitmapEx(aBitmap);
}

void createBitmapAndExportForType(SvStream& rStream, std::u16string_view sType)
{
    BitmapEx aBitmapEx = createBitmap();

    uno::Sequence<beans::PropertyValue> aFilterData;
    GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
    sal_uInt16 nFilterFormat = rGraphicFilter.GetExportFormatNumberForShortName(sType);
    rGraphicFilter.ExportGraphic(aBitmapEx, "none", rStream, nFilterFormat, &aFilterData);

    rStream.Seek(STREAM_SEEK_TO_BEGIN);
}

void GraphicDescriptorTest::testDetectPNG()
{
    SvMemoryStream aStream;
    createBitmapAndExportForType(aStream, u"png");

    GraphicDescriptor aDescriptor(aStream, nullptr);
    aDescriptor.Detect(true);

    CPPUNIT_ASSERT_EQUAL(GraphicFileFormat::PNG, aDescriptor.GetFileFormat());

    CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor.GetSizePixel().Width());
    CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor.GetSizePixel().Height());
}

void GraphicDescriptorTest::testDetectJPG()
{
    SvMemoryStream aStream;
    createBitmapAndExportForType(aStream, u"jpg");

    GraphicDescriptor aDescriptor(aStream, nullptr);
    aDescriptor.Detect(true);

    CPPUNIT_ASSERT_EQUAL(GraphicFileFormat::JPG, aDescriptor.GetFileFormat());

    CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor.GetSizePixel().Width());
    CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor.GetSizePixel().Height());
}

void GraphicDescriptorTest::testDetectGIF()
{
    SvMemoryStream aStream;
    createBitmapAndExportForType(aStream, u"gif");

    GraphicDescriptor aDescriptor(aStream, nullptr);
    aDescriptor.Detect(true);

    CPPUNIT_ASSERT_EQUAL(GraphicFileFormat::GIF, aDescriptor.GetFileFormat());

    CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor.GetSizePixel().Width());
    CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor.GetSizePixel().Height());
}

void GraphicDescriptorTest::testDetectBMP()
{
    GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
    SvFileStream aFileStream(getFullUrl(u"graphic-descriptor-mapmode.bmp"), StreamMode::READ);

    Graphic aGraphic = rGraphicFilter.ImportUnloadedGraphic(aFileStream);

    CPPUNIT_ASSERT(!aGraphic.isAvailable());
    // Without the accompanying fix in place, this test would have failed with:
    // - Expected: 2 (MapUnit::MapMM)
    // - Actual  : 0 (MapUnit::Map100thMM)
    // i.e. lazy load and load created different map modes, breaking the contour polygon code in
    // Writer.
    CPPUNIT_ASSERT_EQUAL(MapUnit::MapMM, aGraphic.GetPrefMapMode().GetMapUnit());
    aGraphic.makeAvailable();
    CPPUNIT_ASSERT_EQUAL(MapUnit::MapMM, aGraphic.GetPrefMapMode().GetMapUnit());
}

void GraphicDescriptorTest::testDetectWEBP()
{
    SvMemoryStream aStream;
    createBitmapAndExportForType(aStream, u"webp");

    GraphicDescriptor aDescriptor(aStream, nullptr);
    aDescriptor.Detect(true);

    CPPUNIT_ASSERT_EQUAL(GraphicFileFormat::WEBP, aDescriptor.GetFileFormat());

    CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor.GetSizePixel().Width());
    CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor.GetSizePixel().Height());
}

} // namespace

CPPUNIT_TEST_SUITE_REGISTRATION(GraphicDescriptorTest);

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