summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/test_imagebitmap_on_worker.html
blob: bc7ebe11e8d5c47f3aa1b1838f4ce76683ae368d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!DOCTYPE HTML>
<title>Test ImageBitmap on Worker</title>
<meta charset="utf-8">
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css">
<body>
<script type="text/javascript">

// The following tests is not enabled in Worker now:
// create from a HTMLImageElement
// create from a HTMLVideoElement
// create from a HTMLCanvasElement
// create from a CanvasRenderingContext2D
// call CanvasRenderingContext2D.drawImage()
// call CanvasRenderingContext2D.createPaattern()
// test security error from an unclean HTHMLImageElemnt
// test security error from an unclean HTHMLVideoElemnt
// test security error from an tainted HTHMLCanvasElemnt
// test security error from an tainted CanvasRenderingContext2D

// Task constructor function
function Task(aType, aWidth, aHeight, aMsg, aSource) {
  this.type = aType;
  this.width = aWidth;
  this.height = aHeight;
  this.msg = aMsg;
  this.source = aSource;
}

function TaskWithCrop(aType, aWidth, aHeight, aMsg, aSource, aSx, aSy, aSw, aSh) {
  Task.call(this, aType, aWidth, aHeight, aMsg, aSource);
  this.sx = aSx;
  this.sy = aSy;
  this.sw = aSw;
  this.sh = aSh;
}
TaskWithCrop.prototype = Object.create(Task.prototype);
TaskWithCrop.prototype.constructor = TaskWithCrop;

var WORKER_TASKS = {
  tasks: [], // an arrayf of Task objects
  dispatch() {
    if (this.tasks.length) {
      worker.postMessage(this.tasks.pop());
    } else {
      worker.terminate();
      SimpleTest.finish();
    }
  },
};

var worker = new Worker("imagebitmap_on_worker.js");
worker.onmessage = function(event) {
  if (event.data.type == "status") {
    ok(event.data.status, event.data.msg);
  } else if (event.data.type == "doneTask") {
    WORKER_TASKS.dispatch();
  }
}

function runTests() {
  ok(worker, "Worker created successfully.");

  // prepare an ImageData object
  var image = document.createElement('img');
  var canvas = document.createElement('canvas');
  var ctx = canvas.getContext('2d');
  var imageData;
  image.src = "image_rgrg-256x256.png";
  image.onload = function() {
    var width = image.naturalWidth;
    var height = image.naturalHeight;

    canvas.width = image.naturalWidth;
    canvas.height = image.naturalHeight;
    ctx.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight);

    imageData = ctx.getImageData(0, 0, image.naturalWidth, image.naturalHeight);

    // task: test soruce: an ImageData
    WORKER_TASKS.tasks.push(new Task("testImageData", width, height, "", imageData));

    // task: test soruce: an ImageBitmap
    WORKER_TASKS.tasks.push(new Task("testImageBitmap", width, height, "", imageData));

    // task: test soruce: a Blob
    canvas.toBlob(function(aBlob) {
    	WORKER_TASKS.tasks.push(new Task("testBlob", width, height, "", aBlob));
    });
  };

  // task: throw exception: general: sw == 0 || sh == 0
  WORKER_TASKS.tasks.push(new TaskWithCrop("testException", 0, 0, "createImageBitmap should throw with 0 width/height", imageData, 0, 0, 0, 0));

  // task: throw exception: general: source is a null
  WORKER_TASKS.tasks.push(new TaskWithCrop("testException", 0, 0, "createImageBitmap should throw with null source", null, 0, 0, 0, 0));

  // task: throw exception: ImageData: an ImageData object whose data attribute is backed by a detached buffer
  var neuturedImageData = function getNeuturedImageData(iData) {
    worker.postMessage(iData.data.buffer, [iData.data.buffer]);
    return iData;
  }(ctx.getImageData(0, 0, 50, 50));
  WORKER_TASKS.tasks.push(new TaskWithCrop("testException", neuturedImageData.width, neuturedImageData.height,
                                           "createImageBitmap should throw with neutured ImageData",
                                           neuturedImageData, 0, 0, neuturedImageData.width, neuturedImageData.height));

  // task: throw exception: Blob: a corrupted blob
  function getCorruptedBlob(fileName) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", fileName);
    xhr.responseType = "blob";//force the HTTP response, response-type header to be blob
    xhr.onload = function() {
        WORKER_TASKS.tasks.push(new Task("testException", 0, 0, "createImageBitmap should reject promise with corrupted blob", xhr.response));
    }
    xhr.send();
  }
  getCorruptedBlob("image_error-early.png");

  // task: throw exception: Blob: non-image file
  function getNonImageFile(fileName) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", fileName);
    xhr.responseType = "blob";//force the HTTP response, response-type header to be blob
    xhr.onload = function() {
      WORKER_TASKS.tasks.push(new Task("testException", 0, 0, "createImageBitmap should reject promise with non-image blob", xhr.response));

      // start to dispatch tasks to worker
      WORKER_TASKS.dispatch();
    }
    xhr.send();
  }
  getNonImageFile("imagebitmap_on_worker.js");

  // task: test bug : bug 1239300
  WORKER_TASKS.tasks.push(new Task("testBug1239300"));
}

SimpleTest.waitForExplicitFinish();
addLoadEvent(runTests);

</script>
</body>