1 /*
   2  * Copyright (c) 2010, 2018, 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.javafx.scene.canvas;
  27 
  28 import javafx.geometry.VPos;
  29 import test.javafx.scene.NodeTest;
  30 import javafx.scene.canvas.Canvas;
  31 import javafx.scene.canvas.GraphicsContext;
  32 import javafx.scene.effect.BlendMode;
  33 import test.javafx.scene.image.ImageForTesting;
  34 import javafx.scene.paint.Color;
  35 import javafx.scene.shape.ArcType;
  36 import javafx.scene.shape.FillRule;
  37 import javafx.scene.shape.StrokeLineCap;
  38 import javafx.scene.shape.StrokeLineJoin;
  39 import javafx.scene.text.Font;
  40 import javafx.scene.text.FontSmoothingType;
  41 import javafx.scene.text.TextAlignment;
  42 import javafx.scene.transform.Affine;
  43 import javafx.scene.transform.Rotate;
  44 import javafx.scene.transform.Transform;
  45 import org.junit.Before;
  46 import org.junit.Test;
  47 
  48 import static org.junit.Assert.assertEquals;
  49 import static org.junit.Assert.assertArrayEquals;
  50 import static org.junit.Assert.assertNull;
  51 import static org.junit.Assert.assertNotNull;
  52 import static org.junit.Assert.assertTrue;
  53 import static org.junit.Assert.assertFalse;
  54 
  55 public class CanvasTest {
  56 
  57     private Canvas canvas;
  58     private GraphicsContext gc;
  59 
  60     @Before
  61     public void setUp() {
  62         canvas = new Canvas();
  63         gc = canvas.getGraphicsContext2D();
  64     }
  65 
  66     @Test public void testPropertyPropagation_visible() throws Exception {
  67         NodeTest.testBooleanPropertyPropagation(canvas, "visible", false, true);
  68     }
  69 
  70     //maybe test doing stuff from different threads
  71     @Test public void testGetGC2() throws Exception {
  72         GraphicsContext gc2 = canvas.getGraphicsContext2D();
  73         GraphicsContext gc3 = canvas.getGraphicsContext2D();
  74         GraphicsContext gc4 = canvas.getGraphicsContext2D();
  75         GraphicsContext gc5 = canvas.getGraphicsContext2D();
  76         assertEquals(gc,gc2);
  77         assertEquals(gc,gc3);
  78         assertEquals(gc,gc4);
  79         assertEquals(gc,gc5);
  80     }
  81 
  82     //basic tests make sure that the methods do not blow up.
  83     @Test public void testGCfillRect_basic() throws Exception {
  84         gc.fillRect(0, 0, 1, 1);
  85     }
  86 
  87     @Test public void testGCfillOval_basic() throws Exception {
  88         gc.fillOval(0, 0, 1, 1);
  89     }
  90 
  91     @Test public void testGCfillRoundRect_basic() throws Exception {
  92         gc.fillRoundRect(0, 0, 1, 1, 2, 2);
  93     }
  94 
  95     @Test public void testGCfillText_basic() throws Exception {
  96         gc.fillText("Test", 0, 0);
  97         gc.fillText("Test", 0, 0, 0);
  98         gc.fillText("", 0, 0, 0);
  99         gc.fillText("", 0, 0);
 100         gc.fillText(null, 0, 0);
 101         gc.fillText(null, 0, 0, 0);
 102     }
 103 
 104     @Test public void testGCfillPolygon_basic() throws Exception {
 105         double[] xPoints = {0.0,10.0};
 106         double[] yPoints = {0.0,10.0};
 107         gc.fillPolygon( xPoints, yPoints, 2);
 108         gc.fillPolygon( xPoints, null, 2);
 109         gc.fillPolygon( null, yPoints, 2);
 110     }
 111 
 112     @Test public void testGCfillArc_basic() throws Exception {
 113         gc.fillArc(10, 10, 100, 100, 0, 40, ArcType.OPEN);
 114         gc.fillArc(10, 10, 100, 100, 0, 360, ArcType.CHORD);
 115         gc.fillArc(10, 10, 100, 100, 0, 361, ArcType.ROUND);
 116         gc.fillArc(10, 10, 100, 100, 0, 361, null);
 117     }
 118 
 119     @Test public void testGCdrawRect_basic() throws Exception {
 120         gc.rect(0, 0, 1, 1);
 121     }
 122 
 123     @Test public void testGCdrawOval_basic() throws Exception {
 124         gc.strokeOval(0, 0, 1, 1);
 125     }
 126 
 127     @Test public void testGCdrawRoundRect_basic() throws Exception {
 128         gc.strokeRoundRect(0, 0, 1, 1, 2, 2);
 129     }
 130 
 131     @Test public void testGCstrokeText_basic() throws Exception {
 132         gc.strokeText("Test", 0, 0);
 133         gc.strokeText("", 0, 0);
 134         gc.strokeText(null, 0, 0);
 135     }
 136 
 137     @Test public void testGCdrawPolygon_basic() throws Exception {
 138         double[] xPoints = {0.0,10.0};
 139         double[] yPoints = {0.0,10.0};
 140         gc.strokePolygon( xPoints, yPoints, 2);
 141         gc.strokePolygon( null, yPoints, 2);
 142         gc.strokePolygon( xPoints, null, 2);
 143     }
 144 
 145     @Test public void testGCdrawArc_basic() throws Exception {
 146         gc.strokeArc(10, 10, 100, 100, 0, 40, ArcType.OPEN);
 147         gc.strokeArc(10, 10, 100, 100, 0, 360, ArcType.CHORD);
 148         gc.strokeArc(10, 10, 100, 100, 0, 361, ArcType.ROUND);
 149         gc.strokeArc(10, 10, 100, 100, 0, 361, null);
 150     }
 151 
 152     @Test public void testGCfillPath_basic() throws Exception {
 153         gc.arcTo(0, 0, 5, 5, 5);
 154         gc.moveTo(50, 50);
 155         gc.lineTo(100, 100);
 156         gc.bezierCurveTo(75, 150, 75, 150, 80, 80);
 157         gc.beginPath();
 158         gc.moveTo(50, 50);
 159         gc.lineTo(100, 100);
 160         gc.bezierCurveTo(75, 150, 75, 150, 80, 80);
 161         gc.arcTo(0, 0, 5, 5, 5);
 162         gc.closePath();
 163     }
 164 
 165     @Test public void testGCclip_basic() throws Exception {
 166         gc.beginPath();
 167         gc.moveTo(50, 50);
 168         gc.lineTo(100, 100);
 169         gc.bezierCurveTo(75, 150, 75, 150, 80, 80);
 170         gc.arcTo(0, 0, 5, 5, 5);
 171         gc.closePath();
 172         gc.clip();
 173     }
 174 
 175     @Test public void testGCfillDrawPath_basic() throws Exception {
 176         gc.beginPath();
 177         gc.moveTo(50, 50);
 178         gc.lineTo(100, 100);
 179         gc.bezierCurveTo(75, 150, 75, 150, 80, 80);
 180         gc.arcTo(0, 0, 5, 5, 5);
 181         gc.closePath();
 182         gc.stroke();
 183         gc.fill();
 184     }
 185 
 186     @Test public void testGCPath_LineTo_NoMoveto() throws Exception {
 187         gc.lineTo(10, 10);
 188     }
 189 
 190     @Test public void testGCPath_QuadraticCurveTo_NoMoveto() throws Exception {
 191         gc.quadraticCurveTo(10, 10, 20, 20);
 192     }
 193 
 194     @Test public void testGCPath_BezierCurveTo_NoMoveto() throws Exception {
 195         gc.bezierCurveTo(10, 10, 20, 20, 30, 30);
 196     }
 197 
 198     @Test public void testGCPath_ArcTo_NoMoveto() throws Exception {
 199         gc.arcTo(10, 10, 20, 20, 30);
 200     }
 201 
 202     @Test public void testGCPath_Arc_NoMoveto() throws Exception {
 203         gc.arc(10, 10, 20, 20, 30, 30);
 204     }
 205 
 206     @Test public void testGCPath_ClosePath_NoMoveto() throws Exception {
 207         gc.closePath();
 208     }
 209 
 210     @Test public void testGCPath_Rect_NoMoveto() throws Exception {
 211         gc.rect(10, 10, 20, 20);
 212     }
 213 
 214     @Test public void testGCState_Translate() throws Exception {
 215         gc.translate(50, 50);
 216         Affine result = gc.getTransform();
 217         Affine expected = new Affine();
 218 
 219         expected.setTx(50);
 220         expected.setTy(50);
 221 
 222         assertMatrix(result, expected);
 223     }
 224 
 225     @Test public void testGCState_Scale() throws Exception {
 226         gc.scale(3, 3);
 227         Affine result = gc.getTransform();
 228         Affine expected = new Affine();
 229 
 230         expected.setMxx(3);
 231         expected.setMyy(3);
 232 
 233         assertMatrix(result, expected);
 234     }
 235 
 236     @Test public void testGCState_Rotate() throws Exception {
 237         gc.rotate(45.0);
 238         Affine result = gc.getTransform();
 239 
 240         Rotate expected = new Rotate(45, 0, 0);
 241 
 242         assertMatrix(result, expected);
 243     }
 244 
 245     @Test public void testGCState_getTransform() throws Exception {
 246         Affine expected = new Affine();
 247         gc.setTransform(expected);
 248         Affine result = gc.getTransform();
 249 
 250         assertMatrix(result, expected);
 251 
 252         gc.setTransform(expected.getMxx(), expected.getMyx(), expected.getMxy(),
 253                 expected.getMyy(), expected.getTx(), expected.getTy());
 254 
 255         Affine result2 = gc.getTransform();
 256 
 257         assertMatrix(result2, expected);
 258     }
 259 
 260     @Test public void testGCState_FillStrokeSaveRestore() throws Exception {
 261         Affine expected = new Affine();
 262         gc.setTransform(expected);
 263         Affine result = gc.getTransform();
 264 
 265         assertMatrix(result, expected);
 266 
 267         gc.setFill(Color.BLACK);
 268         assertEquals(Color.BLACK, gc.getFill());
 269         gc.save();
 270         gc.setFill(Color.RED);
 271         assertEquals(gc.getFill(), Color.RED);
 272         gc.restore();
 273         assertEquals(Color.BLACK, gc.getFill());
 274         gc.setStroke(Color.BLACK);
 275         assertEquals(Color.BLACK, gc.getStroke());
 276         gc.save();
 277         gc.setStroke(Color.RED);
 278         assertEquals(gc.getStroke(), Color.RED);
 279         gc.restore();
 280         assertEquals(Color.BLACK, gc.getStroke());
 281         assertMatrix(result, expected);
 282     }
 283 
 284     @Test public void testGCState_SetStroke() {
 285         gc.setStroke(Color.RED);
 286         assertEquals(Color.RED, gc.getStroke());
 287         gc.setStroke(null);
 288         assertEquals(Color.RED, gc.getStroke());
 289     }
 290 
 291     @Test public void testGCState_Fill_Null() {
 292         gc.setFill(Color.BLACK);
 293         assertEquals(Color.BLACK, gc.getFill());
 294         gc.setFill(Color.RED);
 295         assertEquals(gc.getFill(), Color.RED);
 296         gc.setFill(null);
 297         assertEquals(gc.getFill(), Color.RED);
 298     }
 299 
 300     @Test public void testGCState_FillRule_Null() {
 301         gc.setFillRule(FillRule.EVEN_ODD);
 302         assertEquals(FillRule.EVEN_ODD, gc.getFillRule());
 303         gc.setFillRule(null);
 304         assertEquals(FillRule.EVEN_ODD, gc.getFillRule());
 305     }
 306 
 307     @Test public void testGCState_Font_Null() {
 308         Font f = new Font(10);
 309         gc.setFont(f);
 310         assertEquals(f, gc.getFont());
 311         gc.setFont(null);
 312         assertEquals(f, gc.getFont());
 313     }
 314 
 315     @Test public void testGCState_FontSmoothing_Null() {
 316         gc.setFontSmoothingType(FontSmoothingType.GRAY);
 317         assertEquals(FontSmoothingType.GRAY, gc.getFontSmoothingType());
 318         gc.setFontSmoothingType(null);
 319         assertEquals(FontSmoothingType.GRAY, gc.getFontSmoothingType());
 320     }
 321 
 322     @Test public void testGCState_TextBaseline_Null() {
 323         gc.setTextBaseline(VPos.BASELINE);
 324         assertEquals(VPos.BASELINE, gc.getTextBaseline());
 325         gc.setTextBaseline(null);
 326         assertEquals(VPos.BASELINE, gc.getTextBaseline());
 327     }
 328 
 329     @Test public void testGCState_TextAlign_Null() {
 330         gc.setTextAlign(TextAlignment.JUSTIFY);
 331         assertEquals(TextAlignment.JUSTIFY, gc.getTextAlign());
 332         gc.setTextAlign(null);
 333         assertEquals(TextAlignment.JUSTIFY, gc.getTextAlign());
 334     }
 335 
 336     @Test public void testGCState_Line() throws Exception {
 337         gc.setLineCap(StrokeLineCap.BUTT);
 338         gc.setLineJoin(StrokeLineJoin.MITER);
 339         gc.setLineWidth(5);
 340         gc.setMiterLimit(3);
 341 
 342         gc.save();
 343         gc.setLineCap(StrokeLineCap.ROUND);
 344         gc.setLineJoin(StrokeLineJoin.BEVEL);
 345         gc.setLineWidth(1);
 346         gc.setMiterLimit(1);
 347         gc.setLineDashes(10, 10);
 348         gc.setLineDashOffset(100);
 349         assertEquals(gc.getLineCap(), StrokeLineCap.ROUND);
 350         assertEquals(gc.getLineJoin(), StrokeLineJoin.BEVEL);
 351         assertEquals(gc.getLineWidth(), 1, 0.00001);
 352         assertEquals(gc.getMiterLimit(), 1, 0.00001);
 353         assertArrayEquals(gc.getLineDashes(), new double[] {10, 10}, 0.00001);
 354         assertEquals(gc.getLineDashOffset(), 100, 0.00001);
 355         gc.restore();
 356 
 357         assertEquals(gc.getLineCap(), StrokeLineCap.BUTT);
 358         assertEquals(gc.getLineJoin(), StrokeLineJoin.MITER);
 359         assertEquals(gc.getLineWidth(), 5, 0.00001);
 360         assertEquals(gc.getMiterLimit(), 3, 0.00001);
 361         assertNull(gc.getLineDashes());
 362         assertEquals(gc.getLineDashOffset(), 0, 0.00001);
 363     }
 364 
 365     @Test
 366     public void testGCState_LineCapNull() throws Exception {
 367         gc.setLineCap(StrokeLineCap.BUTT);
 368         gc.setLineCap(null);
 369         assertEquals(gc.getLineCap(), StrokeLineCap.BUTT);
 370         gc.setLineCap(StrokeLineCap.ROUND);
 371         gc.setLineCap(null);
 372         assertEquals(gc.getLineCap(), StrokeLineCap.ROUND);
 373         gc.setLineCap(StrokeLineCap.SQUARE);
 374         gc.setLineCap(null);
 375         assertEquals(gc.getLineCap(), StrokeLineCap.SQUARE);
 376     }
 377 
 378     @Test
 379     public void testGCState_LineJoinNull() throws Exception {
 380         gc.setLineJoin(StrokeLineJoin.BEVEL);
 381         gc.setLineJoin(null);
 382         assertEquals(gc.getLineJoin(), StrokeLineJoin.BEVEL);
 383         gc.setLineJoin(StrokeLineJoin.MITER);
 384         gc.setLineJoin(null);
 385         assertEquals(gc.getLineJoin(), StrokeLineJoin.MITER);
 386         gc.setLineJoin(StrokeLineJoin.ROUND);
 387         gc.setLineJoin(null);
 388         assertEquals(gc.getLineJoin(), StrokeLineJoin.ROUND);
 389     }
 390 
 391     @Test
 392     public void testGCState_LineDashNonPositive() throws Exception {
 393         gc.setLineDashes(20, 10);
 394         assertArrayEquals(gc.getLineDashes(), new double[] {20, 10}, 0.00001);
 395         gc.setLineDashes(1, Double.NaN);
 396         assertArrayEquals(gc.getLineDashes(), new double[] {20, 10}, 0.00001);
 397         gc.setLineDashes(1, Double.POSITIVE_INFINITY);
 398         assertArrayEquals(gc.getLineDashes(), new double[] {20, 10}, 0.00001);
 399         gc.setLineDashes(1, -1);
 400         assertArrayEquals(gc.getLineDashes(), new double[] {20, 10}, 0.00001);
 401     }
 402 
 403     @Test
 404     public void testGCState_LineDashNull() throws Exception {
 405         gc.setLineDashes(10, 10);
 406         assertNotNull(gc.getLineDashes());
 407         gc.setLineDashes(null);
 408         assertNull(gc.getLineDashes());
 409 
 410         gc.setLineDashes(10, 10);
 411         assertNotNull(gc.getLineDashes());
 412         gc.setLineDashes();
 413         assertNull(gc.getLineDashes());
 414 
 415         gc.setLineDashes(10, 10);
 416         assertNotNull(gc.getLineDashes());
 417         gc.setLineDashes(new double[0]);
 418         assertNull(gc.getLineDashes());
 419 
 420         gc.setLineDashes(10, 10);
 421         assertNotNull(gc.getLineDashes());
 422         gc.setLineDashes(0, 0);
 423         assertNull(gc.getLineDashes());
 424     }
 425 
 426     @Test
 427     public void testGCState_LineDashOddLength() throws Exception {
 428         gc.setLineDashes(10);
 429         assertArrayEquals(gc.getLineDashes(), new double[] {10, 10}, 0.00001);
 430         gc.setLineDashes(10, 20, 30);
 431         assertArrayEquals(gc.getLineDashes(), new double[] {10, 20, 30, 10, 20, 30}, 0.00001);
 432     }
 433 
 434     @Test
 435     public void testGCState_LineDashOffsetNonFinite() throws Exception {
 436         gc.setLineDashOffset(5.0);
 437         assertEquals(gc.getLineDashOffset(), 5.0, 0.00001);
 438         gc.setLineDashOffset(Double.NaN);
 439         assertEquals(gc.getLineDashOffset(), 5.0, 0.00001);
 440         gc.setLineDashOffset(Double.NEGATIVE_INFINITY);
 441         assertEquals(gc.getLineDashOffset(), 5.0, 0.00001);
 442         gc.setLineDashOffset(Double.POSITIVE_INFINITY);
 443         assertEquals(gc.getLineDashOffset(), 5.0, 0.00001);
 444     }
 445 
 446     @Test public void testGCState_BlendMode() throws Exception {
 447         gc.setGlobalBlendMode(BlendMode.ADD);
 448         gc.setGlobalAlpha(0);
 449 
 450         gc.save();
 451         gc.setGlobalAlpha(0.5);
 452         gc.setGlobalBlendMode(BlendMode.COLOR_BURN);
 453         assertEquals(gc.getGlobalBlendMode(), BlendMode.COLOR_BURN);
 454         assertEquals(gc.getGlobalAlpha(), 0.5, 0.000001);
 455         gc.restore();
 456 
 457         assertEquals(BlendMode.ADD, gc.getGlobalBlendMode());
 458         assertEquals(0, gc.getGlobalAlpha(), 0.000001);
 459     }
 460 
 461     @Test public void testGCState_BlendMode_Null() {
 462         gc.setGlobalBlendMode(BlendMode.ADD);
 463         assertEquals(BlendMode.ADD, gc.getGlobalBlendMode());
 464         gc.setGlobalBlendMode(null);
 465         assertEquals(BlendMode.ADD, gc.getGlobalBlendMode());
 466     }
 467 
 468     @Test public void testGCState_ImageSmoothing() {
 469         assertTrue("Image smoothing should be enabled by default.", gc.isImageSmoothing());
 470         // Disable image smoothing.
 471         gc.setImageSmoothing(false);
 472         assertFalse("Image smoothing should be disabled.", gc.isImageSmoothing());
 473         // Reset image smoothing to true.
 474         gc.setImageSmoothing(true);
 475         assertTrue("Image smoothing should be enabled.", gc.isImageSmoothing());
 476     }
 477 
 478     @Test public void testGCappendSVGPath_Null() {
 479         gc.appendSVGPath("m 0 0");
 480         gc.appendSVGPath("Q 150 -300 300 0");
 481         gc.appendSVGPath(null);
 482     }
 483 
 484     @Test public void testGCappendSVGPath_IncorrectPath() {
 485         gc.appendSVGPath("Q 150 -300 300 0"); // No move at the beginning
 486     }
 487 
 488     @Test public void testGCappendSVGPath_IncorrectPath2() {
 489         gc.appendSVGPath("F 150"); // No move at the beginning
 490     }
 491 
 492     @Test public void testGCapplyEffect_Null() {
 493         gc.applyEffect(null);
 494     }
 495 
 496     @Test public void testGCdrawImage_Null() {
 497         gc.drawImage(null, 0 ,0);
 498         gc.drawImage(null, 0 ,0, 100, 100);
 499         gc.drawImage(null, 0, 0, 100, 100, 0, 0, 100, 100);
 500     }
 501 
 502     @Test public void testGCdrawImage_InProgress() {
 503         ImageForTesting image = new ImageForTesting("http://something.png", false);
 504         image.updateProgress(0.5);
 505 
 506         gc.drawImage(image, 0 ,0);
 507         gc.drawImage(image, 0 ,0, 100, 100);
 508         gc.drawImage(image, 0, 0, 100, 100, 0, 0, 100, 100);
 509     }
 510 
 511     public static void assertMatrix(Transform expected,
 512             Transform result) {
 513         assertEquals(expected.getMxx(), result.getMxx(), 0.00001);
 514         assertEquals(expected.getMxy(), result.getMxy(), 0.00001);
 515         assertEquals(expected.getMxz(), result.getMxz(), 0.00001);
 516         assertEquals(expected.getTx(), result.getTx(), 0.00001);
 517         assertEquals(expected.getMyx(), result.getMyx(), 0.00001);
 518         assertEquals(expected.getMyy(), result.getMyy(), 0.00001);
 519         assertEquals(expected.getMyz(), result.getMyz(), 0.00001);
 520         assertEquals(expected.getTy(), result.getTy(), 0.00001);
 521         assertEquals(expected.getMzx(), result.getMzx(), 0.00001);
 522         assertEquals(expected.getMzy(), result.getMzy(), 0.00001);
 523         assertEquals(expected.getMzz(), result.getMzz(), 0.00001);
 524         assertEquals(expected.getTz(), result.getTz(), 0.00001);
 525     }
 526 }