blob: 4354b9ac4188cd904f7e941001e724f78b0bde54 [file] [log] [blame]
xidachen9bf98772016-11-10 20:27:011<!DOCTYPE html>
2<meta charset="utf-8">
3<title>Canvas's ImageBitmapRenderingContext test</title>
4<script src="/resources/testharness.js"></script>
5<script src="/resources/testharnessreport.js"></script>
6<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context">
7<script>
8var width = 10;
9var height = 10;
10
11function testCanvas(ctx, r, g, b, a)
12{
13 var color = ctx.getImageData(5, 5, 1, 1).data;
14 assert_array_equals(color, [r, g, b, a]);
15}
16
17function consumeImageBitmap(image, alphaVal, expectedR, expectedG, expectedB, expectedA)
18{
19 var dstCanvas = document.createElement('canvas');
20 dstCanvas.width = width;
21 dstCanvas.height = height;
22 var dstCtx;
23 if (alphaVal == 'true')
24 dstCtx = dstCanvas.getContext('bitmaprenderer', { alpha: true });
25 else if (alphaVal == 'false')
26 dstCtx = dstCanvas.getContext('bitmaprenderer', { alpha: false });
27 else
28 dstCtx = dstCanvas.getContext('bitmaprenderer');
29 dstCtx.transferFromImageBitmap(image);
30
31 var myCanvas = document.createElement('canvas');
32 myCanvas.width = width;
33 myCanvas.height = height;
34 var myCtx = myCanvas.getContext('2d');
35 myCtx.drawImage(dstCanvas, 0, 0);
36 testCanvas(myCtx, expectedR, expectedG, expectedB, expectedA);
37}
38
39promise_test(function() {
40 var srcCanvas = document.createElement('canvas');
41 srcCanvas.width = width;
42 srcCanvas.height = height;
43 var ctx = srcCanvas.getContext('2d');
44 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
45 ctx.fillRect(0, 0, width, height);
xidachen47e0eb92016-11-14 19:43:1146 return createImageBitmap(srcCanvas).then(function(image) {
xidachen9bf98772016-11-10 20:27:0147 consumeImageBitmap(image, 'false', 0, 127, 0, 255);
48 });
49}, "Test that an ImageBitmapRenderingContext with alpha disabled makes the canvas opaque");
50
51promise_test(function() {
52 var srcCanvas = document.createElement('canvas');
53 srcCanvas.width = width;
54 srcCanvas.height = height;
55 var ctx = srcCanvas.getContext('2d');
56 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
57 ctx.fillRect(0, 0, width, height);
xidachen47e0eb92016-11-14 19:43:1158 return createImageBitmap(srcCanvas).then(function(image) {
xidachen9bf98772016-11-10 20:27:0159 consumeImageBitmap(image, 'true', 0, 255, 0, 127);
60 });
61}, "Test that an ImageBitmapRenderingContext with alpha enabled preserves the alpha");
62
63promise_test(function() {
64 var srcCanvas = document.createElement('canvas');
65 srcCanvas.width = width;
66 srcCanvas.height = height;
67 var ctx = srcCanvas.getContext('2d');
68 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
69 ctx.fillRect(0, 0, width, height);
xidachen47e0eb92016-11-14 19:43:1170 return createImageBitmap(srcCanvas).then(function(image) {
xidachen9bf98772016-11-10 20:27:0171 consumeImageBitmap(image, '', 0, 255, 0, 127);
72 });
73}, "Test that the 'alpha' context creation attribute is true by default");
74
75</script>