1*a4c7eb23SAndrew Rist /**************************************************************
2*a4c7eb23SAndrew Rist  *
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 
22cdf0e10cSrcweir /*
23cdf0e10cSrcweir  * To change this template, choose Tools | Templates
24cdf0e10cSrcweir  * and open the template in the editor.
25cdf0e10cSrcweir  */
26cdf0e10cSrcweir 
27cdf0e10cSrcweir package complex.sfx2;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.document.DocumentEvent;
30cdf0e10cSrcweir import com.sun.star.document.XDocumentEventBroadcaster;
31cdf0e10cSrcweir import com.sun.star.document.XDocumentEventListener;
32cdf0e10cSrcweir import com.sun.star.lang.EventObject;
33cdf0e10cSrcweir import com.sun.star.lang.XEventListener;
34cdf0e10cSrcweir import com.sun.star.uno.Exception;
35cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
36cdf0e10cSrcweir import com.sun.star.util.CloseVetoException;
37cdf0e10cSrcweir import com.sun.star.util.XCloseListener;
38cdf0e10cSrcweir import com.sun.star.util.XCloseable;
39cdf0e10cSrcweir import java.util.Vector;
40cdf0e10cSrcweir import java.util.logging.Level;
41cdf0e10cSrcweir import java.util.logging.Logger;
42cdf0e10cSrcweir import org.junit.After;
43cdf0e10cSrcweir import org.junit.Before;
44cdf0e10cSrcweir import org.junit.Test;
45cdf0e10cSrcweir import static org.junit.Assert.*;
46cdf0e10cSrcweir import org.openoffice.test.tools.OfficeDocument;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir /**
49cdf0e10cSrcweir  *
50cdf0e10cSrcweir  * @author frank.shoenheit@oracle.com
51cdf0e10cSrcweir  */
52cdf0e10cSrcweir public class DocumentEvents extends JUnitBasedTest
53cdf0e10cSrcweir {
54cdf0e10cSrcweir     @Before
beforeTest()55cdf0e10cSrcweir     public void beforeTest() throws Exception
56cdf0e10cSrcweir     {
57cdf0e10cSrcweir         m_document = OfficeDocument.blankTextDocument( this.getORB() );
58cdf0e10cSrcweir     }
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     @After
afterTest()61cdf0e10cSrcweir     public void afterTest()
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         if ( m_document != null )
64cdf0e10cSrcweir         {
65cdf0e10cSrcweir             assertTrue( "closing the test document failed", m_document.close() );
66cdf0e10cSrcweir             m_document = null;
67cdf0e10cSrcweir         }
68cdf0e10cSrcweir     }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     /**
71cdf0e10cSrcweir      * sets up the environment for a test which checks the behavior upon closing a doc
72cdf0e10cSrcweir      */
impl_setupDocCloseTest()73cdf0e10cSrcweir     private void impl_setupDocCloseTest()
74cdf0e10cSrcweir     {
75cdf0e10cSrcweir         m_observedCloseEvents.clear();
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         final XDocumentEventBroadcaster docEventBroadcaster = UnoRuntime.queryInterface(
78cdf0e10cSrcweir             XDocumentEventBroadcaster.class, m_document.getDocument() );
79cdf0e10cSrcweir         docEventBroadcaster.addDocumentEventListener( new DocumentEventListener() );
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         final XCloseable docCloseable = UnoRuntime.queryInterface( XCloseable.class,
82cdf0e10cSrcweir                 m_document.getDocument() );
83cdf0e10cSrcweir         docCloseable.addCloseListener( new CloseListener() );
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         m_document.getDocument().addEventListener( new DocDisposeListener() );
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     /**
89cdf0e10cSrcweir      * sets up the environment for a test which checks the behavior upon closing a doc
90cdf0e10cSrcweir      */
impl_tearDownDocCloseTest( final String i_docCloseMethod )91cdf0e10cSrcweir     private void impl_tearDownDocCloseTest( final String i_docCloseMethod )
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         synchronized( m_document )
94cdf0e10cSrcweir         {
95cdf0e10cSrcweir             try
96cdf0e10cSrcweir             {
97cdf0e10cSrcweir                 m_document.wait(10000);
98cdf0e10cSrcweir             }
99cdf0e10cSrcweir             catch (InterruptedException ex)
100cdf0e10cSrcweir             {
101cdf0e10cSrcweir                 // don't continue the test if somebody interrupted us ...
102cdf0e10cSrcweir                 return;
103cdf0e10cSrcweir             }
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         m_document = null;
107cdf0e10cSrcweir         synchronized( m_observedCloseEvents )
108cdf0e10cSrcweir         {
109cdf0e10cSrcweir             assertArrayEquals(
110cdf0e10cSrcweir                 "wrong order of events when closing a doc " + i_docCloseMethod,
111cdf0e10cSrcweir                 new CloseEventType[] { CloseEventType.OnUnload, CloseEventType.NotifyClosing, CloseEventType.Disposing },
112cdf0e10cSrcweir                 m_observedCloseEvents.toArray( new CloseEventType[0] )
113cdf0e10cSrcweir             );
114cdf0e10cSrcweir         }
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     @Test
testCloseWinEvents()118cdf0e10cSrcweir     public void testCloseWinEvents() throws Exception
119cdf0e10cSrcweir     {
120cdf0e10cSrcweir         impl_setupDocCloseTest();
121cdf0e10cSrcweir         m_document.getCurrentView().dispatch( ".uno:CloseWin" );
122cdf0e10cSrcweir         impl_tearDownDocCloseTest( "via .uno:CloseWin" );
123cdf0e10cSrcweir     }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     //@Test
testCloseDocEvents()126cdf0e10cSrcweir     public void testCloseDocEvents() throws Exception
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         impl_setupDocCloseTest();
129cdf0e10cSrcweir         m_document.getCurrentView().dispatch( ".uno:CloseDoc" );
130cdf0e10cSrcweir         impl_tearDownDocCloseTest( "via .uno:CloseDoc" );
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     //@Test
testCloseByAPI()134cdf0e10cSrcweir     public void testCloseByAPI() throws Exception
135cdf0e10cSrcweir     {
136cdf0e10cSrcweir         impl_setupDocCloseTest();
137cdf0e10cSrcweir         // closing the doc by API is synchronous, so do this in a separate thread, else we will get a deadlock
138cdf0e10cSrcweir         // when the document tries to call back our listener (well, I admit I didn't understand *why* we get this
139cdf0e10cSrcweir         // deadlock ... :-\ )
140cdf0e10cSrcweir         (new DocCloser()).start();
141cdf0e10cSrcweir         impl_tearDownDocCloseTest( "by API" );
142cdf0e10cSrcweir     }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     private class DocumentEventListener implements XDocumentEventListener
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir 
documentEventOccured( DocumentEvent i_documentEvent )147cdf0e10cSrcweir         public void documentEventOccured( DocumentEvent i_documentEvent )
148cdf0e10cSrcweir         {
149cdf0e10cSrcweir             if ( i_documentEvent.EventName.equals( "OnUnload" ) )
150cdf0e10cSrcweir             {
151cdf0e10cSrcweir                 synchronized( m_observedCloseEvents )
152cdf0e10cSrcweir                 {
153cdf0e10cSrcweir                     m_observedCloseEvents.add( CloseEventType.OnUnload );
154cdf0e10cSrcweir                 }
155cdf0e10cSrcweir             }
156cdf0e10cSrcweir         }
157cdf0e10cSrcweir 
disposing(EventObject eo)158cdf0e10cSrcweir         public void disposing(EventObject eo)
159cdf0e10cSrcweir         {
160cdf0e10cSrcweir             // not interested in
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir     };
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     private class CloseListener implements XCloseListener
165cdf0e10cSrcweir     {
166cdf0e10cSrcweir 
queryClosing(EventObject eo, boolean bln)167cdf0e10cSrcweir         public void queryClosing(EventObject eo, boolean bln) throws CloseVetoException
168cdf0e10cSrcweir         {
169cdf0e10cSrcweir             // not interested in
170cdf0e10cSrcweir         }
171cdf0e10cSrcweir 
notifyClosing(EventObject eo)172cdf0e10cSrcweir         public void notifyClosing(EventObject eo)
173cdf0e10cSrcweir         {
174cdf0e10cSrcweir             synchronized( m_observedCloseEvents )
175cdf0e10cSrcweir             {
176cdf0e10cSrcweir                 m_observedCloseEvents.add( CloseEventType.NotifyClosing );
177cdf0e10cSrcweir             }
178cdf0e10cSrcweir         }
179cdf0e10cSrcweir 
disposing(EventObject eo)180cdf0e10cSrcweir         public void disposing(EventObject eo)
181cdf0e10cSrcweir         {
182cdf0e10cSrcweir             // not interested in
183cdf0e10cSrcweir         }
184cdf0e10cSrcweir     };
185cdf0e10cSrcweir 
186cdf0e10cSrcweir     private class DocDisposeListener implements XEventListener
187cdf0e10cSrcweir     {
disposing(EventObject eo)188cdf0e10cSrcweir         public void disposing(EventObject eo)
189cdf0e10cSrcweir         {
190cdf0e10cSrcweir             synchronized( m_observedCloseEvents )
191cdf0e10cSrcweir             {
192cdf0e10cSrcweir                 m_observedCloseEvents.add( CloseEventType.Disposing );
193cdf0e10cSrcweir             }
194cdf0e10cSrcweir             synchronized ( m_document )
195cdf0e10cSrcweir             {
196cdf0e10cSrcweir                 m_document.notifyAll();
197cdf0e10cSrcweir             }
198cdf0e10cSrcweir         }
199cdf0e10cSrcweir     };
200cdf0e10cSrcweir 
201cdf0e10cSrcweir     private class DocCloser extends Thread
202cdf0e10cSrcweir     {
203cdf0e10cSrcweir         @Override
run()204cdf0e10cSrcweir         public void run()
205cdf0e10cSrcweir         {
206cdf0e10cSrcweir             try
207cdf0e10cSrcweir             {
208cdf0e10cSrcweir                 final XCloseable docCloseable = UnoRuntime.queryInterface(XCloseable.class, m_document.getDocument());
209cdf0e10cSrcweir                 docCloseable.close(true);
210cdf0e10cSrcweir             }
211cdf0e10cSrcweir             catch (CloseVetoException ex)
212cdf0e10cSrcweir             {
213cdf0e10cSrcweir                 Logger.getLogger(DocumentEvents.class.getName()).log(Level.SEVERE, null, ex);
214cdf0e10cSrcweir             }
215cdf0e10cSrcweir         }
216cdf0e10cSrcweir     };
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     private enum CloseEventType
219cdf0e10cSrcweir     {
220cdf0e10cSrcweir         OnUnload,
221cdf0e10cSrcweir         NotifyClosing,
222cdf0e10cSrcweir         Disposing
223cdf0e10cSrcweir     };
224cdf0e10cSrcweir 
225cdf0e10cSrcweir     private OfficeDocument m_document = null;
226cdf0e10cSrcweir     final private Vector< CloseEventType > m_observedCloseEvents = new Vector<DocumentEvents.CloseEventType>();
227cdf0e10cSrcweir }
228