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 28*cdf0e10cSrcweir package ifc.sheet; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import lib.MultiMethodTest; 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir import com.sun.star.sheet.CellDeleteMode; 33*cdf0e10cSrcweir import com.sun.star.sheet.CellInsertMode; 34*cdf0e10cSrcweir import com.sun.star.sheet.XCellRangeAddressable; 35*cdf0e10cSrcweir import com.sun.star.sheet.XCellRangeMovement; 36*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet; 37*cdf0e10cSrcweir import com.sun.star.table.CellAddress; 38*cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress; 39*cdf0e10cSrcweir import com.sun.star.table.XColumnRowRange; 40*cdf0e10cSrcweir import com.sun.star.table.XTableRows; 41*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir /** 44*cdf0e10cSrcweir * Testing <code>com.sun.star.sheet.XCellRangeMovement</code> 45*cdf0e10cSrcweir * interface methods : 46*cdf0e10cSrcweir * <ul> 47*cdf0e10cSrcweir * <li><code> insertCells()</code></li> 48*cdf0e10cSrcweir * <li><code> removeRange()</code></li> 49*cdf0e10cSrcweir * <li><code> moveRange()</code></li> 50*cdf0e10cSrcweir * <li><code> copyRange()</code></li> 51*cdf0e10cSrcweir * </ul> <p> 52*cdf0e10cSrcweir * Test object must implements interfaces <code>XCellRangeAddressable</code> 53*cdf0e10cSrcweir * and <code>XSpreadsheet</code> also. <p> 54*cdf0e10cSrcweir * @see com.sun.star.sheet.XCellRangeMovement 55*cdf0e10cSrcweir * @see com.sun.star.sheet.XSpreadsheet 56*cdf0e10cSrcweir * @see com.sun.star.sheet.XCellRangeAddressable 57*cdf0e10cSrcweir */ 58*cdf0e10cSrcweir public class _XCellRangeMovement extends MultiMethodTest { 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir public XCellRangeMovement oObj = null; 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir /** 63*cdf0e10cSrcweir * Test sets specific values to cells in the range, copies this cell range 64*cdf0e10cSrcweir * to another position in document and checks cell's values in new position.<p> 65*cdf0e10cSrcweir * Has <b> OK </b> status if cell's values in source range are equal to 66*cdf0e10cSrcweir * cell's values in destination range and no exceptions were thrown. <p> 67*cdf0e10cSrcweir */ 68*cdf0e10cSrcweir public void _copyRange(){ 69*cdf0e10cSrcweir log.println("Prepare cells before test methods."); 70*cdf0e10cSrcweir XSpreadsheet oSheet = (XSpreadsheet) 71*cdf0e10cSrcweir UnoRuntime.queryInterface(XSpreadsheet.class, oObj); 72*cdf0e10cSrcweir try { 73*cdf0e10cSrcweir oSheet.getCellByPosition(1,1).setValue(100); 74*cdf0e10cSrcweir oSheet.getCellByPosition(1,2).setValue(200); 75*cdf0e10cSrcweir oSheet.getCellByPosition(2,1).setValue(300); 76*cdf0e10cSrcweir oSheet.getCellByPosition(2,2).setValue(400); 77*cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 78*cdf0e10cSrcweir e.printStackTrace(log); 79*cdf0e10cSrcweir tRes.tested("copyRange()", false); 80*cdf0e10cSrcweir } 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir XCellRangeAddressable oAddr = 83*cdf0e10cSrcweir (XCellRangeAddressable) 84*cdf0e10cSrcweir UnoRuntime.queryInterface (XCellRangeAddressable.class, oObj); 85*cdf0e10cSrcweir short iSheet = oAddr.getRangeAddress().Sheet; 86*cdf0e10cSrcweir CellAddress sDest; 87*cdf0e10cSrcweir CellRangeAddress sSrc; 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir sSrc = new CellRangeAddress(iSheet, 1, 1, 2, 2); 90*cdf0e10cSrcweir sDest = new CellAddress(iSheet, 1, 10); 91*cdf0e10cSrcweir boolean result = true; 92*cdf0e10cSrcweir boolean loc_result = true; 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir oObj.copyRange(sDest, sSrc); 95*cdf0e10cSrcweir try { 96*cdf0e10cSrcweir loc_result = (oSheet.getCellByPosition(1, 10).getValue() == 100); 97*cdf0e10cSrcweir loc_result &= (oSheet.getCellByPosition(1, 11).getValue() == 200); 98*cdf0e10cSrcweir loc_result &= (oSheet.getCellByPosition(2, 10).getValue() == 300); 99*cdf0e10cSrcweir loc_result &= (oSheet.getCellByPosition(2, 11).getValue() == 400); 100*cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 101*cdf0e10cSrcweir e.printStackTrace(log); 102*cdf0e10cSrcweir tRes.tested("copyRange()", false); 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir result &= loc_result; 106*cdf0e10cSrcweir tRes.tested("copyRange()", result); 107*cdf0e10cSrcweir } 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir /** 110*cdf0e10cSrcweir * Test sets specific values to cells in the two contiguous rows, inserts 111*cdf0e10cSrcweir * new empty row between them and checks value in 112*cdf0e10cSrcweir * one cell of the inserted row. <p> 113*cdf0e10cSrcweir * Has <b> OK </b> status if value of cell in the inserted row is zero 114*cdf0e10cSrcweir * and no exceptions were thrown. <p> 115*cdf0e10cSrcweir */ 116*cdf0e10cSrcweir public void _insertCells(){ 117*cdf0e10cSrcweir boolean result = false; 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir XSpreadsheet oSheet = (XSpreadsheet) 120*cdf0e10cSrcweir UnoRuntime.queryInterface(XSpreadsheet.class, oObj); 121*cdf0e10cSrcweir XCellRangeAddressable oAddr = (XCellRangeAddressable) 122*cdf0e10cSrcweir UnoRuntime.queryInterface (XCellRangeAddressable.class, oObj); 123*cdf0e10cSrcweir short iSheet = oAddr.getRangeAddress().Sheet; 124*cdf0e10cSrcweir try { 125*cdf0e10cSrcweir oSheet.getCellByPosition(0,20).setValue(100); 126*cdf0e10cSrcweir oSheet.getCellByPosition(1,20).setValue(100); 127*cdf0e10cSrcweir oSheet.getCellByPosition(2,20).setValue(100); 128*cdf0e10cSrcweir oSheet.getCellByPosition(3,20).setValue(100); 129*cdf0e10cSrcweir oSheet.getCellByPosition(0,21).setValue(200); 130*cdf0e10cSrcweir oSheet.getCellByPosition(1,21).setValue(200); 131*cdf0e10cSrcweir oSheet.getCellByPosition(2,21).setValue(200); 132*cdf0e10cSrcweir oSheet.getCellByPosition(3,21).setValue(200); 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir // catch some sleight of hand threads 135*cdf0e10cSrcweir if (oSheet.getCellByPosition(1,21).getValue() == 200){ 136*cdf0e10cSrcweir //log.println("Rows closed."); 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir else{ 139*cdf0e10cSrcweir log.println("Cells were already inserted. "+ 140*cdf0e10cSrcweir "Delete old cells now"); 141*cdf0e10cSrcweir XColumnRowRange oColumnRowRange = (XColumnRowRange) 142*cdf0e10cSrcweir UnoRuntime.queryInterface(XColumnRowRange.class, oSheet); 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir XTableRows oRows = (XTableRows) oColumnRowRange.getRows(); 145*cdf0e10cSrcweir oRows.removeByIndex(21,1); 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir CellRangeAddress sSrc = new CellRangeAddress(iSheet, 0, 21, 5, 21); 148*cdf0e10cSrcweir oObj.insertCells (sSrc, CellInsertMode.DOWN) ; 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir // check the result 151*cdf0e10cSrcweir double res = oSheet.getCellByPosition(1, 21).getValue(); 152*cdf0e10cSrcweir if (res == 0.0) { 153*cdf0e10cSrcweir result = true; 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 156*cdf0e10cSrcweir e.printStackTrace(log); 157*cdf0e10cSrcweir result = false; 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir tRes.tested("insertCells()", result); 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir /** 164*cdf0e10cSrcweir * Test sets specific values to cells in the range, moves this cell range 165*cdf0e10cSrcweir * to another position in document and checks cell's values in new position. 166*cdf0e10cSrcweir * <p>Has <b>OK</b> status if sum of values in source range is equal to sum 167*cdf0e10cSrcweir * of values in destination range and no exceptions were thrown. <p> 168*cdf0e10cSrcweir */ 169*cdf0e10cSrcweir public void _moveRange(){ 170*cdf0e10cSrcweir boolean result = false; 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir XSpreadsheet oSheet = (XSpreadsheet) 173*cdf0e10cSrcweir UnoRuntime.queryInterface(XSpreadsheet.class, oObj); 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir XCellRangeAddressable oAddr = (XCellRangeAddressable) 176*cdf0e10cSrcweir UnoRuntime.queryInterface (XCellRangeAddressable.class, oObj); 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir short iSheet = oAddr.getRangeAddress().Sheet; 179*cdf0e10cSrcweir //prepare source range 180*cdf0e10cSrcweir try { 181*cdf0e10cSrcweir oSheet.getCellByPosition(4,0).setValue(111); 182*cdf0e10cSrcweir oSheet.getCellByPosition(4,1).setValue(222); 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir CellRangeAddress sSrc = new CellRangeAddress(iSheet, 4, 0, 4, 1); 185*cdf0e10cSrcweir CellAddress sDest = new CellAddress(iSheet, 4, 4); 186*cdf0e10cSrcweir oObj.moveRange(sDest, sSrc); 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir double cntA = 0; 189*cdf0e10cSrcweir double cntB = 0; 190*cdf0e10cSrcweir cntA = oSheet.getCellByPosition(4, 4).getValue(); 191*cdf0e10cSrcweir cntB = oSheet.getCellByPosition(4, 5).getValue(); 192*cdf0e10cSrcweir if (cntA + cntB == 333.0){ result = true; } 193*cdf0e10cSrcweir //clean up 194*cdf0e10cSrcweir oSheet.getCellByPosition(4,4).setValue(0); 195*cdf0e10cSrcweir oSheet.getCellByPosition(4,5).setValue(0); 196*cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 197*cdf0e10cSrcweir e.printStackTrace(log); 198*cdf0e10cSrcweir result = false; 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir tRes.tested("moveRange()", result); 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir /** 206*cdf0e10cSrcweir * Test sets specific values to cells in the range, deletes this cell range 207*cdf0e10cSrcweir * from document and checks values of cells in position 208*cdf0e10cSrcweir * of the deleted range. 209*cdf0e10cSrcweir * <p>Has <b>OK</b> status if sum of cell values in position of the deleted 210*cdf0e10cSrcweir * range is equal to zero and no exceptions were thrown. <p> 211*cdf0e10cSrcweir */ 212*cdf0e10cSrcweir public void _removeRange(){ 213*cdf0e10cSrcweir boolean result = false; 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir XSpreadsheet oSheet = (XSpreadsheet) 216*cdf0e10cSrcweir UnoRuntime.queryInterface(XSpreadsheet.class, oObj); 217*cdf0e10cSrcweir XCellRangeAddressable oAddr = (XCellRangeAddressable) 218*cdf0e10cSrcweir UnoRuntime.queryInterface (XCellRangeAddressable.class, oObj); 219*cdf0e10cSrcweir short iSheet = oAddr.getRangeAddress().Sheet; 220*cdf0e10cSrcweir try { 221*cdf0e10cSrcweir //prepare source range 222*cdf0e10cSrcweir oSheet.getCellByPosition(5, 0).setValue(333); 223*cdf0e10cSrcweir oSheet.getCellByPosition(5, 1).setValue(444); 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir CellRangeAddress sSrc = new CellRangeAddress(iSheet, 5, 0, 5, 1); 226*cdf0e10cSrcweir oObj.removeRange(sSrc, CellDeleteMode.UP); 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir double cntA = 0; 229*cdf0e10cSrcweir double cntB = 0; 230*cdf0e10cSrcweir cntA = oSheet.getCellByPosition(5, 0).getValue(); 231*cdf0e10cSrcweir cntB = oSheet.getCellByPosition(5, 1).getValue(); 232*cdf0e10cSrcweir if (cntA + cntB == 0.0){ result = true; } 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir //clean up 235*cdf0e10cSrcweir oSheet.getCellByPosition(5, 0).setValue(0); 236*cdf0e10cSrcweir oSheet.getCellByPosition(5, 1).setValue(0); 237*cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 238*cdf0e10cSrcweir e.printStackTrace(log); 239*cdf0e10cSrcweir result = false; 240*cdf0e10cSrcweir } 241*cdf0e10cSrcweir 242*cdf0e10cSrcweir tRes.tested("removeRange()", result); 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir } // EOC _XCellRangeMovement 246*cdf0e10cSrcweir 247