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