1*eba4d44aSLiu Zhe package fvt.uno.sw.table;
2f4c702faSLiu Zhe 
3f4c702faSLiu Zhe import static org.junit.Assert.*;
4f4c702faSLiu Zhe 
5f4c702faSLiu Zhe import org.junit.After;
6f4c702faSLiu Zhe import org.junit.Before;
7f4c702faSLiu Zhe import org.junit.Ignore;
8f4c702faSLiu Zhe import org.junit.Test;
9f4c702faSLiu Zhe import org.openoffice.test.common.FileUtil;
10f4c702faSLiu Zhe import org.openoffice.test.common.Testspace;
11f4c702faSLiu Zhe import org.openoffice.test.uno.UnoApp;
12f4c702faSLiu Zhe 
13f4c702faSLiu Zhe import com.sun.star.uno.UnoRuntime;
14f4c702faSLiu Zhe import com.sun.star.text.*;
15f4c702faSLiu Zhe import com.sun.star.lang.XMultiServiceFactory;
16f4c702faSLiu Zhe import com.sun.star.beans.PropertyValue;
17f4c702faSLiu Zhe import com.sun.star.beans.XPropertySet;
18f4c702faSLiu Zhe import com.sun.star.container.XIndexAccess;
19f4c702faSLiu Zhe import com.sun.star.frame.XStorable;
20f4c702faSLiu Zhe 
21f4c702faSLiu Zhe 
22f4c702faSLiu Zhe public class TableCellProtect {
23f4c702faSLiu Zhe 
24f4c702faSLiu Zhe 	private static final UnoApp app = new UnoApp();
25f4c702faSLiu Zhe 	private XTextDocument xTextDocument=null;
26f4c702faSLiu Zhe 	private XMultiServiceFactory xWriterFactory=null;
27f4c702faSLiu Zhe 	private XText xText=null;
28f4c702faSLiu Zhe 	@Before
29f4c702faSLiu Zhe 	public void setUp() throws Exception {
30f4c702faSLiu Zhe 		app.start();
31f4c702faSLiu Zhe 	}
32f4c702faSLiu Zhe 
33f4c702faSLiu Zhe 	@After
34f4c702faSLiu Zhe 	public void tearDown() throws Exception {
35f4c702faSLiu Zhe 		app.close();
36f4c702faSLiu Zhe 	}
37f4c702faSLiu Zhe 	/*
38f4c702faSLiu Zhe 	 * test table border spacing to content
39f4c702faSLiu Zhe 	 * 1.new a text document and create a table
40f4c702faSLiu Zhe 	 * 2.set table cell protect
41f4c702faSLiu Zhe 	 * 3.save to odt/doc,close it and reopen new saved document
42f4c702faSLiu Zhe 	 * 4.check the table cell protect setting
43f4c702faSLiu Zhe 	 */
44932de486SLiu Zhe 	@Test@Ignore("Bug #120745 - [testUNO patch]table cell protect effect lost when save to doc.")
45f4c702faSLiu Zhe 	public void testtableBorderSpacingtoContent() throws Exception {
46f4c702faSLiu Zhe 		xTextDocument =(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
47f4c702faSLiu Zhe 		xText=xTextDocument.getText();
48f4c702faSLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
49f4c702faSLiu Zhe 		// get internal service factory of the document
50f4c702faSLiu Zhe 		xWriterFactory =(XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
51f4c702faSLiu Zhe 		// Create a new table from the document's factory
52f4c702faSLiu Zhe 		XTextTable xTable = (XTextTable)UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.TextTable"));
53f4c702faSLiu Zhe 		xText.insertTextContent(xTextCursor,xTable,false);
54f4c702faSLiu Zhe 		String[] cellName=xTable.getCellNames();
55f4c702faSLiu Zhe 		int i=0;
56f4c702faSLiu Zhe 		while(cellName[i] != null)
57f4c702faSLiu Zhe 		{
58f4c702faSLiu Zhe 		XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable.getCellByName(cellName[i]));
59f4c702faSLiu Zhe 		xCursorProps.setPropertyValue("IsProtected",true);
60f4c702faSLiu Zhe 		i++;
61f4c702faSLiu Zhe 		if(i==4)break;
62f4c702faSLiu Zhe 		}
63f4c702faSLiu Zhe 		//save to odt
64f4c702faSLiu Zhe 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
65f4c702faSLiu Zhe 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
66f4c702faSLiu Zhe 		aStoreProperties_odt[0] = new PropertyValue();
67f4c702faSLiu Zhe 		aStoreProperties_odt[1] = new PropertyValue();
68f4c702faSLiu Zhe 		aStoreProperties_odt[0].Name = "Override";
69f4c702faSLiu Zhe 		aStoreProperties_odt[0].Value = true;
70f4c702faSLiu Zhe 		aStoreProperties_odt[1].Name = "FilterName";
71f4c702faSLiu Zhe 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
72f4c702faSLiu Zhe 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
73f4c702faSLiu Zhe 		//save to doc
74f4c702faSLiu Zhe 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
75f4c702faSLiu Zhe 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
76f4c702faSLiu Zhe 		aStoreProperties_doc[0] = new PropertyValue();
77f4c702faSLiu Zhe 		aStoreProperties_doc[1] = new PropertyValue();
78f4c702faSLiu Zhe 		aStoreProperties_doc[0].Name = "Override";
79f4c702faSLiu Zhe 		aStoreProperties_doc[0].Value = true;
80f4c702faSLiu Zhe 		aStoreProperties_doc[1].Name = "FilterName";
81f4c702faSLiu Zhe 		aStoreProperties_doc[1].Value = "MS Word 97";
82f4c702faSLiu Zhe 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
83f4c702faSLiu Zhe 		app.closeDocument(xTextDocument);
84f4c702faSLiu Zhe 
85f4c702faSLiu Zhe 		//reopen the odt document and assert table border spacing to content
86f4c702faSLiu Zhe 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
87f4c702faSLiu Zhe 		XTextTablesSupplier xTablesSupplier_odt = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_odt );
88f4c702faSLiu Zhe 		XIndexAccess xIndexedTables_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_odt.getTextTables());
89f4c702faSLiu Zhe 		Object xTable_obj_odt=xIndexedTables_odt.getByIndex(0);
90f4c702faSLiu Zhe 		XTextTable xTable_Assert_odt=(XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_odt);
91f4c702faSLiu Zhe 		String[] cellName_assert_odt=xTable_Assert_odt.getCellNames();
92f4c702faSLiu Zhe 		int j=0;
93f4c702faSLiu Zhe 		while(cellName_assert_odt[j] != null)
94f4c702faSLiu Zhe 		{
95f4c702faSLiu Zhe 		XPropertySet xCursorProps_assert_odt = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable_Assert_odt.getCellByName(cellName_assert_odt[j]));
96f4c702faSLiu Zhe 		assertEquals("assert table cell proptext",true,xCursorProps_assert_odt.getPropertyValue("IsProtected"));
97f4c702faSLiu Zhe 		j++;
98f4c702faSLiu Zhe 		if(j==4)break;
99f4c702faSLiu Zhe 		}
100f4c702faSLiu Zhe 
101f4c702faSLiu Zhe 		//reopen the doc document and assert table border spacing to content
102f4c702faSLiu Zhe 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
103f4c702faSLiu Zhe 		XTextTablesSupplier xTablesSupplier_doc = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_doc );
104f4c702faSLiu Zhe 		XIndexAccess xIndexedTables_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_doc.getTextTables());
105f4c702faSLiu Zhe 		Object xTable_obj_doc=xIndexedTables_doc.getByIndex(0);
106f4c702faSLiu Zhe 		XTextTable xTable_Assert_doc=(XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_doc);
107f4c702faSLiu Zhe 		String[] cellName_assert_doc=xTable_Assert_doc.getCellNames();
108f4c702faSLiu Zhe 		int k=0;
109f4c702faSLiu Zhe 		while(cellName_assert_doc[k] != null)
110f4c702faSLiu Zhe 		{
111f4c702faSLiu Zhe 		XPropertySet xCursorProps_assert_doc = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable_Assert_doc.getCellByName(cellName_assert_doc[k]));
112f4c702faSLiu Zhe 		assertEquals("assert table cell proptext",true,xCursorProps_assert_doc.getPropertyValue("IsProtected"));
113f4c702faSLiu Zhe 		k++;
114f4c702faSLiu Zhe 		if(k==4)break;
115f4c702faSLiu Zhe 		}
116f4c702faSLiu Zhe 	}
117f4c702faSLiu Zhe }
118f4c702faSLiu Zhe 
119