2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import lib
.MultiMethodTest
;
22 import lib
.StatusException
;
24 import com
.sun
.star
.form
.XUpdateBroadcaster
;
25 import com
.sun
.star
.form
.XUpdateListener
;
26 import com
.sun
.star
.lang
.EventObject
;
29 * Testing <code>com.sun.star.form.XUpdateBroadcaster</code>
32 * <li><code> addUpdateListener()</code></li>
33 * <li><code> removeUpdateListener()</code></li>
35 * This test needs the following object relations :
37 * <li> <code>'XUpdateBroadcaster.Checker'</code> : <code>
38 * _XUpdateBroadcaster.UpdateChecker</code> interface implementation
39 * which can update, commit data and check if the data was successfully
42 * Test is <b> NOT </b> multithread compliant. <p>
43 * @see com.sun.star.form.XUpdateBroadcaster
45 public class _XUpdateBroadcaster
extends MultiMethodTest
{
47 public XUpdateBroadcaster oObj
= null;
48 UpdateChecker checker
= null ;
51 * Interface for relation. Updating, committing and checking
52 * if data was committed is object dependent behaviour.
54 public interface UpdateChecker
{
56 * Method must make some data update in the object tested.
58 void update() throws com
.sun
.star
.uno
.Exception
;
60 * Method must commit data change made by method <code>update</code>.
62 void commit() throws com
.sun
.star
.uno
.Exception
;
64 * Checks if the data committed by <code>commit</code> method
65 * became permanent in data source.
66 * @return <code>true</code> if data was committed.
68 boolean wasCommited() throws com
.sun
.star
.uno
.Exception
;
72 * Retrieves object relations.
73 * @throws StatusException If one of relations not found.
76 public void before() {
77 checker
= (UpdateChecker
)
78 tEnv
.getObjRelation("XUpdateBroadcaster.Checker") ;
79 if (checker
== null) {
80 log
.println("Relation not found") ;
81 throw new StatusException("Relation not found",
82 new NullPointerException("Relation not found")) ;
87 * Listener implementation, which can accept or reject update
88 * requests and store event calls.
90 protected class TestListener
implements XUpdateListener
{
92 * Indicates must listener approve update requests or not.
94 public boolean approve
= false ;
96 * Indicates that <code>approveUpdate</code> method was called.
98 public boolean approveCalled
= false ;
100 * Indicates that <code>updated</code> method was called.
102 public boolean updateCalled
= false ;
108 approveCalled
= false ;
109 updateCalled
= false ;
111 public void disposing(EventObject ev
) {}
112 public boolean approveUpdate(EventObject ev
) {
113 approveCalled
= true ;
116 public void updated(EventObject ev
) {
117 updateCalled
= true ;
121 private final TestListener listener
= new TestListener();
124 * The listener methods calls are checked twice with approving
125 * and rejecting updates. <p>
126 * Has <b>OK</b> status if on update rejected only <code>
127 * approveUpdate</code> listener method was called, and if
128 * on update approved <code>approveUpdate</code> and
129 * <code>updated</code> methods called, and data was committed
132 public void _addUpdateListener() {
133 boolean bResult
= true;
135 oObj
.addUpdateListener(listener
) ;
139 util
.utils
.pause(200);
141 util
.utils
.pause(200);
142 boolean committed
= checker
.wasCommited() ;
144 util
.utils
.pause(200);
146 bResult
= listener
.approveCalled
&&
147 ! listener
.updateCalled
&&
150 log
.println("Calling with no approving : approveUpdate() was " +
151 (listener
.approveCalled ?
"":"NOT")+" called, updated() was "+
152 (listener
.updateCalled ?
"":"NOT")+" called, the value was " +
153 (committed ?
"" : "NOT") + " committed.") ;
155 util
.utils
.pause(200);
158 listener
.approve
= true ;
159 util
.utils
.pause(200);
161 util
.utils
.pause(200);
163 util
.utils
.pause(200);
164 committed
= checker
.wasCommited() ;
166 util
.utils
.pause(200);
168 log
.println("Calling with approving : approveUpdate() was " +
169 (listener
.approveCalled ?
"":"NOT")+" called, updated() was "+
170 (listener
.updateCalled ?
"":"NOT")+" called, the value was "+
171 (committed ?
"" : "NOT") + " committed.") ;
173 bResult
= listener
.approveCalled
&&
174 listener
.updateCalled
&&
176 } catch (com
.sun
.star
.uno
.Exception e
) {
178 e
.printStackTrace(log
) ;
181 tRes
.tested("addUpdateListener()", bResult
);
185 * Removes listener, updates data, and checks if no listener
186 * methods were called. <p>
187 * Has <b> OK </b> status if after listener removing no of its methods
189 * The following method tests are to be completed successfully before :
191 * <li> <code> addUpdateListener </code> : to have a listener added.</li>
194 public void _removeUpdateListener() {
195 requiredMethod("addUpdateListener()");
196 boolean bResult
= true;
199 listener
.approve
= true ;
201 oObj
.removeUpdateListener(listener
);
205 util
.utils
.pause(200);
208 util
.utils
.pause(200);
210 bResult
= ! listener
.approveCalled
&&
211 ! listener
.updateCalled
;
213 catch (com
.sun
.star
.uno
.Exception e
) {
214 log
.println("Exception occurred during removeUpdateListener()");
215 e
.printStackTrace(log
);
219 tRes
.tested("removeUpdateListener()", bResult
);
223 * Forces environment recreation.
226 protected void after() {
227 disposeEnvironment();