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;
  27 
  28 import javafx.scene.Group;
  29 import javafx.scene.Scene;
  30 import javafx.scene.SceneShim;
  31 import static org.junit.Assert.assertFalse;
  32 import static org.junit.Assert.assertNull;
  33 import static org.junit.Assert.assertSame;
  34 import static org.junit.Assert.assertTrue;
  35 import javafx.scene.shape.Circle;
  36 import javafx.scene.shape.Rectangle;
  37 
  38 import org.junit.Test;
  39 
  40 /**
  41  * Tests various aspects of Picking.
  42  *
  43  */
  44 public class PickAndContainsTest {
  45 
  46     /***************************************************************************
  47      *                                                                         *
  48      *                              Picking Tests                              *
  49      *                                                                         *
  50      **************************************************************************/
  51     @Test
  52     public void testScenePickingRect() {
  53         Rectangle rect = new Rectangle(50, 25, 100, 50);
  54         Group g = new Group();
  55         g.getChildren().add(rect);
  56         Scene scene = new Scene(g);
  57 
  58         assertSame(rect, SceneShim.test_pick(scene,100, 50));
  59         assertNull(SceneShim.test_pick(scene,0, 0));
  60         assertNull(SceneShim.test_pick(scene,160, 50));
  61     }
  62 
  63     @Test
  64     public void testScenePickingCircle() {
  65         Circle circle = new Circle(60, 60, 50);
  66         Group g = new Group();
  67         g.getChildren().add(circle);
  68         Scene scene = new Scene(g);
  69 
  70         assertSame(circle, SceneShim.test_pick(scene,100, 50));
  71         assertNull(SceneShim.test_pick(scene,0, 0));
  72         assertNull(SceneShim.test_pick(scene,160, 50));
  73     }
  74 
  75     @Test
  76     public void testScenePickingGroup() {
  77         Group grp;
  78         Rectangle r0;
  79         Rectangle r1;
  80         Rectangle r2;
  81         Group root = new Group();
  82 
  83         root.getChildren().addAll(r0 = new Rectangle(100, 100, 40, 20),
  84                                 grp = new Group(r1 = new Rectangle(0,0,40,20), 
  85                                                 r2 = new Rectangle(200,200,40,20))
  86                                 );
  87 
  88         Scene scene = new Scene(root);
  89 
  90         r1.setId("Rect 1");
  91         r2.setId("Rect 2");
  92         
  93         int pickX = 100;
  94         int pickY = 100;
  95         assertSame(r0, SceneShim.test_pick(scene,pickX, pickY));
  96         assertFalse(grp.contains(pickX, pickY));
  97         assertTrue(r0.contains(pickX, pickY));
  98         assertFalse(r1.contains(pickX, pickY));
  99         assertFalse(r2.contains(pickX, pickY));
 100 
 101         pickX = 45;
 102         pickY = 50;
 103         assertNull(SceneShim.test_pick(scene,pickX, pickY));
 104         assertFalse(grp.contains(pickX, pickY));
 105         assertFalse(r0.contains(pickX, pickY));
 106         assertFalse(r1.contains(pickX, pickY));
 107         assertFalse(r2.contains(pickX, pickY));
 108 
 109         pickX = 38;
 110         pickY = 18;
 111         assertSame(r1, SceneShim.test_pick(scene,pickX, pickY));
 112         assertTrue(grp.contains(pickX, pickY));
 113         assertFalse(r0.contains(pickX, pickY));
 114         assertTrue(r1.contains(pickX, pickY));
 115         assertFalse(r2.contains(pickX, pickY));
 116 
 117         pickX = 230;
 118         pickY = 215;
 119         assertSame(r2, SceneShim.test_pick(scene,pickX, pickY));
 120         assertTrue(grp.contains(pickX, pickY));
 121         assertFalse(r0.contains(pickX, pickY));
 122         assertFalse(r1.contains(pickX, pickY));
 123         assertTrue(r2.contains(pickX, pickY));
 124 
 125         pickX = 120;
 126         pickY = 110;
 127         assertSame(r0, SceneShim.test_pick(scene,pickX, pickY));
 128     }
 129 
 130     @Test
 131     public void testScenePickingGroupAndClip() {
 132         Group grp;
 133         Rectangle r0;
 134         Rectangle r1;
 135         Rectangle r2;
 136         Group root = new Group();
 137         root.getChildren().addAll(r0 = new Rectangle(100, 100, 40, 20),
 138                                 grp = new Group(r1 = new Rectangle(0,0,40,20),
 139                                                 r2 = new Rectangle(200,200,40,20))
 140                                 );
 141         Scene scene = new Scene(root);
 142         r1.setId("Rect 1");
 143         r1.setClip(new Circle(20,10,10));
 144         r2.setId("Rect 2");
 145         grp.setClip(new Circle(120,120,120));
 146 
 147         int pickX = 38;
 148         int pickY = 18;
 149         assertNull(SceneShim.test_pick(scene,pickX, pickY));
 150         assertFalse(grp.contains(pickX, pickY));
 151         assertFalse(r0.contains(pickX, pickY));
 152         assertFalse(r1.contains(pickX, pickY));
 153         assertFalse(r2.contains(pickX, pickY));
 154 
 155         pickX = 230;
 156         pickY = 215;
 157         assertNull(SceneShim.test_pick(scene,pickX, pickY));
 158         assertFalse(grp.contains(pickX, pickY));
 159         assertFalse(r0.contains(pickX, pickY));
 160         assertFalse(r1.contains(pickX, pickY));
 161         assertTrue(r2.contains(pickX, pickY));
 162 
 163         pickX = 120;
 164         pickY = 110;
 165         assertSame(r0, SceneShim.test_pick(scene,pickX, pickY));
 166     }
 167 
 168     @Test
 169     public void testScenePickingGroupAndClipWithPickOnBounds() {
 170         Group grp;
 171         Rectangle r0;
 172         Rectangle r1;
 173         Rectangle r2;
 174         Group root = new Group();
 175         root.getChildren().addAll(r0 = new Rectangle(100, 100, 40, 20),
 176                                 grp = new Group(r1 = new Rectangle(0,0,40,20),
 177                                                 r2 = new Rectangle(200,200,40,20))
 178                                 );
 179         Scene scene = new Scene(root);
 180         r1.setId("Rect 1");
 181         r1.setClip(new Circle(20,10,10));
 182         r2.setId("Rect 2");
 183         grp.setClip(new Circle(120,120,120));
 184         grp.getClip().setPickOnBounds(true);
 185 
 186         int pickX = 38;
 187         int pickY = 18;
 188         assertNull(SceneShim.test_pick(scene,pickX, pickY));
 189         assertFalse(grp.contains(pickX, pickY));
 190         assertFalse(r0.contains(pickX, pickY));
 191         assertFalse(r1.contains(pickX, pickY));
 192         assertFalse(r2.contains(pickX, pickY));
 193 
 194         pickX = 230;
 195         pickY = 215;
 196         assertSame(r2, SceneShim.test_pick(scene,pickX, pickY));
 197 
 198         pickX = 120;
 199         pickY = 110;
 200         assertSame(r0, SceneShim.test_pick(scene,pickX, pickY));
 201     }
 202 
 203     @Test
 204     public void testSceneGroupPickOnBounds() {
 205         Group grp;
 206         Rectangle r0;
 207         Rectangle r1;
 208         Rectangle r2;
 209         Group root = new Group();
 210         root.getChildren().addAll(r0 = new Rectangle(100, 100, 40, 20),
 211                 grp = new Group(r1 = new Rectangle(0,0,40,20),
 212                         r2 = new Rectangle(200,200,40,20))
 213                 );
 214         Scene scene = new Scene(root);
 215 
 216         r1.setId("Rect 1");
 217         r2.setId("Rect 2");
 218 
 219         int pickX = 45;
 220         int pickY = 50;
 221 
 222         assertNull(SceneShim.test_pick(scene,pickX, pickY));
 223         
 224         grp.setPickOnBounds(true);
 225 
 226         assertSame(grp, SceneShim.test_pick(scene,pickX, pickY));
 227         assertTrue(grp.contains(pickX, pickY));
 228         assertFalse(r0.contains(pickX, pickY));
 229         assertFalse(r1.contains(pickX, pickY));
 230         assertFalse(r2.contains(pickX, pickY));
 231     }
 232 }
 233