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 javafx.scene.canvas; 27 28 import com.sun.javafx.FXUnit; 29 import javafx.scene.NodeTest; 30 import javafx.scene.effect.BlendMode; 31 import javafx.scene.paint.Color; 32 import javafx.scene.shape.ArcType; 33 import javafx.scene.shape.StrokeLineCap; 34 import javafx.scene.shape.StrokeLineJoin; 35 import javafx.scene.transform.Affine; 36 import javafx.scene.transform.Rotate; 37 import javafx.scene.transform.Transform; 38 import org.junit.Rule; 39 import org.junit.Test; 40 import static org.junit.Assert.*; 41 42 public class CanvasTest { 43 44 @Rule 45 public FXUnit fx = new FXUnit(); 46 47 @Test public void testPropertyPropagation_visible() throws Exception { 48 final Canvas node = new Canvas(); 49 NodeTest.testBooleanPropertyPropagation(node, "visible", false, true); 50 } 51 52 // @Test public void testPropertyPropagation_x() throws Exception { 53 // final Canvas node = new Canvas(); 54 // NodeTest.testDoublePropertyPropagation(node, "x", 100, 200); 55 // } 56 // 57 // @Test public void testPropertyPropagation_y() throws Exception { 58 // final Canvas node = new Canvas(); 59 // NodeTest.testDoublePropertyPropagation(node, "y", 100, 200); 60 // } 61 62 @Test public void testInitCanvas() throws Exception { 63 final Canvas node = new Canvas(); 64 } 65 66 @Test public void testGetGC() throws Exception { 67 Canvas node = new Canvas(); 68 GraphicsContext gc = node.getGraphicsContext2D(); 69 } 70 71 //maybe test doing stuff from different threads 72 @Test public void testGetGC2() throws Exception { 73 Canvas node = new Canvas(); 74 GraphicsContext gc = node.getGraphicsContext2D(); 75 GraphicsContext gc2 = node.getGraphicsContext2D(); 76 GraphicsContext gc3 = node.getGraphicsContext2D(); 77 GraphicsContext gc4 = node.getGraphicsContext2D(); 78 GraphicsContext gc5 = node.getGraphicsContext2D(); 79 assertEquals(gc,gc2); 80 assertEquals(gc,gc3); 81 assertEquals(gc,gc4); 82 assertEquals(gc,gc5); 83 } 84 85 //basic tests make sure that the methods do not blow up. 86 @Test public void testGCfillRect_basic() throws Exception { 87 Canvas node = new Canvas(); 88 GraphicsContext gc = node.getGraphicsContext2D(); 89 gc.fillRect(0, 0, 1, 1); 90 } 91 92 @Test public void testGCfillOval_basic() throws Exception { 93 Canvas node = new Canvas(); 94 GraphicsContext gc = node.getGraphicsContext2D(); 95 gc.fillOval(0, 0, 1, 1); 96 } 97 98 @Test public void testGCfillRoundRect_basic() throws Exception { 99 Canvas node = new Canvas(); 100 GraphicsContext gc = node.getGraphicsContext2D(); 101 gc.fillRoundRect(0, 0, 1, 1, 2, 2); 102 } 103 104 @Test public void testGCfillText_basic() throws Exception { 105 Canvas node = new Canvas(); 106 GraphicsContext gc = node.getGraphicsContext2D(); 107 gc.fillText("Test", 0, 0); 108 gc.fillText("", 0, 0); 109 gc.fillText(null, 0, 0); 110 } 111 112 @Test public void testGCfillPolygon_basic() throws Exception { 113 Canvas node = new Canvas(); 114 GraphicsContext gc = node.getGraphicsContext2D(); 115 double[] xPoints = {0.0,10.0}; 116 double[] yPoints = {0.0,10.0}; 117 gc.fillPolygon( xPoints, yPoints, 2); 118 } 119 120 @Test public void testGCfillArc_basic() throws Exception { 121 Canvas node = new Canvas(); 122 GraphicsContext gc = node.getGraphicsContext2D(); 123 double[] xPoints = {0.0,10.0}; 124 double[] yPoints = {0.0,10.0}; 125 gc.fillArc(10, 10, 100, 100, 0, 40, ArcType.OPEN); 126 gc.fillArc(10, 10, 100, 100, 0, 360, ArcType.CHORD); 127 gc.fillArc(10, 10, 100, 100, 0, 361, ArcType.ROUND); 128 } 129 130 @Test public void testGCdrawRect_basic() throws Exception { 131 Canvas node = new Canvas(); 132 GraphicsContext gc = node.getGraphicsContext2D(); 133 gc.rect(0, 0, 1, 1); 134 } 135 136 @Test public void testGCdrawOval_basic() throws Exception { 137 Canvas node = new Canvas(); 138 GraphicsContext gc = node.getGraphicsContext2D(); 139 gc.strokeOval(0, 0, 1, 1); 140 } 141 142 @Test public void testGCdrawRoundRect_basic() throws Exception { 143 Canvas node = new Canvas(); 144 GraphicsContext gc = node.getGraphicsContext2D(); 145 gc.strokeRoundRect(0, 0, 1, 1, 2, 2); 146 } 147 148 @Test public void testGCstrokeText_basic() throws Exception { 149 Canvas node = new Canvas(); 150 GraphicsContext gc = node.getGraphicsContext2D(); 151 gc.strokeText("Test", 0, 0); 152 gc.strokeText("", 0, 0); 153 gc.strokeText(null, 0, 0); 154 } 155 156 @Test public void testGCdrawPolygon_basic() throws Exception { 157 Canvas node = new Canvas(); 158 GraphicsContext gc = node.getGraphicsContext2D(); 159 double[] xPoints = {0.0,10.0}; 160 double[] yPoints = {0.0,10.0}; 161 gc.strokePolygon( xPoints, yPoints, 2); 162 } 163 164 @Test public void testGCdrawArc_basic() throws Exception { 165 Canvas node = new Canvas(); 166 GraphicsContext gc = node.getGraphicsContext2D(); 167 double[] xPoints = {0.0,10.0}; 168 double[] yPoints = {0.0,10.0}; 169 gc.strokeArc(10, 10, 100, 100, 0, 40, ArcType.OPEN); 170 gc.strokeArc(10, 10, 100, 100, 0, 360, ArcType.CHORD); 171 gc.strokeArc(10, 10, 100, 100, 0, 361, ArcType.ROUND); 172 } 173 174 @Test public void testGCfillPath_basic() throws Exception { 175 Canvas node = new Canvas(); 176 GraphicsContext gc = node.getGraphicsContext2D(); 177 gc.arcTo(0, 0, 5, 5, 5); 178 gc.moveTo(50, 50); 179 gc.lineTo(100, 100); 180 gc.bezierCurveTo(75, 150, 75, 150, 80, 80); 181 gc.beginPath(); 182 gc.moveTo(50, 50); 183 gc.lineTo(100, 100); 184 gc.bezierCurveTo(75, 150, 75, 150, 80, 80); 185 gc.arcTo(0, 0, 5, 5, 5); 186 gc.closePath(); 187 } 188 189 @Test public void testGCclip_basic() throws Exception { 190 Canvas node = new Canvas(); 191 GraphicsContext gc = node.getGraphicsContext2D(); 192 gc.beginPath(); 193 gc.moveTo(50, 50); 194 gc.lineTo(100, 100); 195 gc.bezierCurveTo(75, 150, 75, 150, 80, 80); 196 gc.arcTo(0, 0, 5, 5, 5); 197 gc.closePath(); 198 gc.clip(); 199 } 200 201 @Test public void testGCfillDrawPath_basic() throws Exception { 202 Canvas node = new Canvas(); 203 GraphicsContext gc = node.getGraphicsContext2D(); 204 gc.beginPath(); 205 gc.moveTo(50, 50); 206 gc.lineTo(100, 100); 207 gc.bezierCurveTo(75, 150, 75, 150, 80, 80); 208 gc.arcTo(0, 0, 5, 5, 5); 209 gc.closePath(); 210 gc.stroke(); 211 gc.fill(); 212 } 213 214 215 216 @Test public void testGCState_Translate() throws Exception { 217 Canvas node = new Canvas(); 218 GraphicsContext gc = node.getGraphicsContext2D(); 219 220 gc.translate(50, 50); 221 Affine result = gc.getTransform(); 222 Affine expected = new Affine(); 223 224 expected.setTx(50); 225 expected.setTy(50); 226 227 assertMatrix(result, expected); 228 } 229 230 @Test public void testGCState_Scale() throws Exception { 231 Canvas node = new Canvas(); 232 GraphicsContext gc = node.getGraphicsContext2D(); 233 234 gc.scale(3, 3); 235 Affine result = gc.getTransform(); 236 Affine expected = new Affine(); 237 238 expected.setMxx(3); 239 expected.setMyy(3); 240 241 assertMatrix(result, expected); 242 } 243 244 @Test public void testGCState_Rotate() throws Exception { 245 Canvas node = new Canvas(); 246 GraphicsContext gc = node.getGraphicsContext2D(); 247 248 gc.rotate(45.0); 249 Affine result = gc.getTransform(); 250 251 Rotate expected = new Rotate(45, 0, 0); 252 253 assertMatrix(result, expected); 254 } 255 256 @Test public void testGCState_getTransform() throws Exception { 257 Canvas node = new Canvas(); 258 GraphicsContext gc = node.getGraphicsContext2D(); 259 260 261 Affine expected = new Affine(); 262 gc.setTransform(expected); 263 Affine result = gc.getTransform(); 264 265 assertMatrix(result, expected); 266 267 gc.setTransform(expected.getMxx(), expected.getMyx(), expected.getMxy(), 268 expected.getMyy(), expected.getTx(), expected.getTy()); 269 270 Affine result2 = gc.getTransform(); 271 272 assertMatrix(result2, expected); 273 } 274 275 @Test public void testGCState_FillStrokeSaveRestore() throws Exception { 276 Canvas node = new Canvas(); 277 GraphicsContext gc = node.getGraphicsContext2D(); 278 279 Affine expected = new Affine(); 280 gc.setTransform(expected); 281 Affine result = gc.getTransform(); 282 283 assertMatrix(result, expected); 284 285 gc.setFill(Color.BLACK); 286 assertEquals(Color.BLACK, gc.getFill()); 287 gc.save(); 288 gc.setFill(Color.RED); 289 assertEquals(gc.getFill(), Color.RED); 290 gc.restore(); 291 assertEquals(Color.BLACK, gc.getFill()); 292 gc.setStroke(Color.BLACK); 293 assertEquals(Color.BLACK, gc.getStroke()); 294 gc.save(); 295 gc.setStroke(Color.RED); 296 assertEquals(gc.getStroke(), Color.RED); 297 gc.restore(); 298 assertEquals(Color.BLACK, gc.getStroke()); 299 assertMatrix(result, expected); 300 } 301 302 @Test public void testGCState_Line() throws Exception { 303 Canvas node = new Canvas(); 304 GraphicsContext gc = node.getGraphicsContext2D(); 305 306 gc.setLineCap(StrokeLineCap.BUTT); 307 gc.setLineJoin(StrokeLineJoin.MITER); 308 gc.setLineWidth(5); 309 gc.setMiterLimit(3); 310 311 gc.save(); 312 gc.setLineCap(StrokeLineCap.ROUND); 313 gc.setLineJoin(StrokeLineJoin.BEVEL); 314 gc.setLineWidth(1); 315 gc.setMiterLimit(1); 316 assertEquals(gc.getLineCap(), StrokeLineCap.ROUND); 317 assertEquals(gc.getLineJoin(), StrokeLineJoin.BEVEL); 318 assertEquals(gc.getLineWidth(), 1, 0.00001); 319 assertEquals(gc.getMiterLimit(), 1, 0.00001); 320 gc.restore(); 321 322 assertEquals(gc.getLineCap(), StrokeLineCap.BUTT); 323 assertEquals(gc.getLineJoin(), StrokeLineJoin.MITER); 324 assertEquals(gc.getLineWidth(), 5, 0.00001); 325 assertEquals(gc.getMiterLimit(), 3, 0.00001); 326 } 327 328 @Test 329 public void testGCState_LineCapNull() throws Exception { 330 Canvas node = new Canvas(); 331 GraphicsContext gc = node.getGraphicsContext2D(); 332 333 gc.setLineCap(StrokeLineCap.BUTT); 334 gc.setLineCap(null); 335 assertEquals(gc.getLineCap(), StrokeLineCap.BUTT); 336 gc.setLineCap(StrokeLineCap.ROUND); 337 gc.setLineCap(null); 338 assertEquals(gc.getLineCap(), StrokeLineCap.ROUND); 339 gc.setLineCap(StrokeLineCap.SQUARE); 340 gc.setLineCap(null); 341 assertEquals(gc.getLineCap(), StrokeLineCap.SQUARE); 342 } 343 344 @Test 345 public void testGCState_LineJoinNull() throws Exception { 346 Canvas node = new Canvas(); 347 GraphicsContext gc = node.getGraphicsContext2D(); 348 349 gc.setLineJoin(StrokeLineJoin.BEVEL); 350 gc.setLineJoin(null); 351 assertEquals(gc.getLineJoin(), StrokeLineJoin.BEVEL); 352 gc.setLineJoin(StrokeLineJoin.MITER); 353 gc.setLineJoin(null); 354 assertEquals(gc.getLineJoin(), StrokeLineJoin.MITER); 355 gc.setLineJoin(StrokeLineJoin.ROUND); 356 gc.setLineJoin(null); 357 assertEquals(gc.getLineJoin(), StrokeLineJoin.ROUND); 358 } 359 360 @Test public void testGCState_BlendMode() throws Exception { 361 Canvas node = new Canvas(); 362 GraphicsContext gc = node.getGraphicsContext2D(); 363 364 gc.setGlobalBlendMode(BlendMode.ADD); 365 gc.setGlobalAlpha(0); 366 367 gc.save(); 368 gc.setGlobalAlpha(0.5); 369 gc.setGlobalBlendMode(BlendMode.COLOR_BURN); 370 assertEquals(gc.getGlobalBlendMode(), BlendMode.COLOR_BURN); 371 assertEquals(gc.getGlobalAlpha(), 0.5, 0.000001); 372 gc.restore(); 373 374 assertEquals(BlendMode.ADD, gc.getGlobalBlendMode()); 375 assertEquals(0, gc.getGlobalAlpha(), 0.000001); 376 } 377 378 public static void assertMatrix(Transform expected, 379 Transform result) { 380 assertEquals(expected.getMxx(), result.getMxx(), 0.00001); 381 assertEquals(expected.getMxy(), result.getMxy(), 0.00001); 382 assertEquals(expected.getMxz(), result.getMxz(), 0.00001); 383 assertEquals(expected.getTx(), result.getTx(), 0.00001); 384 assertEquals(expected.getMyx(), result.getMyx(), 0.00001); 385 assertEquals(expected.getMyy(), result.getMyy(), 0.00001); 386 assertEquals(expected.getMyz(), result.getMyz(), 0.00001); 387 assertEquals(expected.getTy(), result.getTy(), 0.00001); 388 assertEquals(expected.getMzx(), result.getMzx(), 0.00001); 389 assertEquals(expected.getMzy(), result.getMzy(), 0.00001); 390 assertEquals(expected.getMzz(), result.getMzz(), 0.00001); 391 assertEquals(expected.getTz(), result.getTz(), 0.00001); 392 } 393 }