1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package mod._sd;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.PrintWriter;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import lib.StatusException;
29cdf0e10cSrcweir import lib.TestCase;
30cdf0e10cSrcweir import lib.TestEnvironment;
31cdf0e10cSrcweir import lib.TestParameters;
32cdf0e10cSrcweir import util.SOfficeFactory;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
35cdf0e10cSrcweir import com.sun.star.container.XIndexContainer;
36cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
37cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage;
38cdf0e10cSrcweir import com.sun.star.drawing.XDrawPages;
39cdf0e10cSrcweir import com.sun.star.drawing.XDrawPagesSupplier;
40cdf0e10cSrcweir import com.sun.star.lang.XComponent;
41cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
42cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
43cdf0e10cSrcweir import com.sun.star.presentation.XCustomPresentationSupplier;
44cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
45cdf0e10cSrcweir import com.sun.star.uno.Type;
46cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
47cdf0e10cSrcweir import com.sun.star.uno.XInterface;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir /**
50cdf0e10cSrcweir * Test for object which is represented by service
51cdf0e10cSrcweir * <code>com.sun.star.presentation.CustomPresentation</code>. <p>
52cdf0e10cSrcweir * Object implements the following interfaces :
53cdf0e10cSrcweir * <ul>
54cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XNamed</code></li>
55cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XIndexContainer</code></li>
56cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
57cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XElementAccess</code></li>
58cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XIndexReplace</code></li>
59cdf0e10cSrcweir * </ul>
60cdf0e10cSrcweir * @see com.sun.star.presentation.CustomPresentation
61cdf0e10cSrcweir * @see com.sun.star.container.XNamed
62cdf0e10cSrcweir * @see com.sun.star.container.XIndexContainer
63cdf0e10cSrcweir * @see com.sun.star.container.XIndexAccess
64cdf0e10cSrcweir * @see com.sun.star.container.XElementAccess
65cdf0e10cSrcweir * @see com.sun.star.container.XIndexReplace
66cdf0e10cSrcweir * @see ifc.container._XNamed
67cdf0e10cSrcweir * @see ifc.container._XIndexContainer
68cdf0e10cSrcweir * @see ifc.container._XIndexAccess
69cdf0e10cSrcweir * @see ifc.container._XElementAccess
70cdf0e10cSrcweir * @see ifc.container._XIndexReplace
71cdf0e10cSrcweir */
72cdf0e10cSrcweir public class SdXCustomPresentation extends TestCase {
73cdf0e10cSrcweir     XComponent xImpressDoc;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     /**
76cdf0e10cSrcweir     * Creates Impress document.
77cdf0e10cSrcweir     */
initialize(TestParameters Param, PrintWriter log)78cdf0e10cSrcweir     protected void initialize(TestParameters Param, PrintWriter log) {
79cdf0e10cSrcweir         // get a soffice factory object
80cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory(
81cdf0e10cSrcweir                                 (XMultiServiceFactory)Param.getMSF());
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         try {
84cdf0e10cSrcweir             log.println( "creating a draw document" );
85*170fb961SPedro Giffuni             xImpressDoc = SOF.createImpressDoc(null);
86cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
87cdf0e10cSrcweir             // Some exception occures.FAILED
88cdf0e10cSrcweir             e.printStackTrace( log );
89cdf0e10cSrcweir             throw new StatusException( "Couldn't create document", e );
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     /**
95cdf0e10cSrcweir     * Disposes Impress document.
96cdf0e10cSrcweir     */
cleanup( TestParameters Param, PrintWriter log)97cdf0e10cSrcweir     protected void cleanup( TestParameters Param, PrintWriter log) {
98cdf0e10cSrcweir         log.println("disposing xImpressDoc");
99*170fb961SPedro Giffuni         util.DesktopTools.closeDoc(xImpressDoc);
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     /**
103cdf0e10cSrcweir     * Creating a Testenvironment for the interfaces to be tested.
104cdf0e10cSrcweir     * Retrieves the collection of the CustomPresentation from the document
105cdf0e10cSrcweir     * using the interface <code>XCustomPresentationSupplier</code>. Creates and
106cdf0e10cSrcweir     * inserts new instance of the presentation to the collection. The created
107cdf0e10cSrcweir     * instance is the instance of the service
108cdf0e10cSrcweir     * <code>com.sun.star.presentation.CustomPresentation</code>. Obtains
109cdf0e10cSrcweir     * the collection of draw pages from the document and takes one of them.
110cdf0e10cSrcweir     * Inserts the obtained draw page to the created instance. Inserts some new
111cdf0e10cSrcweir     * draw pages.
112cdf0e10cSrcweir     * Object relations created :
113cdf0e10cSrcweir     * <ul>
114cdf0e10cSrcweir     *  <li> <code>'XIndexContainerINDEX'</code> for
115cdf0e10cSrcweir     *      {@link ifc.container._XIndexContainer}(the string representation of
116cdf0e10cSrcweir     *      the zero)</li>
117cdf0e10cSrcweir     *  <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> for
118cdf0e10cSrcweir     *      {@link ifc.container._XIndexContainer}(the new inserted draw pages)</li>
119cdf0e10cSrcweir     * </ul>
120cdf0e10cSrcweir     * @see com.sun.star.presentation.XCustomPresentationSupplier
121cdf0e10cSrcweir     * @see com.sun.star.presentation.CustomPresentation
122cdf0e10cSrcweir     */
createTestEnvironment( TestParameters Param, PrintWriter log)123cdf0e10cSrcweir     protected TestEnvironment createTestEnvironment(
124cdf0e10cSrcweir                                 TestParameters Param, PrintWriter log) {
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         log.println( "creating a test environment" );
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         log.println( "get presentation" );
130cdf0e10cSrcweir         XCustomPresentationSupplier oPS = (XCustomPresentationSupplier)
131cdf0e10cSrcweir             UnoRuntime.queryInterface(
132cdf0e10cSrcweir                 XCustomPresentationSupplier.class, xImpressDoc);
133cdf0e10cSrcweir         XInterface oObj = oPS.getCustomPresentations();
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         XSingleServiceFactory oSingleMSF = (XSingleServiceFactory)
136cdf0e10cSrcweir             UnoRuntime.queryInterface(XSingleServiceFactory.class, oObj);
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         XInterface oInstance = null;
139cdf0e10cSrcweir         try {
140cdf0e10cSrcweir             oInstance = (XInterface) oSingleMSF.createInstance();
141cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
142cdf0e10cSrcweir             e.printStackTrace(log);
143cdf0e10cSrcweir             throw new StatusException("Couldn't create instance", e);
144cdf0e10cSrcweir         }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         XNameContainer aContainer = (XNameContainer)
147cdf0e10cSrcweir             UnoRuntime.queryInterface(XNameContainer.class, oObj);
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         try {
150cdf0e10cSrcweir             aContainer.insertByName("FirstPresentation", oInstance);
151cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e){
152cdf0e10cSrcweir             e.printStackTrace(log);
153cdf0e10cSrcweir             throw new StatusException("Could't insert Presentation", e);
154cdf0e10cSrcweir         } catch (com.sun.star.container.ElementExistException e){
155cdf0e10cSrcweir             e.printStackTrace(log);
156cdf0e10cSrcweir             throw new StatusException("Could't insert Presentation", e);
157cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e){
158cdf0e10cSrcweir             e.printStackTrace(log);
159cdf0e10cSrcweir             throw new StatusException("Could't insert Presentation", e);
160cdf0e10cSrcweir         }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir         // get the drawpage of drawing here
163cdf0e10cSrcweir         log.println( "getting Drawpage" );
164cdf0e10cSrcweir         XDrawPagesSupplier oDPS = (XDrawPagesSupplier)
165cdf0e10cSrcweir             UnoRuntime.queryInterface(XDrawPagesSupplier.class, xImpressDoc);
166cdf0e10cSrcweir         XDrawPages oDPn = oDPS.getDrawPages();
167cdf0e10cSrcweir         XIndexAccess oDPi = (XIndexAccess)
168cdf0e10cSrcweir             UnoRuntime.queryInterface(XIndexAccess.class, oDPn);
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         XDrawPage oDrawPage = null;
171cdf0e10cSrcweir         try {
172cdf0e10cSrcweir             oDrawPage = (XDrawPage) AnyConverter.toObject(
173cdf0e10cSrcweir                     new Type(XDrawPage.class),oDPi.getByIndex(0));
174cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
175cdf0e10cSrcweir             e.printStackTrace(log);
176cdf0e10cSrcweir             throw new StatusException("Couldn't get by index", e);
177cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
178cdf0e10cSrcweir             e.printStackTrace(log);
179cdf0e10cSrcweir             throw new StatusException("Couldn't get by index", e);
180cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
181cdf0e10cSrcweir             e.printStackTrace(log);
182cdf0e10cSrcweir             throw new StatusException("Couldn't get by index", e);
183cdf0e10cSrcweir         }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir         XIndexContainer aIContainer = (XIndexContainer)
186cdf0e10cSrcweir             UnoRuntime.queryInterface(XIndexContainer.class,oInstance);
187cdf0e10cSrcweir 
188cdf0e10cSrcweir         try {
189cdf0e10cSrcweir             aIContainer.insertByIndex(0, oDrawPage);
190cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e){
191cdf0e10cSrcweir             e.printStackTrace(log);
192cdf0e10cSrcweir             throw new StatusException("Could't insert DrawPage", e);
193cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e){
194cdf0e10cSrcweir             e.printStackTrace(log);
195cdf0e10cSrcweir             throw new StatusException("Could't insert DrawPage", e);
196cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
197cdf0e10cSrcweir             e.printStackTrace(log);
198cdf0e10cSrcweir             throw new StatusException("Could't insert DrawPage", e);
199cdf0e10cSrcweir         }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir         log.println( "creating a new environment for XPresentation object" );
202cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment( oInstance );
203cdf0e10cSrcweir 
204cdf0e10cSrcweir         int THRCNT=1;
205cdf0e10cSrcweir         if ((String)Param.get("THRCNT") != null) {
206cdf0e10cSrcweir             THRCNT = Integer.parseInt((String)Param.get("THRCNT"));
207cdf0e10cSrcweir         }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir         // INDEX : _XNameContainer
210cdf0e10cSrcweir         log.println( "adding XIndexContainerINDEX as mod relation to environment" );
211cdf0e10cSrcweir         tEnv.addObjRelation("XIndexContainerINDEX", "0");
212cdf0e10cSrcweir 
213cdf0e10cSrcweir         // INSTANCEn : _XNameContainer; _XNameReplace
214cdf0e10cSrcweir         log.println( "adding INSTANCEn as mod relation to environment" );
215cdf0e10cSrcweir         try {
216cdf0e10cSrcweir             for (int n = 1; n < (2*THRCNT+1) ;n++ ) {
217cdf0e10cSrcweir                 log.println( "adding INSTANCE" + n
218cdf0e10cSrcweir                     +" as mod relation to environment" );
219cdf0e10cSrcweir                 oDPn.insertNewByIndex(0);
220cdf0e10cSrcweir                 oDrawPage = (XDrawPage) AnyConverter.toObject(
221cdf0e10cSrcweir                         new Type(XDrawPage.class),oDPi.getByIndex(0));
222cdf0e10cSrcweir                 tEnv.addObjRelation("INSTANCE" + n, oDrawPage);
223cdf0e10cSrcweir             }
224cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
225cdf0e10cSrcweir             e.printStackTrace(log);
226cdf0e10cSrcweir             throw new StatusException("Could't adding INSTANCEn", e);
227cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
228cdf0e10cSrcweir             e.printStackTrace(log);
229cdf0e10cSrcweir             throw new StatusException("Could't adding INSTANCEn", e);
230cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
231cdf0e10cSrcweir             e.printStackTrace(log);
232cdf0e10cSrcweir             throw new StatusException("Could't adding INSTANCEn", e);
233cdf0e10cSrcweir         }
234cdf0e10cSrcweir 
235cdf0e10cSrcweir         return tEnv;
236cdf0e10cSrcweir 
237cdf0e10cSrcweir     } // finish method getTestEnvironment
238cdf0e10cSrcweir 
239cdf0e10cSrcweir }    // finish class SdXCustomPresentation
240