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