/** * Copyright (C) 2012,2013 KO GmbH * @licstart * The JavaScript code in this page is free software: you can redistribute it * and/or modify it under the terms of the GNU Affero General Public License * (GNU AGPL) as published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. The code is distributed * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details. * * You should have received a copy of the GNU Affero General Public License * along with this code. If not, see . * * As additional permission under GNU AGPL version 3 section 7, you * may distribute non-source (e.g., minimized or compacted) forms of * that code without the copy of the GNU GPL normally required by * section 4, provided you include this license notice and a URL * through which recipients can access the Corresponding Source. * * As a special exception to the AGPL, any HTML file which merely makes function * calls to this code, and for that purpose includes it by reference shall be * deemed a separate work for copyright law purposes. In addition, the copyright * holders of this code give you permission to combine this code with free * software libraries that are released under the GNU LGPL. You may copy and * distribute such a system following the terms of the GNU AGPL for this code * and the LGPL for the libraries. If you modify this code, you may extend this * exception to your version of the code, but you are not obligated to do so. * If you do not wish to do so, delete this exception statement from your * version. * * This license applies to this entire compilation. * @licend * @source: http://www.webodf.org/ * @source: https://github.com/kogmbh/WebODF/ */ /*global runtime, Runtime, core, Node, Element*/ /*jslint evil: true, continue: true, emptyblock: true, unparam: true*/ /** * @interface */ core.UnitTest = function UnitTest() {"use strict"; }; /** * @return {undefined} */ core.UnitTest.prototype.setUp = function () {"use strict"; }; /** * @return {undefined} */ core.UnitTest.prototype.tearDown = function () {"use strict"; }; /** * @return {!string} */ core.UnitTest.prototype.description = function () {"use strict"; }; /** * @return {Array.} */ core.UnitTest.prototype.tests = function () {"use strict"; }; /** * @return {Array.} */ core.UnitTest.prototype.asyncTests = function () {"use strict"; }; /** * @return {!Element} */ core.UnitTest.provideTestAreaDiv = function () { "use strict"; var maindoc = runtime.getWindow().document, testarea = maindoc.getElementById('testarea'); runtime.assert(!testarea, "Unclean test environment, found a div with id \"testarea\"."); testarea = maindoc.createElement('div'); testarea.setAttribute('id', 'testarea'); maindoc.body.appendChild(testarea); return testarea; }; /** * @return {undefined} */ core.UnitTest.cleanupTestAreaDiv = function () { "use strict"; var maindoc = runtime.getWindow().document, testarea = maindoc.getElementById('testarea'); runtime.assert((!!testarea && (testarea.parentNode === maindoc.body)), "Test environment broken, found no div with id \"testarea\" below body."); maindoc.body.removeChild(testarea); }; /** * Creates and returns a simple ODT document * @param {!string} xml Xml fragment to insert in the document between the .. tags * @param {!Object.} namespaceMap Name-value pairs that map the prefix onto the appropriate uri namespace * @returns {?Document} */ core.UnitTest.createOdtDocument = function(xml, namespaceMap) { "use strict"; var xmlDoc = ""; xmlDoc += "" + text + ""; } /** * Run the tests from TestClass. * If parameter testNames is supplied only the tests with the names * supplied in that array will be executed. * * @param {Function} TestClass the constructor for the test class * @param {!function():undefined} callback * @param {!Array.} testNames * @return {undefined} */ this.runTests = function (TestClass, callback, testNames) { var testName = Runtime.getFunctionName(TestClass), tname, runner = new core.UnitTestRunner(), test = new TestClass(runner), testResults = {}, i, t, tests, lastFailCount, testNameString = "testName", inBrowser = runtime.type() === "BrowserRuntime"; // check that this test has not been run or started yet if (results.hasOwnProperty(testName)) { runtime.log("Test " + testName + " has already run."); return; } if (inBrowser) { runtime.log("Running " + link(testName, "runSuite(\"" + testName + "\");") + ": " + test.description() + ""); } else { runtime.log("Running " + testName + ": " + test.description); } tests = test.tests(); for (i = 0; i < tests.length; i += 1) { t = tests[i]; tname = Runtime.getFunctionName(t) || t[testNameString]; if (testNames.length && testNames.indexOf(tname) === -1) { continue; } if (inBrowser) { runtime.log("Running " + link(tname, "runTest(\"" + testName + "\",\"" + tname + "\")") + ""); } else { runtime.log("Running " + tname); } lastFailCount = runner.countFailedTests(); test.setUp(); t(); test.tearDown(); testResults[tname] = lastFailCount === runner.countFailedTests(); } function runAsyncTests(todo) { if (todo.length === 0) { results[testName] = testResults; failedTests += runner.countFailedTests(); callback(); return; } t = todo[0]; var fname = Runtime.getFunctionName(t); runtime.log("Running " + fname); lastFailCount = runner.countFailedTests(); test.setUp(); t(function () { test.tearDown(); testResults[fname] = lastFailCount === runner.countFailedTests(); runAsyncTests(todo.slice(1)); }); } runAsyncTests(test.asyncTests()); }; /** * @return {!number} **/ this.countFailedTests = function () { return failedTests; }; /** * @return {!Object} **/ this.results = function () { return results; }; };