1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/sheet/xdocumentauditing.hxx>
12 #include <com/sun/star/awt/Point.hpp>
13 #include <com/sun/star/beans/PropertyValue.hpp>
14 #include <com/sun/star/container/XIndexAccess.hpp>
15 #include <com/sun/star/container/XNamed.hpp>
16 #include <com/sun/star/drawing/XDrawPage.hpp>
17 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
18 #include <com/sun/star/drawing/XShape.hpp>
19 #include <com/sun/star/frame/DispatchHelper.hpp>
20 #include <com/sun/star/frame/XDispatchHelper.hpp>
21 #include <com/sun/star/frame/XDispatchProvider.hpp>
22 #include <com/sun/star/frame/XFrame.hpp>
23 #include <com/sun/star/frame/XModel.hpp>
24 #include <com/sun/star/sheet/XDocumentAuditing.hpp>
25 #include <com/sun/star/sheet/XSheetAuditing.hpp>
26 #include <com/sun/star/sheet/XSpreadsheet.hpp>
27 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
28 #include <com/sun/star/sheet/XSpreadsheets.hpp>
29 #include <com/sun/star/table/CellAddress.hpp>
30 #include <com/sun/star/uno/Reference.hxx>
31 #include <com/sun/star/uno/Sequence.hxx>
32 #include <com/sun/star/uno/XComponentContext.hpp>
34 #include <comphelper/processfactory.hxx>
36 #include <cppunit/TestAssert.h>
38 using namespace com::sun::star
;
39 using namespace com::sun::star::uno
;
43 void XDocumentAuditing::dispatch(const uno::Reference
<frame::XFrame
>& xFrame
,
44 const uno::Sequence
<beans::PropertyValue
>& rArguments
)
46 uno::Reference
<uno::XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
47 uno::Reference
<frame::XDispatchHelper
> xDispatchHelper(frame::DispatchHelper::create(xContext
),
49 CPPUNIT_ASSERT(xDispatchHelper
.is());
51 uno::Reference
<frame::XDispatchProvider
> xDispatchProvider(xFrame
, UNO_QUERY_THROW
);
52 CPPUNIT_ASSERT(xDispatchProvider
.is());
54 xDispatchHelper
->executeDispatch(xDispatchProvider
, ".uno:AutoRefreshArrows", "", 0,
58 bool XDocumentAuditing::hasRightAmountOfShapes(const uno::Reference
<drawing::XDrawPage
>& xDrawPage
,
59 sal_Int32 nElementCount
, sal_Int32 nShapes
)
61 const sal_Int32 nCount
= xDrawPage
->getCount();
62 if (nCount
!= nElementCount
+ nShapes
)
68 for (sal_Int32 i
= nElementCount
; i
< nCount
; i
++)
70 uno::Reference
<drawing::XShape
> xShape(xDrawPage
->getByIndex(i
), UNO_QUERY_THROW
);
71 m_Position
= xShape
->getPosition();
78 void XDocumentAuditing::testRefreshArrows()
80 uno::Reference
<sheet::XDocumentAuditing
> xDocumentAuditing(init(), UNO_QUERY_THROW
);
82 uno::Reference
<sheet::XSpreadsheetDocument
> xDoc(xDocumentAuditing
, UNO_QUERY_THROW
);
83 uno::Reference
<sheet::XSpreadsheets
> xSheets(xDoc
->getSheets(), UNO_SET_THROW
);
84 uno::Reference
<container::XIndexAccess
> xIA(xSheets
, UNO_QUERY_THROW
);
85 uno::Reference
<sheet::XSpreadsheet
> xSheet1(xIA
->getByIndex(0), UNO_QUERY_THROW
);
86 uno::Reference
<sheet::XSpreadsheet
> xSheet2(xIA
->getByIndex(1), UNO_QUERY_THROW
);
88 uno::Reference
<drawing::XDrawPagesSupplier
> xDPS(xDocumentAuditing
, UNO_QUERY_THROW
);
89 uno::Reference
<drawing::XDrawPage
> xDrawPage(xDPS
->getDrawPages()->getByIndex(1),
92 sal_Int32 nDrawPageElementCount
= 0;
93 if (xDrawPage
->hasElements())
94 nDrawPageElementCount
= xDrawPage
->getCount();
96 uno::Sequence
<beans::PropertyValue
> aPropertyValue(1);
99 aPropertyValue
[0].Name
= "AutoRefreshArrows";
100 aPropertyValue
[0].Value
= aValue
;
101 uno::Reference
<frame::XModel
> xModel(xDocumentAuditing
, UNO_QUERY_THROW
);
102 dispatch(xModel
->getCurrentController()->getFrame(), aPropertyValue
);
104 xSheet1
->getCellByPosition(6, 6)->setValue(9);
105 uno::Reference
<container::XNamed
> xNA1(xSheet1
, UNO_QUERY_THROW
);
106 OUString sSheet1Name
= xNA1
->getName();
108 xSheet2
->getCellByPosition(6, 6)->setValue(16);
109 xSheet2
->getCellByPosition(6, 7)->setFormula("= SQRT(G7)");
111 uno::Reference
<sheet::XSheetAuditing
> xSheetAuditing(xSheet2
, UNO_QUERY_THROW
);
112 xSheetAuditing
->showPrecedents(table::CellAddress(1, 6, 7));
113 bool bResult
= hasRightAmountOfShapes(xDrawPage
, nDrawPageElementCount
, 1);
114 CPPUNIT_ASSERT_MESSAGE("Wrong amount of shapes on page", bResult
);
115 awt::Point Position0
= m_Position
;
117 CPPUNIT_ASSERT_DOUBLES_EQUAL(4, xSheet2
->getCellByPosition(6, 7)->getValue(), 0.1);
118 xSheet2
->getCellByPosition(6, 7)->setFormula("= SQRT(" + sSheet1Name
+ ".G7)");
119 CPPUNIT_ASSERT_DOUBLES_EQUAL(3, xSheet2
->getCellByPosition(6, 7)->getValue(), 0.1);
121 bResult
= hasRightAmountOfShapes(xDrawPage
, nDrawPageElementCount
, 1);
122 CPPUNIT_ASSERT_MESSAGE("Wrong amount of shapes on page", bResult
);
123 awt::Point Position1
= m_Position
;
125 CPPUNIT_ASSERT_MESSAGE("Arrow has been refreshed",
126 Position0
.X
== Position1
.X
&& Position0
.Y
== Position1
.Y
);
128 xDocumentAuditing
->refreshArrows();
130 bResult
= hasRightAmountOfShapes(xDrawPage
, nDrawPageElementCount
, 1);
131 CPPUNIT_ASSERT_MESSAGE("Wrong amount of shapes on page", bResult
);
132 awt::Point Position2
= m_Position
;
134 CPPUNIT_ASSERT_MESSAGE("Arrow has not been refreshed",
135 Position1
.X
!= Position2
.X
|| Position1
.Y
!= Position2
.Y
);
137 } // namespace apitest
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */