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