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