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