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
.beans
.XPropertySet
;
21 import com
.sun
.star
.drawing
.XDrawPage
;
22 import com
.sun
.star
.drawing
.XDrawPageSupplier
;
23 import com
.sun
.star
.drawing
.XShape
;
24 import com
.sun
.star
.sheet
.ValidationType
;
25 import com
.sun
.star
.sheet
.XSheetAuditing
;
26 import com
.sun
.star
.sheet
.XSpreadsheet
;
27 import com
.sun
.star
.table
.CellAddress
;
28 import com
.sun
.star
.table
.XCell
;
29 import com
.sun
.star
.text
.XText
;
30 import com
.sun
.star
.uno
.UnoRuntime
;
31 import lib
.MultiMethodTest
;
33 import lib
.StatusException
;
38 public class _XSheetAuditing
extends MultiMethodTest
{
39 public XSheetAuditing oObj
= null;
40 CellAddress address
= null;
41 CellAddress precedentAddress
= null;
42 CellAddress dependentAddress
= null;
43 XCell xAddress
= null;
44 XCell xPrecedentAddress
= null;
45 XCell xDependentAddress
= null;
46 XDrawPage xDrawPage
= null;
50 public void before() {
51 address
= (CellAddress
)tEnv
.getObjRelation("XSheetAuditing.CellAddress");
52 precedentAddress
= (CellAddress
)tEnv
.getObjRelation("XSheetAuditing.PrecedentCellAddress");
53 dependentAddress
= (CellAddress
)tEnv
.getObjRelation("XSheetAuditing.DependentCellAddress");
54 if (address
== null || precedentAddress
== null || dependentAddress
== null) {
55 throw new StatusException(Status
.failed("Necessary CellAddress object relations not found."));
58 // get the draw page for checking the shapes
59 xDrawPage
= (XDrawPage
)tEnv
.getObjRelation("XSheetAuditing.DrawPage");
60 if (xDrawPage
== null) { // get from object
61 XDrawPageSupplier oDPS
= UnoRuntime
.queryInterface(XDrawPageSupplier
.class, oObj
);
62 xDrawPage
= oDPS
.getDrawPage();
64 if (xDrawPage
== null) {
65 throw new StatusException(Status
.failed("'XSheetAuditing.DrawPage' object relation not found."));
67 if (xDrawPage
.hasElements()) {
68 elementCount
= xDrawPage
.getCount();
71 // get a sheet for changing the cells
72 XSpreadsheet xSheet
= (XSpreadsheet
)tEnv
.getObjRelation("XSheetAuditing.Spreadsheet");
73 if (xSheet
== null) // query on their object
74 xSheet
= UnoRuntime
.queryInterface(XSpreadsheet
.class, oObj
);
76 throw new StatusException(Status
.failed("'XSheetAuditing.Spreadsheet' object relation not found."));
78 xAddress
= xSheet
.getCellByPosition(address
.Column
, address
.Row
);
79 xDependentAddress
= xSheet
.getCellByPosition(dependentAddress
.Column
, dependentAddress
.Row
);
80 xPrecedentAddress
= xSheet
.getCellByPosition(precedentAddress
.Column
, precedentAddress
.Row
);
82 catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
83 throw new StatusException(e
, Status
.failed("Invalid cell addresses in object relations."));
87 public void _clearArrows() {
88 requiredMethod("hideDependents()");
90 oObj
.showDependents(address
);
91 oObj
.showPrecedents(address
);
92 erg
= hasRightAmountOfShapes(3);
94 erg
&= hasRightAmountOfShapes(0);
95 tRes
.tested("clearArrows()", erg
);
98 public void _hideDependents() {
99 requiredMethod("showDependents()");
100 oObj
.hideDependents(address
);
101 tRes
.tested("hideDependents()", hasRightAmountOfShapes(0));
104 public void _hidePrecedents() {
105 requiredMethod("showPrecedents()");
106 oObj
.hidePrecedents(address
);
107 tRes
.tested("hidePrecedents()", hasRightAmountOfShapes(0));
110 public void _showDependents() {
111 requiredMethod("hidePrecedents()");
112 oObj
.showDependents(address
);
113 tRes
.tested("showDependents()", hasRightAmountOfShapes(1));
116 public void _showErrors() {
117 requiredMethod("clearArrows()");
118 // construct an error: square root from -3
119 xPrecedentAddress
.setValue(-9);
120 String cellAddress
= new String(new byte[]{(byte)(precedentAddress
.Column
+ 65)}) + (precedentAddress
.Row
+1);
121 xAddress
.setFormula("=SQRT(" + cellAddress
+ ")");
122 XText xText
= UnoRuntime
.queryInterface(XText
.class, xAddress
);
123 // correct error in cell:
124 String error
= xText
.getString();
125 boolean erg
= error
.equals("Err:502");
126 log
.println("Content: " + error
);
127 oObj
.showErrors(dependentAddress
);
128 erg
&= hasRightAmountOfShapes(2);
129 tRes
.tested("showErrors()", erg
);
132 public void _showInvalid() {
133 requiredMethod("showErrors()");
134 boolean result
= true;
136 xAddress
.setValue(2.5);
138 // add a validitation to a cell: only whole numbers are allowed
139 XPropertySet xPropertySet
= UnoRuntime
.queryInterface(XPropertySet
.class, xAddress
);
140 Object o
= xPropertySet
.getPropertyValue("Validation");
141 XPropertySet xValidation
= UnoRuntime
.queryInterface(XPropertySet
.class, o
);
142 xValidation
.setPropertyValue("Type", ValidationType
.WHOLE
);
143 xPropertySet
.setPropertyValue("Validation", xValidation
);
146 result
= hasRightAmountOfShapes(1);
148 result
&= hasRightAmountOfShapes(0);
149 // revoke the validitation to a cell
150 xValidation
.setPropertyValue("Type", ValidationType
.ANY
);
151 xPropertySet
.setPropertyValue("Validation", xValidation
);
154 result
&= hasRightAmountOfShapes(0);
156 catch(com
.sun
.star
.uno
.Exception e
) {
157 e
.printStackTrace(log
);
161 tRes
.tested("showInvalid()", result
);
164 public void _showPrecedents() {
165 oObj
.showPrecedents(address
);
166 tRes
.tested("showPrecedents()", hasRightAmountOfShapes(2));
170 * Check if the amount of shapes is the right one after displaying that stuff
171 * 2do improve this: check that the shapes are the correct ones -> convwatch
172 * @desiredValue That's the amount of shapes that have to be here.
173 * @return True, if the actual count of shapes is the same
175 private boolean hasRightAmountOfShapes(int desiredValue
) {
176 int newCount
= xDrawPage
.getCount();
177 if (newCount
!= elementCount
+ desiredValue
) {
181 if (desiredValue
>= 0) {
182 for (int i
=elementCount
; i
<newCount
; i
++) {
184 Object o
= xDrawPage
.getByIndex(i
);
185 XShape xShape
= UnoRuntime
.queryInterface(XShape
.class, o
);
186 System
.out
.println("Shape Type: " + xShape
.getShapeType());
188 catch(com
.sun
.star
.uno
.Exception e
) {