summaryrefslogtreecommitdiffstats
path: root/sfx2/qa/complex/sfx2/DocumentEvents.java
blob: a38e13756551c478fd18eed91c24101b83aa5310 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package complex.sfx2;

import com.sun.star.document.DocumentEvent;
import com.sun.star.document.XDocumentEventBroadcaster;
import com.sun.star.document.XDocumentEventListener;
import com.sun.star.lang.EventObject;
import com.sun.star.lang.XEventListener;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.util.CloseVetoException;
import com.sun.star.util.XCloseListener;
import com.sun.star.util.XCloseable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import org.openoffice.test.tools.OfficeDocument;

/**
 *
 * @author frank.shoenheit@oracle.com
 */
public class DocumentEvents extends JUnitBasedTest
{
    @Before
    public void beforeTest() throws Exception
    {
        m_document = OfficeDocument.blankTextDocument( this.getORB() );
    }

    @After
    public void afterTest()
    {
        if ( m_document != null )
        {
            assertTrue( "closing the test document failed", m_document.close() );
            m_document = null;
        }
    }

    /**
     * sets up the environment for a test which checks the behavior upon closing a doc
     */
    private void impl_setupDocCloseTest()
    {
        m_observedCloseEvents.clear();

        final XDocumentEventBroadcaster docEventBroadcaster = UnoRuntime.queryInterface(
            XDocumentEventBroadcaster.class, m_document.getDocument() );
        docEventBroadcaster.addDocumentEventListener( new DocumentEventListener() );

        final XCloseable docCloseable = UnoRuntime.queryInterface( XCloseable.class,
                m_document.getDocument() );
        docCloseable.addCloseListener( new CloseListener() );

        m_document.getDocument().addEventListener( new DocDisposeListener() );
    }

    /**
     * sets up the environment for a test which checks the behavior upon closing a doc
     */
    private void impl_tearDownDocCloseTest( final String i_docCloseMethod )
    {
        synchronized( m_document )
        {
            try
            {
                m_document.wait(10000);
            }
            catch (InterruptedException ex)
            {
                // don't continue the test if somebody interrupted us ...
                return;
            }
        }

        m_document = null;
        synchronized( m_observedCloseEvents )
        {
            assertArrayEquals(
                "wrong order of events when closing a doc " + i_docCloseMethod,
                new CloseEventType[] { CloseEventType.OnUnload, CloseEventType.NotifyClosing, CloseEventType.Disposing },
                m_observedCloseEvents.toArray( new CloseEventType[0] )
            );
        }
    }

    @Test
    public void testCloseWinEvents() throws Exception
    {
        impl_setupDocCloseTest();
        m_document.getCurrentView().dispatch( ".uno:CloseWin" );
        impl_tearDownDocCloseTest( "via .uno:CloseWin" );
    }

    //@Test
    public void testCloseDocEvents() throws Exception
    {
        impl_setupDocCloseTest();
        m_document.getCurrentView().dispatch( ".uno:CloseDoc" );
        impl_tearDownDocCloseTest( "via .uno:CloseDoc" );
    }

    //@Test
    public void testCloseByAPI() throws Exception
    {
        impl_setupDocCloseTest();
        // closing the doc by API is synchronous, so do this in a separate thread, else we will get a deadlock
        // when the document tries to call back our listener (well, I admit I didn't understand *why* we get this
        // deadlock ... :-\ )
        (new DocCloser()).start();
        impl_tearDownDocCloseTest( "by API" );
    }

    private class DocumentEventListener implements XDocumentEventListener
    {

        public void documentEventOccured( DocumentEvent i_documentEvent )
        {
            if ( i_documentEvent.EventName.equals( "OnUnload" ) )
            {
                synchronized( m_observedCloseEvents )
                {
                    m_observedCloseEvents.add( CloseEventType.OnUnload );
                }
            }
        }

        public void disposing(EventObject eo)
        {
            // not interested in
        }
    };

    private class CloseListener implements XCloseListener
    {

        public void queryClosing(EventObject eo, boolean bln) throws CloseVetoException
        {
            // not interested in
        }

        public void notifyClosing(EventObject eo)
        {
            synchronized( m_observedCloseEvents )
            {
                m_observedCloseEvents.add( CloseEventType.NotifyClosing );
            }
        }

        public void disposing(EventObject eo)
        {
            // not interested in
        }
    };

    private class DocDisposeListener implements XEventListener
    {
        public void disposing(EventObject eo)
        {
            synchronized( m_observedCloseEvents )
            {
                m_observedCloseEvents.add( CloseEventType.Disposing );
            }
            synchronized ( m_document )
            {
                m_document.notifyAll();
            }
        }
    };

    private class DocCloser extends Thread
    {
        @Override
        public void run()
        {
            try
            {
                final XCloseable docCloseable = UnoRuntime.queryInterface(XCloseable.class, m_document.getDocument());
                docCloseable.close(true);
            }
            catch (CloseVetoException ex)
            {
                Logger.getLogger(DocumentEvents.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };

    private enum CloseEventType
    {
        OnUnload,
        NotifyClosing,
        Disposing
    };

    private OfficeDocument m_document = null;
    final private Vector< CloseEventType > m_observedCloseEvents = new Vector<DocumentEvents.CloseEventType>();
}