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 
48 import com.sun.star.presentation.XPresentation;
49 import com.sun.star.presentation.XPresentationSupplier;
50 
51 import com.sun.star.text.ControlCharacter;
52 import com.sun.star.text.XText;
53 import com.sun.star.text.XTextCursor;
54 import com.sun.star.text.XTextRange;
55 import com.sun.star.uno.UnoRuntime;
56 
57 public class ParagraphTextProperty {
58 	XPresentationSupplier sdDocument = null;
59 	XPresentation pre = null;
60 	XComponent precomp = null;
61 	XComponent impressDocument = null;
62 	XComponent reLoadFile = null;
63 	XDrawPagesSupplier drawsupplier = null;
64 	XDrawPages drawpages = null;
65 	XShapes xShapes = null;
66 	XDrawPage xpage = null;
67 	String filePath=null;
68 
69 	UnoApp unoApp = new UnoApp();
70 
71 	/**
72 	 * @throws java.lang.Exception
73 	 */
74 	@Before
setUp()75 	public void setUp() throws Exception {
76 		unoApp.start();
77 		createDocumentAndSlide();
78 	}
79 
80 	@After
tearDown()81 	public void tearDown() throws Exception {
82 		unoApp.closeDocument(impressDocument);
83 		unoApp.closeDocument(reLoadFile);
84 		unoApp.close();
85 	}
86 
87 	@Test
testParagraphPropertyShape()88 	public void testParagraphPropertyShape() throws Exception {
89 		 Point po = new Point(5000, 5000);
90 		 xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
91 		 // create the shape
92 		 XShape xRectangle = ShapeUtil.createShape(impressDocument, po, new Size(21000, 12500), "com.sun.star.drawing.RectangleShape");
93 		 xShapes.add(xRectangle);
94 
95 		 XPropertySet xShapePropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xRectangle);
96 		 // TextFitToSize
97 		 xShapePropSet.setPropertyValue("TextFitToSize", TextFitToSizeType.PROPORTIONAL);
98 		 // border size
99 		 xShapePropSet.setPropertyValue("TextLeftDistance", new Integer(2500));
100 		 xShapePropSet.setPropertyValue("TextRightDistance", new Integer(2500));
101 		 xShapePropSet.setPropertyValue("TextUpperDistance", new Integer(2500));
102 		 xShapePropSet.setPropertyValue("TextLowerDistance", new Integer(2500));
103 		 XPropertySet xTextPropSet = addPortion(xRectangle, "using TextFitToSize", false);
104 		 xTextPropSet = addPortion(xRectangle, "and a Border distance of 2,5 cm", true);
105 
106 		 xRectangle = saveAndLoadShape(1,0);
107 
108 		 Assert.assertEquals("TextLeftDistance is 2500", 2500, xShapePropSet.getPropertyValue("TextLeftDistance"));
109 		 Assert.assertEquals("TextRightDistance is 2500", 2500, xShapePropSet.getPropertyValue("TextRightDistance"));
110 		 Assert.assertEquals("TextUpperDistance is 2500", 2500, xShapePropSet.getPropertyValue("TextUpperDistance"));
111 		 Assert.assertEquals("TextLowerDistance is 2500", 2500, xShapePropSet.getPropertyValue("TextLowerDistance"));
112 
113 	}
114 
addPortion(XShape xShape, String sText, boolean bNewParagraph)115 	public static XPropertySet addPortion(XShape xShape, String sText, boolean bNewParagraph)
116 	         throws com.sun.star.lang.IllegalArgumentException {
117 	     XText xText = (XText)UnoRuntime.queryInterface(XText.class, xShape);
118 	     XTextCursor xTextCursor = xText.createTextCursor();
119 	     xTextCursor.gotoEnd(false);
120 	     if (bNewParagraph) {
121 	         xText.insertControlCharacter(xTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);
122 	         xTextCursor.gotoEnd(false);
123 	     }
124 	     XTextRange xTextRange = (XTextRange)UnoRuntime.queryInterface(XTextRange.class, xTextCursor);
125 	     xTextRange.setString(sText);
126 	     xTextCursor.gotoEnd(true);
127 	     XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextRange);
128 	     return xPropSet;
129 	 }
130 
131 	/**
132 	 * create a new presentation document and insert a new slide.
133 	 *
134 	 * @throws Exception
135 	 */
createDocumentAndSlide()136 	public void createDocumentAndSlide() throws Exception {
137 		impressDocument = (XComponent) UnoRuntime.queryInterface(
138 				XComponent.class, unoApp.newDocument("simpress"));
139 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
140 				XDrawPagesSupplier.class, impressDocument);
141 		drawpages = drawsupplier.getDrawPages();
142 		drawpages.insertNewByIndex(1);
143 		xpage = PageUtil.getDrawPageByIndex(impressDocument, 1);
144 	}
145 
146 	/**
147 	 * Save presentation and reLoad the presentation and shape in it.
148 	 *
149 	 * @param po
150 	 * @param shapeType
151 	 * @return
152 	 * @throws Exception
153 	 */
saveAndLoadShape(int pageIndex, int shapeIndex)154 	public XShape saveAndLoadShape(int pageIndex, int shapeIndex) throws Exception {
155 		reLoadFile = saveAndReloadDoc(impressDocument,
156 				"impress8", "odp");
157 		xShapes=ShapeUtil.getShapes(reLoadFile, pageIndex);
158 		return  (XShape) UnoRuntime.queryInterface(XShape.class, xShapes.getByIndex(shapeIndex));
159 	}
160 
161 	/**
162 	 * save and reload Presentation document.
163 	 *
164 	 * @param presentationDocument
165 	 * @param sFilter
166 	 * @param sExtension
167 	 * @return
168 	 * @throws Exception
169 	 */
saveAndReloadDoc(XComponent presentationDocument, String sFilter, String sExtension)170 	private XComponent saveAndReloadDoc(XComponent presentationDocument,
171 			String sFilter, String sExtension) throws Exception {
172 		filePath = Testspace.getPath("tmp/paragraphtextproperty."
173 				+ sExtension);
174 		PropertyValue[] aStoreProperties = new PropertyValue[2];
175 		aStoreProperties[0] = new PropertyValue();
176 		aStoreProperties[1] = new PropertyValue();
177 		aStoreProperties[0].Name = "Override";
178 		aStoreProperties[0].Value = true;
179 		aStoreProperties[1].Name = "FilterName";
180 		aStoreProperties[1].Value = sFilter;
181 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
182 				XStorable.class, presentationDocument);
183 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
184 
185 		return (XComponent) UnoRuntime.queryInterface(XComponent.class,
186 				unoApp.loadDocument(filePath));
187 	}
188 }
189