1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package ifc.sheet;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
26cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage;
27cdf0e10cSrcweir import com.sun.star.drawing.XDrawPageSupplier;
28cdf0e10cSrcweir import com.sun.star.drawing.XShape;
29cdf0e10cSrcweir import com.sun.star.sheet.ValidationType;
30cdf0e10cSrcweir import com.sun.star.sheet.XSheetAuditing;
31cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
32cdf0e10cSrcweir import com.sun.star.table.CellAddress;
33cdf0e10cSrcweir import com.sun.star.table.XCell;
34cdf0e10cSrcweir import com.sun.star.text.XText;
35cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
36cdf0e10cSrcweir import lib.MultiMethodTest;
37cdf0e10cSrcweir import lib.Status;
38cdf0e10cSrcweir import lib.StatusException;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir /**
41cdf0e10cSrcweir  *
42cdf0e10cSrcweir  */
43cdf0e10cSrcweir public class _XSheetAuditing extends MultiMethodTest {
44cdf0e10cSrcweir     public XSheetAuditing oObj = null;
45cdf0e10cSrcweir     CellAddress address = null;
46cdf0e10cSrcweir     CellAddress precedentAddress = null;
47cdf0e10cSrcweir     CellAddress dependentAddress = null;
48cdf0e10cSrcweir     XCell xAddress = null;
49cdf0e10cSrcweir     XCell xPrecedentAddress = null;
50cdf0e10cSrcweir     XCell xDependentAddress = null;
51cdf0e10cSrcweir     XDrawPage xDrawPage = null;
52cdf0e10cSrcweir     int elementCount = 0;
53cdf0e10cSrcweir 
before()54cdf0e10cSrcweir     public void before() {
55cdf0e10cSrcweir         address = (CellAddress)tEnv.getObjRelation("XSheetAuditing.CellAddress");
56cdf0e10cSrcweir         precedentAddress = (CellAddress)tEnv.getObjRelation("XSheetAuditing.PrecedentCellAddress");
57cdf0e10cSrcweir         dependentAddress= (CellAddress)tEnv.getObjRelation("XSheetAuditing.DependentCellAddress");
58cdf0e10cSrcweir         if (address == null || precedentAddress == null || dependentAddress == null) {
59cdf0e10cSrcweir             throw new StatusException(Status.failed("Necessary CellAddress object relations not found."));
60cdf0e10cSrcweir         }
61cdf0e10cSrcweir 
62cdf0e10cSrcweir         // get the draw page for checking the shapes
63cdf0e10cSrcweir         xDrawPage = (XDrawPage)tEnv.getObjRelation("XSheetAuditing.DrawPage");
64cdf0e10cSrcweir         if (xDrawPage == null) { // get from object
65cdf0e10cSrcweir             XDrawPageSupplier oDPS = (XDrawPageSupplier)
66cdf0e10cSrcweir                 UnoRuntime.queryInterface(XDrawPageSupplier.class, oObj);
67cdf0e10cSrcweir             xDrawPage = (XDrawPage) oDPS.getDrawPage();
68cdf0e10cSrcweir         }
69cdf0e10cSrcweir         if (xDrawPage == null) {
70cdf0e10cSrcweir             throw new StatusException(Status.failed("'XSheetAuditing.DrawPage' object relation not found."));
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir         if (xDrawPage.hasElements()) {
73cdf0e10cSrcweir             elementCount = xDrawPage.getCount();
74cdf0e10cSrcweir         }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         // get a sheet for changing the cells
77cdf0e10cSrcweir         XSpreadsheet xSheet = (XSpreadsheet)tEnv.getObjRelation("XSheetAuditing.Spreadsheet");
78cdf0e10cSrcweir         if (xSheet == null) // query on ther object
79cdf0e10cSrcweir             xSheet = (XSpreadsheet)UnoRuntime.queryInterface(XSpreadsheet.class, oObj);
80cdf0e10cSrcweir         if (xSheet == null)
81cdf0e10cSrcweir             throw new StatusException(Status.failed("'XSheetAuditing.Spreadsheet' object relation not found."));
82cdf0e10cSrcweir         try {
83cdf0e10cSrcweir             xAddress = xSheet.getCellByPosition(address.Column, address.Row);
84cdf0e10cSrcweir             xDependentAddress = xSheet.getCellByPosition(dependentAddress.Column, dependentAddress.Row);
85cdf0e10cSrcweir             xPrecedentAddress = xSheet.getCellByPosition(precedentAddress.Column, precedentAddress.Row);
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir         catch(com.sun.star.lang.IndexOutOfBoundsException e) {
88cdf0e10cSrcweir             throw new StatusException(Status.failed("Invalid cell addresses in object relations."));
89cdf0e10cSrcweir         }
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
_clearArrows()92cdf0e10cSrcweir     public void _clearArrows() {
93cdf0e10cSrcweir         requiredMethod("hideDependents()");
94cdf0e10cSrcweir         boolean erg = false;
95cdf0e10cSrcweir         oObj.showDependents(address);
96cdf0e10cSrcweir         oObj.showPrecedents(address);
97cdf0e10cSrcweir         erg = hasRightAmountOfShapes(3);
98cdf0e10cSrcweir         oObj.clearArrows();
99cdf0e10cSrcweir         erg &= hasRightAmountOfShapes(0);
100cdf0e10cSrcweir         tRes.tested("clearArrows()", erg);
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir 
_hideDependents()103cdf0e10cSrcweir     public void _hideDependents() {
104cdf0e10cSrcweir         requiredMethod("showDependents()");
105cdf0e10cSrcweir         oObj.hideDependents(address);
106cdf0e10cSrcweir         tRes.tested("hideDependents()", hasRightAmountOfShapes(0));
107cdf0e10cSrcweir     }
108cdf0e10cSrcweir 
_hidePrecedents()109cdf0e10cSrcweir     public void _hidePrecedents() {
110cdf0e10cSrcweir         requiredMethod("showPrecedents()");
111cdf0e10cSrcweir //        requiredMethod("showPrecedents()");
112cdf0e10cSrcweir         oObj.hidePrecedents(address);
113cdf0e10cSrcweir         tRes.tested("hidePrecedents()", hasRightAmountOfShapes(0));
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir 
_showDependents()116cdf0e10cSrcweir     public void _showDependents() {
117cdf0e10cSrcweir         requiredMethod("hidePrecedents()");
118cdf0e10cSrcweir         oObj.showDependents(address);
119cdf0e10cSrcweir         tRes.tested("showDependents()", hasRightAmountOfShapes(1));
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir 
_showErrors()122cdf0e10cSrcweir     public void _showErrors() {
123cdf0e10cSrcweir         requiredMethod("clearArrows()");
124cdf0e10cSrcweir         // construct an error: square root from -3
125cdf0e10cSrcweir         xPrecedentAddress.setValue(-9);
126cdf0e10cSrcweir         String cellAddress = new String(new byte[]{(byte)(precedentAddress.Column + 65)}) + (precedentAddress.Row+1);
127cdf0e10cSrcweir         xAddress.setFormula("=SQRT(" + cellAddress + ")");
128cdf0e10cSrcweir         XText xText = (XText)UnoRuntime.queryInterface(XText.class, xAddress);
129cdf0e10cSrcweir         // correct error in cell:
130cdf0e10cSrcweir         String error = xText.getString();
131cdf0e10cSrcweir         boolean erg = error.equals("Err:502");
132cdf0e10cSrcweir         log.println("Content: " + error);
133cdf0e10cSrcweir         oObj.showErrors(dependentAddress);
134cdf0e10cSrcweir         erg &= hasRightAmountOfShapes(2);
135cdf0e10cSrcweir         tRes.tested("showErrors()", erg);
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
_showInvalid()138cdf0e10cSrcweir     public void _showInvalid() {
139cdf0e10cSrcweir         requiredMethod("showErrors()");
140cdf0e10cSrcweir         boolean result = true;
141cdf0e10cSrcweir         // insert a value
142cdf0e10cSrcweir         xAddress.setValue(2.5);
143cdf0e10cSrcweir         try {
144cdf0e10cSrcweir             // add a validitation to a cell: only whole numbers are allowed
145cdf0e10cSrcweir             XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xAddress);
146cdf0e10cSrcweir             Object o = xPropertySet.getPropertyValue("Validation");
147cdf0e10cSrcweir             XPropertySet xValidation = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, o);
148cdf0e10cSrcweir             xValidation.setPropertyValue("Type", ValidationType.WHOLE);
149cdf0e10cSrcweir             xPropertySet.setPropertyValue("Validation", xValidation);
150cdf0e10cSrcweir             // test
151cdf0e10cSrcweir             oObj.showInvalid();
152cdf0e10cSrcweir             result = hasRightAmountOfShapes(1);
153cdf0e10cSrcweir             oObj.clearArrows();
154cdf0e10cSrcweir             result &= hasRightAmountOfShapes(0);
155cdf0e10cSrcweir             // revoke the validitation to a cell
156cdf0e10cSrcweir             xValidation.setPropertyValue("Type", ValidationType.ANY);
157cdf0e10cSrcweir             xPropertySet.setPropertyValue("Validation", xValidation);
158cdf0e10cSrcweir             // test again
159cdf0e10cSrcweir             oObj.showInvalid();
160cdf0e10cSrcweir             result &= hasRightAmountOfShapes(0);
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir         catch(com.sun.star.uno.Exception e) {
163cdf0e10cSrcweir             e.printStackTrace((java.io.PrintWriter)log);
164cdf0e10cSrcweir             result = false;
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir         tRes.tested("showInvalid()", result);
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir 
_showPrecedents()170cdf0e10cSrcweir     public void _showPrecedents() {
171cdf0e10cSrcweir         oObj.showPrecedents(address);
172cdf0e10cSrcweir         tRes.tested("showPrecedents()", hasRightAmountOfShapes(2));
173cdf0e10cSrcweir     }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     /**
176cdf0e10cSrcweir      * Check if the amount of shapes is the right one after displaying that stuff
177cdf0e10cSrcweir      * 2do improve this: check taht the shapes are the correct ones -> convwatch
178cdf0e10cSrcweir      * @desiredValue That's the amount of shapes that have to be here.
179cdf0e10cSrcweir      * @return True, if the actual count of shapes is the same
180cdf0e10cSrcweir      */
hasRightAmountOfShapes(int desiredValue)181cdf0e10cSrcweir     private boolean hasRightAmountOfShapes(int desiredValue) {
182cdf0e10cSrcweir         int newCount = xDrawPage.getCount();
183cdf0e10cSrcweir         if (newCount != elementCount + desiredValue) {
184cdf0e10cSrcweir             return false;
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir         else {
187cdf0e10cSrcweir             if (desiredValue >= 0) {
188cdf0e10cSrcweir                 for (int i=elementCount; i<newCount; i++) {
189cdf0e10cSrcweir                     try {
190cdf0e10cSrcweir                         Object o = xDrawPage.getByIndex(i);
191cdf0e10cSrcweir                         XShape xShape = (XShape)UnoRuntime.queryInterface(XShape.class, o);
192cdf0e10cSrcweir                         System.out.println("Shape Type: " + xShape.getShapeType());
193cdf0e10cSrcweir                     }
194cdf0e10cSrcweir                     catch(com.sun.star.uno.Exception e) {
195cdf0e10cSrcweir                         e.printStackTrace();
196cdf0e10cSrcweir                     }
197cdf0e10cSrcweir                 }
198cdf0e10cSrcweir             }
199cdf0e10cSrcweir         }
200cdf0e10cSrcweir         return true;
201cdf0e10cSrcweir     }
202cdf0e10cSrcweir }
203