merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / util / _XChangesNotifier.java
blobfaa28114757f5465c6673f916f69a0bb0fb07cdf
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: _XChangesNotifier.java,v $
10 * $Revision: 1.5 $
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.util;
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.container.XNameReplace;
35 import com.sun.star.util.XChangesBatch;
36 import com.sun.star.util.XChangesListener;
37 import com.sun.star.util.XChangesNotifier;
38 import java.io.PrintWriter;
39 import lib.StatusException;
40 import lib.MultiMethodTest;
42 /**
43 * Test the XChangesNotifier interface. To produce some changes,
44 * XChangesBatch is used.
45 * @see com.sun.star.util.XChangesNotifier
46 * @see com.sun.star.util.XChangesBatch
48 public class _XChangesNotifier extends MultiMethodTest {
50 public XChangesNotifier oObj = null;
51 private XChangesBatch xBatch = null;
52 private Object changeElement = null;
53 private Object originalElement = null;
54 private String elementName = null;
55 private XPropertySet xProp = null;
56 private XNameReplace xNameReplace = null;
57 private _XChangesNotifier.MyChangesListener xListener = null;
59 /**
60 * Own implementation of the XChangesListener interface
61 * @see com.sun.star.util.XChangesListener
63 private static class MyChangesListener implements XChangesListener {
64 /** Just lo a call of the listener **/
65 boolean bChangesOccured = false;
67 /** A change did occur
68 * @param changesEvent The event.
69 **/
70 public void changesOccurred(com.sun.star.util.ChangesEvent changesEvent) {
71 bChangesOccured = true;
74 /** Disposing of the listener
75 * @param eventObject The event.
76 **/
77 public void disposing(com.sun.star.lang.EventObject eventObject) {
78 bChangesOccured = true;
81 /**
82 * Reset the listener
84 public void reset() {
85 bChangesOccured = false;
88 /**
89 * Has the listener been called?
90 * @return True, if the listener has been called.
92 public boolean didChangesOccur() {
93 return bChangesOccured;
97 /**
98 * Before the test: get the 'XChangesNotifier.ChangesBatch' object relation
99 * and create the listener.
101 protected void before() {
102 xBatch = (XChangesBatch)tEnv.getObjRelation("XChangesNotifier.ChangesBatch");
103 changeElement = tEnv.getObjRelation("XChangesNotifier.ChangeElement");
104 originalElement = tEnv.getObjRelation("XChangesNotifier.OriginalElement");
105 elementName = (String)tEnv.getObjRelation("XChangesNotifier.PropertyName");
107 xProp = (XPropertySet)tEnv.getObjRelation("XChangesNotifier.PropertySet");
108 try {
109 if (originalElement == null && xProp != null)
110 originalElement = xProp.getPropertyValue(elementName);
112 catch(com.sun.star.uno.Exception e) {
113 throw new StatusException("Could not get property '" + elementName + "'.", e);
116 // or get an XNameReplace
117 xNameReplace = (XNameReplace)tEnv.getObjRelation("XChangesNotifier.NameReplace");
118 try {
119 if (originalElement == null && xNameReplace != null)
120 originalElement = xNameReplace.getByName(elementName);
122 catch(com.sun.star.uno.Exception e) {
123 throw new StatusException("Could not get element by name '" + elementName + "'.", e);
126 if (changeElement == null || originalElement == null || elementName == null || (xProp == null && xNameReplace == null) || xBatch == null) {
127 log.println(
128 changeElement == null?"Missing property 'XChangesNotifier.ChangeElement'\n":"" +
129 originalElement == null?"Missing property 'XChangesNotifier.OriginalElement'\n":"" +
130 elementName == null?"Missing property 'XChangesNotifier.PropertyName'\n":"" +
131 xProp == null?"Missing property 'XChangesNotifier.PropertySet'":"" +
132 xNameReplace == null?"Missing property 'XChangesNotifier.NameReplace'":"" +
133 xBatch == null?"Missing property 'XChangesNotifier.ChangesBatch'":""
135 throw new StatusException("Some needed object relations are missing.", new Exception());
138 xListener = new _XChangesNotifier.MyChangesListener();
141 /** test addChangesListener **/
142 public void _addChangesListener() {
143 oObj.addChangesListener(xListener);
144 tRes.tested("addChangesListener()", true);
147 /** test removeChangesListener **/
148 public void _removeChangesListener() {
149 requiredMethod("addChangesListener()");
150 boolean result = true;
151 result &= commitChanges();
152 result &= xListener.didChangesOccur();
153 if (!result)
154 log.println("Listener has not been called.");
155 oObj.removeChangesListener(xListener);
156 xListener.reset();
157 result &= redoChanges();
158 boolean result2 = xListener.didChangesOccur();
159 if (result2)
160 log.println("Removed listener has been called.");
162 tRes.tested("removeChangesListener()", result && !result2);
166 * Commit a change, using an implementation of the XChangesBatch interface.
167 * @return true, if changing worked.
169 private boolean commitChanges() {
170 if (!executeChange(changeElement)) return false;
171 if (!xBatch.hasPendingChanges()) return false;
172 try {
173 xBatch.commitChanges();
175 catch(com.sun.star.lang.WrappedTargetException e) {
176 e.printStackTrace((PrintWriter)log);
177 return false;
179 return true;
183 * Redo the change, using an implementation of the XChangesBatch interface.
184 * @return true, if changing worked.
186 private boolean redoChanges() {
187 if (!executeChange(originalElement)) return false;
188 if (!xBatch.hasPendingChanges()) return false;
189 try {
190 xBatch.commitChanges();
192 catch(com.sun.star.lang.WrappedTargetException e) {
193 e.printStackTrace((PrintWriter)log);
194 return false;
196 return true;
200 * Execute the change, use XPropertySet or XNameReplace
201 * @return False, if changing did throw an exception.
203 private boolean executeChange(Object element) throws StatusException {
204 if (xProp != null) {
205 try {
206 xProp.setPropertyValue(elementName, element);
208 catch(com.sun.star.uno.Exception e) {
209 e.printStackTrace((PrintWriter)log);
210 return false;
213 else if (xNameReplace != null) {
214 try {
215 xNameReplace.replaceByName(elementName, element);
217 catch(com.sun.star.uno.Exception e) {
218 e.printStackTrace((PrintWriter)log);
219 return false;
222 return true;