xref: /AOO41X/main/qadevOOo/tests/java/mod/_pcr/ObjectInspectorModel.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._pcr;
29 import com.sun.star.beans.UnknownPropertyException;
30 import com.sun.star.beans.XPropertySet;
31 import com.sun.star.frame.XFrame;
32 import com.sun.star.inspection.XObjectInspectorModel;
33 import com.sun.star.lang.WrappedTargetException;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.uno.XComponentContext;
36 import com.sun.star.util.CloseVetoException;
37 import com.sun.star.util.XCloseable;
38 import helper.PropertyHandlerFactroy;
39 import java.io.PrintWriter;
40 
41 import lib.StatusException;
42 import lib.TestCase;
43 import lib.TestEnvironment;
44 import lib.TestParameters;
45 
46 import com.sun.star.lang.XMultiServiceFactory;
47 import util.DesktopTools;
48 import util.utils;
49 
50 /**
51  * Test for object which is represented by service
52  * <code>com.sun.star.reflection.ObjectInspectorModel</code>. <p>
53  * Object implements the following interfaces :
54  * <ul>
55  *  <li> <code>com::sun::star::inspection::XObjectInspectorModel</code></li>
56  * </ul>
57  * This object test <b> is NOT </b> designed to be run in several
58  * threads concurently.
59  * @see com.sun.star.inspection.XObjectInspectorModel
60  */
61 public class ObjectInspectorModel extends TestCase {
62 
63     /**
64      * module variable which holds the Desktop
65      * @see com.sun.star.frame.Desktop
66      */
67     protected static Object StarDesktop = null;
68 
69     /**
70      * assign to the module variable <CODE>StarDesktop</CODE> the desktop
71      * @param Param the test parameters
72      * @param log the log writer
73      * @see lib.TestParameters
74      * @see share.LogWriter
75      * @see com.sun.star.frame.Desktop
76      */
77     protected void initialize(TestParameters Param, PrintWriter log) {
78         log.println("create a desktop...");
79         StarDesktop = DesktopTools.createDesktop((XMultiServiceFactory) Param.getMSF());
80         if (StarDesktop == null){
81             throw new StatusException("Could not get a Desktop: null", null);
82         }
83     }
84 
85     /**
86      * Creating a Testenvironment for the interfaces to be tested.
87      * Creates an instance of the service
88      * <code>com.sun.star.inspection.ObjectInspectorModel</code> with a <code>DefaultContext</code> and
89      * <code>PropertyHandlerFactroy[]</code> as parameter
90      *
91      * @param tParam the tests parameter
92      * @param log the logger
93      * @return the test environement
94      * @see util.DesktopTools
95      * @see helper.PropertyHandlerImpl
96      */
97     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
98 
99         this.cleanup(tParam, log);
100 
101         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
102 
103         XPropertySet xMSFProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xMSF);
104         XComponentContext xDefaultContext = null;
105         try{
106             // Get the default context from the office server.
107             Object oDefaultContext = xMSFProp.getPropertyValue("DefaultContext");
108 
109             // Query for the interface XComponentContext.
110             xDefaultContext = (XComponentContext) UnoRuntime.queryInterface(
111                     XComponentContext.class, oDefaultContext);
112 
113         } catch (UnknownPropertyException e){
114             throw new StatusException("could not get DefaultContext from xMSF", e);
115         } catch (WrappedTargetException e){
116             throw new StatusException("could not get DefaultContext from xMSF", e);
117         } catch (Exception e){
118             throw new StatusException("could not get DefaultContext from xMSF", e);
119         }
120 
121         try {
122 
123             Object[] oHandlerFactories = new Object[1];
124             oHandlerFactories[0] = new PropertyHandlerFactroy();
125 
126             int minHelpTextLines = 200;
127             int maxHelpTextLines = 400;
128 
129             XObjectInspectorModel oInspectorModel = com.sun.star.inspection.ObjectInspectorModel.
130                     createWithHandlerFactoriesAndHelpSection(xDefaultContext, oHandlerFactories,
131                                                              minHelpTextLines, maxHelpTextLines);
132 
133             log.println("ImplementationName '" + utils.getImplName(oInspectorModel) + "'");
134 
135             TestEnvironment tEnv = new TestEnvironment(oInspectorModel);
136 
137             // com.sun.star.inspection.XObjectInspectorModel
138             tEnv.addObjRelation("minHelpTextLines", new Integer(minHelpTextLines));
139             tEnv.addObjRelation("maxHelpTextLines", new Integer(maxHelpTextLines));
140 
141             return tEnv;
142         } catch (com.sun.star.uno.Exception e) {
143             e.printStackTrace(log);
144             throw new StatusException("Unexpected exception", e);
145         }
146 
147     }
148 
149     /**
150      * Closes the ObjectOnspector using <CODE>XCloseable</CODE>
151      * @see com.sun.star.util.XCloseable
152      * @param Param the test parameter
153      * @param log the logger
154      */
155     protected void cleanup(TestParameters Param, PrintWriter log) {
156         log.println("    Closing dialog if one exists ... ");
157 
158         XFrame existentInspector = null;
159 
160         XFrame xFrame = (XFrame) UnoRuntime.queryInterface(XFrame.class, StarDesktop);
161 
162         existentInspector = xFrame.findFrame( "ObjectInspector", 255 );
163 
164         if ( existentInspector != null ){
165             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
166                     XCloseable.class, existentInspector);
167             try{
168                 closer.close(true);
169             } catch (CloseVetoException e){
170                 log.println("Could not close inspector: " + e.toString());
171             }
172         }
173     }
174 }
175