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