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