diff --git a/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptTestMojo.java b/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptTestMojo.java index 083deb0b6..217898fef 100644 --- a/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptTestMojo.java +++ b/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptTestMojo.java @@ -153,9 +153,18 @@ public class BuildJavascriptTestMojo extends AbstractMojo { } final Log log = getLog(); new File(outputDir, "tests").mkdirs(); - resourceToFile("org/teavm/javascript/runtime.js", "runtime.js"); - resourceToFile("org/teavm/maven/junit-support.js", "junit-support.js"); - resourceToFile("org/teavm/maven/junit.css", "junit.css"); + new File(outputDir, "res").mkdirs(); + resourceToFile("org/teavm/javascript/runtime.js", "res/runtime.js"); + resourceToFile("org/teavm/maven/res/junit-support.js", "res/junit-support.js"); + resourceToFile("org/teavm/maven/res/junit.css", "res/junit.css"); + resourceToFile("org/teavm/maven/res/class_obj.png", "res/class_obj.png"); + resourceToFile("org/teavm/maven/res/control-000-small.png", "res/control-000-small.png"); + resourceToFile("org/teavm/maven/res/methpub_obj.png", "res/methpub_obj.png"); + resourceToFile("org/teavm/maven/res/package_obj.png", "res/package_obj.png"); + resourceToFile("org/teavm/maven/res/tick-small-red.png", "res/tick-small-red.png"); + resourceToFile("org/teavm/maven/res/tick-small.png", "res/tick-small.png"); + resourceToFile("org/teavm/maven/res/toggle-small-expand.png", "res/toggle-small-expand.png"); + resourceToFile("org/teavm/maven/res/toggle-small.png", "res/toggle-small.png"); resourceToFile("org/teavm/maven/junit.html", "junit.html"); final ClassHolderSource classSource = new ClasspathClassHolderSource(classLoader); for (String testClass : testClasses) { @@ -170,8 +179,8 @@ public class BuildJavascriptTestMojo extends AbstractMojo { includeAdditionalScripts(classLoader); File allTestsFile = new File(outputDir, "tests/all.js"); try (Writer allTestsWriter = new OutputStreamWriter(new FileOutputStream(allTestsFile), "UTF-8")) { - allTestsWriter.write("doRunTests = function() {\n"); - allTestsWriter.write(" new JUnitServer(document.body).runAllTests(["); + allTestsWriter.write("prepare = function() {\n"); + allTestsWriter.write(" return new JUnitServer(document.body).readTests(["); boolean first = true; for (String testClass : testClasses) { if (!first) { @@ -547,3 +556,4 @@ public class BuildJavascriptTestMojo extends AbstractMojo { } } } + diff --git a/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit-support.js b/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit-support.js deleted file mode 100644 index 7be876edb..000000000 --- a/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit-support.js +++ /dev/null @@ -1,241 +0,0 @@ -/* - * Copyright 2013 Alexey Andreev. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -JUnitServer = function(container) { - this.container = container; - this.timeSpent = 0; - this.totalTimeSpent = 0; - this.methodCount = 0; - this.statusCell = null; - this.exceptionCell = null; - this.timeCell = null; - this.startTime = 0; - this.expectedExceptions = []; - this.table = null; - this.tableBody = null; - this.frame = null; -} -JUnitServer.prototype = new Object(); -JUnitServer.prototype.handleEvent = function(message, callback) { - endTime = new Date().getTime(); - if (message.status === "ok") { - if (this.expectedExceptions.length > 0) { - this.statusCell.appendChild(document.createTextNode("expected exception not thrown")); - this.statusCell.style.color = 'yellow'; - } else { - this.statusCell.appendChild(document.createTextNode("ok")); - this.statusCell.style.color = 'green'; - } - } else if (message.status === "exception") { - if (message.exception && this.isExpectedException(message.exception)) { - this.statusCell.appendChild(document.createTextNode("ok")); - this.statusCell.style.color = 'green'; - } else { - this.statusCell.appendChild(document.createTextNode("unexpected exception")); - var exceptionText = document.createElement("pre"); - exceptionText.appendChild(document.createTextNode(message.stack)); - this.exceptionCell.appendChild(exceptionText); - this.statusCell.style.color = 'red'; - } - } - ++this.methodCount; - var timeSpent = (endTime - this.startTime) / 1000; - this.timeSpent += timeSpent; - this.timeCell.appendChild(document.createTextNode(timeSpent.toFixed(3))); - document.body.removeChild(this.frame); - self.frame = null; - callback(); -} -JUnitServer.prototype.isExpectedException = function(ex) { - for (var i = 0; i < this.expectedExceptions.length; ++i) { - if (this.expectedExceptions[i] === ex) { - return true; - } - } - return false; -} -JUnitServer.prototype.runTestCase = function(methodName, path, expectedExceptions, additionalScripts, callback) { - this.createRow(methodName); - this.startTime = new Date().getTime(); - this.expectedExceptions = expectedExceptions; - var self = this; - this.loadCode(path, additionalScripts, function() { - messageHandler = function(event) { - window.removeEventListener("message", messageHandler); - self.handleEvent(JSON.parse(event.data), callback); - }; - window.addEventListener("message", messageHandler); - self.frame.contentWindow.postMessage("runTest", "*"); - }); -} -JUnitServer.prototype.createRow = function(methodName) { - var row = document.createElement("tr"); - this.tableBody.appendChild(row); - var nameCell = document.createElement("td"); - row.appendChild(nameCell); - nameCell.appendChild(document.createTextNode(methodName)); - this.statusCell = document.createElement("td"); - row.appendChild(this.statusCell); - this.exceptionCell = document.createElement("td"); - row.appendChild(this.exceptionCell); - this.timeCell = document.createElement("td"); - row.appendChild(this.timeCell); -} -JUnitServer.prototype.loadCode = function(path, additionalScripts, callback) { - this.frame = document.createElement("iframe"); - document.body.appendChild(this.frame); - var frameDoc = this.frame.contentWindow.document; - var self = this; - var sequence = ["junit-support.js", "runtime.js"]; - if (additionalScripts) { - for (var i = 0; i < additionalScripts.length; ++i) { - sequence.push(additionalScripts[i]); - } - } - sequence.push(path); - self.loadScripts(sequence, 0, callback); -} -JUnitServer.prototype.loadScripts = function(scripts, index, callback) { - var self = this; - if (index == scripts.length) { - callback(); - } else { - this.loadScript(scripts[index], function() { self.loadScripts(scripts, index + 1, callback) }); - } -} -JUnitServer.prototype.loadScript = function(name, callback) { - var doc = this.frame.contentWindow.document; - var script = doc.createElement("script"); - script.src = name; - doc.body.appendChild(script); - script.onload = function() { - callback(); - } -} -JUnitServer.prototype.runTest = function(test, callback) { - this.timeSpent = 0; - this.methodCount = 0; - this.createTable(test.name); - var self = this; - this.runMethodFromList(test.methods, 0, function() { - self.createFooter(); - callback(); - }); -} -JUnitServer.prototype.runAllTests = function(tests, callback) { - this.runTestFromList(tests, 0, callback); -} -JUnitServer.prototype.runTestFromList = function(tests, index, callback) { - if (index < tests.length) { - var test = tests[index]; - var self = this; - this.runTest(test, function() { - self.runTestFromList(tests, index + 1, callback); - }); - } else { - callback(); - } -} -JUnitServer.prototype.runMethodFromList = function(methods, index, callback) { - if (index < methods.length) { - var method = methods[index]; - var self = this; - this.runTestCase(method.name, method.script, method.expected, method.additionalScripts, function() { - self.runMethodFromList(methods, index + 1, callback); - }); - } else { - callback(); - } -} -JUnitServer.prototype.createTable = function(name) { - this.table = document.createElement("table"); - this.container.appendChild(this.table); - var caption = document.createElement("caption"); - this.table.appendChild(caption); - this.createHeader(); - caption.appendChild(document.createTextNode(name)); - this.tableBody = document.createElement("tbody"); - this.table.appendChild(this.tableBody); -} -JUnitServer.prototype.createHeader = function() { - var head = document.createElement("thead"); - this.table.appendChild(head); - var headRow = document.createElement("tr"); - head.appendChild(headRow); - var headCell = document.createElement("th"); - headRow.appendChild(headCell); - headCell.appendChild(document.createTextNode("Method")); - headCell = document.createElement("th"); - headRow.appendChild(headCell); - headCell.appendChild(document.createTextNode("Result")); - headCell = document.createElement("th"); - headRow.appendChild(headCell); - headCell.appendChild(document.createTextNode("Exception")); - headCell = document.createElement("th"); - headRow.appendChild(headCell); - headCell.appendChild(document.createTextNode("Time spent, s")); - this.table.appendChild(head); -} -JUnitServer.prototype.createFooter = function() { - var foot = document.createElement("tfoot"); - this.table.appendChild(foot); - var footRow = document.createElement("tr"); - foot.appendChild(footRow); - var footName = document.createElement("td"); - footRow.appendChild(footName); - footName.appendChild(document.createTextNode("---")); - var footMethods = document.createElement("td"); - footRow.appendChild(footMethods); - footMethods.appendChild(document.createTextNode(this.methodCount)); - var footSpace = document.createElement("td"); - footRow.appendChild(footSpace); - footSpace.appendChild(document.createTextNode("---")); - var footTime = document.createElement("td"); - footRow.appendChild(footTime); - footTime.appendChild(document.createTextNode(this.timeSpent.toFixed(3))); -} - -JUnitClient = {}; -JUnitClient.run = function() { - var handler = window.addEventListener("message", function() { - window.removeEventListener("message", handler); - var message = {}; - try { - var instance = new TestClass(); - initInstance(instance); - runTest(instance); - message.status = "ok"; - } catch (e) { - message.status = "exception"; - if (e.$javaException && e.$javaException.constructor.$meta) { - message.exception = e.$javaException.constructor.$meta.name; - message.stack = e.$javaException.constructor.$meta.name + ": "; - var exceptionMessage = extractException(e.$javaException); - message.stack += exceptionMessage ? $rt_ustr(exceptionMessage) : ""; - message.stack += "\n" + e.stack; - } else { - message.stack = e.stack; - } - } - window.parent.postMessage(JSON.stringify(message), "*"); - }); -} -JUnitClient.reportError = function(error) { - var handler = window.addEventListener("message", function() { - window.removeEventListener("message", handler); - var message = { status : "exception", stack : error }; - window.parent.postMessage(JSON.stringify(message), "*"); - }); -} \ No newline at end of file diff --git a/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit.css b/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit.css deleted file mode 100644 index d66b8c9fb..000000000 --- a/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit.css +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2013 Alexey Andreev. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -table { - border-collapse: collapse; - border: 2px solid black; - margin: 2em 1em 2em 1em; -} -table td, table th { - border: 1px solid gray; - padding: 0.1em 0.5em 0.2em 0.5em; -} -table thead, table tfoot { - border: 2px solid black; -} -iframe { - with: 1px; - height: 1px; - padding: 0px; - margin: 0px; - border-style: none; -} \ No newline at end of file diff --git a/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit.html b/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit.html index 45e3e79e3..06b655d11 100644 --- a/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit.html +++ b/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit.html @@ -19,17 +19,35 @@