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