Skip to content

Commit d64f6c8

Browse files
committed
Adding system test using Group and Shape
1 parent cbcc316 commit d64f6c8

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package test.javafx.scene.shape;
27+
28+
import javafx.application.Application;
29+
import javafx.application.Platform;
30+
import javafx.scene.Group;
31+
import javafx.scene.Scene;
32+
import javafx.scene.layout.StackPane;
33+
import javafx.scene.shape.Circle;
34+
import javafx.scene.shape.Rectangle;
35+
import javafx.scene.shape.Shape;
36+
import javafx.stage.Stage;
37+
38+
import java.lang.ref.WeakReference;
39+
import java.util.concurrent.CountDownLatch;
40+
import java.util.concurrent.TimeUnit;
41+
42+
import junit.framework.Assert;
43+
import org.junit.AfterClass;
44+
import org.junit.BeforeClass;
45+
import org.junit.Test;
46+
import test.util.Util;
47+
48+
public class ShapeViewOrderLeakTest extends Application {
49+
50+
private static CountDownLatch startupLatch;
51+
private static StackPane root;
52+
private static Stage stage;
53+
private static Group group;
54+
private static WeakReference<Shape> shapeWeakRef;
55+
56+
@Override
57+
public void start(Stage primaryStage) throws Exception {
58+
stage = primaryStage;
59+
Shape shape1 = new Rectangle();
60+
Shape shape2 = new Circle();
61+
shape1.setViewOrder(1);
62+
shape2.setViewOrder(0);
63+
shapeWeakRef = new WeakReference<>(shape1);
64+
65+
group = new Group(shape1, shape2);
66+
shape1 = null;
67+
shape2 = null;
68+
69+
root = new StackPane(group);
70+
Scene scene = new Scene(root);
71+
primaryStage.setScene(scene);
72+
primaryStage.setOnShown(l -> {
73+
startupLatch.countDown();
74+
});
75+
primaryStage.show();
76+
}
77+
78+
@BeforeClass
79+
public static void initFX() {
80+
startupLatch = new CountDownLatch(1);
81+
new Thread(() -> Application.launch(ShapeViewOrderLeakTest.class, (String[]) null)).start();
82+
try {
83+
if (!startupLatch.await(15, TimeUnit.SECONDS)) {
84+
Assert.fail("Timeout waiting for FX runtime to start");
85+
}
86+
} catch (InterruptedException ex) {
87+
Assert.fail("Unexpected exception: " + ex);
88+
}
89+
}
90+
91+
@Test
92+
public void testShapeViewOrderLeak() throws Exception {
93+
Util.sleep(100);
94+
Util.runAndWait(() -> {
95+
group.getChildren().clear();
96+
root.getChildren().clear();
97+
});
98+
for (int i = 0; i < 10; i++) {
99+
System.gc();
100+
System.runFinalization();
101+
if (shapeWeakRef.get() == null) {
102+
break;
103+
}
104+
Util.sleep(500);
105+
}
106+
// Ensure that Shape is GCed.
107+
Assert.assertNull("Couldn't collect Shape", shapeWeakRef.get());
108+
}
109+
110+
@AfterClass
111+
public static void teardownOnce() {
112+
Platform.runLater(() -> {
113+
stage.hide();
114+
Platform.exit();
115+
});
116+
}
117+
}

0 commit comments

Comments
 (0)