c9-core/plugins/c9.ide.plugins/test.js

280 wiersze
8.7 KiB
JavaScript
Czysty Zwykły widok Historia

2016-06-26 11:53:19 +00:00
/* global requirejs */
define(function(require, exports, module) {
main.consumes = [
"Plugin", "plugin.debug", "c9", "menus", "ui", "ext", "preview",
"preview.browser"
];
main.provides = ["plugin.test"];
return main;
function main(options, imports, register) {
var Plugin = imports.Plugin;
var c9 = imports.c9;
var menus = imports.menus;
var preview = imports.preview;
var ext = imports.ext;
var ui = imports.ui;
var debug = imports["plugin.debug"];
var browser = imports["preview.browser"];
/***** Initialization *****/
var plugin = new Plugin("Ajax.org", main.consumes);
var emit = plugin.getEmitter();
var chai, mocha, iframe, architect;
var ENABLED = c9.location.indexOf("debug=2") > -1;
var loaded = false;
function load() {
if (loaded) return false;
loaded = true;
if (!ENABLED) return;
2017-01-30 11:32:54 +00:00
debug.once("ready", function() {
2016-06-26 11:53:19 +00:00
menus.addItemByPath("Tools/Developer/Tests", null, 200, plugin);
2017-01-30 11:32:54 +00:00
debug.plugins.forEach(function(name, i) {
2016-06-26 11:53:19 +00:00
menus.addItemByPath("Tools/Developer/Tests/" + name.replace(/\//g, "\\/"), new ui.item({
2017-01-30 11:32:54 +00:00
onclick: function() {
run(name, function(err) {
2016-06-26 11:53:19 +00:00
if (err) console.error(err);
});
}
}), i + 1, plugin);
});
});
2017-01-30 11:32:54 +00:00
ext.on("register", function() {
2016-06-26 11:53:19 +00:00
// TODO
}, plugin);
2017-01-30 11:32:54 +00:00
ext.on("unregister", function() {
2016-06-26 11:53:19 +00:00
// TODO
}, plugin);
var reloading;
2017-01-30 11:32:54 +00:00
function loadPreview(url, session) {
2016-06-26 11:53:19 +00:00
var idx = url.indexOf(options.staticPrefix);
if (!reloading && idx > -1) {
reloading = true;
var name = session.doc.meta.pluginName;
2017-01-30 11:32:54 +00:00
run(name, function(err) {
2016-06-26 11:53:19 +00:00
if (err) console.error(err);
});
reloading = false;
}
}
2017-01-30 11:32:54 +00:00
browser.on("reload", function(e) {
2016-06-26 11:53:19 +00:00
loadPreview(e.session.path, e.session);
});
}
/***** Methods *****/
2017-01-30 11:32:54 +00:00
function setReferences(c, m) {
2016-06-26 11:53:19 +00:00
chai = c;
mocha = m;
emit("ready");
}
2017-01-30 11:32:54 +00:00
function loadIframe(pluginName, callback) {
2016-06-26 11:53:19 +00:00
var url = options.staticPrefix + "/test.html";
if (url.indexOf("http") !== 0)
url = location.origin + url;
var tab = preview.openPreview(url, null, true);
iframe = tab.document.getSession().iframe;
iframe.addEventListener("load", handle);
iframe.addEventListener("error", onError);
2017-01-30 11:32:54 +00:00
function handle(err) {
2016-06-26 11:53:19 +00:00
iframe.removeEventListener("load", handle);
iframe.removeEventListener("error", onError);
callback(err instanceof Error ? err : null, tab);
}
2017-01-30 11:32:54 +00:00
function onError(e) {
2016-06-26 11:53:19 +00:00
debugger; // e.??
handle(new Error());
}
tab.document.meta.ignoreState = true;
tab.document.meta.pluginName = pluginName;
}
2017-01-30 11:32:54 +00:00
function loadTestSuite(name, callback) {
2016-06-26 11:53:19 +00:00
// Clear require cache
requirejs.undef("plugins/" + name + "_test"); // global
// Load plugin
architect.loadAdditionalPlugins([{
packagePath: "plugins/" + name + "_test"
2017-01-30 11:32:54 +00:00
}], function(err) {
2016-06-26 11:53:19 +00:00
callback(err);
});
}
2017-01-30 11:32:54 +00:00
function run(pluginName, callback) {
2016-06-26 11:53:19 +00:00
// Load test runner
2017-01-30 11:32:54 +00:00
loadIframe(pluginName, function(err, tab) {
2016-06-26 11:53:19 +00:00
if (err) return callback(err);
2017-01-30 11:32:54 +00:00
tab.editor.setLocation("test://" + pluginName);
2016-06-26 11:53:19 +00:00
// Wait until iframe is loaded
2017-01-30 11:32:54 +00:00
plugin.once("ready", function() {
2016-06-26 11:53:19 +00:00
// Load the test for the plugin
2017-01-30 11:32:54 +00:00
loadTestSuite(pluginName, function(err) {
2016-06-26 11:53:19 +00:00
if (err) return callback(err);
// Run the test
2017-01-30 11:32:54 +00:00
mocha.run(function() {
2016-06-26 11:53:19 +00:00
// Done
callback();
});
});
});
// Load iframe with new test runner frame
iframe.contentWindow.start(plugin);
2017-01-30 11:32:54 +00:00
});
2016-06-26 11:53:19 +00:00
}
/***** Lifecycle *****/
plugin.on("load", function() {
load();
});
plugin.on("unload", function() {
loaded = false;
chai = null;
mocha = null;
iframe = null;
architect = null;
});
/***** Register and define API *****/
/**
*
**/
plugin.freezePublicAPI({
/**
*
*/
2017-01-30 11:32:54 +00:00
get architect() { throw new Error(); },
set architect(v) { architect = v; },
2016-06-26 11:53:19 +00:00
/**
*
*/
2017-01-30 11:32:54 +00:00
get describe() { return mocha.describe; },
2016-06-26 11:53:19 +00:00
/**
*
*/
2017-01-30 11:32:54 +00:00
get it() { return mocha.it; },
2016-06-26 11:53:19 +00:00
/**
*
*/
2017-01-30 11:32:54 +00:00
get before() { return mocha.before; },
2016-06-26 11:53:19 +00:00
/**
*
*/
2017-01-30 11:32:54 +00:00
get after() { return mocha.after; },
2016-06-26 11:53:19 +00:00
/**
*
*/
2017-01-30 11:32:54 +00:00
get beforeEach() { return mocha.beforeEach; },
2016-06-26 11:53:19 +00:00
/**
*
*/
2017-01-30 11:32:54 +00:00
get afterEach() { return mocha.afterEach; },
2016-06-26 11:53:19 +00:00
/**
*
*/
2017-01-30 11:32:54 +00:00
get assert() { return chai.assert; },
2016-06-26 11:53:19 +00:00
/**
*
*/
2017-01-30 11:32:54 +00:00
get expect() { return chai.expect; },
2016-06-26 11:53:19 +00:00
/**
*
*/
setReferences: setReferences,
/**
*
*/
run: run
});
register(null, {
"plugin.test": plugin
});
}
});
//@TODO look at jasmine instead
// require(["lib/architect/architect", "lib/chai/chai", "/vfs-root"], function() {
// mocha.setup('bdd');
// mocha.bail(false);
// mocha.ignoreLeaks(true);
// mocha.run(done)
// /*global Mocha, mocha*/
// mocha.reporter(function(runner) {
// Mocha.reporters.Base.call(this, runner);
// Mocha.reporters.HTML.call(this, runner);
// var tests = [];
// var stats = this.stats;
// mocha.report = { stats: stats, tests: tests };
// runner.on('test end', function(test) {
// stats.percent = stats.tests / runner.total * 100 | 0;
// tests.push(clean(test));
// });
// runner.on('end', function() {
// console.log(JSON.stringify(mocha.report, null, 4));
// });
// function parseError(err) {
// var str = err.stack || err.toString();
// // FF / Opera do not add the message
// if (!~str.indexOf(err.message)) {
// str = err.message + '\n' + str;
// }
// // <=IE7 stringifies to [Object Error]. Since it can be overloaded, we
// // check for the result of the stringifying.
// if ('[object Error]' == str) str = err.message;
// // Safari doesn't give you a stack. Let's at least provide a source line.
// if (!err.stack && err.sourceURL && err.line !== undefined) {
// str += "\n(" + err.sourceURL + ":" + err.line + ")";
// }
// return str;
// }
// function clean(test) {
// return {
// title: test.title,
// duration: test.duration,
// error: test.err && parseError(test.err),
// speed: test.speed,
// state: test.state
// };
// }
// });