Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / form / _XUpdateBroadcaster.java
blob34ca21809240130abe71ec1550b304abd23cefe7
1 /*
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 .
19 package ifc.form;
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;
28 /**
29 * Testing <code>com.sun.star.form.XUpdateBroadcaster</code>
30 * interface methods :
31 * <ul>
32 * <li><code> addUpdateListener()</code></li>
33 * <li><code> removeUpdateListener()</code></li>
34 * </ul>
35 * This test needs the following object relations :
36 * <ul>
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
40 * committed.</li>
41 * <ul> <p>
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 ;
50 /**
51 * Interface for relation. Updating, committing and checking
52 * if data was committed is object dependent behaviour.
54 public interface UpdateChecker {
55 /**
56 * Method must make some data update in the object tested.
58 void update() throws com.sun.star.uno.Exception ;
59 /**
60 * Method must commit data change made by method <code>update</code>.
62 void commit() throws com.sun.star.uno.Exception ;
63 /**
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 ;
71 /**
72 * Retrieves object relations.
73 * @throws StatusException If one of relations not found.
75 @Override
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")) ;
86 /**
87 * Listener implementation, which can accept or reject update
88 * requests and store event calls.
90 protected class TestListener implements XUpdateListener {
91 /**
92 * Indicates must listener approve update requests or not.
94 public boolean approve = false ;
95 /**
96 * Indicates that <code>approveUpdate</code> method was called.
98 public boolean approveCalled = false ;
99 /**
100 * Indicates that <code>updated</code> method was called.
102 public boolean updateCalled = false ;
105 * Clears all flags.
107 public void init() {
108 approveCalled = false ;
109 updateCalled = false ;
111 public void disposing(EventObject ev) {}
112 public boolean approveUpdate(EventObject ev) {
113 approveCalled = true ;
114 return approve ;
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
130 * to the source.
132 public void _addUpdateListener() {
133 boolean bResult = true;
135 oObj.addUpdateListener(listener) ;
137 try {
138 checker.update() ;
139 util.utils.pause(200);
140 checker.commit() ;
141 util.utils.pause(200);
142 boolean committed = checker.wasCommited() ;
144 util.utils.pause(200);
146 bResult = listener.approveCalled &&
147 ! listener.updateCalled &&
148 ! committed ;
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);
157 listener.init() ;
158 listener.approve = true ;
159 util.utils.pause(200);
160 checker.update() ;
161 util.utils.pause(200);
162 checker.commit() ;
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 &&
175 committed ;
176 } catch (com.sun.star.uno.Exception e) {
177 bResult = false ;
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
188 * were called. <p>
189 * The following method tests are to be completed successfully before :
190 * <ul>
191 * <li> <code> addUpdateListener </code> : to have a listener added.</li>
192 * </ul>
194 public void _removeUpdateListener() {
195 requiredMethod("addUpdateListener()");
196 boolean bResult = true;
198 listener.init() ;
199 listener.approve = true ;
201 oObj.removeUpdateListener(listener);
203 try {
204 checker.update() ;
205 util.utils.pause(200);
206 checker.commit() ;
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);
216 bResult = false;
219 tRes.tested("removeUpdateListener()", bResult);
223 * Forces environment recreation.
225 @Override
226 protected void after() {
227 disposeEnvironment();