1 description("Series of tests to ensure clip() works with path and winding rule parameters.");
3 var ctx = document.getElementById('canvas').getContext('2d');
5 function pixelDataAtPoint() {
6 return ctx.getImageData(50, 50, 1, 1).data;
9 function checkResult(expectedColors, sigma) {
10 for (var i = 0; i < 4; i++)
11 shouldBeCloseTo("pixelDataAtPoint()[" + i + "]", expectedColors[i], sigma);
14 function drawRectanglesOn(contextOrPath) {
15 contextOrPath.rect(0, 0, 100, 100);
16 contextOrPath.rect(25, 25, 50, 50);
19 function formatName(fillRule, path) {
20 return 'clip(' + (path ? 'path' : '') + (fillRule && path ? ', ' : '') +
21 (fillRule ? '"' + fillRule + '"' : '') + ')';
24 function testClipWith(fillRule, path) {
25 debug('Testing ' + formatName(fillRule, path));
26 ctx.fillStyle = 'rgb(255,0,0)';
28 ctx.fillRect(0, 0, 100, 100);
29 ctx.fillStyle = 'rgb(0,255,0)';
32 ctx.clip(path, fillRule);
38 drawRectanglesOn(ctx);
46 ctx.fillRect(0, 0, 100, 100);
47 if (fillRule == 'evenodd') {
48 checkResult([255, 0, 0, 255], 5);
50 checkResult([0, 255, 0, 255], 5);
56 function prepareTestScenario() {
57 fillRules = [undefined, 'nonzero', 'evenodd'];
59 drawRectanglesOn(path);
61 for (var i = 0; i < fillRules.length; i++) {
62 testClipWith(fillRules[i]);
63 testClipWith(fillRules[i], path);
66 // Test exception cases.
67 shouldThrow("ctx.clip(null)");
68 shouldThrow("ctx.clip(null, null)");
69 shouldThrow("ctx.clip(null, 'nonzero')");
70 shouldThrow("ctx.clip(path, null)");
71 shouldThrow("ctx.clip([], 'nonzero')");
72 shouldThrow("ctx.clip({}, 'nonzero')");
73 shouldThrow("ctx.clip(null, 'evenodd')");
74 shouldThrow("ctx.clip([], 'evenodd')");
75 shouldThrow("ctx.clip({}, 'evenodd')");
76 shouldThrow("ctx.clip('gazonk')");
77 shouldThrow("ctx.clip(path, 'gazonk')");
78 shouldThrow("ctx.clip(undefined)");
79 shouldThrow("ctx.clip(undefined, undefined)");
80 shouldThrow("ctx.clip(undefined, 'nonzero')");
81 shouldThrow("ctx.clip(path, undefined)");
84 // Run test and allow variation of results.
85 prepareTestScenario();