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:
034: import nextapp.echo2.app.Alignment;
035: import nextapp.echo2.app.Button;
036: import nextapp.echo2.app.Color;
037: import nextapp.echo2.app.IllegalChildException;
038: import nextapp.echo2.app.Label;
039: import nextapp.echo2.app.button.ButtonModel;
040: import nextapp.echo2.app.event.ActionEvent;
041: import nextapp.echo2.app.event.ActionListener;
042:
043: /**
044: * Unit tests for the <code>nextapp.echo2.app.button.AbstractButton</code>-based
045: * components.
046: */
047: public class ButtonTest extends TestCase {
048:
049: /**
050: * <code>ActionListener</code> that retains last fired event and counts
051: * events received.
052: */
053: private static class ActionHandler implements ActionListener {
054:
055: int eventCount = 0;
056: ActionEvent lastEvent;
057:
058: /**
059: * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
060: */
061: public void actionPerformed(ActionEvent e) {
062: lastEvent = e;
063: ++eventCount;
064: }
065: }
066:
067: /**
068: * Test behavior of <code>ActionListener</code>s.
069: */
070: public void testActionListener() {
071: ActionHandler buttonActionListener = new ActionHandler();
072: ActionHandler modelActionListener = new ActionHandler();
073: Button button = new Button("Test");
074: ButtonModel model = button.getModel();
075: button.addActionListener(buttonActionListener);
076: model.addActionListener(modelActionListener);
077: assertEquals(0, buttonActionListener.eventCount);
078: assertEquals(0, modelActionListener.eventCount);
079: button.doAction();
080: assertEquals(1, buttonActionListener.eventCount);
081: assertEquals(1, modelActionListener.eventCount);
082: assertEquals(button, buttonActionListener.lastEvent.getSource());
083: assertEquals(model, modelActionListener.lastEvent.getSource());
084:
085: buttonActionListener.lastEvent = null;
086: modelActionListener.lastEvent = null;
087: assertEquals(null, buttonActionListener.lastEvent);
088: assertEquals(null, modelActionListener.lastEvent);
089:
090: model.doAction();
091:
092: assertEquals(2, buttonActionListener.eventCount);
093: assertEquals(2, modelActionListener.eventCount);
094: assertEquals(button, buttonActionListener.lastEvent.getSource());
095: assertEquals(model, modelActionListener.lastEvent.getSource());
096: }
097:
098: /**
099: * Test default button values.
100: */
101: public void testDefaults() {
102: Button button = new Button();
103: assertTrue(button.isLineWrap());
104: assertFalse(button.isPressedEnabled());
105: assertFalse(button.isRolloverEnabled());
106: assertNull(button.getActionCommand());
107: }
108:
109: /**
110: * Attempt to illegally add children, test for failure.
111: */
112: public void testIllegalChildren() {
113: Button button = new Button();
114: boolean exceptionThrown = false;
115: try {
116: button
117: .add(new Label(
118: "you can't add children to this component, right?"));
119: } catch (IllegalChildException ex) {
120: exceptionThrown = true;
121: }
122: assertTrue(exceptionThrown);
123: }
124:
125: /**
126: * Ensure setting model to null fails.
127: */
128: public void testNullModelException() {
129: boolean exceptionThrown = false;
130: Button button = new Button();
131: try {
132: button.setModel(null);
133: } catch (IllegalArgumentException ex) {
134: exceptionThrown = true;
135: }
136: assertTrue(exceptionThrown);
137: }
138:
139: /**
140: * Test pressed-state-related property accessors and mutators.
141: */
142: public void testPressedProperties() {
143: Button button = new Button();
144:
145: button.setPressedBackground(Color.GREEN);
146: button
147: .setPressedBackgroundImage(TestConstants.BACKGROUND_IMAGE);
148: button.setPressedBorder(TestConstants.BORDER_THICK_ORANGE);
149: button.setPressedEnabled(true);
150: button.setPressedFont(TestConstants.TIMES_72);
151: button.setPressedForeground(Color.YELLOW);
152: button.setPressedIcon(TestConstants.PRESSED_ICON);
153:
154: assertEquals(Color.GREEN, button.getPressedBackground());
155: assertEquals(TestConstants.BACKGROUND_IMAGE, button
156: .getPressedBackgroundImage());
157: assertEquals(TestConstants.BORDER_THICK_ORANGE, button
158: .getPressedBorder());
159: assertTrue(button.isPressedEnabled());
160: assertEquals(TestConstants.TIMES_72, button.getPressedFont());
161: assertEquals(Color.YELLOW, button.getPressedForeground());
162: assertEquals(TestConstants.PRESSED_ICON, button
163: .getPressedIcon());
164: }
165:
166: /**
167: * Test property accessors and mutators.
168: */
169: public void testProperties() {
170: Button button = new Button();
171:
172: button.setText("Alpha");
173: assertEquals("Alpha", button.getText());
174:
175: button.setBackgroundImage(TestConstants.BACKGROUND_IMAGE);
176: assertEquals(TestConstants.BACKGROUND_IMAGE, button
177: .getBackgroundImage());
178:
179: button.setIcon(TestConstants.ICON);
180: assertEquals(TestConstants.ICON, button.getIcon());
181: button.setIconTextMargin(TestConstants.EXTENT_100_PX);
182: assertEquals(TestConstants.EXTENT_100_PX, button
183: .getIconTextMargin());
184:
185: button.setBorder(TestConstants.BORDER_THIN_YELLOW);
186: assertEquals(TestConstants.BORDER_THIN_YELLOW, button
187: .getBorder());
188:
189: button.setHeight(TestConstants.EXTENT_500_PX);
190: assertEquals(TestConstants.EXTENT_500_PX, button.getHeight());
191: button.setWidth(TestConstants.EXTENT_200_PX);
192: assertEquals(TestConstants.EXTENT_200_PX, button.getWidth());
193:
194: button.setTextAlignment(new Alignment(Alignment.LEADING,
195: Alignment.BOTTOM));
196: assertEquals(
197: new Alignment(Alignment.LEADING, Alignment.BOTTOM),
198: button.getTextAlignment());
199:
200: button.setTextPosition(new Alignment(Alignment.DEFAULT,
201: Alignment.TOP));
202: assertEquals(new Alignment(Alignment.DEFAULT, Alignment.TOP),
203: button.getTextPosition());
204: }
205:
206: /**
207: * Test rollover-state-related property accessors and mutators.
208: */
209: public void testRolloverProperties() {
210: Button button = new Button();
211:
212: button.setRolloverEnabled(true);
213: button
214: .setRolloverBackgroundImage(TestConstants.BACKGROUND_IMAGE);
215: button.setRolloverBackground(Color.RED);
216: button.setRolloverForeground(Color.BLUE);
217: button.setRolloverFont(TestConstants.MONOSPACE_12);
218: button.setRolloverBorder(TestConstants.BORDER_THIN_YELLOW);
219: button.setRolloverIcon(TestConstants.ROLLOVER_ICON);
220:
221: assertTrue(button.isRolloverEnabled());
222: assertEquals(TestConstants.BACKGROUND_IMAGE, button
223: .getRolloverBackgroundImage());
224: assertEquals(Color.RED, button.getRolloverBackground());
225: assertEquals(Color.BLUE, button.getRolloverForeground());
226: assertEquals(TestConstants.MONOSPACE_12, button
227: .getRolloverFont());
228: assertEquals(TestConstants.BORDER_THIN_YELLOW, button
229: .getRolloverBorder());
230: assertEquals(TestConstants.ROLLOVER_ICON, button
231: .getRolloverIcon());
232:
233: button.setRolloverEnabled(false);
234: assertFalse(button.isRolloverEnabled());
235: }
236: }
|