1 /*
   2  * Copyright (c) 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.sg.prism;
  27 
  28 import com.sun.javafx.sg.prism.NGNodeShim;
  29 import com.sun.javafx.sg.prism.NGRegion;
  30 import javafx.geometry.Insets;
  31 import javafx.scene.layout.Background;
  32 import javafx.scene.layout.BackgroundFill;
  33 import javafx.scene.layout.Region;
  34 import javafx.scene.paint.Color;
  35 import javafx.scene.shape.ClosePath;
  36 import javafx.scene.shape.LineTo;
  37 import javafx.scene.shape.MoveTo;
  38 import javafx.scene.shape.Path;
  39 import org.junit.Test;
  40 import static org.junit.Assert.assertFalse;
  41 import static org.junit.Assert.assertTrue;
  42 
  43 /**
  44  */
  45 public class NGRegionTest {
  46     @Test public void setOpaqueInsetsInvalidatesOpaqueRegion() {
  47         NGRegion r = new NGRegion();
  48         r.getOpaqueRegion(); // Forces to validate the opaque region
  49         assertFalse(NGNodeShim.isOpaqueRegionInvalid(r)); // sanity check
  50         r.setOpaqueInsets(0, 0, 0, 0);
  51         assertTrue(NGNodeShim.isOpaqueRegionInvalid(r));
  52     }
  53 
  54     @Test public void updateShapeInvalidatesOpaqueRegion() {
  55         NGRegion r = new NGRegion();
  56         r.getOpaqueRegion(); // Forces to validate the opaque region
  57         assertFalse(NGNodeShim.isOpaqueRegionInvalid(r)); // sanity check
  58         r.updateShape(null, true, false, false); // Actual values don't matter
  59         assertTrue(NGNodeShim.isOpaqueRegionInvalid(r));
  60     }
  61 
  62     // RT-13820: We change the shape internally and call this same method, so it
  63     // needs to invalidate the opaque region.
  64     @Test public void updateShapeToSameInstanceInvalidatesOpaqueRegion() {
  65         LineTo lineTo;
  66         Path p = new Path(
  67                 new MoveTo(10, 20),
  68                 lineTo = new LineTo(100, 100),
  69                 new ClosePath()
  70         );
  71 
  72         NGRegion r = new NGRegion();
  73         r.updateShape(p, true, true, true);
  74         r.getOpaqueRegion(); // Forces to validate the opaque region
  75         assertFalse(NGNodeShim.isOpaqueRegionInvalid(r)); // sanity check
  76         lineTo.setX(200);
  77         r.updateShape(p, true, true, true);
  78         assertTrue(NGNodeShim.isOpaqueRegionInvalid(r));
  79     }
  80 
  81     @Test public void setSizeInvalidatesOpaqueRegion() {
  82         NGRegion r = new NGRegion();
  83         r.getOpaqueRegion(); // Forces to validate the opaque region
  84         assertFalse(NGNodeShim.isOpaqueRegionInvalid(r)); // sanity check
  85         r.setSize(100, 100);
  86         assertTrue(NGNodeShim.isOpaqueRegionInvalid(r));
  87     }
  88 
  89     // Note: These tests are using a Region and doing a sync because it was found that
  90     // doing the check directly on the updateBackground method itself gave incorrect
  91     // results, but doing so via Region's sync worked correctly (because every time a
  92     // background is changed on the Region, setOpaqueInsets is called which invalidates
  93     // the opaque region).
  94 
  95     @Test public void updateBackgroundWithSameSizeButTransparentFillInvalidatesOpaqueInsets() {
  96         Region r = new Region();
  97         NGRegion peer = r.impl_getPeer();
  98         r.setBackground(new Background(new BackgroundFill(Color.RED, null, null)));
  99         r.impl_updatePeer();
 100         peer.getOpaqueRegion(); // Forces to validate the opaque region
 101         assertFalse(NGNodeShim.isOpaqueRegionInvalid(peer)); // sanity check
 102         r.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, null, null)));
 103         r.impl_updatePeer();
 104         assertTrue(NGNodeShim.isOpaqueRegionInvalid(peer));
 105     }
 106 
 107     @Test public void updateBackgroundWithDifferentSizeBackgroundInvalidatesOpaqueInsets() {
 108         Region r = new Region();
 109         NGRegion peer = r.impl_getPeer();
 110         r.setBackground(new Background(new BackgroundFill(Color.RED, null, null)));
 111         r.impl_updatePeer();
 112         peer.getOpaqueRegion(); // Forces to validate the opaque region
 113         assertFalse(NGNodeShim.isOpaqueRegionInvalid(peer)); // sanity check
 114         r.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, null, new Insets(10))));
 115         r.impl_updatePeer();
 116         assertTrue(NGNodeShim.isOpaqueRegionInvalid(peer));
 117     }
 118 
 119     @Test public void updateBackgroundWithDifferentSizeBackgroundInvalidatesOpaqueInsets2() {
 120         Region r = new Region();
 121         NGRegion peer = r.impl_getPeer();
 122         r.setBackground(new Background(new BackgroundFill(Color.RED, null, null)));
 123         r.impl_updatePeer();
 124         peer.getOpaqueRegion(); // Forces to validate the opaque region
 125         assertFalse(NGNodeShim.isOpaqueRegionInvalid(peer)); // sanity check
 126         r.setBackground(new Background(new BackgroundFill(Color.RED, null, new Insets(10))));
 127         r.impl_updatePeer();
 128         assertTrue(NGNodeShim.isOpaqueRegionInvalid(peer));
 129     }
 130 
 131     @Test public void updateBackgroundWithDifferentSizeBackgroundInvalidatesOpaqueInsets3() {
 132         Region r = new Region();
 133         NGRegion peer = r.impl_getPeer();
 134         r.setBackground(new Background(new BackgroundFill(Color.RED, null, null)));
 135         r.impl_updatePeer();
 136         peer.getOpaqueRegion(); // Forces to validate the opaque region
 137         assertFalse(NGNodeShim.isOpaqueRegionInvalid(peer)); // sanity check
 138         r.setBackground(new Background(new BackgroundFill(Color.RED, null, new Insets(-10))));
 139         r.impl_updatePeer();
 140         assertTrue(NGNodeShim.isOpaqueRegionInvalid(peer));
 141     }
 142 }