1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CheckGlobalEventBroadcaster_writer1.java,v $
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 ************************************************************************/
30 package complex
.framework
;
32 import com
.sun
.star
.awt
.XWindow
;
33 import com
.sun
.star
.document
.XEventBroadcaster
;
34 import com
.sun
.star
.document
.XEventListener
;
35 import com
.sun
.star
.lang
.XMultiServiceFactory
;
36 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
37 import com
.sun
.star
.text
.XTextDocument
;
38 import com
.sun
.star
.uno
.UnoRuntime
;
39 import complex
.framework
.DocHelper
.WriterHelper
;
40 import complexlib
.ComplexTestCase
;
41 import java
.util
.ArrayList
;
42 import com
.sun
.star
.task
.XJobExecutor
;
43 import com
.sun
.star
.util
.URL
;
47 * This testcase checks the GlobalEventBroadcaster
48 * it will add an XEventListener and verify the Events
49 * raised when opening/changing and closing Office Documents
51 public class CheckGlobalEventBroadcaster_writer1
extends ComplexTestCase
{
52 XMultiServiceFactory m_xMSF
= null;
53 XEventBroadcaster m_xEventBroadcaster
= null;
54 ArrayList notifyEvents
= new ArrayList();
55 XTextDocument xTextDoc
;
56 XSpreadsheetDocument xSheetDoc
;
57 XEventListener m_xEventListener
= new EventListenerImpl();
59 public String
[] getTestMethodNames() {
61 "initialize", "checkWriter", "cleanup"
65 public void initialize() {
66 m_xMSF
= (XMultiServiceFactory
) param
.getMSF();
67 log
.println("check wether there is a valid MultiServiceFactory");
70 assure("## Couldn't get MultiServiceFactory make sure your Office is started",
74 log
.println("... done");
77 "Create an instance of com.sun.star.frame.GlobalEventBroadcaster");
79 Object GlobalEventBroadcaster
= null;
80 Object dispatcher
= null;
83 GlobalEventBroadcaster
= m_xMSF
.createInstance(
84 "com.sun.star.frame.GlobalEventBroadcaster");
85 } catch (com
.sun
.star
.uno
.Exception e
) {
86 assure("## Exception while creating instance", false);
89 log
.println("... done");
91 log
.println("check wether the created instance is valid");
93 if (GlobalEventBroadcaster
== null) {
94 assure("couldn't create service", false);
97 log
.println("... done");
100 "try to query the XEventBroadcaster from the gained Object");
101 m_xEventBroadcaster
= (XEventBroadcaster
) UnoRuntime
.queryInterface(
102 XEventBroadcaster
.class,
103 GlobalEventBroadcaster
);
105 if (util
.utils
.isVoid(m_xEventBroadcaster
)) {
106 assure("couldn't get XEventBroadcaster", false);
109 log
.println("... done");
111 log
.println("adding Listener");
112 m_xEventBroadcaster
.addEventListener(m_xEventListener
);
113 log
.println("... done");
116 public void checkWriter() {
117 log
.println("-- Checking Writer --");
119 WriterHelper wHelper
= new WriterHelper(m_xMSF
);
121 boolean locRes
= true;
122 log
.println("opening an empty writer doc");
123 notifyEvents
.clear();
124 xTextDoc
= wHelper
.openEmptyDoc();
126 expected
= new String
[] { "OnUnfocus", "OnCreate", "OnViewCreated", "OnFocus" };
128 assure("Wrong events fired when opening empty doc",
129 proveExpectation(expected
));
130 log
.println("... done");
132 log
.println("changing the writer doc");
133 notifyEvents
.clear();
134 xTextDoc
.getText().setString("GlobalEventBroadcaster");
136 expected
= new String
[] { "OnModifyChanged" };
138 assure("Wrong events fired when changing doc",
139 proveExpectation(expected
));
140 log
.println("... done");
142 log
.println("closing the empty writer doc");
143 notifyEvents
.clear();
144 wHelper
.closeDoc(xTextDoc
);
146 expected
= new String
[] { "OnUnfocus", "OnFocus", "OnViewClosed", "OnUnload" };
148 assure("Wrong events fired when closing empty doc",
149 proveExpectation(expected
));
150 log
.println("... done");
152 log
.println("opening an writer doc via Window-New Window");
153 notifyEvents
.clear();
154 xTextDoc
= wHelper
.openFromDialog(".uno:NewWindow", "", false);
156 expected
= new String
[] { "OnUnfocus", "OnCreate", "OnViewCreated", "OnFocus", "OnUnfocus", "OnViewCreated", "OnFocus", };
158 assure("Wrong events fired when opening an writer doc via Window-New Window",
159 proveExpectation(expected
));
160 log
.println("... done");
162 log
.println("closing the created writer doc");
163 notifyEvents
.clear();
165 wHelper
.closeDoc(xTextDoc
);
167 expected
= new String
[] { "OnViewClosed", "OnUnfocus", "OnFocus", "OnViewClosed", "OnUnload" };
169 assure("Wrong events fired when closing Window-New Window",
170 proveExpectation(expected
));
172 log
.println("... done");
174 log
.println("Opening document with label wizard");
175 xTextDoc
= wHelper
.openFromDialog("private:factory/swriter?slot=21051", "", false);
177 XWindow xWindow
= (XWindow
) UnoRuntime
.queryInterface(XWindow
.class, wHelper
.getToolkit ().getActiveTopWindow ());
178 UITools ut
= new UITools(m_xMSF
,xWindow
);
179 notifyEvents
.clear();
180 log
.println("pressing button 'New Document'");
182 ut
.clickButton ("New Document");
183 } catch (Exception e
) {
184 log
.println("Couldn't press Button");
186 log
.println("... done");
190 expected
= new String
[] { "OnViewClosed", "OnCreate", "OnFocus", "OnModifyChanged" };
192 assure("Wrong events fired when starting labels wizard",
193 proveExpectation(expected
));
195 log
.println("-- Done Writer --");
198 public void cleanup() {
199 log
.println("removing Listener");
200 m_xEventBroadcaster
.removeEventListener(m_xEventListener
);
201 log
.println("... done");
205 * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
208 private void shortWait() {
211 } catch (InterruptedException e
) {
212 log
.println("While waiting :" + e
);
216 private boolean proveExpectation(String
[] expected
) {
217 boolean locRes
= true;
218 boolean failure
= false;
220 log
.println("Fired Events:");
221 for (int k
=0;k
<notifyEvents
.size();k
++) {
222 System
.out
.println("\t- "+notifyEvents
.get(k
));
225 for (int i
= 0; i
< expected
.length
; i
++) {
226 locRes
= notifyEvents
.contains(expected
[i
]);
229 log
.println("The event " + expected
[i
] + " isn't fired");
237 public class EventListenerImpl
implements XEventListener
{
238 public void disposing(com
.sun
.star
.lang
.EventObject eventObject
) {
239 log
.println("disposing: " + eventObject
.Source
.toString());
242 public void notifyEvent(com
.sun
.star
.document
.EventObject eventObject
) {
243 notifyEvents
.add(eventObject
.EventName
);