1 package fvt.uno.sc.rowcolumn;
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.uno.UnoApp;
12 
13 import testlib.uno.SCUtil;
14 import testlib.uno.TestUtil;
15 
16 import com.sun.star.lang.XComponent;
17 import com.sun.star.sheet.XSpreadsheet;
18 import com.sun.star.sheet.XSpreadsheetDocument;
19 import com.sun.star.table.CellAddress;
20 import com.sun.star.table.XCell;
21 import com.sun.star.uno.UnoRuntime;
22 import com.sun.star.sheet.XCellAddressable;
23 import com.sun.star.sheet.XSheetAnnotations;
24 import com.sun.star.sheet.XSheetAnnotationsSupplier;
25 import com.sun.star.sheet.XSheetAnnotation;
26 import com.sun.star.sheet.XSheetAnnotationAnchor;
27 import com.sun.star.sheet.XSpreadsheets;
28 import com.sun.star.container.XIndexAccess;
29 
30 
31 /**
32  * Test Create Show Hide Edit Delete Comments
33  * @author BinGuo 9/5/2012
34  *
35  */
36 
37 public class CreateShowHideEditDeleteComments {
38 
39 	UnoApp unoApp = new UnoApp();
40 	XSpreadsheetDocument scDocument = null;
41 	XComponent scComponent = 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 	 * New a spreadsheet
67 	 * Insert annotations for A1:A5
68 	 * Delete the 2nd annotations for A1:A5
69 	 * Edit text in annotation
70 	 */
71 
72 	@Test
73 	public void testCreateEditDeleteComments() throws Exception {
74 
75 		scComponent = unoApp.newDocument("scalc");
76 		scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent);
77 		XSpreadsheets xSpreadsheets = scDocument.getSheets();
78 
79 		// Gets the first sheet in the document.
80 	    XIndexAccess xSheetsIA = (XIndexAccess)UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
81 		Object sheetObj = (XSpreadsheet)UnoRuntime.queryInterface(XSpreadsheet.class, xSheetsIA.getByIndex(0));
82 	    XSpreadsheet xSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
83 
84 		// Get current sheet
85 		xSheet = SCUtil.getCurrentSheet(scDocument);
86 
87 		// Create cell range A2:A5 and Add annotation for cells
88 		int nRow = 1;
89 
90 		for (int i = 1; i < 5; ++i) {
91 			XCell xCell = xSheet.getCellByPosition(0, nRow);
92 			xCell.setValue(nRow);
93 
94 			// Create the CellAddress structure
95 	        XCellAddressable xCellAddr = (XCellAddressable)
96 	         UnoRuntime.queryInterface(XCellAddressable.class, xCell);
97 	        CellAddress aAddress = xCellAddr.getCellAddress();
98 
99 	        // Insert an annotation
100 	        XSheetAnnotationsSupplier xAnnotationsSupp =
101 	         (XSheetAnnotationsSupplier) UnoRuntime.queryInterface(
102 	             XSheetAnnotationsSupplier.class, xSheet);
103 	        XSheetAnnotations xAnnotations = xAnnotationsSupp.getAnnotations();
104 	        xAnnotations.insertNew(aAddress, "This is an annotation");
105 
106 	        nRow += 1;
107 	    }
108 
109         XSheetAnnotationsSupplier xAnnotationsSupp =
110 		         (XSheetAnnotationsSupplier) UnoRuntime.queryInterface(
111 		             XSheetAnnotationsSupplier.class, xSheet);
112 		XSheetAnnotations xAnnotations = xAnnotationsSupp.getAnnotations();
113 
114         // Verify results after insert annotations for cell range A2:A5
115      	assertEquals("Verify total number of annotations after execute insert annotations."
116      	    		  ,4, xAnnotations.getCount());
117 
118      	// Remove annotation
119         xAnnotations.removeByIndex(1);
120 
121         // Verify results after delete annotations from cell range A2:A5
122      	assertEquals("Verify number of annotations after execute delete annotations."
123      	    		  ,3, xAnnotations.getCount());
124 
125     }
126 
127 	/**
128 	 * New a spreadsheet
129 	 * Insert annotations for B2
130 	 * Show it
131 	 * Hide it
132 	 */
133 
134 	@Test
135 	public void testShowHideComments() throws Exception {
136 
137 		scComponent = unoApp.newDocument("scalc");
138 		scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent);
139 		XSpreadsheets xSpreadsheets = scDocument.getSheets();
140 
141 		// Gets the first sheet in the document.
142 	    XIndexAccess xSheetsIA = (XIndexAccess)UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
143 		Object sheetObj = (XSpreadsheet)UnoRuntime.queryInterface(XSpreadsheet.class, xSheetsIA.getByIndex(0));
144 	    XSpreadsheet xSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
145 
146 		// Get current sheet
147 		xSheet = SCUtil.getCurrentSheet(scDocument);
148 
149 		// Create the CellAddress structure
150 
151 		// Get Cell B2
152 		int nColumn = 1;
153 		int nRow = 1;
154 
155 	    XCell xCell = xSheet.getCellByPosition(nColumn, nRow);
156 	    XCellAddressable xCellAddr = (XCellAddressable)
157 	         UnoRuntime.queryInterface(XCellAddressable.class, xCell);
158 	    CellAddress aAddress = xCellAddr.getCellAddress();
159 
160 	    // Insert an annotation
161 	    XSheetAnnotationsSupplier xAnnotationsSupp =
162 	         (XSheetAnnotationsSupplier) UnoRuntime.queryInterface(
163 	             XSheetAnnotationsSupplier.class, xSheet);
164 	    XSheetAnnotations xAnnotations = xAnnotationsSupp.getAnnotations();
165 	    xAnnotations.insertNew(aAddress, "This is an annotation");
166 
167 	    XSheetAnnotationAnchor xAnnotAnchor =
168                 (XSheetAnnotationAnchor) UnoRuntime.queryInterface(XSheetAnnotationAnchor.class, xCell);
169         XSheetAnnotation xAnnotation = xAnnotAnchor.getAnnotation();
170 
171         // Make the annotation visible
172         xAnnotation.setIsVisible(true);
173         ////////
174         TestUtil.printPropertiesList(xAnnotAnchor);
175         ////////////////
176         // Verify annotation is visible.
177         assertTrue("Verify annotation is visible in cell B2.",xAnnotation.getIsVisible());
178 
179         // Make the annotation invisible
180         xAnnotation.setIsVisible(false);
181 
182         // Verify annotation is invisible.
183         assertFalse("Verify annotation is invisible in cell B2.",xAnnotation.getIsVisible());
184 
185 	}
186 
187 }
188 
189 
190 
191 
192