summaryrefslogtreecommitdiff
path: root/forms/qa/integration/forms/RadioButtons.java
blob: 3e83af998fef3d5df9bed8401918269ac51528f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
package integration.forms;

import com.sun.star.uno.*;
import com.sun.star.util.*;
import com.sun.star.lang.*;
import com.sun.star.container.*;
import com.sun.star.beans.*;
import com.sun.star.awt.XRadioButton;

import integration.forms.dbfTools;
import integration.forms.DocumentHelper;
import integration.forms.SpreadsheetDocument;

public class RadioButtons extends complexlib.ComplexTestCase
{
    private DocumentHelper          m_document;         /// our current test document
    private FormLayer               m_formLayer;        /// quick access to the form layer
    private XMultiServiceFactory    m_orb;              /// our service factory
    private XPropertySet            m_primaryForm;      /// the primary form, to be used in text documents and in the first page of spreadsheets
    private XPropertySet            m_secondaryForm;    /// the secondary form, to be used in the second page of spreadsheets

    /* ------------------------------------------------------------------ */
    public RadioButtons()
    {
    }

    /* ------------------------------------------------------------------ */
    public String[] getTestMethodNames()
    {
        return new String[] {
            "checkSingleButtons",
            "checkThreeGroups",
            "checkMultipleForms",
            "checkCalcPageSwitch"
        };
    }

    /* ------------------------------------------------------------------ */
    public String getTestObjectName()
    {
        return "Form Radio Buttons Test";
    }

    /* ------------------------------------------------------------------ */
    public void before() throws com.sun.star.uno.Exception, java.lang.Exception
    {
        m_orb = param.getMSF();
    }

    /* ------------------------------------------------------------------ */
    private XPropertySet insertRadio( int nXPos, int nYPos, String label, String name, String refValue ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        return insertRadio( nXPos, nYPos, label, name, refValue, null );
    }

    /* ------------------------------------------------------------------ */
    private XPropertySet insertRadio( int nXPos, int nYPos, String label, String name, String refValue, XPropertySet parentForm ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        XIndexContainer parentContainer = dbfTools.queryIndexContainer( parentForm );
        XPropertySet xRadio = m_formLayer.createControlAndShape( "DatabaseRadioButton", nXPos, nYPos, 25, 6, parentContainer );
        xRadio.setPropertyValue( "Label", label );
        xRadio.setPropertyValue( "RefValue", refValue );
        xRadio.setPropertyValue( "Name", name );

        if ( null == m_primaryForm )
            m_primaryForm = (XPropertySet)dbfTools.getParent( xRadio, XPropertySet.class );

        return xRadio;
    }

    /* ------------------------------------------------------------------ */
    /** this checks whether n groups of radio buttons, consisting of only one button each,
     *  behave properly
     */
    public void checkSingleButtons() throws com.sun.star.uno.Exception, java.lang.Exception
    {
        prepareTestStep( false );

        insertRadio( 20, 30,  "group 1", "group 1", "" );
        insertRadio( 20, 38,  "group 2", "group 2", "" );
        insertRadio( 20, 46,  "group 3", "group 3", "" );
        insertRadio( 20, 54,  "group 4", "group 4", "" );

        // switch to alive mode
        m_document.getCurrentView( ).toggleFormDesignMode( );

        checkRadio( "group 1", "" );
        verifySingleRadios( 1, 0, 0, 0 );

        checkRadio( "group 4", "" );
        verifySingleRadios( 1, 0, 0, 1 );

        checkRadio( "group 2", "" );
        verifySingleRadios( 1, 1, 0, 1 );

        checkRadio( "group 3", "" );
        verifySingleRadios( 1, 1, 1, 1 );

        cleanupTestStep();
    }

