2018-04-28 13:37:17 +09:00
|
|
|
/* -*- 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/.
|
|
|
|
*/
|
|
|
|
|
2020-05-11 11:09:50 +02:00
|
|
|
#include <sal/config.h>
|
|
|
|
#include <config_oox.h>
|
2018-04-28 13:37:17 +09:00
|
|
|
#include <cppunit/TestAssert.h>
|
|
|
|
#include <cppunit/TestFixture.h>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
#include <cppunit/plugin/TestPlugIn.h>
|
|
|
|
|
2018-05-17 15:15:01 +09:00
|
|
|
#include <com/sun/star/beans/PropertyValue.hpp>
|
|
|
|
|
|
|
|
#include <vcl/bitmapaccess.hxx>
|
2018-04-28 13:37:17 +09:00
|
|
|
#include <vcl/graph.hxx>
|
|
|
|
#include <vcl/graphicfilter.hxx>
|
|
|
|
#include <tools/stream.hxx>
|
2019-11-18 21:18:24 +01:00
|
|
|
#include <unotest/directories.hxx>
|
2020-04-25 12:06:53 +02:00
|
|
|
#include <comphelper/DirectoryHelper.hxx>
|
|
|
|
#include <comphelper/hash.hxx>
|
|
|
|
#include <unotools/ucbstreamhelper.hxx>
|
2020-11-25 18:01:08 +01:00
|
|
|
#include <unotools/tempfile.hxx>
|
2020-12-07 17:10:56 +01:00
|
|
|
#include <vcl/cvtgrf.hxx>
|
2020-04-25 12:06:53 +02:00
|
|
|
|
|
|
|
#include <impgraph.hxx>
|
2020-12-07 17:10:56 +01:00
|
|
|
#include <graphic/GraphicFormatDetector.hxx>
|
2018-04-28 13:37:17 +09:00
|
|
|
|
2020-05-11 11:09:50 +02:00
|
|
|
#if USE_TLS_NSS
|
|
|
|
#include <nss.h>
|
|
|
|
#endif
|
|
|
|
|
2018-05-17 15:15:01 +09:00
|
|
|
using namespace css;
|
|
|
|
|
2018-04-28 13:37:17 +09:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class GraphicTest : public CppUnit::TestFixture
|
|
|
|
{
|
2020-05-11 11:09:50 +02:00
|
|
|
public:
|
|
|
|
~GraphicTest();
|
|
|
|
|
|
|
|
private:
|
2018-04-28 13:37:17 +09:00
|
|
|
void testUnloadedGraphic();
|
2018-05-17 15:15:01 +09:00
|
|
|
void testUnloadedGraphicLoading();
|
2019-09-30 21:36:56 +02:00
|
|
|
void testUnloadedGraphicWmf();
|
2019-10-31 01:36:22 +11:00
|
|
|
void testUnloadedGraphicAlpha();
|
2019-11-18 21:18:24 +01:00
|
|
|
void testUnloadedGraphicSizeUnit();
|
2020-04-25 12:06:53 +02:00
|
|
|
void testSwapping();
|
|
|
|
void testSwappingVectorGraphic();
|
2020-08-17 07:44:57 +02:00
|
|
|
void testSwappingPageNumber();
|
2020-11-25 18:01:08 +01:00
|
|
|
void testWMFRoundtrip();
|
2020-12-07 17:10:56 +01:00
|
|
|
void testEmfToWmfConversion();
|
2018-04-28 13:37:17 +09:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(GraphicTest);
|
|
|
|
CPPUNIT_TEST(testUnloadedGraphic);
|
2018-05-17 15:15:01 +09:00
|
|
|
CPPUNIT_TEST(testUnloadedGraphicLoading);
|
2019-09-30 21:36:56 +02:00
|
|
|
CPPUNIT_TEST(testUnloadedGraphicWmf);
|
2019-10-31 01:36:22 +11:00
|
|
|
CPPUNIT_TEST(testUnloadedGraphicAlpha);
|
2019-11-18 21:18:24 +01:00
|
|
|
CPPUNIT_TEST(testUnloadedGraphicSizeUnit);
|
2020-04-25 12:06:53 +02:00
|
|
|
CPPUNIT_TEST(testSwapping);
|
|
|
|
CPPUNIT_TEST(testSwappingVectorGraphic);
|
2020-08-17 07:44:57 +02:00
|
|
|
CPPUNIT_TEST(testSwappingPageNumber);
|
2020-11-25 18:01:08 +01:00
|
|
|
CPPUNIT_TEST(testWMFRoundtrip);
|
2020-12-07 17:10:56 +01:00
|
|
|
CPPUNIT_TEST(testEmfToWmfConversion);
|
2018-04-28 13:37:17 +09:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
};
|
|
|
|
|
2020-05-11 11:09:50 +02:00
|
|
|
GraphicTest::~GraphicTest()
|
|
|
|
{
|
|
|
|
#if USE_TLS_NSS
|
|
|
|
NSS_Shutdown();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-10-31 01:36:22 +11:00
|
|
|
BitmapEx createBitmap(bool alpha = false)
|
2018-04-28 13:37:17 +09:00
|
|
|
{
|
2019-10-31 01:36:22 +11:00
|
|
|
Bitmap aBitmap(Size(120, 100), 24);
|
2018-04-28 13:37:17 +09:00
|
|
|
aBitmap.Erase(COL_LIGHTRED);
|
|
|
|
|
2019-10-31 01:36:22 +11:00
|
|
|
aBitmap.SetPrefSize(Size(6000, 5000));
|
|
|
|
aBitmap.SetPrefMapMode(MapMode(MapUnit::Map100thMM));
|
|
|
|
|
|
|
|
if (alpha)
|
|
|
|
{
|
|
|
|
sal_uInt8 uAlphaValue = 0x80;
|
|
|
|
AlphaMask aAlphaMask(Size(120, 100), &uAlphaValue);
|
|
|
|
|
|
|
|
return BitmapEx(aBitmap, aAlphaMask);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return BitmapEx(aBitmap);
|
|
|
|
}
|
2018-04-28 13:37:17 +09:00
|
|
|
}
|
|
|
|
|
2020-11-13 14:29:59 +02:00
|
|
|
void createBitmapAndExportForType(SvStream& rStream, std::u16string_view sType, bool alpha)
|
2018-04-28 13:37:17 +09:00
|
|
|
{
|
2019-10-31 01:36:22 +11:00
|
|
|
BitmapEx aBitmapEx = createBitmap(alpha);
|
2018-04-28 13:37:17 +09:00
|
|
|
|
2018-05-17 15:15:01 +09:00
|
|
|
uno::Sequence<beans::PropertyValue> aFilterData;
|
|
|
|
GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
|
|
|
|
sal_uInt16 nFilterFormat = rGraphicFilter.GetExportFormatNumberForShortName(sType);
|
|
|
|
rGraphicFilter.ExportGraphic(aBitmapEx, "none", rStream, nFilterFormat, &aFilterData);
|
2018-04-28 13:37:17 +09:00
|
|
|
|
|
|
|
rStream.Seek(STREAM_SEEK_TO_BEGIN);
|
|
|
|
}
|
|
|
|
|
2020-11-13 14:29:59 +02:00
|
|
|
Graphic makeUnloadedGraphic(std::u16string_view sType, bool alpha = false)
|
2018-04-28 13:37:17 +09:00
|
|
|
{
|
|
|
|
SvMemoryStream aStream;
|
|
|
|
GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
|
2019-10-31 01:36:22 +11:00
|
|
|
createBitmapAndExportForType(aStream, sType, alpha);
|
2018-04-28 13:37:17 +09:00
|
|
|
return rGraphicFilter.ImportUnloadedGraphic(aStream);
|
|
|
|
}
|
|
|
|
|
2020-04-25 12:06:53 +02:00
|
|
|
std::string toHexString(const std::vector<unsigned char>& a)
|
|
|
|
{
|
|
|
|
std::stringstream aStrm;
|
|
|
|
for (auto& i : a)
|
|
|
|
{
|
|
|
|
aStrm << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return aStrm.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<SvStream> createStream(OUString const& rSwapFileURL)
|
|
|
|
{
|
|
|
|
std::unique_ptr<SvStream> xStream;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
xStream = ::utl::UcbStreamHelper::CreateStream(
|
|
|
|
rSwapFileURL, StreamMode::READWRITE | StreamMode::SHARE_DENYWRITE);
|
|
|
|
}
|
|
|
|
catch (const css::uno::Exception&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
return xStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<unsigned char> calculateHash(std::unique_ptr<SvStream>& rStream)
|
|
|
|
{
|
|
|
|
comphelper::Hash aHashEngine(comphelper::HashType::SHA1);
|
|
|
|
const sal_uInt32 nSize(rStream->remainingSize());
|
|
|
|
std::vector<sal_uInt8> aData(nSize);
|
|
|
|
aHashEngine.update(aData.data(), nSize);
|
|
|
|
return aHashEngine.finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool checkBitmap(Graphic& rGraphic)
|
|
|
|
{
|
|
|
|
bool bResult = true;
|
|
|
|
|
|
|
|
Bitmap aBitmap(rGraphic.GetBitmapEx().GetBitmap());
|
|
|
|
{
|
|
|
|
Bitmap::ScopedReadAccess pReadAccess(aBitmap);
|
2020-10-19 15:12:38 +02:00
|
|
|
for (tools::Long y = 0; y < rGraphic.GetSizePixel().Height(); y++)
|
2020-04-25 12:06:53 +02:00
|
|
|
{
|
2020-10-19 15:12:38 +02:00
|
|
|
for (tools::Long x = 0; x < rGraphic.GetSizePixel().Width(); x++)
|
2020-04-25 12:06:53 +02:00
|
|
|
{
|
|
|
|
if (pReadAccess->HasPalette())
|
|
|
|
{
|
|
|
|
sal_uInt32 nIndex = pReadAccess->GetPixelIndex(y, x);
|
|
|
|
Color aColor = pReadAccess->GetPaletteColor(nIndex);
|
|
|
|
bResult &= (aColor == Color(0xff, 0x00, 0x00));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Color aColor = pReadAccess->GetPixel(y, x);
|
|
|
|
bResult &= (aColor == Color(0xff, 0x00, 0x00));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bResult;
|
|
|
|
}
|
|
|
|
|
2019-11-18 21:18:24 +01:00
|
|
|
char const DATA_DIRECTORY[] = "/vcl/qa/cppunit/data/";
|
2020-08-17 07:44:57 +02:00
|
|
|
char const PDFEXPORT_DATA_DIRECTORY[] = "/vcl/qa/cppunit/pdfexport/data/";
|
2019-11-18 21:18:24 +01:00
|
|
|
|
2018-04-28 13:37:17 +09:00
|
|
|
void GraphicTest::testUnloadedGraphic()
|
|
|
|
{
|
|
|
|
// make unloaded test graphic
|
2020-11-13 14:29:59 +02:00
|
|
|
Graphic aGraphic = makeUnloadedGraphic(u"png");
|
2018-04-28 13:37:17 +09:00
|
|
|
Graphic aGraphic2 = aGraphic;
|
|
|
|
|
|
|
|
// check available
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic2.isAvailable());
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic2.makeAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic2.isAvailable());
|
|
|
|
|
|
|
|
// check GetSizePixel doesn't load graphic
|
2020-11-13 14:29:59 +02:00
|
|
|
aGraphic = makeUnloadedGraphic(u"png");
|
2018-04-28 13:37:17 +09:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
2020-11-13 09:04:59 +03:00
|
|
|
CPPUNIT_ASSERT_EQUAL(tools::Long(120), aGraphic.GetSizePixel().Width());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(tools::Long(100), aGraphic.GetSizePixel().Height());
|
2018-04-28 13:37:17 +09:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
|
2019-10-31 01:36:22 +11:00
|
|
|
// check GetPrefSize doesn't load graphic
|
2020-11-13 09:04:59 +03:00
|
|
|
CPPUNIT_ASSERT_EQUAL(tools::Long(6000), aGraphic.GetPrefSize().Width());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(tools::Long(5000), aGraphic.GetPrefSize().Height());
|
2019-10-31 01:36:22 +11:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
|
2018-04-28 13:37:17 +09:00
|
|
|
// check GetSizeBytes loads graphic
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT(aGraphic.GetSizeBytes() > 0);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
|
2020-04-25 12:06:53 +02:00
|
|
|
|
|
|
|
//check Type
|
2020-11-13 14:29:59 +02:00
|
|
|
aGraphic = makeUnloadedGraphic(u"png");
|
2020-04-25 12:06:53 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(GraphicType::Bitmap, aGraphic.GetType());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.makeAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(GraphicType::Bitmap, aGraphic.GetType());
|
2018-04-28 13:37:17 +09:00
|
|
|
}
|
|
|
|
|
2018-05-17 15:15:01 +09:00
|
|
|
void GraphicTest::testUnloadedGraphicLoading()
|
|
|
|
{
|
|
|
|
const OUString aFormats[] = { "png", "gif", "jpg" };
|
|
|
|
|
|
|
|
for (OUString const& sFormat : aFormats)
|
|
|
|
{
|
|
|
|
Graphic aGraphic = makeUnloadedGraphic(sFormat);
|
|
|
|
|
|
|
|
// check available
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
2020-11-13 09:04:59 +03:00
|
|
|
CPPUNIT_ASSERT_EQUAL(tools::Long(120), aGraphic.GetSizePixel().Width());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(tools::Long(100), aGraphic.GetSizePixel().Height());
|
2018-05-17 15:15:01 +09:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT(aGraphic.GetSizeBytes() > 0);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
|
|
|
|
|
2020-04-25 12:06:53 +02:00
|
|
|
if (sFormat != "jpg")
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, checkBitmap(aGraphic));
|
2018-05-17 15:15:01 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-30 21:36:56 +02:00
|
|
|
void GraphicTest::testUnloadedGraphicWmf()
|
|
|
|
{
|
|
|
|
// Create some in-memory WMF data, set its own preferred size to 99x99.
|
|
|
|
BitmapEx aBitmapEx = createBitmap();
|
|
|
|
SvMemoryStream aStream;
|
|
|
|
GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
|
2020-11-13 14:29:59 +02:00
|
|
|
sal_uInt16 nFilterFormat = rGraphicFilter.GetExportFormatNumberForShortName(u"wmf");
|
2019-09-30 21:36:56 +02:00
|
|
|
Graphic aGraphic(aBitmapEx);
|
|
|
|
aGraphic.SetPrefSize(Size(99, 99));
|
|
|
|
aGraphic.SetPrefMapMode(MapMode(MapUnit::Map100thMM));
|
|
|
|
rGraphicFilter.ExportGraphic(aGraphic, "none", aStream, nFilterFormat);
|
|
|
|
aStream.Seek(STREAM_SEEK_TO_BEGIN);
|
|
|
|
|
|
|
|
// Now lazy-load this WMF data, with a custom preferred size of 42x42.
|
|
|
|
Size aMtfSize100(42, 42);
|
|
|
|
aGraphic = rGraphicFilter.ImportUnloadedGraphic(aStream, 0, &aMtfSize100);
|
|
|
|
aGraphic.makeAvailable();
|
|
|
|
|
|
|
|
// Without the accompanying fix in place, this test would have failed with:
|
|
|
|
// - Expected: 42x42
|
|
|
|
// - Actual : 99x99
|
2019-10-01 12:26:03 +02:00
|
|
|
// i.e. the custom preferred size was lost after lazy-load.
|
2019-09-30 21:36:56 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(Size(42, 42), aGraphic.GetPrefSize());
|
|
|
|
}
|
|
|
|
|
2019-10-31 01:36:22 +11:00
|
|
|
void GraphicTest::testUnloadedGraphicAlpha()
|
|
|
|
{
|
|
|
|
// make unloaded test graphic with alpha
|
2020-11-13 14:29:59 +02:00
|
|
|
Graphic aGraphic = makeUnloadedGraphic(u"png", true);
|
2019-10-31 01:36:22 +11:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.IsAlpha());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.IsTransparent());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
|
|
|
|
// make unloaded test graphic without alpha
|
2020-11-13 14:29:59 +02:00
|
|
|
aGraphic = makeUnloadedGraphic(u"png", false);
|
2019-10-31 01:36:22 +11:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.IsAlpha());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.IsTransparent());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
}
|
|
|
|
|
2019-11-18 21:18:24 +01:00
|
|
|
void GraphicTest::testUnloadedGraphicSizeUnit()
|
|
|
|
{
|
|
|
|
GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
|
|
|
|
test::Directories aDirectories;
|
|
|
|
OUString aURL = aDirectories.getURLFromSrc(DATA_DIRECTORY) + "inch-size.emf";
|
|
|
|
Size aMtfSize100(42, 42);
|
|
|
|
SvFileStream aStream(aURL, StreamMode::READ);
|
|
|
|
Graphic aGraphic = rGraphicFilter.ImportUnloadedGraphic(aStream, 0, &aMtfSize100);
|
|
|
|
aGraphic.makeAvailable();
|
|
|
|
|
|
|
|
// Without the accompanying fix in place, this test would have failed with:
|
|
|
|
// - Expected: 400x363
|
|
|
|
// - Actual : 42x42
|
|
|
|
// i.e. a mm100 size was used as a hint and the inch size was set for a non-matching unit.
|
|
|
|
CPPUNIT_ASSERT_EQUAL(Size(400, 363), aGraphic.GetPrefSize());
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:10:56 +01:00
|
|
|
void GraphicTest::testEmfToWmfConversion()
|
|
|
|
{
|
|
|
|
// Load EMF data.
|
|
|
|
GraphicFilter aGraphicFilter;
|
|
|
|
test::Directories aDirectories;
|
|
|
|
OUString aURL = aDirectories.getURLFromSrc(DATA_DIRECTORY) + "to-wmf.emf";
|
|
|
|
SvFileStream aStream(aURL, StreamMode::READ);
|
|
|
|
Graphic aGraphic;
|
|
|
|
// This similar to an application/x-openoffice-wmf mime type in manifest.xml in the ODF case.
|
|
|
|
sal_uInt16 nFormat = aGraphicFilter.GetImportFormatNumberForShortName(u"WMF");
|
|
|
|
CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE,
|
|
|
|
aGraphicFilter.ImportGraphic(aGraphic, OUString(), aStream, nFormat));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(VectorGraphicDataType::Wmf,
|
|
|
|
aGraphic.getVectorGraphicData()->getVectorGraphicDataType());
|
|
|
|
|
|
|
|
// Save as WMF.
|
|
|
|
sal_uInt16 nFilterType = aGraphicFilter.GetExportFormatNumberForShortName(u"WMF");
|
|
|
|
SvMemoryStream aGraphicStream;
|
|
|
|
CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, aGraphicFilter.ExportGraphic(aGraphic, OUString(),
|
|
|
|
aGraphicStream, nFilterType));
|
|
|
|
aGraphicStream.Seek(0);
|
|
|
|
vcl::GraphicFormatDetector aDetector(aGraphicStream, OUString());
|
|
|
|
CPPUNIT_ASSERT(aDetector.detect());
|
|
|
|
CPPUNIT_ASSERT(aDetector.checkWMForEMF());
|
|
|
|
|
|
|
|
// Without the accompanying fix in place, this test would have failed with:
|
|
|
|
// - Expected: WMF
|
|
|
|
// - Actual : EMF
|
|
|
|
// i.e. EMF data was requested to be converted to WMF, but the output was still EMF.
|
|
|
|
CPPUNIT_ASSERT_EQUAL(OUString("WMF"), aDetector.msDetectedFormat);
|
|
|
|
}
|
|
|
|
|
2020-04-25 12:06:53 +02:00
|
|
|
void GraphicTest::testSwapping()
|
|
|
|
{
|
|
|
|
// Prepare Graphic from a PNG image first
|
2020-11-13 14:29:59 +02:00
|
|
|
Graphic aGraphic = makeUnloadedGraphic(u"png");
|
2020-04-25 12:06:53 +02:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(GraphicType::Bitmap, aGraphic.GetType());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.makeAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
|
|
|
|
|
2020-11-13 09:04:59 +03:00
|
|
|
CPPUNIT_ASSERT_EQUAL(tools::Long(120), aGraphic.GetSizePixel().Width());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(tools::Long(100), aGraphic.GetSizePixel().Height());
|
2020-05-14 16:02:31 +02:00
|
|
|
|
|
|
|
BitmapChecksum aChecksumBeforeSwapping = aGraphic.GetChecksum();
|
2020-04-25 12:06:53 +02:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_uInt32(319), aGraphic.GetGfxLink().GetDataSize());
|
|
|
|
|
|
|
|
// We loaded the Graphic and made it available
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.ImplGetImpGraphic()->isSwappedOut());
|
|
|
|
// Get the declared byte size of the graphic
|
|
|
|
sal_uLong rByteSize = aGraphic.GetSizeBytes();
|
|
|
|
OUString rSwapFileURL = aGraphic.ImplGetImpGraphic()->getSwapFileURL();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, rSwapFileURL.isEmpty());
|
|
|
|
|
|
|
|
// Swapping out
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->swapOut());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->isSwappedOut());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
|
|
|
|
// Byte size doesn't change when we swapped out
|
|
|
|
CPPUNIT_ASSERT_EQUAL(rByteSize, aGraphic.GetSizeBytes());
|
|
|
|
|
|
|
|
// Let's check the swap file
|
|
|
|
rSwapFileURL = aGraphic.ImplGetImpGraphic()->getSwapFileURL();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, comphelper::DirectoryHelper::fileExists(rSwapFileURL));
|
|
|
|
|
|
|
|
{ // Check the swap file content
|
|
|
|
std::unique_ptr<SvStream> xStream = createStream(rSwapFileURL);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, bool(xStream));
|
|
|
|
|
|
|
|
// Check size of the stream
|
2020-07-29 20:57:40 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_uInt64(449), xStream->remainingSize());
|
2020-04-25 12:06:53 +02:00
|
|
|
|
|
|
|
std::vector<unsigned char> aHash = calculateHash(xStream);
|
2020-07-29 20:57:40 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("878281e583487b29ae09078e8040c01791c7649a"),
|
2020-04-25 12:06:53 +02:00
|
|
|
toHexString(aHash));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's swap in
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.makeAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.ImplGetImpGraphic()->isSwappedOut());
|
|
|
|
|
2020-05-14 16:02:31 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(aChecksumBeforeSwapping, aGraphic.GetChecksum());
|
2020-04-25 12:06:53 +02:00
|
|
|
|
|
|
|
// File shouldn't be available anymore
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, comphelper::DirectoryHelper::fileExists(rSwapFileURL));
|
|
|
|
|
|
|
|
// Check the bitmap
|
2020-11-13 09:04:59 +03:00
|
|
|
CPPUNIT_ASSERT_EQUAL(tools::Long(120), aGraphic.GetSizePixel().Width());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(tools::Long(100), aGraphic.GetSizePixel().Height());
|
2020-04-25 12:06:53 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, checkBitmap(aGraphic));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, checkBitmap(aGraphic));
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicTest::testSwappingVectorGraphic()
|
|
|
|
{
|
|
|
|
test::Directories aDirectories;
|
|
|
|
OUString aURL = aDirectories.getURLFromSrc(DATA_DIRECTORY) + "SimpleExample.svg";
|
|
|
|
SvFileStream aStream(aURL, StreamMode::READ);
|
|
|
|
GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
|
|
|
|
Graphic aGraphic = rGraphicFilter.ImportUnloadedGraphic(aStream);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(GraphicType::Bitmap, aGraphic.GetType());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
|
|
|
|
// Load the vector graphic
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, bool(aGraphic.getVectorGraphicData()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_uInt32(223),
|
|
|
|
aGraphic.getVectorGraphicData()->getVectorGraphicDataArrayLength());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_uInt32(223), aGraphic.GetGfxLink().GetDataSize());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
|
|
|
|
|
|
|
|
BitmapChecksum aBitmapChecksumBeforeSwapping = aGraphic.GetBitmapEx().GetChecksum();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.ImplGetImpGraphic()->isSwappedOut());
|
|
|
|
|
|
|
|
// Get the declared byte size of the graphic
|
|
|
|
sal_uLong rByteSize = aGraphic.GetSizeBytes();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_uLong(223), rByteSize);
|
|
|
|
OUString rSwapFileURL = aGraphic.ImplGetImpGraphic()->getSwapFileURL();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, rSwapFileURL.isEmpty());
|
|
|
|
|
|
|
|
// Swapping out
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->swapOut());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->isSwappedOut());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
|
|
|
|
// Byte size doesn't change when we swapped out
|
|
|
|
// TODO: In case we don't trigger GetBitmapEx (above) the size is 0
|
|
|
|
CPPUNIT_ASSERT_EQUAL(rByteSize, aGraphic.GetSizeBytes());
|
|
|
|
|
|
|
|
// Let's check the swap file
|
|
|
|
rSwapFileURL = aGraphic.ImplGetImpGraphic()->getSwapFileURL();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, comphelper::DirectoryHelper::fileExists(rSwapFileURL));
|
|
|
|
|
|
|
|
{ // Check the swap file content
|
|
|
|
std::unique_ptr<SvStream> xStream = createStream(rSwapFileURL);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, bool(xStream));
|
|
|
|
|
|
|
|
// Check size of the stream
|
2020-07-29 20:57:40 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_uInt64(353), xStream->remainingSize());
|
2020-04-25 12:06:53 +02:00
|
|
|
|
|
|
|
std::vector<unsigned char> aHash = calculateHash(xStream);
|
2020-07-29 20:57:40 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("6ae83fc9c06ca253ada0b156d6e4700a4a028c34"),
|
2020-04-25 12:06:53 +02:00
|
|
|
toHexString(aHash));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's swap in
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.makeAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.ImplGetImpGraphic()->isSwappedOut());
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(aBitmapChecksumBeforeSwapping, aGraphic.GetBitmapEx().GetChecksum());
|
|
|
|
|
|
|
|
// File shouldn't be available anymore
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, comphelper::DirectoryHelper::fileExists(rSwapFileURL));
|
|
|
|
}
|
|
|
|
|
2020-08-17 07:44:57 +02:00
|
|
|
void GraphicTest::testSwappingPageNumber()
|
|
|
|
{
|
|
|
|
test::Directories aDirectories;
|
|
|
|
OUString aURL = aDirectories.getURLFromSrc(PDFEXPORT_DATA_DIRECTORY) + "SimpleMultiPagePDF.pdf";
|
|
|
|
SvFileStream aStream(aURL, StreamMode::READ);
|
|
|
|
GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
|
|
|
|
Graphic aGraphic = rGraphicFilter.ImportUnloadedGraphic(aStream);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(GraphicType::Bitmap, aGraphic.GetType());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
|
|
|
|
// Load the vector graphic
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, bool(aGraphic.getVectorGraphicData()));
|
|
|
|
// Set the page index
|
|
|
|
aGraphic.getVectorGraphicData()->setPageIndex(1);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(VectorGraphicDataType::Pdf,
|
|
|
|
aGraphic.getVectorGraphicData()->getVectorGraphicDataType());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_uInt32(17693),
|
|
|
|
aGraphic.getVectorGraphicData()->getVectorGraphicDataArrayLength());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aGraphic.getVectorGraphicData()->getPageIndex());
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.ImplGetImpGraphic()->isSwappedOut());
|
|
|
|
|
|
|
|
// Swapping out
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->swapOut());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->isSwappedOut());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
|
|
|
|
// Let's swap in
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.makeAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.ImplGetImpGraphic()->isSwappedOut());
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aGraphic.getVectorGraphicData()->getPageIndex());
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:08 +01:00
|
|
|
void GraphicTest::testWMFRoundtrip()
|
|
|
|
{
|
|
|
|
// Load a WMF file.
|
|
|
|
test::Directories aDirectories;
|
|
|
|
OUString aURL = aDirectories.getURLFromSrc("vcl/qa/cppunit/data/roundtrip.wmf");
|
|
|
|
SvFileStream aStream(aURL, StreamMode::READ);
|
|
|
|
sal_uInt64 nExpectedSize = aStream.TellEnd();
|
|
|
|
GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
|
|
|
|
Graphic aGraphic = rGraphicFilter.ImportUnloadedGraphic(aStream);
|
|
|
|
|
|
|
|
// Save as WMF.
|
|
|
|
utl::TempFile aTempFile;
|
|
|
|
aTempFile.EnableKillingFile();
|
|
|
|
sal_uInt16 nFormat = rGraphicFilter.GetExportFormatNumberForShortName(u"WMF");
|
|
|
|
SvStream& rOutStream = *aTempFile.GetStream(StreamMode::READWRITE);
|
|
|
|
rGraphicFilter.ExportGraphic(aGraphic, OUString(), rOutStream, nFormat);
|
|
|
|
|
|
|
|
// Check if we preserved the WMF data perfectly.
|
|
|
|
sal_uInt64 nActualSize = rOutStream.TellEnd();
|
|
|
|
|
|
|
|
// Without the accompanying fix in place, this test would have failed with:
|
|
|
|
// - Expected: 6475
|
|
|
|
// - Actual : 2826
|
|
|
|
// i.e. we lost some of the WMF data on roundtrip.
|
|
|
|
CPPUNIT_ASSERT_EQUAL(nExpectedSize, nActualSize);
|
|
|
|
}
|
|
|
|
|
2018-04-28 13:37:17 +09:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(GraphicTest);
|
|
|
|
|
|
|
|
CPPUNIT_PLUGIN_IMPLEMENT();
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|