1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package ifc.sheet;
29 
30 import lib.MultiMethodTest;
31 
32 import com.sun.star.sheet.CellDeleteMode;
33 import com.sun.star.sheet.CellInsertMode;
34 import com.sun.star.sheet.XCellRangeAddressable;
35 import com.sun.star.sheet.XCellRangeMovement;
36 import com.sun.star.sheet.XSpreadsheet;
37 import com.sun.star.table.CellAddress;
38 import com.sun.star.table.CellRangeAddress;
39 import com.sun.star.table.XColumnRowRange;
40 import com.sun.star.table.XTableRows;
41 import com.sun.star.uno.UnoRuntime;
42 
43 /**
44 * Testing <code>com.sun.star.sheet.XCellRangeMovement</code>
45 * interface methods :
46 * <ul>
47 *  <li><code> insertCells()</code></li>
48 *  <li><code> removeRange()</code></li>
49 *  <li><code> moveRange()</code></li>
50 *  <li><code> copyRange()</code></li>
51 * </ul> <p>
52 * Test object must implements interfaces <code>XCellRangeAddressable</code>
53 * and <code>XSpreadsheet</code> also. <p>
54 * @see com.sun.star.sheet.XCellRangeMovement
55 * @see com.sun.star.sheet.XSpreadsheet
56 * @see com.sun.star.sheet.XCellRangeAddressable
57 */
58 public class _XCellRangeMovement extends MultiMethodTest {
59 
60     public XCellRangeMovement oObj = null;
61 
62     /**
63     * Test sets specific values to cells in the range, copies this cell range
64     * to another position in document and checks cell's values in new position.<p>
65     * Has <b> OK </b> status if cell's values in source range are equal to
66     * cell's values in destination range and no exceptions were thrown. <p>
67     */
68     public void _copyRange(){
69         log.println("Prepare cells before test methods.");
70         XSpreadsheet oSheet = (XSpreadsheet)
71             UnoRuntime.queryInterface(XSpreadsheet.class, oObj);
72         try {
73             oSheet.getCellByPosition(1,1).setValue(100);
74             oSheet.getCellByPosition(1,2).setValue(200);
75             oSheet.getCellByPosition(2,1).setValue(300);
76             oSheet.getCellByPosition(2,2).setValue(400);
77         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
78             e.printStackTrace(log);
79             tRes.tested("copyRange()", false);
80         }
81 
82         XCellRangeAddressable oAddr =
83             (XCellRangeAddressable)
84                  UnoRuntime.queryInterface (XCellRangeAddressable.class, oObj);
85         short iSheet = oAddr.getRangeAddress().Sheet;
86         CellAddress sDest;
87         CellRangeAddress sSrc;
88 
89         sSrc = new CellRangeAddress(iSheet, 1, 1, 2, 2);
90         sDest = new CellAddress(iSheet, 1, 10);
91         boolean result = true;
92         boolean loc_result = true;
93 
94         oObj.copyRange(sDest, sSrc);
95         try {
96             loc_result  = (oSheet.getCellByPosition(1, 10).getValue() == 100);
97             loc_result &= (oSheet.getCellByPosition(1, 11).getValue() == 200);
98             loc_result &= (oSheet.getCellByPosition(2, 10).getValue() == 300);
99             loc_result &= (oSheet.getCellByPosition(2, 11).getValue() == 400);
100         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
101             e.printStackTrace(log);
102             tRes.tested("copyRange()", false);
103         }
104 
105         result &= loc_result;
106         tRes.tested("copyRange()", result);
107     }
108 
109     /**
110     * Test sets specific values to cells in the two contiguous rows, inserts
111     * new empty row between them and checks value in
112     * one cell of the inserted row. <p>
113     * Has <b> OK </b> status if value of cell in the inserted row is zero
114     * and no exceptions were thrown. <p>
115     */
116     public void _insertCells(){
117         boolean result = false;
118 
119         XSpreadsheet oSheet = (XSpreadsheet)
120             UnoRuntime.queryInterface(XSpreadsheet.class, oObj);
121         XCellRangeAddressable oAddr = (XCellRangeAddressable)
122             UnoRuntime.queryInterface (XCellRangeAddressable.class, oObj);
123         short iSheet = oAddr.getRangeAddress().Sheet;
124         try {
125             oSheet.getCellByPosition(0,20).setValue(100);
126             oSheet.getCellByPosition(1,20).setValue(100);
127             oSheet.getCellByPosition(2,20).setValue(100);
128             oSheet.getCellByPosition(3,20).setValue(100);
129             oSheet.getCellByPosition(0,21).setValue(200);
130             oSheet.getCellByPosition(1,21).setValue(200);
131             oSheet.getCellByPosition(2,21).setValue(200);
132             oSheet.getCellByPosition(3,21).setValue(200);
133 
134             // catch some sleight of hand threads
135             if (oSheet.getCellByPosition(1,21).getValue() == 200){
136                 //log.println("Rows closed.");
137             }
138             else{
139                 log.println("Cells were already inserted. "+
140                     "Delete old cells now");
141                 XColumnRowRange oColumnRowRange = (XColumnRowRange)
142                     UnoRuntime.queryInterface(XColumnRowRange.class, oSheet);
143 
144                 XTableRows oRows = (XTableRows) oColumnRowRange.getRows();
145                 oRows.removeByIndex(21,1);
146             }
147             CellRangeAddress sSrc = new CellRangeAddress(iSheet, 0, 21, 5, 21);
148             oObj.insertCells (sSrc, CellInsertMode.DOWN) ;
149 
150             // check the result
151             double res = oSheet.getCellByPosition(1, 21).getValue();
152             if (res == 0.0) {
153                 result = true;
154             }
155         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
156             e.printStackTrace(log);
157             result = false;
158         }
159 
160         tRes.tested("insertCells()", result);
161     }
162 
163     /**
164     * Test sets specific values to cells in the range, moves this cell range
165     * to another position in document and checks cell's values in new position.
166     * <p>Has <b>OK</b> status if sum of values in source range is equal to sum
167     * of values in destination range and no exceptions were thrown. <p>
168     */
169     public void _moveRange(){
170         boolean result = false;
171 
172         XSpreadsheet oSheet = (XSpreadsheet)
173             UnoRuntime.queryInterface(XSpreadsheet.class, oObj);
174 
175         XCellRangeAddressable oAddr = (XCellRangeAddressable)
176             UnoRuntime.queryInterface (XCellRangeAddressable.class, oObj);
177 
178         short iSheet = oAddr.getRangeAddress().Sheet;
179         //prepare source range
180         try {
181             oSheet.getCellByPosition(4,0).setValue(111);
182             oSheet.getCellByPosition(4,1).setValue(222);
183 
184             CellRangeAddress sSrc = new CellRangeAddress(iSheet, 4, 0, 4, 1);
185             CellAddress sDest = new CellAddress(iSheet, 4, 4);
186             oObj.moveRange(sDest, sSrc);
187 
188             double cntA = 0;
189             double cntB = 0;
190             cntA = oSheet.getCellByPosition(4, 4).getValue();
191             cntB = oSheet.getCellByPosition(4, 5).getValue();
192             if (cntA + cntB == 333.0){ result = true; }
193             //clean up
194             oSheet.getCellByPosition(4,4).setValue(0);
195             oSheet.getCellByPosition(4,5).setValue(0);
196         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
197             e.printStackTrace(log);
198             result = false;
199         }
200 
201         tRes.tested("moveRange()", result);
202     }
203 
204 
205     /**
206     * Test sets specific values to cells in the range, deletes this cell range
207     * from document and checks values of cells in position
208     * of the deleted range.
209     * <p>Has <b>OK</b> status if sum of cell values in position of the deleted
210     * range is equal to zero and no exceptions were thrown. <p>
211     */
212     public void _removeRange(){
213         boolean result = false;
214 
215         XSpreadsheet oSheet = (XSpreadsheet)
216             UnoRuntime.queryInterface(XSpreadsheet.class, oObj);
217         XCellRangeAddressable oAddr = (XCellRangeAddressable)
218             UnoRuntime.queryInterface (XCellRangeAddressable.class, oObj);
219         short iSheet = oAddr.getRangeAddress().Sheet;
220         try {
221             //prepare source range
222             oSheet.getCellByPosition(5, 0).setValue(333);
223             oSheet.getCellByPosition(5, 1).setValue(444);
224 
225             CellRangeAddress sSrc = new CellRangeAddress(iSheet, 5, 0, 5, 1);
226             oObj.removeRange(sSrc, CellDeleteMode.UP);
227 
228             double cntA = 0;
229             double cntB = 0;
230             cntA = oSheet.getCellByPosition(5, 0).getValue();
231             cntB = oSheet.getCellByPosition(5, 1).getValue();
232             if (cntA + cntB == 0.0){ result = true; }
233 
234             //clean up
235             oSheet.getCellByPosition(5, 0).setValue(0);
236             oSheet.getCellByPosition(5, 1).setValue(0);
237         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
238             e.printStackTrace(log);
239             result = false;
240         }
241 
242         tRes.tested("removeRange()", result);
243     }
244 
245 } // EOC _XCellRangeMovement
246 
247