var testCaseList = [];
function TestCase(name) {
- this.name = name;
- this.toString = function() {
+ var testCase = function() { return this; };
+ testCase.name = name;
+ testCase.toString = function() {
return "Fake test case " + name;
};
- var testCase = function() { return this; };
testCase.prototype.setUp = function() { };
testCase.prototype.tearDown = function() { };
/**
return false;
}
};
+
testCase.prototype.runAllTests = function() {
+ var results = {};
+ var names = this.getTestNames();
+ for (var idx in names) {
+ var name = names[idx];
+ console.log("Running " + name);
+ var result = this.runTest(name);
+ results[name] = result;
+ }
+ console.log(prettyPrintEntity_(results));
+ return results;
+ };
+
+ testCase.prototype.getTestNames = function() {
// what's better than for ... in for non-array objects?
- var tests = {};
+ var tests = [];
for (var name in this) {
if (name.indexOf('test') == 0 && typeof(this[name]) == 'function') {
- console.log("Running " + name);
- var result = this.runTest(name);
- tests[name] = result;
+ tests.push(name);
}
}
- console.log(prettyPrintEntity_(tests));
- };
+ return tests;
+ }
- testCaseList.push(testCase);
+ testCaseList.push({name : name, testCase : testCase});
return testCase;
};
var num_tests = 0;
for (var i = 0; i < testCaseList.length; i++) {
- var tc_class = testCaseList[i];
+ var tc_class = testCaseList[i].testCase;
for (var name in tc_class.prototype) {
if (name.indexOf('test') == 0 && typeof(tc_class.prototype[name]) == 'function') {
if (globalTestDb.hasOwnProperty(name)) {
console.log('Loaded ' + num_tests + ' tests in ' +
testCaseList.length + ' test cases');
}
+
+function getAllTestCases() {
+ return testCaseList;
+}
<script type="text/javascript">
var tc = null;
+ var name = null;
function processVariables() {
var splitVariables = function() { // http://www.idealog.us/2006/06/javascript_to_p.html
var query = window.location.search.substring(1);
var test = args.test;
var command = args.command;
- if (args.testCase) {
+ if (args.testCaseName) {
+ var testCases = getAllTestCases();
+ name = args.testCaseName;
+ for (var idx in testCases) {
+ var entry = testCases[idx];
+ if (entry.name == args.testCaseName) {
+ var prototype = entry.testCase;
+ tc = new entry.testCase();
+ break;
+ }
+ }
+ } else if (args.testCase) {
+ name = args.testCase;
eval("tc = new " + args.testCase + "()");
+ }
+ if (tc != null) {
if (args.command) {
if (args.command == "runAllTests") {
console.log("Running all tests for " + args.testCase);
- tc.runAllTests();
+ postResults(tc.runAllTests());
}
if (args.command == "runTest") {
console.log("Running test " + args.testCase + "." + args.test);
- tc.runTest(args.test);
+ postResults(tc.runTest(args.test));
}
}
}
}
+
+ function postResults(results) {
+ var body = document.getElementsByTagName("body")[0];
+ var div = document.createElement("div");
+ body.insertBefore(div, body.firstChild);
+
+ var resultToHtml = function(result) {
+ return result ?
+ "<span style='color:green;'>pass</span>" :
+ "<span style='color:red;'>fail</span>";
+ }
+
+ if (typeof(results) == "boolean") {
+ div.innerHTML = "Test results: " + resultToHtml(results);
+ } else { // hash
+ var html = "";
+ for (var key in results) {
+ if (results.hasOwnProperty(key)) {
+ html = html + key + ": " + resultToHtml(results[key]) + "<br/>";
+ }
+ }
+ div.innerHTML = "Test results:<br/>" + html;
+ }
+ div.appendChild(document.createElement("hr"));
+ }
</script>
</head>
<body>
<div id='graph'></div>
+ <div id="selector"></div>
<p>This file is really nothing more than all the tests coalesced into a single
- HTML file. To run a test, open a Javascript console and execute, for
- instance,</p>
+ HTML file. To run a test, use the selector above, or
+ open a Javascript console and execute, for instance,</p>
<code>testDrawSimpleRangePlusOne()</code>
<p>Alternatively you can use query args: <ul>
- <li>testCase - for the name of the test case
+ <li>testCase - for the name of the test case prototype
+ <li>testCaseName - for the name of the test case
<li>test - for the name of the test (use command=runTest)
<li>command - either runTest or runAllTests.
</ul>
Example: <code>local.html?testCase=ScrollingDivTestCase&test=testNestedDiv_Scrolled&command=runTest</code>
+ <p/>
</body>
<script>
processVariables();
addGlobalTestSymbols();
+
+var selector = document.getElementById("selector");
+
+if (selector != null) { // running a test
+ var createAttached = function(name, parent) {
+ var elem = document.createElement(name);
+ parent.appendChild(elem);
+ return elem;
+ }
+
+ var description = createAttached("div", selector);
+ var list = createAttached("ul", selector);
+ var parent = list.parentElement;
+ var createLink = function(parent, text, url) {
+ var li = createAttached("li", parent);
+ var a = createAttached("a", li);
+ a.innerText = text;
+ a.href = url;
+ }
+ if (tc == null) {
+ description.innerHTML = "Test cases:";
+ var testCases = getAllTestCases();
+ for (var idx in testCases) {
+ var entryName = testCases[idx].name;
+ createLink(list, entryName, document.URL + "?testCaseName=" + entryName);
+ }
+ } else {
+ description.innerHTML = "Tests for " + name;
+ var names = tc.getTestNames();
+ createLink(list, "Run All Tests", document.URL + "&command=runAllTests");
+ for (var idx in names) {
+ var name = names[idx];
+ createLink(list, name, document.URL + "&test=" + name + "&command=runTest");
+ }
+ }
+}
</script>
</html>
assertEquals(1, clicked);
};
+InteractionModelTestCase.prototype.testIsZoomed_none = function() {
+ var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+ assertFalse(g.isZoomed());
+ assertFalse(g.isZoomed("x"));
+ assertFalse(g.isZoomed("y"));
+};
+
+InteractionModelTestCase.prototype.testIsZoomed_x = function() {
+ var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+ DygraphOps.dispatchMouseDown_Point(g, 10, 10);
+ DygraphOps.dispatchMouseMove_Point(g, 30, 10);
+ DygraphOps.dispatchMouseUp_Point(g, 30, 10);
+
+ assertTrue(g.isZoomed());
+ assertTrue(g.isZoomed("x"));
+ assertFalse(g.isZoomed("y"));
+};
+
+InteractionModelTestCase.prototype.testIsZoomed_y = function() {
+ var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+ DygraphOps.dispatchMouseDown_Point(g, 10, 10);
+ DygraphOps.dispatchMouseMove_Point(g, 10, 30);
+ DygraphOps.dispatchMouseUp_Point(g, 10, 30);
+
+ assertTrue(g.isZoomed());
+ assertFalse(g.isZoomed("x"));
+ assertTrue(g.isZoomed("y"));
+};
+
+InteractionModelTestCase.prototype.testIsZoomed_both = function() {
+ var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+ // Zoom x axis
+ DygraphOps.dispatchMouseDown_Point(g, 10, 10);
+ DygraphOps.dispatchMouseMove_Point(g, 30, 10);
+ DygraphOps.dispatchMouseUp_Point(g, 30, 10);
+
+ // Now zoom y axis
+ DygraphOps.dispatchMouseDown_Point(g, 10, 10);
+ DygraphOps.dispatchMouseMove_Point(g, 10, 30);
+ DygraphOps.dispatchMouseUp_Point(g, 10, 30);
+
+
+ assertTrue(g.isZoomed());
+ assertTrue(g.isZoomed("x"));
+ assertTrue(g.isZoomed("y"));
+};
+
+InteractionModelTestCase.prototype.testIsZoomed_updateOptions_none = function() {
+ var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+ g.updateOptions({});
+
+ assertFalse(g.isZoomed());
+ assertFalse(g.isZoomed("x"));
+ assertFalse(g.isZoomed("y"));
+};
+
+InteractionModelTestCase.prototype.testIsZoomed_updateOptions_x = function() {
+ var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+ g.updateOptions({dateWindow: [-.5, .3]});
+ assertTrue(g.isZoomed());
+ assertTrue(g.isZoomed("x"));
+ assertFalse(g.isZoomed("y"));
+};
+
+InteractionModelTestCase.prototype.testIsZoomed_updateOptions_y = function() {
+ var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+ g.updateOptions({valueRange: [1, 10]});
+
+ assertTrue(g.isZoomed());
+ assertFalse(g.isZoomed("x"));
+ assertTrue(g.isZoomed("y"));
+};
+
+InteractionModelTestCase.prototype.testIsZoomed_updateOptions_both = function() {
+ var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+ g.updateOptions({dateWindow: [-1, 1], valueRange: [1, 10]});
+
+ assertTrue(g.isZoomed());
+ assertTrue(g.isZoomed("x"));
+ assertTrue(g.isZoomed("y"));
+};
* option is also specified).
*/
Dygraph.prototype.isZoomed = function(axis) {
- if (axis === null) return this.zoomed_x_ || this.zoomed_y_;
+ if (axis == null) return this.zoomed_x_ || this.zoomed_y_;
if (axis === 'x') return this.zoomed_x_;
if (axis === 'y') return this.zoomed_y_;
- throw "axis parameter to Dygraph.isZoomed must be missing, 'x' or 'y'.";
+ throw "axis parameter is [" + axis + "] must be null, 'x' or 'y'.";
};
/**
this.parseFloat_(vals[1], i, line),
this.parseFloat_(vals[2], i, line) ];
} else {
- this.warning('When using customBars, values must be either blank ' +
- 'or "low;center;high" tuples (got "' + val +
- '" on line ' + (1+i));
+ this.warn('When using customBars, values must be either blank ' +
+ 'or "low;center;high" tuples (got "' + val +
+ '" on line ' + (1+i));
}
}
}