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 31.01.2005
021:
022: */package javax.swing;
023:
024: import java.awt.event.ActionEvent;
025: import java.awt.image.BufferedImage;
026: import javax.swing.Timer_MultithreadedTest.ConcreteActionListener;
027: import junit.framework.TestCase;
028:
029: public class AbstractButton_MultithreadedTest extends TestCase {
030: protected AbstractButton button = null;
031:
032: public AbstractButton_MultithreadedTest(final String str) {
033: super (str);
034: }
035:
036: /*
037: * @see JComponentTest#setUp()
038: */
039: @Override
040: protected void setUp() throws Exception {
041: super .setUp();
042: button = new JButton();
043: }
044:
045: /*
046: * @see JComponentTest#tearDown()
047: */
048: @Override
049: protected void tearDown() throws Exception {
050: super .tearDown();
051: }
052:
053: class MyAction extends AbstractAction {
054: private static final long serialVersionUID = 1L;
055:
056: public Object performed = null;
057:
058: public int eventsCounter = 0;
059:
060: public MyAction(final String text, final Icon icon) {
061: super (text, icon);
062: }
063:
064: public void actionPerformed(final ActionEvent e) {
065: synchronized (this ) {
066: eventsCounter++;
067: performed = e;
068: notifyAll();
069: }
070: }
071: };
072:
073: public void testDoClickint1() throws InterruptedException {
074: MyAction action = new MyAction(null, null);
075: MyAction listener = new MyAction(null, null);
076: button = new JButton(action);
077: button.addActionListener(listener);
078: synchronized (action) {
079: button.doClick(50);
080: action.wait(1000);
081: }
082: assertTrue("action performed", listener.performed != null);
083: assertTrue("action performed", action.performed != null);
084: }
085:
086: public void testDoClickint2() throws InterruptedException {
087: ConcreteActionListener listener = new ConcreteActionListener();
088: button = new JButton();
089: button.addActionListener(listener);
090: synchronized (listener) {
091: button.doClick(1);
092: button.doClick(1);
093: button.doClick(1);
094: button.doClick(1);
095: button.doClick(1);
096: listener.waitNumActions(1500, 5);
097: }
098: assertEquals(5, listener.getCounter());
099: }
100:
101: public void testDoClick() {
102: Icon icon = new ImageIcon(new BufferedImage(20, 20,
103: BufferedImage.TYPE_BYTE_GRAY));
104: String text = "texttext";
105: MyAction action = new MyAction(text, icon);
106: MyAction listener = new MyAction(text, icon);
107: button = new JButton(action);
108: button.addActionListener(listener);
109: try {
110: synchronized (action) {
111: button.doClick();
112: action.wait(1000);
113: }
114: } catch (InterruptedException e) {
115: fail(e.getMessage());
116: }
117: assertTrue("action performed", listener.performed != null);
118: assertTrue("action performed", action.performed != null);
119: }
120: }
|