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.AfterClass;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 
32 import org.openoffice.test.uno.UnoApp;
33 
34 import testlib.uno.SCUtil;
35 import testlib.uno.TestUtil;
36 
37 import com.sun.star.lang.XComponent;
38 import com.sun.star.sheet.XSpreadsheet;
39 import com.sun.star.sheet.XSpreadsheetDocument;
40 import com.sun.star.table.CellAddress;
41 import com.sun.star.table.XCell;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.sheet.XCellAddressable;
44 import com.sun.star.sheet.XSheetAnnotations;
45 import com.sun.star.sheet.XSheetAnnotationsSupplier;
46 import com.sun.star.sheet.XSheetAnnotation;
47 import com.sun.star.sheet.XSheetAnnotationAnchor;
48 import com.sun.star.sheet.XSpreadsheets;
49 import com.sun.star.container.XIndexAccess;
50 
51 
52 /**
53  * Test Create Show Hide Edit Delete Comments
54  * @author BinGuo 9/5/2012
55  *
56  */
57 
58 public class CreateShowHideEditDeleteComments {
59 
60 	UnoApp unoApp = new UnoApp();
61 	XSpreadsheetDocument scDocument = null;
62 	XComponent scComponent = 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 	 * New a spreadsheet
88 	 * Insert annotations for A1:A5
89 	 * Delete the 2nd annotations for A1:A5
90 	 * Edit text in annotation
91 	 */
92 
93 	@Test
testCreateEditDeleteComments()94 	public void testCreateEditDeleteComments() throws Exception {
95 
96 		scComponent = unoApp.newDocument("scalc");
97 		scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent);
98 		XSpreadsheets xSpreadsheets = scDocument.getSheets();
99 
100 		// Gets the first sheet in the document.
101 	    XIndexAccess xSheetsIA = (XIndexAccess)UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
102 		Object sheetObj = (XSpreadsheet)UnoRuntime.queryInterface(XSpreadsheet.class, xSheetsIA.getByIndex(0));
103 	    XSpreadsheet xSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
104 
105 		// Get current sheet
106 		xSheet = SCUtil.getCurrentSheet(scDocument);
107 
108 		// Create cell range A2:A5 and Add annotation for cells
109 		int nRow = 1;
110 
111 		for (int i = 1; i < 5; ++i) {
112 			XCell xCell = xSheet.getCellByPosition(0, nRow);
113 			xCell.setValue(nRow);
114 
115 			// Create the CellAddress structure
116 	        XCellAddressable xCellAddr = (XCellAddressable)
117 	         UnoRuntime.queryInterface(XCellAddressable.class, xCell);
118 	        CellAddress aAddress = xCellAddr.getCellAddress();
119 
120 	        // Insert an annotation
121 	        XSheetAnnotationsSupplier xAnnotationsSupp =
122 	         (XSheetAnnotationsSupplier) UnoRuntime.queryInterface(
123 	             XSheetAnnotationsSupplier.class, xSheet);
124 	        XSheetAnnotations xAnnotations = xAnnotationsSupp.getAnnotations();
125 	        xAnnotations.insertNew(aAddress, "This is an annotation");
126 
127 	        nRow += 1;
128 	    }
129 
130         XSheetAnnotationsSupplier xAnnotationsSupp =
131 		         (XSheetAnnotationsSupplier) UnoRuntime.queryInterface(
132 		             XSheetAnnotationsSupplier.class, xSheet);
133 		XSheetAnnotations xAnnotations = xAnnotationsSupp.getAnnotations();
134 
135         // Verify results after insert annotations for cell range A2:A5
136      	assertEquals("Verify total number of annotations after execute insert annotations."
137      	    		  ,4, xAnnotations.getCount());
138 
139      	// Remove annotation
140         xAnnotations.removeByIndex(1);
141 
142         // Verify results after delete annotations from cell range A2:A5
143      	assertEquals("Verify number of annotations after execute delete annotations."
144      	    		  ,3, xAnnotations.getCount());
145 
146     }
147 
148 	/**
149 	 * New a spreadsheet
150 	 * Insert annotations for B2
151 	 * Show it
152 	 * Hide it
153 	 */
154 
155 	@Test
testShowHideComments()156 	public void testShowHideComments() throws Exception {
157 
158 		scComponent = unoApp.newDocument("scalc");
159 		scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent);
160 		XSpreadsheets xSpreadsheets = scDocument.getSheets();
161 
162 		// Gets the first sheet in the document.
163 	    XIndexAccess xSheetsIA = (XIndexAccess)UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
164 		Object sheetObj = (XSpreadsheet)UnoRuntime.queryInterface(XSpreadsheet.class, xSheetsIA.getByIndex(0));
165 	    XSpreadsheet xSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
166 
167 		// Get current sheet
168 		xSheet = SCUtil.getCurrentSheet(scDocument);
169 
170 		// Create the CellAddress structure
171 
172 		// Get Cell B2
173 		int nColumn = 1;
174 		int nRow = 1;
175 
176 	    XCell xCell = xSheet.getCellByPosition(nColumn, nRow);
177 	    XCellAddressable xCellAddr = (XCellAddressable)
178 	         UnoRuntime.queryInterface(XCellAddressable.class, xCell);
179 	    CellAddress aAddress = xCellAddr.getCellAddress();
180 
181 	    // Insert an annotation
182 	    XSheetAnnotationsSupplier xAnnotationsSupp =
183 	         (XSheetAnnotationsSupplier) UnoRuntime.queryInterface(
184 	             XSheetAnnotationsSupplier.class, xSheet);
185 	    XSheetAnnotations xAnnotations = xAnnotationsSupp.getAnnotations();
186 	    xAnnotations.insertNew(aAddress, "This is an annotation");
187 
188 	    XSheetAnnotationAnchor xAnnotAnchor =
189                 (XSheetAnnotationAnchor) UnoRuntime.queryInterface(XSheetAnnotationAnchor.class, xCell);
190         XSheetAnnotation xAnnotation = xAnnotAnchor.getAnnotation();
191 
192         // Make the annotation visible
193         xAnnotation.setIsVisible(true);
194         ////////
195         TestUtil.printPropertiesList(xAnnotAnchor);
196         ////////////////
197         // Verify annotation is visible.
198         assertTrue("Verify annotation is visible in cell B2.",xAnnotation.getIsVisible());
199 
200         // Make the annotation invisible
201         xAnnotation.setIsVisible(false);
202 
203         // Verify annotation is invisible.
204         assertFalse("Verify annotation is invisible in cell B2.",xAnnotation.getIsVisible());
205 
206 	}
207 
208 }
209 
210 
211 
212 
213