1 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 47 public class ToggleButtonTest extends TestCase { 48 49 53 private static class ChangeHandler 54 implements ChangeListener { 55 56 int eventCount = 0; 57 ChangeEvent lastEvent; 58 59 62 public void stateChanged(ChangeEvent e) { 63 lastEvent = e; 64 ++eventCount; 65 } 66 } 67 68 71 public void testButtonGroup() { 72 RadioButton radioButton1 = new RadioButton(); 73 RadioButton radioButton2 = new RadioButton(); 74 RadioButton radioButton3 = new RadioButton(); 75 76 assertFalse(radioButton1.isSelected()); 78 radioButton1.setSelected(true); 79 assertTrue(radioButton1.isSelected()); 80 radioButton1.setSelected(false); 81 assertFalse(radioButton1.isSelected()); 82 83 ButtonGroup buttonGroup = new ButtonGroup(); 85 radioButton1.setGroup(buttonGroup); 86 radioButton2.setGroup(buttonGroup); 87 88 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 radioButton3.setSelected(true); 100 assertTrue(radioButton1.isSelected()); 101 assertFalse(radioButton2.isSelected()); 102 assertTrue(radioButton3.isSelected()); 103 104 radioButton3.setGroup(buttonGroup); 106 assertFalse(radioButton1.isSelected()); 107 assertFalse(radioButton2.isSelected()); 108 assertTrue(radioButton3.isSelected()); 109 110 radioButton3.setGroup(null); 112 assertFalse(radioButton1.isSelected()); 113 assertFalse(radioButton2.isSelected()); 114 assertTrue(radioButton3.isSelected()); 115 116 radioButton2.setSelected(true); 118 assertFalse(radioButton1.isSelected()); 119 assertTrue(radioButton2.isSelected()); 120 assertTrue(radioButton3.isSelected()); 121 122 ((ToggleButtonModel) radioButton1.getModel()).setSelected(true); 124 assertTrue(radioButton1.isSelected()); 125 assertFalse(radioButton2.isSelected()); 126 } 127 128 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 162 public void testDefaults() { 163 CheckBox checkBox = new CheckBox(); 164 assertFalse(checkBox.isSelected()); 165 } 166 167 171 public void testInvalidModelException() { 172 CheckBox checkBox = new CheckBox(); 173 try { 174 checkBox.setModel(new DefaultButtonModel()); 175 fail(); 176 } catch (IllegalArgumentException ex) { 177 } 179 } 180 181 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 |