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.geom.RectBounds;
  29 import com.sun.javafx.sg.prism.NGNodeShim;
  30 import com.sun.javafx.sg.prism.NGRectangle;
  31 import com.sun.prism.paint.Color;
  32 import org.junit.Before;
  33 import org.junit.Test;
  34 import static org.junit.Assert.assertEquals;
  35 import static org.junit.Assert.assertFalse;
  36 import static org.junit.Assert.assertTrue;
  37 
  38 /**
  39  * Tests for NGRectangle class
  40  */
  41 public class NGRectangleTest extends NGTestBase {
  42     NGRectangleMock r;
  43 
  44     @Before
  45     public void setup() {
  46         r = new NGRectangleMock();
  47         r.updateRectangle(0, 0, 100, 100, 0, 0);
  48         r.setFillPaint(Color.BLACK);
  49     }
  50 
  51     /**
  52      * NGRectangle supports opaque regions
  53      */
  54     @Test
  55     public void testSupportsOpaqueRegions() {
  56         assertTrue(NGNodeShim.supportsOpaqueRegions(r));
  57     }
  58 
  59     /**
  60      * The default rectangle with a fill and size should have an opaque region
  61      */
  62     @Test
  63     public void testHasOpaqueRegion() {
  64         assertTrue(r.hasOpaqueRegion());
  65     }
  66 
  67     /**
  68      * If there's no fill, there is no opaque region because
  69      * we don't yet support strokes as being part of our
  70      * opaque region computation.
  71      */
  72     @Test
  73     public void testHasOpaqueRegion_NoFill() {
  74         r.setFillPaint(null);
  75         assertFalse(r.hasOpaqueRegion());
  76     }
  77 
  78     /**
  79      * If we have no width, we won't have any opaque region.
  80      */
  81     @Test
  82     public void testHasOpaqueRegion_NoWidth() {
  83         r.updateRectangle(0, 0, 0, 100, 0, 0);
  84         assertFalse(r.hasOpaqueRegion());
  85     }
  86 
  87     /**
  88      * If we have no height, we won't have any opaque region.
  89      */
  90     @Test
  91     public void testHasOpaqueRegion_NoHeight() {
  92         r.updateRectangle(0, 0, 100, 0, 0, 0);
  93         assertFalse(r.hasOpaqueRegion());
  94     }
  95 
  96     /**
  97      * In this case we still compute opaque insets,
  98      * based on the same logic as we'd use with
  99      * an ellipse.
 100      */
 101     @Test
 102     public void testHasOpaqueRegion_ArcWidthSoBig() {
 103         r.updateRectangle(0, 0, 100, 100, 100, 100);
 104         assertTrue(r.hasOpaqueRegion());
 105     }
 106 
 107     @Test
 108     public void computeOpaqueRegion_NoArc() {
 109         assertEquals(new RectBounds(0, 0, 100, 100), r.computeOpaqueRegion(new RectBounds()));
 110     }
 111 
 112     class NGRectangleMock extends NGRectangle {
 113         boolean opaqueRegionRecomputed = false;
 114 
 115         @Override
 116         public boolean hasOpaqueRegion() {
 117             opaqueRegionRecomputed = true;
 118             return super.hasOpaqueRegion();
 119         }
 120 
 121         @Override
 122         protected RectBounds computeOpaqueRegion(RectBounds opaqueRegion) {
 123             opaqueRegionRecomputed = true;
 124             return super.computeOpaqueRegion(opaqueRegion);
 125         }
 126     }
 127 }