1*a4c7eb23SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a4c7eb23SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a4c7eb23SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a4c7eb23SAndrew Rist  * distributed with this work for additional information
6*a4c7eb23SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a4c7eb23SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a4c7eb23SAndrew Rist  * "License"); you may not use this file except in compliance
9*a4c7eb23SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a4c7eb23SAndrew Rist  *
11*a4c7eb23SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a4c7eb23SAndrew Rist  *
13*a4c7eb23SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a4c7eb23SAndrew Rist  * software distributed under the License is distributed on an
15*a4c7eb23SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a4c7eb23SAndrew Rist  * KIND, either express or implied.  See the License for the
17*a4c7eb23SAndrew Rist  * specific language governing permissions and limitations
18*a4c7eb23SAndrew Rist  * under the License.
19*a4c7eb23SAndrew Rist  *
20*a4c7eb23SAndrew Rist  *************************************************************/
21*a4c7eb23SAndrew Rist 
22*a4c7eb23SAndrew Rist 
23cdf0e10cSrcweir package complex.sfx2;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.awt.XWindow;
26cdf0e10cSrcweir import com.sun.star.document.XEventBroadcaster;
27cdf0e10cSrcweir import com.sun.star.document.XEventListener;
28cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
29cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
30cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
31cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
32cdf0e10cSrcweir import complex.sfx2.tools.WriterHelper;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import java.util.ArrayList;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir import util.UITools;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import org.junit.After;
39cdf0e10cSrcweir import org.junit.AfterClass;
40cdf0e10cSrcweir import org.junit.Before;
41cdf0e10cSrcweir import org.junit.BeforeClass;
42cdf0e10cSrcweir import org.junit.Test;
43cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
44cdf0e10cSrcweir import static org.junit.Assert.*;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
47cdf0e10cSrcweir /**
48cdf0e10cSrcweir  * This testcase checks the GlobalEventBroadcaster
49cdf0e10cSrcweir  * it will add an XEventListener and verify the Events
50cdf0e10cSrcweir  * raised when opening/changing and closing Office Documents
51cdf0e10cSrcweir  */
52cdf0e10cSrcweir public class GlobalEventBroadcaster {
53cdf0e10cSrcweir     XMultiServiceFactory m_xMSF = null;
54cdf0e10cSrcweir     XEventBroadcaster m_xEventBroadcaster = null;
55cdf0e10cSrcweir     ArrayList notifyEvents = new ArrayList();
56cdf0e10cSrcweir     // XTextDocument xTextDoc;
57cdf0e10cSrcweir     XSpreadsheetDocument xSheetDoc;
58cdf0e10cSrcweir     XEventListener m_xEventListener = new EventListenerImpl();
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     @Before public void initialize() {
61cdf0e10cSrcweir         m_xMSF = getMSF();
62cdf0e10cSrcweir         System.out.println("check wether there is a valid MultiServiceFactory");
63cdf0e10cSrcweir 
64cdf0e10cSrcweir         assertNotNull("## Couldn't get MultiServiceFactory make sure your Office is started", m_xMSF);
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         System.out.println("... done");
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         System.out.println(
69cdf0e10cSrcweir                 "Create an instance of com.sun.star.frame.GlobalEventBroadcaster");
70cdf0e10cSrcweir 
71cdf0e10cSrcweir         Object GlobalEventBroadcaster = null;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir         try {
74cdf0e10cSrcweir             GlobalEventBroadcaster = m_xMSF.createInstance(
75cdf0e10cSrcweir                                              "com.sun.star.frame.GlobalEventBroadcaster");
76cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
77cdf0e10cSrcweir             fail("## Exception while creating instance");
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir         System.out.println("... done");
81cdf0e10cSrcweir 
82cdf0e10cSrcweir         System.out.println("check wether the created instance is valid");
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         assertNotNull("couldn't create service", GlobalEventBroadcaster);
85cdf0e10cSrcweir 
86cdf0e10cSrcweir         System.out.println("... done");
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         System.out.println(
89cdf0e10cSrcweir                 "try to query the XEventBroadcaster from the gained Object");
90cdf0e10cSrcweir         m_xEventBroadcaster = UnoRuntime.queryInterface(XEventBroadcaster.class, GlobalEventBroadcaster);
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         if (util.utils.isVoid(m_xEventBroadcaster)) {
93cdf0e10cSrcweir             fail("couldn't get XEventBroadcaster");
94cdf0e10cSrcweir         }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         System.out.println("... done");
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         System.out.println("adding Listener");
99cdf0e10cSrcweir         m_xEventBroadcaster.addEventListener(m_xEventListener);
100cdf0e10cSrcweir         System.out.println("... done");
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     @Test public void checkWriter() {
104cdf0e10cSrcweir         System.out.println("-- Checking Writer --");
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         WriterHelper wHelper = new WriterHelper(m_xMSF);
107cdf0e10cSrcweir         String[] expected;
108cdf0e10cSrcweir         System.out.println("opening an empty writer doc");
109cdf0e10cSrcweir         notifyEvents.clear();
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir             XTextDocument xTextDoc = wHelper.openEmptyDoc();
112cdf0e10cSrcweir             shortWait();
113cdf0e10cSrcweir             expected = new String[] { "OnUnfocus", "OnCreate", "OnViewCreated", "OnFocus" };
114cdf0e10cSrcweir 
115cdf0e10cSrcweir             assertTrue("Wrong events fired when opening empty doc",
116cdf0e10cSrcweir                    proveExpectation(expected));
117cdf0e10cSrcweir             System.out.println("... done");
118cdf0e10cSrcweir 
119cdf0e10cSrcweir             System.out.println("changing the writer doc");
120cdf0e10cSrcweir             notifyEvents.clear();
121cdf0e10cSrcweir             xTextDoc.getText().setString("GlobalEventBroadcaster");
122cdf0e10cSrcweir             shortWait();
123cdf0e10cSrcweir             expected = new String[] { "OnModifyChanged" };
124cdf0e10cSrcweir 
125cdf0e10cSrcweir             assertTrue("Wrong events fired when changing doc",
126cdf0e10cSrcweir                    proveExpectation(expected));
127cdf0e10cSrcweir             System.out.println("... done");
128cdf0e10cSrcweir 
129cdf0e10cSrcweir             System.out.println("closing the empty writer doc");
130cdf0e10cSrcweir             notifyEvents.clear();
131cdf0e10cSrcweir             wHelper.closeDoc(xTextDoc);
132cdf0e10cSrcweir             shortWait();
133cdf0e10cSrcweir         }
134cdf0e10cSrcweir         expected = new String[] { "OnUnfocus", "OnFocus", "OnViewClosed", "OnUnload" };
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         assertTrue("Wrong events fired when closing empty doc",
137cdf0e10cSrcweir                proveExpectation(expected));
138cdf0e10cSrcweir         System.out.println("... done");
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         System.out.println("opening an writer doc via Window-New Window");
141cdf0e10cSrcweir         notifyEvents.clear();
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir             XTextDocument xTextDoc = wHelper.openFromDialog(".uno:NewWindow", "", false);
144cdf0e10cSrcweir 
145cdf0e10cSrcweir             shortWait();
146cdf0e10cSrcweir             expected = new String[] { "OnUnfocus", "OnCreate", "OnViewCreated", "OnFocus", "OnUnfocus", "OnViewCreated", "OnFocus", };
147cdf0e10cSrcweir 
148cdf0e10cSrcweir             assertTrue("Wrong events fired when opening an writer doc via Window-New Window",
149cdf0e10cSrcweir                    proveExpectation(expected));
150cdf0e10cSrcweir             System.out.println("... done");
151cdf0e10cSrcweir 
152cdf0e10cSrcweir             System.out.println("closing the created writer doc");
153cdf0e10cSrcweir             notifyEvents.clear();
154cdf0e10cSrcweir 
155cdf0e10cSrcweir             wHelper.closeDoc(xTextDoc);
156cdf0e10cSrcweir             shortWait();
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir         expected = new String[] { "OnViewClosed", "OnUnfocus", "OnFocus", "OnViewClosed", "OnUnload" };
159cdf0e10cSrcweir 
160cdf0e10cSrcweir         assertTrue("Wrong events fired when closing Window-New Window",
161cdf0e10cSrcweir                proveExpectation(expected));
162cdf0e10cSrcweir 
163cdf0e10cSrcweir         System.out.println("... done");
164cdf0e10cSrcweir         // TODO: It seems not possible to close the document without interactiv question
165cdf0e10cSrcweir         //       there the follow test will not be execute
166cdf0e10cSrcweir         if (false) {
167cdf0e10cSrcweir             System.out.println("Opening document with label wizard");
168cdf0e10cSrcweir             XTextDocument xTextDoc = wHelper.openFromDialog("private:factory/swriter?slot=21051", "", false);
169cdf0e10cSrcweir             shortWait();
170cdf0e10cSrcweir             XWindow xWindow = UnoRuntime.queryInterface(XWindow.class, wHelper.getToolkit().getActiveTopWindow());
171cdf0e10cSrcweir             UITools ut = new UITools(m_xMSF,xWindow);
172cdf0e10cSrcweir             notifyEvents.clear();
173cdf0e10cSrcweir             System.out.println("pressing button 'New Document'");
174cdf0e10cSrcweir             try{
175cdf0e10cSrcweir                 ut.clickButton ("New Document");
176cdf0e10cSrcweir             } catch (Exception e) {
177cdf0e10cSrcweir                 System.out.println("Couldn't press Button");
178cdf0e10cSrcweir             }
179cdf0e10cSrcweir             System.out.println("... done");
180cdf0e10cSrcweir             shortWait();
181cdf0e10cSrcweir             shortWait();
182cdf0e10cSrcweir             shortWait();
183cdf0e10cSrcweir             expected = new String[] { "OnViewClosed", "OnCreate", "OnFocus", "OnModifyChanged" };
184cdf0e10cSrcweir 
185cdf0e10cSrcweir             assertTrue("Wrong events fired when starting labels wizard",
186cdf0e10cSrcweir                    proveExpectation(expected));
187cdf0e10cSrcweir 
188cdf0e10cSrcweir             System.out.println("Try to close document...");
189cdf0e10cSrcweir             wHelper.closeDoc(xTextDoc);
190cdf0e10cSrcweir             shortWait();
191cdf0e10cSrcweir             wHelper.closeFromDialog();
192cdf0e10cSrcweir             shortWait();
193cdf0e10cSrcweir             xTextDoc = null;
194cdf0e10cSrcweir         }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir         System.out.println("-- Done Writer --");
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir     @After public void cleanup() {
200cdf0e10cSrcweir         System.out.println("removing Listener");
201cdf0e10cSrcweir         m_xEventBroadcaster.removeEventListener(m_xEventListener);
202cdf0e10cSrcweir         System.out.println("... done");
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir     /**
206cdf0e10cSrcweir     * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
207cdf0e10cSrcweir     * reset</code> call.
208cdf0e10cSrcweir     */
209cdf0e10cSrcweir     private void shortWait() {
210cdf0e10cSrcweir         try {
211cdf0e10cSrcweir             Thread.sleep(2000);
212cdf0e10cSrcweir         } catch (InterruptedException e) {
213cdf0e10cSrcweir             System.out.println("While waiting :" + e);
214cdf0e10cSrcweir         }
215cdf0e10cSrcweir     }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir     private boolean proveExpectation(String[] expected) {
218cdf0e10cSrcweir         boolean locRes = true;
219cdf0e10cSrcweir 				boolean failure = false;
220cdf0e10cSrcweir 
221cdf0e10cSrcweir         System.out.println("Fired Events:");
222cdf0e10cSrcweir         for (int k=0;k<notifyEvents.size();k++) {
223cdf0e10cSrcweir             System.out.println("\t- "+notifyEvents.get(k));
224cdf0e10cSrcweir         }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir         for (int i = 0; i < expected.length; i++) {
227cdf0e10cSrcweir             locRes = notifyEvents.contains(expected[i]);
228cdf0e10cSrcweir 
229cdf0e10cSrcweir             if (!locRes) {
230cdf0e10cSrcweir                 System.out.println("The event " + expected[i] + " isn't fired");
231cdf0e10cSrcweir 								failure = true;
232cdf0e10cSrcweir             }
233cdf0e10cSrcweir         }
234cdf0e10cSrcweir 
235cdf0e10cSrcweir         return !failure;
236cdf0e10cSrcweir     }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir     public class EventListenerImpl implements XEventListener {
239cdf0e10cSrcweir         public void disposing(com.sun.star.lang.EventObject eventObject) {
240cdf0e10cSrcweir             System.out.println("disposing: " + eventObject.Source.toString());
241cdf0e10cSrcweir         }
242cdf0e10cSrcweir 
243cdf0e10cSrcweir         public void notifyEvent(com.sun.star.document.EventObject eventObject) {
244cdf0e10cSrcweir             notifyEvents.add(eventObject.EventName);
245cdf0e10cSrcweir         }
246cdf0e10cSrcweir     }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir         private XMultiServiceFactory getMSF()
249cdf0e10cSrcweir     {
250cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
251cdf0e10cSrcweir         return xMSF1;
252cdf0e10cSrcweir     }
253cdf0e10cSrcweir 
254cdf0e10cSrcweir     // setup and close connections
255cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
256cdf0e10cSrcweir         System.out.println("setUpConnection()");
257cdf0e10cSrcweir         connection.setUp();
258cdf0e10cSrcweir     }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
261cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
262cdf0e10cSrcweir     {
263cdf0e10cSrcweir         System.out.println("tearDownConnection() CheckGlobalEventBroadcaster_writer1");
264cdf0e10cSrcweir         connection.tearDown();
265cdf0e10cSrcweir     }
266cdf0e10cSrcweir 
267cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
268cdf0e10cSrcweir 
269cdf0e10cSrcweir }
270