28package com.jogamp.opengl.test.bugs;
30import java.applet.Applet;
31import java.awt.BorderLayout;
32import java.awt.Canvas;
34import java.awt.Component;
35import java.awt.EventQueue;
36import java.awt.Graphics;
37import java.lang.reflect.InvocationTargetException;
38import java.util.concurrent.atomic.AtomicInteger;
40@SuppressWarnings(
"serial")
43 private static String currentThreadName() {
return "["+Thread.currentThread().getName()+
", isAWT-EDT "+EventQueue.isDispatchThread()+
"]"; }
45 private static void invoke(
final boolean wait,
final Runnable r) {
46 if(EventQueue.isDispatchThread()) {
51 EventQueue.invokeAndWait(r);
53 EventQueue.invokeLater(r);
55 }
catch (
final InvocationTargetException e) {
56 throw new RuntimeException(e.getTargetException());
57 }
catch (
final InterruptedException e) {
58 throw new RuntimeException(e);
63 private static final String comp2Str(
final Component c) {
64 return c.getClass().getSimpleName()+
"[visible "+c.isVisible()+
", showing "+c.isShowing()+
", valid "+c.isValid()+
65 ", displayable "+c.isDisplayable()+
", "+c.getX()+
"/"+c.getY()+
" "+c.getWidth()+
"x"+c.getHeight()+
"]";
68 private void println(
final String msg) {
69 System.err.println(msg);
72 private final void checkComponentState(
final String msg,
final boolean expIsContained,
final int expAddNotifyCount,
final int expRemoveNotifyCount) {
73 final int compCount = getComponentCount();
74 final Component c = 1 <= compCount ? getComponent(0) :
null;
75 final String clazzName =
null != c ? c.getName() :
"n/a";
76 final boolean isContained = c == myCanvas;
77 final String okS = ( expIsContained == isContained &&
78 expAddNotifyCount == myCanvas.addNotifyCount &&
79 expRemoveNotifyCount == myCanvas.removeNotifyCount ) ?
"OK" :
"ERROR";
80 println(
"Component-State @ "+msg+
": "+okS+
81 ", contained[exp "+expIsContained+
", has "+isContained+
"]"+(expIsContained!=isContained?
"*":
"")+
82 ", addNotify[exp "+expAddNotifyCount+
", has "+myCanvas.addNotifyCount+
"]"+(expAddNotifyCount!=myCanvas.addNotifyCount?
"*":
"")+
83 ", removeNotify[exp "+expRemoveNotifyCount+
", has "+myCanvas.removeNotifyCount+
"]"+(expRemoveNotifyCount!=myCanvas.removeNotifyCount?
"*":
"")+
84 ", compCount "+compCount+
", compClazz "+clazzName);
87 AtomicInteger initCount =
new AtomicInteger(0);
88 AtomicInteger startCount =
new AtomicInteger(0);
89 AtomicInteger stopCount =
new AtomicInteger(0);
90 AtomicInteger destroyCount =
new AtomicInteger(0);
92 private final void checkAppletState(
final String msg,
final boolean expIsActive,
93 final int expInitCount,
final int expStartCount,
final int expStopCount,
final boolean startStopCountEquals,
final int expDestroyCount) {
94 final boolean isActive = this.isActive();
95 final String okS = ( expInitCount == initCount.get() &&
96 expIsActive == isActive &&
97 expStartCount == startCount.get() &&
98 expStopCount == stopCount.get() &&
99 expDestroyCount == destroyCount.get() &&
100 ( !startStopCountEquals || startCount == stopCount ) ) ?
"OK" :
"ERROR";
101 println(
"Applet-State @ "+msg+
": "+okS+
102 ", active[exp "+expIsActive+
", has "+isActive+
"]"+(expIsActive!=isActive?
"*":
"")+
103 ", init[exp "+expInitCount+
", has "+initCount+
"]"+(expInitCount!=initCount.get()?
"*":
"")+
104 ", start[exp "+expStartCount+
", has "+startCount+
"]"+(expStartCount!=startCount.get()?
"*":
"")+
105 ", stop[exp "+expStopCount+
", has "+stopCount+
"]"+(expStopCount!=stopCount.get()?
"*":
"")+
106 ", start==stop[exp "+startStopCountEquals+
", start "+startCount+
", stop "+stopCount+
"]"+(( startStopCountEquals && startCount != stopCount )?
"*":
"")+
107 ", destroy[exp "+expDestroyCount+
", has "+destroyCount+
"]"+(expDestroyCount!=destroyCount.get()?
"*":
""));
110 private class MyCanvas
extends Canvas {
111 int addNotifyCount = 0;
112 int removeNotifyCount = 0;
116 setBackground(
new Color( 200, 200, 255 ) );
119 public String toString() {
120 return comp2Str(
this)+
", add/remove[addNotify "+addNotifyCount+
", removeCount "+removeNotifyCount+
"]";
124 public void addNotify() {
126 println(
"Applet.Canvas.addNotify() - "+currentThreadName());
127 if( !EventQueue.isDispatchThread() ) {
128 println(
"Applet.Canvas.addNotify() ERROR: Not on AWT-EDT");
132 println(
"Applet.Canvas.addNotify(): "+
this);
136 public void removeNotify() {
138 println(
"Applet.Canvas.removeNotify() - "+currentThreadName());
139 println(
"Applet.Canvas.removeNotify(): "+
this);
140 if( !EventQueue.isDispatchThread() ) {
141 println(
"Applet.Canvas.removeNotify() ERROR: Not on AWT-EDT");
144 super.removeNotify();
148 public void paint(
final Graphics g) {
151 final int width = getWidth();
152 final int height = getHeight();
153 final String msg =
"The payload Canvas. Paint "+width+
"x"+height+
" #"+paintCount;
154 g.setColor(Color.black);
155 g.drawString(msg, 64, 64);
158 MyCanvas myCanvas =
null;
162 final java.awt.Dimension aSize = getSize();
163 println(
"Applet.init() START - applet.size "+aSize+
" - "+currentThreadName());
164 initCount.incrementAndGet();
165 checkAppletState(
"init",
false , 1 ,
168 invoke(
true,
new Runnable() {
170 setLayout(
new BorderLayout());
171 myCanvas =
new MyCanvas();
173 println(
"Applet.init(): canvas "+comp2Str(myCanvas));
174 checkComponentState(
"init-add.pre",
false, 0, 0);
175 add(myCanvas, BorderLayout.CENTER);
177 checkComponentState(
"init-add.post",
true, 1, 0);
178 println(
"Applet.init(): canvas "+comp2Str(myCanvas));
180 println(
"Applet.init() END - "+currentThreadName());
185 println(
"Applet.start() START (isVisible "+isVisible()+
", isDisplayable "+isDisplayable()+
") - "+currentThreadName());
186 startCount.incrementAndGet();
187 checkAppletState(
"start",
true , 1 ,
188 startCount.get() , startCount.get()-1 ,
false ,
190 invoke(
true,
new Runnable() {
192 checkComponentState(
"start-visible.pre",
true, 1, 0);
193 if(
null != myCanvas ) {
194 myCanvas.setFocusable(
true);
195 myCanvas.requestFocus();
197 checkComponentState(
"start-visible.post",
true, 1, 0);
199 println(
"Applet.start(): canvas "+comp2Str(myCanvas));
202 println(
"Applet.start() END - "+currentThreadName());
207 println(
"Applet.stop() START - "+currentThreadName());
208 stopCount.incrementAndGet();
209 checkAppletState(
"stop",
false , 1 ,
210 stopCount.get() , stopCount.get() ,
true ,
212 invoke(
true,
new Runnable() {
214 checkComponentState(
"stop",
true, 1, 0);
216 println(
"Applet.stop() END - "+currentThreadName());
221 println(
"Applet.destroy() START - "+currentThreadName());
222 destroyCount.incrementAndGet();
223 checkAppletState(
"destroy",
false , 1 ,
224 startCount.get() , stopCount.get() ,
true ,
226 invoke(
true,
new Runnable() {
228 checkComponentState(
"destroy-remove.pre",
true, 1, 0);
230 checkComponentState(
"destroy-remove.post",
false, 1, 1);
232 println(
"Applet.destroy() END - "+currentThreadName());