2 <title>Test ImageBitmap on Worker
</title>
4 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
5 <link rel=
"stylesheet" href=
"/tests/SimpleTest/test.css">
7 <script type=
"text/javascript">
9 // The following tests is not enabled in Worker now:
10 // create from a HTMLImageElement
11 // create from a HTMLVideoElement
12 // create from a HTMLCanvasElement
13 // create from a CanvasRenderingContext2D
14 // call CanvasRenderingContext2D.drawImage()
15 // call CanvasRenderingContext2D.createPaattern()
16 // test security error from an unclean HTHMLImageElemnt
17 // test security error from an unclean HTHMLVideoElemnt
18 // test security error from an tainted HTHMLCanvasElemnt
19 // test security error from an tainted CanvasRenderingContext2D
21 // Task constructor function
22 function Task(aType
, aWidth
, aHeight
, aMsg
, aSource
) {
25 this.height
= aHeight
;
27 this.source
= aSource
;
30 function TaskWithCrop(aType
, aWidth
, aHeight
, aMsg
, aSource
, aSx
, aSy
, aSw
, aSh
) {
31 Task
.call(this, aType
, aWidth
, aHeight
, aMsg
, aSource
);
37 TaskWithCrop
.prototype = Object
.create(Task
.prototype);
38 TaskWithCrop
.prototype.constructor = TaskWithCrop
;
41 tasks
: [], // an arrayf of Task objects
43 if (this.tasks
.length
) {
44 worker
.postMessage(this.tasks
.pop());
52 var worker
= new Worker("imagebitmap_on_worker.js");
53 worker
.onmessage = function(event
) {
54 if (event
.data
.type
== "status") {
55 ok(event
.data
.status
, event
.data
.msg
);
56 } else if (event
.data
.type
== "doneTask") {
57 WORKER_TASKS
.dispatch();
62 ok(worker
, "Worker created successfully.");
64 // prepare an ImageData object
65 var image
= document
.createElement('img');
66 var canvas
= document
.createElement('canvas');
67 var ctx
= canvas
.getContext('2d');
69 image
.src
= "image_rgrg-256x256.png";
70 image
.onload = function() {
71 var width
= image
.naturalWidth
;
72 var height
= image
.naturalHeight
;
74 canvas
.width
= image
.naturalWidth
;
75 canvas
.height
= image
.naturalHeight
;
76 ctx
.drawImage(image
, 0, 0, image
.naturalWidth
, image
.naturalHeight
);
78 imageData
= ctx
.getImageData(0, 0, image
.naturalWidth
, image
.naturalHeight
);
80 // task: test soruce: an ImageData
81 WORKER_TASKS
.tasks
.push(new Task("testImageData", width
, height
, "", imageData
));
83 // task: test soruce: an ImageBitmap
84 WORKER_TASKS
.tasks
.push(new Task("testImageBitmap", width
, height
, "", imageData
));
86 // task: test soruce: a Blob
87 canvas
.toBlob(function(aBlob
) {
88 WORKER_TASKS
.tasks
.push(new Task("testBlob", width
, height
, "", aBlob
));
92 // task: throw exception: general: sw == 0 || sh == 0
93 WORKER_TASKS
.tasks
.push(new TaskWithCrop("testException", 0, 0, "createImageBitmap should throw with 0 width/height", imageData
, 0, 0, 0, 0));
95 // task: throw exception: general: source is a null
96 WORKER_TASKS
.tasks
.push(new TaskWithCrop("testException", 0, 0, "createImageBitmap should throw with null source", null, 0, 0, 0, 0));
98 // task: throw exception: ImageData: an ImageData object whose data attribute is backed by a detached buffer
99 var neuturedImageData
= function getNeuturedImageData(iData
) {
100 worker
.postMessage(iData
.data
.buffer
, [iData
.data
.buffer
]);
102 }(ctx
.getImageData(0, 0, 50, 50));
103 WORKER_TASKS
.tasks
.push(new TaskWithCrop("testException", neuturedImageData
.width
, neuturedImageData
.height
,
104 "createImageBitmap should throw with neutured ImageData",
105 neuturedImageData
, 0, 0, neuturedImageData
.width
, neuturedImageData
.height
));
107 // task: throw exception: Blob: a corrupted blob
108 function getCorruptedBlob(fileName
) {
109 var xhr
= new XMLHttpRequest();
110 xhr
.open("GET", fileName
);
111 xhr
.responseType
= "blob";//force the HTTP response, response-type header to be blob
112 xhr
.onload = function() {
113 WORKER_TASKS
.tasks
.push(new Task("testException", 0, 0, "createImageBitmap should reject promise with corrupted blob", xhr
.response
));
117 getCorruptedBlob("image_error-early.png");
119 // task: throw exception: Blob: non-image file
120 function getNonImageFile(fileName
) {
121 var xhr
= new XMLHttpRequest();
122 xhr
.open("GET", fileName
);
123 xhr
.responseType
= "blob";//force the HTTP response, response-type header to be blob
124 xhr
.onload = function() {
125 WORKER_TASKS
.tasks
.push(new Task("testException", 0, 0, "createImageBitmap should reject promise with non-image blob", xhr
.response
));
127 // start to dispatch tasks to worker
128 WORKER_TASKS
.dispatch();
132 getNonImageFile("imagebitmap_on_worker.js");
134 // task: test bug : bug 1239300
135 WORKER_TASKS
.tasks
.push(new Task("testBug1239300"));
138 SimpleTest
.waitForExplicitFinish();
139 addLoadEvent(runTests
);