|
| 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.stage; |
| 27 | + |
| 28 | +import javafx.application.Application; |
| 29 | +import javafx.application.Platform; |
| 30 | +import javafx.scene.Node; |
| 31 | +import javafx.scene.Scene; |
| 32 | +import javafx.scene.control.TextField; |
| 33 | +import javafx.stage.Stage; |
| 34 | +import javafx.stage.WindowEvent; |
| 35 | + |
| 36 | +import java.lang.ref.WeakReference; |
| 37 | +import java.util.concurrent.CountDownLatch; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | + |
| 40 | +import junit.framework.Assert; |
| 41 | +import test.util.Util; |
| 42 | + |
| 43 | +public abstract class FocusedWindowTestBase { |
| 44 | + |
| 45 | + static CountDownLatch startupLatch; |
| 46 | + static Stage stage = null; |
| 47 | + |
| 48 | + public static void initFXBase() throws Exception { |
| 49 | + startupLatch = new CountDownLatch(1); |
| 50 | + Platform.startup(startupLatch::countDown); |
| 51 | + Platform.setImplicitExit(false); |
| 52 | + Assert.assertTrue("Timeout waiting for FX runtime to start", |
| 53 | + startupLatch.await(15, TimeUnit.MILLISECONDS)); |
| 54 | + } |
| 55 | + |
| 56 | + WeakReference<Stage> closedFocusedStageWeak = null; |
| 57 | + Stage closedFocusedStage = null; |
| 58 | + |
| 59 | + public void testClosedFocusedStageLeakBase() throws Exception { |
| 60 | + CountDownLatch latch = new CountDownLatch(1); |
| 61 | + Util.runAndWait(() -> { |
| 62 | + closedFocusedStage = new Stage(); |
| 63 | + closedFocusedStage.setTitle("Focused Stage"); |
| 64 | + closedFocusedStageWeak = new WeakReference<>(closedFocusedStage); |
| 65 | + TextField textField = new TextField(); |
| 66 | + closedFocusedStage.setScene(new Scene(textField)); |
| 67 | + closedFocusedStage.setOnShown(l -> { |
| 68 | + latch.countDown(); |
| 69 | + }); |
| 70 | + closedFocusedStage.show(); |
| 71 | + }); |
| 72 | + Assert.assertTrue("Timeout waiting for closedFocusedStage to show`", |
| 73 | + latch.await(15, TimeUnit.MILLISECONDS)); |
| 74 | + |
| 75 | + CountDownLatch hideLatch = new CountDownLatch(1); |
| 76 | + closedFocusedStage.setOnHidden(a -> { |
| 77 | + hideLatch.countDown(); |
| 78 | + }); |
| 79 | + Util.runAndWait(() -> closedFocusedStage.close()); |
| 80 | + Assert.assertTrue("Timeout waiting for closedFocusedStage to hide`", |
| 81 | + hideLatch.await(15, TimeUnit.MILLISECONDS)); |
| 82 | + |
| 83 | + closedFocusedStage.requestFocus(); |
| 84 | + closedFocusedStage = null; |
| 85 | + assertCollectable(closedFocusedStageWeak); |
| 86 | + } |
| 87 | + |
| 88 | + public static void assertCollectable(WeakReference weakReference) throws Exception { |
| 89 | + int counter = 0; |
| 90 | + |
| 91 | + System.gc(); |
| 92 | + System.runFinalization(); |
| 93 | + |
| 94 | + while (counter < 10 && weakReference.get() != null) { |
| 95 | + Thread.sleep(100); |
| 96 | + counter = counter + 1; |
| 97 | + System.gc(); |
| 98 | + System.runFinalization(); |
| 99 | + } |
| 100 | + |
| 101 | + Assert.assertNull(weakReference.get()); |
| 102 | + } |
| 103 | +} |
0 commit comments