X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Ftests%2FUtil.js;h=7c1eaa6aa6a2b22aee351fa7d2e5371a767a90a8;hb=bf7d192b5aef6ea82e7e627d4cfcfca1cb978f23;hp=66daa7f2fb1dcff7847cc8a1829aea14cc28a92c;hpb=3a84670d430444e0fb4b89c6991c3787f2f329dc;p=dygraphs.git diff --git a/auto_tests/tests/Util.js b/auto_tests/tests/Util.js index 66daa7f..7c1eaa6 100644 --- a/auto_tests/tests/Util.js +++ b/auto_tests/tests/Util.js @@ -16,7 +16,7 @@ Util.getYLabels = function(axis_num, parent) { var y_labels = parent.getElementsByClassName("dygraph-axis-label-y" + axis_num); var ary = []; for (var i = 0; i < y_labels.length; i++) { - ary.push(y_labels[i].innerHTML); + ary.push(y_labels[i].innerHTML.replace(/ | /g, ' ')); } return ary; }; @@ -31,7 +31,7 @@ Util.getXLabels = function(parent) { var x_labels = parent.getElementsByClassName("dygraph-axis-label-x"); var ary = []; for (var i = 0; i < x_labels.length; i++) { - ary.push(x_labels[i].innerHTML); + ary.push(x_labels[i].innerHTML.replace(/ | /g, ' ')); } return ary; }; @@ -54,15 +54,91 @@ 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 elements have a certain style property. */ Util.assertStyleOfChildren = function(selector, property, expectedValue) { - assertTrue(selector.length > 0); - $.each(selector, function(idx, child) { - assertEquals(expectedValue, $(child).css(property)); - }); + assert.isTrue(selector.length > 0); + for (var idx = 0; idx < selector.length; idx++) { + var child = selector[idx]; + assert.equal(expectedValue, window.getComputedStyle(child)[property]); + } +}; + + +/** + * Takes in an array of strings and returns an array of floats. + */ +Util.makeNumbers = function(ary) { + var ret = []; + for (var i = 0; i < ary.length; i++) { + ret.push(parseFloat(ary[i])); + } + 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() { + window.XMLHttpRequest = originalXMLHttpRequest; + }; + FakeXMLHttpRequest.respond = function() { + for (var i = 0; i < requests.length; i++) { + requests[i].onreadystatechange(); + } + FakeXMLHttpRequest.restore(); + }; + window.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 };