26 package test.javafx.embed.swing;
27
28 import java.awt.image.BufferedImage;
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.TimeUnit;
31 import javafx.application.Application;
32 import javafx.application.Platform;
33 import javafx.embed.swing.SwingFXUtils;
34 import javafx.scene.image.Image;
35 import javafx.scene.image.PixelFormat;
36 import javafx.scene.image.PixelReader;
37 import javafx.stage.Stage;
38 import junit.framework.AssertionFailedError;
39 import org.junit.AfterClass;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNotNull;
42 import static org.junit.Assert.assertTrue;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45
46 public class SwingFXUtilsTest {
47 static final boolean verbose = false;
48
49 // Used to launch the application before running any test
50 private static final CountDownLatch launchLatch = new CountDownLatch(1);
51
52 // Application class. An instance is created and initialized before running
53 // the first test, and it lives through the execution of all tests.
54 public static class MyApp extends Application {
55 @Override
56 public void start(Stage primaryStage) throws Exception {
57 Platform.setImplicitExit(false);
58 assertTrue(Platform.isFxApplicationThread());
59 assertNotNull(primaryStage);
60
61 launchLatch.countDown();
62 }
63 }
64
65 @BeforeClass
66 public static void doSetupOnce() {
67 // Start the Application
68 new Thread(() -> Application.launch(MyApp.class, (String[]) null)).start();
69
70 try {
71 if (!launchLatch.await(5000, TimeUnit.MILLISECONDS)) {
72 throw new AssertionFailedError("Timeout waiting for Application to launch");
73 }
74 } catch (InterruptedException ex) {
75 AssertionFailedError err = new AssertionFailedError("Unexpected exception");
76 err.initCause(ex);
77 throw err;
78 }
79
80 assertEquals(0, launchLatch.getCount());
81 }
82
83 @AfterClass
84 public static void doTeardownOnce() {
85 Platform.exit();
86 }
87
88 @Test
89 public void testFromFXImg() {
90 testFromFXImg("alpha.png");
91 testFromFXImg("opaque.gif");
92 testFromFXImg("opaque.jpg");
93 testFromFXImg("opaque.png");
94 testFromFXImg("trans.gif");
95 }
96
97 static void testFromFXImg(String imgfilename) {
98 Image img = new Image("test/javafx/embed/swing/"+imgfilename);
99 boolean rgbrequired = (img.getPixelReader().getPixelFormat().getType() == PixelFormat.Type.BYTE_RGB);
100 BufferedImage bimg = SwingFXUtils.fromFXImage(img, null);
101 checkBimg(img, bimg);
102 boolean reusesitself = reusesBimg(img, bimg, true);
103 boolean reusesxrgb = reusesBimg(img, BufferedImage.TYPE_INT_RGB, rgbrequired);
104 boolean reusesargb = reusesBimg(img, BufferedImage.TYPE_INT_ARGB, true);
105 boolean reusesargbpre = reusesBimg(img, BufferedImage.TYPE_INT_ARGB_PRE, true);
|
26 package test.javafx.embed.swing;
27
28 import java.awt.image.BufferedImage;
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.TimeUnit;
31 import javafx.application.Application;
32 import javafx.application.Platform;
33 import javafx.embed.swing.SwingFXUtils;
34 import javafx.scene.image.Image;
35 import javafx.scene.image.PixelFormat;
36 import javafx.scene.image.PixelReader;
37 import javafx.stage.Stage;
38 import junit.framework.AssertionFailedError;
39 import org.junit.AfterClass;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNotNull;
42 import static org.junit.Assert.assertTrue;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45
46
47 public class SwingFXUtilsTest {
48 static final boolean verbose = false;
49
50 // Used to launch the application before running any test
51 private static final CountDownLatch launchLatch = new CountDownLatch(1);
52
53
54 @BeforeClass
55 public static void doSetupOnce() {
56 JFXPanelTest.doSetupOnce();
57 }
58
59 @Test
60 public void testFromFXImg() {
61 testFromFXImg("alpha.png");
62 testFromFXImg("opaque.gif");
63 testFromFXImg("opaque.jpg");
64 testFromFXImg("opaque.png");
65 testFromFXImg("trans.gif");
66 }
67
68 static void testFromFXImg(String imgfilename) {
69 Image img = new Image("test/javafx/embed/swing/"+imgfilename);
70 boolean rgbrequired = (img.getPixelReader().getPixelFormat().getType() == PixelFormat.Type.BYTE_RGB);
71 BufferedImage bimg = SwingFXUtils.fromFXImage(img, null);
72 checkBimg(img, bimg);
73 boolean reusesitself = reusesBimg(img, bimg, true);
74 boolean reusesxrgb = reusesBimg(img, BufferedImage.TYPE_INT_RGB, rgbrequired);
75 boolean reusesargb = reusesBimg(img, BufferedImage.TYPE_INT_ARGB, true);
76 boolean reusesargbpre = reusesBimg(img, BufferedImage.TYPE_INT_ARGB_PRE, true);
|