xref: /trunk/test/testuno/source/fvt/uno/sd/paragraph/ParagraphTextProperty.java (revision 42a976b737d4e2b88cfbe2fe1cd610fe957bdc84)
1*b832fe9eSHerbert Dürr /**************************************************************
2*b832fe9eSHerbert Dürr  *
3*b832fe9eSHerbert Dürr  * Licensed to the Apache Software Foundation (ASF) under one
4*b832fe9eSHerbert Dürr  * or more contributor license agreements.  See the NOTICE file
5*b832fe9eSHerbert Dürr  * distributed with this work for additional information
6*b832fe9eSHerbert Dürr  * regarding copyright ownership.  The ASF licenses this file
7*b832fe9eSHerbert Dürr  * to you under the Apache License, Version 2.0 (the
8*b832fe9eSHerbert Dürr  * "License"); you may not use this file except in compliance
9*b832fe9eSHerbert Dürr  * with the License.  You may obtain a copy of the License at
10*b832fe9eSHerbert Dürr  *
11*b832fe9eSHerbert Dürr  *   http://www.apache.org/licenses/LICENSE-2.0
12*b832fe9eSHerbert Dürr  *
13*b832fe9eSHerbert Dürr  * Unless required by applicable law or agreed to in writing,
14*b832fe9eSHerbert Dürr  * software distributed under the License is distributed on an
15*b832fe9eSHerbert Dürr  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b832fe9eSHerbert Dürr  * KIND, either express or implied.  See the License for the
17*b832fe9eSHerbert Dürr  * specific language governing permissions and limitations
18*b832fe9eSHerbert Dürr  * under the License.
19*b832fe9eSHerbert Dürr  *
20*b832fe9eSHerbert Dürr  *************************************************************/
2122a547feSLi Feng Wang package fvt.uno.sd.paragraph;
2222a547feSLi Feng Wang 
2322a547feSLi Feng Wang import junit.framework.Assert;
2422a547feSLi Feng Wang 
2522a547feSLi Feng Wang import org.junit.After;
2622a547feSLi Feng Wang import org.junit.Before;
2722a547feSLi Feng Wang import org.junit.Test;
2822a547feSLi Feng Wang import org.openoffice.test.common.FileUtil;
2922a547feSLi Feng Wang import org.openoffice.test.common.Testspace;
3022a547feSLi Feng Wang import org.openoffice.test.uno.UnoApp;
3122a547feSLi Feng Wang 
3222a547feSLi Feng Wang import testlib.uno.PageUtil;
3322a547feSLi Feng Wang import testlib.uno.ShapeUtil;
3422a547feSLi Feng Wang 
3522a547feSLi Feng Wang import com.sun.star.awt.Point;
3622a547feSLi Feng Wang import com.sun.star.awt.Size;
3722a547feSLi Feng Wang import com.sun.star.beans.PropertyValue;
3822a547feSLi Feng Wang import com.sun.star.beans.XPropertySet;
3922a547feSLi Feng Wang import com.sun.star.drawing.TextFitToSizeType;
4022a547feSLi Feng Wang import com.sun.star.drawing.XDrawPage;
4122a547feSLi Feng Wang import com.sun.star.drawing.XDrawPages;
4222a547feSLi Feng Wang import com.sun.star.drawing.XDrawPagesSupplier;
4322a547feSLi Feng Wang import com.sun.star.drawing.XShape;
4422a547feSLi Feng Wang import com.sun.star.drawing.XShapes;
4522a547feSLi Feng Wang import com.sun.star.frame.XStorable;
4622a547feSLi Feng Wang import com.sun.star.lang.XComponent;
4722a547feSLi Feng Wang 
4822a547feSLi Feng Wang import com.sun.star.presentation.XPresentation;
4922a547feSLi Feng Wang import com.sun.star.presentation.XPresentationSupplier;
5022a547feSLi Feng Wang 
5122a547feSLi Feng Wang import com.sun.star.text.ControlCharacter;
5222a547feSLi Feng Wang import com.sun.star.text.XText;
5322a547feSLi Feng Wang import com.sun.star.text.XTextCursor;
5422a547feSLi Feng Wang import com.sun.star.text.XTextRange;
5522a547feSLi Feng Wang import com.sun.star.uno.UnoRuntime;
5622a547feSLi Feng Wang 
5722a547feSLi Feng Wang public class ParagraphTextProperty {
5822a547feSLi Feng Wang     XPresentationSupplier sdDocument = null;
5922a547feSLi Feng Wang     XPresentation pre = null;
6022a547feSLi Feng Wang     XComponent precomp = null;
6122a547feSLi Feng Wang     XComponent impressDocument = null;
6222a547feSLi Feng Wang     XComponent reLoadFile = null;
6322a547feSLi Feng Wang     XDrawPagesSupplier drawsupplier = null;
6422a547feSLi Feng Wang     XDrawPages drawpages = null;
6522a547feSLi Feng Wang     XShapes xShapes = null;
6622a547feSLi Feng Wang     XDrawPage xpage = null;
6722a547feSLi Feng Wang     String filePath=null;
6822a547feSLi Feng Wang 
6922a547feSLi Feng Wang     UnoApp unoApp = new UnoApp();
7022a547feSLi Feng Wang 
7122a547feSLi Feng Wang     /**
7222a547feSLi Feng Wang      * @throws java.lang.Exception
7322a547feSLi Feng Wang      */
7422a547feSLi Feng Wang     @Before
setUp()7522a547feSLi Feng Wang     public void setUp() throws Exception {
7622a547feSLi Feng Wang         unoApp.start();
7722a547feSLi Feng Wang         createDocumentAndSlide();
7822a547feSLi Feng Wang     }
7922a547feSLi Feng Wang 
8022a547feSLi Feng Wang     @After
tearDown()8122a547feSLi Feng Wang     public void tearDown() throws Exception {
8222a547feSLi Feng Wang         unoApp.closeDocument(impressDocument);
8322a547feSLi Feng Wang         unoApp.closeDocument(reLoadFile);
8422a547feSLi Feng Wang         unoApp.close();
8522a547feSLi Feng Wang     }
8622a547feSLi Feng Wang 
8722a547feSLi Feng Wang     @Test
testParagraphPropertyShape()8822a547feSLi Feng Wang     public void testParagraphPropertyShape() throws Exception {
8922a547feSLi Feng Wang          Point po = new Point(5000, 5000);
9022a547feSLi Feng Wang          xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
9122a547feSLi Feng Wang          // create the shape
9222a547feSLi Feng Wang          XShape xRectangle = ShapeUtil.createShape(impressDocument, po, new Size(21000, 12500), "com.sun.star.drawing.RectangleShape");
9322a547feSLi Feng Wang          xShapes.add(xRectangle);
9422a547feSLi Feng Wang 
9522a547feSLi Feng Wang          XPropertySet xShapePropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xRectangle);
9622a547feSLi Feng Wang          // TextFitToSize
9722a547feSLi Feng Wang          xShapePropSet.setPropertyValue("TextFitToSize", TextFitToSizeType.PROPORTIONAL);
9822a547feSLi Feng Wang          // border size
9922a547feSLi Feng Wang          xShapePropSet.setPropertyValue("TextLeftDistance", new Integer(2500));
10022a547feSLi Feng Wang          xShapePropSet.setPropertyValue("TextRightDistance", new Integer(2500));
10122a547feSLi Feng Wang          xShapePropSet.setPropertyValue("TextUpperDistance", new Integer(2500));
10222a547feSLi Feng Wang          xShapePropSet.setPropertyValue("TextLowerDistance", new Integer(2500));
10322a547feSLi Feng Wang          XPropertySet xTextPropSet = addPortion(xRectangle, "using TextFitToSize", false);
10422a547feSLi Feng Wang          xTextPropSet = addPortion(xRectangle, "and a Border distance of 2,5 cm", true);
10522a547feSLi Feng Wang 
10622a547feSLi Feng Wang          xRectangle = saveAndLoadShape(1,0);
10722a547feSLi Feng Wang 
10822a547feSLi Feng Wang          Assert.assertEquals("TextLeftDistance is 2500", 2500, xShapePropSet.getPropertyValue("TextLeftDistance"));
10922a547feSLi Feng Wang          Assert.assertEquals("TextRightDistance is 2500", 2500, xShapePropSet.getPropertyValue("TextRightDistance"));
11022a547feSLi Feng Wang          Assert.assertEquals("TextUpperDistance is 2500", 2500, xShapePropSet.getPropertyValue("TextUpperDistance"));
11122a547feSLi Feng Wang          Assert.assertEquals("TextLowerDistance is 2500", 2500, xShapePropSet.getPropertyValue("TextLowerDistance"));
11222a547feSLi Feng Wang 
11322a547feSLi Feng Wang     }
11422a547feSLi Feng Wang 
addPortion(XShape xShape, String sText, boolean bNewParagraph)11522a547feSLi Feng Wang     public static XPropertySet addPortion(XShape xShape, String sText, boolean bNewParagraph)
11622a547feSLi Feng Wang              throws com.sun.star.lang.IllegalArgumentException {
11722a547feSLi Feng Wang          XText xText = (XText)UnoRuntime.queryInterface(XText.class, xShape);
11822a547feSLi Feng Wang          XTextCursor xTextCursor = xText.createTextCursor();
11922a547feSLi Feng Wang          xTextCursor.gotoEnd(false);
12022a547feSLi Feng Wang          if (bNewParagraph) {
12122a547feSLi Feng Wang              xText.insertControlCharacter(xTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);
12222a547feSLi Feng Wang              xTextCursor.gotoEnd(false);
12322a547feSLi Feng Wang          }
12422a547feSLi Feng Wang          XTextRange xTextRange = (XTextRange)UnoRuntime.queryInterface(XTextRange.class, xTextCursor);
12522a547feSLi Feng Wang          xTextRange.setString(sText);
12622a547feSLi Feng Wang          xTextCursor.gotoEnd(true);
12722a547feSLi Feng Wang          XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextRange);
12822a547feSLi Feng Wang          return xPropSet;
12922a547feSLi Feng Wang      }
13022a547feSLi Feng Wang 
13122a547feSLi Feng Wang     /**
13222a547feSLi Feng Wang      * create a new presentation document and insert a new slide.
13322a547feSLi Feng Wang      *
13422a547feSLi Feng Wang      * @throws Exception
13522a547feSLi Feng Wang      */
createDocumentAndSlide()13622a547feSLi Feng Wang     public void createDocumentAndSlide() throws Exception {
13722a547feSLi Feng Wang         impressDocument = (XComponent) UnoRuntime.queryInterface(
13822a547feSLi Feng Wang                 XComponent.class, unoApp.newDocument("simpress"));
13922a547feSLi Feng Wang         drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
14022a547feSLi Feng Wang                 XDrawPagesSupplier.class, impressDocument);
14122a547feSLi Feng Wang         drawpages = drawsupplier.getDrawPages();
14222a547feSLi Feng Wang         drawpages.insertNewByIndex(1);
14322a547feSLi Feng Wang         xpage = PageUtil.getDrawPageByIndex(impressDocument, 1);
14422a547feSLi Feng Wang     }
14522a547feSLi Feng Wang 
14622a547feSLi Feng Wang     /**
14722a547feSLi Feng Wang      * Save presentation and reLoad the presentation and shape in it.
14822a547feSLi Feng Wang      *
14922a547feSLi Feng Wang      * @param po
15022a547feSLi Feng Wang      * @param shapeType
15122a547feSLi Feng Wang      * @return
15222a547feSLi Feng Wang      * @throws Exception
15322a547feSLi Feng Wang      */
saveAndLoadShape(int pageIndex, int shapeIndex)15422a547feSLi Feng Wang     public XShape saveAndLoadShape(int pageIndex, int shapeIndex) throws Exception {
15522a547feSLi Feng Wang         reLoadFile = saveAndReloadDoc(impressDocument,
15622a547feSLi Feng Wang                 "impress8", "odp");
15722a547feSLi Feng Wang         xShapes=ShapeUtil.getShapes(reLoadFile, pageIndex);
15822a547feSLi Feng Wang         return  (XShape) UnoRuntime.queryInterface(XShape.class, xShapes.getByIndex(shapeIndex));
15922a547feSLi Feng Wang     }
16022a547feSLi Feng Wang 
16122a547feSLi Feng Wang     /**
16222a547feSLi Feng Wang      * save and reload Presentation document.
16322a547feSLi Feng Wang      *
16422a547feSLi Feng Wang      * @param presentationDocument
16522a547feSLi Feng Wang      * @param sFilter
16622a547feSLi Feng Wang      * @param sExtension
16722a547feSLi Feng Wang      * @return
16822a547feSLi Feng Wang      * @throws Exception
16922a547feSLi Feng Wang      */
saveAndReloadDoc(XComponent presentationDocument, String sFilter, String sExtension)17022a547feSLi Feng Wang     private XComponent saveAndReloadDoc(XComponent presentationDocument,
17122a547feSLi Feng Wang             String sFilter, String sExtension) throws Exception {
17222a547feSLi Feng Wang         filePath = Testspace.getPath("tmp/paragraphtextproperty."
17322a547feSLi Feng Wang                 + sExtension);
17422a547feSLi Feng Wang         PropertyValue[] aStoreProperties = new PropertyValue[2];
17522a547feSLi Feng Wang         aStoreProperties[0] = new PropertyValue();
17622a547feSLi Feng Wang         aStoreProperties[1] = new PropertyValue();
17722a547feSLi Feng Wang         aStoreProperties[0].Name = "Override";
17822a547feSLi Feng Wang         aStoreProperties[0].Value = true;
17922a547feSLi Feng Wang         aStoreProperties[1].Name = "FilterName";
18022a547feSLi Feng Wang         aStoreProperties[1].Value = sFilter;
18122a547feSLi Feng Wang         XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
18222a547feSLi Feng Wang                 XStorable.class, presentationDocument);
18322a547feSLi Feng Wang         xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
18422a547feSLi Feng Wang 
18522a547feSLi Feng Wang         return (XComponent) UnoRuntime.queryInterface(XComponent.class,
18622a547feSLi Feng Wang                 unoApp.loadDocument(filePath));
18722a547feSLi Feng Wang     }
18822a547feSLi Feng Wang }
189