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 10cdf0e10cSrcweir * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 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. 19cdf0e10cSrcweir * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package ifc.sheet; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import lib.MultiMethodTest; 27cdf0e10cSrcweir import lib.Status; 28cdf0e10cSrcweir import lib.StatusException; 29cdf0e10cSrcweir 30cdf0e10cSrcweir import com.sun.star.sheet.XArrayFormulaRange; 31cdf0e10cSrcweir import com.sun.star.sheet.XCellRangeAddressable; 32cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet; 33cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress; 34cdf0e10cSrcweir import com.sun.star.table.XCell; 35cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 36cdf0e10cSrcweir 37cdf0e10cSrcweir /** 38cdf0e10cSrcweir * Testing <code>com.sun.star.sheet.XArrayFormulaRange</code> 39cdf0e10cSrcweir * interface methods : 40cdf0e10cSrcweir * <ul> 41cdf0e10cSrcweir * <li><code> getArrayFormula()</code></li> 42cdf0e10cSrcweir * <li><code> setArrayFormula()</code></li> 43cdf0e10cSrcweir * </ul> <p> 44cdf0e10cSrcweir * This test needs the following object relations : 45cdf0e10cSrcweir * <ul> 46cdf0e10cSrcweir * <li> <code>'SHEET'</code> (of type <code>XSpreadsheet</code>): 47cdf0e10cSrcweir * to check contents of spreadsheet </li> 48cdf0e10cSrcweir * <li> <code>'noArray'</code> (of type <code>Object</code>): 49cdf0e10cSrcweir * if the relation is null then given component doesn't really support 50cdf0e10cSrcweir * this interface </li> 51cdf0e10cSrcweir * <ul> <p> 52cdf0e10cSrcweir * Test object must implements interface <code>XCellRangeAddressable</code> also. 53cdf0e10cSrcweir * @see com.sun.star.sheet.XArrayFormulaRange 54cdf0e10cSrcweir * @see com.sun.star.sheet.XSpreadsheet 55cdf0e10cSrcweir * @see com.sun.star.sheet.XCellRangeAddressable 56cdf0e10cSrcweir */ 57cdf0e10cSrcweir public class _XArrayFormulaRange extends MultiMethodTest { 58cdf0e10cSrcweir 59cdf0e10cSrcweir public XArrayFormulaRange oObj = null; 60cdf0e10cSrcweir String formula = "=1 + 2 * 5"; 61cdf0e10cSrcweir 62cdf0e10cSrcweir /** 63cdf0e10cSrcweir * Test calls the method and then checks content sof spreadsheet using 64cdf0e10cSrcweir * object relation <code>'SHEET'</code>. <p> 65cdf0e10cSrcweir * Has <b> OK </b> status if values in cells of spreadsheet are equal to 11 66cdf0e10cSrcweir * or ArrayFormula not supported.<p> 67cdf0e10cSrcweir */ _setArrayFormula()68cdf0e10cSrcweir public void _setArrayFormula() { 69cdf0e10cSrcweir Object noArray = tEnv.getObjRelation("noArray"); 70cdf0e10cSrcweir if (noArray != null) { 71cdf0e10cSrcweir log.println("Component " + noArray.toString() + 72cdf0e10cSrcweir " doesn't really support this Interface"); 73cdf0e10cSrcweir log.println("It doesn't make sense to set an ArrayFormula over" 74cdf0e10cSrcweir + " the whole sheet"); 75cdf0e10cSrcweir tRes.tested("setArrayFormula()", true); 76cdf0e10cSrcweir return; 77cdf0e10cSrcweir } 78cdf0e10cSrcweir 79cdf0e10cSrcweir boolean result = true; 80cdf0e10cSrcweir double dresult = 11; 81cdf0e10cSrcweir 82cdf0e10cSrcweir log.println("setArrayFormula() ..."); 83cdf0e10cSrcweir 84cdf0e10cSrcweir oObj.setArrayFormula(formula); 85cdf0e10cSrcweir 86cdf0e10cSrcweir log.println("checking that formula was set correctly..."); 87cdf0e10cSrcweir XCellRangeAddressable crAddr = 88cdf0e10cSrcweir (XCellRangeAddressable) 89cdf0e10cSrcweir UnoRuntime.queryInterface(XCellRangeAddressable.class, oObj); 90cdf0e10cSrcweir CellRangeAddress addr = crAddr.getRangeAddress() ; 91cdf0e10cSrcweir XSpreadsheet oSheet = (XSpreadsheet)tEnv.getObjRelation("SHEET"); 92cdf0e10cSrcweir if (oSheet == null) throw new StatusException(Status.failed 93cdf0e10cSrcweir ("Relation 'SHEET' not found")); 94cdf0e10cSrcweir 95cdf0e10cSrcweir XCell oCell = null; 96cdf0e10cSrcweir double value; 97cdf0e10cSrcweir 98cdf0e10cSrcweir for (int i = addr.StartColumn; i <= addr.EndColumn; i++) 99cdf0e10cSrcweir for (int j = addr.StartRow; j <= addr.EndRow; j++) { 100cdf0e10cSrcweir try { 101cdf0e10cSrcweir oCell = oSheet.getCellByPosition(i, j); 102cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 103cdf0e10cSrcweir e.printStackTrace(log); 104cdf0e10cSrcweir result = false; 105cdf0e10cSrcweir break; 106cdf0e10cSrcweir } 107cdf0e10cSrcweir 108cdf0e10cSrcweir value = oCell.getValue(); 109cdf0e10cSrcweir result &= (value == dresult); 110cdf0e10cSrcweir } 111cdf0e10cSrcweir 112cdf0e10cSrcweir tRes.tested("setArrayFormula()", result) ; 113cdf0e10cSrcweir 114cdf0e10cSrcweir } // end setArrayFormula() 115cdf0e10cSrcweir 116cdf0e10cSrcweir /** 117cdf0e10cSrcweir * Test calls the method and compare formula that set by method 118cdf0e10cSrcweir * <code>setArrayFormula</code> with returned value ignoring spaces. <p> 119cdf0e10cSrcweir * 120cdf0e10cSrcweir * Has <b> OK </b> status if values are equal or 121cdf0e10cSrcweir * ArrayFormula not supported. <p> 122cdf0e10cSrcweir * 123cdf0e10cSrcweir * The following method tests are to be completed successfully before : 124cdf0e10cSrcweir * <ul> 125cdf0e10cSrcweir * <li> <code> setArrayFormula </code> : to set formula </li> 126cdf0e10cSrcweir * </ul> 127cdf0e10cSrcweir */ _getArrayFormula()128cdf0e10cSrcweir public void _getArrayFormula() { 129cdf0e10cSrcweir 130cdf0e10cSrcweir Object noArray = tEnv.getObjRelation("noArray"); 131cdf0e10cSrcweir if (noArray != null) { 132cdf0e10cSrcweir log.println("Component "+noArray.toString()+" doesn't really support this Interface"); 133cdf0e10cSrcweir log.println("It doesn't make sense to set an ArrayFormula over the whole sheet"); 134cdf0e10cSrcweir log.println("and therefore 'getArrayFormula()' won't work"); 135cdf0e10cSrcweir tRes.tested("getArrayFormula()",true); 136cdf0e10cSrcweir return; 137cdf0e10cSrcweir } 138cdf0e10cSrcweir 139cdf0e10cSrcweir requiredMethod("setArrayFormula()"); 140cdf0e10cSrcweir boolean result = true; 141cdf0e10cSrcweir log.println("Testing getArrayFormula() ..."); 142cdf0e10cSrcweir String gFormula = oObj.getArrayFormula() ; 143cdf0e10cSrcweir result &= equalIgnoreSpaces("{" + formula + "}", gFormula); 144cdf0e10cSrcweir if (!result) 145cdf0e10cSrcweir log.println("Method returned : '" + oObj.getArrayFormula() + "'") ; 146cdf0e10cSrcweir tRes.tested("getArrayFormula()", result) ; 147cdf0e10cSrcweir 148cdf0e10cSrcweir } // end getArrayFormula() 149cdf0e10cSrcweir 150cdf0e10cSrcweir /** 151cdf0e10cSrcweir * Method compares two string ignoring spaces. 152cdf0e10cSrcweir * @return <code>true</code> if the argument 153cdf0e10cSrcweir * is not null and the Strings are equal, 154cdf0e10cSrcweir * ignoring spaces; <code>false</code> otherwise. 155cdf0e10cSrcweir */ equalIgnoreSpaces(String s1, String s2)156cdf0e10cSrcweir private boolean equalIgnoreSpaces(String s1, String s2) { 157cdf0e10cSrcweir int p1 = 0, p2 = 0 ; 158cdf0e10cSrcweir s1 = s1.trim() ; 159cdf0e10cSrcweir s2 = s2.trim() ; 160cdf0e10cSrcweir while (p1 < s1.length() && p2 < s2.length()) { 161cdf0e10cSrcweir while (s1.charAt(p1) == ' ') p1 ++ ; 162cdf0e10cSrcweir while (s2.charAt(p2) == ' ') p2 ++ ; 163cdf0e10cSrcweir if (s1.charAt(p1) != s2.charAt(p2)) return false ; 164cdf0e10cSrcweir p1 ++ ; 165cdf0e10cSrcweir p2 ++ ; 166cdf0e10cSrcweir } 167cdf0e10cSrcweir 168cdf0e10cSrcweir return p1 == s1.length() && p2 == s2.length() ; 169cdf0e10cSrcweir } 170cdf0e10cSrcweir 171cdf0e10cSrcweir /** 172cdf0e10cSrcweir * Forces environment recreation. 173cdf0e10cSrcweir */ after()174cdf0e10cSrcweir protected void after() { 175cdf0e10cSrcweir disposeEnvironment(); 176cdf0e10cSrcweir } 177cdf0e10cSrcweir } 178cdf0e10cSrcweir 179