KickJava   Java API By Example, From Geeks To Geeks.

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


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
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 /**
44  * Unit tests for the <code>nextapp.echo2.app.button.AbstractButton</code>-based
45  * components.
46  */

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

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

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

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     /**
100      * Test default button values.
101      */

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     /**
111      * Attempt to illegally add children, test for failure.
112      */

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     /**
125      * Ensure setting model to null fails.
126      */

127     public void testNullModelException() {
128         boolean exceptionThrown = false;
129         Button button = new Button();
130         try {
131             button.setModel(null);
132         } catch (IllegalArgumentException JavaDoc ex) {
133             exceptionThrown = true;
134         }
135         assertTrue(exceptionThrown);
136     }
137     
138     /**
139      * Test pressed-state-related property accessors and mutators.
140      */

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     /**
162      * Test property accessors and mutators.
163      */

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     /**
194      * Test rollover-state-related property accessors and mutators.
195      */

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