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