1 /*
   2  * Copyright (c) 2011, 2013, 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.com.sun.javafx.test;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.fail;
  30 
  31 import java.util.List;
  32 
  33 import javafx.geometry.BoundingBox;
  34 import javafx.geometry.Bounds;
  35 import javafx.scene.Group;
  36 import javafx.scene.Node;
  37 
  38 public class TestHelper {
  39 
  40     // tests that they are within 1px error of being the same. This is to
  41     // account
  42     // for stroke width
  43     public static void assertSimilar(Bounds expected, Bounds actual) {
  44         assertEquals("minX", expected.getMinX(), actual.getMinX(), 1);
  45         assertEquals("minY", expected.getMinY(), actual.getMinY(), 1);
  46         assertEquals("maxX", expected.getMaxX(), actual.getMaxX(), 1);
  47         assertEquals("maxY", expected.getMaxY(), actual.getMaxY(), 1);
  48     }
  49     
  50     public static final float EPSILON = 1.0e-4f;
  51 
  52     public static void assertAlmostEquals(float a, float b) {
  53         assertEquals(a, b, EPSILON);
  54     }
  55 
  56     public static void assertBoundsEqual(Bounds expected, Bounds actual) {
  57         if (expected.isEmpty() && actual.isEmpty()) {
  58             return;
  59         } else {
  60             assertEquals(expected, actual);
  61         }
  62     }
  63 
  64     public static void assertGroupBounds(Group g) {
  65         // first figure out what the 'expected' bounds are
  66         float x1 = 0;
  67         float y1 = 0;
  68         float x2 = -1;
  69         float y2 = -1;
  70         boolean first = true;
  71         for (Node n : g.getChildren()) {
  72             if (n.isVisible() && !n.getBoundsInLocal().isEmpty()) {
  73                 if (first) {
  74                     x1 = (float) n.getBoundsInParent().getMinX();
  75                     y1 = (float) n.getBoundsInParent().getMinY();
  76                     x2 = (float) n.getBoundsInParent().getMaxX();
  77                     y2 = (float) n.getBoundsInParent().getMaxY();
  78                     first = false;
  79                 } else {
  80                     x1 = Math.min(x1, (float) n.getBoundsInParent().getMinX());
  81                     y1 = Math.min(y1, (float) n.getBoundsInParent().getMinY());
  82                     x2 = Math.max(x2, (float) n.getBoundsInParent().getMaxX());
  83                     y2 = Math.max(y2, (float) n.getBoundsInParent().getMaxY());
  84                 }
  85             }
  86         }
  87         Bounds expected = box2(x1, y1, x2, y2);
  88         assertBoundsEqual(expected, g.getBoundsInLocal());
  89     }
  90 
  91     public static String formatBounds(Bounds b) {
  92         return "(" + b.getMinX() + ", " + b.getMinY() + ", " + b.getWidth()
  93                 + ", " + b.getHeight() + ")";
  94     }
  95 
  96     public static BoundingBox box(int minX, int minY, int width, int height) {
  97         return box((float) minX, (float) minY, (float) width, (float) height);
  98     }
  99 
 100     public static BoundingBox box(double minX, double minY, double width, double height) {
 101         return box((float) minX, (float) minY, (float) width, (float) height);
 102     }
 103 
 104     public static BoundingBox box(float minX, float minY, float width,
 105             float height) {
 106         return new BoundingBox(minX, minY, width, height);
 107     }
 108 
 109     public static BoundingBox box(float minX, float minY, float minZ, float width,
 110             float height, float depth) {
 111         return new BoundingBox(minX, minY, minZ, width, height, depth);
 112     }
 113     
 114     public static BoundingBox box2(int minX, int minY, int maxX, int maxY) {
 115         return box2((float) minX, (float) minY, (float) maxX, (float) maxY);
 116     }
 117 
 118     public static BoundingBox box2(float minX, float minY, float maxX,
 119             float maxY) {
 120         return box(minX, minY, maxX - minX, maxY - minY);
 121     }
 122     
 123     @SuppressWarnings({ "rawtypes", "unchecked" })
 124     public static void assertImmutableList(List list) {
 125         try {
 126             list.add(new Object());
 127             fail("Exception expected while modifying the list.");
 128         } catch (Exception e) {
 129             // expected
 130         }
 131     }
 132 }