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 helloworld;
  27 
  28 import javafx.scene.Group;
  29 import javafx.scene.Scene;
  30 import javafx.scene.paint.Color;
  31 import javafx.scene.shape.Rectangle;
  32 import javafx.stage.Stage;
  33 import org.junit.Test;
  34 import testharness.VisualTestBase;
  35 
  36 /**
  37  * Basic visual tests using glass Robot to sample pixels.
  38  */
  39 public class RectangleTest extends VisualTestBase {
  40 
  41     private Stage testStage;
  42     private Scene testScene;
  43 
  44     private static final double TOLERANCE = 0.07;
  45 
  46     @Test(timeout=5000)
  47     public void testSceneDefaultFill() {
  48         final int WIDTH = 400;
  49         final int HEIGHT = 300;
  50 
  51         runAndWait(() -> {
  52             testStage = getStage();
  53             testScene = new Scene(new Group(), WIDTH, HEIGHT);
  54             testStage.setScene(testScene);
  55             testStage.show();
  56         });
  57         waitFirstFrame();
  58         runAndWait(() -> {
  59             Color color = getColor(testScene, WIDTH / 2, HEIGHT / 2);
  60             assertColorEquals(Color.WHITE, color, TOLERANCE);
  61         });
  62     }
  63 
  64     @Test(timeout=5000)
  65     public void testSceneFillColor() {
  66         final int WIDTH = 400;
  67         final int HEIGHT = 300;
  68 
  69         runAndWait(() -> {
  70             testStage = getStage();
  71             testScene = new Scene(new Group(), WIDTH, HEIGHT);
  72             testScene.setFill(Color.CORNFLOWERBLUE);
  73             testStage.setScene(testScene);
  74             testStage.show();
  75         });
  76         waitFirstFrame();
  77         runAndWait(() -> {
  78             Color color = getColor(testScene, WIDTH / 2, HEIGHT / 2);
  79             assertColorEquals(Color.CORNFLOWERBLUE, color, TOLERANCE);
  80         });
  81     }
  82 
  83     @Test(timeout=5000)
  84     public void testFillRect() {
  85         final int WIDTH = 400;
  86         final int HEIGHT = 300;
  87         final int RECT_X = 200;
  88         final int RECT_Y = 70;
  89         final int RECT_W = 30;
  90         final int RECT_H = 60;
  91         final int OFFSET = 10;
  92 
  93         runAndWait(() -> {
  94             Rectangle rect = new Rectangle(RECT_X, RECT_Y, RECT_W, RECT_H);
  95             rect.setFill(Color.ORANGE);
  96             Group root = new Group(rect);
  97             testScene = new Scene(root, WIDTH, HEIGHT);
  98             testScene.setFill(Color.PALEGREEN);
  99 
 100             testStage = getStage();
 101             testStage.setScene(testScene);
 102             testStage.show();
 103         });
 104         waitFirstFrame();
 105         runAndWait(() -> {
 106             Color color = getColor(testScene, RECT_X - OFFSET, RECT_Y - OFFSET);
 107             assertColorEquals(Color.PALEGREEN, color, TOLERANCE);
 108             color = getColor(testScene, RECT_X + RECT_W + OFFSET, RECT_Y + RECT_H + OFFSET);
 109             assertColorEquals(Color.PALEGREEN, color, TOLERANCE);
 110             color = getColor(testScene, RECT_X + (RECT_W / 2), RECT_Y + (RECT_H / 2));
 111             assertColorEquals(Color.ORANGE, color, TOLERANCE);
 112         });
 113     }
 114 
 115     @Test(timeout=5000)
 116     public void testAddFillRect() {
 117         final int WIDTH = 400;
 118         final int HEIGHT = 300;
 119         final int RECT_X = 200;
 120         final int RECT_Y = 70;
 121         final int RECT_W = 30;
 122         final int RECT_H = 60;
 123         final int OFFSET = 10;
 124 
 125         runAndWait(() -> {
 126             Group root = new Group();
 127             testScene = new Scene(root, WIDTH, HEIGHT);
 128             testScene.setFill(Color.PALEGREEN);
 129 
 130             testStage = getStage();
 131             testStage.setScene(testScene);
 132             testStage.show();
 133         });
 134         waitFirstFrame();
 135         runAndWait(() -> {
 136             Color color = getColor(testScene, RECT_X - OFFSET, RECT_Y - OFFSET);
 137             assertColorEquals(Color.PALEGREEN, color, TOLERANCE);
 138             color = getColor(testScene, RECT_X + RECT_W + OFFSET, RECT_Y + RECT_H + OFFSET);
 139             assertColorEquals(Color.PALEGREEN, color, TOLERANCE);
 140             color = getColor(testScene, RECT_X + (RECT_W / 2), RECT_Y + (RECT_H / 2));
 141             assertColorEquals(Color.PALEGREEN, color, TOLERANCE);
 142         });
 143 
 144         // Now add a rectangle
 145         runAndWait(() -> {
 146             Rectangle rect = new Rectangle(RECT_X, RECT_Y, RECT_W, RECT_H);
 147             rect.setFill(Color.ORANGE);
 148             Group root = (Group)testScene.getRoot();
 149             root.getChildren().add(rect);
 150         });
 151         waitNextFrame();
 152         runAndWait(() -> {
 153             Color color = getColor(testScene, RECT_X - OFFSET, RECT_Y - OFFSET);
 154             assertColorEquals(Color.PALEGREEN, color, TOLERANCE);
 155             color = getColor(testScene, RECT_X + RECT_W + OFFSET, RECT_Y + RECT_H + OFFSET);
 156             assertColorEquals(Color.PALEGREEN, color, TOLERANCE);
 157             color = getColor(testScene, RECT_X + (RECT_W / 2), RECT_Y + (RECT_H / 2));
 158             assertColorEquals(Color.ORANGE, color, TOLERANCE);
 159         });
 160     }
 161 
 162 }