xref: /AOO41X/test/testuno/source/fvt/uno/sd/paragraph/ParagraphStyle.java (revision b832fe9ee96f9a0260deab239932d14d0af31494)
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 package fvt.uno.sd.paragraph;
22 
23 import junit.framework.Assert;
24 
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.openoffice.test.common.FileUtil;
29 import org.openoffice.test.common.Testspace;
30 import org.openoffice.test.uno.UnoApp;
31 
32 import testlib.uno.PageUtil;
33 import testlib.uno.ShapeUtil;
34 
35 import com.sun.star.awt.Point;
36 import com.sun.star.awt.Size;
37 import com.sun.star.beans.PropertyValue;
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.drawing.TextFitToSizeType;
40 import com.sun.star.drawing.XDrawPage;
41 import com.sun.star.drawing.XDrawPages;
42 import com.sun.star.drawing.XDrawPagesSupplier;
43 import com.sun.star.drawing.XShape;
44 import com.sun.star.drawing.XShapes;
45 import com.sun.star.frame.XStorable;
46 import com.sun.star.lang.XComponent;
47 import com.sun.star.lang.XMultiServiceFactory;
48 import com.sun.star.presentation.XPresentation;
49 import com.sun.star.presentation.XPresentationSupplier;
50 import com.sun.star.style.LineSpacing;
51 import com.sun.star.style.LineSpacingMode;
52 import com.sun.star.style.ParagraphAdjust;
53 import com.sun.star.text.ControlCharacter;
54 import com.sun.star.text.XText;
55 import com.sun.star.text.XTextCursor;
56 import com.sun.star.text.XTextRange;
57 import com.sun.star.uno.UnoRuntime;
58 
59 public class ParagraphStyle {
60     XPresentationSupplier sdDocument = null;
61     XPresentation pre = null;
62     XComponent precomp = null;
63     XComponent impressDocument = null;
64     XComponent reLoadFile = null;
65     XDrawPagesSupplier drawsupplier = null;
66     XDrawPages drawpages = null;
67     XShapes xShapes = null;
68     XDrawPage xpage = null;
69     String filePath=null;
70 
71     UnoApp unoApp = new UnoApp();
72 
73     /**
74      * @throws java.lang.Exception
75      */
76     @Before
setUp()77     public void setUp() throws Exception {
78         unoApp.start();
79         createDocumentAndSlide();
80     }
81 
82     @After
tearDown()83     public void tearDown() throws Exception {
84         unoApp.closeDocument(impressDocument);
85         unoApp.closeDocument(reLoadFile);
86         unoApp.close();
87     }
88 
89     @Test
ParaStyle()90     public void ParaStyle() throws Exception {
91         Point po = new Point(5000, 5000);
92         xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
93         // create the shape
94         XShape xRectangle = ShapeUtil.createShape(impressDocument, po, new Size(21000, 12500), "com.sun.star.drawing.RectangleShape");
95          xShapes.add(xRectangle);
96          XPropertySet xShapePropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xRectangle);
97          // TextFitToSize
98          xShapePropSet.setPropertyValue("TextFitToSize", TextFitToSizeType.PROPORTIONAL);
99 
100          XPropertySet xTextPropSet1 = addPortion(xRectangle, "New text paragraph", true);
101          xTextPropSet1.setPropertyValue("ParaAdjust", ParagraphAdjust.CENTER);
102 
103          //Line Spacing
104          LineSpacing xLineSpacing = new LineSpacing(LineSpacingMode.LEADING, (short)1);
105          xTextPropSet1.setPropertyValue("ParaLineSpacing",  xLineSpacing);
106 
107          //left, right, top and bottom margin
108          xTextPropSet1.setPropertyValue("ParaLeftMargin",  1000);
109          xTextPropSet1.setPropertyValue("ParaRightMargin",  1000);
110          xTextPropSet1.setPropertyValue("ParaTopMargin",  1000);
111          xTextPropSet1.setPropertyValue("ParaBottomMargin",  1000);
112 
113          XPropertySet xTextPropSet2 = addPortion(xRectangle, "And another text paragraph", true);
114          xTextPropSet2.setPropertyValue("CharColor", new Integer(0xff0000));
115 
116          xRectangle = saveAndLoadShape(1, 0);
117 
118 
119          Assert.assertEquals("Paragraph Left Margin is 1000",1000, xTextPropSet1.getPropertyValue("ParaLeftMargin"));
120          Assert.assertEquals("Paragraph Right Margin is 1000", 1000,xTextPropSet1.getPropertyValue("ParaRightMargin"));
121          Assert.assertEquals("Paragraph Top Margin is 1000",1000, xTextPropSet1.getPropertyValue("ParaTopMargin") );
122          Assert.assertEquals("Paragraph Bottom Margin is 1000 ",1000, xTextPropSet1.getPropertyValue("ParaBottomMargin"));
123          Assert.assertEquals("Text Color is red",0xff0000,xTextPropSet2.getPropertyValue("CharColor"));
124 
125     }
126 
127 
createShape(XComponent xComponent, int x, int y, int width, int height, String sShapeType)128     public static XShape createShape(XComponent xComponent, int x, int y,
129             int width, int height, String sShapeType)
130             throws java.lang.Exception {
131         // query the document for the document-internal service factory
132         XMultiServiceFactory xFactory = (XMultiServiceFactory) UnoRuntime
133                 .queryInterface(XMultiServiceFactory.class, xComponent);
134 
135         // get the given Shape service from the factory
136         Object xObj = xFactory.createInstance(sShapeType);
137         Point aPos = new Point(x, y);
138         Size aSize = new Size(width, height);
139 
140         // use its XShape interface to determine position and size before
141         // insertion
142         XShape xShape = (XShape) UnoRuntime.queryInterface(XShape.class, xObj);
143         xShape.setPosition(aPos);
144         xShape.setSize(aSize);
145         return xShape;
146     }
147 
addPortion(XShape xShape, String sText, boolean bNewParagraph)148     public static XPropertySet addPortion(XShape xShape, String sText, boolean bNewParagraph)
149              throws com.sun.star.lang.IllegalArgumentException {
150          XText xText = (XText)UnoRuntime.queryInterface(XText.class, xShape);
151          XTextCursor xTextCursor = xText.createTextCursor();
152          xTextCursor.gotoEnd(false);
153          if (bNewParagraph) {
154              xText.insertControlCharacter(xTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);
155              xTextCursor.gotoEnd(false);
156          }
157          XTextRange xTextRange = (XTextRange)UnoRuntime.queryInterface(XTextRange.class, xTextCursor);
158          xTextRange.setString(sText);
159          xTextCursor.gotoEnd(true);
160          XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextRange);
161          return xPropSet;
162      }
163 
164     /**
165      * create a new presentation document and insert a new slide.
166      *
167      * @throws Exception
168      */
createDocumentAndSlide()169     public void createDocumentAndSlide() throws Exception {
170         impressDocument = (XComponent) UnoRuntime.queryInterface(
171                 XComponent.class, unoApp.newDocument("simpress"));
172         drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
173                 XDrawPagesSupplier.class, impressDocument);
174         drawpages = drawsupplier.getDrawPages();
175         drawpages.insertNewByIndex(1);
176         xpage = PageUtil.getDrawPageByIndex(impressDocument, 1);
177     }
178 
179     /**
180      * Save presentation and reLoad the presentation and shape in it.
181      *
182      * @param po
183      * @param shapeType
184      * @return
185      * @throws Exception
186      */
saveAndLoadShape(int pageIndex, int shapeIndex)187     public XShape saveAndLoadShape(int pageIndex, int shapeIndex) throws Exception {
188         reLoadFile = saveAndReloadDoc(impressDocument,
189                 "impress8", "odp");
190         xShapes=ShapeUtil.getShapes(reLoadFile, pageIndex);
191         return  (XShape) UnoRuntime.queryInterface(XShape.class, xShapes.getByIndex(shapeIndex));
192     }
193 
194     /**
195      * save and reload Presentation document.
196      *
197      * @param presentationDocument
198      * @param sFilter
199      * @param sExtension
200      * @return
201      * @throws Exception
202      */
saveAndReloadDoc(XComponent presentationDocument, String sFilter, String sExtension)203     private XComponent saveAndReloadDoc(XComponent presentationDocument,
204             String sFilter, String sExtension) throws Exception {
205         filePath = Testspace.getPath("tmp/paragraphstyle."
206                 + sExtension);
207         PropertyValue[] aStoreProperties = new PropertyValue[2];
208         aStoreProperties[0] = new PropertyValue();
209         aStoreProperties[1] = new PropertyValue();
210         aStoreProperties[0].Name = "Override";
211         aStoreProperties[0].Value = true;
212         aStoreProperties[1].Name = "FilterName";
213         aStoreProperties[1].Value = sFilter;
214         XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
215                 XStorable.class, presentationDocument);
216         xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
217 
218         return (XComponent) UnoRuntime.queryInterface(XComponent.class,
219                 unoApp.loadDocument(filePath));
220     }
221 }
222