offscreencanvas.getcontext.worker.js (3194B)
1 // spec link: https://html.spec.whatwg.org/#dom-offscreencanvas-getcontext 2 3 importScripts("/resources/testharness.js"); 4 importScripts("/html/canvas/resources/canvas-tests.js"); 5 6 test(function() { 7 var offscreenCanvas = new OffscreenCanvas(1, 1); 8 assert_throws_js(TypeError, function() { offscreenCanvas.getContext('3d'); }); 9 }, "Test that getContext with un-supported string throws a TypeError."); 10 11 test(function() { 12 var offscreenCanvas1 = new OffscreenCanvas(1, 1); 13 var ctx1 = offscreenCanvas1.getContext('2d'); 14 assert_true(ctx1 instanceof OffscreenCanvasRenderingContext2D); 15 16 var offscreenCanvas2 = new OffscreenCanvas(1, 1); 17 var ctx2 = offscreenCanvas2.getContext('webgl'); 18 assert_true(ctx2 instanceof WebGLRenderingContext); 19 20 var offscreenCanvas3 = new OffscreenCanvas(1, 1); 21 var ctx3 = offscreenCanvas3.getContext('webgl2'); 22 assert_true(ctx3 instanceof WebGL2RenderingContext); 23 }, "Test that getContext with supported string returns correct results"); 24 25 test(function() { 26 var offscreenCanvas1 = new OffscreenCanvas(1, 1); 27 var ctx1 = offscreenCanvas1.getContext('2d'); 28 var ctx2 = offscreenCanvas1.getContext('webgl'); 29 assert_equals(ctx2, null); 30 31 var offscreenCanvas2 = new OffscreenCanvas(1, 1); 32 var ctx3 = offscreenCanvas2.getContext('webgl'); 33 var ctx4 = offscreenCanvas2.getContext('2d'); 34 assert_equals(ctx4, null); 35 }, "Test that getContext twice with different context type returns null the second time"); 36 37 test(function() { 38 var offscreenCanvas = new OffscreenCanvas(1, 2); 39 var ctx = offscreenCanvas.getContext('2d'); 40 var dstCanvas = ctx.canvas; 41 assert_equals(dstCanvas.width, 1); 42 assert_equals(dstCanvas.height, 2); 43 }, "Test that 2dcontext.canvas should return the original OffscreenCanvas"); 44 45 test(function() { 46 var offscreenCanvas = new OffscreenCanvas(1, 2); 47 var ctx = offscreenCanvas.getContext('webgl'); 48 var dstCanvas = ctx.canvas; 49 assert_equals(dstCanvas.width, 1); 50 assert_equals(dstCanvas.height, 2); 51 }, "Test that webglcontext.canvas should return the original OffscreenCanvas"); 52 53 test(function() { 54 var offscreenCanvas = new OffscreenCanvas(10, 10); 55 var ctx = offscreenCanvas.getContext('2d', {alpha: false}); 56 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)'; 57 ctx.fillRect(0, 0, 10, 10); 58 _assertPixelApprox(offscreenCanvas, 5,5, 0,127,0,255, 2); 59 }, "Test that OffscreenCanvasRenderingContext2D with alpha disabled makes the OffscreenCanvas opaque"); 60 61 test(function() { 62 var offscreenCanvas = new OffscreenCanvas(10, 10); 63 var ctx = offscreenCanvas.getContext('2d', {alpha: true}); 64 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)'; 65 ctx.fillRect(0, 0, 10, 10); 66 _assertPixelApprox(offscreenCanvas, 5,5, 0,255,0,127, 2); 67 }, "Test that OffscreenCanvasRenderingContext2D with alpha enabled preserves the alpha"); 68 69 test(function() { 70 var offscreenCanvas = new OffscreenCanvas(10, 10); 71 var ctx = offscreenCanvas.getContext('2d'); 72 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)'; 73 ctx.fillRect(0, 0, 10, 10); 74 _assertPixelApprox(offscreenCanvas, 5,5, 0,255,0,127, 2); 75 }, "Test that 'alpha' context creation attribute is true by default"); 76 77 done();