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