Merge pull request #268 from danvk/color-option
[dygraphs.git] / auto_tests / tests / Util.js
CommitLineData
fa607ffb
RK
1/**
2 * @fileoverview Utility functions for Dygraphs.
3 *
4 * @author konigsberg@google.com (Robert Konigsberg)
5 */
6var Util = {};
7
8/**
846038aa
RK
9 * Get the y-labels for a given axis.
10 *
11 * You can specify a parent if more than one graph is in the document.
fa607ffb
RK
12 */
13Util.getYLabels = function(axis_num, parent) {
14 axis_num = axis_num || "";
15 parent = parent || document;
16 var y_labels = parent.getElementsByClassName("dygraph-axis-label-y" + axis_num);
17 var ary = [];
18 for (var i = 0; i < y_labels.length; i++) {
19 ary.push(y_labels[i].innerHTML);
20 }
21 return ary;
d574a45e 22};
fa607ffb
RK
23
24/**
846038aa
RK
25 * Get the x-labels for a given axis.
26 *
27 * You can specify a parent if more than one graph is in the document.
28 */
29Util.getXLabels = function(parent) {
30 parent = parent || document;
31 var x_labels = parent.getElementsByClassName("dygraph-axis-label-x");
32 var ary = [];
33 for (var i = 0; i < x_labels.length; i++) {
34 ary.push(x_labels[i].innerHTML);
35 }
36 return ary;
d574a45e 37};
846038aa
RK
38
39/**
fa607ffb
RK
40 * Returns all text in tags w/ a given css class, sorted.
41 * You can specify a parent if more than one graph is on the document.
42 */
43Util.getClassTexts = function(css_class, parent) {
44 parent = parent || document;
45 var texts = [];
46 var els = parent.getElementsByClassName(css_class);
47 for (var i = 0; i < els.length; i++) {
48 texts[i] = els[i].textContent;
49 }
50 texts.sort();
51 return texts;
d574a45e 52};
846038aa
RK
53
54Util.getLegend = function(parent) {
55 parent = parent || document;
56 var legend = parent.getElementsByClassName("dygraph-legend")[0];
79aabc9d
DV
57 var re = new RegExp(String.fromCharCode(160), 'g');
58 return legend.textContent.replace(re, ' ');
d574a45e 59};
846038aa 60
d574a45e 61/**
3a84670d 62 * Assert that all elements have a certain style property.
d574a45e 63 */
3a84670d
RK
64Util.assertStyleOfChildren = function(selector, property, expectedValue) {
65 assertTrue(selector.length > 0);
66 $.each(selector, function(idx, child) {
67 assertEquals(expectedValue, $(child).css(property));
68 });
d574a45e 69};
846038aa 70
71a1930b
RK
71
72/**
73 * Takes in an array of strings and returns an array of floats.
74 */
75Util.makeNumbers = function(ary) {
76 var ret = [];
77 for (var i = 0; i < ary.length; i++) {
78 ret.push(parseFloat(ary[i]));
79 }
80 return ret;
901b2ebb 81};
12b879f4
DV
82
83
84/**
85 * Sample a pixel from the canvas.
86 * Returns an [r, g, b, a] tuple where each values is in [0, 255].
87 */
88Util.samplePixel = function(canvas, x, y) {
89 var ctx = canvas.getContext("2d"); // bypasses Proxy if applied.
90
91 // TODO(danvk): Any performance issues with this?
92 var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
93
94 var i = 4 * (x + imageData.width * y);
95 var d = imageData.data;
96 return [d[i], d[i+1], d[i+2], d[i+3]];
97};
5bcc58b4
DV
98
99/**
100 * Overrides the browser's built-in XMLHttpRequest with a mock.
101 * Usage:
102 *
103 * var mockXhr = Util.overrideXMLHttpRequest(your_data);
104 * ... call code that does an XHR ...
105 * mockXhr.respond(); // restores default behavior.
106 * ... do your assertions ...
107 */
108Util.overrideXMLHttpRequest = function(data) {
109 var originalXMLHttpRequest = XMLHttpRequest;
110
111 var requests = [];
112 var FakeXMLHttpRequest = function () {
113 requests.push(this);
114 };
115 FakeXMLHttpRequest.prototype.open = function () {};
116 FakeXMLHttpRequest.prototype.send = function () {
117 this.readyState = 4;
118 this.status = 200;
119 this.responseText = data;
120 };
121 FakeXMLHttpRequest.restore = function() {
122 XMLHttpRequest = originalXMLHttpRequest;
123 };
124 FakeXMLHttpRequest.respond = function() {
125 for (var i = 0; i < requests.length; i++) {
126 requests[i].onreadystatechange();
127 }
128 FakeXMLHttpRequest.restore();
129 };
130 XMLHttpRequest = FakeXMLHttpRequest;
131 return FakeXMLHttpRequest;
132};
133