X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Ftests%2FCanvasAssertions.js;h=4059a0aeee189614ef6fceb62dc4e9e2c9f9f24f;hb=ccb0001c3c8aadd02358995c8d7b71fbb37c0bc3;hp=1ef0573ae2e859fd11efd18427b004dd625399c0;hpb=02eb7ae72eb3d5e83c2bf546d1fc2a6a60b91008;p=dygraphs.git diff --git a/auto_tests/tests/CanvasAssertions.js b/auto_tests/tests/CanvasAssertions.js index 1ef0573..4059a0a 100644 --- a/auto_tests/tests/CanvasAssertions.js +++ b/auto_tests/tests/CanvasAssertions.js @@ -83,7 +83,73 @@ CanvasAssertions.assertLineDrawn = function(proxy, p1, p2, attrs) { }; fail("Can't find a line drawn between " + p1 + " and " + p2 + " with attributes " + toString(attrs)); -} +}; + +/** + * Return the lines drawn with specific attributes. + * + * This merely looks for one of these four possibilities: + * moveTo(p1) -> lineTo(p2) + * moveTo(p2) -> lineTo(p1) + * lineTo(p1) -> lineTo(p2) + * lineTo(p2) -> lineTo(p1) + * + * attrs is meant to be used when you want to track things like + * color and stroke width. + */ +CanvasAssertions.getLinesDrawn = function(proxy) { + var lastCall; + var lines = []; + for (var i = 0; i < proxy.calls__.length; i++) { + var call = proxy.calls__[i]; + + if (call.name == "lineTo") { + if (lastCall != null) { + lines.push([lastCall, call]); + } + } + + lastCall = (call.name === "lineTo" || call.name === "moveTo") ? call : null; + } + return lines; +}; + +/** + * Verifies that every call to context.save() has a matching call to + * context.restore(). + */ +CanvasAssertions.assertBalancedSaveRestore = function(proxy) { + var depth = 0; + for (var i = 0; i < proxy.calls__.length; i++) { + var call = proxy.calls__[i]; + if (call.name == "save") depth++ + if (call.name == "restore") { + if (depth == 0) { + fail("Too many calls to restore()"); + } + depth--; + } + } + + if (depth > 0) { + fail("Missing matching 'context.restore()' calls."); + } +}; + +/** + * Checks how many lines of the given color have been drawn. + * @return {Integer} The number of lines of the given color. + */ +CanvasAssertions.numLinesDrawn = function(proxy, color) { + var num_lines = 0; + for (var i = 0; i < proxy.calls__.length; i++) { + var call = proxy.calls__[i]; + if (call.name == "lineTo" && call.properties.strokeStyle == color) { + num_lines++; + } + } + return num_lines; +}; CanvasAssertions.matchPixels = function(expected, actual) { // Expect array of two integers. Assuming the values are within one @@ -91,7 +157,7 @@ CanvasAssertions.matchPixels = function(expected, actual) { // who knows what pixel a value of 5.8888 results in. return Math.abs(expected[0] - actual[0]) < 1 && Math.abs(expected[1] - actual[1]) < 1; -} +}; CanvasAssertions.matchAttributes = function(expected, actual) { for (var attr in expected) { @@ -100,4 +166,4 @@ CanvasAssertions.matchAttributes = function(expected, actual) { } } return true; -} +};