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.javafx.scene.bounds;
  27 
  28 import static test.com.sun.javafx.test.TestHelper.box;
  29 import static org.junit.Assert.assertEquals;
  30 import javafx.scene.Group;
  31 import javafx.scene.shape.Polygon;
  32 import javafx.scene.shape.Rectangle;
  33 
  34 import org.junit.Test;
  35 
  36 public class ClipBoundsTest {
  37     
  38     /***************************************************************************
  39      *                                                                         *
  40      *                          Clipping and Effects                           *
  41      *                                                                         *
  42      *  These tests build upon the simple bounds tests by adding clipping and  *
  43      *  effects into the mix. These tests assume that boundsInLocal, without   *
  44      *  any effects or clipping, is correct.                                   *
  45      *                                                                         *
  46     /**************************************************************************/
  47 
  48     // a simple test that clipping a rectangle with another rectangle gives
  49     // the appropriate boundsInLocal
  50     public @Test void testClippingWithRectangle() {
  51         // the intersection of the clip & rect will be 10, 10, 90, 90
  52         Rectangle rect = new Rectangle(100, 100);
  53         Rectangle clip = new Rectangle(10, 10, 100, 100);
  54         rect.setClip(clip);
  55 
  56         assertEquals(box(10, 10, 90, 90), rect.getBoundsInLocal());
  57         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
  58     }
  59     
  60     // test clipping with a group clips according to the groups bounds
  61     public @Test void testClippingWithGroup() {
  62         Rectangle r1 = new Rectangle(20, 20, 50, 50);
  63         Rectangle r2 = new Rectangle(90, 20, 50, 50);
  64         Group group = new Group(r1, r2);
  65         Rectangle rect = new Rectangle(100, 100);
  66         rect.setClip(group);
  67         
  68         assertEquals(box(20, 20, 80, 50), rect.getBoundsInLocal());
  69         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
  70     }
  71     
  72     // test clipping with a node that is also clipped yields the correct bounds
  73     public @Test void testClippingWithClippedRectangle() {
  74         // clip's bounds should be 20, 20, 50, 50
  75         Rectangle clip = new Rectangle(10, 10, 100, 100);
  76         Rectangle clipClip = new Rectangle(20, 20, 50, 50);
  77         clip.setClip(clipClip);
  78         Rectangle rect = new Rectangle(40, 40);
  79         rect.setClip(clip);
  80 
  81         assertEquals(box(20, 20, 20, 20), rect.getBoundsInLocal());
  82         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
  83     }
  84 
  85     // test clipping with a clipped node where the subclip changes
  86     public @Test void testSwappingSubClipOnClippingRectangle() {
  87         // clip's bounds should be 20, 20, 50, 50
  88         Rectangle clip = new Rectangle(10, 10, 100, 100);
  89         Rectangle clipClip = new Rectangle(20, 20, 50, 50);
  90         clip.setClip(clipClip);
  91         Rectangle rect = new Rectangle(40, 40);
  92         rect.setClip(clip);
  93 
  94         assertEquals(box(20, 20, 20, 20), rect.getBoundsInLocal());
  95         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
  96 
  97         // testing that changing clip's clip changes bounds
  98         Rectangle clipClip2 = new Rectangle(30, 30, 50, 50);
  99         clip.setClip(clipClip2);
 100 
 101         assertEquals(box(30, 30, 10, 10), rect.getBoundsInLocal());
 102         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 103     }
 104 
 105     // test clipping with a node with a complex clip yields the correct bounds
 106     public @Test void testClippingWithComplexClippedRectangle() {
 107         // clip's bounds should be 20, 20, 50, 50
 108         Rectangle clip = new Rectangle(10, 10, 100, 100);
 109         Polygon polyClip = new Polygon(new double[] {
 110             30, 25, 35, 30, 30, 35, 25, 30,
 111         });
 112         clip.setClip(polyClip);
 113         Rectangle rect = new Rectangle(40, 40);
 114         rect.setClip(clip);
 115 
 116         assertEquals(box(25, 25, 10, 10), rect.getBoundsInLocal());
 117         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 118     }
 119 
 120     // test clipping with an image results in the right bounds
 121     // test clipping with text results in the right bounds
 122     
 123     // test changing the clips bounds also changes the bounds of the clip parent
 124     public @Test void testChangingClipBounds() {
 125         Rectangle clip = new Rectangle(50, 50);
 126         Rectangle rect = new Rectangle(100, 100);
 127         rect.setClip(clip);
 128 
 129         assertEquals(box(0, 0, 50, 50), rect.getBoundsInLocal());
 130         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 131         
 132         clip.setWidth(60);
 133         clip.setHeight(60);
 134         assertEquals(box(0, 0, 60, 60), rect.getBoundsInLocal());
 135         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());        
 136     }
 137     
 138     // test setting a clip changes the bounds
 139     public @Test void testSettingClip() {
 140         Rectangle rect = new Rectangle(100, 100);
 141 
 142         assertEquals(box(0, 0, 100, 100), rect.getBoundsInLocal());
 143         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 144         
 145         rect.setClip(new Rectangle(50, 50));
 146 
 147         assertEquals(box(0, 0, 50, 50), rect.getBoundsInLocal());
 148         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());        
 149     }
 150     
 151     // test swapping clips changes the bounds
 152     public @Test void testSwappingClip() {
 153         Rectangle clip = new Rectangle(50, 50);
 154         Rectangle rect = new Rectangle(100, 100);
 155         rect.setClip(clip);
 156         
 157         assertEquals(box(0, 0, 50, 50), rect.getBoundsInLocal());
 158         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 159         
 160         rect.setClip(new Rectangle(10, 10, 50, 50));
 161         assertEquals(box(10, 10, 50, 50), rect.getBoundsInLocal());
 162         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 163     }
 164     
 165     // test removing the clip changes the bounds
 166     public @Test void testRemovingClip() {
 167         Rectangle clip = new Rectangle(50, 50);
 168         Rectangle rect = new Rectangle(100, 100);
 169         rect.setClip(clip);
 170         assertEquals(box(0, 0, 50, 50), rect.getBoundsInLocal());
 171         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 172         
 173         rect.setClip(null);
 174         assertEquals(box(0, 0, 100, 100), rect.getBoundsInLocal());
 175         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 176     }
 177     
 178     // test setting the clip and changing the geom of the Node give right bounds
 179     public @Test void testClippingAndChangingGeometry() {
 180         Rectangle clip = new Rectangle(50, 50);
 181         Rectangle rect = new Rectangle(100, 100);
 182         rect.setClip(clip);
 183         assertEquals(box(0, 0, 50, 50), rect.getBoundsInLocal());
 184         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 185         
 186         rect.setWidth(20);
 187         assertEquals(box(0, 0, 20, 50), rect.getBoundsInLocal());
 188         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 189     }
 190 
 191 }