Skip to content

Commit

Permalink
Add tests for Canvas2D imageSmoothingEnabled (#2416)
Browse files Browse the repository at this point in the history
* Add tests for Canvas2D imageSmoothingEnabled

* Remove duplicate test; move save/restore tests into own file

* Assert that imageSmoothingEnabled is initially true
  • Loading branch information
Elchi3 authored and sideshowbarker committed Aug 17, 2016
1 parent d34d733 commit a67d9ab
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 44 deletions.
162 changes: 118 additions & 44 deletions 2dcontext/image-smoothing/imagesmoothing.html
@@ -1,45 +1,119 @@
<!DOCTYPE html>
<html>
<head>
<title>CanvasRenderingContext2D imageSmoothingEnabled test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="author" title="ShinHyunjin" href="mailto:jini7927@gmail.com">
<link rel="help" href="http://www.w3.org/html/wg/drafts/2dcontext/html5_canvas/#image-smoothing">
</head>
<body>
<div id="log"></div>
<canvas id="test_canvas_1"></canvas>
<canvas id="test_canvas_2"></canvas>
<canvas id="test_canvas_3"></canvas>

<script>
(function() {
test(function() {
var canvas = document.getElementById('test_canvas_1');
var ctx = canvas.getContext('2d');

assert_true(ctx.imageSmoothingEnabled);
}, "When the CanvasRenderingContext2D object is created, the attribute must be set to true.");

test(function() {
var canvas = document.getElementById('test_canvas_2');
var ctx = canvas.getContext('2d');

ctx.imageSmoothingEnabled = false;
assert_false(ctx.imageSmoothingEnabled);
}, "On getting the imageSmoothingEnabled attribute, the user agent must return the last value it was set to.");

test(function() {
var canvas = document.getElementById('test_canvas_3');
var ctx = canvas.getContext('2d');

ctx.imageSmoothingEnabled = false;
assert_equals(ctx.imageSmoothingEnabled, false);
}, "On setting the imageSmoothingEnabled attribute, it must be set to the new value.");
})();


</script>
</body>
</html>
<meta charset="utf-8">
<title>CanvasRenderingContext2D imageSmoothingEnabled test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#image-smoothing">
<script>
function createTestImage() {
var image = document.createElement('canvas');
var imgctx = image.getContext('2d');
imgctx.fillStyle = "#F00";
imgctx.fillRect(0, 0, 2, 2);
imgctx.fillStyle = "#0F0";
imgctx.fillRect(0, 0, 1, 1);
return image;
}

test(function() {
var ctx = document.createElement('canvas').getContext('2d');
assert_true(ctx.imageSmoothingEnabled);
}, "When the canvas context is created, imageSmoothingEnabled must be set to true.");

test(function() {
var ctx = document.createElement('canvas').getContext('2d');
ctx.imageSmoothingEnabled = false;
assert_false(ctx.imageSmoothingEnabled);
}, "On getting imageSmoothingEnabled, the user agent must return the last value it was set to.");

test(function() {
var ctx = document.createElement('canvas').getContext('2d');
var image = createTestImage();
ctx.scale(10, 10);
ctx.drawImage(image, 0, 0);
var pixels = ctx.getImageData(9, 9, 1, 1).data;
assert_not_equals(pixels[0], 0);
assert_not_equals(pixels[1], 255);
}, "Test that image smoothing is actually on by default and just the attribute value.");

test(function() {
var ctx = document.createElement('canvas').getContext('2d');
ctx.imageSmoothingEnabled = true;
var image = createTestImage();
ctx.scale(10, 10);
ctx.drawImage(image, 0, 0);
var pixels = ctx.getImageData(9, 9, 1, 1).data;
assert_not_equals(pixels[0], 0);
assert_not_equals(pixels[1], 255);
}, "Test that image smoothing works when imageSmoothingEnabled is set to true");

test(function() {
var ctx = document.createElement('canvas').getContext('2d');
var image = createTestImage();
ctx.imageSmoothingEnabled = false;
ctx.scale(10, 10);
ctx.drawImage(image, 0, 0);
var pixels = ctx.getImageData(9, 9, 1, 1).data;
assert_array_equals(pixels, [0, 255, 0, 255]);
}, "Test that imageSmoothingEnabled = false (nearest-neighbor interpolation) works with drawImage().");

test(function() {
var ctx = document.createElement('canvas').getContext('2d');
var image = createTestImage();
ctx.imageSmoothingEnabled = false;
ctx.scale(10, 10);
ctx.fillStyle = ctx.createPattern(image, 'repeat');
ctx.fillRect(0, 0, 10, 10);
var pixels = ctx.getImageData(9, 9, 1, 1).data;
assert_array_equals(pixels, [0, 255, 0, 255]);
}, "Test that imageSmoothingEnabled = false (nearest-neighbor interpolation) works with fillRect and createPattern().");

test(function() {
var ctx = document.createElement('canvas').getContext('2d');
var image = createTestImage();
ctx.imageSmoothingEnabled = false;
ctx.fillStyle = ctx.createPattern(image, 'repeat');
ctx.scale(10, 10);
ctx.rect(0, 0, 10, 10);
ctx.fill();
var pixels = ctx.getImageData(9, 9, 1, 1).data;
assert_array_equals(pixels, [0, 255, 0, 255]);
}, "Test that imageSmoothingEnabled = false (nearest-neighbor interpolation) works with fill() and createPattern().");

test(function() {
var ctx = document.createElement('canvas').getContext('2d');
var image = createTestImage();
ctx.strokeStyle = ctx.createPattern(image, 'repeat');
ctx.lineWidth = 5;
ctx.imageSmoothingEnabled = false;
ctx.scale(10, 10);
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(10, 10);
ctx.stroke();
var pixels = ctx.getImageData(9, 9, 1, 1).data;
assert_array_equals(pixels, [0, 255, 0, 255]);
}, "Test that imageSmoothingEnabled = false (nearest-neighbor interpolation) works with stroke() and createPattern().");

test(function() {
var repaints = 5;
var ctx = document.createElement('canvas').getContext('2d');

function draw() {
ctx.clearRect(0, 0, 10, 10);
ctx.setTransform(1, 0, 0, 1, 0, 0);
var image = createTestImage();
ctx.imageSmoothingEnabled = false;
ctx.scale(10, 10);
ctx.drawImage(image, 0, 0);
var pixels = ctx.getImageData(9, 9, 1, 1).data;
assert_array_equals(pixels, [0, 255, 0, 255]);
}

while (repaints > 0) {
draw();
repaints = repaints - 1;
}

}, "Test that imageSmoothingEnabled = false (nearest-neighbor interpolation) still works after repaints.");
</script>
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>CanvasRenderingContext2D imageSmoothingEnabled save/restore test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#image-smoothing">
<script>
function createTestImage() {
var image = document.createElement('canvas');
var imgctx = image.getContext('2d');
imgctx.fillStyle = "#F00";
imgctx.fillRect(0, 0, 2, 2);
imgctx.fillStyle = "#0F0";
imgctx.fillRect(0, 0, 1, 1);
return image;
}

test(function() {
var ctx = document.createElement('canvas').getContext('2d');
ctx.save();
ctx.imageSmoothingEnabled = false;
ctx.restore();
assert_equals(ctx.imageSmoothingEnabled, true);
}, "Test that restore() undoes any modifications to imageSmoothingEnabled.");

test(function() {
var ctx = document.createElement('canvas').getContext('2d');
ctx.imageSmoothingEnabled = false;
var old = ctx.imageSmoothingEnabled;
ctx.save();
assert_equals(ctx.imageSmoothingEnabled, old);
ctx.restore();
}, "Test that save() doesn't modify the values of imageSmoothingEnabled.");

test(function() {
var ctx = document.createElement('canvas').getContext('2d');
ctx.imageSmoothingEnabled = false;
ctx.save();
ctx.imageSmoothingEnabled = true;
ctx.restore();
var image = createTestImage();
ctx.scale(10, 10);
ctx.drawImage(image, 0, 0);
var pixels = ctx.getImageData(0, 0, 1, 1).data;
assert_array_equals(pixels, [0, 255, 0, 255]);
}, "Test that restoring actually changes smoothing and not just the attribute value.");
</script>

0 comments on commit a67d9ab

Please sign in to comment.