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