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 
26 import com.sun.star.awt.XWindow;
27 import com.sun.star.awt.XWindowPeer;
28 import com.sun.star.frame.XFrame;
29 import com.sun.star.frame.XFrames;
30 import com.sun.star.frame.XFramesSupplier;
31 import com.sun.star.inspection.XObjectInspector;
32 import com.sun.star.inspection.XObjectInspectorModel;
33 import com.sun.star.lang.XInitialization;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.util.CloseVetoException;
36 import com.sun.star.util.XCloseable;
37 import helper.PropertyHandlerImpl;
38 import java.io.PrintWriter;
39 
40 import lib.StatusException;
41 import lib.TestCase;
42 import lib.TestEnvironment;
43 import lib.TestParameters;
44 
45 import com.sun.star.lang.XMultiServiceFactory;
46 import com.sun.star.uno.XInterface;
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.ObjectInspector</code>. <p>
53  * Object implements the following interfaces :
54  * <ul>
55  *  <li> <code>com::sun::star::lang::XComponent</code></li>
56  *  <li> <code>com::sun::star::inspection::XObjectInspector</code></li>
57  *  <li> <code>com::sun::star::frame::XController</code></li>
58  * </ul>
59  * This object test <b> is NOT </b> designed to be run in several
60  * threads concurently.
61  * @see com.sun.star.lang.XComponent
62  * @see com.sun.star.inspection.XObjectInspector
63  * @see com.sun.star.frame.XController
64  */
65 public class ObjectInspector extends TestCase {
66 
67     /**
68      * module variable which holds the Desktop
69      * @see com.sun.star.frame.Desktop
70      */
71     protected static Object StarDesktop = null;
72 
73     /**
74      * assign to the module variable <CODE>StarDesktop</CODE> the desktop
75      * @param Param the test parameters
76      * @param log the log writer
77      * @see lib.TestParameters
78      * @see share.LogWriter
79      * @see com.sun.star.frame.Desktop
80      */
initialize(TestParameters Param, PrintWriter log)81     protected void initialize(TestParameters Param, PrintWriter log) {
82         log.println("create a desktop...");
83         StarDesktop = DesktopTools.createDesktop((XMultiServiceFactory) Param.getMSF());
84         if (StarDesktop == null){
85             throw new StatusException("Could not get a Desktop: null", null);
86         }
87     }
88 
89     /**
90      * Creating a Testenvironment for the interfaces to be tested.
91      * Creates an instance of the service
92      * <code>com.sun.star.inspection.ObjectInspector</code> and adds it to
93      * a floating frame.<br>
94      * To test <CODE>com.sun.star.inspection.XObjectInspector.inspect()</CODE>
95      * an own implementation of
96      * <CODE>com.sun.star.inspection.XPropertyHandler</CODE> was used.
97      * @param tParam the tests parameter
98      * @param log the logger
99      * @return the test environement
100      * @see util.DesktopTools
101      * @see helper.PropertyHandlerImpl
102      */
createTestEnvironment(TestParameters tParam, PrintWriter log)103     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
104 
105         this.cleanup(tParam, log);
106 
107         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
108 
109         try {
110             XInterface oInspector = (XInterface) xMSF.createInstance("com.sun.star.inspection.ObjectInspector");
111 
112             XObjectInspector xInspector = (XObjectInspector) UnoRuntime.queryInterface(XObjectInspector.class, oInspector);
113 
114             log.println("ImplementationName '" + utils.getImplName(xInspector) + "'");
115 
116             XInterface oInspectorModel = (XInterface) xMSF.createInstance("com.sun.star.inspection.ObjectInspectorModel");
117 
118             XObjectInspectorModel xInspectorModel = (XObjectInspectorModel)
119             UnoRuntime.queryInterface(XObjectInspectorModel.class, oInspectorModel);
120 
121             XInterface oInspectorModelToSet = (XInterface) xMSF.createInstance("com.sun.star.inspection.ObjectInspectorModel");
122 
123             XObjectInspectorModel xInspectorModelToSet = (XObjectInspectorModel)
124             UnoRuntime.queryInterface(XObjectInspectorModel.class, oInspectorModelToSet);
125 
126 
127             log.println("create a floating frame...");
128 
129             XWindow xWindow = null;
130             try{
131 
132                 XWindowPeer xWindowPeer = DesktopTools.createFloatingWindow(xMSF);
133 
134                 xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, xWindowPeer);
135 
136             } catch (StatusException e){
137                 throw new StatusException("Coud not create test object", e);
138             }
139 
140             XInterface oFrame = (XInterface) xMSF.createInstance("com.sun.star.frame.Frame");
141 
142             XFrame xFrame = (XFrame) UnoRuntime.queryInterface(XFrame.class, oFrame);
143 
144             xFrame.setName("ObjectInspector");
145             xFrame.initialize(xWindow);
146 
147             XFramesSupplier xFramesSup = (XFramesSupplier) UnoRuntime.queryInterface(XFramesSupplier.class, StarDesktop);
148 
149             XFrames xFrames = xFramesSup.getFrames();
150             xFrames.append(xFrame);
151 
152 
153             log.println("attach ObjectInspector to floating frame...");
154 
155             XInitialization xOII = (XInitialization) UnoRuntime.queryInterface(XInitialization.class, xInspectorModel);
156 
157             xOII.initialize(new Object[0]);
158 
159             xInspector.setInspectorModel(xInspectorModel);
160 
161             // for debug purposes the following lines could commented out. But in
162             // this case the com.sun.star.frame.XController would be failed!
163             //xInspector.attachFrame(xFrame);
164             //xWindow.setVisible(true);
165 
166             Object[] oInspect = new Object[1];
167             oInspect[0] = new PropertyHandlerImpl();
168 
169             TestEnvironment tEnv = new TestEnvironment(xInspector);
170 
171             // com.sun.star.frame.XController
172             tEnv.addObjRelation("Frame",xFrame);
173 
174             tEnv.addObjRelation("XObjectInspector.toInspect", oInspect);
175 
176             tEnv.addObjRelation("XObjectInspector.InspectorModelToSet", xInspectorModelToSet);
177 
178             return tEnv;
179         } catch (com.sun.star.uno.Exception e) {
180             e.printStackTrace(log);
181             throw new StatusException("Unexpected exception", e);
182         }
183 
184     }
185 
186     /**
187      * Closes the ObjectOnspector using <CODE>XCloseable</CODE>
188      * @see com.sun.star.util.XCloseable
189      * @param Param the test parameter
190      * @param log the logger
191      */
cleanup(TestParameters Param, PrintWriter log)192     protected void cleanup(TestParameters Param, PrintWriter log) {
193         log.println("    Closing dialog if one exists ... ");
194 
195         XFrame existentInspector = null;
196 
197         XFrame xFrame = (XFrame) UnoRuntime.queryInterface(XFrame.class, StarDesktop);
198 
199         existentInspector = xFrame.findFrame( "ObjectInspector", 255 );
200 
201         if ( existentInspector != null ){
202             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
203                     XCloseable.class, existentInspector);
204             try{
205                 closer.close(true);
206             } catch (CloseVetoException e){
207                 log.println("Could not close inspector: " + e.toString());
208             }
209         }
210     }
211 }
212