1 /*
   2  * Copyright (c) 2013, 2014, 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 javafx.embed.swing;
  27 
  28 import javafx.application.Platform;
  29 import javafx.scene.Scene;
  30 import javafx.scene.layout.Region;
  31 
  32 import java.awt.AWTException;
  33 import java.awt.Color;
  34 import java.awt.Dimension;
  35 import java.awt.Point;
  36 import java.awt.Robot;
  37 import java.awt.Toolkit;
  38 
  39 import javax.swing.JFrame;
  40 import javax.swing.SwingUtilities;
  41 
  42 import sun.awt.SunToolkit;
  43 
  44 import java.util.concurrent.CountDownLatch;
  45 import java.util.concurrent.TimeUnit;
  46 
  47 import javafx.animation.AnimationTimer;
  48 import junit.framework.Assert;
  49 
  50 import org.junit.Test;
  51 import org.junit.Ignore;
  52 
  53 /**
  54  * RT-23603: WebView does not display in JFXPanel on initialization
  55  */
  56 @Ignore("RT-29515")
  57 public class RT23603Test {
  58     volatile JFrame frame;
  59     final CountDownLatch l1 = new CountDownLatch(2);
  60 
  61     @Test
  62     public void test() {
  63         SwingUtilities.invokeLater(this::initAndShowGUI);
  64 
  65         //
  66         // wait for frame to be set visible and jfxpanel to be installed
  67         //
  68         waitForLatch(l1, 5000);
  69 
  70         //
  71         // wait for frame to become really visible
  72         //
  73         ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
  74 
  75         // 3 pulses should guarantee the scene is rendered
  76         final CountDownLatch l2 = new CountDownLatch(3);
  77         com.sun.javafx.tk.Toolkit.getToolkit().addSceneTkPulseListener(l2::countDown);
  78 
  79         //
  80         // wait for jfxpanel to be rendered
  81         //
  82         waitForLatch(l2, 5000);
  83 
  84         //
  85         // finally, check that jfxpanel is really visible
  86         //
  87         Robot r = null;
  88         try {
  89             r = new Robot();
  90         } catch (AWTException ex) {
  91             Assert.fail("unexpected error: couldn't create java.awt.Robot: " + ex);
  92         }
  93         Point pt = frame.getLocationOnScreen();
  94         Color color = r.getPixelColor(pt.x + 100, pt.y + 100);
  95         Assert.assertEquals(color, Color.GREEN);
  96     }
  97 
  98     private void waitForLatch(CountDownLatch latch, long ms) {
  99         try {
 100             latch.await(ms, TimeUnit.MILLISECONDS);
 101         } catch (InterruptedException e) {
 102         }
 103         if (latch.getCount() > 0) {
 104             Assert.fail("unexpected error: waiting timeout " + ms + "ms elapsed for " + latch);
 105         }
 106     }
 107 
 108     public void initAndShowGUI() {
 109         frame = new JFrame("RT23603");
 110 
 111         final JFXPanel fxPanel = new JFXPanel();
 112 
 113         Platform.runLater(() -> {
 114             Region rgn = new Region();
 115             Scene scene = new Scene(rgn);
 116             rgn.setStyle("-fx-background-color: #00ff00;");
 117             fxPanel.setScene(scene);
 118 
 119             // start constant pulse activity
 120             new AnimationTimer() {
 121                 @Override public void handle(long l) {}
 122             }.start();
 123 
 124             l1.countDown(); // jfxpanel is installed
 125         });
 126 
 127         frame.getContentPane().setBackground(java.awt.Color.RED);
 128         frame.getContentPane().setPreferredSize(new Dimension(400, 300));
 129         frame.pack();
 130 
 131         fxPanel.setSize(400, 300);
 132 
 133         frame.getContentPane().add(fxPanel);
 134         frame.getContentPane().remove(fxPanel);
 135         frame.getContentPane().add(fxPanel);
 136         frame.setVisible(true);
 137 
 138         l1.countDown(); // frame is set visible
 139     }
 140 
 141     public static void main(String[] args) {
 142         new RT23603Test().test();
 143     }
 144 }