X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Ftests%2FUtil.js;h=0e2c25d609d67275390ae04ed28fd695915d1b1c;hb=a22cc80916b6e165451995e1ae3ed4d36dc86eab;hp=08cc88ad598498a0edfcc16548d8190275c7cdad;hpb=e4a06d92cb5cb671a668ecc7b2c9c8f5155b16ea;p=dygraphs.git diff --git a/auto_tests/tests/Util.js b/auto_tests/tests/Util.js index 08cc88a..0e2c25d 100644 --- a/auto_tests/tests/Util.js +++ b/auto_tests/tests/Util.js @@ -54,7 +54,8 @@ 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, ' '); }; /** @@ -78,3 +79,65 @@ 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 scale = Dygraph.getContextPixelRatio(ctx); + + var i = 4 * (x * scale + imageData.width * y * scale); + var d = imageData.data; + return [d[i], d[i+1], d[i+2], d[i+3]]; +}; + +/** + * Overrides the browser's built-in XMLHttpRequest with a mock. + * Usage: + * + * var mockXhr = Util.overrideXMLHttpRequest(your_data); + * ... call code that does an XHR ... + * mockXhr.respond(); // restores default behavior. + * ... do your assertions ... + */ +Util.overrideXMLHttpRequest = function(data) { + var originalXMLHttpRequest = XMLHttpRequest; + + var requests = []; + var FakeXMLHttpRequest = function () { + requests.push(this); + }; + FakeXMLHttpRequest.prototype.open = function () {}; + FakeXMLHttpRequest.prototype.send = function () { + this.readyState = 4; + this.status = 200; + this.responseText = data; + }; + FakeXMLHttpRequest.restore = function() { + XMLHttpRequest = originalXMLHttpRequest; + }; + FakeXMLHttpRequest.respond = function() { + for (var i = 0; i < requests.length; i++) { + requests[i].onreadystatechange(); + } + FakeXMLHttpRequest.restore(); + }; + XMLHttpRequest = FakeXMLHttpRequest; + return FakeXMLHttpRequest; +}; + +/** + * Format a date as 2000/01/23 + * @param {number} dateMillis Millis since epoch. + * @return {string} The date formatted as YYYY-MM-DD. + */ +Util.formatDate = function(dateMillis) { + return Dygraph.dateString_(dateMillis).slice(0, 10); // 10 == "YYYY/MM/DD".length +};