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.Test;
29f4c702faSLiu Zhe import org.openoffice.test.common.FileUtil;
30f4c702faSLiu Zhe import org.openoffice.test.common.Testspace;
31f4c702faSLiu Zhe import org.openoffice.test.uno.UnoApp;
32f4c702faSLiu Zhe 
33f4c702faSLiu Zhe import com.sun.star.uno.UnoRuntime;
34f4c702faSLiu Zhe import com.sun.star.text.*;
35f4c702faSLiu Zhe import com.sun.star.lang.XMultiServiceFactory;
36f4c702faSLiu Zhe import com.sun.star.beans.PropertyValue;
37f4c702faSLiu Zhe import com.sun.star.beans.XPropertySet;
38f4c702faSLiu Zhe import com.sun.star.container.XIndexAccess;
39f4c702faSLiu Zhe import com.sun.star.frame.XStorable;
40f4c702faSLiu Zhe 
41f4c702faSLiu Zhe 
42f4c702faSLiu Zhe public class TableBorderSpacingtoContent {
43f4c702faSLiu Zhe 
44f4c702faSLiu Zhe 	private static final UnoApp app = new UnoApp();
45f4c702faSLiu Zhe 	private XTextDocument xTextDocument=null;
46f4c702faSLiu Zhe 	private XMultiServiceFactory xWriterFactory=null;
47f4c702faSLiu Zhe 	private XText xText=null;
48f4c702faSLiu Zhe 	@Before
setUp()49f4c702faSLiu Zhe 	public void setUp() throws Exception {
50f4c702faSLiu Zhe 		app.start();
51f4c702faSLiu Zhe 	}
52f4c702faSLiu Zhe 
53f4c702faSLiu Zhe 	@After
tearDown()54f4c702faSLiu Zhe 	public void tearDown() throws Exception {
55f4c702faSLiu Zhe 		app.close();
56f4c702faSLiu Zhe 	}
57f4c702faSLiu Zhe 	/*
58f4c702faSLiu Zhe 	 * test table border spacing to content
59f4c702faSLiu Zhe 	 * 1.new a text document and create a table
60f4c702faSLiu Zhe 	 * 2.set table border spacing to content
61f4c702faSLiu Zhe 	 * 3.save to odt/doc,close it and reopen new saved document
62f4c702faSLiu Zhe 	 * 4.check the table border spacing to content
63f4c702faSLiu Zhe 	 */
64f4c702faSLiu Zhe 	@Test
testtableBorderSpacingtoContent()65f4c702faSLiu Zhe 	public void testtableBorderSpacingtoContent() throws Exception {
66f4c702faSLiu Zhe 		xTextDocument =(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
67f4c702faSLiu Zhe 		xText=xTextDocument.getText();
68f4c702faSLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
69f4c702faSLiu Zhe 		// get internal service factory of the document
70f4c702faSLiu Zhe 		xWriterFactory =(XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
71f4c702faSLiu Zhe 		// Create a new table from the document's factory
72f4c702faSLiu Zhe 		XTextTable xTable = (XTextTable)UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.TextTable"));
73f4c702faSLiu Zhe 		xText.insertTextContent(xTextCursor,xTable,false);
74f4c702faSLiu Zhe 		String[] cellName=xTable.getCellNames();
75f4c702faSLiu Zhe 		int i=0;
76f4c702faSLiu Zhe 		while(cellName[i] != null)
77f4c702faSLiu Zhe 		{
78f4c702faSLiu Zhe 		XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable.getCellByName(cellName[i]));
79f4c702faSLiu Zhe 		xCursorProps.setPropertyValue("LeftBorderDistance",499);
80f4c702faSLiu Zhe 		xCursorProps.setPropertyValue("RightBorderDistance",499);
81f4c702faSLiu Zhe 		xCursorProps.setPropertyValue("TopBorderDistance",499);
82f4c702faSLiu Zhe 		xCursorProps.setPropertyValue("BottomBorderDistance",499);
83f4c702faSLiu Zhe 		i++;
84f4c702faSLiu Zhe 		if(i==4)break;
85f4c702faSLiu Zhe 		}
86f4c702faSLiu Zhe 		//save to odt
87f4c702faSLiu Zhe 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
88f4c702faSLiu Zhe 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
89f4c702faSLiu Zhe 		aStoreProperties_odt[0] = new PropertyValue();
90f4c702faSLiu Zhe 		aStoreProperties_odt[1] = new PropertyValue();
91f4c702faSLiu Zhe 		aStoreProperties_odt[0].Name = "Override";
92f4c702faSLiu Zhe 		aStoreProperties_odt[0].Value = true;
93f4c702faSLiu Zhe 		aStoreProperties_odt[1].Name = "FilterName";
94f4c702faSLiu Zhe 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
95f4c702faSLiu Zhe 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
96f4c702faSLiu Zhe 		//save to doc
97f4c702faSLiu Zhe 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
98f4c702faSLiu Zhe 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
99f4c702faSLiu Zhe 		aStoreProperties_doc[0] = new PropertyValue();
100f4c702faSLiu Zhe 		aStoreProperties_doc[1] = new PropertyValue();
101f4c702faSLiu Zhe 		aStoreProperties_doc[0].Name = "Override";
102f4c702faSLiu Zhe 		aStoreProperties_doc[0].Value = true;
103f4c702faSLiu Zhe 		aStoreProperties_doc[1].Name = "FilterName";
104f4c702faSLiu Zhe 		aStoreProperties_doc[1].Value = "MS Word 97";
105f4c702faSLiu Zhe 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
106f4c702faSLiu Zhe 		app.closeDocument(xTextDocument);
107f4c702faSLiu Zhe 
108f4c702faSLiu Zhe 		//reopen the odt document and assert table border spacing to content
109f4c702faSLiu Zhe 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
110f4c702faSLiu Zhe 		XTextTablesSupplier xTablesSupplier_odt = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_odt );
111f4c702faSLiu Zhe 		XIndexAccess xIndexedTables_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_odt.getTextTables());
112f4c702faSLiu Zhe 		Object xTable_obj_odt=xIndexedTables_odt.getByIndex(0);
113f4c702faSLiu Zhe 		XTextTable xTable_Assert_odt=(XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_odt);
114f4c702faSLiu Zhe 		String[] cellName_assert_odt=xTable_Assert_odt.getCellNames();
115f4c702faSLiu Zhe 		int j=0;
116f4c702faSLiu Zhe 		while(cellName_assert_odt[j] != null)
117f4c702faSLiu Zhe 		{
118f4c702faSLiu Zhe 		XPropertySet xCursorProps_assert_odt = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable_Assert_odt.getCellByName(cellName_assert_odt[j]));
119f4c702faSLiu Zhe 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_odt.getPropertyValue("LeftBorderDistance"));
120f4c702faSLiu Zhe 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_odt.getPropertyValue("RightBorderDistance"));
121f4c702faSLiu Zhe 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_odt.getPropertyValue("TopBorderDistance"));
122f4c702faSLiu Zhe 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_odt.getPropertyValue("BottomBorderDistance"));
123f4c702faSLiu Zhe 		j++;
124f4c702faSLiu Zhe 		if(j==4)break;
125f4c702faSLiu Zhe 		}
126f4c702faSLiu Zhe 
127f4c702faSLiu Zhe 		//reopen the doc document and assert table border spacing to content
128f4c702faSLiu Zhe 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
129f4c702faSLiu Zhe 		XTextTablesSupplier xTablesSupplier_doc = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_doc );
130f4c702faSLiu Zhe 		XIndexAccess xIndexedTables_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_doc.getTextTables());
131f4c702faSLiu Zhe 		Object xTable_obj_doc=xIndexedTables_doc.getByIndex(0);
132f4c702faSLiu Zhe 		XTextTable xTable_Assert_doc=(XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_doc);
133f4c702faSLiu Zhe 		String[] cellName_assert_doc=xTable_Assert_doc.getCellNames();
134f4c702faSLiu Zhe 		int k=0;
135f4c702faSLiu Zhe 		while(cellName_assert_doc[k] != null)
136f4c702faSLiu Zhe 		{
137f4c702faSLiu Zhe 		XPropertySet xCursorProps_assert_doc = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable_Assert_doc.getCellByName(cellName_assert_doc[k]));
138f4c702faSLiu Zhe 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_doc.getPropertyValue("LeftBorderDistance"));
139f4c702faSLiu Zhe 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_doc.getPropertyValue("RightBorderDistance"));
140f4c702faSLiu Zhe 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_doc.getPropertyValue("TopBorderDistance"));
141f4c702faSLiu Zhe 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_doc.getPropertyValue("BottomBorderDistance"));
142f4c702faSLiu Zhe 		k++;
143f4c702faSLiu Zhe 		if(k==4)break;
144f4c702faSLiu Zhe 		}
145f4c702faSLiu Zhe 	}
146f4c702faSLiu Zhe }
147f4c702faSLiu Zhe 
148