001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app.test;
031:
032: import junit.framework.TestCase;
033: import nextapp.echo2.app.Alignment;
034: import nextapp.echo2.app.CheckBox;
035: import nextapp.echo2.app.RadioButton;
036: import nextapp.echo2.app.ResourceImageReference;
037: import nextapp.echo2.app.button.ButtonGroup;
038: import nextapp.echo2.app.button.DefaultButtonModel;
039: import nextapp.echo2.app.button.ToggleButtonModel;
040: import nextapp.echo2.app.event.ChangeEvent;
041: import nextapp.echo2.app.event.ChangeListener;
042:
043: /**
044: * Unit tests for the <code>nextapp.echo2.app.button.ToggleButton</code>-based
045: * components.
046: */
047: public class ToggleButtonTest extends TestCase {
048:
049: /**
050: * <code>ChangeListener</code> that retains last fired event and counts
051: * events received.
052: */
053: private static class ChangeHandler implements ChangeListener {
054:
055: int eventCount = 0;
056: ChangeEvent lastEvent;
057:
058: /**
059: * @see nextapp.echo2.app.event.ChangeListener#stateChanged(nextapp.echo2.app.event.ChangeEvent)
060: */
061: public void stateChanged(ChangeEvent e) {
062: lastEvent = e;
063: ++eventCount;
064: }
065: }
066:
067: /**
068: * Test behavior of <code>ButtonGroup</code>s.
069: */
070: public void testButtonGroup() {
071: RadioButton radioButton1 = new RadioButton();
072: RadioButton radioButton2 = new RadioButton();
073: RadioButton radioButton3 = new RadioButton();
074:
075: // Test selection state of single button prior to setting group.
076: assertFalse(radioButton1.isSelected());
077: radioButton1.setSelected(true);
078: assertTrue(radioButton1.isSelected());
079: radioButton1.setSelected(false);
080: assertFalse(radioButton1.isSelected());
081:
082: // Add Buttons to group.
083: ButtonGroup buttonGroup = new ButtonGroup();
084: radioButton1.setGroup(buttonGroup);
085: radioButton2.setGroup(buttonGroup);
086:
087: // Ensure mutual exclusivity between buttons in button group.
088: radioButton1.setSelected(true);
089: assertTrue(radioButton1.isSelected());
090: radioButton2.setSelected(true);
091: assertTrue(radioButton2.isSelected());
092: assertFalse(radioButton1.isSelected());
093: radioButton1.setSelected(true);
094: assertTrue(radioButton1.isSelected());
095: assertFalse(radioButton2.isSelected());
096:
097: // Create selected button independent of button group (no effect).
098: radioButton3.setSelected(true);
099: assertTrue(radioButton1.isSelected());
100: assertFalse(radioButton2.isSelected());
101: assertTrue(radioButton3.isSelected());
102:
103: // Add selected button to button group: ensure new selected button becomes group selection.
104: radioButton3.setGroup(buttonGroup);
105: assertFalse(radioButton1.isSelected());
106: assertFalse(radioButton2.isSelected());
107: assertTrue(radioButton3.isSelected());
108:
109: // Remove selected button from button group: ensure no effect other than group removal.
110: radioButton3.setGroup(null);
111: assertFalse(radioButton1.isSelected());
112: assertFalse(radioButton2.isSelected());
113: assertTrue(radioButton3.isSelected());
114:
115: // Select new button in button group: ensure no effect on button that was removed.
116: radioButton2.setSelected(true);
117: assertFalse(radioButton1.isSelected());
118: assertTrue(radioButton2.isSelected());
119: assertTrue(radioButton3.isSelected());
120:
121: // Set model state of button in group to selected: ensure button is selected in group.
122: ((ToggleButtonModel) radioButton1.getModel()).setSelected(true);
123: assertTrue(radioButton1.isSelected());
124: assertFalse(radioButton2.isSelected());
125: }
126:
127: /**
128: * Test behavior of <code>ChangeListener</code>s.
129: */
130: public void testChangeListener() {
131: ChangeHandler buttonChangeListener = new ChangeHandler();
132: ChangeHandler modelChangeListener = new ChangeHandler();
133: CheckBox checkBox = new CheckBox("Test");
134: ToggleButtonModel model = (ToggleButtonModel) checkBox
135: .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
144: .getSource());
145: assertEquals(model, modelChangeListener.lastEvent.getSource());
146:
147: buttonChangeListener.lastEvent = null;
148: modelChangeListener.lastEvent = null;
149: assertEquals(null, buttonChangeListener.lastEvent);
150: assertEquals(null, modelChangeListener.lastEvent);
151:
152: model.setSelected(false);
153:
154: assertEquals(2, buttonChangeListener.eventCount);
155: assertEquals(2, modelChangeListener.eventCount);
156: assertEquals(checkBox, buttonChangeListener.lastEvent
157: .getSource());
158: assertEquals(model, modelChangeListener.lastEvent.getSource());
159: }
160:
161: /**
162: * Test default property values.
163: */
164: public void testDefaults() {
165: CheckBox checkBox = new CheckBox();
166: assertFalse(checkBox.isSelected());
167: }
168:
169: /**
170: * Ensure that <code>ToggleButtonModel</code> requirement is being
171: * enforced.
172: */
173: public void testInvalidModelException() {
174: CheckBox checkBox = new CheckBox();
175: try {
176: checkBox.setModel(new DefaultButtonModel());
177: fail();
178: } catch (IllegalArgumentException ex) {
179: // Expected.
180: }
181: }
182:
183: /**
184: * Test property accessors and mutators.
185: */
186: public void testProperties() {
187: CheckBox checkBox = new CheckBox();
188:
189: checkBox.setSelectedStateIcon(new ResourceImageReference(
190: "SelectedState.png"));
191: checkBox.setStateIcon(new ResourceImageReference("State.png"));
192: checkBox
193: .setPressedSelectedStateIcon(new ResourceImageReference(
194: "PressedSelectedState.png"));
195: checkBox.setPressedStateIcon(new ResourceImageReference(
196: "PressedState.png"));
197: checkBox
198: .setRolloverSelectedStateIcon(new ResourceImageReference(
199: "RolloverSelectedState.png"));
200: checkBox.setRolloverStateIcon(new ResourceImageReference(
201: "RolloverState.png"));
202: checkBox.setStateAlignment(new Alignment(Alignment.RIGHT,
203: Alignment.BOTTOM));
204: checkBox.setStatePosition(new Alignment(Alignment.TRAILING,
205: Alignment.DEFAULT));
206: checkBox.setStateMargin(TestConstants.EXTENT_100_PX);
207:
208: assertEquals(new ResourceImageReference("SelectedState.png"),
209: checkBox.getSelectedStateIcon());
210: assertEquals(new ResourceImageReference("State.png"), checkBox
211: .getStateIcon());
212: assertEquals(new ResourceImageReference(
213: "PressedSelectedState.png"), checkBox
214: .getPressedSelectedStateIcon());
215: assertEquals(new ResourceImageReference("PressedState.png"),
216: checkBox.getPressedStateIcon());
217: assertEquals(new ResourceImageReference(
218: "RolloverSelectedState.png"), checkBox
219: .getRolloverSelectedStateIcon());
220: assertEquals(new ResourceImageReference("RolloverState.png"),
221: checkBox.getRolloverStateIcon());
222: assertEquals(new Alignment(Alignment.RIGHT, Alignment.BOTTOM),
223: checkBox.getStateAlignment());
224: assertEquals(new Alignment(Alignment.TRAILING,
225: Alignment.DEFAULT), checkBox.getStatePosition());
226: assertEquals(TestConstants.EXTENT_100_PX, checkBox
227: .getStateMargin());
228:
229: checkBox.setSelected(true);
230: assertTrue(checkBox.isSelected());
231: checkBox.setSelected(false);
232: assertFalse(checkBox.isSelected());
233: }
234: }
|