1 package fvt.uno.sw.paragraph;
2 
3 import static org.junit.Assert.*;
4 
5 import org.junit.After;
6 import org.junit.Before;
7 import org.junit.Ignore;
8 import org.junit.Test;
9 import org.openoffice.test.common.FileUtil;
10 import org.openoffice.test.common.Testspace;
11 import org.openoffice.test.uno.UnoApp;
12 import com.sun.star.text.*;
13 import com.sun.star.beans.*;
14 import com.sun.star.frame.XStorable;
15 import com.sun.star.uno.UnoRuntime;
16 
17 public class ParagraphAlignment {
18 	private static final UnoApp app = new UnoApp();
19 	XText xText = null;
20 
21 	@Before
22 	public void setUp() throws Exception {
23 		app.start();
24 
25 	}
26 
27 	@After
28 	public void tearDown() throws Exception {
29 		app.close();
30 	}
31 	/*
32 	 * test paragraph alignment is justified
33 	 * 1.new a text document
34 	 * 2.insert some text
35 	 * 3.set paragraph alignment is justified,and last line align to left,check expand single word
36 	 * 4.save and close the document
37 	 * 5.reload the saved document and check the paragraph alignment
38 	 */
39 	@Test@Ignore("Bug #120636 - [testUNO patch]the expand single word option disable when save to doc")
40 	public void testParagraphAlignmentJustified() throws Exception {
41 
42 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
43 		xText = xTextDocument.getText();
44 		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!" +
45 				"Hello,world!Hello,world!");
46 		// create text cursor for selecting and formatting text
47 		XTextCursor xTextCursor = xText.createTextCursor();
48 		XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
49 		//apply paragraph alignment as justified and last line alignment
50 		xTextCursor.gotoStart(false);
51 		xTextCursor.goRight((short)180 , true);
52 		xCursorProps.setPropertyValue("ParaAdjust",com.sun.star.style.ParagraphAdjust.BLOCK);
53 		xCursorProps.setPropertyValue("ParaLastLineAdjust", com.sun.star.style.ParagraphAdjust.LEFT);
54 		xCursorProps.setPropertyValue("ParaExpandSingleWord", true);
55 		//save to odt
56 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
57 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
58 		aStoreProperties_odt[0] = new PropertyValue();
59 		aStoreProperties_odt[1] = new PropertyValue();
60 		aStoreProperties_odt[0].Name = "Override";
61 		aStoreProperties_odt[0].Value = true;
62 		aStoreProperties_odt[1].Name = "FilterName";
63 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
64 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
65 		//save to doc
66 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
67 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
68 		aStoreProperties_doc[0] = new PropertyValue();
69 		aStoreProperties_doc[1] = new PropertyValue();
70 		aStoreProperties_doc[0].Name = "Override";
71 		aStoreProperties_doc[0].Value = true;
72 		aStoreProperties_doc[1].Name = "FilterName";
73 		aStoreProperties_doc[1].Value = "MS Word 97";
74 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
75 		app.closeDocument(xTextDocument);
76 
77 		//reopen the document and assert table margin to page setting
78 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
79 		XTextCursor xTextCursor_assert_odt = assertDocument_odt.getText().createTextCursor();
80 		XPropertySet xCursorProps_assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor_assert_odt);
81 		//verify paragraph alignment property
82 		assertEquals("assert first paragraph alignment is justified",(short)2,xCursorProps_assert_odt.getPropertyValue("ParaAdjust"));
83 		assertEquals("assert first paragraph last line alignment is left",(short)0, xCursorProps_assert_odt.getPropertyValue("ParaLastLineAdjust"));
84 		assertEquals("assert expand single word is true",true,xCursorProps_assert_odt.getPropertyValue("ParaExpandSingleWord"));
85 
86 		//reopen the document and assert table margin to page setting
87 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
88 		XTextCursor xTextCursor_assert_doc = assertDocument_doc.getText().createTextCursor();
89 		XPropertySet xCursorProps_assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor_assert_doc);
90 		//verify paragraph alignment property
91 	    assertEquals("assert first paragraph alignment is justified",(short)2,xCursorProps_assert_doc.getPropertyValue("ParaAdjust"));
92 		assertEquals("assert first paragraph last line alignment is left",(short)0, xCursorProps_assert_doc.getPropertyValue("ParaLastLineAdjust"));
93 		assertEquals("assert expand single word is true",true,xCursorProps_assert_doc.getPropertyValue("ParaExpandSingleWord"));
94 
95 	}
96 	/*
97 	 * test paragraph alignment is left
98 	 * 1.new a text document
99 	 * 2.insert some text
100 	 * 3.set paragraph alignment is left
101 	 * 4.save and close the document
102 	 * 5.reload the saved document and check the paragraph alignment
103 	 */
104 	@Test
105 	public void testParagraphAlignmentLeft() 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 		xTextCursor.gotoStart(false);
115 		xTextCursor.goRight((short)180 , true);
116 		xCursorProps.setPropertyValue("ParaAdjust",com.sun.star.style.ParagraphAdjust.LEFT);
117 		//save to odt
118 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
119 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
120 		aStoreProperties_odt[0] = new PropertyValue();
121 		aStoreProperties_odt[1] = new PropertyValue();
122 		aStoreProperties_odt[0].Name = "Override";
123 		aStoreProperties_odt[0].Value = true;
124 		aStoreProperties_odt[1].Name = "FilterName";
125 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
126 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
127 		//save to doc
128 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
129 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
130 		aStoreProperties_doc[0] = new PropertyValue();
131 		aStoreProperties_doc[1] = new PropertyValue();
132 		aStoreProperties_doc[0].Name = "Override";
133 		aStoreProperties_doc[0].Value = true;
134 		aStoreProperties_doc[1].Name = "FilterName";
135 		aStoreProperties_doc[1].Value = "MS Word 97";
136 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
137 
138 		app.closeDocument(xTextDocument);
139 
140 		//reopen the odt document and assert paragraph alignment
141 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
142 		XTextCursor xTextCursor_assert_odt = assertDocument_odt.getText().createTextCursor();
143 		XPropertySet xCursorProps_assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor_assert_odt);
144 		//verify paragraph alignment property
145 		assertEquals("assert first paragraph alignment is left",(short)0,xCursorProps_assert_odt.getPropertyValue("ParaAdjust"));
146 		//reopen the doc document and assert paragraph alignment
147 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
148 		XTextCursor xTextCursor_assert_doc = assertDocument_doc.getText().createTextCursor();
149 		XPropertySet xCursorProps_assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor_assert_doc);
150 		//verify paragraph alignment property
151 		assertEquals("assert first paragraph alignment is left",(short)0,xCursorProps_assert_doc.getPropertyValue("ParaAdjust"));
152 
153 
154 	}
155 	/*
156 	 * test paragraph alignment is justified
157 	 * 1.new a text document
158 	 * 2.insert some text
159 	 * 3.set paragraph alignment is right
160 	 * 4.save and close the document
161 	 * 5.reload the saved document and check the paragraph alignment
162 	 */
163 	@Test
164 	public void testParagraphAlignmentRight() throws Exception {
165 
166 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
167 		xText = xTextDocument.getText();
168 		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!" +
169 				"Hello,world!Hello,world!");
170 		// create text cursor for selecting and formatting text
171 		XTextCursor xTextCursor = xText.createTextCursor();
172 		XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
173 		//apply paragraph alignment as justified and last line alignment
174 		xTextCursor.gotoStart(false);
175 		xTextCursor.goRight((short)180 , true);
176 		xCursorProps.setPropertyValue("ParaAdjust",com.sun.star.style.ParagraphAdjust.RIGHT);
177 		//save to odt
178 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
179 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
180 		aStoreProperties_odt[0] = new PropertyValue();
181 		aStoreProperties_odt[1] = new PropertyValue();
182 		aStoreProperties_odt[0].Name = "Override";
183 		aStoreProperties_odt[0].Value = true;
184 		aStoreProperties_odt[1].Name = "FilterName";
185 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
186 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
187 		//save to doc
188 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
189 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
190 		aStoreProperties_doc[0] = new PropertyValue();
191 		aStoreProperties_doc[1] = new PropertyValue();
192 		aStoreProperties_doc[0].Name = "Override";
193 		aStoreProperties_doc[0].Value = true;
194 		aStoreProperties_doc[1].Name = "FilterName";
195 		aStoreProperties_doc[1].Value = "MS Word 97";
196 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
197 		app.closeDocument(xTextDocument);
198 
199 		//reopen the document and assert paragraph alignment
200 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
201 		XTextCursor xTextCursor_assert_odt = assertDocument_odt.getText().createTextCursor();
202 		XPropertySet xCursorProps_assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor_assert_odt);
203 		//verify paragraph alignment property
204 		assertEquals("assert first paragraph alignment is right",(short)1,xCursorProps_assert_odt.getPropertyValue("ParaAdjust"));
205 		//reopen the document and assert paragraph alignment
206 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
207 		XTextCursor xTextCursor_assert_doc = assertDocument_doc.getText().createTextCursor();
208 		XPropertySet xCursorProps_assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor_assert_doc);
209 		//verify paragraph alignment property
210 		assertEquals("assert first paragraph alignment is right",(short)1,xCursorProps_assert_doc.getPropertyValue("ParaAdjust"));
211 	}
212 	/*
213 	 * test paragraph alignment is justified
214 	 * 1.new a text document
215 	 * 2.insert some text
216 	 * 3.set paragraph alignment is center
217 	 * 4.save and close the document
218 	 * 5.reload the saved document and check the paragraph alignment
219 	 */
220 	@Test
221 	public void testParagraphAlignmentCenter() throws Exception {
222 
223 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
224 		xText = xTextDocument.getText();
225 		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!" +
226 				"Hello,world!Hello,world!");
227 		// create text cursor for selecting and formatting text
228 		XTextCursor xTextCursor = xText.createTextCursor();
229 		XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
230 		//apply paragraph alignment as justified and last line alignment
231 		xTextCursor.gotoStart(false);
232 		xTextCursor.goRight((short)180 , true);
233 		xCursorProps.setPropertyValue("ParaAdjust",com.sun.star.style.ParagraphAdjust.CENTER);
234 		//save to odt
235 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
236 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
237 		aStoreProperties_odt[0] = new PropertyValue();
238 		aStoreProperties_odt[1] = new PropertyValue();
239 		aStoreProperties_odt[0].Name = "Override";
240 		aStoreProperties_odt[0].Value = true;
241 		aStoreProperties_odt[1].Name = "FilterName";
242 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
243 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
244 		//save to doc
245 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
246 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
247 		aStoreProperties_doc[0] = new PropertyValue();
248 		aStoreProperties_doc[1] = new PropertyValue();
249 		aStoreProperties_doc[0].Name = "Override";
250 		aStoreProperties_doc[0].Value = true;
251 		aStoreProperties_doc[1].Name = "FilterName";
252 		aStoreProperties_doc[1].Value = "MS Word 97";
253 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
254 
255 		app.closeDocument(xTextDocument);
256 
257 		//reopen the document and assert paragraph alignment
258 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
259 		XTextCursor xTextCursor_assert_odt = assertDocument_odt.getText().createTextCursor();
260 		XPropertySet xCursorProps_assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor_assert_odt);
261 		assertEquals("assert first paragraph alignment is center",(short)3,xCursorProps_assert_odt.getPropertyValue("ParaAdjust"));
262 		//reopen the document and assert paragraph alignment
263 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
264 		XTextCursor xTextCursor_assert_doc = assertDocument_doc.getText().createTextCursor();
265 		XPropertySet xCursorProps_assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor_assert_doc);
266 		assertEquals("assert first paragraph alignment is center",(short)3,xCursorProps_assert_doc.getPropertyValue("ParaAdjust"));
267 	}
268 }
269