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