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 05.04.2005
021:
022: */package javax.swing;
023:
024: import java.awt.Dimension;
025: import java.awt.Image;
026: import java.awt.image.ImageObserver;
027: import java.net.URL;
028: import junit.framework.TestCase;
029:
030: public class ImageIcon_MultithreadedTest extends TestCase {
031: class MyImageObserver implements ImageObserver {
032: public boolean updated = false;
033:
034: public boolean installed = false;
035:
036: public Object lock = new Object();
037:
038: public void reset() {
039: updated = false;
040: }
041:
042: public boolean imageUpdate(final Image img,
043: final int infoflags, final int x, final int y,
044: final int width, final int height) {
045: updated = true;
046: synchronized (lock) {
047: lock.notifyAll();
048: }
049: return true;
050: }
051: };
052:
053: protected ImageIcon icon = null;
054:
055: private JFrame frame = null;
056:
057: public static void main(final String[] args) {
058: junit.textui.TestRunner.run(ImageIcon_MultithreadedTest.class);
059: }
060:
061: /*
062: * @see TestCase#setUp()
063: */
064: @Override
065: protected void setUp() throws Exception {
066: super .setUp();
067: }
068:
069: /*
070: * @see TestCase#tearDown()
071: */
072: @Override
073: protected void tearDown() throws Exception {
074: if (frame != null) {
075: frame.dispose();
076: frame = null;
077: }
078: super .tearDown();
079: }
080:
081: public void testPaintIcon() {
082: // JFrame frame = new JFrame();
083: // String fileName1 = "images/animated.gif";
084: // URL url1 = getClass().getResource(fileName1);
085: // icon = new ImageIcon(url1);
086: // JButton button = new JButton(icon);
087: // frame.getContentPane().add(button);
088: // frame.pack();
089: // frame.show();
090: // InternalTests.isRealized(frame);
091: // MyImageObserver observer1 = new MyImageObserver();
092: // icon.setImageObserver(observer1);
093: // int timeToWait = 15000;
094: // while(timeToWait > 0) {
095: // try {
096: // Thread.sleep(10);
097: // timeToWait -= 10;
098: // } catch (InterruptedException e) {
099: // e.printStackTrace();
100: // }
101: // }
102: }
103:
104: @SuppressWarnings("deprecation")
105: public void testSetImageObserver() {
106: final MyImageObserver observer1 = new MyImageObserver();
107: String fileName1 = "images/animated.gif";
108: final URL url = getClass().getResource(fileName1);
109: assertTrue("file is found", url != null);
110: assertFalse("observer is not notified", observer1.updated);
111: frame = new JFrame();
112: ImageIcon icon1 = new ImageIcon(url);
113: JButton button = new JButton(icon1);
114: button.setPreferredSize(new Dimension(30, 30));
115: icon1.setImageObserver(observer1);
116: synchronized (observer1.lock) {
117: observer1.installed = true;
118: observer1.lock.notifyAll();
119: }
120: frame.getContentPane().add(button);
121: frame.pack();
122: frame.show();
123: SwingWaitTestCase.isRealized(frame);
124: waitTillObserverNotified(observer1, 3000);
125: assertTrue("observer is notified", observer1.updated);
126: observer1.reset();
127: waitTillObserverNotified(observer1, 3000);
128: assertTrue("observer is notified", observer1.updated);
129: observer1.reset();
130: waitTillObserverNotified(observer1, 3000);
131: assertTrue("observer is notified", observer1.updated);
132: observer1.reset();
133: }
134:
135: /**
136: * waits till observer change his state to 'installed' and then to 'updated'
137: * quits waiting when maxWaitTime is over
138: */
139: private void waitTillObserverNotified(
140: final MyImageObserver observer, final int maxWaitTime) {
141: int timeRemains = maxWaitTime;
142: while (timeRemains > 0 && !observer.installed) {
143: try {
144: synchronized (observer.lock) {
145: observer.lock.wait(10);
146: timeRemains -= 10;
147: }
148: } catch (InterruptedException e) {
149: fail(e.getMessage());
150: }
151: }
152: assertTrue("observer did manage to be installed",
153: observer.installed);
154: timeRemains = maxWaitTime;
155: while (timeRemains > 0 && !observer.updated) {
156: try {
157: synchronized (observer.lock) {
158: observer.lock.wait(10);
159: }
160: timeRemains -= 10;
161: } catch (InterruptedException e) {
162: fail(e.getMessage());
163: }
164: }
165: }
166: }
|