|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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.web; |
| 27 | + |
| 28 | +import javafx.application.Application; |
| 29 | +import javafx.application.Platform; |
| 30 | +import javafx.concurrent.Worker; |
| 31 | +import javafx.scene.Scene; |
| 32 | +import javafx.scene.web.WebView; |
| 33 | +import javafx.stage.Stage; |
| 34 | +import org.junit.jupiter.api.AfterAll; |
| 35 | +import org.junit.jupiter.api.BeforeAll; |
| 36 | +import org.junit.jupiter.api.BeforeEach; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import test.util.Util; |
| 39 | + |
| 40 | +import java.util.concurrent.CountDownLatch; |
| 41 | + |
| 42 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 45 | + |
| 46 | +public class CSSRoundingTest { |
| 47 | + |
| 48 | + private static final CountDownLatch launchLatch = new CountDownLatch(1); |
| 49 | + |
| 50 | + // Maintain one application instance |
| 51 | + static CSSRoundingTestApp cssRoundingTestApp; |
| 52 | + public static Stage primaryStage; |
| 53 | + public static WebView webView; |
| 54 | + |
| 55 | + public static class CSSRoundingTestApp extends Application { |
| 56 | + |
| 57 | + @Override |
| 58 | + public void init() { |
| 59 | + CSSRoundingTest.cssRoundingTestApp = this; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void start(Stage primaryStage) throws Exception { |
| 64 | + CSSRoundingTest.primaryStage = primaryStage; |
| 65 | + webView = new WebView(); |
| 66 | + Scene scene = new Scene(webView, 150, 100); |
| 67 | + primaryStage.setScene(scene); |
| 68 | + primaryStage.show(); |
| 69 | + launchLatch.countDown(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @BeforeAll |
| 74 | + public static void setupOnce() { |
| 75 | + Util.launch(launchLatch, CSSRoundingTestApp.class); |
| 76 | + } |
| 77 | + |
| 78 | + @AfterAll |
| 79 | + public static void tearDownOnce() { |
| 80 | + Util.shutdown(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testCSSroundingForLinearLayout() { |
| 85 | + |
| 86 | + final CountDownLatch webViewStateLatch = new CountDownLatch(1); |
| 87 | + |
| 88 | + Util.runAndWait(() -> { |
| 89 | + assertNotNull(webView); |
| 90 | + |
| 91 | + webView.getEngine().getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> { |
| 92 | + if (newValue == Worker.State.SUCCEEDED) { |
| 93 | + webView.requestFocus(); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + webView.focusedProperty().addListener((observable, oldValue, newValue) -> { |
| 98 | + if (newValue) { |
| 99 | + webViewStateLatch.countDown(); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + String content = """ |
| 104 | + <html> |
| 105 | + <head> |
| 106 | + <style type="text/css"> |
| 107 | + body, div { |
| 108 | + margin: 0; |
| 109 | + padding: 0; |
| 110 | + border: 0; |
| 111 | + } |
| 112 | + #top, #bottom { |
| 113 | + line-height: 1.5; |
| 114 | + font-size: 70%; |
| 115 | + background:green; |
| 116 | + color:white; |
| 117 | + width:100%; |
| 118 | + } |
| 119 | + #top { |
| 120 | + padding:.6em 0 .7em; |
| 121 | + } |
| 122 | + #bottom { |
| 123 | + position:absolute; |
| 124 | + top:2.8em; |
| 125 | + } |
| 126 | + </style> |
| 127 | + </head> |
| 128 | + <body> |
| 129 | + <div id="top">no gap below</div> |
| 130 | + <div id="bottom">no gap above</div> |
| 131 | + <div id="description"></div> |
| 132 | + <div id="console"></div> |
| 133 | + <script> |
| 134 | + description("This test checks that floating point rounding doesn't cause misalignment. There should be no gap between the divs."); |
| 135 | + var divtop = document.getElementById("top").getBoundingClientRect(); |
| 136 | + var divbottom = document.getElementById("bottom").getBoundingClientRect(); |
| 137 | + console.log("divtop.bottom: " + divtop.bottom); |
| 138 | + console.log("divbottom.top: " + divbottom.top); |
| 139 | + window.testResults = { topBottom: Math.round(divtop.bottom), bottomTop: Math.round(divbottom.top) }; |
| 140 | + </script> |
| 141 | + </body> |
| 142 | + </html> |
| 143 | + """; |
| 144 | + webView.getEngine().loadContent(content); |
| 145 | + }); |
| 146 | + |
| 147 | + assertTrue(Util.await(webViewStateLatch), "Timeout when waiting for focus change"); |
| 148 | + // Introduce sleep to ensure web contents are loaded |
| 149 | + Util.sleep(1000); |
| 150 | + |
| 151 | + Util.runAndWait(() -> { |
| 152 | + webView.getEngine().executeScript(""" |
| 153 | + var divtop = document.getElementById("top").getBoundingClientRect(); |
| 154 | + var divbottom = document.getElementById("bottom").getBoundingClientRect(); |
| 155 | + var topBottom = Math.round(divtop.bottom); |
| 156 | + var bottomTop = Math.round(divbottom.top); |
| 157 | + window.testResults = { topBottom: topBottom, bottomTop: bottomTop }; |
| 158 | + """); |
| 159 | + |
| 160 | + int topBottom = ((Number) webView.getEngine().executeScript("window.testResults.topBottom")).intValue(); |
| 161 | + int bottomTop = ((Number) webView.getEngine().executeScript("window.testResults.bottomTop")).intValue(); |
| 162 | + |
| 163 | + assertEquals(31, topBottom); |
| 164 | + assertEquals(31, bottomTop); |
| 165 | + }); |
| 166 | + } |
| 167 | +} |
0 commit comments