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 import static testlib.uno.SCUtil.*;
26 
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.openoffice.test.uno.UnoApp;
31 
32 import com.sun.star.lang.XComponent;
33 import com.sun.star.sheet.XSpreadsheet;
34 import com.sun.star.sheet.XSpreadsheetDocument;
35 import com.sun.star.sheet.XSpreadsheets;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.table.XTableColumns;
38 import com.sun.star.table.XTableRows;
39 import com.sun.star.table.XColumnRowRange;
40 import com.sun.star.beans.XPropertySet;
41 import com.sun.star.table.XCellRange;
42 import com.sun.star.frame.XModel;
43 import com.sun.star.frame.XController;
44 import com.sun.star.sheet.XSpreadsheetView;
45 
46 
47 public class ResizeHideShowRowColumn {
48 
49 	UnoApp unoApp = new UnoApp();
50 	XSpreadsheetDocument scDocument = null;
51 	XComponent scComponent = null;
52 
53 	@Before
setUp()54 	public void setUp() throws Exception {
55 		unoApp.start();
56 	}
57 
58 	@After
tearDown()59 	public void tearDown() throws Exception {
60 		unoApp.closeDocument(scComponent);
61 		unoApp.close();
62 		}
63 
64 @Test
testResizeColumn()65 public void testResizeColumn() throws Exception {
66 	String sheetname = "AddTest";
67   	XPropertySet PropSet = null;
68 
69 	//Create Spreadsheet file.
70 	scComponent = unoApp.newDocument("scalc");
71 	scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent);
72 
73 	//Create a sheet at the first place.
74 	XSpreadsheets spreadsheets = scDocument.getSheets();
75 	spreadsheets.insertNewByName(sheetname, (short) 0);
76 	Object sheetObj = spreadsheets.getByName(sheetname);
77 
78 	XSpreadsheet sheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
79 
80 	//Active the new sheet.
81 	XModel scModel = (XModel) UnoRuntime.queryInterface(XModel.class, scDocument);
82     XController scController = scModel.getCurrentController();
83     XSpreadsheetView sheetview = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class, scController);
84     sheetview.setActiveSheet(sheet);
85 
86     //Set cell range to A1:B1
87     XCellRange CellRange = sheet.getCellRangeByPosition(0, 0, 1, 0);
88 
89     //Get column A1 by index
90     XColumnRowRange ColRowRange = (XColumnRowRange)UnoRuntime.queryInterface( XColumnRowRange.class, CellRange );
91     XTableColumns Columns = ColRowRange.getColumns();
92     Object aColumnObj = Columns.getByIndex( 0 );
93 
94     PropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, aColumnObj);
95 
96     //Verify the default values of specified column A1
97     assertTrue("Verify column is visible as default.",  (Boolean) PropSet.getPropertyValue("IsVisible"));
98 
99     //Resize width of column A1 to "6001"
100     PropSet.setPropertyValue( "Width", new Integer( 6001 ));
101 
102     //Save and reload document
103     saveFileAs(scComponent, "TestColumn", "ods");
104     XSpreadsheetDocument TempSCDocument = reloadFile(unoApp, scDocument, "TestColumn.ods");
105     scDocument = TempSCDocument;
106 
107     spreadsheets = scDocument.getSheets();
108    	sheetObj = spreadsheets.getByName(sheetname);
109 	sheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
110 
111     //Set cell range to A1:B1
112     CellRange = sheet.getCellRangeByPosition(0, 0, 1, 0);
113     ColRowRange = (XColumnRowRange)UnoRuntime.queryInterface( XColumnRowRange.class, CellRange );
114     Columns = ColRowRange.getColumns();
115 
116     //Get column A1 by index
117     aColumnObj = Columns.getByIndex( 0 );
118 
119     PropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, aColumnObj);
120 
121     //Verify the  values of specified column A1 after resize
122        int expectedWidth = 6001;
123 
124     assertEquals("Verify current width value is 6001.", expectedWidth, PropSet.getPropertyValue("Width"));
125     assertTrue("Verify column is visible as default.",  (Boolean) PropSet.getPropertyValue("IsVisible"));
126 
127     //Set column is invisible
128     PropSet.setPropertyValue("IsVisible", new Boolean(false));
129 
130     //Save and reload document
131     //Save the modified spreadsheet first
132     save(TempSCDocument);
133     //close it and reload it
134     TempSCDocument = reloadFile(unoApp, scDocument, "TestColumn.ods");
135     scDocument = TempSCDocument;
136 
137     spreadsheets = scDocument.getSheets();
138 	sheetObj = spreadsheets.getByName(sheetname);
139 	sheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
140 
141     //Set cell range to A1:B1
142     CellRange = sheet.getCellRangeByPosition(0, 0, 1, 0);
143     ColRowRange = (XColumnRowRange)UnoRuntime.queryInterface( XColumnRowRange.class, CellRange );
144     Columns = ColRowRange.getColumns();
145 
146     //Get column A1 by index
147     aColumnObj = Columns.getByIndex( 0 );
148 
149     PropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, aColumnObj);
150 
151     //Verify the values of specified column A1 after save
152     assertFalse("Verify column A1 is invisible", (Boolean) PropSet.getPropertyValue("IsVisible"));
153     assertEquals("Verify current width value is 6001 after hide it.", expectedWidth, PropSet.getPropertyValue("Width"));
154     }
155 
156 @Test
testResizeRow()157 public void testResizeRow() throws Exception {
158 	String sheetname = "AddTest";
159 	XPropertySet PropSet = null;
160 
161 	//Create Spreadsheet file.
162 	scComponent = unoApp.newDocument("scalc");
163 	scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent);
164 
165 	//Create a sheet at the first place.
166 	XSpreadsheets spreadsheets = scDocument.getSheets();
167 	spreadsheets.insertNewByName(sheetname, (short) 0);
168 	Object sheetObj = spreadsheets.getByName(sheetname);
169 	XSpreadsheet sheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
170 
171 	//Active the new sheet.
172 	XModel scModel = (XModel) UnoRuntime.queryInterface(XModel.class, scDocument);
173     XController scController = scModel.getCurrentController();
174     XSpreadsheetView sheetview = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class, scController);
175     sheetview.setActiveSheet(sheet);
176 
177     //Set cell range to A1:A2
178     XCellRange CellRange = sheet.getCellRangeByPosition(0, 0, 0, 1);
179     //XCell cell = sheet.getCellByPosition(1, 0);
180     XColumnRowRange ColRowRange = (XColumnRowRange)UnoRuntime.queryInterface( XColumnRowRange.class, CellRange );
181     XTableRows Rows = ColRowRange.getRows();
182 
183     //Get Row 1 by index
184     Object aRowObj = Rows.getByIndex( 0 );
185 
186     PropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, aRowObj );
187 
188     //Verify the default values of specified Row 1
189     assertTrue("Verify column is visible as default.",  (Boolean) PropSet.getPropertyValue("IsVisible"));
190 
191     //Resize Height of Row 1 to "5001"
192     PropSet.setPropertyValue( "Height", new Integer( 5001 ) );
193 
194     // Save and reload document
195     saveFileAs(scComponent, "TestRow", "xls");
196     XSpreadsheetDocument TempSCDocument = reloadFile(unoApp, scDocument, "TestRow.xls");
197     scDocument = TempSCDocument;
198 
199     spreadsheets = scDocument.getSheets();
200 	sheetObj = spreadsheets.getByName(sheetname);
201 	sheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
202 
203     //Set cell range to A1:A2
204     CellRange = sheet.getCellRangeByPosition(0, 0, 0, 1);
205     ColRowRange = (XColumnRowRange)UnoRuntime.queryInterface( XColumnRowRange.class, CellRange );
206     Rows = ColRowRange.getRows();
207 
208     //Get Row 1 by index
209     aRowObj = Rows.getByIndex( 0 );
210 
211     PropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, aRowObj);
212 
213     //Verify the  values of specified Row 1 after resize
214     int expectedHeight = 5001;
215 
216     assertEquals("Verify current width value is 5001.", expectedHeight, PropSet.getPropertyValue("Height"));
217     assertTrue("Verify column is visible as default.", (Boolean) PropSet.getPropertyValue("IsVisible"));
218 
219     //Set Row is invisible
220     PropSet.setPropertyValue("IsVisible", new Boolean(false));
221 
222     //Save and reload document
223     //Save the modified spreadsheet first
224     save(TempSCDocument);
225     //Close and reload it
226     TempSCDocument = reloadFile(unoApp, scDocument, "TestRow.xls");
227     scDocument = TempSCDocument;
228 
229     spreadsheets = scDocument.getSheets();
230  	sheetObj = spreadsheets.getByName(sheetname);
231  	sheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
232 
233     //Set cell range to A1:A2
234     CellRange = sheet.getCellRangeByPosition(0, 0, 0, 1);
235     ColRowRange = (XColumnRowRange)UnoRuntime.queryInterface( XColumnRowRange.class, CellRange );
236     Rows = ColRowRange.getRows();
237 
238     //Get Row 1 by index
239     aRowObj = Rows.getByIndex( 0 );
240 
241     PropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, aRowObj);
242 
243     //Verify the values of specified Row 1 after resize
244     assertEquals("Verify current height value is 5001 after hide it.", expectedHeight, PropSet.getPropertyValue("Height"));
245     assertFalse("Verify column is invisible.",  (Boolean) PropSet.getPropertyValue("IsVisible"));
246 
247     }
248 
249 }
250 
251 
252