1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package ifc.container;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import java.io.PrintWriter;
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir import lib.MultiMethodTest;
33*cdf0e10cSrcweir import lib.Status;
34*cdf0e10cSrcweir import lib.StatusException;
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir import com.sun.star.awt.XControl;
37*cdf0e10cSrcweir import com.sun.star.awt.XControlContainer;
38*cdf0e10cSrcweir import com.sun.star.container.ContainerEvent;
39*cdf0e10cSrcweir import com.sun.star.container.XContainer;
40*cdf0e10cSrcweir import com.sun.star.container.XContainerListener;
41*cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
42*cdf0e10cSrcweir import com.sun.star.container.XNameReplace;
43*cdf0e10cSrcweir import com.sun.star.lang.EventObject;
44*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
45*cdf0e10cSrcweir import com.sun.star.uno.XNamingService;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir /**
49*cdf0e10cSrcweir * Testing <code>com.sun.star.container.XContainer</code>
50*cdf0e10cSrcweir * interface methods :
51*cdf0e10cSrcweir * <ul>
52*cdf0e10cSrcweir *  <li><code> addContainerListener()</code></li>
53*cdf0e10cSrcweir *  <li><code> removeContainerListener()</code></li>
54*cdf0e10cSrcweir * </ul>
55*cdf0e10cSrcweir * This test needs the following object relations :
56*cdf0e10cSrcweir * <ul>
57*cdf0e10cSrcweir *  <li> <code>'INSTANCE'</code> : Object which can be inserted into
58*cdf0e10cSrcweir *    container.</li>
59*cdf0e10cSrcweir *  <li> <code>'INSTANCE2'</code> : <b>(optional)</b>
60*cdf0e10cSrcweir *     Object which can be inserted into container. The relation
61*cdf0e10cSrcweir *     must be specified when container cann't contain two
62*cdf0e10cSrcweir *     identical objects. Replaces the first instance.</li>
63*cdf0e10cSrcweir *  <li> <code>'XContainer.Container'</code> (of type
64*cdf0e10cSrcweir *  <code>com.sun.star.container.XNameContainer</code>)
65*cdf0e10cSrcweir *  <b>optional</b> : is required when the tested component
66*cdf0e10cSrcweir *  doesn't implement <code>XNameContainer</code> and is used
67*cdf0e10cSrcweir *  for adding and removing elements.</li>
68*cdf0e10cSrcweir * <ul> <p>
69*cdf0e10cSrcweir * Object <b>must implement</b>
70*cdf0e10cSrcweir * <code>com.sun.star.container.XNameContainer</code>.
71*cdf0e10cSrcweir * <p>
72*cdf0e10cSrcweir * Test is <b> NOT </b> multithread compilant. <p>
73*cdf0e10cSrcweir * @see com.sun.star.container.XContainer
74*cdf0e10cSrcweir */
75*cdf0e10cSrcweir public class _XContainer extends MultiMethodTest {
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     public XContainer oObj = null;
78*cdf0e10cSrcweir     private boolean bElementInserted = false;
79*cdf0e10cSrcweir     private boolean bElementRemoved  = false;
80*cdf0e10cSrcweir     private boolean bElementReplaced = false;
81*cdf0e10cSrcweir     private PrintWriter _log = null;
82*cdf0e10cSrcweir     private XNameContainer NC = null ;
83*cdf0e10cSrcweir     private XControlContainer CC = null ;
84*cdf0e10cSrcweir     private XNamingService NV = null ;
85*cdf0e10cSrcweir     private XNameReplace NR = null ;
86*cdf0e10cSrcweir     private Object inst = null ;
87*cdf0e10cSrcweir     private Object inst2 = null ;
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir     /**
90*cdf0e10cSrcweir     * Retrieves object relations, and tries to query object for
91*cdf0e10cSrcweir     * <code>XNameContainer</code> interface.
92*cdf0e10cSrcweir     * @throws StatusException If one of relations not found or
93*cdf0e10cSrcweir     * object doesn't implement <code>XNameContainer</code> interface.
94*cdf0e10cSrcweir     */
95*cdf0e10cSrcweir     public void before() throws StatusException {
96*cdf0e10cSrcweir         _log = log;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir         // do this test with a different object
99*cdf0e10cSrcweir         Object altObj = tEnv.getObjRelation("XContainer.AlternateObject");
100*cdf0e10cSrcweir         if (altObj != null) {
101*cdf0e10cSrcweir             oObj = (XContainer)UnoRuntime.queryInterface(XContainer.class, altObj);
102*cdf0e10cSrcweir         }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         NC = (XNameContainer) UnoRuntime.queryInterface
105*cdf0e10cSrcweir             (XNameContainer.class, oObj) ;
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir         Object container = null;
108*cdf0e10cSrcweir         if (NC == null) {
109*cdf0e10cSrcweir             container = tEnv.getObjRelation("XContainer.Container") ;
110*cdf0e10cSrcweir         }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir         if (container != null) {
113*cdf0e10cSrcweir             if (container instanceof com.sun.star.awt.XControlContainer) {
114*cdf0e10cSrcweir                 CC = (XControlContainer) container;
115*cdf0e10cSrcweir             } else if (container instanceof com.sun.star.uno.XNamingService) {
116*cdf0e10cSrcweir                 NV = (XNamingService) container;
117*cdf0e10cSrcweir             } else if (container instanceof com.sun.star.container.XNameReplace) {
118*cdf0e10cSrcweir                 NR = (XNameReplace) container;
119*cdf0e10cSrcweir                 inst2 = tEnv.getObjRelation("XContainer.NewValue");
120*cdf0e10cSrcweir                 inst = tEnv.getObjRelation("XContainer.ElementName");
121*cdf0e10cSrcweir             } else if (container instanceof com.sun.star.container.XNameContainer) {
122*cdf0e10cSrcweir                 NC = (XNameContainer) container;
123*cdf0e10cSrcweir             }
124*cdf0e10cSrcweir         }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir         if (NC == null && CC == null && NV == null && NR == null)
127*cdf0e10cSrcweir             throw new StatusException(
128*cdf0e10cSrcweir                 Status.failed("Neither object implements XNameContainer" +
129*cdf0e10cSrcweir                     " nor relation 'XContainer.Container' found.")) ;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir         if (inst == null)
132*cdf0e10cSrcweir             inst = tEnv.getObjRelation("INSTANCE");
133*cdf0e10cSrcweir         if (inst == null) {
134*cdf0e10cSrcweir             log.println("No INSTANCE ObjRelation!!! ");
135*cdf0e10cSrcweir             throw new StatusException(Status.failed("No INSTANCE ObjRelation!!!")) ;
136*cdf0e10cSrcweir         }
137*cdf0e10cSrcweir         if (inst2 == null)
138*cdf0e10cSrcweir             inst2 = tEnv.getObjRelation("INSTANCE2");
139*cdf0e10cSrcweir     }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     /**
142*cdf0e10cSrcweir     * Listener implementation which just set flags on appropriate
143*cdf0e10cSrcweir     * events.
144*cdf0e10cSrcweir     */
145*cdf0e10cSrcweir     public class MyListener implements XContainerListener {
146*cdf0e10cSrcweir          public void elementInserted(ContainerEvent e) {
147*cdf0e10cSrcweir             //_log.println("Element was inserted");
148*cdf0e10cSrcweir             bElementInserted = true;
149*cdf0e10cSrcweir          }
150*cdf0e10cSrcweir          public void elementRemoved(ContainerEvent e) {
151*cdf0e10cSrcweir             //_log.println("Element was removed");
152*cdf0e10cSrcweir             bElementRemoved = true;
153*cdf0e10cSrcweir          }
154*cdf0e10cSrcweir          public void elementReplaced(ContainerEvent e) {
155*cdf0e10cSrcweir             //_log.println("Element was replaced");
156*cdf0e10cSrcweir             bElementReplaced = true;
157*cdf0e10cSrcweir          }
158*cdf0e10cSrcweir          public void disposing (EventObject obj) {}
159*cdf0e10cSrcweir     };
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir     MyListener listener = new MyListener();
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     /**
164*cdf0e10cSrcweir     * Adds <code>MyListener</code> and performs all possible changes
165*cdf0e10cSrcweir     * (insert, replace, remove) with container. The checks which
166*cdf0e10cSrcweir     * events were called. <p>
167*cdf0e10cSrcweir     * Has <b>OK</b> status if all methods of the listener were called.
168*cdf0e10cSrcweir     */
169*cdf0e10cSrcweir     public void _addContainerListener() {
170*cdf0e10cSrcweir         boolean bResult = true;
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir         oObj.addContainerListener(listener);
173*cdf0e10cSrcweir         bResult &= performChanges();
174*cdf0e10cSrcweir         //we can't replace if the container is XControlContainer
175*cdf0e10cSrcweir         if (NC != null) bResult &= bElementReplaced;
176*cdf0e10cSrcweir         // we do not remove and insert if the listener is triggered by XNameReplace
177*cdf0e10cSrcweir         if (NR == null) bResult &= bElementRemoved;
178*cdf0e10cSrcweir         if (NR == null) bResult &= bElementInserted;
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir         if (!bResult) {
181*cdf0e10cSrcweir             log.println("inserted was " + (bElementInserted ? "" : "NOT")
182*cdf0e10cSrcweir                                                                 + " called.");
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir             if (NC != null) {
185*cdf0e10cSrcweir                 log.println("replaced was " + (bElementReplaced ? "" : "NOT")
186*cdf0e10cSrcweir                                                                     + " called.");
187*cdf0e10cSrcweir             }
188*cdf0e10cSrcweir             log.println("removed was " + (bElementRemoved ? "" : "NOT")
189*cdf0e10cSrcweir                                                                 + " called.");
190*cdf0e10cSrcweir         }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir         tRes.tested("addContainerListener()", bResult);
193*cdf0e10cSrcweir     }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir     /**
196*cdf0e10cSrcweir     * Removes listener added before and performs all possible changes
197*cdf0e10cSrcweir     * (insert, replace, remove) with container. The checks which
198*cdf0e10cSrcweir     * events were called. <p>
199*cdf0e10cSrcweir     * Has <b>OK</b> status if no methods of the listener were called. <p>
200*cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
201*cdf0e10cSrcweir     * <ul>
202*cdf0e10cSrcweir     *  <li> <code> addContainerListener() </code> : to remove it now. </li>
203*cdf0e10cSrcweir     * </ul>
204*cdf0e10cSrcweir     */
205*cdf0e10cSrcweir     public void _removeContainerListener() {
206*cdf0e10cSrcweir         requiredMethod("addContainerListener()") ;
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir         boolean bResult = true;
209*cdf0e10cSrcweir         bElementReplaced = bElementRemoved = bElementInserted = false;
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir         oObj.removeContainerListener(listener);
212*cdf0e10cSrcweir         bResult &= performChanges();
213*cdf0e10cSrcweir         bResult &= !bElementReplaced;
214*cdf0e10cSrcweir         bResult &= !bElementRemoved;
215*cdf0e10cSrcweir         bResult &= !bElementInserted;
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir         tRes.tested("removeContainerListener()", bResult);
218*cdf0e10cSrcweir     }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir     /**
221*cdf0e10cSrcweir     * Inserts, replaces and finally removes object from container.
222*cdf0e10cSrcweir     * Object is gotten from <code>'INSTANCE'</code> relation. If
223*cdf0e10cSrcweir     * the relation <code>'INSTANCE2'</code> exists then the first
224*cdf0e10cSrcweir     * instance is replaced with second.
225*cdf0e10cSrcweir     */
226*cdf0e10cSrcweir     protected boolean performChanges() {
227*cdf0e10cSrcweir         if (CC != null) return performChanges2();
228*cdf0e10cSrcweir         if (NV != null) return performChanges3();
229*cdf0e10cSrcweir         if (NR != null) return performChanges4();
230*cdf0e10cSrcweir         boolean bResult = true;
231*cdf0e10cSrcweir         try {
232*cdf0e10cSrcweir             String[] names = NC.getElementNames();
233*cdf0e10cSrcweir             log.println("Elements count = " + names.length);
234*cdf0e10cSrcweir             NC.insertByName("XContainer_dummy", inst);
235*cdf0e10cSrcweir             names = NC.getElementNames();
236*cdf0e10cSrcweir             log.println("Elements count = " + names.length);
237*cdf0e10cSrcweir             if (inst2 == null) {
238*cdf0e10cSrcweir                 NC.replaceByName("XContainer_dummy", inst);
239*cdf0e10cSrcweir             } else {
240*cdf0e10cSrcweir                 NC.replaceByName("XContainer_dummy", inst2);
241*cdf0e10cSrcweir             }
242*cdf0e10cSrcweir             NC.removeByName("XContainer_dummy");
243*cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException ex) {
244*cdf0e10cSrcweir             log.println("Exception occured ");
245*cdf0e10cSrcweir             ex.printStackTrace(log);
246*cdf0e10cSrcweir             bResult = false;
247*cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException ex) {
248*cdf0e10cSrcweir             log.println("Exception occured ");
249*cdf0e10cSrcweir             ex.printStackTrace(log);
250*cdf0e10cSrcweir             bResult = false;
251*cdf0e10cSrcweir         } catch (com.sun.star.container.NoSuchElementException ex) {
252*cdf0e10cSrcweir             log.println("Exception occured ");
253*cdf0e10cSrcweir             ex.printStackTrace(log);
254*cdf0e10cSrcweir             bResult = false;
255*cdf0e10cSrcweir         } catch (com.sun.star.container.ElementExistException ex) {
256*cdf0e10cSrcweir             log.println("Exception occured ");
257*cdf0e10cSrcweir             ex.printStackTrace(log);
258*cdf0e10cSrcweir             bResult = false;
259*cdf0e10cSrcweir         }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir         return bResult;
262*cdf0e10cSrcweir     }
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir     /**
265*cdf0e10cSrcweir     * In case no XNameContainer is available, but a XControlContainer
266*cdf0e10cSrcweir     * instead.
267*cdf0e10cSrcweir     * the XControl instance is inserted
268*cdf0e10cSrcweir     * Method returns true if the count of Controls is changed afterwards
269*cdf0e10cSrcweir     */
270*cdf0e10cSrcweir     protected boolean performChanges2() {
271*cdf0e10cSrcweir         int precount = CC.getControls().length;
272*cdf0e10cSrcweir         CC.addControl("NewControl",(XControl) inst);
273*cdf0e10cSrcweir         shortWait();
274*cdf0e10cSrcweir         int count = CC.getControls().length;
275*cdf0e10cSrcweir         CC.removeControl(CC.getControl("NewControl"));
276*cdf0e10cSrcweir         shortWait();
277*cdf0e10cSrcweir         return count>precount;
278*cdf0e10cSrcweir     }
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir     /**
281*cdf0e10cSrcweir     * In case no XNameContainer is available, but a XNamingService
282*cdf0e10cSrcweir     * instead.
283*cdf0e10cSrcweir     * the instance is registered and revoked again
284*cdf0e10cSrcweir     * Method return true if getRegisteredObject() works after
285*cdf0e10cSrcweir     * registering and doesn't after revoke
286*cdf0e10cSrcweir     */
287*cdf0e10cSrcweir     protected boolean performChanges3() {
288*cdf0e10cSrcweir         boolean res = true;
289*cdf0e10cSrcweir         Object reg = null;
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir         try {
292*cdf0e10cSrcweir             reg = NV.getRegisteredObject("MyFactory");
293*cdf0e10cSrcweir             NV.revokeObject("MyFactory");
294*cdf0e10cSrcweir         } catch (Exception e) {
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir         }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir         try {
299*cdf0e10cSrcweir             NV.registerObject("MyFactory", inst);
300*cdf0e10cSrcweir             reg = NV.getRegisteredObject("MyFactory");
301*cdf0e10cSrcweir             res &= (reg != null);
302*cdf0e10cSrcweir         } catch (Exception e) {
303*cdf0e10cSrcweir             e.printStackTrace(log);
304*cdf0e10cSrcweir             log.println("registerObject failed");
305*cdf0e10cSrcweir             res &= false;
306*cdf0e10cSrcweir         }
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir         try {
309*cdf0e10cSrcweir             NV.revokeObject("MyFactory");
310*cdf0e10cSrcweir             reg = NV.getRegisteredObject("MyFactory");
311*cdf0e10cSrcweir             log.println("revokeObject failed");
312*cdf0e10cSrcweir             res &= false;
313*cdf0e10cSrcweir         } catch (Exception e) {
314*cdf0e10cSrcweir             res &= true;
315*cdf0e10cSrcweir         }
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir         return res;
318*cdf0e10cSrcweir     }
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir     /**
321*cdf0e10cSrcweir     * In case no XNameContainer is available, but a XNamingReplace
322*cdf0e10cSrcweir     * instead.
323*cdf0e10cSrcweir     */
324*cdf0e10cSrcweir     protected boolean performChanges4() {
325*cdf0e10cSrcweir         boolean res = true;
326*cdf0e10cSrcweir         Object newValue = inst2;
327*cdf0e10cSrcweir         Object originalValue = null;
328*cdf0e10cSrcweir         String name = null;
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir         try {
331*cdf0e10cSrcweir             name = (String)inst;
332*cdf0e10cSrcweir         }
333*cdf0e10cSrcweir         catch(java.lang.ClassCastException e) {
334*cdf0e10cSrcweir             log.write("Expected String as object relations 'XContainer.ElementName'.");
335*cdf0e10cSrcweir             e.printStackTrace(log);
336*cdf0e10cSrcweir             return false;
337*cdf0e10cSrcweir         }
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir         try {
340*cdf0e10cSrcweir             originalValue = NR.getByName(name);
341*cdf0e10cSrcweir             NR.replaceByName(name, newValue);
342*cdf0e10cSrcweir         } catch (Exception e) {
343*cdf0e10cSrcweir             e.printStackTrace(log);
344*cdf0e10cSrcweir             res = false;
345*cdf0e10cSrcweir         }
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir         try {
348*cdf0e10cSrcweir             NR.replaceByName(name, originalValue);
349*cdf0e10cSrcweir         } catch (Exception e) {
350*cdf0e10cSrcweir             e.printStackTrace(log);
351*cdf0e10cSrcweir             res = false;
352*cdf0e10cSrcweir         }
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir         return res;
355*cdf0e10cSrcweir     }
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir     /**
358*cdf0e10cSrcweir     * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
359*cdf0e10cSrcweir     * reset</code> call.
360*cdf0e10cSrcweir     */
361*cdf0e10cSrcweir     private void shortWait() {
362*cdf0e10cSrcweir         try {
363*cdf0e10cSrcweir             Thread.sleep(1000) ;
364*cdf0e10cSrcweir         } catch (InterruptedException e) {
365*cdf0e10cSrcweir             log.println("While waiting :" + e) ;
366*cdf0e10cSrcweir         }
367*cdf0e10cSrcweir     }
368*cdf0e10cSrcweir }
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir 
371