1 /*
   2  * Copyright (c) 2011, 2015, 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.scene.web;
  27 
  28 import com.sun.javafx.util.Utils;
  29 import javafx.geometry.Rectangle2D;
  30 import javafx.scene.Group;
  31 import javafx.scene.Node;
  32 import javafx.scene.Scene;
  33 import javafx.stage.Screen;
  34 import javafx.stage.Stage;
  35 import org.junit.Test;
  36 import static org.junit.Assert.*;
  37 import netscape.javascript.*;
  38 
  39 public class ScreenAndWindowTest extends TestBase {
  40 
  41     // called on FX thread
  42     private void checkScreenProperties(Rectangle2D screenSize, Rectangle2D availSize) {
  43         JSObject screen = (JSObject) getEngine().executeScript("screen");
  44         int depth = (Integer) screen.getMember("colorDepth");
  45         int width = (Integer) screen.getMember("width");
  46         int height = (Integer) screen.getMember("height");
  47         int availWidth = (Integer) screen.getMember("availWidth");
  48         int availHeight = (Integer) screen.getMember("availHeight");
  49 
  50         assertEquals("screen.width", (int)screenSize.getWidth(), width);
  51         assertEquals("screen.height", (int)screenSize.getHeight(), height);
  52         assertEquals("screen.availWidth", (int)availSize.getWidth(), availWidth);
  53         assertEquals("screen.availHeight", (int)availSize.getHeight(), availHeight);
  54         
  55         // do some basic checking, too
  56         assertTrue("screen.depth >= 0", depth >= 0);
  57         assertTrue("screen.width >= screen.availWidth", width >= availWidth);
  58         assertTrue("screen.height >= screen.availHeight", height >= availHeight);
  59     }
  60 
  61     // called on FX thread
  62     private void checkWindowProperties(int windowWidth, int windowHeight) {
  63         JSObject window = (JSObject)getEngine().executeScript("window");
  64         int innerWidth = (Integer)window.getMember("innerWidth");
  65         int innerHeight = (Integer)window.getMember("innerHeight");
  66         int outerWidth = (Integer)window.getMember("outerWidth");
  67         int outerHeight = (Integer)window.getMember("outerHeight");
  68 
  69         if (windowWidth >= 0) {
  70             assertEquals("window.outerWidth", windowWidth, outerWidth);
  71         }
  72         if (windowHeight >= 0) {
  73             assertEquals("window.outerHeight", windowHeight, outerHeight);
  74         }
  75         
  76         // do some sanity checks
  77         assertTrue("window.outerWidth >= window.innerWidth", outerWidth >= innerWidth);
  78         assertTrue("window.outerHeight >= window.innerHeight", outerHeight >= innerHeight);
  79     }
  80 
  81     // called on FX thread
  82     private void checkProperties(Rectangle2D screenSize, Rectangle2D availSize,
  83                                 int windowWidth, int windowHeight) {
  84         checkScreenProperties(screenSize, availSize);
  85         checkWindowProperties(windowWidth, windowHeight);
  86     }
  87 
  88     /**
  89      * Checks that no exceptions, crashes etc occur when accessing
  90      * screen.* and window.* properties from inside a WebEngine.
  91      */
  92     @Test public void test() throws InterruptedException {
  93         submit(new Runnable() { public void run() {
  94             Node view = getView();
  95 
  96             // test WebView not added to a Scene
  97             checkWindowProperties(-1, -1);
  98 
  99             // add WebView to a Scene with no Window
 100             Scene scene = new Scene(new Group(view));
 101             checkWindowProperties(-1, -1);
 102 
 103             // add Scene to a 0x0 Window
 104             Stage stage = new Stage();
 105             stage.setScene(scene);
 106             stage.setWidth(0);
 107             stage.setHeight(0);
 108 
 109             Screen screen = Utils.getScreen(view);
 110             Rectangle2D screenSize = screen.getBounds();
 111             Rectangle2D availSize = screen.getVisualBounds();
 112 
 113             checkProperties(screenSize, availSize, 0, 0);
 114 
 115             // resize the Window
 116             stage.setWidth(400);
 117             stage.setHeight(300);
 118             checkProperties(screenSize, availSize, 400, 300);
 119         }});
 120     }
 121 }