    /* ------------------------------------------------------------------ */
    /** creates three groups of radio buttons in a sample document, and checks whether they're working
     */
    public void checkThreeGroups( ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        prepareTestStep( false );

        insertRadio( 20, 30,  "group 1 (a)", "group 1", "a" );
        insertRadio( 20, 38,  "group 1 (b)", "group 1", "b" );

        insertRadio( 20, 50,  "group 2 (a)", "group 2", "a" );
        insertRadio( 20, 58,  "group 2 (b)", "group 2", "b" );

        insertRadio( 20, 70,  "group 3 (a)", "group 3", "a" );
        insertRadio( 20, 78,  "group 3 (b)", "group 3", "b" );

        // switch to alive mode
        m_document.getCurrentView( ).toggleFormDesignMode( );

        // initially, after switching to alive mode, all buttons should be unchecked
        verifySixPack( 0, 0, 0, 0, 0, 0 );

        // check one button in every group
        checkRadio( "group 1", "a" );
        checkRadio( "group 2", "b" );
        checkRadio( "group 3", "a" );
        // and verify that this worked
        verifySixPack( 1, 0, 0, 1, 1, 0 );

        // check all buttons which are currently unchecked
        checkRadio( "group 1", "b" );
        checkRadio( "group 2", "a" );
        checkRadio( "group 3", "b" );
        // this should have reset the previous checks in the respective groups
        verifySixPack( 0, 1, 1, 0, 0, 1 );

        // and back to the previous check state
        checkRadio( "group 1", "a" );
        checkRadio( "group 2", "b" );
        checkRadio( "group 3", "a" );
        verifySixPack( 1, 0, 0, 1, 1, 0 );

        cleanupTestStep();
    }

    /* ------------------------------------------------------------------ */
    /** tests whether radio buttons which belong to different forms behave properly
     */
    public void checkMultipleForms( ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        prepareTestStep( false );

        insertRadio( 20, 30,  "group 1 (a)", "group 1", "a" );
        insertRadio( 20, 38,  "group 1 (b)", "group 1", "b" );
        insertRadio( 20, 46,  "group 1 (c)", "group 1", "c" );

        m_secondaryForm = dbfTools.queryPropertySet( m_document.createSiblingForm( m_primaryForm, "secondary" ) );

        insertRadio( 70, 30,  "group 2 (a)", "group 2", "a", m_secondaryForm );
        insertRadio( 70, 38,  "group 2 (b)", "group 2", "b", m_secondaryForm );
        insertRadio( 70, 46,  "group 2 (c)", "group 2", "c", m_secondaryForm );

        // switch to alive mode
        m_document.getCurrentView( ).toggleFormDesignMode( );

        // play around with different check states
        checkRadio( "group 1", "b", m_primaryForm );
        checkRadio( "group 2", "c", m_secondaryForm );
        verifyTwoFormRadios( 0, 1, 0, 0, 0, 1 );

        checkRadio( "group 1", "c", m_primaryForm );
        verifyTwoFormRadios( 0, 0, 1, 0, 0, 1 );

        checkRadio( "group 2", "a", m_secondaryForm );
        verifyTwoFormRadios( 0, 0, 1, 1, 0, 0 );

        checkRadio( "group 1", "a", m_primaryForm );
        verifyTwoFormRadios( 1, 0, 0, 1, 0, 0 );

        checkRadio( "group 2", "b", m_secondaryForm );
        verifyTwoFormRadios( 1, 0, 0, 0, 1, 0 );

        cleanupTestStep();
    }

