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