X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Ftests%2FUtil.js;h=2d652eda7ab43032a037f0a8835c3bea36517b59;hb=2dde24e570f97f8d0f69445563d068a5678aa475;hp=3eb59e3db6c1351431ccb967d5e4ce280caaf794;hpb=e41ef65aa9e87306657157c0663032e5ea9547fe;p=dygraphs.git diff --git a/auto_tests/tests/Util.js b/auto_tests/tests/Util.js index 3eb59e3..2d652ed 100644 --- a/auto_tests/tests/Util.js +++ b/auto_tests/tests/Util.js @@ -54,30 +54,18 @@ Util.getClassTexts = function(css_class, parent) { Util.getLegend = function(parent) { parent = parent || document; var legend = parent.getElementsByClassName("dygraph-legend")[0]; - return legend.textContent; + var re = new RegExp(String.fromCharCode(160), 'g'); + return legend.textContent.replace(re, ' '); }; /** - * Assert that all the elements in 'parent' with class 'className' is - * the expected font size. + * Assert that all elements have a certain style property. */ -Util.assertFontSizes = function(parent, className, expectedSize) { - var expectedSizePx = expectedSize + "px"; - var labels = parent.getElementsByClassName(className); - assertTrue(labels.length > 0); - - // window.getComputedStyle is apparently compatible with all browsers - // (IE first became compatible with IE9.) - // If this test fails on earlier browsers, then enable something like this, - // because the font size is set by the parent div. - // if (!window.getComputedStyle) { - // fontSize = label.parentElement.style.fontSize; - // } - for (var idx = 0; idx < labels.length; idx++) { - var label = labels[idx]; - var fontSize = window.getComputedStyle(label).fontSize; - assertEquals(expectedSizePx, fontSize); - } +Util.assertStyleOfChildren = function(selector, property, expectedValue) { + assertTrue(selector.length > 0); + $.each(selector, function(idx, child) { + assertEquals(expectedValue, $(child).css(property)); + }); }; @@ -91,3 +79,19 @@ Util.makeNumbers = function(ary) { } return ret; }; + + +/** + * Sample a pixel from the canvas. + * Returns an [r, g, b, a] tuple where each values is in [0, 255]. + */ +Util.samplePixel = function(canvas, x, y) { + var ctx = canvas.getContext("2d"); // bypasses Proxy if applied. + + // TODO(danvk): Any performance issues with this? + var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); + + var i = 4 * (x + imageData.width * y); + var d = imageData.data; + return [d[i], d[i+1], d[i+2], d[i+3]]; +};