diff --git a/test/reportTestResults.browser.js b/test/reportTestResults.browser.js new file mode 100644 index 0000000000..dfb2a254ee --- /dev/null +++ b/test/reportTestResults.browser.js @@ -0,0 +1,75 @@ +console._error = console.error; +console._warn = console.warn; +console._log = console.log; + +console.log = window.print = function(message){ + console._log(message); + testImageURL('/print?type=log&message=' + encodeURIComponent(message) + '&_=' + Date.now().toString(36)); +}; + +console.error = function(message){ + console._error(message); + testImageURL('/print?type=error&message=' + encodeURIComponent(message) + '&_=' + Date.now().toString(36)); +}; + +console.warn = function(message){ + console._warn(message); + testImageURL('/print?type=warn&message=' + encodeURIComponent(message) + '&_=' + Date.now().toString(36)); +}; + +;(function(env){ + + env.addReporter(new jasmine.JSReporter()); + env.addReporter(new TAPReporter(window.print)); + + function report(){ + if (typeof jasmine.getJSReport != 'function') return setTimeout(report, 100); + postDataToURL(JSON.stringify(jasmine.getJSReport()), '/reportTestResults', function(error, event){ + if (error) return console.error(error); + console.log(event); + }); + } + + var oldCallback = env.currentRunner().finishCallback; + env.currentRunner().finishCallback = function(){ + if (oldCallback) oldCallback.apply(this, arguments); + report(); + }; + + function postDataToURL(data, url, callback){ + var id = '$' + (+new Date()).toString(36); + + var postReportingTarget = document.createElement('iframe'); + postReportingTarget.id = postReportingTarget.name = 'postReportingTarget' + id; + + var postReportingForm = document.createElement('form'); + postReportingForm.method = 'POST'; + postReportingForm.action = url; + postReportingForm.target = postReportingTarget.name; + + var postReportingData = document.createElement('input'); + postReportingData.type = 'hidden'; + postReportingData.name = 'data'; + postReportingData.value = data || '{"error":"unknown error in postReportingData"}'; + + postReportingForm.appendChild(postReportingData); + postReportingForm.appendChild(postReportingTarget); + postReportingForm.style.cssText = "visibility:hidden; position:absolute; bottom:100%; right:100%"; + + function done(error, event){ + postReportingForm.parentNode.removeChild(postReportingForm); + callback(error, event); + } + + postReportingTarget.onerror = function(error){ + done(error); + } + postReportingTarget.onload = function(event){ + done(null, event); + } + document.body.appendChild(postReportingForm); + postReportingForm.submit(); + } + +}(window.jasmine.getEnv())); + diff --git a/test/testImageURL.browser.js b/test/testImageURL.browser.js new file mode 100644 index 0000000000..8dc34d16f1 --- /dev/null +++ b/test/testImageURL.browser.js @@ -0,0 +1,60 @@ +testImageURL._recycle = function(img){ + console.log('_recycle', img); + try { + img.src = ''; + img.onload = img.onerror = null; + } catch(e){} + testImageURL._recycleBin.push(img); +} +testImageURL.getImage = function(callback){ + // if (!testImageURL._recycleBin) testImageURL._recycleBin = [new Image(),new Image(),new Image(),new Image()]; + // function get(){ + // if (testImageURL._recycleBin.length === 0) return setTimeout(get, 100); + // callback(testImageURL._recycleBin.shift(), testImageURL._recycle); + // } + // get(); + callback(new Image(), function recycle(){}); +} + +testImageURL.defaultCallback = function(error, event){} + +function testImageURL(url, timeout, callback){ + if (typeof timeout == 'function'){ + callback = timeout; + timeout = testImageURL.timeout; + } + if (typeof callback != 'function') callback = testImageURL.defaultCallback; + + testImageURL.getImage(function(img, done){ + function callbackWrapper(error, event){ + callbackWrapper = testImageURL.noop; + clearTimeout(timer); + done(img); + img = url = timeout = null; + callback(error, event); + error = event = callback = null; + } + + var timer = setTimeout(function(){callbackWrapper(Error('timeout'));}, timeout); + + try { + img.onload = function(event){ callbackWrapper(null, event || window.event); }; + img.onerror = function(error){ callbackWrapper(error); }; + img.src = url; + + if (img.complete === true + || img.readyState == 4 + || img.width > 0 + || img.height > 0 + || img.readyState == 'complete' + ) callbackWrapper(null, null); + } + catch(error){ + callbackWrapper(error); + } + }); +} + +testImageURL.noop = function(){}; + +testImageURL.timeout = 5000;