1
|
|
package abbot.editor.widgets;
|
2
|
|
|
3
|
|
import java.awt.*;
|
4
|
|
import java.awt.event.*;
|
5
|
|
import javax.swing.*;
|
6
|
|
import javax.swing.tree.*;
|
7
|
|
import java.lang.ref.*;
|
8
|
|
|
9
|
|
import junit.extensions.abbot.*;
|
10
|
|
import abbot.Log;
|
11
|
|
import abbot.tester.*;
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
public class AbstractComponentDecoratorTest extends ComponentTestFixture {
|
16
|
|
|
17
|
|
private static class Decorator extends AbstractComponentDecorator {
|
18
|
5
|
public Decorator(JComponent c) { super(c); }
|
19
|
6
|
public void paint(Graphics g) {
|
20
|
6
|
Log.debug("paint");
|
21
|
6
|
g.setColor(Color.red);
|
22
|
6
|
g.fillRect(0, 0, 100, 100);
|
23
|
|
}
|
24
|
|
}
|
25
|
|
|
26
|
|
private static class DecoratedLabel extends JLabel {
|
27
|
5
|
public DecoratedLabel(String text) {
|
28
|
5
|
super(text);
|
29
|
|
}
|
30
|
|
}
|
31
|
|
|
32
|
|
private class MouseWatcher extends MouseAdapter {
|
33
|
|
public boolean gotPress = false;
|
34
|
1
|
public void mousePressed(MouseEvent e) {
|
35
|
1
|
gotPress = true;
|
36
|
|
}
|
37
|
|
}
|
38
|
1
|
public void testNoEffectOnInput() {
|
39
|
1
|
MouseWatcher watcher = new MouseWatcher();
|
40
|
1
|
JLabel label = new DecoratedLabel(getName());
|
41
|
1
|
label.addMouseListener(watcher);
|
42
|
1
|
showFrame(label);
|
43
|
1
|
new Decorator(label);
|
44
|
1
|
getRobot().waitForIdle();
|
45
|
1
|
getRobot().click(label);
|
46
|
1
|
getRobot().waitForIdle();
|
47
|
1
|
assertTrue("Input not passed to label", watcher.gotPress);
|
48
|
|
}
|
49
|
|
|
50
|
1
|
public void testDecorateBeforeShow() {
|
51
|
1
|
JLabel label = new DecoratedLabel(getName());
|
52
|
1
|
new Decorator(label);
|
53
|
1
|
showFrame(label);
|
54
|
1
|
Point where = label.getLocationOnScreen();
|
55
|
1
|
getRobot().waitForIdle();
|
56
|
1
|
assertEquals("Decorator added before show didn't take effect",
|
57
|
|
Color.red, getRobot().sample(where.x, where.y));
|
58
|
|
}
|
59
|
|
|
60
|
1
|
public void testDecorateAfterRepaint() {
|
61
|
1
|
JLabel label = new DecoratedLabel(getName());
|
62
|
1
|
showFrame(label);
|
63
|
1
|
Decorator decorator = new Decorator(label);
|
64
|
1
|
getRobot().waitForIdle();
|
65
|
1
|
Point where = label.getLocationOnScreen();
|
66
|
1
|
assertEquals("Decoration not applied", Color.red,
|
67
|
|
getRobot().sample(where.x, where.y));
|
68
|
1
|
label.repaint();
|
69
|
1
|
getRobot().waitForIdle();
|
70
|
1
|
assertEquals("Decoration not applied after repaint", Color.red,
|
71
|
|
getRobot().sample(where.x, where.y));
|
72
|
1
|
decorator.dispose();
|
73
|
1
|
decorator = null;
|
74
|
1
|
getRobot().waitForIdle();
|
75
|
1
|
assertTrue("Decoration not removed on decorator removal",
|
76
|
|
!Color.red.equals(getRobot().sample(where.x, where.y)));
|
77
|
|
}
|
78
|
|
|
79
|
1
|
public void testDecorateAndUndecorate() {
|
80
|
1
|
JLabel label = new DecoratedLabel(getName());
|
81
|
1
|
showFrame(label);
|
82
|
1
|
Decorator decorator = new Decorator(label);
|
83
|
1
|
getRobot().waitForIdle();
|
84
|
1
|
Point where = label.getLocationOnScreen();
|
85
|
1
|
assertEquals("Decoration not applied", Color.red,
|
86
|
|
getRobot().sample(where.x, where.y));
|
87
|
1
|
WeakReference ref = new WeakReference(decorator);
|
88
|
1
|
decorator.dispose();
|
89
|
1
|
decorator = null;
|
90
|
1
|
getRobot().waitForIdle();
|
91
|
1
|
assertTrue("Decoration not removed",
|
92
|
|
!Color.red.equals(getRobot().sample(where.x, where.y)));
|
93
|
1
|
System.gc();
|
94
|
1
|
assertNull("Reference to decorator still held", ref.get());
|
95
|
|
}
|
96
|
|
|
97
|
1
|
public void testAutoDispose() throws Throwable {
|
98
|
1
|
JLabel label = new DecoratedLabel(getName());
|
99
|
1
|
Frame frame = showFrame(label);
|
100
|
1
|
Decorator decorator = new Decorator(label);
|
101
|
1
|
WeakReference dref = new WeakReference(decorator);
|
102
|
1
|
WeakReference lref = new WeakReference(label);
|
103
|
1
|
decorator = null;
|
104
|
1
|
frame.remove(label);
|
105
|
1
|
assertNull("Label should have no parent", label.getParent());
|
106
|
1
|
label = null;
|
107
|
|
|
108
|
|
|
109
|
1
|
disposeWindow(frame);
|
110
|
|
|
111
|
1
|
System.gc();
|
112
|
1
|
assertEquals("Label still referenced", null, lref.get());
|
113
|
1
|
assertEquals("Decorator still referenced", null, dref.get());
|
114
|
|
}
|
115
|
|
|
116
|
5
|
public AbstractComponentDecoratorTest(String name) { super(name); }
|
117
|
0
|
public static void main(String[] args) {
|
118
|
0
|
TestHelper.runTests(args, AbstractComponentDecoratorTest.class);
|
119
|
|
}
|
120
|
|
}
|
121
|
|
|