X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Ftests%2FUtil.js;h=3b8c1b8d64f6b2fa8ad3d0e83faedd605794f9d1;hb=20aaadda52577cfa97d25b350405726e7f51ffd1;hp=2793cb7fff560cdc2cc65aa9c124bee31e03014e;hpb=ee60939f4e2238cbb64ebbe7acdc27195c149cfb;p=dygraphs.git diff --git a/auto_tests/tests/Util.js b/auto_tests/tests/Util.js index 2793cb7..3b8c1b8 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; }; @@ -51,21 +51,27 @@ Util.getClassTexts = function(css_class, parent) { return texts; }; +// Convert   to a normal space +Util.nbspToSpace = function(str) { + var re = new RegExp(String.fromCharCode(160), 'g'); + return str.replace(re, ' '); +}; + Util.getLegend = function(parent) { parent = parent || document; var legend = parent.getElementsByClassName("dygraph-legend")[0]; - var re = new RegExp(String.fromCharCode(160), 'g'); - return legend.textContent.replace(re, ' '); + return Util.nbspToSpace(legend.textContent); }; /** * 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]); + } }; @@ -91,7 +97,9 @@ Util.samplePixel = function(canvas, x, y) { // 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 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]]; }; @@ -119,7 +127,7 @@ Util.overrideXMLHttpRequest = function(data) { this.responseText = data; }; FakeXMLHttpRequest.restore = function() { - XMLHttpRequest = originalXMLHttpRequest; + window.XMLHttpRequest = originalXMLHttpRequest; }; FakeXMLHttpRequest.respond = function() { for (var i = 0; i < requests.length; i++) { @@ -127,7 +135,7 @@ Util.overrideXMLHttpRequest = function(data) { } FakeXMLHttpRequest.restore(); }; - XMLHttpRequest = FakeXMLHttpRequest; + window.XMLHttpRequest = FakeXMLHttpRequest; return FakeXMLHttpRequest; }; @@ -139,3 +147,24 @@ Util.overrideXMLHttpRequest = function(data) { Util.formatDate = function(dateMillis) { return Dygraph.dateString_(dateMillis).slice(0, 10); // 10 == "YYYY/MM/DD".length }; + +/** + * Capture console.{log,warn,error} statements into obj. + * obj will look like {log:[], warn:[], error:[]} + * This returns a function which will restore the original console. + */ +Util.captureConsole = function(obj) { + obj.log = []; + obj.warn = []; + obj.error = []; + var orig = [console.log, console.warn, console.error]; + console.log = function(text) { obj.log.push(text); }; + console.warn = function(text) { obj.warn.push(text); }; + console.error = function(text) { obj.error.push(text); }; + + return function() { + console.log = orig[0]; + console.warn = orig[1]; + console.error = orig[2]; + }; +};