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