2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc
.container
;
21 import lib
.MultiMethodTest
;
23 import lib
.StatusException
;
25 import com
.sun
.star
.awt
.XControl
;
26 import com
.sun
.star
.awt
.XControlContainer
;
27 import com
.sun
.star
.container
.ContainerEvent
;
28 import com
.sun
.star
.container
.XContainer
;
29 import com
.sun
.star
.container
.XContainerListener
;
30 import com
.sun
.star
.container
.XNameContainer
;
31 import com
.sun
.star
.container
.XNameReplace
;
32 import com
.sun
.star
.lang
.EventObject
;
33 import com
.sun
.star
.uno
.UnoRuntime
;
34 import com
.sun
.star
.uno
.XNamingService
;
38 * Testing <code>com.sun.star.container.XContainer</code>
41 * <li><code> addContainerListener()</code></li>
42 * <li><code> removeContainerListener()</code></li>
44 * This test needs the following object relations :
46 * <li> <code>'INSTANCE'</code> : Object which can be inserted into
48 * <li> <code>'INSTANCE2'</code> : <b>(optional)</b>
49 * Object which can be inserted into container. The relation
50 * must be specified when container cann't contain two
51 * identical objects. Replaces the first instance.</li>
52 * <li> <code>'XContainer.Container'</code> (of type
53 * <code>com.sun.star.container.XNameContainer</code>)
54 * <b>optional</b> : is required when the tested component
55 * doesn't implement <code>XNameContainer</code> and is used
56 * for adding and removing elements.</li>
58 * Object <b>must implement</b>
59 * <code>com.sun.star.container.XNameContainer</code>.
61 * Test is <b> NOT </b> multithread compilant. <p>
62 * @see com.sun.star.container.XContainer
64 public class _XContainer
extends MultiMethodTest
{
66 public XContainer oObj
= null;
67 private boolean bElementInserted
= false;
68 private boolean bElementRemoved
= false;
69 private boolean bElementReplaced
= false;
70 private XNameContainer NC
= null ;
71 private XControlContainer CC
= null ;
72 private XNamingService NV
= null ;
73 private XNameReplace NR
= null ;
74 private Object inst
= null ;
75 private Object inst2
= null ;
78 * Retrieves object relations, and tries to query object for
79 * <code>XNameContainer</code> interface.
80 * @throws StatusException If one of relations not found or
81 * object doesn't implement <code>XNameContainer</code> interface.
83 public void before() throws StatusException
{
84 // do this test with a different object
85 Object altObj
= tEnv
.getObjRelation("XContainer.AlternateObject");
87 oObj
= UnoRuntime
.queryInterface(XContainer
.class, altObj
);
90 NC
= UnoRuntime
.queryInterface
91 (XNameContainer
.class, oObj
) ;
93 Object container
= null;
95 container
= tEnv
.getObjRelation("XContainer.Container") ;
98 if (container
!= null) {
99 if (container
instanceof com
.sun
.star
.awt
.XControlContainer
) {
100 CC
= (XControlContainer
) container
;
101 } else if (container
instanceof com
.sun
.star
.uno
.XNamingService
) {
102 NV
= (XNamingService
) container
;
103 } else if (container
instanceof com
.sun
.star
.container
.XNameReplace
) {
104 NR
= (XNameReplace
) container
;
105 inst2
= tEnv
.getObjRelation("XContainer.NewValue");
106 inst
= tEnv
.getObjRelation("XContainer.ElementName");
107 } else if (container
instanceof com
.sun
.star
.container
.XNameContainer
) {
108 NC
= (XNameContainer
) container
;
112 if (NC
== null && CC
== null && NV
== null && NR
== null)
113 throw new StatusException(
114 Status
.failed("Neither object implements XNameContainer" +
115 " nor relation 'XContainer.Container' found.")) ;
118 inst
= tEnv
.getObjRelation("INSTANCE");
120 log
.println("No INSTANCE ObjRelation!!! ");
121 throw new StatusException(Status
.failed("No INSTANCE ObjRelation!!!")) ;
124 inst2
= tEnv
.getObjRelation("INSTANCE2");
128 * Listener implementation which just set flags on appropriate
131 public class MyListener
implements XContainerListener
{
132 public void elementInserted(ContainerEvent e
) {
133 //_log.println("Element was inserted");
134 bElementInserted
= true;
136 public void elementRemoved(ContainerEvent e
) {
137 //_log.println("Element was removed");
138 bElementRemoved
= true;
140 public void elementReplaced(ContainerEvent e
) {
141 //_log.println("Element was replaced");
142 bElementReplaced
= true;
144 public void disposing (EventObject obj
) {}
147 MyListener listener
= new MyListener();
150 * Adds <code>MyListener</code> and performs all possible changes
151 * (insert, replace, remove) with container. The checks which
152 * events were called. <p>
153 * Has <b>OK</b> status if all methods of the listener were called.
155 public void _addContainerListener() {
156 boolean bResult
= true;
158 oObj
.addContainerListener(listener
);
159 bResult
&= performChanges();
160 //we can't replace if the container is XControlContainer
161 if (NC
!= null) bResult
&= bElementReplaced
;
162 // we do not remove and insert if the listener is triggered by XNameReplace
163 if (NR
== null) bResult
&= bElementRemoved
;
164 if (NR
== null) bResult
&= bElementInserted
;
167 log
.println("inserted was " + (bElementInserted ?
"" : "NOT")
171 log
.println("replaced was " + (bElementReplaced ?
"" : "NOT")
174 log
.println("removed was " + (bElementRemoved ?
"" : "NOT")
178 tRes
.tested("addContainerListener()", bResult
);
182 * Removes listener added before and performs all possible changes
183 * (insert, replace, remove) with container. The checks which
184 * events were called. <p>
185 * Has <b>OK</b> status if no methods of the listener were called. <p>
186 * The following method tests are to be completed successfully before :
188 * <li> <code> addContainerListener() </code> : to remove it now. </li>
191 public void _removeContainerListener() {
192 requiredMethod("addContainerListener()") ;
194 boolean bResult
= true;
195 bElementReplaced
= bElementRemoved
= bElementInserted
= false;
197 oObj
.removeContainerListener(listener
);
198 bResult
&= performChanges();
199 bResult
&= !bElementReplaced
;
200 bResult
&= !bElementRemoved
;
201 bResult
&= !bElementInserted
;
203 tRes
.tested("removeContainerListener()", bResult
);
207 * Inserts, replaces and finally removes object from container.
208 * Object is gotten from <code>'INSTANCE'</code> relation. If
209 * the relation <code>'INSTANCE2'</code> exists then the first
210 * instance is replaced with second.
212 protected boolean performChanges() {
213 if (CC
!= null) return performChanges2();
214 if (NV
!= null) return performChanges3();
215 if (NR
!= null) return performChanges4();
216 boolean bResult
= true;
218 String
[] names
= NC
.getElementNames();
219 log
.println("Elements count = " + names
.length
);
220 NC
.insertByName("XContainer_dummy", inst
);
221 names
= NC
.getElementNames();
222 log
.println("Elements count = " + names
.length
);
224 NC
.replaceByName("XContainer_dummy", inst
);
226 NC
.replaceByName("XContainer_dummy", inst2
);
228 NC
.removeByName("XContainer_dummy");
229 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
230 log
.println("Exception occurred ");
231 ex
.printStackTrace(log
);
233 } catch (com
.sun
.star
.lang
.WrappedTargetException ex
) {
234 log
.println("Exception occurred ");
235 ex
.printStackTrace(log
);
237 } catch (com
.sun
.star
.container
.NoSuchElementException ex
) {
238 log
.println("Exception occurred ");
239 ex
.printStackTrace(log
);
241 } catch (com
.sun
.star
.container
.ElementExistException ex
) {
242 log
.println("Exception occurred ");
243 ex
.printStackTrace(log
);
251 * In case no XNameContainer is available, but a XControlContainer
253 * the XControl instance is inserted
254 * Method returns true if the count of Controls is changed afterwards
256 protected boolean performChanges2() {
257 int precount
= CC
.getControls().length
;
258 CC
.addControl("NewControl",(XControl
) inst
);
260 int count
= CC
.getControls().length
;
261 CC
.removeControl(CC
.getControl("NewControl"));
263 return count
>precount
;
267 * In case no XNameContainer is available, but a XNamingService
269 * the instance is registered and revoked again
270 * Method return true if getRegisteredObject() works after
271 * registering and doesn't after revoke
273 protected boolean performChanges3() {
278 reg
= NV
.getRegisteredObject("MyFactory");
279 NV
.revokeObject("MyFactory");
280 } catch (Exception e
) {
285 NV
.registerObject("MyFactory", inst
);
286 reg
= NV
.getRegisteredObject("MyFactory");
287 res
&= (reg
!= null);
288 } catch (Exception e
) {
289 e
.printStackTrace(log
);
290 log
.println("registerObject failed");
295 NV
.revokeObject("MyFactory");
296 reg
= NV
.getRegisteredObject("MyFactory");
297 log
.println("revokeObject failed");
299 } catch (Exception e
) {
307 * In case no XNameContainer is available, but a XNamingReplace
310 protected boolean performChanges4() {
312 Object newValue
= inst2
;
313 Object originalValue
= null;
319 catch(java
.lang
.ClassCastException e
) {
320 log
.write("Expected String as object relations 'XContainer.ElementName'.");
321 e
.printStackTrace(log
);
326 originalValue
= NR
.getByName(name
);
327 NR
.replaceByName(name
, newValue
);
328 } catch (Exception e
) {
329 e
.printStackTrace(log
);
334 NR
.replaceByName(name
, originalValue
);
335 } catch (Exception e
) {
336 e
.printStackTrace(log
);
344 * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
347 private void shortWait() {
350 } catch (InterruptedException e
) {
351 log
.println("While waiting :" + e
) ;