|
| 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 | +package test.javafx.scene; |
| 26 | + |
| 27 | +import com.sun.javafx.PlatformUtil; |
| 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.Label; |
| 33 | +import javafx.scene.control.ScrollPane; |
| 34 | +import javafx.scene.layout.VBox; |
| 35 | +import javafx.stage.Stage; |
| 36 | +import javafx.stage.WindowEvent; |
| 37 | +import junit.framework.Assert; |
| 38 | +import org.junit.AfterClass; |
| 39 | +import org.junit.BeforeClass; |
| 40 | +import org.junit.Test; |
| 41 | + |
| 42 | +import java.util.concurrent.CountDownLatch; |
| 43 | +import java.util.concurrent.TimeUnit; |
| 44 | + |
| 45 | +import static org.junit.Assert.assertTrue; |
| 46 | +import static org.junit.Assume.assumeTrue; |
| 47 | + |
| 48 | +public class UIRenderSnapToPixelTest { |
| 49 | + private static final double scale = 1.25; |
| 50 | + private static CountDownLatch startupLatch; |
| 51 | + private static volatile Stage stage; |
| 52 | + private static final double epsilon = 0.00001; |
| 53 | + |
| 54 | + @BeforeClass |
| 55 | + public static void setupOnce() throws Exception { |
| 56 | + System.setProperty("glass.win.uiScale", String.valueOf(scale)); |
| 57 | + System.setProperty("glass.gtk.uiScale", String.valueOf(scale)); |
| 58 | + startupLatch = new CountDownLatch(1); |
| 59 | + new Thread(() -> Application.launch(TestApp.class, (String[]) null)).start(); |
| 60 | + assertTrue("Timeout waiting for FX runtime to start", startupLatch.await(15, TimeUnit.SECONDS)); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterClass |
| 64 | + public static void teardown() { |
| 65 | + Platform.runLater(stage::hide); |
| 66 | + Platform.exit(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testScrollPaneSnapChildrenToPixels() { |
| 71 | + assumeTrue(PlatformUtil.isLinux() || PlatformUtil.isWindows()); |
| 72 | + |
| 73 | + Assert.assertEquals("Wrong render scale", scale, stage.getRenderScaleY(), 0.0001); |
| 74 | + |
| 75 | + for (Node node : stage.getScene().getRoot().getChildrenUnmodifiable()) { |
| 76 | + if (node instanceof ScrollPane) { |
| 77 | + var sp = (ScrollPane) node; |
| 78 | + Assert.assertEquals("Top inset not snapped to pixel", 0, ((sp.snappedTopInset() * scale) + epsilon) % 1, 0.0001); |
| 79 | + Assert.assertEquals("Bottom inset not snapped to pixel", 0, ((sp.snappedBottomInset() * scale) + epsilon) % 1, 0.0001); |
| 80 | + Assert.assertEquals("Left inset not snapped to pixel", 0, ((sp.snappedLeftInset() * scale) + epsilon) % 1, 0.0001); |
| 81 | + Assert.assertEquals("Right inset not snapped to pixel", 0, ((sp.snappedRightInset() * scale) + epsilon) % 1, 0.0001); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public static class TestApp extends Application { |
| 87 | + private static void run() { |
| 88 | + startupLatch.countDown(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void start(Stage primaryStage) throws Exception { |
| 93 | + final Label label = new Label("This text may appear blurry at some screen scale without the fix for JDK-8211294"); |
| 94 | + final ScrollPane scrollpane = new ScrollPane(label); |
| 95 | + scrollpane.setSnapToPixel(true); |
| 96 | + final VBox root = new VBox(); |
| 97 | + root.getChildren().add(new Label("This text should be sharp at all screen scale")); |
| 98 | + root.getChildren().add(scrollpane); |
| 99 | + final Scene scene = new Scene(root); |
| 100 | + primaryStage.setScene(scene); |
| 101 | + stage = primaryStage; |
| 102 | + stage.addEventHandler(WindowEvent.WINDOW_SHOWN, e -> Platform.runLater(TestApp::run)); |
| 103 | + stage.show(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments