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