1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <test/bootstrapfixture.hxx>
11 #include <unotest/macros_test.hxx>
12 #include <test/xmltesttools.hxx>
14 #include <com/sun/star/beans/XPropertySet.hpp>
15 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
16 #include <com/sun/star/drawing/XDrawPage.hpp>
17 #include <com/sun/star/frame/Desktop.hpp>
18 #include <com/sun/star/text/XTextRange.hpp>
20 #include <drawinglayer/tools/primitive2dxmldump.hxx>
21 #include <rtl/ustring.hxx>
22 #include <svx/sdr/contact/displayinfo.hxx>
23 #include <svx/sdr/contact/viewcontact.hxx>
24 #include <svx/sdr/contact/viewobjectcontact.hxx>
25 #include <svx/sdrpagewindow.hxx>
26 #include <svx/svdpage.hxx>
27 #include <svx/svdpagv.hxx>
28 #include <svx/svdview.hxx>
29 #include <svx/unopage.hxx>
30 #include <vcl/virdev.hxx>
31 #include <comphelper/propertyvalue.hxx>
32 #include <sfx2/viewsh.hxx>
33 #include <svx/svdview.hxx>
34 #include <svx/unoapi.hxx>
35 #include <sal/log.hxx>
37 using namespace ::com::sun::star
;
41 /// Tests for svx/source/svdraw/ code.
42 class SvdrawTest
: public test::BootstrapFixture
, public unotest::MacrosTest
, public XmlTestTools
45 uno::Reference
<lang::XComponent
> mxComponent
;
48 virtual void setUp() override
50 test::BootstrapFixture::setUp();
51 mxDesktop
.set(frame::Desktop::create(m_xContext
));
54 virtual void tearDown() override
58 mxComponent
->dispose();
60 test::BootstrapFixture::tearDown();
62 uno::Reference
<lang::XComponent
>& getComponent() { return mxComponent
; }
65 CPPUNIT_TEST_FIXTURE(SvdrawTest
, testSemiTransparentText
)
67 // Create a new Draw document with a rectangle.
68 getComponent() = loadFromDesktop("private:factory/sdraw");
69 uno::Reference
<lang::XMultiServiceFactory
> xFactory(getComponent(), uno::UNO_QUERY
);
70 uno::Reference
<drawing::XShape
> xShape(
71 xFactory
->createInstance("com.sun.star.drawing.RectangleShape"), uno::UNO_QUERY
);
72 xShape
->setSize(awt::Size(10000, 10000));
73 xShape
->setPosition(awt::Point(1000, 1000));
75 uno::Reference
<drawing::XDrawPagesSupplier
> xDrawPagesSupplier(getComponent(), uno::UNO_QUERY
);
76 uno::Reference
<drawing::XDrawPage
> xDrawPage(xDrawPagesSupplier
->getDrawPages()->getByIndex(0),
78 xDrawPage
->add(xShape
);
80 // Add semi-transparent text on the rectangle.
81 uno::Reference
<text::XTextRange
> xShapeText(xShape
, uno::UNO_QUERY
);
82 xShapeText
->getText()->setString("hello");
84 uno::Reference
<beans::XPropertySet
> xShapeProperties(xShape
, uno::UNO_QUERY
);
85 xShapeProperties
->setPropertyValue("CharColor", uno::makeAny(COL_RED
));
86 sal_Int16 nTransparence
= 75;
87 xShapeProperties
->setPropertyValue("CharTransparence", uno::makeAny(nTransparence
));
89 // Generates drawinglayer primitives for the page.
90 auto pDrawPage
= dynamic_cast<SvxDrawPage
*>(xDrawPage
.get());
91 CPPUNIT_ASSERT(pDrawPage
);
92 SdrPage
* pSdrPage
= pDrawPage
->GetSdrPage();
93 ScopedVclPtrInstance
<VirtualDevice
> aVirtualDevice
;
94 SdrView
aSdrView(pSdrPage
->getSdrModelFromSdrPage(), aVirtualDevice
);
95 SdrPageView
aSdrPageView(pSdrPage
, aSdrView
);
96 SdrPageWindow
* pSdrPageWindow
= aSdrPageView
.GetPageWindow(0);
97 sdr::contact::ObjectContact
& rObjectContactOfPageView
= pSdrPageWindow
->GetObjectContact();
98 const sdr::contact::ViewObjectContact
& rDrawPageVOContact
99 = pSdrPage
->GetViewContact().GetViewObjectContact(rObjectContactOfPageView
);
100 sdr::contact::DisplayInfo aDisplayInfo
;
101 drawinglayer::primitive2d::Primitive2DContainer xPrimitiveSequence
102 = rDrawPageVOContact
.getPrimitive2DSequenceHierarchy(aDisplayInfo
);
104 // Make sure the text is semi-transparent.
105 drawinglayer::tools::Primitive2dXmlDump aDumper
;
106 xmlDocPtr pDocument
= aDumper
.dumpAndParse(xPrimitiveSequence
);
108 // Without the accompanying fix in place, this test would have failed with:
111 // - XPath '//unifiedtransparence' number of nodes is incorrect
112 // i.e. the text was just plain red, not semi-transparent.
113 double fTransparence
= getXPath(pDocument
, "//unifiedtransparence", "transparence").toDouble();
114 CPPUNIT_ASSERT_EQUAL(nTransparence
,
115 static_cast<sal_Int16
>(basegfx::fround(fTransparence
* 100)));
118 CPPUNIT_TEST_FIXTURE(SvdrawTest
, testTextEditEmptyGrabBag
)
120 // Given a document with a groupshape, which has 2 children.
121 getComponent() = loadFromDesktop("private:factory/sdraw");
122 uno::Reference
<lang::XMultiServiceFactory
> xFactory(getComponent(), uno::UNO_QUERY
);
123 uno::Reference
<drawing::XShape
> xRect1(
124 xFactory
->createInstance("com.sun.star.drawing.RectangleShape"), uno::UNO_QUERY
);
125 xRect1
->setPosition(awt::Point(1000, 1000));
126 xRect1
->setSize(awt::Size(10000, 10000));
127 uno::Reference
<drawing::XShape
> xRect2(
128 xFactory
->createInstance("com.sun.star.drawing.RectangleShape"), uno::UNO_QUERY
);
129 xRect2
->setPosition(awt::Point(1000, 1000));
130 xRect2
->setSize(awt::Size(10000, 10000));
131 uno::Reference
<drawing::XShapes
> xGroup(
132 xFactory
->createInstance("com.sun.star.drawing.GroupShape"), uno::UNO_QUERY
);
133 uno::Reference
<drawing::XDrawPagesSupplier
> xDrawPagesSupplier(getComponent(), uno::UNO_QUERY
);
134 uno::Reference
<drawing::XDrawPage
> xDrawPage(xDrawPagesSupplier
->getDrawPages()->getByIndex(0),
136 uno::Reference
<drawing::XShape
> xGroupShape(xGroup
, uno::UNO_QUERY
);
137 xDrawPage
->add(xGroupShape
);
140 uno::Reference
<text::XTextRange
> xRect2Text(xRect2
, uno::UNO_QUERY
);
141 xRect2Text
->setString("x");
142 uno::Sequence
<beans::PropertyValue
> aGrabBag
= {
143 comphelper::makePropertyValue("OOXLayout", true),
145 uno::Reference
<beans::XPropertySet
> xGroupProps(xGroup
, uno::UNO_QUERY
);
146 xGroupProps
->setPropertyValue("InteropGrabBag", uno::makeAny(aGrabBag
));
148 // When editing the shape text of the 2nd rectangle (insert a char at the start).
149 SfxViewShell
* pViewShell
= SfxViewShell::Current();
150 SdrView
* pSdrView
= pViewShell
->GetDrawView();
151 SdrObject
* pObject
= GetSdrObjectFromXShape(xRect2
);
152 pSdrView
->SdrBeginTextEdit(pObject
);
153 EditView
& rEditView
= pSdrView
->GetTextEditOutlinerView()->GetEditView();
154 rEditView
.InsertText("y");
155 pSdrView
->SdrEndTextEdit();
157 // Then make sure that grab-bag is empty to avoid loosing the new text.
158 xGroupProps
->getPropertyValue("InteropGrabBag") >>= aGrabBag
;
159 // Without the accompanying fix in place, this test would have failed with:
161 // - Expression: !aGrabBag.hasElements()
162 // i.e. the grab-bag was still around after modifying the shape, and that grab-bag contained the
164 CPPUNIT_ASSERT(!aGrabBag
.hasElements());
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */