LibreOffice Module test (master)  1
xdocumentauditing.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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  */
9 
11 
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>
33 
35 
36 #include <cppunit/TestAssert.h>
37 
38 using namespace com::sun::star;
39 using namespace com::sun::star::uno;
40 
41 namespace apitest
42 {
43 void XDocumentAuditing::dispatch(const uno::Reference<frame::XFrame>& xFrame,
44  const uno::Sequence<beans::PropertyValue>& rArguments)
45 {
46  uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
47  uno::Reference<frame::XDispatchHelper> xDispatchHelper(frame::DispatchHelper::create(xContext),
48  UNO_SET_THROW);
49  CPPUNIT_ASSERT(xDispatchHelper.is());
50 
51  uno::Reference<frame::XDispatchProvider> xDispatchProvider(xFrame, UNO_QUERY_THROW);
52  CPPUNIT_ASSERT(xDispatchProvider.is());
53 
54  xDispatchHelper->executeDispatch(xDispatchProvider, ".uno:AutoRefreshArrows", "", 0,
55  rArguments);
56 }
57 
58 bool XDocumentAuditing::hasRightAmountOfShapes(const uno::Reference<drawing::XDrawPage>& xDrawPage,
59  sal_Int32 nElementCount, sal_Int32 nShapes)
60 {
61  const sal_Int32 nCount = xDrawPage->getCount();
62  if (nCount != nElementCount + nShapes)
63  return false;
64  else
65  {
66  if (nShapes >= 0)
67  {
68  for (sal_Int32 i = nElementCount; i < nCount; i++)
69  {
70  uno::Reference<drawing::XShape> xShape(xDrawPage->getByIndex(i), UNO_QUERY_THROW);
71  m_Position = xShape->getPosition();
72  }
73  }
74  }
75  return true;
76 }
77 
78 void XDocumentAuditing::testRefreshArrows()
79 {
80  uno::Reference<sheet::XDocumentAuditing> xDocumentAuditing(init(), UNO_QUERY_THROW);
81 
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);
87 
88  uno::Reference<drawing::XDrawPagesSupplier> xDPS(xDocumentAuditing, UNO_QUERY_THROW);
89  uno::Reference<drawing::XDrawPage> xDrawPage(xDPS->getDrawPages()->getByIndex(1),
90  UNO_QUERY_THROW);
91 
92  sal_Int32 nDrawPageElementCount = 0;
93  if (xDrawPage->hasElements())
94  nDrawPageElementCount = xDrawPage->getCount();
95 
96  uno::Sequence<beans::PropertyValue> aPropertyValue(1);
97  uno::Any aValue;
98  aValue <<= false;
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);
103 
104  xSheet1->getCellByPosition(6, 6)->setValue(9);
105  uno::Reference<container::XNamed> xNA1(xSheet1, UNO_QUERY_THROW);
106  OUString sSheet1Name = xNA1->getName();
107 
108  xSheet2->getCellByPosition(6, 6)->setValue(16);
109  xSheet2->getCellByPosition(6, 7)->setFormula("= SQRT(G7)");
110 
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;
116 
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);
120 
121  bResult = hasRightAmountOfShapes(xDrawPage, nDrawPageElementCount, 1);
122  CPPUNIT_ASSERT_MESSAGE("Wrong amount of shapes on page", bResult);
123  awt::Point Position1 = m_Position;
124 
125  CPPUNIT_ASSERT_EQUAL_MESSAGE("Arrow has been refreshed", Position0.X, Position1.X);
126  CPPUNIT_ASSERT_EQUAL_MESSAGE("Arrow has been refreshed", Position0.Y, Position1.Y);
127 
128  xDocumentAuditing->refreshArrows();
129 
130  bResult = hasRightAmountOfShapes(xDrawPage, nDrawPageElementCount, 1);
131  CPPUNIT_ASSERT_MESSAGE("Wrong amount of shapes on page", bResult);
132  awt::Point Position2 = m_Position;
133 
134  CPPUNIT_ASSERT_MESSAGE("Arrow has not been refreshed",
135  Position1.X != Position2.X || Position1.Y != Position2.Y);
136 }
137 } // namespace apitest
138 
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
geometry::RealPoint2D m_Position
int nCount
int i
Object Value
FILE * init(int, char **)
Reference< XModel > xModel