bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / container / _XContainer.java
blob95c79dc5046418d580049283854eb0c867f27497
1 /*
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;
22 import lib.Status;
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;
37 /**
38 * Testing <code>com.sun.star.container.XContainer</code>
39 * interface methods :
40 * <ul>
41 * <li><code> addContainerListener()</code></li>
42 * <li><code> removeContainerListener()</code></li>
43 * </ul>
44 * This test needs the following object relations :
45 * <ul>
46 * <li> <code>'INSTANCE'</code> : Object which can be inserted into
47 * container.</li>
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>
57 * <ul> <p>
58 * Object <b>must implement</b>
59 * <code>com.sun.star.container.XNameContainer</code>.
60 * <p>
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 ;
77 /**
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");
86 if (altObj != null) {
87 oObj = UnoRuntime.queryInterface(XContainer.class, altObj);
90 NC = UnoRuntime.queryInterface
91 (XNameContainer.class, oObj) ;
93 Object container = null;
94 if (NC == 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.")) ;
117 if (inst == null)
118 inst = tEnv.getObjRelation("INSTANCE");
119 if (inst == null) {
120 log.println("No INSTANCE ObjRelation!!! ");
121 throw new StatusException(Status.failed("No INSTANCE ObjRelation!!!")) ;
123 if (inst2 == null)
124 inst2 = tEnv.getObjRelation("INSTANCE2");
128 * Listener implementation which just set flags on appropriate
129 * events.
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;
166 if (!bResult) {
167 log.println("inserted was " + (bElementInserted ? "" : "NOT")
168 + " called.");
170 if (NC != null) {
171 log.println("replaced was " + (bElementReplaced ? "" : "NOT")
172 + " called.");
174 log.println("removed was " + (bElementRemoved ? "" : "NOT")
175 + " called.");
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 :
187 * <ul>
188 * <li> <code> addContainerListener() </code> : to remove it now. </li>
189 * </ul>
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;
217 try {
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);
223 if (inst2 == null) {
224 NC.replaceByName("XContainer_dummy", inst);
225 } else {
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);
232 bResult = false;
233 } catch (com.sun.star.lang.WrappedTargetException ex) {
234 log.println("Exception occurred ");
235 ex.printStackTrace(log);
236 bResult = false;
237 } catch (com.sun.star.container.NoSuchElementException ex) {
238 log.println("Exception occurred ");
239 ex.printStackTrace(log);
240 bResult = false;
241 } catch (com.sun.star.container.ElementExistException ex) {
242 log.println("Exception occurred ");
243 ex.printStackTrace(log);
244 bResult = false;
247 return bResult;
251 * In case no XNameContainer is available, but a XControlContainer
252 * instead.
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);
259 shortWait();
260 int count = CC.getControls().length;
261 CC.removeControl(CC.getControl("NewControl"));
262 shortWait();
263 return count>precount;
267 * In case no XNameContainer is available, but a XNamingService
268 * instead.
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() {
274 boolean res = true;
275 Object reg = null;
277 try {
278 reg = NV.getRegisteredObject("MyFactory");
279 NV.revokeObject("MyFactory");
280 } catch (Exception e) {
284 try {
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");
291 res &= false;
294 try {
295 NV.revokeObject("MyFactory");
296 reg = NV.getRegisteredObject("MyFactory");
297 log.println("revokeObject failed");
298 res &= false;
299 } catch (Exception e) {
300 res &= true;
303 return res;
307 * In case no XNameContainer is available, but a XNamingReplace
308 * instead.
310 protected boolean performChanges4() {
311 boolean res = true;
312 Object newValue = inst2;
313 Object originalValue = null;
314 String name = null;
316 try {
317 name = (String)inst;
319 catch(java.lang.ClassCastException e) {
320 log.write("Expected String as object relations 'XContainer.ElementName'.");
321 e.printStackTrace(log);
322 return false;
325 try {
326 originalValue = NR.getByName(name);
327 NR.replaceByName(name, newValue);
328 } catch (Exception e) {
329 e.printStackTrace(log);
330 res = false;
333 try {
334 NR.replaceByName(name, originalValue);
335 } catch (Exception e) {
336 e.printStackTrace(log);
337 res = false;
340 return res;
344 * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
345 * reset</code> call.
347 private void shortWait() {
348 try {
349 Thread.sleep(1000) ;
350 } catch (InterruptedException e) {
351 log.println("While waiting :" + e) ;