copy over lots of changes from "shrink" branch.
[dygraphs.git] / auto_tests / tests / Util.js
index 6469bc6..207f237 100644 (file)
@@ -19,7 +19,7 @@ Util.getYLabels = function(axis_num, parent) {
     ary.push(y_labels[i].innerHTML);
   }
   return ary;
-}
+};
 
 /**
  * Get the x-labels for a given axis.
@@ -34,7 +34,7 @@ Util.getXLabels = function(parent) {
     ary.push(x_labels[i].innerHTML);
   }
   return ary;
-}
+};
 
 /**
  * Returns all text in tags w/ a given css class, sorted.
@@ -49,13 +49,24 @@ Util.getClassTexts = function(css_class, parent) {
   }
   texts.sort();
   return texts;
-}
+};
 
 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));
+  });
+};
 
 
 /**
@@ -67,4 +78,74 @@ Util.makeNumbers = function(ary) {
     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 i = 4 * (x + imageData.width * y);
+  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} date_ms Millis since epoch.
+ * @return {string} The date formatted as YYYY-MM-DD.
+ */
+Util.formatDate = function(date_ms) {
+  var zeropad = function(x) { if (x < 10) return "0" + x; else return "" + x; };
+  var d = new Date(date_ms);
+
+  // Get the year:
+  var year = "" + d.getFullYear();
+  // Get a 0 padded month string
+  var month = zeropad(d.getMonth() + 1);  //months are 0-offset, sigh
+  // Get a 0 padded day string
+  var day = zeropad(d.getDate());
+
+  return year + "/" + month + "/" + day;
+};