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 
33 import com.sun.star.text.*;
34 import com.sun.star.beans.*;
35 import com.sun.star.container.XIndexAccess;
36 import com.sun.star.container.XIndexReplace;
37 import com.sun.star.frame.XStorable;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.uno.UnoRuntime;
40 
41 public class ParagraphNumberingAndBullet_Number_BeforeAndAfterCharacter {
42     private static final UnoApp app = new UnoApp();
43     XText xText = null;
44 
45     @Before
setUp()46     public void setUp() throws Exception {
47         app.start();
48 
49     }
50 
51     @After
tearDown()52     public void tearDown() throws Exception {
53         app.close();
54     }
55     /*
56      * test paragraph background color
57      * 1.new a text document
58      * 2.insert some text
59      * 3.set paragraph numbering before and after character
60      * 4.save and close the document
61      * 5.reload the saved document and check the paragraph numbering before and after character
62      */
63     @Test
testNumberingBulletBeforeAndAfterCharacter()64     public void testNumberingBulletBeforeAndAfterCharacter() throws Exception {
65 
66         XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
67         xText = xTextDocument.getText();
68         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!" +
69                 "Hello,world!Hello,world!");
70         //create cursor to select paragraph and formating paragraph
71         XTextCursor xTextCursor = xText.createTextCursor();
72         //create paragraph property set
73         XPropertySet xTextProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
74         //create document service factory
75         XMultiServiceFactory  xWriterFactory= (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
76         //set numbering character
77         XIndexAccess xNumRule = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class,xWriterFactory.createInstance("com.sun.star.text.NumberingRules"));
78         PropertyValue[] propsRule = {new PropertyValue(),new PropertyValue()};
79         propsRule[1].Name = "Prefix";
80         propsRule[1].Value = "#$%&";
81         propsRule[0].Name = "Suffix";
82         propsRule[0].Value = "abcd";
83         XIndexReplace xReplaceRule = (XIndexReplace) UnoRuntime.queryInterface(XIndexReplace.class, xNumRule);
84         xReplaceRule.replaceByIndex(0, propsRule);
85         //set paragraph numbering and bullet character
86         xTextProps.setPropertyValue("NumberingRules", xNumRule);
87         //save to odt
88         XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
89         PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
90         aStoreProperties_odt[0] = new PropertyValue();
91         aStoreProperties_odt[1] = new PropertyValue();
92         aStoreProperties_odt[0].Name = "Override";
93         aStoreProperties_odt[0].Value = true;
94         aStoreProperties_odt[1].Name = "FilterName";
95         aStoreProperties_odt[1].Value = "writer8";
96         xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
97         //save to doc
98         XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
99         PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
100         aStoreProperties_doc[0] = new PropertyValue();
101         aStoreProperties_doc[1] = new PropertyValue();
102         aStoreProperties_doc[0].Name = "Override";
103         aStoreProperties_doc[0].Value = true;
104         aStoreProperties_doc[1].Name = "FilterName";
105         aStoreProperties_doc[1].Value = "MS Word 97";
106         xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
107         app.closeDocument(xTextDocument);
108 
109         //reopen the document
110         XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
111         XPropertySet xCursorProps_Assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_odt.getText().createTextCursor());
112         XIndexAccess xNumRule_assert_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xCursorProps_Assert_odt.getPropertyValue("NumberingRules"));
113         XIndexReplace xReplaceRule_assert_odt = (XIndexReplace) UnoRuntime.queryInterface(XIndexReplace.class, xNumRule_assert_odt);
114         PropertyValue[] propsRule_assert_odt=(PropertyValue[]) UnoRuntime.queryInterface(PropertyValue[].class,xReplaceRule_assert_odt.getByIndex(0));
115         //verify paragraph numbering and bullet alignment
116         assertEquals("assert numbering and bullet","Prefix",propsRule_assert_odt[2].Name);
117         assertEquals("assert numbering and bullet","#$%&",propsRule_assert_odt[2].Value);
118         assertEquals("assert numbering and bullet","Suffix",propsRule_assert_odt[3].Name);
119         assertEquals("assert numbering and bullet","abcd",propsRule_assert_odt[3].Value);
120 
121         //reopen the document
122         XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
123         XPropertySet xCursorProps_Assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_doc.getText().createTextCursor());
124         XIndexAccess xNumRule_assert_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xCursorProps_Assert_doc.getPropertyValue("NumberingRules"));
125         PropertyValue[] propsRule_assert_doc=(PropertyValue[]) UnoRuntime.queryInterface(PropertyValue[].class,xNumRule_assert_doc.getByIndex(0));
126         //verify paragraph numbering and bullet alignment
127         assertEquals("assert numbering and bullet","Prefix",propsRule_assert_doc[2].Name);
128         assertEquals("assert numbering and bullet","#$%&",propsRule_assert_doc[2].Value);
129         assertEquals("assert numbering and bullet","Suffix",propsRule_assert_doc[3].Name);
130         assertEquals("assert numbering and bullet","abcd",propsRule_assert_doc[3].Value);
131     }
132 }
133