|
| 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.control; |
| 27 | + |
| 28 | +import javafx.application.Application; |
| 29 | +import javafx.application.Platform; |
| 30 | +import javafx.scene.Scene; |
| 31 | +import javafx.scene.control.Tab; |
| 32 | +import javafx.scene.control.TabPane; |
| 33 | +import javafx.scene.control.TextField; |
| 34 | +import javafx.scene.layout.StackPane; |
| 35 | +import javafx.stage.Stage; |
| 36 | + |
| 37 | +import java.lang.ref.WeakReference; |
| 38 | +import java.util.concurrent.CountDownLatch; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | + |
| 41 | +import junit.framework.Assert; |
| 42 | +import org.junit.AfterClass; |
| 43 | +import org.junit.BeforeClass; |
| 44 | +import org.junit.Test; |
| 45 | +import test.util.Util; |
| 46 | +import static org.junit.Assert.assertTrue; |
| 47 | + |
| 48 | +public class TabPaneHeaderLeakTest { |
| 49 | + |
| 50 | + private static CountDownLatch startupLatch; |
| 51 | + private static StackPane root; |
| 52 | + private static Stage stage; |
| 53 | + private static TabPane tabPane; |
| 54 | + private static WeakReference<TextField> textFieldWeakRef; |
| 55 | + private static WeakReference<Tab> tabWeakRef; |
| 56 | + |
| 57 | + public static class TestApp extends Application { |
| 58 | + @Override |
| 59 | + public void start(Stage primaryStage) throws Exception { |
| 60 | + stage = primaryStage; |
| 61 | + TextField tf = new TextField("Weak ref TF"); |
| 62 | + textFieldWeakRef = new WeakReference<>(tf); |
| 63 | + Tab tab = new Tab("Tab", tf); |
| 64 | + tabWeakRef = new WeakReference<>(tab); |
| 65 | + tabPane = new TabPane(tab, new Tab("Tab1")); |
| 66 | + tab = null; |
| 67 | + tf = null; |
| 68 | + |
| 69 | + root = new StackPane(tabPane); |
| 70 | + Scene scene = new Scene(root); |
| 71 | + primaryStage.setScene(scene); |
| 72 | + primaryStage.setOnShown(l -> { |
| 73 | + Platform.runLater(() -> startupLatch.countDown()); |
| 74 | + }); |
| 75 | + primaryStage.show(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @BeforeClass |
| 80 | + public static void initFX() throws Exception { |
| 81 | + startupLatch = new CountDownLatch(1); |
| 82 | + new Thread(() -> Application.launch(TestApp.class, (String[]) null)).start(); |
| 83 | + assertTrue("Timeout waiting for FX runtime to start", |
| 84 | + startupLatch.await(15, TimeUnit.SECONDS)); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testTabPaneHeaderLeak() throws Exception { |
| 89 | + Util.sleep(100); |
| 90 | + Util.runAndWait(() -> { |
| 91 | + tabPane.getTabs().clear(); |
| 92 | + root.getChildren().clear(); |
| 93 | + }); |
| 94 | + for (int i = 0; i < 10; i++) { |
| 95 | + System.gc(); |
| 96 | + System.runFinalization(); |
| 97 | + if (tabWeakRef.get() == null && |
| 98 | + textFieldWeakRef.get() == null) { |
| 99 | + break; |
| 100 | + } |
| 101 | + Util.sleep(500); |
| 102 | + } |
| 103 | + // Ensure that Tab and TextField are GCed. |
| 104 | + Assert.assertNull("Couldn't collect Tab", tabWeakRef.get()); |
| 105 | + Assert.assertNull("Couldn't collect TextField", textFieldWeakRef.get()); |
| 106 | + } |
| 107 | + |
| 108 | + @AfterClass |
| 109 | + public static void teardownOnce() { |
| 110 | + Platform.runLater(() -> { |
| 111 | + stage.hide(); |
| 112 | + Platform.exit(); |
| 113 | + }); |
| 114 | + } |
| 115 | +} |
0 commit comments