001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: * Created on 01.02.2005
021:
022: */package javax.swing;
023:
024: import java.awt.event.ActionEvent;
025: import javax.accessibility.AccessibleRole;
026: import javax.swing.plaf.ButtonUI;
027:
028: public class JButtonTest extends AbstractButtonTest {
029: protected JButton jbutton;
030:
031: /*
032: * @see TestCase#setUp()
033: */
034: @Override
035: protected void setUp() throws Exception {
036: super .setUp();
037: jbutton = new JButton();
038: button = jbutton;
039: }
040:
041: /*
042: * @see TestCase#tearDown()
043: */
044: @Override
045: protected void tearDown() throws Exception {
046: super .tearDown();
047: }
048:
049: /*
050: * Class under test for void JButton(String, Icon)
051: */
052: public void testJButtonStringIcon() {
053: Icon icon = createNewIcon();
054: String text = "texttext";
055: jbutton = new JButton(text, icon);
056: assertEquals("icon ", icon, jbutton.getIcon());
057: assertEquals("text ", text, jbutton.getText());
058: }
059:
060: /*
061: * Class under test for void JButton(Icon)
062: */
063: public void testJButtonIcon() {
064: Icon icon = createNewIcon();
065: jbutton = new JButton(icon);
066: assertEquals("icon ", icon, jbutton.getIcon());
067: assertEquals("text ", "", jbutton.getText());
068: }
069:
070: /*
071: * Class under test for void JButton(Action)
072: */
073: public void testJButtonAction() {
074: final String command = "dnammoc";
075: class MyAction extends AbstractAction {
076: private static final long serialVersionUID = 1L;
077:
078: public MyAction(final String text, final Icon icon) {
079: super (text, icon);
080: putValue(Action.ACTION_COMMAND_KEY, command);
081: }
082:
083: public void actionPerformed(final ActionEvent e) {
084: }
085: }
086: ;
087: Icon icon = createNewIcon();
088: String text = "texttext";
089: MyAction action = new MyAction(text, icon);
090: button = new JButton(action);
091: assertEquals("icon ", icon, button.getIcon());
092: assertEquals("text ", text, button.getText());
093: assertEquals("action", action, button.getAction());
094: assertEquals("command ", command, button.getActionCommand());
095: assertFalse("selected ", button.isSelected());
096: assertTrue("enabled ", button.isEnabled());
097: action.setEnabled(false);
098: button = new JButton(action);
099: assertEquals("icon ", icon, button.getIcon());
100: assertEquals("text ", text, button.getText());
101: assertEquals("action", action, button.getAction());
102: assertEquals("command ", command, button.getActionCommand());
103: assertFalse("selected ", button.isSelected());
104: assertFalse("enabled ", button.isEnabled());
105: button = new JButton((Action) null);
106: assertNull("icon ", button.getIcon());
107: assertNull("text ", button.getText());
108: assertNull("action", button.getAction());
109: assertNull("command ", button.getActionCommand());
110: assertFalse("selected ", button.isSelected());
111: assertTrue("enabled ", button.isEnabled());
112: assertTrue("button model is of the proper type", button
113: .getModel() instanceof DefaultButtonModel);
114: }
115:
116: /*
117: * Class under test for void JButton(String)
118: */
119: public void testJButtonString() {
120: String text = "texttext";
121: jbutton = new JButton(text);
122: assertNull("icon ", jbutton.getIcon());
123: assertEquals("text ", text, jbutton.getText());
124: }
125:
126: /*
127: * Class under test for void JButton()
128: */
129: public void testJButton() {
130: assertNotNull("default buttonModel ", button.getModel());
131: assertNull("icon ", jbutton.getIcon());
132: assertEquals("text ", "", jbutton.getText());
133: }
134:
135: public void testGetAccessibleContext() {
136: boolean assertedValue = (jbutton.getAccessibleContext() != null && jbutton
137: .getAccessibleContext().getClass().getName().equals(
138: "javax.swing.JButton$AccessibleJButton"));
139: assertTrue("AccessibleContext created properly ", assertedValue);
140: assertEquals("AccessibleRole", AccessibleRole.PUSH_BUTTON,
141: jbutton.getAccessibleContext().getAccessibleRole());
142: }
143:
144: /*
145: * Class under test for String paramString()
146: */
147: @Override
148: public void testParamString() {
149: assertTrue("ParamString returns a string ",
150: jbutton.toString() != null);
151: }
152:
153: public void testRemoveNotify() {
154: JRootPane pane = new JRootPane();
155: assertFalse("initial value for defaultButton ", jbutton
156: .isDefaultButton());
157: pane.getContentPane().add(jbutton);
158: pane.getContentPane().add(new JButton());
159: pane.setDefaultButton(jbutton);
160: assertTrue("'ve become defaultButton now ", jbutton
161: .isDefaultButton());
162: jbutton.removeNotify();
163: assertFalse("is not defaultButton now ", jbutton
164: .isDefaultButton());
165: assertNull("rootPane now 've no default jbutton ", pane
166: .getDefaultButton());
167: }
168:
169: public void testGetUIClassID() {
170: assertEquals("UI class ID", "ButtonUI", jbutton.getUIClassID());
171: }
172:
173: @Override
174: public void testUpdateUI() {
175: ButtonUI ui = new ButtonUI() {
176: };
177: jbutton.setUI(ui);
178: assertEquals(ui, jbutton.getUI());
179: jbutton.updateUI();
180: assertNotSame(ui, jbutton.getUI());
181: }
182:
183: /*
184: * Class under test for void configurePropertiesFromAction(Action)
185: */
186: public void testConfigurePropertiesFromActionAction() {
187: Icon icon1 = createNewIcon();
188: Icon icon2 = createNewIcon();
189: String text1 = "texttext1";
190: String text2 = "texttext2";
191: AbstractAction action1 = new AbstractAction(text1, icon1) {
192: private static final long serialVersionUID = 1L;
193:
194: public void actionPerformed(final ActionEvent event) {
195: }
196: };
197: AbstractAction action2 = new AbstractAction(text2, icon2) {
198: private static final long serialVersionUID = 1L;
199:
200: public void actionPerformed(final ActionEvent event) {
201: }
202: };
203: jbutton.configurePropertiesFromAction(action1);
204: assertEquals("icon ", icon1, jbutton.getIcon());
205: assertEquals("text ", text1, jbutton.getText());
206: jbutton.configurePropertiesFromAction(action2);
207: assertEquals("icon ", icon2, jbutton.getIcon());
208: assertEquals("text ", text2, jbutton.getText());
209: jbutton.configurePropertiesFromAction(null);
210: assertNull("icon ", jbutton.getIcon());
211: assertNull("text ", jbutton.getText());
212: assertNull("action", jbutton.getAction());
213: assertNull("command ", jbutton.getActionCommand());
214: assertFalse("selected ", jbutton.isSelected());
215: assertTrue("enabled ", jbutton.isEnabled());
216: }
217:
218: public void testSetDefaultCapable() {
219: jbutton.setDefaultCapable(false);
220: assertFalse("DefaultCapable", jbutton.isDefaultCapable());
221: PropertyChangeController listener = new PropertyChangeController();
222: jbutton.addPropertyChangeListener(listener);
223: jbutton.setDefaultCapable(true);
224: assertTrue("DefaultCapable", jbutton.isDefaultCapable());
225: listener.checkPropertyFired(jbutton, "defaultCapable",
226: Boolean.FALSE, Boolean.TRUE);
227: jbutton.setDefaultCapable(false);
228: assertFalse("DefaultCapable", jbutton.isDefaultCapable());
229: listener.checkPropertyFired(jbutton, "defaultCapable",
230: Boolean.TRUE, Boolean.FALSE);
231: }
232:
233: public void testIsDefaultCapable() {
234: assertTrue("initial DefaultCapable value", jbutton
235: .isDefaultCapable());
236: }
237:
238: public void testIsDefaultButton() {
239: JRootPane pane = new JRootPane();
240: assertFalse("initial value for defaultButton ", jbutton
241: .isDefaultButton());
242: pane.getContentPane().add(jbutton);
243: pane.getContentPane().add(new JButton());
244: assertFalse("is not defaultButton yet ", jbutton
245: .isDefaultButton());
246: pane.setDefaultButton(jbutton);
247: assertTrue("'ve become defaultButton now ", jbutton
248: .isDefaultButton());
249: }
250:
251: public void testWriteObject() {
252: /*
253: String text1 = "can you read betwwen the lines?";
254: String text2 = "can you look through the time?";
255: JButton button1 = new JButton(text1);
256: JButton button2 = new JButton(text2);
257: try {
258: FileOutputStream fo = new FileOutputStream("tmp");
259: ObjectOutputStream so = new ObjectOutputStream(fo);
260: so.writeObject(button1);
261: so.flush();
262: } catch (Exception e) {
263: System.out.println(e);
264: fail("serialization failed");
265: }
266: try {
267: FileOutputStream fo = new FileOutputStream("tmp");
268: ObjectOutputStream so = new ObjectOutputStream(fo);
269: so.writeObject(button2);
270: so.flush();
271: } catch (Exception e) {
272: fail("serialization failed");
273: }
274: */
275: }
276:
277: public void testReadObject() {
278: /*
279: String text1 = "can you read betwwen the lines?";
280: String text2 = "can you look through the time?";
281: JButton button1 = new JButton(text1);
282: JButton button2 = new JButton(text2);
283: try {
284: FileOutputStream fo = new FileOutputStream("tmp");
285: ObjectOutputStream so = new ObjectOutputStream(fo);
286: so.writeObject(button1);
287: so.flush();
288: } catch (Exception e) {
289: fail("serialization failed");
290: }
291: try {
292: FileInputStream fi = new FileInputStream("tmp");
293: ObjectInputStream si = new ObjectInputStream(fi);
294: JButton ressurectedButton = (JButton)si.readObject();
295: assertEquals("text ", text1, ressurectedButton.getText());
296: } catch (Exception e) {
297: fail("deserialization failed");
298: }
299:
300: try {
301: FileOutputStream fo = new FileOutputStream("tmp");
302: ObjectOutputStream so = new ObjectOutputStream(fo);
303: so.writeObject(button2);
304: so.flush();
305: } catch (Exception e) {
306: fail("serialization failed");
307: }
308: try {
309: FileInputStream fi = new FileInputStream("tmp");
310: ObjectInputStream si = new ObjectInputStream(fi);
311: JButton ressurectedButton = (JButton)si.readObject();
312: assertEquals("text ", text2, ressurectedButton.getText());
313: } catch (Exception e) {
314: fail("deserialization failed");
315: }
316: */
317: }
318:
319: @Override
320: public void testGetAlignmentXY() {
321: assertEquals("alignmentX ", button.getAlignmentX(), 0f, 1e-5);
322: assertEquals("alignmentY ", button.getAlignmentY(), 0.5f, 1e-5);
323: }
324:
325: @Override
326: public void testGetUI() {
327: assertTrue("ui is returned ", button.getUI() != null);
328: }
329: }
|