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 com
.sun
.star
.beans
.XPropertySet
;
22 import com
.sun
.star
.container
.XNameReplace
;
23 import com
.sun
.star
.util
.XChangesBatch
;
24 import com
.sun
.star
.util
.XChangesListener
;
25 import com
.sun
.star
.util
.XChangesNotifier
;
26 import lib
.StatusException
;
27 import lib
.MultiMethodTest
;
30 * Test the XChangesNotifier interface. To produce some changes,
31 * XChangesBatch is used.
32 * @see com.sun.star.util.XChangesNotifier
33 * @see com.sun.star.util.XChangesBatch
35 public class _XChangesNotifier
extends MultiMethodTest
{
37 public XChangesNotifier oObj
= null;
38 private XChangesBatch xBatch
= null;
39 private Object changeElement
= null;
40 private Object originalElement
= null;
41 private String elementName
= null;
42 private XPropertySet xProp
= null;
43 private XNameReplace xNameReplace
= null;
44 private _XChangesNotifier
.MyChangesListener xListener
= null;
47 * Own implementation of the XChangesListener interface
48 * @see com.sun.star.util.XChangesListener
50 private static class MyChangesListener
implements XChangesListener
{
51 /** Just lo a call of the listener **/
52 boolean bChangesOccurred
= false;
54 /** A change did occur
55 * @param changesEvent The event.
57 public void changesOccurred(com
.sun
.star
.util
.ChangesEvent changesEvent
) {
58 bChangesOccurred
= true;
61 /** Disposing of the listener
62 * @param eventObject The event.
64 public void disposing(com
.sun
.star
.lang
.EventObject eventObject
) {
65 bChangesOccurred
= true;
72 bChangesOccurred
= false;
76 * Has the listener been called?
77 * @return True, if the listener has been called.
79 public boolean didChangesOccur() {
80 return bChangesOccurred
;
85 * Before the test: get the 'XChangesNotifier.ChangesBatch' object relation
86 * and create the listener.
88 protected void before() {
89 xBatch
= (XChangesBatch
)tEnv
.getObjRelation("XChangesNotifier.ChangesBatch");
90 changeElement
= tEnv
.getObjRelation("XChangesNotifier.ChangeElement");
91 originalElement
= tEnv
.getObjRelation("XChangesNotifier.OriginalElement");
92 elementName
= (String
)tEnv
.getObjRelation("XChangesNotifier.PropertyName");
94 xProp
= (XPropertySet
)tEnv
.getObjRelation("XChangesNotifier.PropertySet");
96 if (originalElement
== null && xProp
!= null)
97 originalElement
= xProp
.getPropertyValue(elementName
);
99 catch(com
.sun
.star
.uno
.Exception e
) {
100 throw new StatusException("Could not get property '" + elementName
+ "'.", e
);
103 // or get an XNameReplace
104 xNameReplace
= (XNameReplace
)tEnv
.getObjRelation("XChangesNotifier.NameReplace");
106 if (originalElement
== null && xNameReplace
!= null)
107 originalElement
= xNameReplace
.getByName(elementName
);
109 catch(com
.sun
.star
.uno
.Exception e
) {
110 throw new StatusException("Could not get element by name '" + elementName
+ "'.", e
);
113 if (changeElement
== null || originalElement
== null || elementName
== null || (xProp
== null && xNameReplace
== null) || xBatch
== null) {
115 changeElement
== null?
"Missing property 'XChangesNotifier.ChangeElement'\n":"" +
116 originalElement
== null?
"Missing property 'XChangesNotifier.OriginalElement'\n":"" +
117 elementName
== null?
"Missing property 'XChangesNotifier.PropertyName'\n":"" +
118 xProp
== null?
"Missing property 'XChangesNotifier.PropertySet'":"" +
119 xNameReplace
== null?
"Missing property 'XChangesNotifier.NameReplace'":"" +
120 xBatch
== null?
"Missing property 'XChangesNotifier.ChangesBatch'":""
122 throw new StatusException("Some needed object relations are missing.", new Exception());
125 xListener
= new _XChangesNotifier
.MyChangesListener();
128 /** test addChangesListener **/
129 public void _addChangesListener() {
130 oObj
.addChangesListener(xListener
);
131 tRes
.tested("addChangesListener()", true);
134 /** test removeChangesListener **/
135 public void _removeChangesListener() {
136 requiredMethod("addChangesListener()");
137 boolean result
= true;
138 result
&= commitChanges();
139 result
&= xListener
.didChangesOccur();
141 log
.println("Listener has not been called.");
142 oObj
.removeChangesListener(xListener
);
144 result
&= redoChanges();
145 boolean result2
= xListener
.didChangesOccur();
147 log
.println("Removed listener has been called.");
149 tRes
.tested("removeChangesListener()", result
&& !result2
);
153 * Commit a change, using an implementation of the XChangesBatch interface.
154 * @return true, if changing worked.
156 private boolean commitChanges() {
157 if (!executeChange(changeElement
)) return false;
158 if (!xBatch
.hasPendingChanges()) return false;
160 xBatch
.commitChanges();
162 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
163 e
.printStackTrace(log
);
170 * Redo the change, using an implementation of the XChangesBatch interface.
171 * @return true, if changing worked.
173 private boolean redoChanges() {
174 if (!executeChange(originalElement
)) return false;
175 if (!xBatch
.hasPendingChanges()) return false;
177 xBatch
.commitChanges();
179 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
180 e
.printStackTrace(log
);
187 * Execute the change, use XPropertySet or XNameReplace
188 * @return False, if changing did throw an exception.
190 private boolean executeChange(Object element
) throws StatusException
{
193 xProp
.setPropertyValue(elementName
, element
);
195 catch(com
.sun
.star
.uno
.Exception e
) {
196 e
.printStackTrace(log
);
200 else if (xNameReplace
!= null) {
202 xNameReplace
.replaceByName(elementName
, element
);
204 catch(com
.sun
.star
.uno
.Exception e
) {
205 e
.printStackTrace(log
);