summaryrefslogtreecommitdiff
path: root/canvas
diff options
context:
space:
mode:
authorLuboš Luňák <l.lunak@collabora.com>2021-05-04 11:32:11 +0200
committerLuboš Luňák <l.lunak@collabora.com>2021-05-06 11:22:47 +0200
commit402acbcfadbc8fd9f45330b46a4bfaad3e02eb39 (patch)
treebb12f967f882208c1b13febbca435f8b68999c57 /canvas
parent834822413d687572691753c33d837ffdb5064f2b (diff)
make it possible to write canvas unittests
One of the problems is that canvas apparently works only with windows, but tests obviously need an offscreen surface. This patch moves Window::GetCanvas() to OutputDevice, and makes vclcanvas capable of working with OutputDevice classes that are not windows. Other canvas implementations still don't work, but presumably at least cairocanvas could be fixed too. This commit adds a "simple" test that just draws a line and tries to verify it's been drawn properly. Adding another test should be a matter of basing it on this existing one, and then copy&pasting the complicated UNO way of drawing using canvas from somewhere, such as canvas/workben/canvasdemo.cxx. Change-Id: I42db12b09433763cd31c3dd497c10157424b8598 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/115117 Tested-by: Jenkins Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
Diffstat (limited to 'canvas')
-rw-r--r--canvas/CppunitTest_canvas_test.mk46
-rw-r--r--canvas/Module_canvas.mk4
-rw-r--r--canvas/qa/cppunit/README11
-rw-r--r--canvas/qa/cppunit/canvastest.cxx109
-rw-r--r--canvas/source/vcl/canvas.cxx27
-rw-r--r--canvas/source/vcl/outdevholder.hxx51
-rw-r--r--canvas/source/vcl/spritecanvas.cxx21
7 files changed, 242 insertions, 27 deletions
diff --git a/canvas/CppunitTest_canvas_test.mk b/canvas/CppunitTest_canvas_test.mk
new file mode 100644
index 000000000000..740e7e11db48
--- /dev/null
+++ b/canvas/CppunitTest_canvas_test.mk
@@ -0,0 +1,46 @@
+# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
+#*************************************************************************
+#
+# 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/.
+#
+#*************************************************************************
+
+$(eval $(call gb_CppunitTest_CppunitTest,canvas_test))
+
+$(eval $(call gb_CppunitTest_add_exception_objects,canvas_test, \
+ canvas/qa/cppunit/canvastest \
+))
+
+$(eval $(call gb_CppunitTest_use_external,canvas_test,boost_headers))
+
+$(eval $(call gb_CppunitTest_use_libraries,canvas_test, \
+ basegfx \
+ comphelper \
+ cppu \
+ cppuhelper \
+ sal \
+ test \
+ tl \
+ unotest \
+ vcl \
+))
+
+$(eval $(call gb_CppunitTest_use_sdk_api,canvas_test))
+
+$(eval $(call gb_CppunitTest_use_ure,canvas_test))
+$(eval $(call gb_CppunitTest_use_vcl,canvas_test))
+
+$(eval $(call gb_CppunitTest_use_components,canvas_test,\
+ canvas/source/vcl/vclcanvas \
+ canvas/source/factory/canvasfactory \
+ configmgr/source/configmgr \
+ i18npool/util/i18npool \
+))
+
+$(eval $(call gb_CppunitTest_use_configuration,canvas_test))
+
+# vim: set noet sw=4 ts=4:
diff --git a/canvas/Module_canvas.mk b/canvas/Module_canvas.mk
index f62d7ef46299..0ca3c4c9bfe0 100644
--- a/canvas/Module_canvas.mk
+++ b/canvas/Module_canvas.mk
@@ -49,4 +49,8 @@ $(eval $(call gb_Module_add_targets,canvas,\
))
endif
+$(eval $(call gb_Module_add_check_targets,canvas,\
+ CppunitTest_canvas_test \
+))
+
# vim: set noet sw=4 ts=4:
diff --git a/canvas/qa/cppunit/README b/canvas/qa/cppunit/README
new file mode 100644
index 000000000000..f4fc2f2e0467
--- /dev/null
+++ b/canvas/qa/cppunit/README
@@ -0,0 +1,11 @@
+How to write a canvas test:
+===========================
+
+The easiest (relatively speaking) way is to copy&paste from somewhere. Use existing tests
+as the base, or canvas/workben/canvasdemo.cxx should be a good source too. If that doesn't
+help, then you'll need to find the right UNO interfaces (see canvas/README). Have "fun".
+
+
+Currently only the vclcanvas implementation is tested, because it's the only one
+capable of working with offscreen surfaces. Other implementations would need to
+be fixed, and then added to the gb_CppunitTest_use_components list in the makefile.
diff --git a/canvas/qa/cppunit/canvastest.cxx b/canvas/qa/cppunit/canvastest.cxx
new file mode 100644
index 000000000000..414b85b2f51c
--- /dev/null
+++ b/canvas/qa/cppunit/canvastest.cxx
@@ -0,0 +1,109 @@
+/* -*- 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 <test/bootstrapfixture.hxx>
+
+#include <vcl/virdev.hxx>
+#include <vcl/BitmapReadAccess.hxx>
+#include <vcl/canvastools.hxx>
+#include <vcl/graphicfilter.hxx>
+#include <tools/stream.hxx>
+
+#include <com/sun/star/rendering/XBitmap.hpp>
+#include <com/sun/star/rendering/XCanvas.hpp>
+#include <com/sun/star/rendering/XBitmapCanvas.hpp>
+#include <com/sun/star/rendering/CompositeOperation.hpp>
+
+using namespace ::com::sun::star;
+
+class CanvasTest : public test::BootstrapFixture
+{
+ VclPtr<VirtualDevice> mVclDevice;
+ uno::Reference<rendering::XCanvas> mCanvas;
+ rendering::ViewState mViewState;
+ rendering::RenderState mRenderState;
+ uno::Sequence<double> mColorBlack;
+
+ // if enabled - check the result images with:
+ // "xdg-open ./workdir/CppunitTest/canvas_test.test.core/"
+ static constexpr const bool mbExportBitmap = false;
+
+ void exportDevice(const OUString& filename, const VclPtr<VirtualDevice>& device)
+ {
+ if (mbExportBitmap)
+ {
+ BitmapEx aBitmapEx(device->GetBitmapEx(Point(0, 0), device->GetOutputSizePixel()));
+ SvFileStream aStream(filename, StreamMode::WRITE | StreamMode::TRUNC);
+ GraphicFilter::GetGraphicFilter().compressAsPNG(aBitmapEx, aStream);
+ }
+ }
+
+public:
+ CanvasTest()
+ : BootstrapFixture(true, false)
+ {
+ }
+
+ virtual void setUp() override
+ {
+ BootstrapFixture::setUp();
+ mColorBlack = vcl::unotools::colorToStdColorSpaceSequence(COL_BLACK);
+ // Geometry init
+ geometry::AffineMatrix2D aUnit(1, 0, 0, 0, 1, 0);
+ mViewState.AffineTransform = aUnit;
+ mRenderState.AffineTransform = aUnit;
+ mRenderState.DeviceColor = mColorBlack;
+ mRenderState.CompositeOperation = rendering::CompositeOperation::OVER;
+ }
+
+ virtual void tearDown() override
+ {
+ mVclDevice.clear();
+ mCanvas = uno::Reference<rendering::XCanvas>();
+ BootstrapFixture::tearDown();
+ }
+
+ void setupCanvas(const Size& size, Color backgroundColor = COL_WHITE, bool alpha = false)
+ {
+ mVclDevice
+ = alpha ? VclPtr<VirtualDevice>::Create(DeviceFormat::DEFAULT, DeviceFormat::DEFAULT)
+ : VclPtr<VirtualDevice>::Create(DeviceFormat::DEFAULT);
+ mVclDevice->SetOutputSizePixel(size);
+ mVclDevice->SetBackground(Wallpaper(backgroundColor));
+ mVclDevice->Erase();
+ mCanvas = mVclDevice->GetCanvas();
+ CPPUNIT_ASSERT(mCanvas.is());
+ }
+
+ void testDrawLine()
+ {
+ setupCanvas(Size(10, 10));
+ mCanvas->drawLine(geometry::RealPoint2D(1, 1), geometry::RealPoint2D(9, 1), mViewState,
+ mRenderState);
+ exportDevice("test-draw-line.png", mVclDevice);
+ Bitmap bitmap = mVclDevice->GetBitmap(Point(), Size(10, 10));
+ Bitmap::ScopedReadAccess access(bitmap);
+ // Canvas uses AA, which blurs the line, and it cannot be turned off,
+ // so do not check the end points.
+ CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE), access->GetPixel(0, 0));
+ CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_BLACK), access->GetPixel(1, 2));
+ CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_BLACK), access->GetPixel(1, 8));
+ }
+
+ CPPUNIT_TEST_SUITE(CanvasTest);
+ CPPUNIT_TEST(testDrawLine);
+ CPPUNIT_TEST_SUITE_END();
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(CanvasTest);
+
+CPPUNIT_PLUGIN_IMPLEMENT();
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/canvas/source/vcl/canvas.cxx b/canvas/source/vcl/canvas.cxx
index 467da51f37ed..e9ebf85b3b3a 100644
--- a/canvas/source/vcl/canvas.cxx
+++ b/canvas/source/vcl/canvas.cxx
@@ -19,40 +19,19 @@
#include <sal/config.h>
+#include "canvas.hxx"
+
#include <com/sun/star/lang/NoSupportException.hpp>
#include <sal/log.hxx>
#include <tools/diagnose_ex.h>
#include <vcl/outdev.hxx>
-#include "canvas.hxx"
+#include "outdevholder.hxx"
using namespace ::com::sun::star;
namespace vclcanvas
{
- namespace
- {
- class OutDevHolder : public OutDevProvider
- {
- public:
- OutDevHolder(const OutDevHolder&) = delete;
- const OutDevHolder& operator=(const OutDevHolder&) = delete;
-
- explicit OutDevHolder( OutputDevice& rOutDev ) :
- mrOutDev(rOutDev)
- {}
-
- private:
- virtual OutputDevice& getOutDev() override { return mrOutDev; }
- virtual const OutputDevice& getOutDev() const override { return mrOutDev; }
-
- // TODO(Q2): Lifetime issue. This _only_ works reliably,
- // if disposing the Canvas correctly disposes all
- // entities which hold this pointer.
- OutputDevice& mrOutDev;
- };
- }
-
Canvas::Canvas( const uno::Sequence< uno::Any >& aArguments,
const uno::Reference< uno::XComponentContext >& /*rxContext*/ ) :
maArguments(aArguments)
diff --git a/canvas/source/vcl/outdevholder.hxx b/canvas/source/vcl/outdevholder.hxx
new file mode 100644
index 000000000000..78f3badf8bb6
--- /dev/null
+++ b/canvas/source/vcl/outdevholder.hxx
@@ -0,0 +1,51 @@
+/* -*- 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#pragma once
+
+#include <com/sun/star/uno/Reference.hxx>
+#include <vcl/outdev.hxx>
+
+#include "outdevprovider.hxx"
+
+namespace vclcanvas
+{
+class OutDevHolder : public OutDevProvider
+{
+public:
+ OutDevHolder(const OutDevHolder&) = delete;
+ const OutDevHolder& operator=(const OutDevHolder&) = delete;
+
+ explicit OutDevHolder(OutputDevice& rOutDev)
+ : mrOutDev(rOutDev)
+ {
+ }
+
+private:
+ virtual OutputDevice& getOutDev() override { return mrOutDev; }
+ virtual const OutputDevice& getOutDev() const override { return mrOutDev; }
+
+ // TODO(Q2): Lifetime issue. This _only_ works reliably,
+ // if disposing the Canvas correctly disposes all
+ // entities which hold this pointer.
+ OutputDevice& mrOutDev;
+};
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/canvas/source/vcl/spritecanvas.cxx b/canvas/source/vcl/spritecanvas.cxx
index 2238a79127f0..60c65fbd7bce 100644
--- a/canvas/source/vcl/spritecanvas.cxx
+++ b/canvas/source/vcl/spritecanvas.cxx
@@ -21,10 +21,12 @@
#include <sal/log.hxx>
#include <com/sun/star/awt/XTopWindow.hpp>
+#include <com/sun/star/lang/NoSupportException.hpp>
#include <cppuhelper/supportsservice.hxx>
#include <tools/diagnose_ex.h>
#include "spritecanvas.hxx"
+#include "outdevholder.hxx"
#include "windowoutdevholder.hxx"
@@ -75,14 +77,27 @@ namespace vclcanvas
maArguments[3].getValueTypeClass() == uno::TypeClass_INTERFACE,
"VCLSpriteCanvas::initialize: wrong number of arguments, or wrong types" );
+ sal_Int64 nPtr = 0;
+ maArguments[0] >>= nPtr;
+
+ OutputDevice* pOutDev = reinterpret_cast<OutputDevice*>(nPtr);
+ if( !pOutDev )
+ throw lang::NoSupportException("Passed OutDev invalid!", nullptr);
+
uno::Reference< awt::XWindow > xParentWindow;
maArguments[3] >>= xParentWindow;
- OutDevProviderSharedPtr pOutDev = std::make_shared<WindowOutDevHolder>(xParentWindow);
+ OutDevProviderSharedPtr pOutDevProvider;
+ if( xParentWindow.is())
+ pOutDevProvider = std::make_shared<WindowOutDevHolder>(xParentWindow);
+ else
+ pOutDevProvider = std::make_shared<OutDevHolder>(*pOutDev);
// setup helper
- maDeviceHelper.init( pOutDev );
- setWindow(uno::Reference<awt::XWindow2>(xParentWindow, uno::UNO_QUERY_THROW));
+ maDeviceHelper.init( pOutDevProvider );
+ setWindow( xParentWindow.is()
+ ? uno::Reference<awt::XWindow2>(xParentWindow, uno::UNO_QUERY_THROW)
+ : uno::Reference<awt::XWindow2>());
maCanvasHelper.init( maDeviceHelper.getBackBuffer(),
*this,
maRedrawManager,