From: Dan Vanderkam Date: Fri, 9 Mar 2012 00:34:08 +0000 (-0500) Subject: Check for balanced context.save() and context.restore() calls X-Git-Tag: v1.0.0~312 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=6278f6fe69767cf21c1d3361bc141321a282d9ef;p=dygraphs.git Check for balanced context.save() and context.restore() calls --- diff --git a/auto_tests/tests/CanvasAssertions.js b/auto_tests/tests/CanvasAssertions.js index 8b52155..6481c60 100644 --- a/auto_tests/tests/CanvasAssertions.js +++ b/auto_tests/tests/CanvasAssertions.js @@ -83,7 +83,29 @@ CanvasAssertions.assertLineDrawn = function(proxy, p1, p2, attrs) { }; fail("Can't find a line drawn between " + p1 + " and " + p2 + " with attributes " + toString(attrs)); -} +}; + +/** + * 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. @@ -98,7 +120,7 @@ CanvasAssertions.numLinesDrawn = function(proxy, color) { } } return num_lines; -} +}; CanvasAssertions.matchPixels = function(expected, actual) { // Expect array of two integers. Assuming the values are within one @@ -106,7 +128,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) { @@ -115,4 +137,4 @@ CanvasAssertions.matchAttributes = function(expected, actual) { } } return true; -} +}; diff --git a/auto_tests/tests/error_bars.js b/auto_tests/tests/error_bars.js index 8bc20e3..c9e3a38 100644 --- a/auto_tests/tests/error_bars.js +++ b/auto_tests/tests/error_bars.js @@ -86,5 +86,6 @@ errorBarsTestCase.prototype.testErrorBarsDrawn = function() { xy2 = g.toDomCoords(data[i + 1][0], data[i + 1][1][1]); CanvasAssertions.assertLineDrawn(htx, xy1, xy2, attrs); } + CanvasAssertions.assertBalancedSaveRestore(htx); }; diff --git a/auto_tests/tests/simple_drawing.js b/auto_tests/tests/simple_drawing.js index 0ea13a4..8f3b0c5 100644 --- a/auto_tests/tests/simple_drawing.js +++ b/auto_tests/tests/simple_drawing.js @@ -55,7 +55,16 @@ SimpleDrawingTestCase.prototype.testDrawSimpleRangePlusOne = function() { strokeStyle: "#008080", lineWidth: 1 }); -} + CanvasAssertions.assertBalancedSaveRestore(htx); +}; + +SimpleDrawingTestCase.prototype.testDrawWithAxis = function() { + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, ZERO_TO_FIFTY); + + var htx = g.hidden_ctx_; + CanvasAssertions.assertBalancedSaveRestore(htx); +}; /** * Tests that it is drawing dashes, and it remember the dash history between @@ -79,4 +88,5 @@ SimpleDrawingTestCase.prototype.testDrawSimpleDash = function() { htx = g.hidden_ctx_; assertEquals(29, CanvasAssertions.numLinesDrawn(htx, "#ff0000")); + CanvasAssertions.assertBalancedSaveRestore(htx); };