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.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 import com.sun.star.text.*;
34 import com.sun.star.beans.*;
35 import com.sun.star.frame.XStorable;
36 import com.sun.star.uno.UnoRuntime;
37 
38 public class ParagraphIndentAndSpacing {
39 	private static final UnoApp app = new UnoApp();
40 	XText xText = null;
41 
42 	@Before
setUp()43 	public void setUp() throws Exception {
44 		app.start();
45 
46 	}
47 
48 	@After
tearDown()49 	public void tearDown() throws Exception {
50 		app.close();
51 	}
52     /*
53      * test paragraph spacing
54      * 1.new a text document
55      * 2.insert some text
56      * 3.set paragraph spacing:before text,after text,above paragraph,below paragraph
57      * 4.save to odt and close it
58      * 5.reopen the saved document and check the paragraph spacing
59      */
60 	@Test
testParagraphSpacingSetting()61 	public void testParagraphSpacingSetting() throws Exception {
62 
63 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
64 		xText = xTextDocument.getText();
65 		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!" +
66 				"Hello,world!Hello,world!");
67 		// create text cursor for selecting and formatting text
68 		XTextCursor xTextCursor = xText.createTextCursor();
69 		XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
70 		//set paragraph margin with page border
71 		xCursorProps.setPropertyValue("ParaLeftMargin",2000);
72 		xCursorProps.setPropertyValue("ParaRightMargin",3000);
73 		xCursorProps.setPropertyValue("ParaTopMargin",1000);
74 		xCursorProps.setPropertyValue("ParaBottomMargin",4000);
75 		xCursorProps.setPropertyValue("ParaFirstLineIndent",4000);
76 
77 		//save to odt
78 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
79 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
80 		aStoreProperties_odt[0] = new PropertyValue();
81 		aStoreProperties_odt[1] = new PropertyValue();
82 		aStoreProperties_odt[0].Name = "Override";
83 		aStoreProperties_odt[0].Value = true;
84 		aStoreProperties_odt[1].Name = "FilterName";
85 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
86 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
87 		//save to doc
88 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
89 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
90 		aStoreProperties_doc[0] = new PropertyValue();
91 		aStoreProperties_doc[1] = new PropertyValue();
92 		aStoreProperties_doc[0].Name = "Override";
93 		aStoreProperties_doc[0].Value = true;
94 		aStoreProperties_doc[1].Name = "FilterName";
95 		aStoreProperties_doc[1].Value = "MS Word 97";
96 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
97 		app.closeDocument(xTextDocument);
98 
99 		//reopen the document and assert line spacing
100 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
101 		XPropertySet xCursorProps_Assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_odt.getText().createTextCursor());
102 		//verify paragraph indent and spacing
103 		assertEquals("assert before text margin",2000,xCursorProps_Assert_odt.getPropertyValue("ParaLeftMargin"));
104 		assertEquals("assert after text margin",3000,xCursorProps_Assert_odt.getPropertyValue("ParaRightMargin"));
105 		assertEquals("assert above paragraph margin",1000,xCursorProps_Assert_odt.getPropertyValue("ParaTopMargin"));
106 		assertEquals("assert below paragraph margin",4001,xCursorProps_Assert_odt.getPropertyValue("ParaBottomMargin"));
107 		assertEquals("assert first line indent",4001,xCursorProps_Assert_odt.getPropertyValue("ParaFirstLineIndent"));
108 		assertEquals("assert paragraph first line is no autoindent",false,xCursorProps_Assert_odt.getPropertyValue("ParaIsAutoFirstLineIndent"));
109 
110 		//reopen the document and assert line spacing
111 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
112 		XPropertySet xCursorProps_Assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_doc.getText().createTextCursor());
113 		//verify paragraph indent and spacing
114 		assertEquals("assert before text margin",2000,xCursorProps_Assert_doc.getPropertyValue("ParaLeftMargin"));
115 		assertEquals("assert after text margin",3000,xCursorProps_Assert_doc.getPropertyValue("ParaRightMargin"));
116 		assertEquals("assert above paragraph margin",1000,xCursorProps_Assert_doc.getPropertyValue("ParaTopMargin"));
117 		assertEquals("assert below paragraph margin",4001,xCursorProps_Assert_doc.getPropertyValue("ParaBottomMargin"));
118 		assertEquals("assert first line indent",4001,xCursorProps_Assert_doc.getPropertyValue("ParaFirstLineIndent"));
119 		assertEquals("assert paragraph first line is no autoindent",false,xCursorProps_Assert_doc.getPropertyValue("ParaIsAutoFirstLineIndent"));
120 	}
121 	@Test@Ignore("Bug #120646 - [testUNO patch]the auto indent effect of first line lost when save to doc")
testParagraphIndent()122 	public void testParagraphIndent() throws Exception {
123 
124 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
125 		xText = xTextDocument.getText();
126 		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!" +
127 				"Hello,world!Hello,world!");
128 		// create text cursor for selecting and formatting text
129 		XTextCursor xTextCursor = xText.createTextCursor();
130 		XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
131 		//set paragraph margin with page border
132 		xCursorProps.setPropertyValue("ParaIsAutoFirstLineIndent",true);
133 
134 		//save to odt
135 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
136 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
137 		aStoreProperties_odt[0] = new PropertyValue();
138 		aStoreProperties_odt[1] = new PropertyValue();
139 		aStoreProperties_odt[0].Name = "Override";
140 		aStoreProperties_odt[0].Value = true;
141 		aStoreProperties_odt[1].Name = "FilterName";
142 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
143 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
144 		//save to doc
145 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
146 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
147 		aStoreProperties_doc[0] = new PropertyValue();
148 		aStoreProperties_doc[1] = new PropertyValue();
149 		aStoreProperties_doc[0].Name = "Override";
150 		aStoreProperties_doc[0].Value = true;
151 		aStoreProperties_doc[1].Name = "FilterName";
152 		aStoreProperties_doc[1].Value = "MS Word 97";
153 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
154 		app.closeDocument(xTextDocument);
155 
156 
157 		//reopen the document and assert paragraph indent
158 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
159 		XPropertySet xCursorProps_Assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_doc.getText().createTextCursor());
160 		//verify paragraph auto indent
161 		assertEquals("assert paragraph first line is autoindent",true,xCursorProps_Assert_doc.getPropertyValue("ParaIsAutoFirstLineIndent"));
162 
163 		//reopen the document and assert paragraph indent
164 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
165 		XPropertySet xCursorProps_Assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_odt.getText().createTextCursor());
166 		//verify paragraph auto indent
167 		assertEquals("assert paragraph first line is autoindent",true,xCursorProps_Assert_odt.getPropertyValue("ParaIsAutoFirstLineIndent"));
168 	}
169 
170 }
171