2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import com
.sun
.star
.awt
.Point
;
21 import com
.sun
.star
.beans
.PropertyValue
;
22 import com
.sun
.star
.container
.XIndexAccess
;
23 import com
.sun
.star
.container
.XNamed
;
24 import com
.sun
.star
.drawing
.XDrawPage
;
25 import com
.sun
.star
.drawing
.XDrawPagesSupplier
;
26 import com
.sun
.star
.drawing
.XShape
;
27 import com
.sun
.star
.frame
.XDispatchHelper
;
28 import com
.sun
.star
.frame
.XDispatchProvider
;
29 import com
.sun
.star
.frame
.XModel
;
30 import com
.sun
.star
.lang
.XMultiServiceFactory
;
31 import com
.sun
.star
.sheet
.XDocumentAuditing
;
32 import com
.sun
.star
.sheet
.XSheetAuditing
;
33 import com
.sun
.star
.sheet
.XSpreadsheet
;
34 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
35 import com
.sun
.star
.sheet
.XSpreadsheets
;
36 import com
.sun
.star
.table
.CellAddress
;
37 import com
.sun
.star
.uno
.UnoRuntime
;
38 import lib
.MultiMethodTest
;
40 import lib
.StatusException
;
45 public class _XDocumentAuditing
extends MultiMethodTest
{
46 public XDocumentAuditing oObj
= null;
47 XDrawPage xDrawPage
= null;
48 XSpreadsheet
[] xSheet
= null;
50 String sheetName
= null;
54 public void before() {
57 xSheet
= new XSpreadsheet
[2];
59 XSpreadsheetDocument xSpreadsheetDocument
= UnoRuntime
.queryInterface(XSpreadsheetDocument
.class, oObj
);
60 XSpreadsheets oSheets
= xSpreadsheetDocument
.getSheets();
61 XIndexAccess oIndexSheets
= UnoRuntime
.queryInterface(
62 XIndexAccess
.class, oSheets
);
63 XSpreadsheet oSheet
= UnoRuntime
.queryInterface(
64 XSpreadsheet
.class, oIndexSheets
.getByIndex(0));
66 oSheet
= UnoRuntime
.queryInterface(
67 XSpreadsheet
.class, oIndexSheets
.getByIndex(1));
70 catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
73 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
76 catch(NullPointerException e
) {
80 throw new StatusException("Could not get two sheets.", ex
);
83 // get the draw page for checking the shapes
84 xDrawPage
= (XDrawPage
)tEnv
.getObjRelation("XDocumentAuditing.DrawPage");
85 if (xDrawPage
== null) { // get from object
87 XDrawPagesSupplier oDPS
= UnoRuntime
.queryInterface(XDrawPagesSupplier
.class, oObj
);
88 Object o
= oDPS
.getDrawPages().getByIndex(1);
89 xDrawPage
= UnoRuntime
.queryInterface(XDrawPage
.class, o
);
91 catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
92 } // ignore exceptions, we'll run into next if statement anyway
93 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
96 if (xDrawPage
== null) {
97 throw new StatusException(Status
.failed("'XSheetAuditing.DrawPage' object relation not found."));
99 if (xDrawPage
.hasElements()) {
100 elementCount
= xDrawPage
.getCount();
103 // switch off the automatic refresh
104 PropertyValue
[] props
= new PropertyValue
[1];
105 props
[0] = new PropertyValue();
106 props
[0].Name
= "AutoRefreshArrows";
107 props
[0].Value
= Boolean
.FALSE
;
108 XModel xModel
= UnoRuntime
.queryInterface(XModel
.class, oObj
);
109 dispatch(xModel
.getCurrentController().getFrame(), tParam
.getMSF(), ".uno:AutoRefreshArrows", props
);
111 // prepare the sheets
113 xSheet
[0].getCellByPosition(6, 6).setValue(9);
114 XNamed xNamed
= UnoRuntime
.queryInterface(XNamed
.class, xSheet
[0]);
115 sheetName
= xNamed
.getName();
116 xSheet
[1].getCellByPosition(6, 6).setValue(16);
117 xSheet
[1].getCellByPosition(6, 7).setFormula("= SQRT(G7)");
118 XSheetAuditing xSheetAuditing
= UnoRuntime
.queryInterface(XSheetAuditing
.class, xSheet
[1]);
119 CellAddress add
= new CellAddress((short)1, 6, 7);
120 xSheetAuditing
.showPrecedents(add
);
121 boolean ok
= hasRightAmountOfShapes(1);
123 throw new StatusException(Status
.failed("Wrong amount of shapes on page."));
125 catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
126 throw new StatusException("Could not set formulas on sheets.", e
);
131 public void after() {
132 // switch the automatic refresh back on
133 PropertyValue
[] props
= new PropertyValue
[1];
134 props
[0] = new PropertyValue();
135 props
[0].Name
= "AutoRefreshArrows";
136 props
[0].Value
= Boolean
.TRUE
;
137 XModel xModel
= UnoRuntime
.queryInterface(XModel
.class, oObj
);
138 dispatch(xModel
.getCurrentController().getFrame(), tParam
.getMSF(), ".uno:AutoRefreshArrows", props
);
141 public void _refreshArrows() {
142 boolean result
= true;
147 result
&= xSheet
[1].getCellByPosition(6, 7).getValue() == 4;
148 xSheet
[1].getCellByPosition(6, 7).setFormula("= SQRT(" + sheetName
+ ".G7)");
149 result
&= xSheet
[1].getCellByPosition(6, 7).getValue() == 3;
151 catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
152 throw new StatusException("Could not set formulas on sheets.", e
);
155 result
&= hasRightAmountOfShapes(1);
158 // points have to be the same: if not we have an auto update
159 boolean res
= (p0
.X
== p1
.X
&& p0
.Y
== p1
.Y
);
162 log
.println("Arrow has been refreshed, but this should have been switched off.");
164 oObj
.refreshArrows();
166 result
&= hasRightAmountOfShapes(1);
169 // points have to differ
170 res
= (p1
.X
!= p2
.X
|| p1
.Y
!= p2
.Y
);
173 log
.println("Arrow has not been refreshed.");
175 tRes
.tested("refreshArrows()", result
);
179 * Check if the amount of shapes is the right one after displaying that stuff
180 * 2do improve this: check that the shapes are the correct ones -> convwatch
181 * @desiredValue That's the amount of shapes that have to be here.
182 * @return True, if the actual count of shapes is the same
184 private boolean hasRightAmountOfShapes(int desiredValue
) {
185 int newCount
= xDrawPage
.getCount();
186 if (newCount
!= elementCount
+ desiredValue
) {
190 if (desiredValue
>= 0) {
191 for (int i
=elementCount
; i
<newCount
; i
++) {
193 Object o
= xDrawPage
.getByIndex(i
);
194 XShape xShape
= UnoRuntime
.queryInterface(XShape
.class, o
);
195 pos
= xShape
.getPosition();
196 System
.out
.println("Shape Type: " + xShape
.getShapeType());
198 catch(com
.sun
.star
.uno
.Exception e
) {
207 private void dispatch(Object oProvider
, XMultiServiceFactory xMSF
, String url
, PropertyValue
[] prop
) {
208 XDispatchProvider xDispatchProvider
= UnoRuntime
.queryInterface(XDispatchProvider
.class, oProvider
);
209 Object dispatcher
= null;
211 dispatcher
= xMSF
.createInstance("com.sun.star.frame.DispatchHelper");
213 catch(com
.sun
.star
.uno
.Exception e
) {
216 XDispatchHelper xDispatchHelper
= UnoRuntime
.queryInterface(XDispatchHelper
.class, dispatcher
);
217 xDispatchHelper
.executeDispatch(xDispatchProvider
, url
, "", 0, prop
);