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