merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / form / _XUpdateBroadcaster.java
blob5054c63d00f494388c367a9c916e27d714828d40
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: _XUpdateBroadcaster.java,v $
10 * $Revision: 1.4 $
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.StatusException;
36 import com.sun.star.form.XUpdateBroadcaster;
37 import com.sun.star.form.XUpdateListener;
38 import com.sun.star.lang.EventObject;
40 /**
41 * Testing <code>com.sun.star.form.XUpdateBroadcaster</code>
42 * interface methods :
43 * <ul>
44 * <li><code> addUpdateListener()</code></li>
45 * <li><code> removeUpdateListener()</code></li>
46 * </ul>
47 * This test needs the following object relations :
48 * <ul>
49 * <li> <code>'XUpdateBroadcaster.Checker'</code> : <code>
50 * _XUpdateBroadcaster.UpdateChecker</code> interface implementation
51 * which can update, commit data and check if the data was successfully
52 * commited.</li>
53 * <ul> <p>
54 * Test is <b> NOT </b> multithread compilant. <p>
55 * @see com.sun.star.form.XUpdateBroadcaster
57 public class _XUpdateBroadcaster extends MultiMethodTest {
59 public XUpdateBroadcaster oObj = null;
60 UpdateChecker checker = null ;
62 /**
63 * Interface for relation. Updating, commiting and checking
64 * if data was commited is object dependent behaviour.
66 public static interface UpdateChecker {
67 /**
68 * Method must make some data update in the object tested.
70 public void update() throws com.sun.star.uno.Exception ;
71 /**
72 * Method must commit data change made by method <code>update</code>.
74 public void commit() throws com.sun.star.uno.Exception ;
75 /**
76 * Checks if the data commited by <code>commit</code> method
77 * became permanent in data source.
78 * @return <code>true</code> if data was commited.
80 public boolean wasCommited() throws com.sun.star.uno.Exception ;
83 /**
84 * Retrieves object relations.
85 * @throws StatusException If one of relations not found.
87 public void before() {
88 checker = (UpdateChecker)
89 tEnv.getObjRelation("XUpdateBroadcaster.Checker") ;
90 if (checker == null) {
91 log.println("Relation not found") ;
92 throw new StatusException("Relation not found",
93 new NullPointerException("Relation not found")) ;
97 /**
98 * Listener implementation, which can accept or reject update
99 * requests and store event calls.
101 protected class TestListener implements XUpdateListener {
103 * Indicates must listener approve update requests or not.
105 public boolean approve = false ;
107 * Indicates that <code>approveUpdate</code> method was called.
109 public boolean approveCalled = false ;
111 * Indicates that <code>updated</code> method was called.
113 public boolean updateCalled = false ;
116 * Clears all flags.
118 public void init() {
119 approveCalled = false ;
120 updateCalled = false ;
122 public void disposing(EventObject ev) {}
123 public boolean approveUpdate(EventObject ev) {
124 approveCalled = true ;
125 return approve ;
127 public void updated(EventObject ev) {
128 updateCalled = true ;
132 private TestListener listener = new TestListener();
135 * The listener methods calls are checked twice with approving
136 * and rejecting updates. <p>
137 * Has <b>OK</b> status if on update rejected only <code>
138 * approveUpdate</code> listener method was called, and if
139 * on update approved <code>approveUpdate</code> and
140 * <code>updated</code> methods called, and data was commited
141 * to the source.
143 public void _addUpdateListener() {
144 boolean bResult = true;
146 oObj.addUpdateListener(listener) ;
148 try {
149 checker.update() ;
150 shortWait() ;
151 checker.commit() ;
152 shortWait() ;
153 boolean commited = checker.wasCommited() ;
155 shortWait() ;
157 bResult = listener.approveCalled &&
158 ! listener.updateCalled &&
159 ! commited ;
161 log.println("Calling with no approving : approveUpdate() was " +
162 (listener.approveCalled ? "":"NOT")+" called, updated() was "+
163 (listener.updateCalled ? "":"NOT")+" called, the value was " +
164 (commited ? "" : "NOT") + " commited.") ;
166 shortWait() ;
168 listener.init() ;
169 listener.approve = true ;
170 shortWait() ;
171 checker.update() ;
172 shortWait() ;
173 checker.commit() ;
174 shortWait() ;
175 commited = checker.wasCommited() ;
177 shortWait() ;
179 log.println("Calling with approving : approveUpdate() was " +
180 (listener.approveCalled ? "":"NOT")+" called, updated() was "+
181 (listener.updateCalled ? "":"NOT")+" called, the value was "+
182 (commited ? "" : "NOT") + " commited.") ;
184 bResult = listener.approveCalled &&
185 listener.updateCalled &&
186 commited ;
187 } catch (com.sun.star.uno.Exception e) {
188 bResult = false ;
189 e.printStackTrace(log) ;
192 tRes.tested("addUpdateListener()", bResult);
196 * Removes listener, updates data, and checks if no listener
197 * methods were called. <p>
198 * Has <b> OK </b> status if after listener removing no of its methods
199 * were called. <p>
200 * The following method tests are to be completed successfully before :
201 * <ul>
202 * <li> <code> addUpdateListener </code> : to have a listener added.</li>
203 * </ul>
205 public void _removeUpdateListener() {
206 requiredMethod("addUpdateListener()");
207 boolean bResult = true;
209 listener.init() ;
210 listener.approve = true ;
212 oObj.removeUpdateListener(listener);
214 try {
215 checker.update() ;
216 shortWait() ;
217 checker.commit() ;
219 shortWait() ;
221 bResult = ! listener.approveCalled &&
222 ! listener.updateCalled ;
224 catch (com.sun.star.uno.Exception e) {
225 log.println("Exception occured during removeUpdateListener()");
226 e.printStackTrace(log);
227 bResult = false;
230 tRes.tested("removeUpdateListener()", bResult);
233 private void shortWait() {
234 try {
235 Thread.sleep(200);
237 catch (InterruptedException ex) {
243 * Forces environment recreation.
245 protected void after() {
246 disposeEnvironment();