nss: upgrade to release 3.73
[LibreOffice.git] / svx / qa / unit / svdraw.cxx
blob783420d56feab58cb7a27accc4165fb2a4b1c196
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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/svdpage.hxx>
26 #include <svx/unopage.hxx>
27 #include <vcl/virdev.hxx>
28 #include <comphelper/propertyvalue.hxx>
29 #include <sfx2/viewsh.hxx>
30 #include <svx/svdview.hxx>
31 #include <svx/unoapi.hxx>
32 #include <sal/log.hxx>
34 #include <sdr/contact/objectcontactofobjlistpainter.hxx>
36 using namespace ::com::sun::star;
38 namespace
40 /// Tests for svx/source/svdraw/ code.
41 class SvdrawTest : public test::BootstrapFixture, public unotest::MacrosTest, public XmlTestTools
43 protected:
44 uno::Reference<lang::XComponent> mxComponent;
46 public:
47 virtual void setUp() override
49 test::BootstrapFixture::setUp();
50 mxDesktop.set(frame::Desktop::create(m_xContext));
53 virtual void tearDown() override
55 if (mxComponent.is())
57 mxComponent->dispose();
59 test::BootstrapFixture::tearDown();
61 uno::Reference<lang::XComponent>& getComponent() { return mxComponent; }
64 CPPUNIT_TEST_FIXTURE(SvdrawTest, testSemiTransparentText)
66 // Create a new Draw document with a rectangle.
67 getComponent() = loadFromDesktop("private:factory/sdraw");
68 uno::Reference<lang::XMultiServiceFactory> xFactory(getComponent(), uno::UNO_QUERY);
69 uno::Reference<drawing::XShape> xShape(
70 xFactory->createInstance("com.sun.star.drawing.RectangleShape"), uno::UNO_QUERY);
71 xShape->setSize(awt::Size(10000, 10000));
72 xShape->setPosition(awt::Point(1000, 1000));
74 uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier(getComponent(), uno::UNO_QUERY);
75 uno::Reference<drawing::XDrawPage> xDrawPage(xDrawPagesSupplier->getDrawPages()->getByIndex(0),
76 uno::UNO_QUERY);
77 xDrawPage->add(xShape);
79 // Add semi-transparent text on the rectangle.
80 uno::Reference<text::XTextRange> xShapeText(xShape, uno::UNO_QUERY);
81 xShapeText->getText()->setString("hello");
83 uno::Reference<beans::XPropertySet> xShapeProperties(xShape, uno::UNO_QUERY);
84 xShapeProperties->setPropertyValue("CharColor", uno::makeAny(COL_RED));
85 sal_Int16 nTransparence = 75;
86 xShapeProperties->setPropertyValue("CharTransparence", uno::makeAny(nTransparence));
88 // Generates drawinglayer primitives for the page.
89 auto pDrawPage = dynamic_cast<SvxDrawPage*>(xDrawPage.get());
90 CPPUNIT_ASSERT(pDrawPage);
91 SdrPage* pSdrPage = pDrawPage->GetSdrPage();
92 ScopedVclPtrInstance<VirtualDevice> aVirtualDevice;
93 sdr::contact::ObjectContactOfObjListPainter aObjectContact(*aVirtualDevice,
94 { pSdrPage->GetObj(0) }, nullptr);
95 const sdr::contact::ViewObjectContact& rDrawPageVOContact
96 = pSdrPage->GetViewContact().GetViewObjectContact(aObjectContact);
97 sdr::contact::DisplayInfo aDisplayInfo;
98 drawinglayer::primitive2d::Primitive2DContainer xPrimitiveSequence
99 = rDrawPageVOContact.getPrimitive2DSequenceHierarchy(aDisplayInfo);
101 // Make sure the text is semi-transparent.
102 drawinglayer::Primitive2dXmlDump aDumper;
103 xmlDocUniquePtr pDocument = aDumper.dumpAndParse(xPrimitiveSequence);
105 // Without the accompanying fix in place, this test would have failed with:
106 // - Expected: 1
107 // - Actual : 0
108 // - XPath '//unifiedtransparence' number of nodes is incorrect
109 // i.e. the text was just plain red, not semi-transparent.
110 double fTransparence = getXPath(pDocument, "//unifiedtransparence", "transparence").toDouble();
111 CPPUNIT_ASSERT_EQUAL(nTransparence,
112 static_cast<sal_Int16>(basegfx::fround(fTransparence * 100)));
115 CPPUNIT_TEST_FIXTURE(SvdrawTest, testTextEditEmptyGrabBag)
117 // Given a document with a groupshape, which has 2 children.
118 getComponent() = loadFromDesktop("private:factory/sdraw");
119 uno::Reference<lang::XMultiServiceFactory> xFactory(getComponent(), uno::UNO_QUERY);
120 uno::Reference<drawing::XShape> xRect1(
121 xFactory->createInstance("com.sun.star.drawing.RectangleShape"), uno::UNO_QUERY);
122 xRect1->setPosition(awt::Point(1000, 1000));
123 xRect1->setSize(awt::Size(10000, 10000));
124 uno::Reference<drawing::XShape> xRect2(
125 xFactory->createInstance("com.sun.star.drawing.RectangleShape"), uno::UNO_QUERY);
126 xRect2->setPosition(awt::Point(1000, 1000));
127 xRect2->setSize(awt::Size(10000, 10000));
128 uno::Reference<drawing::XShapes> xGroup(
129 xFactory->createInstance("com.sun.star.drawing.GroupShape"), uno::UNO_QUERY);
130 uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier(getComponent(), uno::UNO_QUERY);
131 uno::Reference<drawing::XDrawPage> xDrawPage(xDrawPagesSupplier->getDrawPages()->getByIndex(0),
132 uno::UNO_QUERY);
133 uno::Reference<drawing::XShape> xGroupShape(xGroup, uno::UNO_QUERY);
134 xDrawPage->add(xGroupShape);
135 xGroup->add(xRect1);
136 xGroup->add(xRect2);
137 uno::Reference<text::XTextRange> xRect2Text(xRect2, uno::UNO_QUERY);
138 xRect2Text->setString("x");
139 uno::Sequence<beans::PropertyValue> aGrabBag = {
140 comphelper::makePropertyValue("OOXLayout", true),
142 uno::Reference<beans::XPropertySet> xGroupProps(xGroup, uno::UNO_QUERY);
143 xGroupProps->setPropertyValue("InteropGrabBag", uno::makeAny(aGrabBag));
145 // When editing the shape text of the 2nd rectangle (insert a char at the start).
146 SfxViewShell* pViewShell = SfxViewShell::Current();
147 SdrView* pSdrView = pViewShell->GetDrawView();
148 SdrObject* pObject = GetSdrObjectFromXShape(xRect2);
149 pSdrView->SdrBeginTextEdit(pObject);
150 EditView& rEditView = pSdrView->GetTextEditOutlinerView()->GetEditView();
151 rEditView.InsertText("y");
152 pSdrView->SdrEndTextEdit();
154 // Then make sure that grab-bag is empty to avoid loosing the new text.
155 xGroupProps->getPropertyValue("InteropGrabBag") >>= aGrabBag;
156 // Without the accompanying fix in place, this test would have failed with:
157 // assertion failed
158 // - Expression: !aGrabBag.hasElements()
159 // i.e. the grab-bag was still around after modifying the shape, and that grab-bag contained the
160 // old text.
161 CPPUNIT_ASSERT(!aGrabBag.hasElements());
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */