1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package mod._pcr;
25cdf0e10cSrcweir import com.sun.star.beans.UnknownPropertyException;
26cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
27cdf0e10cSrcweir import com.sun.star.frame.XFrame;
28cdf0e10cSrcweir import com.sun.star.inspection.XObjectInspectorModel;
29cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException;
30cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
31cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
32cdf0e10cSrcweir import com.sun.star.util.CloseVetoException;
33cdf0e10cSrcweir import com.sun.star.util.XCloseable;
34cdf0e10cSrcweir import helper.PropertyHandlerFactroy;
35cdf0e10cSrcweir import java.io.PrintWriter;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir import lib.StatusException;
38cdf0e10cSrcweir import lib.TestCase;
39cdf0e10cSrcweir import lib.TestEnvironment;
40cdf0e10cSrcweir import lib.TestParameters;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
43cdf0e10cSrcweir import util.DesktopTools;
44cdf0e10cSrcweir import util.utils;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir /**
47cdf0e10cSrcweir  * Test for object which is represented by service
48cdf0e10cSrcweir  * <code>com.sun.star.reflection.ObjectInspectorModel</code>. <p>
49cdf0e10cSrcweir  * Object implements the following interfaces :
50cdf0e10cSrcweir  * <ul>
51cdf0e10cSrcweir  *  <li> <code>com::sun::star::inspection::XObjectInspectorModel</code></li>
52cdf0e10cSrcweir  * </ul>
53cdf0e10cSrcweir  * This object test <b> is NOT </b> designed to be run in several
54cdf0e10cSrcweir  * threads concurently.
55cdf0e10cSrcweir  * @see com.sun.star.inspection.XObjectInspectorModel
56cdf0e10cSrcweir  */
57cdf0e10cSrcweir public class ObjectInspectorModel extends TestCase {
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     /**
60cdf0e10cSrcweir      * module variable which holds the Desktop
61cdf0e10cSrcweir      * @see com.sun.star.frame.Desktop
62cdf0e10cSrcweir      */
63cdf0e10cSrcweir     protected static Object StarDesktop = null;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     /**
66cdf0e10cSrcweir      * assign to the module variable <CODE>StarDesktop</CODE> the desktop
67cdf0e10cSrcweir      * @param Param the test parameters
68cdf0e10cSrcweir      * @param log the log writer
69cdf0e10cSrcweir      * @see lib.TestParameters
70cdf0e10cSrcweir      * @see share.LogWriter
71cdf0e10cSrcweir      * @see com.sun.star.frame.Desktop
72cdf0e10cSrcweir      */
initialize(TestParameters Param, PrintWriter log)73cdf0e10cSrcweir     protected void initialize(TestParameters Param, PrintWriter log) {
74cdf0e10cSrcweir         log.println("create a desktop...");
75cdf0e10cSrcweir         StarDesktop = DesktopTools.createDesktop((XMultiServiceFactory) Param.getMSF());
76cdf0e10cSrcweir         if (StarDesktop == null){
77cdf0e10cSrcweir             throw new StatusException("Could not get a Desktop: null", null);
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     /**
82cdf0e10cSrcweir      * Creating a Testenvironment for the interfaces to be tested.
83cdf0e10cSrcweir      * Creates an instance of the service
84cdf0e10cSrcweir      * <code>com.sun.star.inspection.ObjectInspectorModel</code> with a <code>DefaultContext</code> and
85cdf0e10cSrcweir      * <code>PropertyHandlerFactroy[]</code> as parameter
86cdf0e10cSrcweir      *
87cdf0e10cSrcweir      * @param tParam the tests parameter
88cdf0e10cSrcweir      * @param log the logger
89cdf0e10cSrcweir      * @return the test environement
90cdf0e10cSrcweir      * @see util.DesktopTools
91cdf0e10cSrcweir      * @see helper.PropertyHandlerImpl
92cdf0e10cSrcweir      */
createTestEnvironment(TestParameters tParam, PrintWriter log)93cdf0e10cSrcweir     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         this.cleanup(tParam, log);
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         XPropertySet xMSFProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xMSF);
100cdf0e10cSrcweir         XComponentContext xDefaultContext = null;
101cdf0e10cSrcweir         try{
102cdf0e10cSrcweir             // Get the default context from the office server.
103cdf0e10cSrcweir             Object oDefaultContext = xMSFProp.getPropertyValue("DefaultContext");
104cdf0e10cSrcweir 
105cdf0e10cSrcweir             // Query for the interface XComponentContext.
106cdf0e10cSrcweir             xDefaultContext = (XComponentContext) UnoRuntime.queryInterface(
107cdf0e10cSrcweir                     XComponentContext.class, oDefaultContext);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         } catch (UnknownPropertyException e){
110cdf0e10cSrcweir             throw new StatusException("could not get DefaultContext from xMSF", e);
111cdf0e10cSrcweir         } catch (WrappedTargetException e){
112cdf0e10cSrcweir             throw new StatusException("could not get DefaultContext from xMSF", e);
113cdf0e10cSrcweir         } catch (Exception e){
114cdf0e10cSrcweir             throw new StatusException("could not get DefaultContext from xMSF", e);
115cdf0e10cSrcweir         }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir         try {
118cdf0e10cSrcweir 
119cdf0e10cSrcweir             Object[] oHandlerFactories = new Object[1];
120cdf0e10cSrcweir             oHandlerFactories[0] = new PropertyHandlerFactroy();
121cdf0e10cSrcweir 
122cdf0e10cSrcweir             int minHelpTextLines = 200;
123cdf0e10cSrcweir             int maxHelpTextLines = 400;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir             XObjectInspectorModel oInspectorModel = com.sun.star.inspection.ObjectInspectorModel.
126cdf0e10cSrcweir                     createWithHandlerFactoriesAndHelpSection(xDefaultContext, oHandlerFactories,
127cdf0e10cSrcweir                                                              minHelpTextLines, maxHelpTextLines);
128cdf0e10cSrcweir 
129cdf0e10cSrcweir             log.println("ImplementationName '" + utils.getImplName(oInspectorModel) + "'");
130cdf0e10cSrcweir 
131cdf0e10cSrcweir             TestEnvironment tEnv = new TestEnvironment(oInspectorModel);
132cdf0e10cSrcweir 
133cdf0e10cSrcweir             // com.sun.star.inspection.XObjectInspectorModel
134cdf0e10cSrcweir             tEnv.addObjRelation("minHelpTextLines", new Integer(minHelpTextLines));
135cdf0e10cSrcweir             tEnv.addObjRelation("maxHelpTextLines", new Integer(maxHelpTextLines));
136cdf0e10cSrcweir 
137cdf0e10cSrcweir             return tEnv;
138cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
139cdf0e10cSrcweir             e.printStackTrace(log);
140cdf0e10cSrcweir             throw new StatusException("Unexpected exception", e);
141cdf0e10cSrcweir         }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     /**
146cdf0e10cSrcweir      * Closes the ObjectOnspector using <CODE>XCloseable</CODE>
147cdf0e10cSrcweir      * @see com.sun.star.util.XCloseable
148cdf0e10cSrcweir      * @param Param the test parameter
149cdf0e10cSrcweir      * @param log the logger
150cdf0e10cSrcweir      */
cleanup(TestParameters Param, PrintWriter log)151cdf0e10cSrcweir     protected void cleanup(TestParameters Param, PrintWriter log) {
152cdf0e10cSrcweir         log.println("    Closing dialog if one exists ... ");
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         XFrame existentInspector = null;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         XFrame xFrame = (XFrame) UnoRuntime.queryInterface(XFrame.class, StarDesktop);
157cdf0e10cSrcweir 
158cdf0e10cSrcweir         existentInspector = xFrame.findFrame( "ObjectInspector", 255 );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir         if ( existentInspector != null ){
161cdf0e10cSrcweir             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
162cdf0e10cSrcweir                     XCloseable.class, existentInspector);
163cdf0e10cSrcweir             try{
164cdf0e10cSrcweir                 closer.close(true);
165cdf0e10cSrcweir             } catch (CloseVetoException e){
166cdf0e10cSrcweir                 log.println("Could not close inspector: " + e.toString());
167cdf0e10cSrcweir             }
168cdf0e10cSrcweir         }
169cdf0e10cSrcweir     }
170cdf0e10cSrcweir }
171