1*eba4d44aSLiu Zhe package fvt.uno.sc.rowcolumn;
290175296SLiu Zhe 
390175296SLiu Zhe import static org.junit.Assert.*;
490175296SLiu Zhe 
590175296SLiu Zhe import org.junit.After;
690175296SLiu Zhe import org.junit.AfterClass;
790175296SLiu Zhe import org.junit.Before;
890175296SLiu Zhe import org.junit.BeforeClass;
990175296SLiu Zhe import org.junit.Test;
1090175296SLiu Zhe 
1190175296SLiu Zhe import org.openoffice.test.uno.UnoApp;
1290175296SLiu Zhe 
1390175296SLiu Zhe import testlib.uno.SCUtil;
1490175296SLiu Zhe import testlib.uno.TestUtil;
1590175296SLiu Zhe 
1690175296SLiu Zhe import com.sun.star.lang.XComponent;
1790175296SLiu Zhe import com.sun.star.sheet.XSpreadsheet;
1890175296SLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
1990175296SLiu Zhe import com.sun.star.table.CellAddress;
2090175296SLiu Zhe import com.sun.star.table.XCell;
2190175296SLiu Zhe import com.sun.star.uno.UnoRuntime;
2290175296SLiu Zhe import com.sun.star.sheet.XCellAddressable;
2390175296SLiu Zhe import com.sun.star.sheet.XSheetAnnotations;
2490175296SLiu Zhe import com.sun.star.sheet.XSheetAnnotationsSupplier;
2590175296SLiu Zhe import com.sun.star.sheet.XSheetAnnotation;
2690175296SLiu Zhe import com.sun.star.sheet.XSheetAnnotationAnchor;
2790175296SLiu Zhe import com.sun.star.sheet.XSpreadsheets;
2890175296SLiu Zhe import com.sun.star.container.XIndexAccess;
2990175296SLiu Zhe 
3090175296SLiu Zhe 
3190175296SLiu Zhe /**
3290175296SLiu Zhe  * Test Create Show Hide Edit Delete Comments
3390175296SLiu Zhe  * @author BinGuo 9/5/2012
3490175296SLiu Zhe  *
3590175296SLiu Zhe  */
3690175296SLiu Zhe 
3790175296SLiu Zhe public class CreateShowHideEditDeleteComments {
3890175296SLiu Zhe 
3990175296SLiu Zhe 	UnoApp unoApp = new UnoApp();
4090175296SLiu Zhe 	XSpreadsheetDocument scDocument = null;
4190175296SLiu Zhe 	XComponent scComponent = null;
4290175296SLiu Zhe 
4390175296SLiu Zhe 	@Before
4490175296SLiu Zhe 	public void setUp() throws Exception {
4590175296SLiu Zhe 		unoApp.start();
4690175296SLiu Zhe 	}
4790175296SLiu Zhe 
4890175296SLiu Zhe 	@After
4990175296SLiu Zhe 	public void tearDown() throws Exception {
5090175296SLiu Zhe 		unoApp.closeDocument(scComponent);
5190175296SLiu Zhe 		unoApp.close();
5290175296SLiu Zhe 		}
5390175296SLiu Zhe 
5490175296SLiu Zhe 	@BeforeClass
5590175296SLiu Zhe 	public static void setUpConnection() throws Exception {
5690175296SLiu Zhe //		unoApp.start();
5790175296SLiu Zhe 	}
5890175296SLiu Zhe 
5990175296SLiu Zhe 	@AfterClass
6090175296SLiu Zhe 	public static void tearDownConnection() throws InterruptedException, Exception {
6190175296SLiu Zhe //		unoApp.close();
6290175296SLiu Zhe 		SCUtil.clearTempDir();
6390175296SLiu Zhe 	}
6490175296SLiu Zhe 
6590175296SLiu Zhe 	/**
6690175296SLiu Zhe 	 * New a spreadsheet
6790175296SLiu Zhe 	 * Insert annotations for A1:A5
6890175296SLiu Zhe 	 * Delete the 2nd annotations for A1:A5
6990175296SLiu Zhe 	 * Edit text in annotation
7090175296SLiu Zhe 	 */
7190175296SLiu Zhe 
7290175296SLiu Zhe 	@Test
7390175296SLiu Zhe 	public void testCreateEditDeleteComments() throws Exception {
7490175296SLiu Zhe 
7590175296SLiu Zhe 		scComponent = unoApp.newDocument("scalc");
7690175296SLiu Zhe 		scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent);
7790175296SLiu Zhe 		XSpreadsheets xSpreadsheets = scDocument.getSheets();
7890175296SLiu Zhe 
7990175296SLiu Zhe 		// Gets the first sheet in the document.
8090175296SLiu Zhe 	    XIndexAccess xSheetsIA = (XIndexAccess)UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
8190175296SLiu Zhe 		Object sheetObj = (XSpreadsheet)UnoRuntime.queryInterface(XSpreadsheet.class, xSheetsIA.getByIndex(0));
8290175296SLiu Zhe 	    XSpreadsheet xSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
8390175296SLiu Zhe 
8490175296SLiu Zhe 		// Get current sheet
8590175296SLiu Zhe 		xSheet = SCUtil.getCurrentSheet(scDocument);
8690175296SLiu Zhe 
8790175296SLiu Zhe 		// Create cell range A2:A5 and Add annotation for cells
8890175296SLiu Zhe 		int nRow = 1;
8990175296SLiu Zhe 
9090175296SLiu Zhe 		for (int i = 1; i < 5; ++i) {
9190175296SLiu Zhe 			XCell xCell = xSheet.getCellByPosition(0, nRow);
9290175296SLiu Zhe 			xCell.setValue(nRow);
9390175296SLiu Zhe 
9490175296SLiu Zhe 			// Create the CellAddress structure
9590175296SLiu Zhe 	        XCellAddressable xCellAddr = (XCellAddressable)
9690175296SLiu Zhe 	         UnoRuntime.queryInterface(XCellAddressable.class, xCell);
9790175296SLiu Zhe 	        CellAddress aAddress = xCellAddr.getCellAddress();
9890175296SLiu Zhe 
9990175296SLiu Zhe 	        // Insert an annotation
10090175296SLiu Zhe 	        XSheetAnnotationsSupplier xAnnotationsSupp =
10190175296SLiu Zhe 	         (XSheetAnnotationsSupplier) UnoRuntime.queryInterface(
10290175296SLiu Zhe 	             XSheetAnnotationsSupplier.class, xSheet);
10390175296SLiu Zhe 	        XSheetAnnotations xAnnotations = xAnnotationsSupp.getAnnotations();
10490175296SLiu Zhe 	        xAnnotations.insertNew(aAddress, "This is an annotation");
10590175296SLiu Zhe 
10690175296SLiu Zhe 	        nRow += 1;
10790175296SLiu Zhe 	    }
10890175296SLiu Zhe 
10990175296SLiu Zhe         XSheetAnnotationsSupplier xAnnotationsSupp =
11090175296SLiu Zhe 		         (XSheetAnnotationsSupplier) UnoRuntime.queryInterface(
11190175296SLiu Zhe 		             XSheetAnnotationsSupplier.class, xSheet);
11290175296SLiu Zhe 		XSheetAnnotations xAnnotations = xAnnotationsSupp.getAnnotations();
11390175296SLiu Zhe 
11490175296SLiu Zhe         // Verify results after insert annotations for cell range A2:A5
11590175296SLiu Zhe      	assertEquals("Verify total number of annotations after execute insert annotations."
11690175296SLiu Zhe      	    		  ,4, xAnnotations.getCount());
11790175296SLiu Zhe 
11890175296SLiu Zhe      	// Remove annotation
11990175296SLiu Zhe         xAnnotations.removeByIndex(1);
12090175296SLiu Zhe 
12190175296SLiu Zhe         // Verify results after delete annotations from cell range A2:A5
12290175296SLiu Zhe      	assertEquals("Verify number of annotations after execute delete annotations."
12390175296SLiu Zhe      	    		  ,3, xAnnotations.getCount());
12490175296SLiu Zhe 
12590175296SLiu Zhe     }
12690175296SLiu Zhe 
12790175296SLiu Zhe 	/**
12890175296SLiu Zhe 	 * New a spreadsheet
12990175296SLiu Zhe 	 * Insert annotations for B2
13090175296SLiu Zhe 	 * Show it
13190175296SLiu Zhe 	 * Hide it
13290175296SLiu Zhe 	 */
13390175296SLiu Zhe 
13490175296SLiu Zhe 	@Test
13590175296SLiu Zhe 	public void testShowHideComments() throws Exception {
13690175296SLiu Zhe 
13790175296SLiu Zhe 		scComponent = unoApp.newDocument("scalc");
13890175296SLiu Zhe 		scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent);
13990175296SLiu Zhe 		XSpreadsheets xSpreadsheets = scDocument.getSheets();
14090175296SLiu Zhe 
14190175296SLiu Zhe 		// Gets the first sheet in the document.
14290175296SLiu Zhe 	    XIndexAccess xSheetsIA = (XIndexAccess)UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
14390175296SLiu Zhe 		Object sheetObj = (XSpreadsheet)UnoRuntime.queryInterface(XSpreadsheet.class, xSheetsIA.getByIndex(0));
14490175296SLiu Zhe 	    XSpreadsheet xSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
14590175296SLiu Zhe 
14690175296SLiu Zhe 		// Get current sheet
14790175296SLiu Zhe 		xSheet = SCUtil.getCurrentSheet(scDocument);
14890175296SLiu Zhe 
14990175296SLiu Zhe 		// Create the CellAddress structure
15090175296SLiu Zhe 
15190175296SLiu Zhe 		// Get Cell B2
15290175296SLiu Zhe 		int nColumn = 1;
15390175296SLiu Zhe 		int nRow = 1;
15490175296SLiu Zhe 
15590175296SLiu Zhe 	    XCell xCell = xSheet.getCellByPosition(nColumn, nRow);
15690175296SLiu Zhe 	    XCellAddressable xCellAddr = (XCellAddressable)
15790175296SLiu Zhe 	         UnoRuntime.queryInterface(XCellAddressable.class, xCell);
15890175296SLiu Zhe 	    CellAddress aAddress = xCellAddr.getCellAddress();
15990175296SLiu Zhe 
16090175296SLiu Zhe 	    // Insert an annotation
16190175296SLiu Zhe 	    XSheetAnnotationsSupplier xAnnotationsSupp =
16290175296SLiu Zhe 	         (XSheetAnnotationsSupplier) UnoRuntime.queryInterface(
16390175296SLiu Zhe 	             XSheetAnnotationsSupplier.class, xSheet);
16490175296SLiu Zhe 	    XSheetAnnotations xAnnotations = xAnnotationsSupp.getAnnotations();
16590175296SLiu Zhe 	    xAnnotations.insertNew(aAddress, "This is an annotation");
16690175296SLiu Zhe 
16790175296SLiu Zhe 	    XSheetAnnotationAnchor xAnnotAnchor =
16890175296SLiu Zhe                 (XSheetAnnotationAnchor) UnoRuntime.queryInterface(XSheetAnnotationAnchor.class, xCell);
16990175296SLiu Zhe         XSheetAnnotation xAnnotation = xAnnotAnchor.getAnnotation();
17090175296SLiu Zhe 
17190175296SLiu Zhe         // Make the annotation visible
17290175296SLiu Zhe         xAnnotation.setIsVisible(true);
17390175296SLiu Zhe         ////////
17490175296SLiu Zhe         TestUtil.printPropertiesList(xAnnotAnchor);
17590175296SLiu Zhe         ////////////////
17690175296SLiu Zhe         // Verify annotation is visible.
17790175296SLiu Zhe         assertTrue("Verify annotation is visible in cell B2.",xAnnotation.getIsVisible());
17890175296SLiu Zhe 
17990175296SLiu Zhe         // Make the annotation invisible
18090175296SLiu Zhe         xAnnotation.setIsVisible(false);
18190175296SLiu Zhe 
18290175296SLiu Zhe         // Verify annotation is invisible.
18390175296SLiu Zhe         assertFalse("Verify annotation is invisible in cell B2.",xAnnotation.getIsVisible());
18490175296SLiu Zhe 
18590175296SLiu Zhe 	}
18690175296SLiu Zhe 
18790175296SLiu Zhe }
18890175296SLiu Zhe 
18990175296SLiu Zhe 
19090175296SLiu Zhe 
19190175296SLiu Zhe 
192