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 
24 package integration.forms;
25 
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.util.XCloseable;
28 import com.sun.star.util.XModifiable;
29 
30 public abstract class TestCase extends complexlib.ComplexTestCase implements com.sun.star.lang.XEventListener
31 {
32     protected XMultiServiceFactory    m_orb;              /// our service factory
33     protected DocumentType            m_documentType;     /// the type of our document
34     protected DocumentHelper          m_document;         /// our current test document
35     protected FormLayer               m_formLayer;
36 
37     /** Creates a new instance of TestCase */
TestCase( DocumentType docType )38     public TestCase( DocumentType docType )
39     {
40         m_documentType = docType;
41     }
42 
43     /* ------------------------------------------------------------------ */
getTestObjectName()44     public String getTestObjectName()
45     {
46         return this.getClass().getName();
47     }
48 
49     /* ------------------------------------------------------------------ */
before()50     public void before() throws com.sun.star.uno.Exception, java.lang.Exception
51     {
52         m_orb = (XMultiServiceFactory)param.getMSF();
53     }
54 
55     /* ------------------------------------------------------------------ */
after()56     public void after() throws com.sun.star.uno.Exception, java.lang.Exception
57     {
58     }
59 
60     /* ------------------------------------------------------------------ */
61     /** closes our document, if we have an open one, via (simulated) user input
62      */
closeDocumentByUI()63     protected void closeDocumentByUI()
64     {
65         try
66         {
67             if ( m_document != null )
68             {
69                 // first, set the document to "unmodified"
70                 XModifiable docModify = (XModifiable)m_document.query( XModifiable.class );
71                 docModify.setModified( false );
72 
73                 m_document.getCurrentView().dispatch( ".uno:CloseDoc" );
74 
75                 // CloseDoc is asynchronous, so wait until it's done - or 1 second, at most
76                 synchronized ( this ) { wait( 1000 ); }
77             }
78         }
79         catch ( java.lang.Exception e )
80         {
81             e.printStackTrace( System.out );
82         }
83     }
84 
85     /* ------------------------------------------------------------------ */
86     /** closes our document, if we have an open one
87      */
closeDocument()88     protected void closeDocument()
89     {
90         try
91         {
92             // close our document
93             if ( m_document != null )
94             {
95                 XCloseable closeDoc = (XCloseable)m_document.query( XCloseable.class );
96                 closeDoc.close( true );
97             }
98         }
99         catch ( com.sun.star.uno.Exception e )
100         {
101             e.printStackTrace( System.out );
102         }
103     }
104 
105     /* ------------------------------------------------------------------ */
106     /** prepares a new document to work with
107      */
prepareDocument()108     protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception
109     {
110         m_document = DocumentHelper.blankDocument( m_orb, m_documentType );
111         m_document.getDocument( ).addEventListener( this );
112         m_formLayer = new FormLayer( m_document );
113     }
114 
115     /* ------------------------------------------------------------------ */
116     /* internal methods                                                   */
117     /* ------------------------------------------------------------------ */
118     /** waits for the user to press a key (on the console where she started the java program)
119         or the document to be closed by the user.
120     @return
121         <TRUE/> if the user pressed a key on the console, <FALSE/> if she closed the document
122     */
waitForUserInput()123     protected boolean waitForUserInput() throws java.lang.Exception
124     {
125         synchronized (this)
126         {
127             integration.forms.WaitForInput aWait = new integration.forms.WaitForInput( this );
128             aWait.start();
129             wait();
130 
131             // if the waiter thread is done, the user pressed enter
132             boolean bKeyPressed = aWait.isDone();
133             if ( !bKeyPressed )
134                 aWait.interrupt();
135 
136             return bKeyPressed;
137         }
138     }
139 
140     /* ------------------------------------------------------------------ */
141     /* XEventListener overridables                                        */
142     /* ------------------------------------------------------------------ */
disposing( com.sun.star.lang.EventObject eventObject )143     public void disposing( com.sun.star.lang.EventObject eventObject )
144     {
145         if ( m_document.getDocument().equals( eventObject.Source ) )
146         {
147             // notify ourself that we can stop waiting for user input
148             synchronized (this)
149             {
150                 notify();
151             }
152         }
153     }
154 }
155