1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XDocumentAuditing.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 import com
.sun
.star
.awt
.Point
;
33 import com
.sun
.star
.beans
.PropertyValue
;
34 import com
.sun
.star
.container
.XIndexAccess
;
35 import com
.sun
.star
.container
.XNamed
;
36 import com
.sun
.star
.drawing
.XDrawPage
;
37 import com
.sun
.star
.drawing
.XDrawPagesSupplier
;
38 import com
.sun
.star
.drawing
.XShape
;
39 import com
.sun
.star
.frame
.XDispatchHelper
;
40 import com
.sun
.star
.frame
.XDispatchProvider
;
41 import com
.sun
.star
.frame
.XModel
;
42 import com
.sun
.star
.lang
.XMultiServiceFactory
;
43 import com
.sun
.star
.sheet
.XDocumentAuditing
;
44 import com
.sun
.star
.sheet
.XSheetAuditing
;
45 import com
.sun
.star
.sheet
.XSpreadsheet
;
46 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
47 import com
.sun
.star
.sheet
.XSpreadsheets
;
48 import com
.sun
.star
.table
.CellAddress
;
49 import com
.sun
.star
.uno
.UnoRuntime
;
50 import lib
.MultiMethodTest
;
52 import lib
.StatusException
;
57 public class _XDocumentAuditing
extends MultiMethodTest
{
58 public XDocumentAuditing oObj
= null;
59 XDrawPage xDrawPage
= null;
60 XSpreadsheet
[] xSheet
= null;
62 String sheetName
= null;
65 public void before() {
68 xSheet
= new XSpreadsheet
[2];
70 XSpreadsheetDocument xSpreadsheetDocument
= (XSpreadsheetDocument
)
71 UnoRuntime
.queryInterface(XSpreadsheetDocument
.class, oObj
);
72 XSpreadsheets oSheets
= xSpreadsheetDocument
.getSheets();
73 XIndexAccess oIndexSheets
= (XIndexAccess
) UnoRuntime
.queryInterface(
74 XIndexAccess
.class, oSheets
);
75 XSpreadsheet oSheet
= (XSpreadsheet
) UnoRuntime
.queryInterface(
76 XSpreadsheet
.class, oIndexSheets
.getByIndex(0));
78 oSheet
= (XSpreadsheet
) UnoRuntime
.queryInterface(
79 XSpreadsheet
.class, oIndexSheets
.getByIndex(1));
82 catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
85 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
88 catch(java
.lang
.NullPointerException e
) {
92 throw new StatusException("Could not get two sheets.", ex
);
95 // get the draw page for checking the shapes
96 xDrawPage
= (XDrawPage
)tEnv
.getObjRelation("XDocumentAuditing.DrawPage");
97 if (xDrawPage
== null) { // get from object
99 XDrawPagesSupplier oDPS
= (XDrawPagesSupplier
)
100 UnoRuntime
.queryInterface(XDrawPagesSupplier
.class, oObj
);
101 Object o
= oDPS
.getDrawPages().getByIndex(1);
102 xDrawPage
= (XDrawPage
)UnoRuntime
.queryInterface(XDrawPage
.class, o
);
104 catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
105 } // ignore exceptions, we'll run into next if statement anyway
106 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
109 if (xDrawPage
== null) {
110 throw new StatusException(Status
.failed("'XSheetAuditing.DrawPage' object relation not found."));
112 if (xDrawPage
.hasElements()) {
113 elementCount
= xDrawPage
.getCount();
116 // switch off the automatic refresh
117 PropertyValue
[] props
= new PropertyValue
[1];
118 props
[0] = new PropertyValue();
119 props
[0].Name
= "AutoRefreshArrows";
120 props
[0].Value
= Boolean
.FALSE
;
121 XModel xModel
= (XModel
)UnoRuntime
.queryInterface(XModel
.class, oObj
);
122 dispatch(xModel
.getCurrentController().getFrame(), (XMultiServiceFactory
)tParam
.getMSF(), ".uno:AutoRefreshArrows", props
);
124 // prepare the sheets
126 xSheet
[0].getCellByPosition(6, 6).setValue(9);
127 XNamed xNamed
= (XNamed
)UnoRuntime
.queryInterface(XNamed
.class, xSheet
[0]);
128 sheetName
= xNamed
.getName();
129 xSheet
[1].getCellByPosition(6, 6).setValue(16);
130 xSheet
[1].getCellByPosition(6, 7).setFormula("= SQRT(G7)");
131 XSheetAuditing xSheetAuditing
= (XSheetAuditing
)UnoRuntime
.queryInterface(XSheetAuditing
.class, xSheet
[1]);
132 CellAddress add
= new CellAddress((short)1, 6, 7);
133 xSheetAuditing
.showPrecedents(add
);
134 boolean ok
= hasRightAmountOfShapes(1);
136 throw new StatusException(Status
.failed("Wrong amount of shapes on page."));
138 catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
139 throw new StatusException("Could not set formulas on sheets.", e
);
143 public void after() {
144 // switch the automatic refresh back on
145 PropertyValue
[] props
= new PropertyValue
[1];
146 props
[0] = new PropertyValue();
147 props
[0].Name
= "AutoRefreshArrows";
148 props
[0].Value
= Boolean
.TRUE
;
149 XModel xModel
= (XModel
)UnoRuntime
.queryInterface(XModel
.class, oObj
);
150 dispatch(xModel
.getCurrentController().getFrame(), (XMultiServiceFactory
)tParam
.getMSF(), ".uno:AutoRefreshArrows", props
);
153 public void _refreshArrows() {
154 boolean result
= true;
159 result
&= xSheet
[1].getCellByPosition(6, 7).getValue() == 4;
160 xSheet
[1].getCellByPosition(6, 7).setFormula("= SQRT(" + sheetName
+ ".G7)");
161 result
&= xSheet
[1].getCellByPosition(6, 7).getValue() == 3;
163 catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
164 throw new StatusException("Could not set formulas on sheets.", e
);
167 result
&= hasRightAmountOfShapes(1);
170 // points have to be the same: if not we have an auto update
171 boolean res
= (p0
.X
== p1
.X
&& p0
.Y
== p1
.Y
);
174 log
.println("Arrow has been refreshed, but this should have been switched off.");
176 oObj
.refreshArrows();
178 result
&= hasRightAmountOfShapes(1);
181 // points have to differ
182 res
= (p1
.X
!= p2
.X
|| p1
.Y
!= p2
.Y
);
185 log
.println("Arrow has not been refreshed.");
187 tRes
.tested("refreshArrows()", result
);
191 * Check if the amount of shapes is the right one after displaying that stuff
192 * 2do improve this: check taht the shapes are the correct ones -> convwatch
193 * @desiredValue That's the amount of shapes that have to be here.
194 * @return True, if the actual count of shapes is the same
196 private boolean hasRightAmountOfShapes(int desiredValue
) {
197 int newCount
= xDrawPage
.getCount();
198 if (newCount
!= elementCount
+ desiredValue
) {
202 if (desiredValue
>= 0) {
203 for (int i
=elementCount
; i
<newCount
; i
++) {
205 Object o
= xDrawPage
.getByIndex(i
);
206 XShape xShape
= (XShape
)UnoRuntime
.queryInterface(XShape
.class, o
);
207 pos
= xShape
.getPosition();
208 System
.out
.println("Shape Type: " + xShape
.getShapeType());
210 catch(com
.sun
.star
.uno
.Exception e
) {
219 private void dispatch(Object oProvider
, XMultiServiceFactory xMSF
, String url
, PropertyValue
[] prop
) {
220 XDispatchProvider xDispatchProvider
= (XDispatchProvider
)UnoRuntime
.queryInterface(XDispatchProvider
.class, oProvider
);
221 Object dispatcher
= null;
223 dispatcher
= xMSF
.createInstance("com.sun.star.frame.DispatchHelper");
225 catch(com
.sun
.star
.uno
.Exception e
) {
228 XDispatchHelper xDispatchHelper
= (XDispatchHelper
)UnoRuntime
.queryInterface(XDispatchHelper
.class, dispatcher
);
229 xDispatchHelper
.executeDispatch(xDispatchProvider
, url
, "", 0, prop
);