Tone.js/test/examples/Examples.js

66 lines
1.3 KiB
JavaScript
Raw Normal View History

define(["Test", "Examples", "helper/Supports"], function (Test, Examples, Supports) {
2017-03-26 18:51:25 +00:00
var baseUrl = "../examples/";
if (window.__karma__){
2017-03-26 19:21:45 +00:00
baseUrl = "/base/examples/";
2017-03-26 18:51:25 +00:00
}
if (Supports.RUN_EXAMPLES){
2017-03-26 19:21:45 +00:00
function getIframeError(url){
return new Promise(function(success, error){
var iframe = document.createElement("iframe");
iframe.onload = function(){
iframe.remove();
success();
};
iframe.width = 1;
iframe.height = 1;
iframe.src = url;
document.body.appendChild(iframe);
//capture the error
iframe.contentWindow.onerror=function(e) {
error(e);
};
});
}
2017-03-26 19:21:45 +00:00
function createTest(url){
2017-12-30 16:26:29 +00:00
it(url, function(){
url = baseUrl + url + ".html";
return testUrl(url).then(getIframeError);
});
}
2017-03-26 19:21:45 +00:00
function testUrl(url){
return new Promise(function(success, fail){
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
success(url);
} else {
fail("404: "+url);
}
2017-03-26 19:21:45 +00:00
}
};
httpRequest.open("GET", url);
httpRequest.send();
});
}
context("Examples", function(){
for (var category in Examples){
var group = Examples[category];
for (var name in group){
var url = group[name];
createTest(url);
2017-03-26 19:21:45 +00:00
}
}
2017-03-26 18:51:25 +00:00
});
}
});