1*07d7dbdcSHerbert Dürr /**************************************************************
2*07d7dbdcSHerbert Dürr  *
3*07d7dbdcSHerbert Dürr  * Licensed to the Apache Software Foundation (ASF) under one
4*07d7dbdcSHerbert Dürr  * or more contributor license agreements.  See the NOTICE file
5*07d7dbdcSHerbert Dürr  * distributed with this work for additional information
6*07d7dbdcSHerbert Dürr  * regarding copyright ownership.  The ASF licenses this file
7*07d7dbdcSHerbert Dürr  * to you under the Apache License, Version 2.0 (the
8*07d7dbdcSHerbert Dürr  * "License"); you may not use this file except in compliance
9*07d7dbdcSHerbert Dürr  * with the License.  You may obtain a copy of the License at
10*07d7dbdcSHerbert Dürr  *
11*07d7dbdcSHerbert Dürr  *   http://www.apache.org/licenses/LICENSE-2.0
12*07d7dbdcSHerbert Dürr  *
13*07d7dbdcSHerbert Dürr  * Unless required by applicable law or agreed to in writing,
14*07d7dbdcSHerbert Dürr  * software distributed under the License is distributed on an
15*07d7dbdcSHerbert Dürr  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*07d7dbdcSHerbert Dürr  * KIND, either express or implied.  See the License for the
17*07d7dbdcSHerbert Dürr  * specific language governing permissions and limitations
18*07d7dbdcSHerbert Dürr  * under the License.
19*07d7dbdcSHerbert Dürr  *
20*07d7dbdcSHerbert Dürr  *************************************************************/
21*07d7dbdcSHerbert Dürr 
22eba4d44aSLiu Zhe package fvt.uno.sw.table;
23f4c702faSLiu Zhe 
24f4c702faSLiu Zhe import static org.junit.Assert.*;
25f4c702faSLiu Zhe 
26f4c702faSLiu Zhe import org.junit.After;
27f4c702faSLiu Zhe import org.junit.Before;
28f4c702faSLiu Zhe import org.junit.Ignore;
29f4c702faSLiu Zhe import org.junit.Test;
30f4c702faSLiu Zhe import org.openoffice.test.common.FileUtil;
31f4c702faSLiu Zhe import org.openoffice.test.common.Testspace;
32f4c702faSLiu Zhe import org.openoffice.test.uno.UnoApp;
33f4c702faSLiu Zhe 
34f4c702faSLiu Zhe import com.sun.star.uno.UnoRuntime;
35f4c702faSLiu Zhe import com.sun.star.text.*;
36f4c702faSLiu Zhe import com.sun.star.lang.XMultiServiceFactory;
37f4c702faSLiu Zhe import com.sun.star.beans.PropertyValue;
38f4c702faSLiu Zhe import com.sun.star.beans.XPropertySet;
39f4c702faSLiu Zhe import com.sun.star.container.XIndexAccess;
40f4c702faSLiu Zhe import com.sun.star.frame.XStorable;
41f4c702faSLiu Zhe 
42f4c702faSLiu Zhe 
43f4c702faSLiu Zhe public class TableCellProtect {
44f4c702faSLiu Zhe 
45f4c702faSLiu Zhe 	private static final UnoApp app = new UnoApp();
46f4c702faSLiu Zhe 	private XTextDocument xTextDocument=null;
47f4c702faSLiu Zhe 	private XMultiServiceFactory xWriterFactory=null;
48f4c702faSLiu Zhe 	private XText xText=null;
49f4c702faSLiu Zhe 	@Before
setUp()50f4c702faSLiu Zhe 	public void setUp() throws Exception {
51f4c702faSLiu Zhe 		app.start();
52f4c702faSLiu Zhe 	}
53f4c702faSLiu Zhe 
54f4c702faSLiu Zhe 	@After
tearDown()55f4c702faSLiu Zhe 	public void tearDown() throws Exception {
56f4c702faSLiu Zhe 		app.close();
57f4c702faSLiu Zhe 	}
58f4c702faSLiu Zhe 	/*
59f4c702faSLiu Zhe 	 * test table border spacing to content
60f4c702faSLiu Zhe 	 * 1.new a text document and create a table
61f4c702faSLiu Zhe 	 * 2.set table cell protect
62f4c702faSLiu Zhe 	 * 3.save to odt/doc,close it and reopen new saved document
63f4c702faSLiu Zhe 	 * 4.check the table cell protect setting
64f4c702faSLiu Zhe 	 */
65932de486SLiu Zhe 	@Test@Ignore("Bug #120745 - [testUNO patch]table cell protect effect lost when save to doc.")
testtableBorderSpacingtoContent()66f4c702faSLiu Zhe 	public void testtableBorderSpacingtoContent() throws Exception {
67f4c702faSLiu Zhe 		xTextDocument =(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
68f4c702faSLiu Zhe 		xText=xTextDocument.getText();
69f4c702faSLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
70f4c702faSLiu Zhe 		// get internal service factory of the document
71f4c702faSLiu Zhe 		xWriterFactory =(XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
72f4c702faSLiu Zhe 		// Create a new table from the document's factory
73f4c702faSLiu Zhe 		XTextTable xTable = (XTextTable)UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.TextTable"));
74f4c702faSLiu Zhe 		xText.insertTextContent(xTextCursor,xTable,false);
75f4c702faSLiu Zhe 		String[] cellName=xTable.getCellNames();
76f4c702faSLiu Zhe 		int i=0;
77f4c702faSLiu Zhe 		while(cellName[i] != null)
78f4c702faSLiu Zhe 		{
79f4c702faSLiu Zhe 		XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable.getCellByName(cellName[i]));
80f4c702faSLiu Zhe 		xCursorProps.setPropertyValue("IsProtected",true);
81f4c702faSLiu Zhe 		i++;
82f4c702faSLiu Zhe 		if(i==4)break;
83f4c702faSLiu Zhe 		}
84f4c702faSLiu Zhe 		//save to odt
85f4c702faSLiu Zhe 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
86f4c702faSLiu Zhe 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
87f4c702faSLiu Zhe 		aStoreProperties_odt[0] = new PropertyValue();
88f4c702faSLiu Zhe 		aStoreProperties_odt[1] = new PropertyValue();
89f4c702faSLiu Zhe 		aStoreProperties_odt[0].Name = "Override";
90f4c702faSLiu Zhe 		aStoreProperties_odt[0].Value = true;
91f4c702faSLiu Zhe 		aStoreProperties_odt[1].Name = "FilterName";
92f4c702faSLiu Zhe 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
93f4c702faSLiu Zhe 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
94f4c702faSLiu Zhe 		//save to doc
95f4c702faSLiu Zhe 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
96f4c702faSLiu Zhe 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
97f4c702faSLiu Zhe 		aStoreProperties_doc[0] = new PropertyValue();
98f4c702faSLiu Zhe 		aStoreProperties_doc[1] = new PropertyValue();
99f4c702faSLiu Zhe 		aStoreProperties_doc[0].Name = "Override";
100f4c702faSLiu Zhe 		aStoreProperties_doc[0].Value = true;
101f4c702faSLiu Zhe 		aStoreProperties_doc[1].Name = "FilterName";
102f4c702faSLiu Zhe 		aStoreProperties_doc[1].Value = "MS Word 97";
103f4c702faSLiu Zhe 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
104f4c702faSLiu Zhe 		app.closeDocument(xTextDocument);
105f4c702faSLiu Zhe 
106f4c702faSLiu Zhe 		//reopen the odt document and assert table border spacing to content
107f4c702faSLiu Zhe 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
108f4c702faSLiu Zhe 		XTextTablesSupplier xTablesSupplier_odt = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_odt );
109f4c702faSLiu Zhe 		XIndexAccess xIndexedTables_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_odt.getTextTables());
110f4c702faSLiu Zhe 		Object xTable_obj_odt=xIndexedTables_odt.getByIndex(0);
111f4c702faSLiu Zhe 		XTextTable xTable_Assert_odt=(XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_odt);
112f4c702faSLiu Zhe 		String[] cellName_assert_odt=xTable_Assert_odt.getCellNames();
113f4c702faSLiu Zhe 		int j=0;
114f4c702faSLiu Zhe 		while(cellName_assert_odt[j] != null)
115f4c702faSLiu Zhe 		{
116f4c702faSLiu Zhe 		XPropertySet xCursorProps_assert_odt = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable_Assert_odt.getCellByName(cellName_assert_odt[j]));
117f4c702faSLiu Zhe 		assertEquals("assert table cell proptext",true,xCursorProps_assert_odt.getPropertyValue("IsProtected"));
118f4c702faSLiu Zhe 		j++;
119f4c702faSLiu Zhe 		if(j==4)break;
120f4c702faSLiu Zhe 		}
121f4c702faSLiu Zhe 
122f4c702faSLiu Zhe 		//reopen the doc document and assert table border spacing to content
123f4c702faSLiu Zhe 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
124f4c702faSLiu Zhe 		XTextTablesSupplier xTablesSupplier_doc = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_doc );
125f4c702faSLiu Zhe 		XIndexAccess xIndexedTables_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_doc.getTextTables());
126f4c702faSLiu Zhe 		Object xTable_obj_doc=xIndexedTables_doc.getByIndex(0);
127f4c702faSLiu Zhe 		XTextTable xTable_Assert_doc=(XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_doc);
128f4c702faSLiu Zhe 		String[] cellName_assert_doc=xTable_Assert_doc.getCellNames();
129f4c702faSLiu Zhe 		int k=0;
130f4c702faSLiu Zhe 		while(cellName_assert_doc[k] != null)
131f4c702faSLiu Zhe 		{
132f4c702faSLiu Zhe 		XPropertySet xCursorProps_assert_doc = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable_Assert_doc.getCellByName(cellName_assert_doc[k]));
133f4c702faSLiu Zhe 		assertEquals("assert table cell proptext",true,xCursorProps_assert_doc.getPropertyValue("IsProtected"));
134f4c702faSLiu Zhe 		k++;
135f4c702faSLiu Zhe 		if(k==4)break;
136f4c702faSLiu Zhe 		}
137f4c702faSLiu Zhe 	}
138f4c702faSLiu Zhe }
139f4c702faSLiu Zhe 
140