modules/graphics/src/test/java/javafx/scene/layout/RegionPickTest.java

Print this page




  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.layout;
  27 
  28 import javafx.geometry.Insets;
  29 import javafx.scene.paint.Color;

  30 import org.junit.Before;
  31 import org.junit.Test;
  32 
  33 import static org.junit.Assert.*;
  34 
  35 /**
  36  * Tests for Region picking. By default, Region has pickOnBounds set to true, so picking
  37  * anything within the bounds of the region will return true, anything outside the bounds
  38  * false. However, due to RT-25066, the bounds of a region defined by a shape will be 0x0,
  39  * in which case it will never be picked.
  40  *
  41  * If pickOnBounds is false, then an entire different code path is executed. We don't care
  42  * whether a fill is transparent or not (transparent fills can still cause the region to
  43  * pick), but we do care about picking within the "shape" and picking within the rounded
  44  * rectangle which is created as a result of background fills / background images.
  45  *
  46  * In the case of background images, we don't care about whether we are picking a transparent
  47  * pixel or not, but just based on the insets etc (since we don't have a good means for testing
  48  * the image pixel value).
  49  */


 495         assertFalse(region.contains(X + WIDTH - 3 - 5 + (6 * Math.cos(45)), Y + HEIGHT - 2 - 6 + (7 * Math.sin(45))));
 496         // Check points in the bottom-left corner area
 497         assertTrue(region.contains(X + 1, Y + HEIGHT - 2 - 7));
 498         assertTrue(region.contains(X + 1 + 8, Y + HEIGHT - 2));
 499         assertTrue(region.contains(X + 1 + 8 - (8 * Math.cos(45)), Y + HEIGHT - 2 - 7 + (7 * Math.sin(45))));
 500         assertTrue(region.contains(X + 1 + 8 - (7 * Math.cos(45)), Y + HEIGHT - 2 - 7 + (6 * Math.sin(45))));
 501         assertFalse(region.contains(X + 1 + 8 - (9 * Math.cos(45)), Y + HEIGHT - 2 - 7 + (8 * Math.sin(45))));
 502         // Check the center
 503         assertFalse(region.contains(CENTER_X, CENTER_Y));
 504         // TODO Could stand to have more tests testing the inside hit edge
 505     }
 506 
 507     /**************************************************************************
 508      *                                                                        *
 509      * Test behavior when picking a shaped region. We have to test all the    *
 510      * positionShape / scaleShape variants to make sure we are always picking *
 511      * based on the perceived (rendered) shape                                *
 512      *                                                                        *
 513      *************************************************************************/
 514 
 515     // TODO implement along with fix for RT-27775















































































 516 }


  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.layout;
  27 
  28 import javafx.geometry.Insets;
  29 import javafx.scene.paint.Color;
  30 import javafx.scene.shape.Circle;
  31 import org.junit.Before;
  32 import org.junit.Test;
  33 
  34 import static org.junit.Assert.*;
  35 
  36 /**
  37  * Tests for Region picking. By default, Region has pickOnBounds set to true, so picking
  38  * anything within the bounds of the region will return true, anything outside the bounds
  39  * false. However, due to RT-25066, the bounds of a region defined by a shape will be 0x0,
  40  * in which case it will never be picked.
  41  *
  42  * If pickOnBounds is false, then an entire different code path is executed. We don't care
  43  * whether a fill is transparent or not (transparent fills can still cause the region to
  44  * pick), but we do care about picking within the "shape" and picking within the rounded
  45  * rectangle which is created as a result of background fills / background images.
  46  *
  47  * In the case of background images, we don't care about whether we are picking a transparent
  48  * pixel or not, but just based on the insets etc (since we don't have a good means for testing
  49  * the image pixel value).
  50  */


 496         assertFalse(region.contains(X + WIDTH - 3 - 5 + (6 * Math.cos(45)), Y + HEIGHT - 2 - 6 + (7 * Math.sin(45))));
 497         // Check points in the bottom-left corner area
 498         assertTrue(region.contains(X + 1, Y + HEIGHT - 2 - 7));
 499         assertTrue(region.contains(X + 1 + 8, Y + HEIGHT - 2));
 500         assertTrue(region.contains(X + 1 + 8 - (8 * Math.cos(45)), Y + HEIGHT - 2 - 7 + (7 * Math.sin(45))));
 501         assertTrue(region.contains(X + 1 + 8 - (7 * Math.cos(45)), Y + HEIGHT - 2 - 7 + (6 * Math.sin(45))));
 502         assertFalse(region.contains(X + 1 + 8 - (9 * Math.cos(45)), Y + HEIGHT - 2 - 7 + (8 * Math.sin(45))));
 503         // Check the center
 504         assertFalse(region.contains(CENTER_X, CENTER_Y));
 505         // TODO Could stand to have more tests testing the inside hit edge
 506     }
 507 
 508     /**************************************************************************
 509      *                                                                        *
 510      * Test behavior when picking a shaped region. We have to test all the    *
 511      * positionShape / scaleShape variants to make sure we are always picking *
 512      * based on the perceived (rendered) shape                                *
 513      *                                                                        *
 514      *************************************************************************/
 515 
 516     @Test public void pickingSimpleShape() {
 517         region.setPickOnBounds(false);
 518         region.setScaleShape(false);
 519         region.setCenterShape(false);
 520         region.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
 521         
 522         region.setShape(new Circle(50, 50, 30));
 523         
 524         assertFalse(region.contains(X + 50, Y + 81));
 525         assertFalse(region.contains(X + 50, Y + 19));
 526         assertFalse(region.contains(X + 81, Y + 50));
 527         assertFalse(region.contains(X + 19, Y + 50));
 528         assertTrue(region.contains(X + 50, Y + 79));
 529         assertTrue(region.contains(X + 50, Y + 21));
 530         assertTrue(region.contains(X + 79, Y + 50));
 531         assertTrue(region.contains(X + 21, Y + 50));
 532         
 533     }
 534     
 535     @Test public void pickingCenteredShape() {
 536         region.setPickOnBounds(false);
 537         region.setScaleShape(false);
 538         region.setCenterShape(true);
 539         region.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
 540 
 541         region.setShape(new Circle(0, 0, 30));
 542 
 543         assertFalse(region.contains(X + 50, Y + 81));
 544         assertFalse(region.contains(X + 50, Y + 19));
 545         assertFalse(region.contains(X + 81, Y + 50));
 546         assertFalse(region.contains(X + 19, Y + 50));
 547         assertTrue(region.contains(X + 50, Y + 79));
 548         assertTrue(region.contains(X + 50, Y + 21));
 549         assertTrue(region.contains(X + 79, Y + 50));
 550         assertTrue(region.contains(X + 21, Y + 50));
 551         
 552     }
 553     
 554     @Test public void pickingScaledShape() {
 555         region.setPickOnBounds(false);
 556         region.setScaleShape(true);
 557         region.setCenterShape(false);
 558         region.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
 559 
 560         region.setShape(new Circle(0, 0, 30));
 561 
 562         assertFalse(region.contains(X, Y + HEIGHT / 2  + 1));
 563         assertFalse(region.contains(X, Y - HEIGHT / 2  - 1));
 564         assertFalse(region.contains(X + WIDTH / 2 + 1, Y));
 565         assertFalse(region.contains(X - WIDTH / 2 - 1, Y));
 566         
 567         assertTrue(region.contains(X + 1, Y + HEIGHT / 2 - 1));
 568         assertTrue(region.contains(X + WIDTH / 2 - 1, Y));
 569         
 570         // Even though the shape is there, these points are outside of region's bounds, so it wouldn't be really picked.
 571         assertFalse(region.contains(X + 1, Y - HEIGHT / 2 + 1));
 572         assertFalse(region.contains(X - WIDTH / 2 + 1, Y));
 573         
 574     }
 575     
 576     @Test public void pickingScaledAndCenteredShape() {
 577         region.setPickOnBounds(false);
 578         region.setScaleShape(true);
 579         region.setCenterShape(true);
 580         region.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
 581 
 582         region.setShape(new Circle(0, 0, 30));
 583 
 584         assertFalse(region.contains(X + WIDTH / 2, Y + HEIGHT + 1));
 585         assertFalse(region.contains(X + WIDTH / 2, Y - 1));
 586         assertFalse(region.contains(X + WIDTH + 1, Y + HEIGHT / 2));
 587         assertFalse(region.contains(X - 1, Y + HEIGHT / 2));
 588         
 589         assertTrue(region.contains(X + WIDTH / 2, Y + HEIGHT - 1));
 590         assertTrue(region.contains(X + WIDTH / 2, Y + 1));
 591         assertTrue(region.contains(X + WIDTH - 1, Y + HEIGHT / 2));
 592         assertTrue(region.contains(X + 1, Y + HEIGHT / 2));
 593         
 594     }
 595     
 596 }