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.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.style.NumberingType;
13 import com.sun.star.text.*;
14 import com.sun.star.beans.*;
15 import com.sun.star.container.XIndexAccess;
16 import com.sun.star.container.XIndexReplace;
17 import com.sun.star.frame.XStorable;
18 import com.sun.star.lang.XMultiServiceFactory;
19 import com.sun.star.uno.UnoRuntime;
20 
21 public class ParagraphNumberingAndBulletAlignment {
22 	private static final UnoApp app = new UnoApp();
23 	XText xText = null;
24 
25 	@Before
26 	public void setUp() throws Exception {
27 		app.start();
28 
29 	}
30 
31 	@After
32 	public void tearDown() throws Exception {
33 		app.close();
34 	}
35 	/*
36 	 * test paragraph background color
37 	 * 1.new a text document
38 	 * 2.insert some text
39 	 * 3.set paragraph numbering alignment
40 	 * 4.save and close the document
41 	 * 5.reload the saved document and check the paragraph numbering alignment
42 	 */
43 	@Test
44 	public void testNumberingBulletAlign_Right() throws Exception {
45 
46 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
47 		xText = xTextDocument.getText();
48 		xText.setString("we are Chinese,they are American.\nwe are all living in one earth!\nHello,world!Hello,world!Hello,world!\nHello,world!Hello,world!Hello,world!" +
49 				"Hello,world!Hello,world!");
50 		//create cursor to select paragraph and formating paragraph
51 		XTextCursor xTextCursor = xText.createTextCursor();
52 		//create paragraph property set
53 		XPropertySet xTextProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
54 		//create document service factory
55 		XMultiServiceFactory  xWriterFactory= (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
56 		//set numbering character
57 		XIndexAccess xNumRule = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class,xWriterFactory.createInstance("com.sun.star.text.NumberingRules"));
58 		PropertyValue[] propsRule = {new PropertyValue(),new PropertyValue()};
59 		propsRule[1].Name = "NumberingType";
60 		propsRule[1].Value = NumberingType.ARABIC;
61 		propsRule[0].Name = "Adjust";
62 		propsRule[0].Value = HoriOrientation.RIGHT;
63 		XIndexReplace xReplaceRule = (XIndexReplace) UnoRuntime.queryInterface(XIndexReplace.class, xNumRule);
64 		xReplaceRule.replaceByIndex(0, propsRule);
65 		//set paragraph numbering and bullet character
66 		xTextProps.setPropertyValue("NumberingRules", xNumRule);
67 		//save to odt
68 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
69 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
70 		aStoreProperties_odt[0] = new PropertyValue();
71 		aStoreProperties_odt[1] = new PropertyValue();
72 		aStoreProperties_odt[0].Name = "Override";
73 		aStoreProperties_odt[0].Value = true;
74 		aStoreProperties_odt[1].Name = "FilterName";
75 		aStoreProperties_odt[1].Value = "writer8";
76 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
77 		//save to doc
78 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
79 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
80 		aStoreProperties_doc[0] = new PropertyValue();
81 		aStoreProperties_doc[1] = new PropertyValue();
82 		aStoreProperties_doc[0].Name = "Override";
83 		aStoreProperties_doc[0].Value = true;
84 		aStoreProperties_doc[1].Name = "FilterName";
85 		aStoreProperties_doc[1].Value = "MS Word 97";
86 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
87 		app.closeDocument(xTextDocument);
88 
89 		//reopen the document
90 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
91 		XPropertySet xCursorProps_Assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_odt.getText().createTextCursor());
92 		XIndexAccess xNumRule_assert_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xCursorProps_Assert_odt.getPropertyValue("NumberingRules"));
93 		XIndexReplace xReplaceRule_assert_odt = (XIndexReplace) UnoRuntime.queryInterface(XIndexReplace.class, xNumRule_assert_odt);
94 		PropertyValue[] propsRule_assert_odt=(PropertyValue[]) UnoRuntime.queryInterface(PropertyValue[].class,xReplaceRule_assert_odt.getByIndex(0));
95 		//verify paragraph numbering and bullet alignment
96 		assertEquals("assert numbering and bullet","Adjust",propsRule_assert_odt[0].Name);
97 		assertEquals("assert numbering and bullet",HoriOrientation.RIGHT,propsRule_assert_odt[0].Value);
98 		assertEquals("assert numbering and bullet","NumberingType",propsRule_assert_odt[11].Name);
99 		assertEquals("assert numbering and bullet",NumberingType.ARABIC,propsRule_assert_odt[11].Value);
100 
101 		//reopen the document
102 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
103 		XPropertySet xCursorProps_Assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_doc.getText().createTextCursor());
104 		XIndexAccess xNumRule_assert_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xCursorProps_Assert_doc.getPropertyValue("NumberingRules"));
105 		PropertyValue[] propsRule_assert_doc=(PropertyValue[]) UnoRuntime.queryInterface(PropertyValue[].class,xNumRule_assert_doc.getByIndex(0));
106 		//verify paragraph numbering and bullet alignment
107 		assertEquals("assert numbering and bullet","Adjust",propsRule_assert_doc[0].Name);
108 		assertEquals("assert numbering and bullet",HoriOrientation.RIGHT,propsRule_assert_doc[0].Value);
109 		assertEquals("assert numbering and bullet","NumberingType",propsRule_assert_doc[11].Name);
110 		assertEquals("assert numbering and bullet",NumberingType.ARABIC,propsRule_assert_doc[11].Value);
111 	}
112 	@Test
113 	public void testNumberingBulletAlign_Left() throws Exception {
114 
115 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
116 		xText = xTextDocument.getText();
117 		xText.setString("we are Chinese,they are American.\nwe are all living in one earth!\nHello,world!Hello,world!Hello,world!\nHello,world!Hello,world!Hello,world!" +
118 				"Hello,world!Hello,world!");
119 		//create cursor to select paragraph and formating paragraph
120 		XTextCursor xTextCursor = xText.createTextCursor();
121 		//create paragraph property set
122 		XPropertySet xTextProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
123 		//create document service factory
124 		XMultiServiceFactory  xWriterFactory= (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
125 		//set numbering character
126 		XIndexAccess xNumRule = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class,xWriterFactory.createInstance("com.sun.star.text.NumberingRules"));
127 		PropertyValue[] propsRule = {new PropertyValue(),new PropertyValue()};
128 		propsRule[1].Name = "NumberingType";
129 		propsRule[1].Value = NumberingType.ARABIC;
130 		propsRule[0].Name = "Adjust";
131 		propsRule[0].Value = HoriOrientation.LEFT;
132 		XIndexReplace xReplaceRule = (XIndexReplace) UnoRuntime.queryInterface(XIndexReplace.class, xNumRule);
133 		xReplaceRule.replaceByIndex(0, propsRule);
134 		//set paragraph numbering and bullet character
135 		xTextProps.setPropertyValue("NumberingRules", xNumRule);
136 		//save to odt
137 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
138 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
139 		aStoreProperties_odt[0] = new PropertyValue();
140 		aStoreProperties_odt[1] = new PropertyValue();
141 		aStoreProperties_odt[0].Name = "Override";
142 		aStoreProperties_odt[0].Value = true;
143 		aStoreProperties_odt[1].Name = "FilterName";
144 		aStoreProperties_odt[1].Value = "writer8";
145 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
146 		//save to doc
147 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
148 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
149 		aStoreProperties_doc[0] = new PropertyValue();
150 		aStoreProperties_doc[1] = new PropertyValue();
151 		aStoreProperties_doc[0].Name = "Override";
152 		aStoreProperties_doc[0].Value = true;
153 		aStoreProperties_doc[1].Name = "FilterName";
154 		aStoreProperties_doc[1].Value = "MS Word 97";
155 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
156 		app.closeDocument(xTextDocument);
157 
158 		//reopen the document
159 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
160 		XPropertySet xCursorProps_Assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_odt.getText().createTextCursor());
161 		XIndexAccess xNumRule_assert_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xCursorProps_Assert_odt.getPropertyValue("NumberingRules"));
162 		XIndexReplace xReplaceRule_assert_odt = (XIndexReplace) UnoRuntime.queryInterface(XIndexReplace.class, xNumRule_assert_odt);
163 		PropertyValue[] propsRule_assert_odt=(PropertyValue[]) UnoRuntime.queryInterface(PropertyValue[].class,xReplaceRule_assert_odt.getByIndex(0));
164 		//verify paragraph numbering and bullet alignment
165 		assertEquals("assert numbering and bullet","Adjust",propsRule_assert_odt[0].Name);
166 		assertEquals("assert numbering and bullet",HoriOrientation.LEFT,propsRule_assert_odt[0].Value);
167 		assertEquals("assert numbering and bullet","NumberingType",propsRule_assert_odt[11].Name);
168 		assertEquals("assert numbering and bullet",NumberingType.ARABIC,propsRule_assert_odt[11].Value);
169 
170 		//reopen the document
171 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
172 		XPropertySet xCursorProps_Assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_doc.getText().createTextCursor());
173 		XIndexAccess xNumRule_assert_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xCursorProps_Assert_doc.getPropertyValue("NumberingRules"));
174 		PropertyValue[] propsRule_assert_doc=(PropertyValue[]) UnoRuntime.queryInterface(PropertyValue[].class,xNumRule_assert_doc.getByIndex(0));
175 		//verify paragraph numbering and bullet alignment
176 		assertEquals("assert numbering and bullet","Adjust",propsRule_assert_doc[0].Name);
177 		assertEquals("assert numbering and bullet",HoriOrientation.LEFT,propsRule_assert_doc[0].Value);
178 		assertEquals("assert numbering and bullet","NumberingType",propsRule_assert_doc[11].Name);
179 		assertEquals("assert numbering and bullet",NumberingType.ARABIC,propsRule_assert_doc[11].Value);
180 	}
181 	@Test
182 	public void testNumberingBulletAlign_Center() throws Exception {
183 
184 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
185 		xText = xTextDocument.getText();
186 		xText.setString("we are Chinese,they are American.\nwe are all living in one earth!\nHello,world!Hello,world!Hello,world!\nHello,world!Hello,world!Hello,world!" +
187 				"Hello,world!Hello,world!");
188 		//create cursor to select paragraph and formating paragraph
189 		XTextCursor xTextCursor = xText.createTextCursor();
190 		//create paragraph property set
191 		XPropertySet xTextProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
192 		//create document service factory
193 		XMultiServiceFactory  xWriterFactory= (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
194 		//set numbering character
195 		XIndexAccess xNumRule = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class,xWriterFactory.createInstance("com.sun.star.text.NumberingRules"));
196 		PropertyValue[] propsRule = {new PropertyValue(),new PropertyValue()};
197 		propsRule[1].Name = "NumberingType";
198 		propsRule[1].Value = NumberingType.ARABIC;
199 		propsRule[0].Name = "Adjust";
200 		propsRule[0].Value = HoriOrientation.CENTER;
201 		XIndexReplace xReplaceRule = (XIndexReplace) UnoRuntime.queryInterface(XIndexReplace.class, xNumRule);
202 		xReplaceRule.replaceByIndex(0, propsRule);
203 		//set paragraph numbering and bullet character
204 		xTextProps.setPropertyValue("NumberingRules", xNumRule);
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 = "writer8";
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 		XIndexAccess xNumRule_assert_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xCursorProps_Assert_odt.getPropertyValue("NumberingRules"));
231 		XIndexReplace xReplaceRule_assert_odt = (XIndexReplace) UnoRuntime.queryInterface(XIndexReplace.class, xNumRule_assert_odt);
232 		PropertyValue[] propsRule_assert_odt=(PropertyValue[]) UnoRuntime.queryInterface(PropertyValue[].class,xReplaceRule_assert_odt.getByIndex(0));
233 		//verify paragraph numbering and bullet alignment
234 		assertEquals("assert numbering and bullet","Adjust",propsRule_assert_odt[0].Name);
235 		assertEquals("assert numbering and bullet",HoriOrientation.CENTER,propsRule_assert_odt[0].Value);
236 		assertEquals("assert numbering and bullet","NumberingType",propsRule_assert_odt[11].Name);
237 		assertEquals("assert numbering and bullet",NumberingType.ARABIC,propsRule_assert_odt[11].Value);
238 
239 		//reopen the document
240 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
241 		XPropertySet xCursorProps_Assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_doc.getText().createTextCursor());
242 		XIndexAccess xNumRule_assert_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xCursorProps_Assert_doc.getPropertyValue("NumberingRules"));
243 		PropertyValue[] propsRule_assert_doc=(PropertyValue[]) UnoRuntime.queryInterface(PropertyValue[].class,xNumRule_assert_doc.getByIndex(0));
244 		//verify paragraph numbering and bullet alignment
245 		assertEquals("assert numbering and bullet","Adjust",propsRule_assert_doc[0].Name);
246 		assertEquals("assert numbering and bullet",HoriOrientation.CENTER,propsRule_assert_doc[0].Value);
247 		assertEquals("assert numbering and bullet","NumberingType",propsRule_assert_doc[11].Name);
248 		assertEquals("assert numbering and bullet",NumberingType.ARABIC,propsRule_assert_doc[11].Value);
249 	}
250 }
251