merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / form / _XChangeBroadcaster.java
blobb3a2c979a40f99bd20cdd413bd175577d31224ab
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XChangeBroadcaster.java,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.form;
33 import lib.MultiMethodTest;
34 import lib.Status;
35 import lib.StatusException;
37 import com.sun.star.awt.XTextComponent;
38 import com.sun.star.awt.XWindow;
39 import com.sun.star.form.XChangeBroadcaster;
40 import com.sun.star.form.XChangeListener;
41 import com.sun.star.lang.EventObject;
42 import com.sun.star.uno.UnoRuntime;
44 /**
45 * Testing <code>com.sun.star.form.XChangeBroadcaster</code>
46 * interface methods:
47 * <ul>
48 * <li><code> addChangeListener() </code></li>
49 * <li><code> removeChangeListener() </code></li>
50 * </ul><p>
51 * This test needs the following object relations :
52 * <ul>
53 * <li> <code>'Win1'</code> (of type <code>XWindow</code>):
54 * used to change context when testing interface methods</li>
55 * <li> <code>'Win2'</code> (of type <code>XWindow</code>):
56 * used to change context when testing interface methods </li>
57 * <li> <code>'CONTROL'</code> (of type <code>XControl</code> and
58 * must implement <code>XTextComponent</code> interface):
59 * used to change context when testing interface methods </li>
60 * <li> <code>'XChangeBroadcaster.Changer'</code>
61 * (of type <code>ifc.form._XChangeBroadcaster.Changer</code>)
62 * <b>optional</b> : this relation <b>must be specified</b> when
63 * <code>XTextComponent</code> is not supported by the tested
64 * component. It is used to change some component content
65 * which must cause listener call. </li>
66 * </ul> <p>
68 * <b>Prerequisites:</b> component must implement <code>XTextComponent</code>
69 * interface for changing component's text which must cause listener call.
70 * If the component cann't support the interface, then the relation
71 * <code>'XChangeBroadcaster.Changer'</code> must be passed. <p>
73 * Test is <b> NOT </b> multithread compilant. <p>
74 * @see com.sun.star.form.XChangeBroadcaster
76 public class _XChangeBroadcaster extends MultiMethodTest {
77 public static XChangeBroadcaster oObj = null;
78 protected boolean changed = false;
80 /**
81 * This interface must be implemented by component and passed
82 * in relation if it doesn't support <code>XTextComponent</code>
83 * interface. It used to change the content of component.
85 public static interface Changer {
86 /**
87 * The method must change the component's content to
88 * cause a listener call.
90 public void change() ;
93 /**
94 * Class we need to test methods
96 protected class MyChangeListener implements XChangeListener {
97 public void disposing ( EventObject oEvent ) {}
98 public void changed ( EventObject oEvent ) {
99 System.out.println("Listener called");
100 changed = true;
104 protected XChangeListener listener = new MyChangeListener();
105 protected XTextComponent xText = null ;
106 protected Changer changer = null ;
109 * Tries to query the tested component for <code>XTextComponent</code>
110 * interface and retrieves a relation
111 * <code>'XChangeBroadcaster.Changer'</code>.
112 * @throw StatusException If neither relation is found nor interface
113 * is queried.
115 public void before() {
116 xText = (XTextComponent)
117 UnoRuntime.queryInterface(XTextComponent.class,oObj);
118 changer = (Changer) tEnv.getObjRelation("XChangeBroadcaster.Changer") ;
120 if (xText == null && changer == null)
121 throw new StatusException(Status.failed
122 ("Neither 'XChangeBroadcaster.Changer' relation found " +
123 "nor XTextComponent is supported")) ;
127 * Test calls the method, then object relations 'Win1', 'Win2', 'CONTROL'
128 * are obtained, and context is changed.<p>
129 * Has <b> OK </b> status if listener was called after context has changed.
131 public void _addChangeListener() {
132 log.println("Testing addChangeListener ...");
133 oObj.addChangeListener( listener );
134 XWindow win1 = (XWindow) tEnv.getObjRelation("Win1");
135 XWindow win2 = (XWindow) tEnv.getObjRelation("Win2");
136 win1.setFocus();
138 changeContent() ;
139 shortWait();
141 win2.setFocus();
142 XTextComponent TC = (XTextComponent)UnoRuntime.queryInterface
143 (XTextComponent.class,tEnv.getObjRelation("CONTROL"));
144 TC.setText("NOXChangeBroadcaster");
145 shortWait();
146 tRes.tested("addChangeListener()", changed);
150 * Test calls the method, then object relations 'Win1', 'Win2', 'CONTROL'
151 * are obtained, and context is changed.<p>
152 * Has <b> OK </b> status if listener was not called after context has
153 * changed.<p>
154 * The following method tests are to be completed successfully before :
155 * <ul>
156 * <li> <code> addChangeListener() </code> : adds the specified listener
157 * to receive the "changed" event</li>
158 * </ul>
160 public void _removeChangeListener() {
161 requiredMethod("addChangeListener()");
162 changed = false;
163 log.println("Testing removeChangeListener ...");
164 oObj.addChangeListener( listener );
165 XWindow win2 = (XWindow) tEnv.getObjRelation("Win2");
166 win2.setFocus();
168 changeContent() ;
170 win2.setFocus();
171 shortWait();
172 tRes.tested("removeChangeListener()", !changed);
176 * Sleeps for 0.2 sec. to allow StarOffice to react on <code>
177 * reset</code> call.
179 private void shortWait() {
180 try {
181 Thread.sleep(2000) ;
182 } catch (InterruptedException e) {
183 log.println("While waiting :" + e) ;
188 * Changes the content of the component depending on whether
189 * <code>XTextComponent</code> is supported or not. If yes
190 * then the text is chahged, if not the relation <code>change()</code>
191 * method is used.
193 protected void changeContent() {
194 if (xText != null) {
195 xText.setText("XChangeBroadcaster".equals(xText.getText()) ?
196 "NoXChangeBroadcaster" : "XChangeBroadcaster") ;
197 } else {
198 changer.change();
202 } // finished class _XChangeBroadcaster