xref: /trunk/main/forms/qa/integration/forms/TestCase.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
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 
31 public abstract class TestCase implements com.sun.star.lang.XEventListener
32 {
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     /* ------------------------------------------------------------------ */
50     /** closes our document, if we have an open one, via (simulated) user input
51      */
closeDocumentByUI()52     protected void closeDocumentByUI()
53     {
54         try
55         {
56             if ( m_document != null )
57             {
58                 // first, set the document to "unmodified"
59                 XModifiable docModify = (XModifiable)m_document.query( XModifiable.class );
60                 docModify.setModified( false );
61 
62                 m_document.getCurrentView().dispatch( ".uno:CloseDoc" );
63 
64                 // CloseDoc is asynchronous, so wait until it's done - or 1 second, at most
65                 synchronized ( this ) { wait( 1000 ); }
66             }
67         }
68         catch ( java.lang.Exception e )
69         {
70             e.printStackTrace( System.out );
71         }
72     }
73 
74     /* ------------------------------------------------------------------ */
75     /** closes our document, if we have an open one
76      */
closeDocument()77     protected void closeDocument()
78     {
79         try
80         {
81             // close our document
82             if ( m_document != null )
83             {
84                 XCloseable closeDoc = (XCloseable)m_document.query( XCloseable.class );
85                 closeDoc.close( true );
86             }
87         }
88         catch ( com.sun.star.uno.Exception e )
89         {
90             e.printStackTrace( System.out );
91         }
92     }
93 
94     /* ------------------------------------------------------------------ */
95     /** prepares a new document to work with
96      */
prepareDocument(XMultiServiceFactory orb)97     protected void prepareDocument(XMultiServiceFactory orb) throws com.sun.star.uno.Exception, java.lang.Exception
98     {
99         m_document = DocumentHelper.blankDocument( orb, m_documentType );
100         m_document.getDocument( ).addEventListener( this );
101         m_formLayer = new FormLayer( m_document );
102     }
103 
104     /* ------------------------------------------------------------------ */
105     /* internal methods                                                   */
106     /* ------------------------------------------------------------------ */
107     /** waits for the user to press a key (on the console where she started the java program)
108         or the document to be closed by the user.
109     @return
110         <TRUE/> if the user pressed a key on the console, <FALSE/> if she closed the document
111     */
waitForUserInput()112     protected boolean waitForUserInput() throws java.lang.Exception
113     {
114         synchronized (this)
115         {
116             integration.forms.WaitForInput aWait = new integration.forms.WaitForInput( this );
117             aWait.start();
118             wait();
119 
120             // if the waiter thread is done, the user pressed enter
121             boolean bKeyPressed = aWait.isDone();
122             if ( !bKeyPressed )
123                 aWait.interrupt();
124 
125             return bKeyPressed;
126         }
127     }
128 
129     /* ------------------------------------------------------------------ */
130     /* XEventListener overridables                                        */
131     /* ------------------------------------------------------------------ */
disposing( com.sun.star.lang.EventObject eventObject )132     public void disposing( com.sun.star.lang.EventObject eventObject )
133     {
134         if ( m_document.getDocument().equals( eventObject.Source ) )
135         {
136             // notify ourself that we can stop waiting for user input
137             synchronized (this)
138             {
139                 notify();
140             }
141         }
142     }
143 }
144