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