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.paragraph;
23 
24 import static org.junit.Assert.*;
25 
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.openoffice.test.common.FileUtil;
30 import org.openoffice.test.common.Testspace;
31 import org.openoffice.test.uno.UnoApp;
32 import com.sun.star.text.*;
33 import com.sun.star.beans.*;
34 import com.sun.star.frame.XStorable;
35 import com.sun.star.uno.UnoRuntime;
36 
37 public class ParagraphTexttoTextAlignment {
38 	private static final UnoApp app = new UnoApp();
39 	XText xText = null;
40 
41 	@Before
setUp()42 	public void setUp() throws Exception {
43 		app.start();
44 
45 	}
46 
47 	@After
tearDown()48 	public void tearDown() throws Exception {
49 		app.close();
50 	}
51 	/*
52 	 * test paragraph background color
53 	 * 1.new a text document
54 	 * 2.insert some text
55 	 * 3.set paragraph text to text alignment
56 	 * 4.save and close the document
57 	 * 5.reload the saved document and check the paragraph text to text alignment
58 	 */
59 	@Test
testTexttoTextAlignment_Baseline()60 	public void testTexttoTextAlignment_Baseline() throws Exception {
61 
62 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
63 		xText = xTextDocument.getText();
64 		xText.setString("we are Chinese,they are American.we are all living in one earth!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!" +
65 				"Hello,world!Hello,world!");
66 		// create text cursor for selecting and formatting text
67 		XTextCursor xTextCursor = xText.createTextCursor();
68 		XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
69 		xCursorProps.setPropertyValue("ParaVertAlignment",ParagraphVertAlign.BASELINE);
70 		//save to odt
71 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
72 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
73 		aStoreProperties_odt[0] = new PropertyValue();
74 		aStoreProperties_odt[1] = new PropertyValue();
75 		aStoreProperties_odt[0].Name = "Override";
76 		aStoreProperties_odt[0].Value = true;
77 		aStoreProperties_odt[1].Name = "FilterName";
78 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
79 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
80 		//save to doc
81 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
82 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
83 		aStoreProperties_doc[0] = new PropertyValue();
84 		aStoreProperties_doc[1] = new PropertyValue();
85 		aStoreProperties_doc[0].Name = "Override";
86 		aStoreProperties_doc[0].Value = true;
87 		aStoreProperties_doc[1].Name = "FilterName";
88 		aStoreProperties_doc[1].Value = "MS Word 97";
89 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
90 		app.closeDocument(xTextDocument);
91 
92 		//reopen the document
93 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
94 		XPropertySet xCursorProps_Assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_odt.getText().createTextCursor());
95 		//verify paragraph text to text alignment
96 		assertEquals("assert paragraph text to text alignment",ParagraphVertAlign.BASELINE,xCursorProps_Assert_odt.getPropertyValue("ParaVertAlignment"));
97 
98 		//reopen the document
99 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
100 		XPropertySet xCursorProps_Assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_doc.getText().createTextCursor());
101 		//verify paragraph text to text alignment
102 		assertEquals("assert paragraph text to text alignment",ParagraphVertAlign.BASELINE,xCursorProps_Assert_doc.getPropertyValue("ParaVertAlignment"));
103 	}
104 	@Test
testTexttoTextAlignment_Bottom()105 	public void testTexttoTextAlignment_Bottom() throws Exception {
106 
107 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
108 		xText = xTextDocument.getText();
109 		xText.setString("we are Chinese,they are American.we are all living in one earth!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!" +
110 				"Hello,world!Hello,world!");
111 		// create text cursor for selecting and formatting text
112 		XTextCursor xTextCursor = xText.createTextCursor();
113 		XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
114 		xCursorProps.setPropertyValue("ParaVertAlignment",ParagraphVertAlign.BOTTOM);
115 		//save to odt
116 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
117 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
118 		aStoreProperties_odt[0] = new PropertyValue();
119 		aStoreProperties_odt[1] = new PropertyValue();
120 		aStoreProperties_odt[0].Name = "Override";
121 		aStoreProperties_odt[0].Value = true;
122 		aStoreProperties_odt[1].Name = "FilterName";
123 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
124 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
125 		//save to doc
126 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
127 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
128 		aStoreProperties_doc[0] = new PropertyValue();
129 		aStoreProperties_doc[1] = new PropertyValue();
130 		aStoreProperties_doc[0].Name = "Override";
131 		aStoreProperties_doc[0].Value = true;
132 		aStoreProperties_doc[1].Name = "FilterName";
133 		aStoreProperties_doc[1].Value = "MS Word 97";
134 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
135 		app.closeDocument(xTextDocument);
136 
137 		//reopen the document
138 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
139 		XPropertySet xCursorProps_Assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_odt.getText().createTextCursor());
140 		//verify paragraph text to text alignment
141 		assertEquals("assert paragraph text to text alignment",ParagraphVertAlign.BOTTOM,xCursorProps_Assert_odt.getPropertyValue("ParaVertAlignment"));
142 
143 		//reopen the document
144 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
145 		XPropertySet xCursorProps_Assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_doc.getText().createTextCursor());
146 		//verify paragraph text to text alignment
147 		assertEquals("assert paragraph text to text alignment",ParagraphVertAlign.BOTTOM,xCursorProps_Assert_doc.getPropertyValue("ParaVertAlignment"));
148 	}
149 	@Test
testTexttoTextAlignment_Center()150 	public void testTexttoTextAlignment_Center() throws Exception {
151 
152 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
153 		xText = xTextDocument.getText();
154 		xText.setString("we are Chinese,they are American.we are all living in one earth!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!" +
155 				"Hello,world!Hello,world!");
156 		// create text cursor for selecting and formatting text
157 		XTextCursor xTextCursor = xText.createTextCursor();
158 		XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
159 		xCursorProps.setPropertyValue("ParaVertAlignment",ParagraphVertAlign.CENTER);
160 		//save to odt
161 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
162 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
163 		aStoreProperties_odt[0] = new PropertyValue();
164 		aStoreProperties_odt[1] = new PropertyValue();
165 		aStoreProperties_odt[0].Name = "Override";
166 		aStoreProperties_odt[0].Value = true;
167 		aStoreProperties_odt[1].Name = "FilterName";
168 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
169 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
170 		//save to doc
171 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
172 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
173 		aStoreProperties_doc[0] = new PropertyValue();
174 		aStoreProperties_doc[1] = new PropertyValue();
175 		aStoreProperties_doc[0].Name = "Override";
176 		aStoreProperties_doc[0].Value = true;
177 		aStoreProperties_doc[1].Name = "FilterName";
178 		aStoreProperties_doc[1].Value = "MS Word 97";
179 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
180 		app.closeDocument(xTextDocument);
181 
182 		//reopen the document
183 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
184 		XPropertySet xCursorProps_Assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_odt.getText().createTextCursor());
185 		//verify paragraph text to text alignment
186 		assertEquals("assert paragraph text to text alignment",ParagraphVertAlign.CENTER,xCursorProps_Assert_odt.getPropertyValue("ParaVertAlignment"));
187 
188 		//reopen the document
189 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
190 		XPropertySet xCursorProps_Assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_doc.getText().createTextCursor());
191 		//verify paragraph text to text alignment
192 		assertEquals("assert paragraph text to text alignment",ParagraphVertAlign.CENTER,xCursorProps_Assert_doc.getPropertyValue("ParaVertAlignment"));
193 	}
194 	@Test
testTexttoTextAlignment_Top()195 	public void testTexttoTextAlignment_Top() throws Exception {
196 
197 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
198 		xText = xTextDocument.getText();
199 		xText.setString("we are Chinese,they are American.we are all living in one earth!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!" +
200 				"Hello,world!Hello,world!");
201 		// create text cursor for selecting and formatting text
202 		XTextCursor xTextCursor = xText.createTextCursor();
203 		XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
204 		xCursorProps.setPropertyValue("ParaVertAlignment",ParagraphVertAlign.TOP);
205 		//save to odt
206 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
207 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
208 		aStoreProperties_odt[0] = new PropertyValue();
209 		aStoreProperties_odt[1] = new PropertyValue();
210 		aStoreProperties_odt[0].Name = "Override";
211 		aStoreProperties_odt[0].Value = true;
212 		aStoreProperties_odt[1].Name = "FilterName";
213 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
214 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
215 		//save to doc
216 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
217 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
218 		aStoreProperties_doc[0] = new PropertyValue();
219 		aStoreProperties_doc[1] = new PropertyValue();
220 		aStoreProperties_doc[0].Name = "Override";
221 		aStoreProperties_doc[0].Value = true;
222 		aStoreProperties_doc[1].Name = "FilterName";
223 		aStoreProperties_doc[1].Value = "MS Word 97";
224 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
225 		app.closeDocument(xTextDocument);
226 
227 		//reopen the document
228 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
229 		XPropertySet xCursorProps_Assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_odt.getText().createTextCursor());
230 		//verify paragraph text to text alignment
231 		assertEquals("assert paragraph text to text alignment",ParagraphVertAlign.TOP,xCursorProps_Assert_odt.getPropertyValue("ParaVertAlignment"));
232 
233 		//reopen the document
234 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
235 		XPropertySet xCursorProps_Assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_doc.getText().createTextCursor());
236 		//verify paragraph text to text alignment
237 		assertEquals("assert paragraph text to text alignment",ParagraphVertAlign.TOP,xCursorProps_Assert_doc.getPropertyValue("ParaVertAlignment"));
238 	}
239 	@Test
testTexttoTextAlignment_Automatic()240 	public void testTexttoTextAlignment_Automatic() throws Exception {
241 
242 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
243 		xText = xTextDocument.getText();
244 		xText.setString("we are Chinese,they are American.we are all living in one earth!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!" +
245 				"Hello,world!Hello,world!");
246 		// create text cursor for selecting and formatting text
247 		XTextCursor xTextCursor = xText.createTextCursor();
248 		XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
249 		xCursorProps.setPropertyValue("ParaVertAlignment",ParagraphVertAlign.AUTOMATIC);
250 		//save to odt
251 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
252 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
253 		aStoreProperties_odt[0] = new PropertyValue();
254 		aStoreProperties_odt[1] = new PropertyValue();
255 		aStoreProperties_odt[0].Name = "Override";
256 		aStoreProperties_odt[0].Value = true;
257 		aStoreProperties_odt[1].Name = "FilterName";
258 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
259 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
260 		//save to doc
261 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
262 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
263 		aStoreProperties_doc[0] = new PropertyValue();
264 		aStoreProperties_doc[1] = new PropertyValue();
265 		aStoreProperties_doc[0].Name = "Override";
266 		aStoreProperties_doc[0].Value = true;
267 		aStoreProperties_doc[1].Name = "FilterName";
268 		aStoreProperties_doc[1].Value = "MS Word 97";
269 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
270 		app.closeDocument(xTextDocument);
271 
272 		//reopen the document
273 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
274 		XPropertySet xCursorProps_Assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_odt.getText().createTextCursor());
275 		//verify paragraph text to text alignment
276 		assertEquals("assert paragraph text to text alignment",ParagraphVertAlign.AUTOMATIC,xCursorProps_Assert_odt.getPropertyValue("ParaVertAlignment"));
277 
278 		//reopen the document
279 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
280 		XPropertySet xCursorProps_Assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_doc.getText().createTextCursor());
281 		//verify paragraph text to text alignment
282 		assertEquals("assert paragraph text to text alignment",ParagraphVertAlign.AUTOMATIC,xCursorProps_Assert_doc.getPropertyValue("ParaVertAlignment"));
283 	}
284 }
285