xref: /AOO41X/test/testuno/source/fvt/uno/sd/textbox/FillProperties.java (revision eba4d44a33e5be0b2528d5a9a6f0dcbf65adaa0d)
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.textbox;
22 
23 import static org.junit.Assert.assertEquals;
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 import testlib.uno.TestUtil;
35 
36 import com.sun.star.awt.Gradient;
37 import com.sun.star.awt.GradientStyle;
38 import com.sun.star.awt.Point;
39 import com.sun.star.awt.Size;
40 import com.sun.star.beans.PropertyValue;
41 import com.sun.star.beans.XPropertySet;
42 import com.sun.star.drawing.FillStyle;
43 import com.sun.star.drawing.Hatch;
44 import com.sun.star.drawing.HatchStyle;
45 import com.sun.star.drawing.TextVerticalAdjust;
46 import com.sun.star.drawing.XDrawPage;
47 import com.sun.star.drawing.XDrawPages;
48 import com.sun.star.drawing.XDrawPagesSupplier;
49 import com.sun.star.drawing.XShape;
50 import com.sun.star.drawing.XShapes;
51 import com.sun.star.frame.XStorable;
52 import com.sun.star.lang.XComponent;
53 import com.sun.star.presentation.XPresentation;
54 import com.sun.star.presentation.XPresentationSupplier;
55 import com.sun.star.uno.UnoRuntime;
56 
57 public class FillProperties {
58     UnoApp unoApp = new UnoApp();
59     XPresentationSupplier sdDocument = null;
60     XPresentation pre = null;
61     XComponent precomp = null;
62     XComponent impressDocument = null;
63     XComponent reLoadFile = null;
64     XDrawPagesSupplier drawsupplier = null;
65     XDrawPages drawpages = null;
66     XShapes xShapes = null;
67     XDrawPage xpage = null;
68     String filePath = null;
69 
70     @Before
setUp()71     public void setUp() throws Exception {
72         unoApp.start();
73         createDocumentAndSlide();
74     }
75 
76     @After
tearDown()77     public void tearDown() throws Exception {
78         unoApp.closeDocument(impressDocument);
79         unoApp.closeDocument(reLoadFile);
80         unoApp.close();
81          if (filePath != null)
82          FileUtil.deleteFile(filePath);
83     }
84 
85     /**
86      * test Insert text to an Textbox
87      *
88      * @throws Exception
89      */
90     @Test
testInsertTextToTextbox()91     public void testInsertTextToTextbox() throws Exception {
92         Point po = new Point(1000, 8000);
93         xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
94         XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
95                 5000, 5000), "com.sun.star.drawing.TextShape");
96         xShapes.add(xShape);
97         xShape = saveAndLoadShape(1, 0);
98         ShapeUtil.addPortion(xShape, "test", false);
99         assertEquals("Not put text correctly", "test",
100                 ShapeUtil.getPortion(xShape));
101     }
102 
103     /**
104      * test textbox fill with Gradient
105      *
106      * @throws Exception
107      */
108     @Test
testFillGradient()109     public void testFillGradient() throws Exception {
110         Point po = new Point(1000, 8000);
111         xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
112         XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
113                 5000, 5000), "com.sun.star.drawing.TextShape");
114         xShapes.add(xShape);
115         ShapeUtil.addPortion(xShape, "test", false);
116         XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
117                 XPropertySet.class, xShape);
118         xPropSet.setPropertyValue("FillStyle", FillStyle.GRADIENT);
119         Gradient aGradient = new Gradient();
120         aGradient.Style = GradientStyle.LINEAR;
121         aGradient.StartColor = 0x00ff00;
122         aGradient.EndColor = 0xffff00;
123         aGradient.Angle = 450;
124         aGradient.Border = 0;
125         aGradient.XOffset = 0;
126         aGradient.YOffset = 0;
127         aGradient.StartIntensity = 100;
128         aGradient.EndIntensity = 100;
129         aGradient.StepCount = 10;
130         xPropSet.setPropertyValue("FillGradient", aGradient);
131         // --------------------------
132         xShape = saveAndLoadShape(1, 0);
133         xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
134                 xShape);
135         // ----------------------------
136         assertEquals("Not Gradient Fill Style", FillStyle.GRADIENT,
137                 xPropSet.getPropertyValue("FillStyle"));
138         aGradient = (Gradient) xPropSet.getPropertyValue("FillGradient");
139         assertEquals("Not Linear Gradient", GradientStyle.LINEAR,
140                 aGradient.Style);
141     }
142 
143     /**
144      * test textbox fill with yellow color
145      *
146      * @throws Exception
147      */
148     @Test
testFillColor()149     public void testFillColor() throws Exception {
150         Point po = new Point(1000, 8000);
151         xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
152         XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
153                 5000, 5000), "com.sun.star.drawing.TextShape");
154         xShapes.add(xShape);
155         ShapeUtil.addPortion(xShape, "test", false);
156         XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
157                 XPropertySet.class, xShape);
158         xPropSet.setPropertyValue("FillStyle", FillStyle.SOLID);
159         xPropSet.setPropertyValue("FillColor", 0xffff00);
160         // --------------------------
161         xShape = saveAndLoadShape(1, 0);
162         xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
163                 xShape);
164         // ----------------------------------------------------
165         assertEquals("Not Color Fill Style", FillStyle.SOLID,
166                 xPropSet.getPropertyValue("FillStyle"));
167         assertEquals("Not Yellow Color Fill", 0xffff00,
168                 xPropSet.getPropertyValue("FillColor"));
169     }
170 
171     /**
172      * test textbox fill with Hatch Style(Pattern in MS)
173      *
174      * @throws Exception
175      */
176     @Test
testFillHatch()177     public void testFillHatch() throws Exception {
178         Point po = new Point(1000, 8000);
179         xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
180         XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
181                 5000, 5000), "com.sun.star.drawing.TextShape");
182         xShapes.add(xShape);
183         ShapeUtil.addPortion(xShape, "test", false);
184         XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
185                 XPropertySet.class, xShape);
186         xPropSet.setPropertyValue("FillStyle", FillStyle.HATCH);
187         Hatch aHatch = new Hatch();
188         aHatch.Style = HatchStyle.DOUBLE;
189         aHatch.Color = 0x00ff00;
190         aHatch.Distance = 100;
191         aHatch.Angle = 450;
192         xPropSet.setPropertyValue("FillHatch", aHatch);
193 
194         // --------------------------
195         xShape = saveAndLoadShape(1, 0);
196         xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
197                 xShape);
198         // ----------------------------
199         assertEquals("Not Gradient Fill Style", FillStyle.HATCH,
200                 xPropSet.getPropertyValue("FillStyle"));
201         aHatch = (Hatch) xPropSet.getPropertyValue("FillHatch");
202         assertEquals("Not Double Hatch", HatchStyle.DOUBLE, aHatch.Style);
203     }
204 
205     /**
206      * create a new presentation document and insert a new slide.
207      *
208      * @throws Exception
209      */
createDocumentAndSlide()210     public void createDocumentAndSlide() throws Exception {
211         impressDocument = (XComponent) UnoRuntime.queryInterface(
212                 XComponent.class, unoApp.newDocument("simpress"));
213         drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
214                 XDrawPagesSupplier.class, impressDocument);
215         drawpages = drawsupplier.getDrawPages();
216         drawpages.insertNewByIndex(1);
217         xpage = PageUtil.getDrawPageByIndex(impressDocument, 1);
218     }
219 
220     /**
221      * Save presentation and reLoad the presentation and shape in it.
222      *
223      * @param po
224      * @param shapeType
225      * @return
226      * @throws Exception
227      */
saveAndLoadShape(int pageIndex, int shapeIndex)228     public XShape saveAndLoadShape(int pageIndex, int shapeIndex)
229             throws Exception {
230         reLoadFile = saveAndReloadDoc(impressDocument, "impress8", "odp");
231         xShapes = ShapeUtil.getShapes(reLoadFile, pageIndex);
232         return (XShape) UnoRuntime.queryInterface(XShape.class,
233                 xShapes.getByIndex(shapeIndex));
234     }
235 
236     /**
237      * save and reload Presentation document.
238      *
239      * @param presentationDocument
240      * @param sFilter
241      * @param sExtension
242      * @return
243      * @throws Exception
244      */
saveAndReloadDoc(XComponent presentationDocument, String sFilter, String sExtension)245     private XComponent saveAndReloadDoc(XComponent presentationDocument,
246             String sFilter, String sExtension) throws Exception {
247         filePath = Testspace.getPath("tmp/textboxfill." + sExtension);
248         PropertyValue[] aStoreProperties = new PropertyValue[2];
249         aStoreProperties[0] = new PropertyValue();
250         aStoreProperties[1] = new PropertyValue();
251         aStoreProperties[0].Name = "Override";
252         aStoreProperties[0].Value = true;
253         aStoreProperties[1].Name = "FilterName";
254         aStoreProperties[1].Value = sFilter;
255         XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
256                 XStorable.class, presentationDocument);
257         xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
258 
259         return (XComponent) UnoRuntime.queryInterface(XComponent.class,
260                 unoApp.loadDocument(filePath));
261     }
262 }
263