    /* ------------------------------------------------------------------ */
    /** tests for a special bug which we once had, where radio buttons lost their state after
     *  switching spreadsheet pages
     */
    public void checkCalcPageSwitch( ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        prepareTestStep( true );

        m_formLayer.setInsertPage( 0 );
        insertRadio( 15, 20,  "group 1 (a)", "group 1", "a" );
        insertRadio( 15, 26,  "group 1 (b)", "group 1", "b" );

        m_formLayer.setInsertPage( 1 );
        XPropertySet xRadio = insertRadio( 15, 20,  "group 2 (a)", "group 2", "a" );
                              insertRadio( 15, 26,  "group 2 (b)", "group 2", "b" );
        m_secondaryForm = (XPropertySet)dbfTools.getParent( xRadio, XPropertySet.class );

        // switch to alive mode
        SpreadsheetView view = (SpreadsheetView)m_document.getCurrentView( );
        view.toggleFormDesignMode( );
        // and do initial checking
        checkRadio( "group 1", "a", m_primaryForm );
        view.activateSheet( 1 );
        checkRadio( "group 2", "b", m_secondaryForm );

        // see whether the check states on the first page survived the page switch
        verifySheetRadios( 1, 0, 0, 1 );
        // switch back to the first sheet, and see whether the check states survived
        view.activateSheet( 0 );
        verifySheetRadios( 1, 0, 0, 1 );
        // and for completely, check again after switching to third sheet and back to the first
        view.activateSheet( 2 );
        view.activateSheet( 1 );
        verifySheetRadios( 1, 0, 0, 1 );

        cleanupTestStep();
    }

    /* ------------------------------------------------------------------ */
    public void after()
    {
        closeDocument();
    }

    /* ------------------------------------------------------------------ */
    /** closes our document, if we have an open one
     */
    private void closeDocument()
    {
        try
        {
            // close our document
            if ( m_document != null )
            {
                XCloseable closeDoc = UnoRuntime.queryInterface( XCloseable.class,
                    m_document.getDocument() );
                closeDoc.close( true );
            }
        }
        catch ( com.sun.star.uno.Exception e )
        {
        }
    }

    /* ------------------------------------------------------------------ */
    private void prepareTestStep( boolean useSpreadsheetDocument ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        m_primaryForm = null;

        m_document = useSpreadsheetDocument ? new SpreadsheetDocument( m_orb ) : DocumentHelper.blankTextDocument( m_orb );
        m_formLayer = new FormLayer( m_document );
    }

    /* ------------------------------------------------------------------ */
    private void cleanupTestStep( )
    {
        closeDocument();
    }

    /* ------------------------------------------------------------------ */
    /** checks or unchecks the radio button (in our primary form) with the given name and the given ref value
     */
    private void checkRadio( String groupName, String refValue ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        checkRadio( groupName, refValue, m_primaryForm );
    }

    /* ------------------------------------------------------------------ */
    /** checks or unchecks the radio button with the given name and the given ref value
     */
    private void checkRadio( String groupName, String refValue, XPropertySet form ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        XPropertySet xRadio = getRadioModel( groupName, refValue, form );

        XRadioButton radioButton = UnoRuntime.queryInterface(
            XRadioButton.class, m_document.getCurrentView().getControl( xRadio ) );
        radioButton.setState( true );
    }

    /* ------------------------------------------------------------------ */
    private XPropertySet getRadioModel( String name, String refValue ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        return getRadioModel( name, refValue, m_primaryForm );
    }

    /* ------------------------------------------------------------------ */
    private XPropertySet getRadioModel( String name, String refValue, XPropertySet form ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        return m_formLayer.getRadioModelByRefValue( form, name, refValue );
    }

    /* ------------------------------------------------------------------ */
    private String stateString( short[] states )
    {
        StringBuffer buf = new StringBuffer();
        for ( int i=0; i<states.length; ++i )
            buf.append( states[i] );
        return buf.toString();
    }

    /* ------------------------------------------------------------------ */
    /** verifies a number of radio buttons for their states
     */
    private boolean verifyRadios( XPropertySet[] radios, short[] expectedStates, String errorMessage ) throws com.sun.star.uno.Exception
    {
        short[] actualStates = new short[radios.length];

        // collect all current states. This is just to be able to emit them, in case of a failure
        for ( int i = 0; i<radios.length; ++i )
        {
            actualStates[i] = ((Short)radios[i].getPropertyValue( "State" )).shortValue();
        }

        // now actually check the states
        for ( int i = 0; i<radios.length; ++i )
        {
            if ( actualStates[i] != expectedStates[i] )
            {
                failed( errorMessage + " (expected: " + stateString( expectedStates ) + ", found: " + stateString( actualStates ) + ")" );
                return false;
            }
        }

        return true;
    }

