1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
30 import lib
.MultiMethodTest
;
31 import lib
.StatusException
;
33 import com
.sun
.star
.form
.XUpdateBroadcaster
;
34 import com
.sun
.star
.form
.XUpdateListener
;
35 import com
.sun
.star
.lang
.EventObject
;
38 * Testing <code>com.sun.star.form.XUpdateBroadcaster</code>
41 * <li><code> addUpdateListener()</code></li>
42 * <li><code> removeUpdateListener()</code></li>
44 * This test needs the following object relations :
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
51 * Test is <b> NOT </b> multithread compilant. <p>
52 * @see com.sun.star.form.XUpdateBroadcaster
54 public class _XUpdateBroadcaster
extends MultiMethodTest
{
56 public XUpdateBroadcaster oObj
= null;
57 UpdateChecker checker
= null ;
60 * Interface for relation. Updating, commiting and checking
61 * if data was commited is object dependent behaviour.
63 public static interface UpdateChecker
{
65 * Method must make some data update in the object tested.
67 public void update() throws com
.sun
.star
.uno
.Exception
;
69 * Method must commit data change made by method <code>update</code>.
71 public void commit() throws com
.sun
.star
.uno
.Exception
;
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.
77 public boolean wasCommited() throws com
.sun
.star
.uno
.Exception
;
81 * Retrieves object relations.
82 * @throws StatusException If one of relations not found.
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")) ;
95 * Listener implementation, which can accept or reject update
96 * requests and store event calls.
98 protected class TestListener
implements XUpdateListener
{
100 * Indicates must listener approve update requests or not.
102 public boolean approve
= false ;
104 * Indicates that <code>approveUpdate</code> method was called.
106 public boolean approveCalled
= false ;
108 * Indicates that <code>updated</code> method was called.
110 public boolean updateCalled
= false ;
116 approveCalled
= false ;
117 updateCalled
= false ;
119 public void disposing(EventObject ev
) {}
120 public boolean approveUpdate(EventObject ev
) {
121 approveCalled
= true ;
124 public void updated(EventObject ev
) {
125 updateCalled
= true ;
129 private TestListener listener
= new TestListener();
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
140 public void _addUpdateListener() {
141 boolean bResult
= true;
143 oObj
.addUpdateListener(listener
) ;
150 boolean commited
= checker
.wasCommited() ;
154 bResult
= listener
.approveCalled
&&
155 ! listener
.updateCalled
&&
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.") ;
166 listener
.approve
= true ;
172 commited
= checker
.wasCommited() ;
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.") ;
181 bResult
= listener
.approveCalled
&&
182 listener
.updateCalled
&&
184 } catch (com
.sun
.star
.uno
.Exception e
) {
186 e
.printStackTrace(log
) ;
189 tRes
.tested("addUpdateListener()", bResult
);
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
197 * The following method tests are to be completed successfully before :
199 * <li> <code> addUpdateListener </code> : to have a listener added.</li>
202 public void _removeUpdateListener() {
203 requiredMethod("addUpdateListener()");
204 boolean bResult
= true;
207 listener
.approve
= true ;
209 oObj
.removeUpdateListener(listener
);
218 bResult
= ! listener
.approveCalled
&&
219 ! listener
.updateCalled
;
221 catch (com
.sun
.star
.uno
.Exception e
) {
222 log
.println("Exception occurred during removeUpdateListener()");
223 e
.printStackTrace(log
);
227 tRes
.tested("removeUpdateListener()", bResult
);
230 private void shortWait() {
234 catch (InterruptedException ex
) {
240 * Forces environment recreation.
242 protected void after() {
243 disposeEnvironment();