xref: /AOO41X/test/testuno/source/fvt/uno/sd/slideshow/CustomShow.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.slideshow;
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 com.sun.star.awt.Point;
33 import com.sun.star.awt.Size;
34 import com.sun.star.beans.PropertyValue;
35 import com.sun.star.beans.XPropertySet;
36 import com.sun.star.container.XIndexContainer;
37 import com.sun.star.container.XNameContainer;
38 import com.sun.star.container.XNamed;
39 import com.sun.star.drawing.XDrawPage;
40 import com.sun.star.drawing.XDrawPages;
41 import com.sun.star.drawing.XDrawPagesSupplier;
42 import com.sun.star.drawing.XShape;
43 import com.sun.star.drawing.XShapes;
44 import com.sun.star.frame.XStorable;
45 import com.sun.star.lang.XComponent;
46 import com.sun.star.lang.XSingleServiceFactory;
47 import com.sun.star.presentation.XCustomPresentationSupplier;
48 import com.sun.star.presentation.XPresentation;
49 import com.sun.star.presentation.XPresentationSupplier;
50 import com.sun.star.uno.UnoRuntime;
51 
52 import testlib.uno.ShapeUtil;
53 
54 public class CustomShow {
55     XPresentationSupplier sdDocument = null;
56     XPresentation pre = null;
57     XComponent precomp = null;
58     XComponent impressDocument = null;
59     XComponent reLoadFile = null;
60     XDrawPagesSupplier drawsupplier = null;
61     XDrawPages drawpages = null;
62 
63     String filePath = null;
64 
65     UnoApp unoApp = new UnoApp();
66 
67     /**
68      * @throws java.lang.Exception
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     }
82 
83     @Test
testCustomShow()84     public void testCustomShow() throws Exception {
85 
86         String aNameArray[] = { "Page 1", "Page 2",  "Page 3", "Page 4", "Page 5"};
87 
88         //insert five  pages
89         while (drawpages.getCount() < aNameArray.length)
90             drawpages.insertNewByIndex(0);
91 
92         //add text shape to each page
93         for (int i = 0; i < aNameArray.length; i++) {
94             XDrawPage xDrawPage = (XDrawPage) UnoRuntime.queryInterface(
95                     XDrawPage.class, drawpages.getByIndex(i));
96             XNamed xPageName = (XNamed) UnoRuntime.queryInterface(XNamed.class,
97                     xDrawPage);
98             xPageName.setName(aNameArray[i]);
99 
100             XShape xTextObj = ShapeUtil.createShape(impressDocument, new Point(
101                     10000, 9000), new Size(10000, 5000),
102                     "com.sun.star.drawing.TextShape");
103             XShapes xShapes = (XShapes) UnoRuntime.queryInterface(
104                     XShapes.class, xDrawPage);
105             xShapes.add(xTextObj);
106             ShapeUtil.addPortion(xTextObj, aNameArray[i], true);
107         }
108 
109 
110         //create two custom shows
111         //one will play slide 3 to 5 and is named "Part"
112         //the other one will play slide 1 t0 5 and is named "All"
113         XCustomPresentationSupplier xCustPresSupplier = (XCustomPresentationSupplier) UnoRuntime
114                 .queryInterface(XCustomPresentationSupplier.class, impressDocument);
115 
116 
117         // the following container is a container for further container which
118         // includes the list of pages that are to play within a custom show
119         XNameContainer xNameContainer = xCustPresSupplier
120                 .getCustomPresentations();
121         XSingleServiceFactory xFactory = (XSingleServiceFactory) UnoRuntime
122                 .queryInterface(XSingleServiceFactory.class, xNameContainer);
123 
124         Object xObj;
125         XIndexContainer xContainer;
126 
127         //instantiate an IndexContainer that will take a list of draw pages for
128         //the first custom show
129         xObj = xFactory.createInstance();
130         xContainer = (XIndexContainer) UnoRuntime.queryInterface(
131                 XIndexContainer.class, xObj);
132         for (int i = 3; i < 5; i++)
133             xContainer.insertByIndex(xContainer.getCount(),
134                     drawpages.getByIndex(i));
135         xNameContainer.insertByName("Part", xContainer);
136 
137 
138         //instantiate an IndexContainer that will take a list of draw page for
139         //the second custom show
140         xObj = xFactory.createInstance();
141         xContainer = (XIndexContainer) UnoRuntime.queryInterface(
142                 XIndexContainer.class, xObj);
143         for (int i = 1; i < 5; i++)
144             xContainer.insertByIndex(xContainer.getCount(),
145                     drawpages.getByIndex(i));
146         xNameContainer.insertByName("All", xContainer);
147 
148 
149         //set which custom show is to used
150         XPresentationSupplier xPresSupplier = (XPresentationSupplier) UnoRuntime
151                 .queryInterface(XPresentationSupplier.class, impressDocument);
152         XPresentation xPresentation = xPresSupplier.getPresentation();
153         XPropertySet xPresPropSet = (XPropertySet) UnoRuntime.queryInterface(
154                 XPropertySet.class, xPresentation);
155         xPresPropSet.setPropertyValue("CustomShow", "Part");
156 
157         saveAndLoadSlide();
158 
159         //assert if custom show is set
160         Assert.assertEquals("Set Part as CustomSHow", xPresPropSet.getPropertyValue("CustomShow"), "Part");
161     }
162 
163     /**
164      * create a new presentation document and insert a new slide.
165      *
166      * @throws Exception
167      */
createDocumentAndSlide()168     public void createDocumentAndSlide() throws Exception {
169         impressDocument = (XComponent) UnoRuntime.queryInterface(
170                 XComponent.class, unoApp.newDocument("simpress"));
171         drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
172                 XDrawPagesSupplier.class, impressDocument);
173         drawpages = drawsupplier.getDrawPages();
174 
175         sdDocument = (XPresentationSupplier) UnoRuntime.queryInterface(
176                 XPresentationSupplier.class, impressDocument);
177         pre = sdDocument.getPresentation();
178     }
179 
180     /**
181      * Save presentation and reLoad the slide.
182      *
183      * @param no
184      * @return void
185      * @throws Exception
186      */
saveAndLoadSlide()187     public void saveAndLoadSlide() throws Exception {
188         reLoadFile = saveAndReloadDoc(impressDocument,
189                 "StarOffice XML (Impress)", "odp");
190         drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
191                 XDrawPagesSupplier.class, reLoadFile);
192         drawpages = drawsupplier.getDrawPages();
193 
194         sdDocument = (XPresentationSupplier) UnoRuntime.queryInterface(
195                 XPresentationSupplier.class, reLoadFile);
196         pre = sdDocument.getPresentation();
197     }
198 
199     /**
200      * save and reload Presentation document.
201      *
202      * @param presentationDocument
203      * @param sFilter
204      * @param sExtension
205      * @return
206      * @throws Exception
207      */
saveAndReloadDoc(XComponent presentationDocument, String sFilter, String sExtension)208     private XComponent saveAndReloadDoc(XComponent presentationDocument,
209             String sFilter, String sExtension) throws Exception {
210         filePath = Testspace.getPath("tmp/customshow." + sExtension);
211         PropertyValue[] aStoreProperties = new PropertyValue[2];
212         aStoreProperties[0] = new PropertyValue();
213         aStoreProperties[1] = new PropertyValue();
214         aStoreProperties[0].Name = "Override";
215         aStoreProperties[0].Value = true;
216         aStoreProperties[1].Name = "FilterName";
217         aStoreProperties[1].Value = sFilter;
218         XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
219                 XStorable.class, presentationDocument);
220         xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
221 
222         return (XComponent) UnoRuntime.queryInterface(XComponent.class,
223                 unoApp.loadDocument(filePath));
224     }
225 }
226