xref: /AOO41X/main/qadevOOo/tests/java/mod/_sd/SdXCustomPresentationAccess.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._sd;
29 
30 import java.io.PrintWriter;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.SOfficeFactory;
37 
38 import com.sun.star.container.XNameContainer;
39 import com.sun.star.lang.XComponent;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.lang.XSingleServiceFactory;
42 import com.sun.star.presentation.XCustomPresentationSupplier;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.uno.XInterface;
45 
46 /**
47 * Test for object which is represented by service
48 * <code>com.sun.star.presentation.CustomPresentationAccess</code>. <p>
49 * Object implements the following interfaces :
50 * <ul>
51 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
52 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
53 *  <li> <code>com::sun::star::container::XNameReplace</code></li>
54 *  <li> <code>com::sun::star::lang::XSingleServiceFactory</code></li>
55 *  <li> <code>com::sun::star::container::XNameContainer</code></li>
56 * </ul>
57 * @see com.sun.star.presentation.CustomPresentationAccess
58 * @see com.sun.star.container.XNameAccess
59 * @see com.sun.star.container.XElementAccess
60 * @see com.sun.star.container.XNameReplace
61 * @see com.sun.star.lang.XSingleServiceFactory
62 * @see com.sun.star.container.XNameContainer
63 * @see ifc.container._XNameAccess
64 * @see ifc.container._XElementAccess
65 * @see ifc.container._XNameReplace
66 * @see ifc.lang._XSingleServiceFactory
67 * @see ifc.container._XNameContainer
68 */
69 public class SdXCustomPresentationAccess extends TestCase {
70     XComponent xImpressDoc;
71 
72     /**
73     * Creates Impress document.
74     */
75     protected void initialize(TestParameters Param, PrintWriter log) {
76         // get a soffice factory object
77         SOfficeFactory SOF = SOfficeFactory.getFactory(
78                                 (XMultiServiceFactory)Param.getMSF());
79 
80         try {
81             log.println( "creating a draw document" );
82             xImpressDoc = SOF.createImpressDoc(null);;
83         } catch (com.sun.star.uno.Exception e) {
84             // Some exception occures.FAILED
85             e.printStackTrace( log );
86             throw new StatusException( "Couldn't create document", e );
87         }
88     }
89 
90     /**
91     * Disposes Impress document.
92     */
93     protected void cleanup( TestParameters Param, PrintWriter log) {
94         log.println("disposing xImpressDoc");
95         util.DesktopTools.closeDoc(xImpressDoc);;
96     }
97 
98     /**
99     * Creating a Testenvironment for the interfaces to be tested.
100     * Retrieves the collection of the CustomPresentation from the document
101     * using the interface <code>XCustomPresentationSupplier</code>.
102     * The retrieved collection is the instance of the servcie
103     * <code>com.sun.star.presentation.CustomPresentationAccess</code>.
104     * Creates two new instances of the custom presentation using the interface
105     * <code>XSingleServiceFactory</code>. Inserts one of them to the
106     * retrieved collection. Creates and inserts several instances of
107     * the presentation.
108     * Object relations created :
109     * <ul>
110     *  <li> <code>'SecondInstance'</code> for
111     *      {@link ifc.container._XNameContainer}(the second created instance
112     *      of the custom presentation)</li>
113     *  <li> <code>'XNameContainerINDEX'</code> for
114     *      {@link ifc.container._XNameContainer}(the number of current running
115     *      threads)</li>
116     *  <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> for
117     *      {@link ifc.container._XNameReplace},
118     *      {@link ifc.container._XNameContainer}(the created instances of
119     *      the presentation)</li>
120     * </ul>
121     * @see com.sun.star.presentation.XCustomPresentationSupplier
122     * @see com.sun.star.lang.XSingleServiceFactory
123     * @see com.sun.star.presentation.CustomPresentationAccess
124     */
125     public TestEnvironment createTestEnvironment(
126         TestParameters Param, PrintWriter log) throws StatusException {
127 
128 
129         log.println( "creating a test environment" );
130 
131         log.println( "get presentation" );
132         XCustomPresentationSupplier oPS = (XCustomPresentationSupplier)
133             UnoRuntime.queryInterface(
134                 XCustomPresentationSupplier.class, xImpressDoc);
135         XInterface oObj = oPS.getCustomPresentations();
136 
137 
138         XSingleServiceFactory oSingleMSF = (XSingleServiceFactory)
139             UnoRuntime.queryInterface(XSingleServiceFactory.class, oObj);
140 
141         XInterface oInstance = null;
142         XInterface oSecondInstance = null;
143         try{
144             oInstance = (XInterface) oSingleMSF.createInstance();
145             oSecondInstance = (XInterface) oSingleMSF.createInstance();
146         } catch (com.sun.star.uno.Exception e) {
147             e.printStackTrace(log);
148             throw new StatusException("Couldn't create instance", e);
149         }
150 
151         XNameContainer aContainer = (XNameContainer)
152             UnoRuntime.queryInterface(XNameContainer.class, oObj);
153 
154         if (aContainer.hasByName("FirstPresentation")) {
155             try {
156                 aContainer.removeByName("FirstPresentation");
157             } catch (com.sun.star.lang.WrappedTargetException e) {
158                 e.printStackTrace(log);
159                 throw new StatusException("Exception while removing instance", e);
160             } catch (com.sun.star.container.NoSuchElementException e) {
161                 e.printStackTrace(log);
162                 throw new StatusException("Exception while removing instance", e);
163             }
164         }
165 
166         try {
167             aContainer.insertByName("FirstPresentation",oInstance);
168         } catch (com.sun.star.lang.WrappedTargetException e){
169             e.printStackTrace(log);
170             throw new StatusException("Could't insert Instance", e);
171         } catch (com.sun.star.container.ElementExistException e){
172             e.printStackTrace(log);
173             throw new StatusException("Could't insert Instance", e);
174         } catch (com.sun.star.lang.IllegalArgumentException e){
175             e.printStackTrace(log);
176             throw new StatusException("Could't insert Instance", e);
177         }
178 
179         log.println( "creating a new environment for XPresentation object" );
180         TestEnvironment tEnv = new TestEnvironment( oObj );
181 
182         // adding an instance for insertByName ('XNameContainer')
183         tEnv.addObjRelation("SecondInstance", oSecondInstance);
184 
185         int THRCNT = 1;
186         if ((String)Param.get("THRCNT") != null) {
187             THRCNT = Integer.parseInt((String)Param.get("THRCNT"));
188         }
189 
190         // INDEX : _XNameContainer
191         log.println( "adding XNameContainerINDEX as mod relation to environment" );
192         tEnv.addObjRelation("XNameContainerINDEX",
193             (new Integer(THRCNT)).toString());
194 
195         // INSTANCEn : _XNameContainer; _XNameReplace
196         log.println( "adding INSTANCEn as mod relation to environment" );
197         try {
198             for (int n = 1; n < (2*THRCNT+1) ;n++ ) {
199                 log.println( "adding INSTANCE" + n
200                     +" as mod relation to environment" );
201                 oInstance = (XInterface) oSingleMSF.createInstance();
202                 tEnv.addObjRelation("INSTANCE" + n, oInstance);
203             }
204         } catch (com.sun.star.uno.Exception e) {
205             log.println("Could't adding INSTANCEn: " + e);
206         }
207 
208         return tEnv;
209     } // finish method getTestEnvironment
210 
211 }    // finish class SdXCustomPresentationAccess
212 
213