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.table;
25 
26 import lib.MultiMethodTest;
27 import lib.Status;
28 import lib.StatusException;
29 
30 import com.sun.star.table.XCell;
31 import com.sun.star.table.XCellRange;
32 import com.sun.star.table.XTableColumns;
33 import com.sun.star.text.XSimpleText;
34 import com.sun.star.uno.UnoRuntime;
35 
36 /**
37 * Testing <code>com.sun.star.table.XTableColumns</code>
38 * interface methods :
39 * <ul>
40 *  <li><code> insertByIndex()</code></li>
41 *  <li><code> removeByIndex()</code></li>
42 * </ul> <p>
43 *
44 * This test needs the following object relations :
45 * <ul>
46 *  <li> <code>'XTableColumns.XCellRange'</code> : <code>
47 *    com.sun.star.table.XCellRange</code> the cell range of
48 *    columns.</li>
49 * <ul> <p>
50 *
51 * Test is multithread compilant. <p>
52 * @see com.sun.star.table.XTableColumns
53 */
54 public class _XTableColumns extends MultiMethodTest {
55 
56     public XTableColumns oObj = null;
57     private XCellRange xCellRange = null;
58     private int lastColumn = 0;
59 
before()60     public void before() {
61         xCellRange = (XCellRange)
62             tEnv.getObjRelation("XTableColumns.XCellRange") ;
63 
64         if (xCellRange == null) throw new
65             StatusException(Status.failed("Relation missing"));
66 
67         lastColumn = oObj.getCount() - 1 ;
68     }
69 
70     /**
71      * First a number of cells in cell range are filled with data.
72      *
73      * Then columns inserted to valid positions : 1 column at 1,
74      * 1 column at 0, 2 columns at 0. <p>
75      *
76      * Then columns inserted to invalid positions : position -1,
77      * the column after last, and 0 columns inserted. <p>
78      *
79      * Has <b> OK </b> status if for valid cases :
80      * <ul>
81      *  <li> content of other cells are properly shifted </li>
82      *  <li> inserted columns are empty </li>
83      *  <li> number of columns increases (in case if it is not the whole
84      *      spreadsheet) by proper number. </li>
85      * </ul>
86      * and for invalid cases exception is thrown.
87      */
_insertByIndex()88     public void _insertByIndex() {
89 
90         boolean result = true;
91         int origCnt = oObj.getCount();
92 
93         try {
94             log.println("Filling range ... ");
95             fillRange(xCellRange);
96 
97             log.println("Inserting 1 column at position 1 ...");
98             oObj.insertByIndex(1,1);
99 
100             result &= checkColumn(0, 0);
101             result &= checkColumnEmpty(1);
102             result &= checkColumn(2, 1);
103             result &= checkColumn(3, 2);
104             result &= checkColumnEmpty(4);
105 
106             if (lastColumn < 200) {
107                 result &= checkColumn(lastColumn + 1, lastColumn);
108                 result &= oObj.getCount() == origCnt + 1;
109             } else {
110                 result &= checkColumnEmpty(lastColumn);
111             }
112 
113             log.println("Inserting 1 column at position 0 ...");
114             oObj.insertByIndex(0,1);
115 
116             result &= checkColumnEmpty(0);
117             result &= checkColumn(1, 0);
118             result &= checkColumnEmpty(2);
119             result &= checkColumn(3, 1);
120             result &= checkColumn(4, 2);
121             result &= checkColumnEmpty(5);
122             if (lastColumn < 200) {
123                 result &= checkColumn(lastColumn + 2, lastColumn);
124                 result &= oObj.getCount() == origCnt + 2;
125             }
126 
127             log.println("Inserting 2 columns at position 0 ...");
128             oObj.insertByIndex(0,2);
129 
130             result &= checkColumnEmpty(0);
131             result &= checkColumnEmpty(1);
132             result &= checkColumnEmpty(2);
133             result &= checkColumn(3, 0);
134             result &= checkColumnEmpty(4);
135             result &= checkColumn(5, 1);
136             result &= checkColumn(6, 2);
137             result &= checkColumnEmpty(7);
138             if (lastColumn < 200) {
139                 result &= checkColumn(lastColumn + 4, lastColumn);
140             }
141 
142         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
143             e.printStackTrace(log);
144             result = false;
145         }
146 
147 
148         // spreadsheet supports 256 columns and after inserting
149         // or removing a column their number remains the same
150         if (origCnt < 200) {
151             log.println("Checking that number of column increased.");
152             result &= oObj.getCount() == origCnt + 4;
153             log.println("Before: " + origCnt + ", After: " + oObj.getCount());
154         } else {
155             log.println("Number of columns is " + origCnt + ",") ;
156             log.println("supposing that this is the whole spreadsheet and ");
157             log.println("number of columns should not change.");
158         }
159 
160         try {
161             oObj.insertByIndex(-1,1);
162             log.println("No Exception occurred while inserting column at -1");
163             result &= false;
164         } catch (Exception e) {
165             log.println("Inserting column at Index -1 ... OK");
166             result &= true;
167         }
168 
169         int cnt = oObj.getCount();
170         try {
171             oObj.insertByIndex(cnt, 1);
172             log.println("No Exception occurred while inserting column at "
173                 + cnt);
174             result &= false;
175         } catch (Exception e) {
176             log.println("Inserting column at Index " + cnt + " ... OK");
177             result &= true;
178         }
179 
180         if (tEnv.getTestCase().getObjectName().equals("ScTableColumnsObj")) {
181 
182             try {
183                 oObj.insertByIndex(0,0);
184                 log.println("No Exception occurred while inserting 0 columns");
185                 result &= false;
186             } catch (Exception e) {
187                 log.println("Inserting 0 columns ... OK");
188                 result &= true;
189             }
190 
191         }
192 
193         tRes.tested( "insertByIndex()", result );
194 
195     } // end insertByIndex()
196 
197     /**
198      * Columns removed from valid positions : 1 column at 1,
199      * 1 column at 0, 2 columns at 0. <p>
200      *
201      * Then columns removed from invalid positions : position -1,
202      * the column after last, and 0 columns removed. <p>
203      *
204      * Has <b> OK </b> status if for valid cases :
205      * <ul>
206      *  <li> content of other cells are properly shifted </li>
207      *  <li> columns which are shifted left are empty </li>
208      *  <li> number of columns decreases (in case if it is not the whole
209      *      spreadsheet) by proper number. </li>
210      * </ul>
211      * and for invalid cases exception is thrown.
212      */
_removeByIndex()213     public void _removeByIndex() {
214         executeMethod("insertByIndex()");
215 
216         boolean result = true;
217         int origCnt = oObj.getCount();
218 
219         try {
220             log.println("Filling range ... ");
221 
222             log.println("Removing 2 columns at position 0 ...");
223             oObj.removeByIndex(0,2);
224 
225             result &= checkColumnEmpty(0);
226             result &= checkColumn(1, 0);
227             result &= checkColumnEmpty(2);
228             result &= checkColumn(3, 1);
229             result &= checkColumn(4, 2);
230             result &= checkColumnEmpty(5);
231             if (lastColumn < 200) {
232                 result &= checkColumn(lastColumn + 2, lastColumn);
233                 result &= oObj.getCount() == origCnt - 2;
234             }
235 
236             log.println("Removing 1 column at position 0 ...");
237             oObj.removeByIndex(0,1);
238 
239             result &= checkColumn(0, 0);
240             result &= checkColumnEmpty(1);
241             result &= checkColumn(2, 1);
242             result &= checkColumn(3, 2);
243             result &= checkColumnEmpty(4);
244             if (lastColumn < 200) {
245                 result &= checkColumn(lastColumn + 1, lastColumn);
246                 result &= oObj.getCount() == origCnt - 3;
247             }
248 
249             log.println("Removing 1 column at position 1 ...");
250             oObj.removeByIndex(1,1);
251 
252             result &= checkColumn(0, 0);
253             result &= checkColumn(1, 1);
254             result &= checkColumn(2, 2);
255             result &= checkColumnEmpty(3);
256             if (lastColumn < 200) {
257                 result &= checkColumn(lastColumn, lastColumn);
258             }
259 
260         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
261             e.printStackTrace(log);
262             result = false;
263         }
264 
265 
266         // spreadsheet supports 256 columns and after inserting
267         // or removing a column their number remains the same
268         if (origCnt < 200) {
269             log.println("Checking that number of column increased.");
270             result &= oObj.getCount() == origCnt - 4;
271             log.println("Before: " + origCnt + ", After: " + oObj.getCount());
272         } else {
273             log.println("Number of columns is " + origCnt + ",") ;
274             log.println("supposing that this is the whole spreadsheet and ");
275             log.println("number of columns should not change.");
276         }
277 
278         try {
279             oObj.removeByIndex(-1,1);
280             log.println("No Exception occurred while removing column at -1");
281             result &= false;
282         } catch (Exception e) {
283             log.println("removing column at Index -1 ... OK");
284             result &= true;
285         }
286 
287         int cnt = oObj.getCount();
288         try {
289             oObj.removeByIndex(cnt, 1);
290             log.println("No Exception occurred while removing column at "
291                 + cnt);
292             result &= false;
293         } catch (Exception e) {
294             log.println("Removing column at Index " + cnt + " ... OK");
295             result &= true;
296         }
297 
298         if (tEnv.getTestCase().getObjectName().equals("ScTableColumnsObj")) {
299             try {
300                 oObj.removeByIndex(0,0);
301                 log.println("No Exception occurred while removing 0 columns");
302                 result &= false;
303             } catch (Exception e) {
304                 log.println("removing 0 columns ... OK");
305                 result &= true;
306             }
307         }
308 
309         tRes.tested( "removeByIndex()", result );
310     } // end removeByIndex()
311 
setCellText(XCell cell, String text)312     private void setCellText(XCell cell, String text) {
313         XSimpleText xText = (XSimpleText) UnoRuntime.queryInterface
314             (XSimpleText.class, cell) ;
315         xText.setString(text);
316     }
getCellText(XCell cell)317     private String getCellText(XCell cell) {
318         XSimpleText xText = (XSimpleText) UnoRuntime.queryInterface
319             (XSimpleText.class, cell) ;
320         return xText.getString();
321     }
322 
323     /**
324      * Fills the range with some data : two rows and 3 columns, and
325      * some columns are cleared.
326      *
327      * @param xRange Range to fill
328      * @throws IndexOutOfBoundsException if any errors occur during filling.
329      */
fillRange(XCellRange xRange)330     private void fillRange(XCellRange xRange)
331             throws com.sun.star.lang.IndexOutOfBoundsException {
332 
333         for (int i = 0; i <= lastColumn && i < 3; i++) {
334             setCellText(xRange.getCellByPosition(i, 0), "" + i + "a");
335             setCellText(xRange.getCellByPosition(i, 1), "" + i + "b");
336         }
337 
338         for (int i = 3; i <= lastColumn && i < 10; i++) {
339             setCellText(xRange.getCellByPosition(i, 0), "");
340             setCellText(xRange.getCellByPosition(i, 1), "");
341         }
342     }
343 
344     /**
345      * Check the column (first two rows) if it has values with
346      * index specified.
347      *
348      * @param col Column to check
349      * @param idx What indexes must be in cells
350      * @return <code>true</code> if expected indexes are found,
351      *  <code>false</code> otherwise.
352      * @throws IndexOutOfBoundsException
353      */
checkColumn(int col, int idx)354     private boolean checkColumn(int col, int idx)
355             throws com.sun.star.lang.IndexOutOfBoundsException {
356 
357         if (col >= oObj.getCount()) return true;
358 
359         String c1 = getCellText(xCellRange.getCellByPosition(col, 0));
360         String c2 = getCellText(xCellRange.getCellByPosition(col, 1));
361 
362         if (!((""+ idx + "a").equals(c1) && (""+ idx + "b").equals(c2))) {
363 
364             log.println("FAILED for column " + col + " and index " + idx + "("
365                 + c1 + "," + c2 + ")");
366             return false ;
367         }
368         return true;
369     }
370 
371     /**
372      * Checks if the column (first two rows) has no data in its cells.
373      *
374      * @param col Column to check
375      * @return <code>true</code> if the column is empty, <code>false</code>
376      *   if first two cells contains some strings.
377      * @throws IndexOutOfBoundsException
378      */
checkColumnEmpty(int col)379     private boolean checkColumnEmpty(int col)
380             throws com.sun.star.lang.IndexOutOfBoundsException {
381 
382         if (col >= oObj.getCount()) return true;
383 
384         String c1 = getCellText(xCellRange.getCellByPosition(col, 0));
385         String c2 = getCellText(xCellRange.getCellByPosition(col, 1));
386         if (!("".equals(c1) && "".equals(c2))) {
387             log.println("FAILED for column " + col + " is not empty ("
388                 + c1 + "," + c2 + ")");
389             return false ;
390         }
391         return true;
392     }
393 
394     } //finish class _XTableColumns
395 
396