Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2357)

Unified Diff: third_party/WebKit/LayoutTests/fast/canvas/canvas-createImageBitmap-resize.html

Issue 2035113002: Implement ImageBitmapOptions resize (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: spec is clear, patch is now ready Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/fast/canvas/canvas-createImageBitmap-resize.html
diff --git a/third_party/WebKit/LayoutTests/fast/canvas/canvas-createImageBitmap-resize.html b/third_party/WebKit/LayoutTests/fast/canvas/canvas-createImageBitmap-resize.html
new file mode 100644
index 0000000000000000000000000000000000000000..0a5073c57d664b39757fd7c32bd06fd751087e7b
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/fast/canvas/canvas-createImageBitmap-resize.html
@@ -0,0 +1,165 @@
+<!DOCTYPE HTML>
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script>
+function checkNoCrop(imageBitmap)
+{
+ var canvas = document.createElement("canvas");
+ canvas.width = 50;
+ canvas.height = 50;
+ var ctx = canvas.getContext("2d");
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ ctx.drawImage(imageBitmap, 0, 0);
+ var d = ctx.getImageData(0, 0, 1, 1).data;
+ assert_array_equals(d, [255, 0, 0, 255], "This pixel should be red.");
+ console.log(d[0] + " " + d[1] + " " + d[2] + " " + d[3]);
+ d = ctx.getImageData(39, 0, 1, 1).data;
+ assert_array_equals(d, [0, 255, 0, 255], "This pixel should be green.");
+ console.log(d[0] + " " + d[1] + " " + d[2] + " " + d[3]);
+ d = ctx.getImageData(0, 39, 1, 1).data;
+ assert_array_equals(d, [0, 0, 255, 255], "This pixel should be blue.");
+ console.log(d[0] + " " + d[1] + " " + d[2] + " " + d[3]);
+ d = ctx.getImageData(39, 39, 1, 1).data;
+ assert_array_equals(d, [0, 0, 0, 255], "This pixel should be black.");
+ console.log(d[0] + " " + d[1] + " " + d[2] + " " + d[3]);
+ d = ctx.getImageData(41, 41, 1, 1).data;
+ assert_array_equals(d, [0, 0, 0, 0], "This pixel should be transparent black.");
+ console.log(d[0] + " " + d[1] + " " + d[2] + " " + d[3]);
+}
+
+function checkCrop(imageBitmap)
+{
+ var canvas = document.createElement("canvas");
+ canvas.width = 50;
+ canvas.height = 50;
+ var ctx = canvas.getContext("2d");
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ ctx.drawImage(imageBitmap, 0, 0);
+ var d = ctx.getImageData(0, 0, 1, 1).data;
+ assert_array_equals(d, [255, 0, 0, 255], "This pixel should be red.");
+ d = ctx.getImageData(19, 0, 1, 1).data;
+ assert_array_equals(d, [0, 255, 0, 255], "This pixel should be green.");
+ d = ctx.getImageData(0, 19, 1, 1).data;
+ assert_array_equals(d, [0, 0, 255, 255], "This pixel should be blue.");
+ d = ctx.getImageData(19, 19, 1, 1).data;
+ assert_array_equals(d, [0, 0, 0, 255], "This pixel should be black.");
+ d = ctx.getImageData(21, 21, 1, 1).data;
+ assert_array_equals(d, [0, 0, 0, 0], "This pixel should be transparent black.");
+}
+
+function compareBitmaps(bitmap1, bitmap2, isTheSame)
+{
+ var canvas1 = document.createElement("canvas");
+ var canvas2 = document.createElement("canvas");
+ canvas1.width = 50;
+ canvas1.height = 50;
+ canvas2.width = 50;
+ canvas2.height = 50;
+ var ctx1 = canvas1.getContext("2d");
+ var ctx2 = canvas2.getContext("2d");
+ ctx1.clearRect(0, 0, canvas1.width, canvas1.height);
+ ctx2.clearRect(0, 0, canvas2.width, canvas2.height);
+ ctx1.drawImage(bitmap1, 0, 0);
+ ctx2.drawImage(bitmap2, 0, 0);
+ var data1 = ctx1.getImageData(0, 0, 50, 50).data;
+ var data2 = ctx2.getImageData(0, 0, 50, 50).data;
+ var dataMatched = true;
+ for (var i = 0; i < data1.length; i++) {
+ if (data1[i] != data2[i]) {
+ dataMatched = false;
+ break;
+ }
+ }
+ assert_equals(dataMatched, isTheSame, "These two bitmaps should be different.");
jbroman 2016/07/07 19:17:59 What's the purpose of this "isTheSame" parameter,
xidachen 2016/07/07 20:43:29 Done.
+}
+
+function testImageBitmap(source)
+{
+ var imageBitmaps = {};
+ var p1 = createImageBitmap(source, {resizeWidth: 40, resizeHeight: 40, resizeQuality: "high"}).then(function (image) { imageBitmaps.noCropHigh = image; });
+ var p2 = createImageBitmap(source, {resizeWidth: 40, resizeHeight: 40, resizeQuality: "medium"}).then(function (image) { imageBitmaps.noCropMedium = image; });
+ var p3 = createImageBitmap(source, {resizeWidth: 40, resizeHeight: 40, resizeQuality: "low"}).then(function (image) { imageBitmaps.noCropLow = image; });
+ var p4 = createImageBitmap(source, {resizeWidth: 40, resizeHeight: 40, resizeQuality: "pixelated"}).then(function (image) { imageBitmaps.noCropPixelated = image; });
+ var p5 = createImageBitmap(source, 10, 10, 20, 20, {resizeWidth: 40, resizeHeight: 40, resizeQuality: "high"}).then(function (image) { imageBitmaps.cropHigh = image; });
+ var p6 = createImageBitmap(source, 10, 10, 20, 20, {resizeWidth: 40, resizeHeight: 40, resizeQuality: "medium"}).then(function (image) { imageBitmaps.cropMedium = image; });
+ var p7 = createImageBitmap(source, 10, 10, 20, 20, {resizeWidth: 40, resizeHeight: 40, resizeQuality: "low"}).then(function (image) { imageBitmaps.cropLow = image; });
+ var p8 = createImageBitmap(source, 10, 10, 20, 20, {resizeWidth: 40, resizeHeight: 40, resizeQuality: "pixelated"}).then(function (image) { imageBitmaps.cropPixelated = image; });
+ return Promise.all([p1, p2, p3, p4, p5, p6, p7, p8]).then(function() {
jbroman 2016/07/07 19:17:59 super-nit: it's a little funny to have these push
xidachen 2016/07/07 20:43:29 Done.
+ checkNoCrop(imageBitmaps.noCropHigh);
+ checkNoCrop(imageBitmaps.noCropMedium);
+ checkNoCrop(imageBitmaps.noCropLow);
+ checkNoCrop(imageBitmaps.noCropPixelated);
+ checkCrop(imageBitmaps.cropHigh);
+ checkCrop(imageBitmaps.cropMedium);
+ checkCrop(imageBitmaps.cropLow);
+ checkCrop(imageBitmaps.cropPixelated);
+ // Brute-force comparison among all bitmaps is too expense
jbroman 2016/07/07 19:18:00 nit: spelling: "expensive"
xidachen 2016/07/07 20:43:29 Done.
+ compareBitmaps(imageBitmaps.noCropHigh, imageBitmaps.noCropMedium, false);
+ compareBitmaps(imageBitmaps.noCropLow, imageBitmaps.noCropPixelated, false);
+ compareBitmaps(imageBitmaps.cropHigh, imageBitmaps.cropMedium, false);
+ compareBitmaps(imageBitmaps.cropLow, imageBitmaps.cropPixelated, false);
+ }, function(ex) {
+ failed(ex);
jbroman 2016/07/07 19:17:59 Is this necessary? promise_test already fails the
xidachen 2016/07/07 20:43:29 Done.
+ });
+}
+
+function initializeTestCanvas(testCanvas)
jbroman 2016/07/07 19:17:59 nit: every call site makes a canvas and then passe
xidachen 2016/07/07 20:43:29 Done.
+{
+ testCanvas.width = 20;
+ testCanvas.height = 20;
+ var testCtx = testCanvas.getContext("2d");
+ testCtx.fillStyle = "rgb(255, 0, 0)";
+ testCtx.fillRect(0, 0, 10, 10);
+ testCtx.fillStyle = "rgb(0, 255, 0)";
+ testCtx.fillRect(10, 0, 10, 10);
+ testCtx.fillStyle = "rgb(0, 0, 255)";
+ testCtx.fillRect(0, 10, 10, 10);
+ testCtx.fillStyle = "rgb(0, 0, 0)";
+ testCtx.fillRect(10, 10, 10, 10);
+}
+
+// Blob
+promise_test(function() {
+ return new Promise((resolve, reject) => {
+ var xhr = new XMLHttpRequest();
+ xhr.open("GET", 'resources/pattern.png');
+ xhr.responseType = 'blob';
+ xhr.send();
+ xhr.onload = function() {
+ blob = xhr.response;
+ resolve(testImageBitmap(blob));
jbroman 2016/07/07 19:17:59 nit: exceptions thrown in testImageBitmap won't be
xidachen 2016/07/07 20:43:29 Done.
+ };
+ });
+}, 'createImageBitmap from a Blob with resize option.');
+
+// HTMLCanvasElement
+promise_test(function() {
+ return new Promise((resolve, reject) => {
jbroman 2016/07/07 19:18:00 testImageBitmap already has all of the asynchronou
xidachen 2016/07/07 20:43:28 Done.
+ var testCanvas = document.createElement("canvas");
+ initializeTestCanvas(testCanvas);
+ resolve(testImageBitmap(testCanvas));
+ });
+}, 'createImageBitmap from a HTMLCanvasElement with resize option.');
+
+// HTMLImageElement
+promise_test(function() {
+ return new Promise((resolve, reject) => {
+ var image = new Image();
+ image.onload = function() {
+ resolve(testImageBitmap(image));
+ };
+ image.src = 'resources/pattern.png'
+ });
jbroman 2016/07/07 19:17:59 nit: Similar advice to XHR above. It's not a big d
xidachen 2016/07/07 20:43:29 Done.
+}, 'createImageBitmap from a HTMLImageElement with resize option.');
+
+// ImageBitmap
+promise_test(function() {
+ return new Promise((resolve, reject) => {
+ var testCanvas = document.createElement("canvas");
+ initializeTestCanvas(testCanvas);
+ createImageBitmap(testCanvas).then(function(bitmap) {
+ resolve(testImageBitmap(bitmap));
jbroman 2016/07/07 19:17:59 Here the asynchronousness already uses promises, s
xidachen 2016/07/07 20:43:29 Done.
+ });
+ });
+}, 'createImageBitmap from an ImageBitmap with resize option.');
+</script>

Powered by Google App Engine
This is Rietveld 408576698