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