1 /*
   2  * Copyright (c) 2016, 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.embed.swing;
  27 
  28 import javafx.application.Application;
  29 import javafx.application.Platform;
  30 import javafx.embed.swing.SwingNode;
  31 import javafx.scene.Scene;
  32 import javafx.scene.layout.StackPane;
  33 import javafx.stage.Stage;
  34 import org.junit.Assert;
  35 import org.junit.BeforeClass;
  36 import org.junit.AfterClass;
  37 import org.junit.Test;
  38 import junit.framework.AssertionFailedError;
  39 import javax.swing.*;
  40 import java.awt.*;
  41 import java.awt.event.FocusAdapter;
  42 import java.awt.event.FocusEvent;
  43 import java.util.concurrent.CountDownLatch;
  44 import java.util.concurrent.TimeUnit;
  45 
  46 import static test.util.Util.TIMEOUT;
  47 
  48 public class SwingNodeScaleTest {
  49     static CountDownLatch launchLatch;
  50     static Dimension request;
  51     static Rectangle result;
  52     static JButton b;
  53 
  54     @BeforeClass
  55     public static void setupOnce() {
  56         System.setProperty("glass.win.uiScale", "2");
  57         System.setProperty("glass.gtk.uiScale", "2");
  58         launchLatch = new CountDownLatch(1);
  59         request = new Dimension(150, 100);
  60         // Start the Application
  61         new Thread(() -> Application.launch(SwingNodeScaleTest.MyApp.class, (String[])null)).start();
  62 
  63         try {
  64             if (!launchLatch.await(5 * TIMEOUT, TimeUnit.MILLISECONDS)) {
  65                 throw new AssertionFailedError("Timeout waiting for Application to launch ("+
  66                     (5 * TIMEOUT) + " seconds)");
  67             }
  68         } catch (InterruptedException ex) {
  69             AssertionFailedError err = new AssertionFailedError("Unexpected exception");
  70             err.initCause(ex);
  71             throw err;
  72         }
  73     }
  74 
  75     @AfterClass
  76     public static void teardownOnce() {
  77         Platform.exit();
  78     }
  79 
  80     @Test
  81     public void testScale() throws Exception {
  82         SwingUtilities.invokeAndWait(() -> result = b.getBounds());
  83         System.out.println(result);
  84         Assert.assertEquals(2 * request.width, 2 * result.x + result.width);
  85         Assert.assertEquals(2 * request.height, 2 * result.y + result.height);
  86     }
  87 
  88     public static class MyApp extends Application {
  89         SwingNode node;
  90 
  91         @Override
  92         public void start(Stage stage) throws Exception {
  93             StackPane root = new StackPane();
  94             stage.setScene(new Scene(root, 2 * request.width,
  95                     2 * request.height));
  96 
  97             SwingUtilities.invokeLater(() -> {
  98                 node = new SwingNode();
  99                 JPanel panel = new JPanel();
 100                 panel.setLayout(new GridBagLayout());
 101                 b = new JButton("Swing Button");
 102                 b.setIcon(UIManager.getDefaults()
 103                         .getIcon("FileView.computerIcon"));
 104                 b.setPreferredSize(request);
 105                 b.addFocusListener(new FocusAdapter() {
 106                     @Override
 107                     public void focusGained(FocusEvent e) {
 108                         launchLatch.countDown();
 109                     }
 110                 });
 111                 panel.add(b);
 112                 node.setContent(panel);
 113 
 114                 Platform.runLater(() -> {
 115                     root.getChildren().add(node);
 116                     stage.show();
 117                 });
 118             });
 119 
 120         }
 121     }
 122 
 123 }