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