modules/graphics/src/test/java/javafx/css/PaintTypeTest.java

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization


   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 


 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 }


   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 javafx.css;
  27 

  28 import javafx.scene.paint.Color;
  29 import javafx.scene.paint.CycleMethod;
  30 import javafx.scene.paint.LinearGradient;
  31 import javafx.scene.paint.Paint;
  32 import javafx.scene.paint.RadialGradient;
  33 import javafx.scene.paint.Stop;
  34 import javafx.scene.text.Font;
  35 import com.sun.javafx.css.ParsedValueImpl;
  36 import javafx.css.converter.PaintConverter;
  37 import javafx.css.converter.StopConverter;
  38 import org.junit.Test;
  39 import static org.junit.Assert.assertEquals;
  40 
  41 
  42 public class PaintTypeTest {
  43 
  44     public PaintTypeTest() {
  45     }
  46 
  47     final Stop[] stops = new Stop[] {
  48         new Stop(0.0f, Color.WHITE),
  49         new Stop(1.0f, Color.BLACK)
  50     };
  51 
  52     final Paint[] paints = new Paint[] {
  53         Color.rgb(0, 128, 255),
  54         new LinearGradient(0.0f, 0.0f, 1.0f, 1.0f, true, CycleMethod.NO_CYCLE, stops),
  55         new RadialGradient(225, 0.28, 1f, 1f, 5.0f, false, CycleMethod.NO_CYCLE, stops)
  56     };
  57 


 147             for (int j=0; j<svals[i].length; j++) {
 148                 for (int k=0; k<svals[i][j].length; k++) {
 149                     expResults[i][j] = Color.web(svals[i][j][k]);
 150                     sbuf.append(svals[i][j][k]);
 151                     if (k+1 < svals[i][j].length) sbuf.append(' ');
 152                 }
 153 
 154                 if (j+1 < svals[i].length) sbuf.append(", ");
 155             }
 156             css[i] = sbuf.toString();
 157         }
 158     }
 159 
 160     @Test
 161     public void testPaintTypeWithCSS() {
 162         setup();
 163 
 164         for (int i=0; i<css.length; i++) {
 165 
 166             Stylesheet stylesheet =
 167                 new CssParser().parse("* { -fx-border-color: " + css[i] + "; }");
 168 
 169             ParsedValue value = TypeTest.getValueFor(stylesheet, "-fx-border-color");
 170 
 171             Paint[][] paints = (Paint[][])value.convert(Font.getDefault());
 172 
 173             //assertEquals(expResults[i].length,paints.length);
 174 
 175             for(int j=0; j<paints.length; j++) {
 176                 String msg = Integer.toString(i) + "." + Integer.toString(j);
 177                 assertEquals(msg, expResults[i][j], paints[j][0]);
 178             }
 179         }
 180 
 181     }
 182 
 183     @Test
 184     public void testParseRadialGradient() {
 185 
 186         // <radial-gradient> = radial-gradient(
 187         //        [ focus-angle <angle>, ]?
 188         //        [ focus-distance <percentage>, ]?
 189         //        [ center <point>, ]?
 190         //        radius <length>,
 191         //        [ [ repeat | reflect ] ,]?
 192         //        <color-stop>[, <color-stop>]+ )
 193         ParsedValue value = new CssParser().parseExpr("-fx-background-color",
 194                 "radial-gradient(focus-angle 90deg, focus-distance 50%, radius 50, red, green, blue)");
 195         RadialGradient result = (RadialGradient)((Paint[])value.convert(null))[0];
 196         RadialGradient expected = new RadialGradient(90, .5, 0, 0, 50,
 197                 false, CycleMethod.NO_CYCLE,
 198                 new Stop(0, Color.RED),
 199                 new Stop(.5, Color.GREEN),
 200                 new Stop(1.0,Color.BLUE));
 201         assertEquals(expected,result);
 202 
 203         value = new CssParser().parseExpr("-fx-background-color",
 204                 "radial-gradient(focus-angle 1.5708rad, focus-distance 50%, radius 50, red, green, blue)");
 205         result = (RadialGradient)((Paint[])value.convert(null))[0];
 206         assertEquals(expected,result);
 207 
 208         value = new CssParser().parseExpr("-fx-background-color",
 209                 "radial-gradient(center 0% 10%, radius 50%, reflect, red, green, blue)");
 210         result = (RadialGradient)((Paint[])value.convert(null))[0];
 211         expected = new RadialGradient(0, 0, 0, .1, .5,
 212                 true, CycleMethod.REFLECT,
 213                 new Stop(0, Color.RED),
 214                 new Stop(.5, Color.GREEN),
 215                 new Stop(1.0,Color.BLUE));
 216         assertEquals(expected,result);
 217     }
 218 
 219     @Test
 220     public void testParseLinearGradient() {
 221 
 222         // <linear-gradient> = linear-gradient(
 223         //        [ [from <point> to <point>] | [ to <side-or-corner> ] ] ,]? [ [ repeat | reflect ] ,]?
 224         //        <color-stop>[, <color-stop>]+
 225         // )
 226         //
 227         ParsedValue value = new CssParser().parseExpr("-fx-background-color",
 228                 "linear-gradient(to top, red, green, blue)");
 229         LinearGradient result = (LinearGradient)((Paint[])value.convert(null))[0];
 230         LinearGradient expected = new LinearGradient(0, 1, 0, 0,
 231                 true, CycleMethod.NO_CYCLE,
 232                 new Stop(0, Color.RED),
 233                 new Stop(.5, Color.GREEN),
 234                 new Stop(1.0, Color.BLUE));
 235         assertEquals(expected,result);
 236 
 237         value = new CssParser().parseExpr("-fx-background-color",
 238                 "linear-gradient(to bottom, red, green, blue)");
 239         result = (LinearGradient)((Paint[])value.convert(null))[0];
 240         expected = new LinearGradient(0, 0, 0, 1,
 241                 true, CycleMethod.NO_CYCLE,
 242                 new Stop(0, Color.RED),
 243                 new Stop(.5, Color.GREEN),
 244                 new Stop(1.0, Color.BLUE));
 245         assertEquals(expected,result);
 246 
 247         value = new CssParser().parseExpr("-fx-background-color",
 248                 "linear-gradient(to left, red, green, blue)");
 249         result = (LinearGradient)((Paint[])value.convert(null))[0];
 250         expected = new LinearGradient(1, 0, 0, 0,
 251                 true, CycleMethod.NO_CYCLE,
 252                 new Stop(0, Color.RED),
 253                 new Stop(.5, Color.GREEN),
 254                 new Stop(1.0, Color.BLUE));
 255         assertEquals(expected,result);
 256 
 257         value = new CssParser().parseExpr("-fx-background-color",
 258                 "linear-gradient(to right, red, green, blue)");
 259         result = (LinearGradient)((Paint[])value.convert(null))[0];
 260         expected = new LinearGradient(0, 0, 1, 0,
 261                 true, CycleMethod.NO_CYCLE,
 262                 new Stop(0, Color.RED),
 263                 new Stop(.5, Color.GREEN),
 264                 new Stop(1.0, Color.BLUE));
 265         assertEquals(expected,result);
 266 
 267         value = new CssParser().parseExpr("-fx-background-color",
 268                 "linear-gradient(to bottom left, red, green, blue)");
 269         result = (LinearGradient)((Paint[])value.convert(null))[0];
 270         expected = new LinearGradient(1, 0, 0, 1,
 271                 true, CycleMethod.NO_CYCLE,
 272                 new Stop(0, Color.RED),
 273                 new Stop(.5, Color.GREEN),
 274                 new Stop(1.0, Color.BLUE));
 275         assertEquals(expected,result);
 276 
 277         value = new CssParser().parseExpr("-fx-background-color",
 278                 "linear-gradient(to bottom right, red, green, blue)");
 279         result = (LinearGradient)((Paint[])value.convert(null))[0];
 280         expected = new LinearGradient(0, 0, 1, 1,
 281                 true, CycleMethod.NO_CYCLE,
 282                 new Stop(0, Color.RED),
 283                 new Stop(.5, Color.GREEN),
 284                 new Stop(1.0, Color.BLUE));
 285         assertEquals(expected,result);
 286 
 287         value = new CssParser().parseExpr("-fx-background-color",
 288                 "linear-gradient(to top left, red, green, blue)");
 289         result = (LinearGradient)((Paint[])value.convert(null))[0];
 290         expected = new LinearGradient(1, 1, 0, 0,
 291                 true, CycleMethod.NO_CYCLE,
 292                 new Stop(0, Color.RED),
 293                 new Stop(.5, Color.GREEN),
 294                 new Stop(1.0, Color.BLUE));
 295         assertEquals(expected,result);
 296 
 297         value = new CssParser().parseExpr("-fx-background-color",
 298                 "linear-gradient(to top right, red, green, blue)");
 299         result = (LinearGradient)((Paint[])value.convert(null))[0];
 300         expected = new LinearGradient(0, 1, 1, 0,
 301                 true, CycleMethod.NO_CYCLE,
 302                 new Stop(0, Color.RED),
 303                 new Stop(.5, Color.GREEN),
 304                 new Stop(1.0, Color.BLUE));
 305         assertEquals(expected,result);
 306 
 307         value = new CssParser().parseExpr("-fx-background-color",
 308                 "linear-gradient(from 10% 10% to 90% 90%, reflect, red, green, blue)");
 309         result = (LinearGradient)((Paint[])value.convert(null))[0];
 310         expected = new LinearGradient(.1, .1, .9, .9,
 311                 true, CycleMethod.REFLECT,
 312                 new Stop(0, Color.RED),
 313                 new Stop(.5, Color.GREEN),
 314                 new Stop(1.0, Color.BLUE));
 315         assertEquals(expected,result);
 316   }
 317 
 318 }