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