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