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