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