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