1 /* 2 * Copyright (c) 2010, 2014, 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 javafx.css.ParsedValue; 29 import javafx.scene.paint.Color; 30 import javafx.scene.paint.CycleMethod; 31 import javafx.scene.paint.LinearGradient; 32 import javafx.scene.paint.Paint; 33 import javafx.scene.paint.RadialGradient; 34 import javafx.scene.paint.Stop; 35 import javafx.scene.text.Font; 36 import com.sun.javafx.css.converters.PaintConverter; 37 import com.sun.javafx.css.parser.CSSParser; 38 import com.sun.javafx.css.parser.StopConverter; 39 import org.junit.Test; 40 import static org.junit.Assert.assertEquals; 41 42 43 public class PaintTypeTest { 44 45 public PaintTypeTest() { 46 } 47 48 final Stop[] stops = new Stop[] { 49 new Stop(0.0f, Color.WHITE), 50 new Stop(1.0f, Color.BLACK) 51 }; 52 53 final Paint[] paints = new Paint[] { 54 Color.rgb(0, 128, 255), 55 new LinearGradient(0.0f, 0.0f, 1.0f, 1.0f, true, CycleMethod.NO_CYCLE, stops), 56 new RadialGradient(225, 0.28, 1f, 1f, 5.0f, false, CycleMethod.NO_CYCLE, stops) 57 }; 58 59 ParsedValue<?,Size> sizeVal(float value) { 60 return new ParsedValueImpl<Size,Size>(new Size(value*100, SizeUnits.PERCENT), null); 61 } 62 63 ParsedValue<ParsedValue[],Stop> stopValue(Stop stop) { 64 ParsedValue<?,Size> offset = sizeVal((float)stop.getOffset()); 65 ParsedValue<Color,Color> color = new ParsedValueImpl<Color,Color>(stop.getColor(), null); 66 ParsedValue[] values = new ParsedValue[] { offset, color }; 67 return new ParsedValueImpl<ParsedValue[],Stop>(values, StopConverter.getInstance()); 68 }; 69 70 ParsedValue<ParsedValue[],Paint> linearGradientValues(LinearGradient lg) { 71 ParsedValue[] values = new ParsedValue[7]; 72 int v = 0; 73 values[v++] = sizeVal((float) lg.getStartX()); 74 values[v++] = sizeVal((float) lg.getStartY()); 75 values[v++] = sizeVal((float) lg.getEndX()); 76 values[v++] = sizeVal((float) lg.getEndY()); 77 values[v++] = new ParsedValueImpl<CycleMethod,CycleMethod>(lg.getCycleMethod(),null); 78 values[v++] = stopValue(stops[0]); 79 values[v++] = stopValue(stops[1]); 80 return new ParsedValueImpl<ParsedValue[],Paint>(values, PaintConverter.LinearGradientConverter.getInstance()); 81 } 82 83 ParsedValue<ParsedValue[],Paint> radialGradientValues(RadialGradient rg) { 84 ParsedValue[] values = new ParsedValue[8]; 85 int v = 0; 86 values[v++] = new ParsedValueImpl<Size,Size>(new Size(rg.getFocusAngle(), SizeUnits.PX), null); 87 values[v++] = new ParsedValueImpl<Size,Size>(new Size(rg.getFocusDistance(), SizeUnits.PX), null); 88 values[v++] = sizeVal((float) rg.getCenterX()); 89 values[v++] = sizeVal((float) rg.getCenterY()); 90 values[v++] = new ParsedValueImpl<Size,Size>(new Size(rg.getRadius(), SizeUnits.PX), null); 91 values[v++] = new ParsedValueImpl<CycleMethod,CycleMethod>(rg.getCycleMethod(),null); 92 values[v++] = stopValue(stops[0]); 93 values[v++] = stopValue(stops[1]); 94 return new ParsedValueImpl<ParsedValue[],Paint>(values, PaintConverter.RadialGradientConverter.getInstance()); 95 } 96 97 final ParsedValue[] paintValues = new ParsedValue[] { 98 new ParsedValueImpl<Paint,Paint>(paints[0], null), 99 linearGradientValues((LinearGradient)paints[1]), 100 radialGradientValues((RadialGradient)paints[2]) 101 }; 102 103 /** 104 * Paint is layered (one value per layer). <paint> [, <paint>]*. 105 * In the convert function, the ParsedValue is a ParsedValue for each layer. 106 * That is, value.getValue() returns a ParsedValue[]. 107 */ 108 ParsedValue<ParsedValue<?,Paint>[],Paint[]> getValue(int nLayers) { 109 ParsedValue<Paint,Paint>[] layers = new ParsedValue[nLayers]; 110 for (int l=0; l<nLayers; l++) { 111 layers[l] = paintValues[l % paintValues.length]; 112 } 113 return new ParsedValueImpl<ParsedValue<?,Paint>[],Paint[]>(layers, PaintConverter.SequenceConverter.getInstance()); 114 } 115 /** 116 * Test of convert method, of class PaintType. 117 */ 118 @Test 119 public void testConvert() { 120 //System.out.println("convert"); 121 int nValues = paints.length; 122 ParsedValue<ParsedValue<?,Paint>[],Paint[]> value = getValue(nValues); 123 Font font = null; 124 Paint[] result = value.convert(font); 125 assertEquals(nValues, result.length); 126 for(int r=0; r<result.length; r++) { 127 Paint expResult = paints[r % paints.length]; 128 assertEquals(expResult, result[r]); 129 } 130 } 131 132 String[] css; 133 134 Paint[][] expResults; 135 136 // values for top, right, bottom, left 137 final String[][][] svals = new String[][][] { 138 { {"#ff0000"} }, 139 { {"#ff0000"}, {"#00ff00"}, {"#0000ff"}, {"#ffffff"} } 140 }; 141 142 public void setup() { 143 css = new String[svals.length]; 144 expResults = new Paint[svals.length][]; 145 for (int i=0; i<svals.length; i++) { 146 StringBuilder sbuf = new StringBuilder(); 147 expResults[i] = new Paint[svals[i].length]; 148 for (int j=0; j<svals[i].length; j++) { 149 for (int k=0; k<svals[i][j].length; k++) { 150 expResults[i][j] = Color.web(svals[i][j][k]); 151 sbuf.append(svals[i][j][k]); 152 if (k+1 < svals[i][j].length) sbuf.append(' '); 153 } 154 155 if (j+1 < svals[i].length) sbuf.append(", "); 156 } 157 css[i] = sbuf.toString(); 158 } 159 } 160 161 @Test 162 public void testPaintTypeWithCSS() { 163 setup(); 164 165 for (int i=0; i<css.length; i++) { 166 167 Stylesheet stylesheet = 168 CSSParser.getInstance().parse("* { -fx-border-color: " + css[i] + "; }"); 169 170 ParsedValue value = TypeTest.getValueFor(stylesheet, "-fx-border-color"); 171 172 Paint[][] paints = (Paint[][])value.convert(Font.getDefault()); 173 174 //assertEquals(expResults[i].length,paints.length); 175 176 for(int j=0; j<paints.length; j++) { 177 String msg = Integer.toString(i) + "." + Integer.toString(j); 178 assertEquals(msg, expResults[i][j], paints[j][0]); 179 } 180 } 181 182 } 183 184 @Test 185 public void testParseRadialGradient() { 186 187 // <radial-gradient> = radial-gradient( 188 // [ focus-angle <angle>, ]? 189 // [ focus-distance <percentage>, ]? 190 // [ center <point>, ]? 191 // radius <length>, 192 // [ [ repeat | reflect ] ,]? 193 // <color-stop>[, <color-stop>]+ ) 194 ParsedValue value = CSSParser.getInstance().parseExpr("-fx-background-color", 195 "radial-gradient(focus-angle 90deg, focus-distance 50%, radius 50, red, green, blue)"); 196 RadialGradient result = (RadialGradient)((Paint[])value.convert(null))[0]; 197 RadialGradient expected = new RadialGradient(90, .5, 0, 0, 50, 198 false, CycleMethod.NO_CYCLE, 199 new Stop(0, Color.RED), 200 new Stop(.5, Color.GREEN), 201 new Stop(1.0,Color.BLUE)); 202 assertEquals(expected,result); 203 204 value = CSSParser.getInstance().parseExpr("-fx-background-color", 205 "radial-gradient(focus-angle 1.5708rad, focus-distance 50%, radius 50, red, green, blue)"); 206 result = (RadialGradient)((Paint[])value.convert(null))[0]; 207 assertEquals(expected,result); 208 209 value = CSSParser.getInstance().parseExpr("-fx-background-color", 210 "radial-gradient(center 0% 10%, radius 50%, reflect, red, green, blue)"); 211 result = (RadialGradient)((Paint[])value.convert(null))[0]; 212 expected = new RadialGradient(0, 0, 0, .1, .5, 213 true, CycleMethod.REFLECT, 214 new Stop(0, Color.RED), 215 new Stop(.5, Color.GREEN), 216 new Stop(1.0,Color.BLUE)); 217 assertEquals(expected,result); 218 } 219 220 @Test 221 public void testParseLinearGradient() { 222 223 // <linear-gradient> = linear-gradient( 224 // [ [from <point> to <point>] | [ to <side-or-corner> ] ] ,]? [ [ repeat | reflect ] ,]? 225 // <color-stop>[, <color-stop>]+ 226 // ) 227 // 228 ParsedValue value = CSSParser.getInstance().parseExpr("-fx-background-color", 229 "linear-gradient(to top, red, green, blue)"); 230 LinearGradient result = (LinearGradient)((Paint[])value.convert(null))[0]; 231 LinearGradient expected = new LinearGradient(0, 1, 0, 0, 232 true, CycleMethod.NO_CYCLE, 233 new Stop(0, Color.RED), 234 new Stop(.5, Color.GREEN), 235 new Stop(1.0, Color.BLUE)); 236 assertEquals(expected,result); 237 238 value = CSSParser.getInstance().parseExpr("-fx-background-color", 239 "linear-gradient(to bottom, red, green, blue)"); 240 result = (LinearGradient)((Paint[])value.convert(null))[0]; 241 expected = new LinearGradient(0, 0, 0, 1, 242 true, CycleMethod.NO_CYCLE, 243 new Stop(0, Color.RED), 244 new Stop(.5, Color.GREEN), 245 new Stop(1.0, Color.BLUE)); 246 assertEquals(expected,result); 247 248 value = CSSParser.getInstance().parseExpr("-fx-background-color", 249 "linear-gradient(to left, red, green, blue)"); 250 result = (LinearGradient)((Paint[])value.convert(null))[0]; 251 expected = new LinearGradient(1, 0, 0, 0, 252 true, CycleMethod.NO_CYCLE, 253 new Stop(0, Color.RED), 254 new Stop(.5, Color.GREEN), 255 new Stop(1.0, Color.BLUE)); 256 assertEquals(expected,result); 257 258 value = CSSParser.getInstance().parseExpr("-fx-background-color", 259 "linear-gradient(to right, red, green, blue)"); 260 result = (LinearGradient)((Paint[])value.convert(null))[0]; 261 expected = new LinearGradient(0, 0, 1, 0, 262 true, CycleMethod.NO_CYCLE, 263 new Stop(0, Color.RED), 264 new Stop(.5, Color.GREEN), 265 new Stop(1.0, Color.BLUE)); 266 assertEquals(expected,result); 267 268 value = CSSParser.getInstance().parseExpr("-fx-background-color", 269 "linear-gradient(to bottom left, red, green, blue)"); 270 result = (LinearGradient)((Paint[])value.convert(null))[0]; 271 expected = new LinearGradient(1, 0, 0, 1, 272 true, CycleMethod.NO_CYCLE, 273 new Stop(0, Color.RED), 274 new Stop(.5, Color.GREEN), 275 new Stop(1.0, Color.BLUE)); 276 assertEquals(expected,result); 277 278 value = CSSParser.getInstance().parseExpr("-fx-background-color", 279 "linear-gradient(to bottom right, red, green, blue)"); 280 result = (LinearGradient)((Paint[])value.convert(null))[0]; 281 expected = new LinearGradient(0, 0, 1, 1, 282 true, CycleMethod.NO_CYCLE, 283 new Stop(0, Color.RED), 284 new Stop(.5, Color.GREEN), 285 new Stop(1.0, Color.BLUE)); 286 assertEquals(expected,result); 287 288 value = CSSParser.getInstance().parseExpr("-fx-background-color", 289 "linear-gradient(to top left, red, green, blue)"); 290 result = (LinearGradient)((Paint[])value.convert(null))[0]; 291 expected = new LinearGradient(1, 1, 0, 0, 292 true, CycleMethod.NO_CYCLE, 293 new Stop(0, Color.RED), 294 new Stop(.5, Color.GREEN), 295 new Stop(1.0, Color.BLUE)); 296 assertEquals(expected,result); 297 298 value = CSSParser.getInstance().parseExpr("-fx-background-color", 299 "linear-gradient(to top right, red, green, blue)"); 300 result = (LinearGradient)((Paint[])value.convert(null))[0]; 301 expected = new LinearGradient(0, 1, 1, 0, 302 true, CycleMethod.NO_CYCLE, 303 new Stop(0, Color.RED), 304 new Stop(.5, Color.GREEN), 305 new Stop(1.0, Color.BLUE)); 306 assertEquals(expected,result); 307 308 value = CSSParser.getInstance().parseExpr("-fx-background-color", 309 "linear-gradient(from 10% 10% to 90% 90%, reflect, red, green, blue)"); 310 result = (LinearGradient)((Paint[])value.convert(null))[0]; 311 expected = new LinearGradient(.1, .1, .9, .9, 312 true, CycleMethod.REFLECT, 313 new Stop(0, Color.RED), 314 new Stop(.5, Color.GREEN), 315 new Stop(1.0, Color.BLUE)); 316 assertEquals(expected,result); 317 } 318 319 }