35
35
import org .junit .runner .RunWith ;
36
36
import org .junit .runners .Parameterized ;
37
37
38
+ import com .sun .javafx .tk .Toolkit ;
39
+
38
40
import static javafx .scene .control .ControlShim .*;
39
41
import static org .junit .Assert .*;
40
42
import static test .com .sun .javafx .scene .control .infrastructure .ControlSkinFactory .*;
41
43
44
+ import javafx .scene .Scene ;
42
45
import javafx .scene .control .Accordion ;
43
46
import javafx .scene .control .ButtonBar ;
44
47
import javafx .scene .control .ColorPicker ;
51
54
import javafx .scene .control .PasswordField ;
52
55
import javafx .scene .control .ScrollBar ;
53
56
import javafx .scene .control .ScrollPane ;
57
+ import javafx .scene .control .Separator ;
58
+ import javafx .scene .control .Skin ;
54
59
import javafx .scene .control .Spinner ;
55
60
import javafx .scene .control .SplitMenuButton ;
56
61
import javafx .scene .control .SplitPane ;
57
62
import javafx .scene .control .TableRow ;
58
63
import javafx .scene .control .TableView ;
59
64
import javafx .scene .control .TreeTableRow ;
60
65
import javafx .scene .control .TreeTableView ;
66
+ import javafx .scene .layout .Pane ;
67
+ import javafx .scene .layout .VBox ;
68
+ import javafx .stage .Stage ;
61
69
62
70
/**
63
71
* Test memory leaks in Skin implementations.
@@ -78,12 +86,39 @@ public class SkinMemoryLeakTest {
78
86
@ Test
79
87
public void testMemoryLeakAlternativeSkin () {
80
88
installDefaultSkin (control );
89
+ // FIXME: JDK-8265406 - fragile test pattern
81
90
WeakReference <?> weakRef = new WeakReference <>(replaceSkin (control ));
82
91
assertNotNull (weakRef .get ());
83
92
attemptGC (weakRef );
84
93
assertEquals ("Skin must be gc'ed" , null , weakRef .get ());
85
94
}
86
95
96
+ /**
97
+ * default skin -> set alternative while showing
98
+ */
99
+ @ Test
100
+ public void testMemoryLeakAlternativeSkinShowing () {
101
+ showControl (control , true );
102
+ Skin <?> replacedSkin = replaceSkin (control );
103
+ WeakReference <?> weakRef = new WeakReference <>(replacedSkin );
104
+ assertNotNull (weakRef .get ());
105
+ // beware: this is important - we might get false reds without!
106
+ Toolkit .getToolkit ().firePulse ();
107
+ replacedSkin = null ;
108
+ attemptGC (weakRef );
109
+ assertEquals ("Skin must be gc'ed" , null , weakRef .get ());
110
+ }
111
+
112
+ @ Test
113
+ public void testControlChildren () {
114
+ installDefaultSkin (control );
115
+ int childCount = control .getChildrenUnmodifiable ().size ();
116
+ String skinClass = control .getSkin ().getClass ().getSimpleName ();
117
+ replaceSkin (control );
118
+ assertEquals (skinClass + " must remove direct children that it has added" ,
119
+ childCount , control .getChildrenUnmodifiable ().size ());
120
+ }
121
+
87
122
//------------ parameters
88
123
89
124
// Note: name property not supported before junit 4.11
@@ -106,6 +141,8 @@ public static Collection<Object[]> data() {
106
141
PasswordField .class ,
107
142
ScrollBar .class ,
108
143
ScrollPane .class ,
144
+ // @Ignore("8273071")
145
+ Separator .class ,
109
146
// @Ignore("8245145")
110
147
Spinner .class ,
111
148
SplitMenuButton .class ,
@@ -126,6 +163,36 @@ public SkinMemoryLeakTest(Class<Control> controlClass) {
126
163
127
164
//------------ setup
128
165
166
+ private Scene scene ;
167
+ private Stage stage ;
168
+ private Pane root ;
169
+
170
+ /**
171
+ * Ensures the control is shown in an active scenegraph. Requests
172
+ * focus on the control if focused == true.
173
+ *
174
+ * @param control the control to show
175
+ * @param focused if true, requests focus on the added control
176
+ */
177
+ protected void showControl (Control control , boolean focused ) {
178
+ if (root == null ) {
179
+ root = new VBox ();
180
+ scene = new Scene (root );
181
+ stage = new Stage ();
182
+ stage .setScene (scene );
183
+ }
184
+ if (!root .getChildren ().contains (control )) {
185
+ root .getChildren ().add (control );
186
+ }
187
+ stage .show ();
188
+ if (focused ) {
189
+ stage .requestFocus ();
190
+ control .requestFocus ();
191
+ assertTrue (control .isFocused ());
192
+ assertSame (control , scene .getFocusOwner ());
193
+ }
194
+ }
195
+
129
196
@ Before
130
197
public void setup () {
131
198
Thread .currentThread ().setUncaughtExceptionHandler ((thread , throwable ) -> {
@@ -141,6 +208,7 @@ public void setup() {
141
208
142
209
@ After
143
210
public void cleanup () {
211
+ if (stage != null ) stage .hide ();
144
212
Thread .currentThread ().setUncaughtExceptionHandler (null );
145
213
}
146
214
0 commit comments