1 /*
   2  * Copyright (c) 2010, 2015, 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.css;
  27 
  28 import com.sun.javafx.css.ParsedValueImpl;
  29 import static org.junit.Assert.assertEquals;
  30 import javafx.css.ParsedValue;
  31 import javafx.css.Size;
  32 import javafx.css.SizeUnits;
  33 import javafx.scene.effect.BlurType;
  34 import javafx.scene.effect.DropShadow;
  35 import javafx.scene.effect.Effect;
  36 import javafx.scene.effect.InnerShadow;
  37 import javafx.scene.paint.Color;
  38 import javafx.scene.text.Font;
  39 
  40 import org.junit.Test;
  41 
  42 import javafx.css.converter.EffectConverter;
  43 import javafx.css.converter.DeriveColorConverter;
  44 
  45 
  46 public class EffectTypeTest {
  47 
  48     public EffectTypeTest() {
  49     }
  50 
  51     Size makeSize(float f) {
  52         return new Size(f, SizeUnits.PX);
  53     }
  54 
  55     InnerShadow getInnerShadow() {
  56         return new InnerShadow();
  57     }
  58 
  59     ParsedValue<ParsedValue[], Effect> getInnerShadowValue(InnerShadow is, boolean colorIsDerived) {
  60         Size offsetX = makeSize((float) is.getOffsetX());
  61         Size offsetY = makeSize((float) is.getOffsetX());
  62         Size choke = makeSize((float) is.getChoke());
  63         Size radius = makeSize((float) is.getRadius());
  64 
  65         ParsedValue<?,Color> colorVal;
  66 
  67         if (colorIsDerived) {
  68             ParsedValue[] values = new ParsedValue[] {
  69                 new ParsedValueImpl<Color,Color>(is.getColor(),null),
  70                 new ParsedValueImpl<Size,Size>(makeSize(0.5f),null)
  71             };
  72             colorVal = new ParsedValueImpl<ParsedValue[],Color>(values, DeriveColorConverter.getInstance());
  73         } else {
  74             colorVal = new ParsedValueImpl<Color,Color>(is.getColor(),null);
  75         }
  76 
  77         BlurType blurType = is.getBlurType();
  78 
  79         ParsedValue[] vals = new ParsedValue[] {
  80             new ParsedValueImpl<BlurType,BlurType>(blurType, null),
  81             colorVal,
  82             new ParsedValueImpl<Size,Size>(radius, null),
  83             new ParsedValueImpl<Size,Size>(choke, null),
  84             new ParsedValueImpl<Size,Size>(offsetX, null),
  85             new ParsedValueImpl<Size,Size>(offsetY, null)
  86         };
  87 
  88         return new ParsedValueImpl<ParsedValue[],Effect>(vals, EffectConverter.InnerShadowConverter.getInstance());
  89     }
  90 
  91     DropShadow getDropShadow() {
  92         return new DropShadow();
  93     }
  94 
  95     ParsedValue<ParsedValue[], Effect> getDropShadowValue(DropShadow ds, boolean colorIsDerived) {
  96         Size offsetX = makeSize((float) ds.getOffsetX());
  97         Size offsetY = makeSize((float) ds.getOffsetX());
  98         Size spread = makeSize((float) ds.getSpread());
  99         Size radius = makeSize((float) ds.getRadius());
 100 
 101         ParsedValue<?,Color> colorVal;
 102 
 103         if (colorIsDerived) {
 104             ParsedValue[] values = new ParsedValue[] {
 105                 new ParsedValueImpl<Color,Color>(ds.getColor(),null),
 106                 new ParsedValueImpl<Size,Size>(makeSize(0.5f),null)
 107             };
 108             colorVal = new ParsedValueImpl<ParsedValue[],Color>(values, DeriveColorConverter.getInstance());
 109         } else {
 110             colorVal = new ParsedValueImpl<Color,Color>(ds.getColor(),null);
 111         }
 112 
 113         BlurType blurType = ds.getBlurType();
 114 
 115         ParsedValue[] vals = new ParsedValue[] {
 116             new ParsedValueImpl<BlurType,BlurType>(blurType, null),
 117             colorVal,
 118             new ParsedValueImpl<Size,Size>(radius, null),
 119             new ParsedValueImpl<Size,Size>(spread, null),
 120             new ParsedValueImpl<Size,Size>(offsetX, null),
 121             new ParsedValueImpl<Size,Size>(offsetY, null)
 122         };
 123 
 124         return new ParsedValueImpl<ParsedValue[],Effect>(vals, EffectConverter.DropShadowConverter.getInstance());
 125     }
 126 
 127     void checkColor(String msg, Color c1, Color c2) {
 128         assertEquals(msg + ".red", c1.getRed(), c2.getRed(), 0.001);
 129         assertEquals(msg + ".blue", c1.getBlue(), c2.getBlue(), 0.001);
 130         assertEquals(msg + ".green", c1.getGreen(), c2.getGreen(), 0.001);
 131         assertEquals(msg + ".opacity", c1.getOpacity(), c2.getOpacity(), 0.001);
 132     }
 133 
 134     void checkInnerShadow(String msg, InnerShadow o1, InnerShadow o2) {
 135         assertEquals(msg + "innershadow.offsetX", o1.getOffsetX(), o2.getOffsetX(), 0.001);
 136         assertEquals(msg + "innershadow.offsety", o1.getOffsetY(), o2.getOffsetY(), 0.001);
 137         assertEquals(msg + "innershadow.choke", o1.getChoke(), o2.getChoke(), 0.001);
 138         assertEquals(msg + "innershadow.radius", o1.getRadius(), o2.getRadius(), 0.001);
 139         checkColor(msg + "innershadow", o1.getColor(), o2.getColor());
 140         assertEquals(msg + "innershadow.blurType", o1.getBlurType(), o2.getBlurType());
 141     }
 142 
 143     void checkDropShadow(String msg, DropShadow o1, DropShadow o2) {
 144         assertEquals(msg + "DropShadow.offsetX", o1.getOffsetX(), o2.getOffsetX(), 0.001);
 145         assertEquals(msg + "DropShadow.offsety", o1.getOffsetY(), o2.getOffsetY(), 0.001);
 146         assertEquals(msg + "DropShadow.spread", o1.getSpread(), o2.getSpread(), 0.001);
 147         assertEquals(msg + "DropShadow.radius", o1.getRadius(), o2.getRadius(), 0.001);
 148         checkColor(msg + "DropShadow", o1.getColor(), o2.getColor());
 149         assertEquals(msg + "DropShadow.blurType", o1.getBlurType(), o2.getBlurType());
 150     }
 151 
 152     /**
 153      * Test of convert method, of class EffectType.
 154      */
 155     @Test
 156     public void testConvert() {
 157         //System.out.println("convert");
 158         InnerShadow is = getInnerShadow();
 159         Font font = null;
 160         ParsedValue<ParsedValue[], Effect> value = getInnerShadowValue(is, false);
 161         Effect result = value.convert(font);
 162         checkInnerShadow("convert[1] ",  is, (InnerShadow)result);
 163 
 164         // Test with derived colors
 165         value = getInnerShadowValue(is, true);
 166         result = value.convert(font);
 167         // derived color is 50% of is.getColor()
 168         is.setColor(com.sun.javafx.util.Utils.deriveColor(is.getColor(), 0.5f));
 169         checkInnerShadow("convert[2] ", is, (InnerShadow)result);
 170 
 171         DropShadow ds = getDropShadow();
 172         value = getDropShadowValue(ds, false);
 173         result = value.convert(font);
 174         checkDropShadow("convert[3] ", ds, (DropShadow)result);
 175 
 176         // Test with derived colors
 177         value = getDropShadowValue(ds, true);
 178         result = value.convert(font);
 179         // derived color is 50% of is.getColor()
 180         ds.setColor(com.sun.javafx.util.Utils.deriveColor(ds.getColor(), 0.5f));
 181         checkDropShadow("convert[4] ", ds, (DropShadow)result);
 182 
 183     }
 184 
 185 }