summaryrefslogtreecommitdiff
path: root/canvas/qa
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/qa
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/qa')
-rw-r--r--canvas/qa/cppunit/README11
-rw-r--r--canvas/qa/cppunit/canvastest.cxx109
2 files changed, 120 insertions, 0 deletions
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: */