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 Michael Danilov
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.awt.event.KeyAdapter;
025: import java.awt.event.KeyEvent;
026: import java.awt.event.KeyListener;
027:
028: import junit.framework.TestCase;
029:
030: @SuppressWarnings("serial")
031: public class ButtonTest extends TestCase {
032:
033: class TestButton extends Button {
034: public TestButton(String label) {
035: super (label);
036: }
037:
038: public TestButton() {
039: super ();
040: }
041: }
042:
043: private TestButton button;
044: private boolean eventProcessed;
045:
046: @Override
047: protected void setUp() throws Exception {
048: super .setUp();
049: button = new TestButton("Button");
050: }
051:
052: public void testButton() {
053: button = new TestButton();
054:
055: assertTrue(button.getLabel().equals(""));
056: }
057:
058: public void testButtonString() {
059: assertTrue(button.getLabel() == "Button");
060: }
061:
062: public void testGetSetLabel() {
063: button.setLabel(null);
064: assertNull(button.getLabel());
065:
066: button.setLabel("Button");
067: assertTrue(button.getLabel().equals("Button"));
068: }
069:
070: public void testGetSetActionCommand() {
071: assertTrue(button.getActionCommand().equals("Button"));
072: button.setLabel(null);
073: assertNull(button.getActionCommand());
074:
075: button.setActionCommand("Button Command");
076: assertTrue(button.getActionCommand().equals("Button Command"));
077: }
078:
079: public void testAddGetRemoveActionListener() {
080: assertTrue(button.getActionListeners().length == 0);
081:
082: ActionListener listener = new ActionListener() {
083: public void actionPerformed(ActionEvent e) {
084: }
085: };
086: button.addActionListener(listener);
087: assertTrue(button.getActionListeners().length == 1);
088: assertTrue(button.getActionListeners()[0] == listener);
089:
090: button.removeActionListener(listener);
091: assertTrue(button.getActionListeners().length == 0);
092: }
093:
094: public void testProcessEvent() {
095: eventProcessed = false;
096: button.addKeyListener(new KeyAdapter() {
097: @Override
098: public void keyPressed(KeyEvent a0) {
099: eventProcessed = true;
100: }
101: });
102: button.processEvent(new KeyEvent(button, KeyEvent.KEY_PRESSED,
103: 0, 0, 0, 's'));
104: assertTrue(eventProcessed);
105: }
106:
107: public void testProcessActionEvent() {
108: eventProcessed = false;
109: button.addActionListener(new ActionListener() {
110: public void actionPerformed(ActionEvent e) {
111: eventProcessed = true;
112: }
113: });
114: button.processEvent(new ActionEvent(button,
115: ActionEvent.ACTION_PERFORMED, null, 0, 0));
116: assertTrue(eventProcessed);
117: }
118:
119: public void testGetListenersClass() {
120: assertTrue(button.getListeners(KeyListener.class).length == 0);
121:
122: KeyAdapter listener = new KeyAdapter() {
123: };
124: button.addKeyListener(listener);
125: assertTrue(button.getListeners(KeyListener.class).length == 1);
126: assertTrue(button.getListeners(KeyListener.class)[0] == listener);
127:
128: button.removeKeyListener(listener);
129: assertTrue(button.getListeners(KeyListener.class).length == 0);
130: }
131: }
|