    /* ------------------------------------------------------------------ */
    /** verifies the states of the 4 radio buttons from the checkSingleButtons test
     */
    private boolean verifySingleRadios( int state1, int state2, int state3, int state4 ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        XPropertySet[] radios = new XPropertySet[4];
        radios[0] = getRadioModel( "group 1", "" );
        radios[1] = getRadioModel( "group 2", "" );
        radios[2] = getRadioModel( "group 3", "" );
        radios[3] = getRadioModel( "group 4", "" );

        short[] states = new short[4];
        states[0] = (short)state1;
        states[1] = (short)state2;
        states[2] = (short)state3;
        states[3] = (short)state4;

        return verifyRadios( radios, states, "single-group radio buttons do not work!" );
    }

    /* ------------------------------------------------------------------ */
    /** verifies the states of 6 radio buttons form the checkThreeGroups test
     */
    private boolean verifySixPack( XPropertySet[] radios, String errorMessage,
        int state1, int state2, int state3, int state4, int state5, int state6 ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        short[] states = new short[6];
        states[0] = (short)state1;
        states[1] = (short)state2;
        states[2] = (short)state3;
        states[3] = (short)state4;
        states[4] = (short)state5;
        states[5] = (short)state6;

        return verifyRadios( radios, states, errorMessage );
    }

    /* ------------------------------------------------------------------ */
    /** verifies the states of 6 radio buttons
     */
    private boolean verifySixPack( int state1, int state2, int state3, int state4, int state5, int state6 ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        XPropertySet[] radios = new XPropertySet[6];
        radios[0] = getRadioModel( "group 1", "a" );
        radios[1] = getRadioModel( "group 1", "b" );
        radios[2] = getRadioModel( "group 2", "a" );
        radios[3] = getRadioModel( "group 2", "b" );
        radios[4] = getRadioModel( "group 3", "a" );
        radios[5] = getRadioModel( "group 3", "b" );

        return verifySixPack( radios, "six radio buttons, forming three different groups, do not properly work!",
            state1, state2, state3, state4, state5, state6 );
    }

    /* ------------------------------------------------------------------ */
    /** verifies the states of the 6 radio buttons in our checkMultipleForms test
     */
    private boolean verifyTwoFormRadios( int state1, int state2, int state3, int state4, int state5, int state6 ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        XPropertySet[] radios = new XPropertySet[6];
        radios[0] = getRadioModel( "group 1", "a", m_primaryForm );
        radios[1] = getRadioModel( "group 1", "b", m_primaryForm );
        radios[2] = getRadioModel( "group 1", "c", m_primaryForm );
        radios[3] = getRadioModel( "group 2", "a", m_secondaryForm );
        radios[4] = getRadioModel( "group 2", "b", m_secondaryForm );
        radios[5] = getRadioModel( "group 2", "c", m_secondaryForm );

        return verifySixPack( radios, "radio buttons on different forms do not work properly!",
            state1, state2, state3, state4, state5, state6 );
    }

    /* ------------------------------------------------------------------ */
    /** verifies the states of the 4 radio buttons in our spreadsheet document (checkCalcPageSwitch)
     */
    private boolean verifySheetRadios( int state1, int state2, int state3, int state4 ) throws com.sun.star.uno.Exception, java.lang.Exception
    {
        XPropertySet[] radios = new XPropertySet[4];
        radios[0] = getRadioModel( "group 1", "a", m_primaryForm );
        radios[1] = getRadioModel( "group 1", "b", m_primaryForm );
        radios[2] = getRadioModel( "group 2", "a", m_secondaryForm );
        radios[3] = getRadioModel( "group 2", "b", m_secondaryForm );

        short[] states = new short[4];
        states[0] = (short)state1;
        states[1] = (short)state2;
        states[2] = (short)state3;
        states[3] = (short)state4;

        return verifyRadios( radios, states, "seems some of the radio button check states didn't survive the page activation(s)!" );
    }
}