|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, 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.image; |
| 27 | + |
| 28 | +import java.awt.Color; |
| 29 | +import java.awt.Graphics2D; |
| 30 | +import java.awt.image.BufferedImage; |
| 31 | +import java.awt.image.DataBuffer; |
| 32 | +import java.awt.image.DataBufferInt; |
| 33 | +import java.io.ByteArrayOutputStream; |
| 34 | +import java.io.PrintStream; |
| 35 | +import java.nio.IntBuffer; |
| 36 | +import java.util.concurrent.CountDownLatch; |
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | + |
| 39 | +import org.junit.AfterClass; |
| 40 | +import org.junit.Assert; |
| 41 | +import org.junit.Before; |
| 42 | +import org.junit.BeforeClass; |
| 43 | +import org.junit.Test; |
| 44 | + |
| 45 | +import javafx.application.Application; |
| 46 | +import javafx.application.Platform; |
| 47 | +import javafx.geometry.Rectangle2D; |
| 48 | +import javafx.scene.Scene; |
| 49 | +import javafx.scene.image.ImageView; |
| 50 | +import javafx.scene.image.PixelBuffer; |
| 51 | +import javafx.scene.image.PixelFormat; |
| 52 | +import javafx.scene.image.WritableImage; |
| 53 | +import javafx.scene.layout.StackPane; |
| 54 | +import javafx.stage.Stage; |
| 55 | +import javafx.stage.WindowEvent; |
| 56 | +import test.util.Util; |
| 57 | + |
| 58 | +/** |
| 59 | + * This test verifies the fix for JDK-8229890. |
| 60 | + * This test ensures that the callback provided to the {@link javafx.scene.image.PixelBuffer#updateBuffer()} |
| 61 | + * method may return {@link javafx.geometry.Rectangle2D.Empty} in order to |
| 62 | + * indicate that no update is necessary and no exception is thrown. |
| 63 | + */ |
| 64 | +public class WritableImageFromBufferTest { |
| 65 | + |
| 66 | + static CountDownLatch startupLatch; |
| 67 | + |
| 68 | + private static final int IMG_WIDTH = 600; |
| 69 | + private static final int IMG_HEIGHT = 400; |
| 70 | + |
| 71 | + private PixelBuffer<IntBuffer> pixelBuffer; |
| 72 | + private Graphics2D g2d; |
| 73 | + private WritableImage fxImage; |
| 74 | + |
| 75 | + private static Scene scene; |
| 76 | + |
| 77 | + public static class TestApp extends Application { |
| 78 | + @Override |
| 79 | + public void start(Stage primaryStage) throws Exception { |
| 80 | + scene = new Scene(new StackPane(), IMG_WIDTH, IMG_HEIGHT); |
| 81 | + primaryStage.addEventHandler(WindowEvent.WINDOW_SHOWN, event -> startupLatch.countDown()); |
| 82 | + primaryStage.setScene(scene); |
| 83 | + primaryStage.show(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Before |
| 88 | + public void setUp() throws Exception { |
| 89 | + BufferedImage awtImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_ARGB_PRE); |
| 90 | + g2d = (Graphics2D) awtImage.getGraphics(); |
| 91 | + |
| 92 | + DataBuffer db = awtImage.getRaster().getDataBuffer(); |
| 93 | + DataBufferInt dbi = (DataBufferInt) db; |
| 94 | + int[] rawInts = dbi.getData(); |
| 95 | + IntBuffer ib = IntBuffer.wrap(rawInts); |
| 96 | + |
| 97 | + PixelFormat<IntBuffer> pixelFormat = PixelFormat.getIntArgbPreInstance(); |
| 98 | + pixelBuffer = new PixelBuffer<>(IMG_WIDTH, IMG_HEIGHT, ib, pixelFormat); |
| 99 | + fxImage = new WritableImage(pixelBuffer); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void test() throws InterruptedException { |
| 104 | + PrintStream defaultErrorStream = System.err; |
| 105 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 106 | + System.setErr(new PrintStream(out, true)); |
| 107 | + |
| 108 | + Thread.sleep(1000); |
| 109 | + Util.runAndWait(() -> { |
| 110 | + StackPane root = (StackPane)scene.getRoot(); |
| 111 | + root.getChildren().add(new ImageView(fxImage)); |
| 112 | + requestFullUpdate(); |
| 113 | + }); |
| 114 | + Thread.sleep(100); |
| 115 | + Util.runAndWait(() -> { |
| 116 | + requestEmptyUpdate(); // This will fail without the fix. |
| 117 | + }); |
| 118 | + Thread.sleep(100); |
| 119 | + Util.runAndWait(() -> { |
| 120 | + requestPartialUpdate(); |
| 121 | + }); |
| 122 | + Thread.sleep(100); |
| 123 | + |
| 124 | + System.setErr(defaultErrorStream); |
| 125 | + Assert.assertEquals("No error should be thrown", "", out.toString()); |
| 126 | + } |
| 127 | + |
| 128 | + private void requestFullUpdate() { |
| 129 | + // This call should work before and after the fix. |
| 130 | + pixelBuffer.updateBuffer(pb -> { |
| 131 | + g2d.setBackground(Color.decode("#FF0000")); |
| 132 | + g2d.clearRect(0, 0, IMG_WIDTH, IMG_HEIGHT); |
| 133 | + return null; |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + private void requestEmptyUpdate() { |
| 138 | + // This call should fail without the fix and pass after the fix. |
| 139 | + pixelBuffer.updateBuffer(pb -> { |
| 140 | + // Nothing to do. |
| 141 | + return Rectangle2D.EMPTY; |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + private void requestPartialUpdate() { |
| 146 | + // This call should work before and after the fix. |
| 147 | + pixelBuffer.updateBuffer(pb -> { |
| 148 | + g2d.setBackground(Color.decode("#0000FF")); |
| 149 | + g2d.clearRect(0, 0, IMG_WIDTH / 2, IMG_HEIGHT); |
| 150 | + return new Rectangle2D(0, 0, IMG_WIDTH / 2, IMG_HEIGHT); |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + @BeforeClass |
| 155 | + public static void initFX() throws Exception { |
| 156 | + startupLatch = new CountDownLatch(1); |
| 157 | + new Thread(() -> Application.launch(TestApp.class, (String[])null)).start(); |
| 158 | + Assert.assertTrue("Timeout waiting for FX runtime to start", |
| 159 | + startupLatch.await(15, TimeUnit.SECONDS)); |
| 160 | + } |
| 161 | + |
| 162 | + @AfterClass |
| 163 | + public static void tearDown() { |
| 164 | + Platform.exit(); |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments