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.uno.*;
27 import com.sun.star.util.*;
28 import com.sun.star.lang.*;
29 import com.sun.star.accessibility.*;
30 import com.sun.star.container.*;
31 import com.sun.star.beans.*;
32 import com.sun.star.form.binding.*;
33 
34 import integration.forms.DocumentHelper;
35 import integration.forms.NumericValidator;
36 import integration.forms.TextValidator;
37 import integration.forms.BooleanValidator;
38 import integration.forms.ListSelectionValidator;
39 import integration.forms.SingleControlValidation;
40 
41 /**
42  *
43  * @author  fs@openoffice.org
44  */
45 public class ControlValidation extends complexlib.ComplexTestCase implements com.sun.star.lang.XEventListener
46 {
47     private DocumentHelper          m_document;         /// our current test document
48     private XMultiServiceFactory    m_orb;              /// our service factory
49 
50     /** Creates a new instance of ControlValidation */
ControlValidation()51     public ControlValidation()
52     {
53     }
54 
getTestMethodNames()55     public String[] getTestMethodNames()
56     {
57         return new String[] {
58             "interactiveValidation"
59         };
60     }
61 
getTestObjectName()62     public String getTestObjectName()
63     {
64         return "Form Control Validation Test";
65     }
66 
isInteractiveTest()67     public static boolean isInteractiveTest()
68     {
69         return true;
70     }
71 
72     /* ------------------------------------------------------------------ */
73     /* test framework                                                     */
74     /* ------------------------------------------------------------------ */
before()75     public void before() throws com.sun.star.uno.Exception, java.lang.Exception
76     {
77         m_orb = (XMultiServiceFactory)param.getMSF();
78     }
79 
80     /* ------------------------------------------------------------------ */
prepareTestStep( )81     private void prepareTestStep( ) throws com.sun.star.uno.Exception, java.lang.Exception
82     {
83         m_document = DocumentHelper.blankTextDocument( m_orb );
84         m_document.getDocument( ).addEventListener( this );
85     }
86 
87     /* ------------------------------------------------------------------ */
after()88     public void after()
89     {
90         closeDocument();
91     }
92 
93     /* ------------------------------------------------------------------ */
94     /** closes our document, if we have an open one
95      */
closeDocument()96     private void closeDocument()
97     {
98         try
99         {
100             // close our document
101             if ( m_document != null )
102             {
103                 XCloseable closeDoc = (XCloseable)UnoRuntime.queryInterface( XCloseable.class,
104                     m_document.getDocument() );
105                 closeDoc.close( true );
106             }
107         }
108         catch ( com.sun.star.uno.Exception e )
109         {
110             e.printStackTrace( System.out );
111         }
112     }
113 
114     /* ------------------------------------------------------------------ */
115     /* public test methods                                                */
116     /* ------------------------------------------------------------------ */
interactiveValidation()117     public void interactiveValidation() throws com.sun.star.uno.Exception, java.lang.Exception
118     {
119         prepareTestStep();
120 
121         SingleControlValidation validation;
122         XPropertySet focusField;
123 
124         validation = new SingleControlValidation( m_document, 5, 5, "DatabaseFormattedField", new NumericValidator() );
125         focusField = validation.getInputField();
126         validation.setExplanatoryText( "Please enter a number between 0 and 100, with at most 1 decimal digit" );
127 
128         validation = new SingleControlValidation( m_document, 90, 5, "DatabaseTextField", new TextValidator() );
129         validation.setExplanatoryText( "Please enter a text whose length is a multiple of 3, and which does not contain the letter 'Z'" );
130 
131         validation = new SingleControlValidation( m_document, 5, 55, "DatabaseDateField", new DateValidator() );
132         validation.setExplanatoryText( "Please enter a date in the current month" );
133         validation.getInputField().setPropertyValue( "Dropdown", new Boolean( true ) );
134 
135         validation = new SingleControlValidation( m_document, 90, 55, "DatabaseTimeField", new TimeValidator() );
136         validation.setExplanatoryText( "Please enter a time. Valid values are all full hours." );
137 
138         validation = new SingleControlValidation( m_document, 5, 110, "DatabaseCheckBox", new BooleanValidator( false ) );
139         validation.setExplanatoryText( "Please check (well, or uncheck) the box. Don't leave it in indetermined state." );
140         validation.getInputField().setPropertyValue( "TriState", new Boolean( true ) );
141 
142         validation = new SingleControlValidation( m_document, 90, 110, "DatabaseRadioButton", new BooleanValidator( true ), 3, 0 );
143         validation.setExplanatoryText( "Please check any but the first button" );
144 
145         validation = new SingleControlValidation( m_document, 5, 165, "DatabaseListBox", new ListSelectionValidator( ), 1, 24 );
146         validation.setExplanatoryText( "Please select not more than two entries." );
147         validation.getInputField().setPropertyValue( "MultiSelection", new Boolean( true ) );
148         validation.getInputField().setPropertyValue( "StringItemList", new String[] { "first", "second", "third", "forth", "fivth" } );
149 
150         // switch to alive mode
151         m_document.getCurrentView( ).toggleFormDesignMode( );
152         m_document.getCurrentView( ).grabControlFocus( focusField );
153 
154         // wait for the user telling us to exit
155         waitForUserInput();
156     }
157 
158     /* ------------------------------------------------------------------ */
159     /* internal methods                                                   */
160     /* ------------------------------------------------------------------ */
161     /** waits for the user to press a key (on the console where she started the java program)
162             or the document to be closed by the user.
163             @return
164                     <TRUE/> if the user pressed a key on the console, <FALSE/> if she closed the document
165     */
waitForUserInput()166     protected boolean waitForUserInput() throws java.lang.Exception
167     {
168         synchronized (this)
169         {
170             integration.forms.WaitForInput aWait = new integration.forms.WaitForInput( this );
171             aWait.start();
172             wait();
173 
174             // if the waiter thread is done, the user pressed enter
175             boolean bKeyPressed = aWait.isDone();
176             if ( !bKeyPressed )
177                 aWait.interrupt();
178 
179             return bKeyPressed;
180         }
181     }
182 
183     /* ------------------------------------------------------------------ */
184     /* XEventListener overridables                                        */
185     /* ------------------------------------------------------------------ */
disposing( com.sun.star.lang.EventObject eventObject )186     public void disposing( com.sun.star.lang.EventObject eventObject )
187     {
188         if ( m_document.getDocument().equals( eventObject.Source ) )
189 	{
190             // notify ourself that we can stop waiting for user input
191             synchronized (this)
192             {
193                 notify();
194             }
195         }
196     }
197 
198 }
199