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.StatusException;
28 
29 import com.sun.star.form.XUpdateBroadcaster;
30 import com.sun.star.form.XUpdateListener;
31 import com.sun.star.lang.EventObject;
32 
33 /**
34 * Testing <code>com.sun.star.form.XUpdateBroadcaster</code>
35 * interface methods :
36 * <ul>
37 *  <li><code> addUpdateListener()</code></li>
38 *  <li><code> removeUpdateListener()</code></li>
39 * </ul>
40 * This test needs the following object relations :
41 * <ul>
42 *  <li> <code>'XUpdateBroadcaster.Checker'</code> : <code>
43 *    _XUpdateBroadcaster.UpdateChecker</code> interface implementation
44 *    which can update, commit data and check if the data was successfully
45 *    committed.</li>
46 * <ul> <p>
47 * Test is <b> NOT </b> multithread compilant. <p>
48 * @see com.sun.star.form.XUpdateBroadcaster
49 */
50 public class _XUpdateBroadcaster extends MultiMethodTest {
51 
52     public XUpdateBroadcaster oObj = null;
53     UpdateChecker checker = null ;
54 
55     /**
56     * Interface for relation. Updating, committing and checking
57     * if data was committed is object dependent behaviour.
58     */
59     public static interface UpdateChecker {
60         /**
61         * Method must make some data update in the object tested.
62         */
update()63         public void update() throws com.sun.star.uno.Exception ;
64         /**
65         * Method must commit data change made by method <code>update</code>.
66         */
commit()67         public void commit() throws com.sun.star.uno.Exception ;
68         /**
69         * Checks if the data committed by <code>commit</code> method
70         * became permanent in data source.
71         * @return <code>true</code> if data was committed.
72         */
wasCommited()73         public boolean wasCommited() throws com.sun.star.uno.Exception ;
74     }
75 
76     /**
77     * Retrieves object relations.
78     * @throws StatusException If one of relations not found.
79     */
before()80     public void before() {
81         checker = (UpdateChecker)
82             tEnv.getObjRelation("XUpdateBroadcaster.Checker") ;
83         if (checker == null) {
84             log.println("Relation not found") ;
85             throw new StatusException("Relation not found",
86                 new NullPointerException("Relation not found")) ;
87         }
88     }
89 
90     /**
91     * Listener implementation, which can accept or reject update
92     * requests and store event calls.
93     */
94     protected class TestListener implements XUpdateListener {
95         /**
96         * Indicates must listener approve update requests or not.
97         */
98         public boolean approve = false ;
99         /**
100         * Indicates that <code>approveUpdate</code> method was called.
101         */
102         public boolean approveCalled = false ;
103         /**
104         * Indicates that <code>updated</code> method was called.
105         */
106         public boolean updateCalled = false ;
107 
108         /**
109         * Clears all flags.
110         */
init()111         public void init() {
112             approveCalled = false ;
113             updateCalled = false ;
114         }
disposing(EventObject ev)115         public void disposing(EventObject ev) {}
approveUpdate(EventObject ev)116         public boolean approveUpdate(EventObject ev) {
117             approveCalled = true ;
118             return approve ;
119         }
updated(EventObject ev)120         public void updated(EventObject ev) {
121             updateCalled = true ;
122         }
123     }
124 
125     private TestListener listener = new TestListener();
126 
127     /**
128     * The listener methods calls are checked twice with approving
129     * and rejecting updates. <p>
130     * Has <b>OK</b> status if on update rejected only <code>
131     * approveUpdate</code> listener method was called, and if
132     * on update approved <code>approveUpdate</code> and
133     * <code>updated</code> methods called, and data was committed
134     * to the source.
135     */
_addUpdateListener()136     public void _addUpdateListener() {
137         boolean bResult = true;
138 
139         oObj.addUpdateListener(listener) ;
140 
141         try {
142             checker.update() ;
143             shortWait() ;
144             checker.commit() ;
145             shortWait() ;
146             boolean committed = checker.wasCommited() ;
147 
148             shortWait() ;
149 
150             bResult = listener.approveCalled &&
151                       ! listener.updateCalled &&
152                       ! committed ;
153 
154             log.println("Calling with no approving : approveUpdate() was " +
155                 (listener.approveCalled ? "":"NOT")+" called, updated() was "+
156                 (listener.updateCalled ? "":"NOT")+" called, the value was " +
157                 (committed ? "" : "NOT") + " committed.") ;
158 
159             shortWait() ;
160 
161             listener.init() ;
162             listener.approve = true ;
163             shortWait() ;
164             checker.update() ;
165             shortWait() ;
166             checker.commit() ;
167             shortWait() ;
168             committed = checker.wasCommited() ;
169 
170             shortWait() ;
171 
172             log.println("Calling with approving : approveUpdate() was " +
173                 (listener.approveCalled ? "":"NOT")+" called, updated() was "+
174                 (listener.updateCalled ? "":"NOT")+" called, the value was "+
175                 (committed ? "" : "NOT") + " committed.") ;
176 
177             bResult = listener.approveCalled &&
178                       listener.updateCalled &&
179                       committed ;
180         } catch (com.sun.star.uno.Exception e) {
181             bResult = false ;
182             e.printStackTrace(log) ;
183         }
184 
185         tRes.tested("addUpdateListener()", bResult);
186     }
187 
188     /**
189     * Removes listener, updates data, and checks if no listener
190     * methods were called. <p>
191     * Has <b> OK </b> status if after listener removing no of its methods
192     * were called. <p>
193     * The following method tests are to be completed successfully before :
194     * <ul>
195     *  <li> <code> addUpdateListener </code> : to have a listener added.</li>
196     * </ul>
197     */
_removeUpdateListener()198     public void _removeUpdateListener() {
199         requiredMethod("addUpdateListener()");
200         boolean bResult = true;
201 
202         listener.init() ;
203         listener.approve = true ;
204 
205         oObj.removeUpdateListener(listener);
206 
207         try {
208             checker.update() ;
209             shortWait() ;
210             checker.commit() ;
211 
212             shortWait() ;
213 
214             bResult = ! listener.approveCalled &&
215                       ! listener.updateCalled ;
216         }
217         catch (com.sun.star.uno.Exception e) {
218             log.println("Exception occurred during removeUpdateListener()");
219             e.printStackTrace(log);
220             bResult = false;
221         }
222 
223         tRes.tested("removeUpdateListener()", bResult);
224     }
225 
shortWait()226     private void shortWait() {
227         try {
228             Thread.sleep(200);
229         }
230         catch (InterruptedException ex) {
231         }
232 
233     }
234 
235     /**
236     * Forces environment recreation.
237     */
after()238     protected void after() {
239         disposeEnvironment();
240     }
241 
242 }
243 
244 
245