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