1 package fvt.uno.sw.table;
2 
3 import static org.junit.Assert.*;
4 
5 import org.junit.After;
6 import org.junit.Before;
7 import org.junit.Test;
8 import org.openoffice.test.common.FileUtil;
9 import org.openoffice.test.common.Testspace;
10 import org.openoffice.test.uno.UnoApp;
11 
12 import com.sun.star.uno.UnoRuntime;
13 import com.sun.star.text.*;
14 import com.sun.star.lang.XMultiServiceFactory;
15 import com.sun.star.beans.PropertyValue;
16 import com.sun.star.beans.XPropertySet;
17 import com.sun.star.container.XIndexAccess;
18 import com.sun.star.frame.XStorable;
19 
20 
21 public class TableVerticalAlignment {
22 
23 	private static final UnoApp app = new UnoApp();
24 	private XTextDocument xTextDocument=null;
25 	private XMultiServiceFactory xWriterFactory=null;
26 	private XText xText=null;
27 	@Before
28 	public void setUp() throws Exception {
29 		app.start();
30 	}
31 
32 	@After
33 	public void tearDown() throws Exception {
34 		app.close();
35 	}
36 	/*
37 	 * test table border spacing to content
38 	 * 1.new a text document and create a table
39 	 * 2.set table cell vertical alignment
40 	 * 3.save to odt/doc,close it and reopen new saved document
41 	 * 4.check the table cell vertical alignment
42 	 */
43 	@Test
44 	public void testtableVerticalAlignment_Bottom() throws Exception {
45 		xTextDocument =(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
46 		xText=xTextDocument.getText();
47 		XTextCursor xTextCursor = xText.createTextCursor();
48 		// get internal service factory of the document
49 		xWriterFactory =(XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
50 		// Create a new table from the document's factory
51 		XTextTable xTable = (XTextTable)UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.TextTable"));
52 		xText.insertTextContent(xTextCursor,xTable,false);
53 		String[] cellName=xTable.getCellNames();
54 		int i=0;
55 		while(cellName[i] != null)
56 		{
57 		XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable.getCellByName(cellName[i]));
58 		xCursorProps.setPropertyValue("VertOrient",VertOrientation.BOTTOM);
59 		i++;
60 		if(i==4)break;
61 		}
62 		//save to odt
63 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
64 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
65 		aStoreProperties_odt[0] = new PropertyValue();
66 		aStoreProperties_odt[1] = new PropertyValue();
67 		aStoreProperties_odt[0].Name = "Override";
68 		aStoreProperties_odt[0].Value = true;
69 		aStoreProperties_odt[1].Name = "FilterName";
70 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
71 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
72 		//save to doc
73 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
74 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
75 		aStoreProperties_doc[0] = new PropertyValue();
76 		aStoreProperties_doc[1] = new PropertyValue();
77 		aStoreProperties_doc[0].Name = "Override";
78 		aStoreProperties_doc[0].Value = true;
79 		aStoreProperties_doc[1].Name = "FilterName";
80 		aStoreProperties_doc[1].Value = "MS Word 97";
81 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
82 		app.closeDocument(xTextDocument);
83 
84 		//reopen the odt document and assert table vertical alignment
85 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
86 		XTextTablesSupplier xTablesSupplier_odt = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_odt );
87 		XIndexAccess xIndexedTables_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_odt.getTextTables());
88 		Object xTable_obj_odt=xIndexedTables_odt.getByIndex(0);
89 		XTextTable xTable_Assert_odt=(XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_odt);
90 		String[] cellName_assert_odt=xTable_Assert_odt.getCellNames();
91 		int j=0;
92 		while(cellName_assert_odt[j] != null)
93 		{
94 		XPropertySet xCursorProps_assert_odt = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable_Assert_odt.getCellByName(cellName_assert_odt[j]));
95 		assertEquals("assert table border spacing to content",VertOrientation.BOTTOM,xCursorProps_assert_odt.getPropertyValue("VertOrient"));
96 		j++;
97 		if(j==4)break;
98 		}
99 
100 		//reopen the doc document and assert table vertical alignment
101 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
102 		XTextTablesSupplier xTablesSupplier_doc = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_doc );
103 		XIndexAccess xIndexedTables_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_doc.getTextTables());
104 		Object xTable_obj_doc=xIndexedTables_doc.getByIndex(0);
105 		XTextTable xTable_Assert_doc=(XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_doc);
106 		String[] cellName_assert_doc=xTable_Assert_doc.getCellNames();
107 		int k=0;
108 		while(cellName_assert_doc[k] != null)
109 		{
110 		XPropertySet xCursorProps_assert_odt = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable_Assert_doc.getCellByName(cellName_assert_doc[k]));
111 		assertEquals("assert table vertical alignment",VertOrientation.BOTTOM,xCursorProps_assert_odt.getPropertyValue("VertOrient"));
112 		k++;
113 		if(k==4)break;
114 		}
115 	}
116 	@Test
117 	public void testtableVerticalAlignment_Center() throws Exception {
118 		xTextDocument =(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
119 		xText=xTextDocument.getText();
120 		XTextCursor xTextCursor = xText.createTextCursor();
121 		// get internal service factory of the document
122 		xWriterFactory =(XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
123 		// Create a new table from the document's factory
124 		XTextTable xTable = (XTextTable)UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.TextTable"));
125 		xText.insertTextContent(xTextCursor,xTable,false);
126 		String[] cellName=xTable.getCellNames();
127 		int i=0;
128 		while(cellName[i] != null)
129 		{
130 		XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable.getCellByName(cellName[i]));
131 		xCursorProps.setPropertyValue("VertOrient",VertOrientation.CENTER);
132 		i++;
133 		if(i==4)break;
134 		}
135 		//save to odt
136 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
137 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
138 		aStoreProperties_odt[0] = new PropertyValue();
139 		aStoreProperties_odt[1] = new PropertyValue();
140 		aStoreProperties_odt[0].Name = "Override";
141 		aStoreProperties_odt[0].Value = true;
142 		aStoreProperties_odt[1].Name = "FilterName";
143 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
144 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
145 		//save to doc
146 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
147 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
148 		aStoreProperties_doc[0] = new PropertyValue();
149 		aStoreProperties_doc[1] = new PropertyValue();
150 		aStoreProperties_doc[0].Name = "Override";
151 		aStoreProperties_doc[0].Value = true;
152 		aStoreProperties_doc[1].Name = "FilterName";
153 		aStoreProperties_doc[1].Value = "MS Word 97";
154 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
155 		app.closeDocument(xTextDocument);
156 
157 		//reopen the odt document and assert table vertical alignment
158 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
159 		XTextTablesSupplier xTablesSupplier_odt = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_odt );
160 		XIndexAccess xIndexedTables_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_odt.getTextTables());
161 		Object xTable_obj_odt=xIndexedTables_odt.getByIndex(0);
162 		XTextTable xTable_Assert_odt=(XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_odt);
163 		String[] cellName_assert_odt=xTable_Assert_odt.getCellNames();
164 		int j=0;
165 		while(cellName_assert_odt[j] != null)
166 		{
167 		XPropertySet xCursorProps_assert_odt = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable_Assert_odt.getCellByName(cellName_assert_odt[j]));
168 		assertEquals("assert table border spacing to content",VertOrientation.CENTER,xCursorProps_assert_odt.getPropertyValue("VertOrient"));
169 		j++;
170 		if(j==4)break;
171 		}
172 
173 		//reopen the doc document and assert table vertical alignment
174 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
175 		XTextTablesSupplier xTablesSupplier_doc = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_doc );
176 		XIndexAccess xIndexedTables_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_doc.getTextTables());
177 		Object xTable_obj_doc=xIndexedTables_doc.getByIndex(0);
178 		XTextTable xTable_Assert_doc=(XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_doc);
179 		String[] cellName_assert_doc=xTable_Assert_doc.getCellNames();
180 		int k=0;
181 		while(cellName_assert_doc[k] != null)
182 		{
183 		XPropertySet xCursorProps_assert_odt = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable_Assert_doc.getCellByName(cellName_assert_doc[k]));
184 		assertEquals("assert table vertical alignment",VertOrientation.CENTER,xCursorProps_assert_odt.getPropertyValue("VertOrient"));
185 		k++;
186 		if(k==4)break;
187 		}
188 	}
189 	@Test
190 	public void testtableVerticalAlignment_Top() throws Exception {
191 		xTextDocument =(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
192 		xText=xTextDocument.getText();
193 		XTextCursor xTextCursor = xText.createTextCursor();
194 		// get internal service factory of the document
195 		xWriterFactory =(XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
196 		// Create a new table from the document's factory
197 		XTextTable xTable = (XTextTable)UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.TextTable"));
198 		xText.insertTextContent(xTextCursor,xTable,false);
199 		String[] cellName=xTable.getCellNames();
200 		int i=0;
201 		while(cellName[i] != null)
202 		{
203 		XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable.getCellByName(cellName[i]));
204 		xCursorProps.setPropertyValue("VertOrient",VertOrientation.TOP);
205 		i++;
206 		if(i==4)break;
207 		}
208 		//save to odt
209 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
210 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
211 		aStoreProperties_odt[0] = new PropertyValue();
212 		aStoreProperties_odt[1] = new PropertyValue();
213 		aStoreProperties_odt[0].Name = "Override";
214 		aStoreProperties_odt[0].Value = true;
215 		aStoreProperties_odt[1].Name = "FilterName";
216 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
217 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
218 		//save to doc
219 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
220 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
221 		aStoreProperties_doc[0] = new PropertyValue();
222 		aStoreProperties_doc[1] = new PropertyValue();
223 		aStoreProperties_doc[0].Name = "Override";
224 		aStoreProperties_doc[0].Value = true;
225 		aStoreProperties_doc[1].Name = "FilterName";
226 		aStoreProperties_doc[1].Value = "MS Word 97";
227 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
228 		app.closeDocument(xTextDocument);
229 
230 		//reopen the odt document and assert table vertical alignment
231 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
232 		XTextTablesSupplier xTablesSupplier_odt = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_odt );
233 		XIndexAccess xIndexedTables_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_odt.getTextTables());
234 		Object xTable_obj_odt=xIndexedTables_odt.getByIndex(0);
235 		XTextTable xTable_Assert_odt=(XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_odt);
236 		String[] cellName_assert_odt=xTable_Assert_odt.getCellNames();
237 		int j=0;
238 		while(cellName_assert_odt[j] != null)
239 		{
240 		XPropertySet xCursorProps_assert_odt = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable_Assert_odt.getCellByName(cellName_assert_odt[j]));
241 		assertEquals("assert table border spacing to content",VertOrientation.TOP,xCursorProps_assert_odt.getPropertyValue("VertOrient"));
242 		j++;
243 		if(j==4)break;
244 		}
245 
246 		//reopen the doc document and assert table vertical alignment
247 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
248 		XTextTablesSupplier xTablesSupplier_doc = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_doc );
249 		XIndexAccess xIndexedTables_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_doc.getTextTables());
250 		Object xTable_obj_doc=xIndexedTables_doc.getByIndex(0);
251 		XTextTable xTable_Assert_doc=(XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_doc);
252 		String[] cellName_assert_doc=xTable_Assert_doc.getCellNames();
253 		int k=0;
254 		while(cellName_assert_doc[k] != null)
255 		{
256 		XPropertySet xCursorProps_assert_odt = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable_Assert_doc.getCellByName(cellName_assert_doc[k]));
257 		assertEquals("assert table vertical alignment",VertOrientation.TOP,xCursorProps_assert_odt.getPropertyValue("VertOrient"));
258 		k++;
259 		if(k==4)break;
260 		}
261 	}
262 }
263 
264