1 29 30 package nextapp.echo2.app.test; 31 32 import junit.framework.TestCase; 33 34 import nextapp.echo2.app.Alignment; 35 import nextapp.echo2.app.Button; 36 import nextapp.echo2.app.Color; 37 import nextapp.echo2.app.IllegalChildException; 38 import nextapp.echo2.app.Label; 39 import nextapp.echo2.app.button.ButtonModel; 40 import nextapp.echo2.app.event.ActionEvent; 41 import nextapp.echo2.app.event.ActionListener; 42 43 47 public class ButtonTest extends TestCase { 48 49 53 private static class ActionHandler 54 implements ActionListener { 55 56 int eventCount = 0; 57 ActionEvent lastEvent; 58 59 62 public void actionPerformed(ActionEvent e) { 63 lastEvent = e; 64 ++eventCount; 65 } 66 } 67 68 71 public void testActionListener() { 72 ActionHandler buttonActionListener = new ActionHandler(); 73 ActionHandler modelActionListener = new ActionHandler(); 74 Button button = new Button("Test"); 75 ButtonModel model = button.getModel(); 76 button.addActionListener(buttonActionListener); 77 model.addActionListener(modelActionListener); 78 assertEquals(0, buttonActionListener.eventCount); 79 assertEquals(0, modelActionListener.eventCount); 80 button.doAction(); 81 assertEquals(1, buttonActionListener.eventCount); 82 assertEquals(1, modelActionListener.eventCount); 83 assertEquals(button, buttonActionListener.lastEvent.getSource()); 84 assertEquals(model, modelActionListener.lastEvent.getSource()); 85 86 buttonActionListener.lastEvent = null; 87 modelActionListener.lastEvent = null; 88 assertEquals(null, buttonActionListener.lastEvent); 89 assertEquals(null, modelActionListener.lastEvent); 90 91 model.doAction(); 92 93 assertEquals(2, buttonActionListener.eventCount); 94 assertEquals(2, modelActionListener.eventCount); 95 assertEquals(button, buttonActionListener.lastEvent.getSource()); 96 assertEquals(model, modelActionListener.lastEvent.getSource()); 97 } 98 99 102 public void testDefaults() { 103 Button button = new Button(); 104 assertTrue(button.isLineWrap()); 105 assertFalse(button.isPressedEnabled()); 106 assertFalse(button.isRolloverEnabled()); 107 assertNull(button.getActionCommand()); 108 } 109 110 113 public void testIllegalChildren() { 114 Button button = new Button(); 115 boolean exceptionThrown = false; 116 try { 117 button.add(new Label("you can't add children to this component, right?")); 118 } catch (IllegalChildException ex) { 119 exceptionThrown = true; 120 } 121 assertTrue(exceptionThrown); 122 } 123 124 127 public void testNullModelException() { 128 boolean exceptionThrown = false; 129 Button button = new Button(); 130 try { 131 button.setModel(null); 132 } catch (IllegalArgumentException ex) { 133 exceptionThrown = true; 134 } 135 assertTrue(exceptionThrown); 136 } 137 138 141 public void testPressedProperties() { 142 Button button = new Button(); 143 144 button.setPressedBackground(Color.GREEN); 145 button.setPressedBackgroundImage(TestConstants.BACKGROUND_IMAGE); 146 button.setPressedBorder(TestConstants.BORDER_THICK_ORANGE); 147 button.setPressedEnabled(true); 148 button.setPressedFont(TestConstants.TIMES_72); 149 button.setPressedForeground(Color.YELLOW); 150 button.setPressedIcon(TestConstants.PRESSED_ICON); 151 152 assertEquals(Color.GREEN, button.getPressedBackground()); 153 assertEquals(TestConstants.BACKGROUND_IMAGE, button.getPressedBackgroundImage()); 154 assertEquals(TestConstants.BORDER_THICK_ORANGE, button.getPressedBorder()); 155 assertTrue(button.isPressedEnabled()); 156 assertEquals(TestConstants.TIMES_72, button.getPressedFont()); 157 assertEquals(Color.YELLOW, button.getPressedForeground()); 158 assertEquals(TestConstants.PRESSED_ICON, button.getPressedIcon()); 159 } 160 161 164 public void testProperties() { 165 Button button = new Button(); 166 167 button.setText("Alpha"); 168 assertEquals("Alpha", button.getText()); 169 170 button.setBackgroundImage(TestConstants.BACKGROUND_IMAGE); 171 assertEquals(TestConstants.BACKGROUND_IMAGE, button.getBackgroundImage()); 172 173 button.setIcon(TestConstants.ICON); 174 assertEquals(TestConstants.ICON, button.getIcon()); 175 button.setIconTextMargin(TestConstants.EXTENT_100_PX); 176 assertEquals(TestConstants.EXTENT_100_PX, button.getIconTextMargin()); 177 178 button.setBorder(TestConstants.BORDER_THIN_YELLOW); 179 assertEquals(TestConstants.BORDER_THIN_YELLOW, button.getBorder()); 180 181 button.setHeight(TestConstants.EXTENT_500_PX); 182 assertEquals(TestConstants.EXTENT_500_PX, button.getHeight()); 183 button.setWidth(TestConstants.EXTENT_200_PX); 184 assertEquals(TestConstants.EXTENT_200_PX, button.getWidth()); 185 186 button.setTextAlignment(new Alignment(Alignment.LEADING, Alignment.BOTTOM)); 187 assertEquals(new Alignment(Alignment.LEADING, Alignment.BOTTOM), button.getTextAlignment()); 188 189 button.setTextPosition(new Alignment(Alignment.DEFAULT, Alignment.TOP)); 190 assertEquals(new Alignment(Alignment.DEFAULT, Alignment.TOP), button.getTextPosition()); 191 } 192 193 196 public void testRolloverProperties() { 197 Button button = new Button(); 198 199 button.setRolloverEnabled(true); 200 button.setRolloverBackgroundImage(TestConstants.BACKGROUND_IMAGE); 201 button.setRolloverBackground(Color.RED); 202 button.setRolloverForeground(Color.BLUE); 203 button.setRolloverFont(TestConstants.MONOSPACE_12); 204 button.setRolloverBorder(TestConstants.BORDER_THIN_YELLOW); 205 button.setRolloverIcon(TestConstants.ROLLOVER_ICON); 206 207 assertTrue(button.isRolloverEnabled()); 208 assertEquals(TestConstants.BACKGROUND_IMAGE, button.getRolloverBackgroundImage()); 209 assertEquals(Color.RED, button.getRolloverBackground()); 210 assertEquals(Color.BLUE, button.getRolloverForeground()); 211 assertEquals(TestConstants.MONOSPACE_12, button.getRolloverFont()); 212 assertEquals(TestConstants.BORDER_THIN_YELLOW, button.getRolloverBorder()); 213 assertEquals(TestConstants.ROLLOVER_ICON, button.getRolloverIcon()); 214 215 button.setRolloverEnabled(false); 216 assertFalse(button.isRolloverEnabled()); 217 } 218 } 219 | Popular Tags |