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 package fvt.uno.sc.rowcolumn;
23
24 import static org.junit.Assert.*;
25
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29
30 import org.openoffice.test.uno.UnoApp;
31
32
33 import com.sun.star.lang.XComponent;
34 import com.sun.star.sheet.XSpreadsheet;
35 import com.sun.star.sheet.XSpreadsheetDocument;
36 import com.sun.star.sheet.XSpreadsheets;
37 import com.sun.star.table.XCell;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.table.XTableColumns;
40 import com.sun.star.table.XTableRows;
41 import com.sun.star.table.XColumnRowRange;
42
43 /**
44 * Test insert or delete rows and columns
45 * @author test
46 *
47 */
48
49 public class InsertDeleteRowAndColumn {
50
51 UnoApp unoApp = new UnoApp();
52 XSpreadsheetDocument scDocument = null;
53 XComponent scComponent = null;
54
55 @Before
setUp()56 public void setUp() throws Exception {
57 unoApp.start();
58 }
59
60 @After
tearDown()61 public void tearDown() throws Exception {
62 unoApp.closeDocument(scComponent);
63 unoApp.close();
64 }
65
66 @Test
testInsertDeleteRows()67 public void testInsertDeleteRows() throws Exception {
68
69 String sheetname = "sheet1";
70 scComponent = unoApp.newDocument("scalc");
71 scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent);
72 XSpreadsheets spreadsheets = scDocument.getSheets();
73 Object sheetObj = spreadsheets.getByName(sheetname);
74
75
76 XSpreadsheet sheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
77 XColumnRowRange xCRRange = (XColumnRowRange) UnoRuntime.queryInterface( XColumnRowRange.class, sheet );
78 XTableRows xRows = xCRRange.getRows();
79
80 // Create a cell series "A2:A8" with the values 1 ... 7.
81 int nRow = 1;
82 for (int i = 1; i < 8; ++i) {
83 sheet.getCellByPosition( 0, nRow ).setValue( nRow );
84 nRow += 1;
85 }
86
87 //Insert a row between row 2 and row 3
88 xRows.insertByIndex( 2, 1 );
89
90 //Get value of cell A3
91 XCell cell = sheet.getCellByPosition(0, 2);
92 double checkvalue = 0.0;
93
94 //Verify after insert row
95 assertEquals("Verify one new row inserted after Row 2",checkvalue, cell.getValue(),0);
96
97 //Delete the row 3 and row 4
98 xRows.removeByIndex( 2, 2 );
99
100 //Get value of cell A3 and A4
101 XCell cellA3 = sheet.getCellByPosition(0, 2);
102 XCell cellA4 = sheet.getCellByPosition(0, 3);
103 double checkvalueA3 = 3.0;
104 double checkvalueA4 = 4.0;
105
106 //Verify after delete row3 and row4
107 assertEquals("Verify tow rows deleted the value of row 3",checkvalueA3, cellA3.getValue(),0);
108 assertEquals("Verify tow rows deleted the value of row 4",checkvalueA4, cellA4.getValue(),0);
109
110 }
111
112 @Test
testInsertDeleteColumns()113 public void testInsertDeleteColumns() throws Exception {
114
115 String sheetname = "sheet1";
116 scComponent = unoApp.newDocument("scalc");
117 scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent);
118 XSpreadsheets spreadsheets = scDocument.getSheets();
119 Object sheetObj = spreadsheets.getByName(sheetname);
120
121
122 XSpreadsheet sheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
123 XColumnRowRange xCRRange = (XColumnRowRange) UnoRuntime.queryInterface( XColumnRowRange.class, sheet );
124 XTableColumns xColumns = xCRRange.getColumns();
125
126 // Create a cell series "A2:A8" with the values 1 ... 7.
127 int nRow = 1;
128 for (int i = 1; i < 8; ++i) {
129 sheet.getCellByPosition( 1, nRow ).setValue( nRow );
130 nRow += 1;
131 }
132
133 //Insert a row between row 2 and row 3
134 xColumns.insertByIndex( 0, 1 );
135
136 //Get value of cell C2
137 XCell cell = sheet.getCellByPosition(2, 1);
138 double checkvalue = 1.0;
139
140 //Verify after insert row
141 assertEquals("Verify if one new column inserted after Column A",checkvalue, cell.getValue(),0);
142
143 //Delete the row 3 and row 4
144 xColumns.removeByIndex( 0, 1 );
145
146 //Get value of cell A3 and A4
147 XCell cellA3 = sheet.getCellByPosition(1, 2);
148 XCell cellA4 = sheet.getCellByPosition(1, 3);
149 double checkvalueA3 = 2.0;
150 double checkvalueA4 = 3.0;
151
152 //Verify after delete row3 and row4
153 assertEquals("Verify after tow rows deleted, the value of A3",checkvalueA3, cellA3.getValue(),0);
154 assertEquals("Verify after tow rows deleted, the value of A4",checkvalueA4, cellA4.getValue(),0);
155
156 }
157
158 }
159
160