Fix two inconsistencies in dygraph-externs.js (#859)
[dygraphs.git] / auto_tests / tests / Util.js
index 8937e43..36532cd 100644 (file)
@@ -3,6 +3,9 @@
  *
  * @author konigsberg@google.com (Robert Konigsberg)
  */
+
+import * as utils from '../../src/dygraph-utils';
+
 var Util = {};
 
 /**
@@ -90,14 +93,14 @@ Util.makeNumbers = function(ary) {
 /**
  * Sample a pixel from the canvas.
  * Returns an [r, g, b, a] tuple where each values is in [0, 255].
+ * This is _very_ slow! If you want to sample many pixels, use PixelSampler.
  */
 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 scale = utils.getContextPixelRatio(ctx);
 
   var i = 4 * (x * scale + imageData.width * y * scale);
   var d = imageData.data;
@@ -145,5 +148,28 @@ Util.overrideXMLHttpRequest = function(data) {
  * @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
+  return utils.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];
+  };
 };
+
+export default Util;