KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > test > ToggleButtonTest


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app.test;
31
32 import junit.framework.TestCase;
33 import nextapp.echo2.app.Alignment;
34 import nextapp.echo2.app.CheckBox;
35 import nextapp.echo2.app.RadioButton;
36 import nextapp.echo2.app.ResourceImageReference;
37 import nextapp.echo2.app.button.ButtonGroup;
38 import nextapp.echo2.app.button.DefaultButtonModel;
39 import nextapp.echo2.app.button.ToggleButtonModel;
40 import nextapp.echo2.app.event.ChangeEvent;
41 import nextapp.echo2.app.event.ChangeListener;
42
43 /**
44  * Unit tests for the <code>nextapp.echo2.app.button.ToggleButton</code>-based
45  * components.
46  */

47 public class ToggleButtonTest extends TestCase {
48     
49     /**
50      * <code>ChangeListener</code> that retains last fired event and counts
51      * events received.
52      */

53     private static class ChangeHandler
54     implements ChangeListener {
55         
56         int eventCount = 0;
57         ChangeEvent lastEvent;
58         
59         /**
60          * @see nextapp.echo2.app.event.ChangeListener#stateChanged(nextapp.echo2.app.event.ChangeEvent)
61          */

62         public void stateChanged(ChangeEvent e) {
63             lastEvent = e;
64             ++eventCount;
65         }
66     }
67     
68     /**
69      * Test behavior of <code>ButtonGroup</code>s.
70      */

71     public void testButtonGroup() {
72         RadioButton radioButton1 = new RadioButton();
73         RadioButton radioButton2 = new RadioButton();
74         RadioButton radioButton3 = new RadioButton();
75         
76         // Test selection state of single button prior to setting group.
77
assertFalse(radioButton1.isSelected());
78         radioButton1.setSelected(true);
79         assertTrue(radioButton1.isSelected());
80         radioButton1.setSelected(false);
81         assertFalse(radioButton1.isSelected());
82         
83         // Add Buttons to group.
84
ButtonGroup buttonGroup = new ButtonGroup();
85         radioButton1.setGroup(buttonGroup);
86         radioButton2.setGroup(buttonGroup);
87         
88         // Ensure mutual exclusivity between buttons in button group.
89
radioButton1.setSelected(true);
90         assertTrue(radioButton1.isSelected());
91         radioButton2.setSelected(true);
92         assertTrue(radioButton2.isSelected());
93         assertFalse(radioButton1.isSelected());
94         radioButton1.setSelected(true);
95         assertTrue(radioButton1.isSelected());
96         assertFalse(radioButton2.isSelected());
97         
98         // Create selected button independent of button group (no effect).
99
radioButton3.setSelected(true);
100         assertTrue(radioButton1.isSelected());
101         assertFalse(radioButton2.isSelected());
102         assertTrue(radioButton3.isSelected());
103         
104         // Add selected button to button group: ensure new selected button becomes group selection.
105
radioButton3.setGroup(buttonGroup);
106         assertFalse(radioButton1.isSelected());
107         assertFalse(radioButton2.isSelected());
108         assertTrue(radioButton3.isSelected());
109         
110         // Remove selected button from button group: ensure no effect other than group removal.
111
radioButton3.setGroup(null);
112         assertFalse(radioButton1.isSelected());
113         assertFalse(radioButton2.isSelected());
114         assertTrue(radioButton3.isSelected());
115
116         // Select new button in button group: ensure no effect on button that was removed.
117
radioButton2.setSelected(true);
118         assertFalse(radioButton1.isSelected());
119         assertTrue(radioButton2.isSelected());
120         assertTrue(radioButton3.isSelected());
121         
122         // Set model state of button in group to selected: ensure button is selected in group.
123
((ToggleButtonModel) radioButton1.getModel()).setSelected(true);
124         assertTrue(radioButton1.isSelected());
125         assertFalse(radioButton2.isSelected());
126     }
127     
128     /**
129      * Test behavior of <code>ChangeListener</code>s.
130      */

131     public void testChangeListener() {
132         ChangeHandler buttonChangeListener = new ChangeHandler();
133         ChangeHandler modelChangeListener = new ChangeHandler();
134         CheckBox checkBox = new CheckBox("Test");
135         ToggleButtonModel model = (ToggleButtonModel) checkBox.getModel();
136         checkBox.addChangeListener(buttonChangeListener);
137         model.addChangeListener(modelChangeListener);
138         assertEquals(0, buttonChangeListener.eventCount);
139         assertEquals(0, modelChangeListener.eventCount);
140         checkBox.setSelected(true);
141         assertEquals(1, buttonChangeListener.eventCount);
142         assertEquals(1, modelChangeListener.eventCount);
143         assertEquals(checkBox, buttonChangeListener.lastEvent.getSource());
144         assertEquals(model, modelChangeListener.lastEvent.getSource());
145
146         buttonChangeListener.lastEvent = null;
147         modelChangeListener.lastEvent = null;
148         assertEquals(null, buttonChangeListener.lastEvent);
149         assertEquals(null, modelChangeListener.lastEvent);
150
151         model.setSelected(false);
152         
153         assertEquals(2, buttonChangeListener.eventCount);
154         assertEquals(2, modelChangeListener.eventCount);
155         assertEquals(checkBox, buttonChangeListener.lastEvent.getSource());
156         assertEquals(model, modelChangeListener.lastEvent.getSource());
157     }
158     
159     /**
160      * Test default property values.
161      */

162     public void testDefaults() {
163         CheckBox checkBox = new CheckBox();
164         assertFalse(checkBox.isSelected());
165     }
166
167     /**
168      * Ensure that <code>ToggleButtonModel</code> requirement is being
169      * enforced.
170      */

171     public void testInvalidModelException() {
172         CheckBox checkBox = new CheckBox();
173         try {
174             checkBox.setModel(new DefaultButtonModel());
175             fail();
176         } catch (IllegalArgumentException JavaDoc ex) {
177             // Expected.
178
}
179     }
180     
181     /**
182      * Test property accessors and mutators.
183      */

184     public void testProperties() {
185         CheckBox checkBox = new CheckBox();
186         
187         checkBox.setSelectedStateIcon(new ResourceImageReference("SelectedState.png"));
188         checkBox.setStateIcon(new ResourceImageReference("State.png"));
189         checkBox.setPressedSelectedStateIcon(new ResourceImageReference("PressedSelectedState.png"));
190         checkBox.setPressedStateIcon(new ResourceImageReference("PressedState.png"));
191         checkBox.setRolloverSelectedStateIcon(new ResourceImageReference("RolloverSelectedState.png"));
192         checkBox.setRolloverStateIcon(new ResourceImageReference("RolloverState.png"));
193         checkBox.setStateAlignment(new Alignment(Alignment.RIGHT, Alignment.BOTTOM));
194         checkBox.setStatePosition(new Alignment(Alignment.TRAILING, Alignment.DEFAULT));
195         checkBox.setStateMargin(TestConstants.EXTENT_100_PX);
196         
197         assertEquals(new ResourceImageReference("SelectedState.png"), checkBox.getSelectedStateIcon());
198         assertEquals(new ResourceImageReference("State.png"), checkBox.getStateIcon());
199         assertEquals(new ResourceImageReference("PressedSelectedState.png"), checkBox.getPressedSelectedStateIcon());
200         assertEquals(new ResourceImageReference("PressedState.png"), checkBox.getPressedStateIcon());
201         assertEquals(new ResourceImageReference("RolloverSelectedState.png"), checkBox.getRolloverSelectedStateIcon());
202         assertEquals(new ResourceImageReference("RolloverState.png"), checkBox.getRolloverStateIcon());
203         assertEquals(new Alignment(Alignment.RIGHT, Alignment.BOTTOM), checkBox.getStateAlignment());
204         assertEquals(new Alignment(Alignment.TRAILING, Alignment.DEFAULT), checkBox.getStatePosition());
205         assertEquals(TestConstants.EXTENT_100_PX, checkBox.getStateMargin());
206         
207         checkBox.setSelected(true);
208         assertTrue(checkBox.isSelected());
209         checkBox.setSelected(false);
210         assertFalse(checkBox.isSelected());
211     }
212 }
213
Popular Tags