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