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.cell;
23 
24 import static org.junit.Assert.*;
25 
26 import org.junit.After;
27 import org.junit.AfterClass;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 
32 import org.openoffice.test.common.Testspace;
33 import org.openoffice.test.uno.UnoApp;
34 
35 import testlib.uno.SCUtil;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.sheet.XSpreadsheet;
38 import com.sun.star.sheet.XSpreadsheetDocument;
39 import com.sun.star.table.XCell;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.table.XCellRange;
42 import com.sun.star.sheet.XSheetAnnotations;
43 import com.sun.star.sheet.XSheetAnnotationsSupplier;
44 import com.sun.star.sheet.XSheetOperation;
45 import com.sun.star.drawing.XDrawPage;
46 import com.sun.star.drawing.XDrawPageSupplier;
47 import com.sun.star.drawing.XShapes;
48 
49 /**
50  * Test delete contents
51  * @author BinGuo 9/4/2012
52  *
53  */
54 
55 public class DeleteContents {
56 
57 	UnoApp unoApp = new UnoApp();
58 	XSpreadsheetDocument scDocument = null;
59 	XComponent scComponent = null;
60 	private String filename = "uno/sc/cell/TestDeleteContents.ods";
61 	XDrawPage xpage = null;
62 	XShapes xShapes = null;
63 
64 	@Before
setUp()65 	public void setUp() throws Exception {
66 		unoApp.start();
67 	}
68 
69 	@After
tearDown()70 	public void tearDown() throws Exception {
71 		unoApp.closeDocument(scComponent);
72 		unoApp.close();
73 		}
74 
75 	@BeforeClass
setUpConnection()76 	public static void setUpConnection() throws Exception {
77 //		unoApp.start();
78 	}
79 
80 	@AfterClass
tearDownConnection()81 	public static void tearDownConnection() throws InterruptedException, Exception {
82 //		unoApp.close();
83 		SCUtil.clearTempDir();
84 	}
85 
86 	/**
87 	 * Open existing ODS file with contents in cell range B5:C15
88 	 * Execute delete Text, Verify results after delete text
89 	 * Execute delete Number, Verify results after delete number
90 	 * Execute delete Formula, Verify results after delete formula
91 	 * Execute delete Comment, Verify results after delete comment
92 	 * Execute delete Format, Verify results after delete format
93 	 * Execute delete Drawing Object, Verify results after delete object
94 	 */
95 
96 	@Test
testDeleteContents()97 	public void testDeleteContents() throws Exception {
98 
99 		// Prepare test data
100 		String sample = Testspace.prepareData(filename);
101 
102 		// Open document
103 		scDocument = SCUtil.openFile(sample, unoApp);
104 
105 		// Get current sheet
106 		XSpreadsheet xSheet = SCUtil.getCurrentSheet(scDocument);
107 
108 		// Get cell range B5:C15 by position - (column, row, column, row)
109         XCellRange xCellRange = xSheet.getCellRangeByPosition( 1, 4, 2, 14 );
110         XSheetOperation xSheetOp = (XSheetOperation) UnoRuntime.queryInterface(XSheetOperation.class, xCellRange);
111 
112         //Get Cell B5
113         XCell cellB5 = xSheet.getCellByPosition(1, 4);
114 
115         //Delete Text from Cell Range B5:C15
116         xSheetOp.clearContents(4);
117 
118         //Verify results after execute delete 'Text' contents.
119         double NullCellValue = 0.0;
120         assertEquals("Verify after execute delete 'Text' contents.",NullCellValue, cellB5.getValue(),0);
121 
122         //Get Cell B7
123         XCell cellB7 = xSheet.getCellByPosition(1, 6);
124 
125         //Delete Date & Time from Cell Range B5:C15
126         xSheetOp.clearContents(2);
127 
128         //Verify results after execute delete 'Date & Time' contents.
129         assertEquals("Verify after execute delete 'Date & Time' contents.",NullCellValue, cellB7.getValue(),0);
130 
131         //Get Cell B8
132         XCell cellB8 = xSheet.getCellByPosition(1, 7);
133 
134         //Delete Formula from Cell Range B5:C15
135         xSheetOp.clearContents(16);
136 
137         //Verify results after execute delete 'Formula' contents.
138         assertEquals("Verify after execute delete 'Formula' contents.",NullCellValue, cellB8.getValue(),0);
139 
140         //Delete Comment from Cell Range B5:C15
141         xSheetOp.clearContents(8);
142 
143         XSheetAnnotationsSupplier xAnnotationsSupp =
144 		         (XSheetAnnotationsSupplier) UnoRuntime.queryInterface(
145 		             XSheetAnnotationsSupplier.class, xSheet);
146 		XSheetAnnotations xAnnotations = xAnnotationsSupp.getAnnotations();
147 
148         //Verify comment is deleted.
149 		assertEquals("Verify total number of annotations after execute delete annotations."
150 	    		  ,0, xAnnotations.getCount());
151         //Delete contents with Formatted from Cell Range B5:C15
152 //        xSheetOp.clearContents(512); @Ignore("#BUGID 120816 - BUG TITLE Method clearContents() clears 'Formatted' contents of the cells used does not work, If constants of com.sun.star.sheet.CellFlags is '512'. ")
153 
154         //Get Cell B6 and B10
155         XCell cellB6 = xSheet.getCellByPosition(1, 5);
156         XCell cellB10 = xSheet.getCellByPosition(1, 9);
157 
158         xSheetOp.clearContents(1);
159 
160         //Verify results after execute delete 'Number' contents.
161         assertEquals("Verify after execute delete 'Number' contents in B6.",NullCellValue, cellB6.getValue(),0);
162         assertEquals("Verify after execute delete 'Number' contents in B10.",NullCellValue, cellB10.getValue(),0);
163 
164         //Get Draw page
165         XDrawPageSupplier xDrawPageSupplier =
166         		(XDrawPageSupplier)UnoRuntime.queryInterface(XDrawPageSupplier.class, xSheet);
167         XDrawPage xDrawPage = xDrawPageSupplier.getDrawPage();
168 
169         XShapes xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xDrawPage);
170 
171         //Verify number of shape in sheet.
172         assertEquals("Verify number of shape in sheet.",1, xShapes.getCount());
173 
174         //Delete drawing object from Cell Range B5:C15
175         xSheetOp.clearContents(128);
176 
177         xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xDrawPage);
178 
179         //Verify results after execute delete 'Objects' contents in cell range.
180         assertEquals("Verify shape is deleted in sheet after execute delete 'Objects' contents in cell range."
181     		  ,0, xShapes.getCount());
182 
183     }
184 
185 }
186 
187 
188