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 package integration.forms;
24 
25 import com.sun.star.uno.*;
26 import com.sun.star.util.*;
27 import com.sun.star.lang.*;
28 import com.sun.star.container.*;
29 import com.sun.star.beans.*;
30 import com.sun.star.awt.XRadioButton;
31 
32 import integration.forms.dbfTools;
33 import integration.forms.DocumentHelper;
34 import integration.forms.SpreadsheetDocument;
35 
36 import util.utils;
37 import java.util.*;
38 import java.io.*;
39 import java.lang.*;
40 
41 /**
42  *
43  * @author  fs@openoffice.org
44  */
45 public class RadioButtons extends complexlib.ComplexTestCase
46 {
47     private DocumentHelper          m_document;         /// our current test document
48     private FormLayer               m_formLayer;        /// quick access to the form layer
49     private XMultiServiceFactory    m_orb;              /// our service factory
50     private XPropertySet            m_primaryForm;      /// the primary form, to be used in text documents and in the first page of spreadsheets
51     private XPropertySet            m_secondaryForm;    /// the secondary form, to be used in the second page of spreadsheets
52 
53     /* ------------------------------------------------------------------ */
RadioButtons()54     public RadioButtons()
55     {
56     }
57 
58     /* ------------------------------------------------------------------ */
getTestMethodNames()59     public String[] getTestMethodNames()
60     {
61         return new String[] {
62             "checkSingleButtons",
63             "checkThreeGroups",
64             "checkMultipleForms",
65             "checkCalcPageSwitch"
66         };
67     }
68 
69     /* ------------------------------------------------------------------ */
getTestObjectName()70     public String getTestObjectName()
71     {
72         return "Form Radio Buttons Test";
73     }
74 
75     /* ------------------------------------------------------------------ */
before()76     public void before() throws com.sun.star.uno.Exception, java.lang.Exception
77     {
78         m_orb = (XMultiServiceFactory)param.getMSF();
79     }
80 
81     /* ------------------------------------------------------------------ */
insertRadio( int nXPos, int nYPos, String label, String name, String refValue )82     private XPropertySet insertRadio( int nXPos, int nYPos, String label, String name, String refValue ) throws com.sun.star.uno.Exception, java.lang.Exception
83     {
84         return insertRadio( nXPos, nYPos, label, name, refValue, null );
85     }
86 
87     /* ------------------------------------------------------------------ */
insertRadio( int nXPos, int nYPos, String label, String name, String refValue, XPropertySet parentForm )88     private XPropertySet insertRadio( int nXPos, int nYPos, String label, String name, String refValue, XPropertySet parentForm ) throws com.sun.star.uno.Exception, java.lang.Exception
89     {
90         XIndexContainer parentContainer = dbfTools.queryIndexContainer( parentForm );
91         XPropertySet xRadio = m_formLayer.createControlAndShape( "DatabaseRadioButton", nXPos, nYPos, 25, 6, parentContainer );
92         xRadio.setPropertyValue( "Label", label );
93         xRadio.setPropertyValue( "RefValue", refValue );
94         xRadio.setPropertyValue( "Name", name );
95 
96         if ( null == m_primaryForm )
97             m_primaryForm = (XPropertySet)dbfTools.getParent( xRadio, XPropertySet.class );
98 
99         return xRadio;
100     }
101 
102     /* ------------------------------------------------------------------ */
103     /** this checks whether n groups of radio buttons, consisting of only one button each,
104      *  behave properly
105      */
checkSingleButtons()106     public void checkSingleButtons() throws com.sun.star.uno.Exception, java.lang.Exception
107     {
108         prepareTestStep( false );
109 
110         insertRadio( 20, 30,  "group 1", "group 1", "" );
111         insertRadio( 20, 38,  "group 2", "group 2", "" );
112         insertRadio( 20, 46,  "group 3", "group 3", "" );
113         insertRadio( 20, 54,  "group 4", "group 4", "" );
114 
115         // switch to alive mode
116         m_document.getCurrentView( ).toggleFormDesignMode( );
117 
118         checkRadio( "group 1", "" );
119         verifySingleRadios( 1, 0, 0, 0 );
120 
121         checkRadio( "group 4", "" );
122         verifySingleRadios( 1, 0, 0, 1 );
123 
124         checkRadio( "group 2", "" );
125         verifySingleRadios( 1, 1, 0, 1 );
126 
127         checkRadio( "group 3", "" );
128         verifySingleRadios( 1, 1, 1, 1 );
129 
130         cleanupTestStep();
131     }
132 
133     /* ------------------------------------------------------------------ */
134     /** creates three groups of radio buttons in a sample document, and checks whether they're working
135      */
checkThreeGroups( )136     public void checkThreeGroups( ) throws com.sun.star.uno.Exception, java.lang.Exception
137     {
138         prepareTestStep( false );
139 
140         insertRadio( 20, 30,  "group 1 (a)", "group 1", "a" );
141         insertRadio( 20, 38,  "group 1 (b)", "group 1", "b" );
142 
143         insertRadio( 20, 50,  "group 2 (a)", "group 2", "a" );
144         insertRadio( 20, 58,  "group 2 (b)", "group 2", "b" );
145 
146         insertRadio( 20, 70,  "group 3 (a)", "group 3", "a" );
147         insertRadio( 20, 78,  "group 3 (b)", "group 3", "b" );
148 
149         // switch to alive mode
150         m_document.getCurrentView( ).toggleFormDesignMode( );
151 
152         // initially, after switching to alive mode, all buttons should be unchecked
153         verifySixPack( 0, 0, 0, 0, 0, 0 );
154 
155         // check one button in every group
156         checkRadio( "group 1", "a" );
157         checkRadio( "group 2", "b" );
158         checkRadio( "group 3", "a" );
159         // and verify that this worked
160         verifySixPack( 1, 0, 0, 1, 1, 0 );
161 
162         // check all buttons which are currently unchecked
163         checkRadio( "group 1", "b" );
164         checkRadio( "group 2", "a" );
165         checkRadio( "group 3", "b" );
166         // this should have reset the previous checks in the respective groups
167         verifySixPack( 0, 1, 1, 0, 0, 1 );
168 
169         // and back to the previous check state
170         checkRadio( "group 1", "a" );
171         checkRadio( "group 2", "b" );
172         checkRadio( "group 3", "a" );
173         verifySixPack( 1, 0, 0, 1, 1, 0 );
174 
175         cleanupTestStep();
176     }
177 
178     /* ------------------------------------------------------------------ */
179     /** tests whether radio buttons which belong to different forms behave properly
180      */
checkMultipleForms( )181     public void checkMultipleForms( ) throws com.sun.star.uno.Exception, java.lang.Exception
182     {
183         prepareTestStep( false );
184 
185         insertRadio( 20, 30,  "group 1 (a)", "group 1", "a" );
186         insertRadio( 20, 38,  "group 1 (b)", "group 1", "b" );
187         insertRadio( 20, 46,  "group 1 (c)", "group 1", "c" );
188 
189         m_secondaryForm = dbfTools.queryPropertySet( m_document.createSiblingForm( m_primaryForm, "secondary" ) );
190 
191         insertRadio( 70, 30,  "group 2 (a)", "group 2", "a", m_secondaryForm );
192         insertRadio( 70, 38,  "group 2 (b)", "group 2", "b", m_secondaryForm );
193         insertRadio( 70, 46,  "group 2 (c)", "group 2", "c", m_secondaryForm );
194 
195         // switch to alive mode
196         m_document.getCurrentView( ).toggleFormDesignMode( );
197 
198         // play around with different check states
199         checkRadio( "group 1", "b", m_primaryForm );
200         checkRadio( "group 2", "c", m_secondaryForm );
201         verifyTwoFormRadios( 0, 1, 0, 0, 0, 1 );
202 
203         checkRadio( "group 1", "c", m_primaryForm );
204         verifyTwoFormRadios( 0, 0, 1, 0, 0, 1 );
205 
206         checkRadio( "group 2", "a", m_secondaryForm );
207         verifyTwoFormRadios( 0, 0, 1, 1, 0, 0 );
208 
209         checkRadio( "group 1", "a", m_primaryForm );
210         verifyTwoFormRadios( 1, 0, 0, 1, 0, 0 );
211 
212         checkRadio( "group 2", "b", m_secondaryForm );
213         verifyTwoFormRadios( 1, 0, 0, 0, 1, 0 );
214 
215         cleanupTestStep();
216     }
217 
218     /* ------------------------------------------------------------------ */
219     /** tests for a special bug which we once had, where radio buttons lost their state after
220      *  switching spreadsheet pages
221      */
checkCalcPageSwitch( )222     public void checkCalcPageSwitch( ) throws com.sun.star.uno.Exception, java.lang.Exception
223     {
224         prepareTestStep( true );
225 
226         m_formLayer.setInsertPage( 0 );
227         insertRadio( 15, 20,  "group 1 (a)", "group 1", "a" );
228         insertRadio( 15, 26,  "group 1 (b)", "group 1", "b" );
229 
230         m_formLayer.setInsertPage( 1 );
231         XPropertySet xRadio = insertRadio( 15, 20,  "group 2 (a)", "group 2", "a" );
232                               insertRadio( 15, 26,  "group 2 (b)", "group 2", "b" );
233         m_secondaryForm = (XPropertySet)dbfTools.getParent( xRadio, XPropertySet.class );
234 
235         // switch to alive mode
236         SpreadsheetView view = (SpreadsheetView)m_document.getCurrentView( );
237         view.toggleFormDesignMode( );
238         // and do initial checking
239         checkRadio( "group 1", "a", m_primaryForm );
240         view.activateSheet( 1 );
241         checkRadio( "group 2", "b", m_secondaryForm );
242 
243         // see whether the check states on the first page survived the page switch
244         verifySheetRadios( 1, 0, 0, 1 );
245         // switch back to the first sheet, and see whether the check states survived
246         view.activateSheet( 0 );
247         verifySheetRadios( 1, 0, 0, 1 );
248         // and for completely, check again after switching to third sheet and back to the first
249         view.activateSheet( 2 );
250         view.activateSheet( 1 );
251         verifySheetRadios( 1, 0, 0, 1 );
252 
253         cleanupTestStep();
254     }
255 
256     /* ------------------------------------------------------------------ */
after()257     public void after()
258     {
259         closeDocument();
260     }
261 
262     /* ------------------------------------------------------------------ */
263     /** closes our document, if we have an open one
264      */
closeDocument()265     private void closeDocument()
266     {
267         try
268         {
269             // close our document
270             if ( m_document != null )
271             {
272                 XCloseable closeDoc = (XCloseable)UnoRuntime.queryInterface( XCloseable.class,
273                     m_document.getDocument() );
274                 closeDoc.close( true );
275             }
276         }
277         catch ( com.sun.star.uno.Exception e )
278         {
279         }
280     }
281 
282     /* ------------------------------------------------------------------ */
prepareTestStep( boolean useSpreadsheetDocument )283     private void prepareTestStep( boolean useSpreadsheetDocument ) throws com.sun.star.uno.Exception, java.lang.Exception
284     {
285         m_primaryForm = null;
286 
287         m_document = useSpreadsheetDocument ? new SpreadsheetDocument( m_orb ) : DocumentHelper.blankTextDocument( m_orb );
288         m_formLayer = new FormLayer( m_document );
289     }
290 
291     /* ------------------------------------------------------------------ */
cleanupTestStep( )292     private void cleanupTestStep( )
293     {
294         closeDocument();
295     }
296 
297     /* ------------------------------------------------------------------ */
298     /** checks or unchecks the radio button (in our primary form) with the given name and the given ref value
299      */
checkRadio( String groupName, String refValue )300     private void checkRadio( String groupName, String refValue ) throws com.sun.star.uno.Exception, java.lang.Exception
301     {
302         checkRadio( groupName, refValue, m_primaryForm );
303     }
304 
305     /* ------------------------------------------------------------------ */
306     /** checks or unchecks the radio button with the given name and the given ref value
307      */
checkRadio( String groupName, String refValue, XPropertySet form )308     private void checkRadio( String groupName, String refValue, XPropertySet form ) throws com.sun.star.uno.Exception, java.lang.Exception
309     {
310         XPropertySet xRadio = getRadioModel( groupName, refValue, form );
311 
312 		XRadioButton radioButton = (XRadioButton)UnoRuntime.queryInterface(
313 			XRadioButton.class, m_document.getCurrentView().getControl( xRadio ) );
314 		radioButton.setState( true );
315     }
316 
317     /* ------------------------------------------------------------------ */
getRadioModel( String name, String refValue )318     private XPropertySet getRadioModel( String name, String refValue ) throws com.sun.star.uno.Exception, java.lang.Exception
319     {
320         return getRadioModel( name, refValue, m_primaryForm );
321     }
322 
323     /* ------------------------------------------------------------------ */
getRadioModel( String name, String refValue, XPropertySet form )324     private XPropertySet getRadioModel( String name, String refValue, XPropertySet form ) throws com.sun.star.uno.Exception, java.lang.Exception
325     {
326         return m_formLayer.getRadioModelByRefValue( form, name, refValue );
327     }
328 
329     /* ------------------------------------------------------------------ */
stateString( short[] states )330     private String stateString( short[] states )
331     {
332 		StringBuffer buf = new StringBuffer();
333 		for ( int i=0; i<states.length; ++i )
334 			buf.append( states[i] );
335 		return buf.toString();
336 	}
337 
338     /* ------------------------------------------------------------------ */
339     /** verifies a number of radio buttons for their states
340      */
verifyRadios( XPropertySet[] radios, short[] expectedStates, String errorMessage )341     private boolean verifyRadios( XPropertySet[] radios, short[] expectedStates, String errorMessage ) throws com.sun.star.uno.Exception
342     {
343 		short[] actualStates = new short[radios.length];
344 
345 		// collect all current states. This is just to be able to emit them, in case of a failure
346         for ( int i = 0; i<radios.length; ++i )
347         {
348 			actualStates[i] = ((Short)radios[i].getPropertyValue( "State" )).shortValue();
349         }
350 
351 		// now actually check the states
352         for ( int i = 0; i<radios.length; ++i )
353         {
354 			if ( actualStates[i] != expectedStates[i] )
355 			{
356 				failed( errorMessage + " (expected: " + stateString( expectedStates ) + ", found: " + stateString( actualStates ) + ")" );
357 				return false;
358 			}
359 		}
360 
361         return true;
362     }
363 
364     /* ------------------------------------------------------------------ */
365     /** verifies the states of the 4 radio buttons from the checkSingleButtons test
366      */
verifySingleRadios( int state1, int state2, int state3, int state4 )367     private boolean verifySingleRadios( int state1, int state2, int state3, int state4 ) throws com.sun.star.uno.Exception, java.lang.Exception
368     {
369         XPropertySet[] radios = new XPropertySet[4];
370         radios[0] = getRadioModel( "group 1", "" );
371         radios[1] = getRadioModel( "group 2", "" );
372         radios[2] = getRadioModel( "group 3", "" );
373         radios[3] = getRadioModel( "group 4", "" );
374 
375         short[] states = new short[4];
376         states[0] = (short)state1;
377         states[1] = (short)state2;
378         states[2] = (short)state3;
379         states[3] = (short)state4;
380 
381         return verifyRadios( radios, states, "single-group radio buttons do not work!" );
382     }
383 
384     /* ------------------------------------------------------------------ */
385     /** verifies the states of 6 radio buttons form the checkThreeGroups test
386      */
verifySixPack( XPropertySet[] radios, String errorMessage, int state1, int state2, int state3, int state4, int state5, int state6 )387     private boolean verifySixPack( XPropertySet[] radios, String errorMessage,
388         int state1, int state2, int state3, int state4, int state5, int state6 ) throws com.sun.star.uno.Exception, java.lang.Exception
389     {
390         short[] states = new short[6];
391         states[0] = (short)state1;
392         states[1] = (short)state2;
393         states[2] = (short)state3;
394         states[3] = (short)state4;
395         states[4] = (short)state5;
396         states[5] = (short)state6;
397 
398         return verifyRadios( radios, states, errorMessage );
399     }
400 
401     /* ------------------------------------------------------------------ */
402     /** verifies the states of 6 radio buttons
403      */
verifySixPack( int state1, int state2, int state3, int state4, int state5, int state6 )404     private boolean verifySixPack( int state1, int state2, int state3, int state4, int state5, int state6 ) throws com.sun.star.uno.Exception, java.lang.Exception
405     {
406         XPropertySet[] radios = new XPropertySet[6];
407         radios[0] = getRadioModel( "group 1", "a" );
408         radios[1] = getRadioModel( "group 1", "b" );
409         radios[2] = getRadioModel( "group 2", "a" );
410         radios[3] = getRadioModel( "group 2", "b" );
411         radios[4] = getRadioModel( "group 3", "a" );
412         radios[5] = getRadioModel( "group 3", "b" );
413 
414         return verifySixPack( radios, "six radio buttons, forming three different groups, do not properly work!",
415             state1, state2, state3, state4, state5, state6 );
416     }
417 
418     /* ------------------------------------------------------------------ */
419     /** verifies the states of the 6 radio buttons in our checkMultipleForms test
420      */
verifyTwoFormRadios( int state1, int state2, int state3, int state4, int state5, int state6 )421     private boolean verifyTwoFormRadios( int state1, int state2, int state3, int state4, int state5, int state6 ) throws com.sun.star.uno.Exception, java.lang.Exception
422     {
423         XPropertySet[] radios = new XPropertySet[6];
424         radios[0] = getRadioModel( "group 1", "a", m_primaryForm );
425         radios[1] = getRadioModel( "group 1", "b", m_primaryForm );
426         radios[2] = getRadioModel( "group 1", "c", m_primaryForm );
427         radios[3] = getRadioModel( "group 2", "a", m_secondaryForm );
428         radios[4] = getRadioModel( "group 2", "b", m_secondaryForm );
429         radios[5] = getRadioModel( "group 2", "c", m_secondaryForm );
430 
431         return verifySixPack( radios, "radio buttons on different forms do not work properly!",
432             state1, state2, state3, state4, state5, state6 );
433     }
434 
435     /* ------------------------------------------------------------------ */
436     /** verifies the states of the 4 radio buttons in our spreadsheet document (checkCalcPageSwitch)
437      */
verifySheetRadios( int state1, int state2, int state3, int state4 )438     private boolean verifySheetRadios( int state1, int state2, int state3, int state4 ) throws com.sun.star.uno.Exception, java.lang.Exception
439     {
440         XPropertySet[] radios = new XPropertySet[4];
441         radios[0] = getRadioModel( "group 1", "a", m_primaryForm );
442         radios[1] = getRadioModel( "group 1", "b", m_primaryForm );
443         radios[2] = getRadioModel( "group 2", "a", m_secondaryForm );
444         radios[3] = getRadioModel( "group 2", "b", m_secondaryForm );
445 
446         short[] states = new short[4];
447         states[0] = (short)state1;
448         states[1] = (short)state2;
449         states[2] = (short)state3;
450         states[3] = (short)state4;
451 
452         return verifyRadios( radios, states, "seems some of the radio button check states didn't survive the page activation(s)!" );
453     }
454 }
455 
456