2012-10-06 21:29:37 +02:00
|
|
|
var helper = {};
|
2012-10-02 01:35:43 +02:00
|
|
|
|
|
|
|
(function(){
|
2012-10-29 12:37:41 +01:00
|
|
|
var $iframeContainer, $iframe, jsLibraries = {};
|
2012-10-03 18:37:48 +02:00
|
|
|
|
2012-10-08 00:34:29 +02:00
|
|
|
helper.init = function(cb){
|
2012-10-03 18:37:48 +02:00
|
|
|
$iframeContainer = $("#iframe-container");
|
2012-10-08 00:34:29 +02:00
|
|
|
|
|
|
|
$.get('/static/js/jquery.js').done(function(code){
|
2012-10-08 14:07:08 +02:00
|
|
|
// make sure we don't override existing jquery
|
|
|
|
jsLibraries["jquery"] = "if(typeof $ === 'undefined') {\n" + code + "\n}";
|
2012-10-08 00:34:29 +02:00
|
|
|
|
2012-10-08 19:54:03 +02:00
|
|
|
$.get('/tests/frontend/lib/sendkeys.js').done(function(code){
|
2012-10-08 00:34:29 +02:00
|
|
|
jsLibraries["sendkeys"] = code;
|
|
|
|
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
});
|
2012-10-03 18:37:48 +02:00
|
|
|
}
|
|
|
|
|
2012-10-06 21:29:37 +02:00
|
|
|
helper.randomString = function randomString(len)
|
2012-10-03 18:37:48 +02:00
|
|
|
{
|
|
|
|
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
|
|
var randomstring = '';
|
|
|
|
for (var i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
var rnum = Math.floor(Math.random() * chars.length);
|
|
|
|
randomstring += chars.substring(rnum, rnum + 1);
|
|
|
|
}
|
|
|
|
return randomstring;
|
|
|
|
}
|
|
|
|
|
2012-10-08 00:34:29 +02:00
|
|
|
var getFrameJQuery = function($iframe){
|
|
|
|
/*
|
|
|
|
I tried over 9000 ways to inject javascript into iframes.
|
|
|
|
This is the only way I found that worked in IE 7+8+9, FF and Chrome
|
|
|
|
*/
|
|
|
|
|
2012-10-06 21:29:37 +02:00
|
|
|
var win = $iframe[0].contentWindow;
|
|
|
|
var doc = win.document;
|
|
|
|
|
2012-10-08 00:34:29 +02:00
|
|
|
//IE 8+9 Hack to make eval appear
|
|
|
|
//http://stackoverflow.com/questions/2720444/why-does-this-window-object-not-have-the-eval-function
|
|
|
|
win.execScript && win.execScript("null");
|
2012-10-06 21:29:37 +02:00
|
|
|
|
2012-10-08 00:34:29 +02:00
|
|
|
win.eval(jsLibraries["jquery"]);
|
|
|
|
win.eval(jsLibraries["sendkeys"]);
|
|
|
|
|
|
|
|
win.$.window = win;
|
|
|
|
win.$.document = doc;
|
2012-10-06 21:29:37 +02:00
|
|
|
|
2012-10-08 00:34:29 +02:00
|
|
|
return win.$;
|
2012-10-06 21:29:37 +02:00
|
|
|
}
|
|
|
|
|
2012-11-04 00:48:10 +01:00
|
|
|
helper.clearCookies = function(){
|
|
|
|
window.document.cookie = "";
|
|
|
|
}
|
|
|
|
|
2013-01-15 22:17:40 +01:00
|
|
|
helper.newPad = function(cb, padName){
|
2012-11-04 00:48:10 +01:00
|
|
|
//build opts object
|
|
|
|
var opts = {clearCookies: true}
|
2013-01-15 22:17:40 +01:00
|
|
|
if(typeof cb === 'function'){
|
|
|
|
opts.cb = cb
|
2012-11-04 00:48:10 +01:00
|
|
|
} else {
|
2013-01-15 22:17:40 +01:00
|
|
|
opts = _.defaults(cb, opts);
|
2012-11-04 00:48:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//clear cookies
|
|
|
|
if(opts.clearCookies){
|
|
|
|
helper.clearCookies();
|
|
|
|
}
|
|
|
|
|
2013-01-15 22:17:40 +01:00
|
|
|
if(!padName)
|
|
|
|
padName = "FRONTEND_TEST_" + helper.randomString(20);
|
2012-10-03 18:37:48 +02:00
|
|
|
$iframe = $("<iframe src='/p/" + padName + "'></iframe>");
|
2012-10-08 00:34:29 +02:00
|
|
|
|
|
|
|
//clean up inner iframe references
|
|
|
|
helper.padChrome$ = helper.padOuter$ = helper.padInner$ = null;
|
|
|
|
|
|
|
|
//clean up iframes properly to prevent IE from memoryleaking
|
|
|
|
$iframeContainer.find("iframe").purgeFrame().done(function(){
|
|
|
|
$iframeContainer.append($iframe);
|
|
|
|
$iframe.one('load', function(){
|
|
|
|
helper.waitFor(function(){
|
|
|
|
return !$iframe.contents().find("#editorloadingbox").is(":visible");
|
2012-11-02 00:19:59 +01:00
|
|
|
}, 50000).done(function(){
|
2012-10-08 00:34:29 +02:00
|
|
|
helper.padChrome$ = getFrameJQuery( $('#iframe-container iframe'));
|
2013-02-10 23:02:24 +01:00
|
|
|
helper.padOuter$ = getFrameJQuery(helper.padChrome$('iframe[name="ace_outer"]'));
|
|
|
|
helper.padInner$ = getFrameJQuery( helper.padOuter$('iframe[name="ace_inner"]'));
|
2012-11-04 00:48:10 +01:00
|
|
|
|
|
|
|
//disable all animations, this makes tests faster and easier
|
|
|
|
helper.padChrome$.fx.off = true;
|
|
|
|
helper.padOuter$.fx.off = true;
|
|
|
|
helper.padInner$.fx.off = true;
|
2012-10-08 00:34:29 +02:00
|
|
|
|
2012-11-04 00:48:10 +01:00
|
|
|
opts.cb();
|
2012-10-08 00:34:29 +02:00
|
|
|
}).fail(function(){
|
|
|
|
throw new Error("Pad never loaded");
|
|
|
|
});
|
2012-10-06 21:29:37 +02:00
|
|
|
});
|
2012-10-08 00:34:29 +02:00
|
|
|
});
|
2012-10-02 01:35:43 +02:00
|
|
|
|
2012-10-03 18:37:48 +02:00
|
|
|
return padName;
|
|
|
|
}
|
|
|
|
|
2012-10-06 21:29:37 +02:00
|
|
|
helper.waitFor = function(conditionFunc, _timeoutTime, _intervalTime){
|
|
|
|
var timeoutTime = _timeoutTime || 1000;
|
|
|
|
var intervalTime = _intervalTime || 10;
|
2012-10-08 00:34:29 +02:00
|
|
|
|
|
|
|
var deferred = $.Deferred();
|
2012-10-06 21:29:37 +02:00
|
|
|
|
2012-10-08 00:34:29 +02:00
|
|
|
var _fail = deferred.fail;
|
|
|
|
var listenForFail = false;
|
|
|
|
deferred.fail = function(){
|
|
|
|
listenForFail = true;
|
|
|
|
_fail.apply(this, arguments);
|
|
|
|
}
|
2012-10-02 01:35:43 +02:00
|
|
|
|
2012-10-06 21:29:37 +02:00
|
|
|
var intervalCheck = setInterval(function(){
|
2012-10-06 21:45:46 +02:00
|
|
|
var passed = false;
|
|
|
|
|
2012-10-08 00:34:29 +02:00
|
|
|
passed = conditionFunc();
|
2012-10-02 01:35:43 +02:00
|
|
|
|
2012-10-06 21:29:37 +02:00
|
|
|
if(passed){
|
|
|
|
clearInterval(intervalCheck);
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
2012-10-08 00:34:29 +02:00
|
|
|
deferred.resolve();
|
2012-10-06 21:29:37 +02:00
|
|
|
}
|
|
|
|
}, intervalTime);
|
|
|
|
|
|
|
|
var timeout = setTimeout(function(){
|
|
|
|
clearInterval(intervalCheck);
|
2012-10-08 00:34:29 +02:00
|
|
|
var error = new Error("wait for condition never became true " + conditionFunc.toString());
|
|
|
|
deferred.reject(error);
|
|
|
|
|
|
|
|
if(!listenForFail){
|
|
|
|
throw error;
|
|
|
|
}
|
2012-10-06 21:29:37 +02:00
|
|
|
}, timeoutTime);
|
2012-10-02 01:35:43 +02:00
|
|
|
|
2012-10-08 00:34:29 +02:00
|
|
|
return deferred;
|
2012-10-03 18:37:48 +02:00
|
|
|
}
|
2012-10-02 01:35:43 +02:00
|
|
|
|
2012-10-08 00:34:29 +02:00
|
|
|
/* Ensure console.log doesn't blow up in IE, ugly but ok for a test framework imho*/
|
|
|
|
window.console = window.console || {};
|
|
|
|
window.console.log = window.console.log || function(){}
|
2012-10-06 21:29:37 +02:00
|
|
|
|
2012-10-08 00:34:29 +02:00
|
|
|
//force usage of callbacks in it
|
|
|
|
var _it = it;
|
|
|
|
it = function(name, func){
|
|
|
|
if(func && func.length !== 1){
|
2012-10-08 13:37:24 +02:00
|
|
|
func = function(){
|
|
|
|
throw new Error("Please use always a callback with it() - " + func.toString());
|
|
|
|
}
|
2012-10-06 21:29:37 +02:00
|
|
|
}
|
2012-10-02 01:35:43 +02:00
|
|
|
|
2012-10-08 13:37:24 +02:00
|
|
|
_it(name, func);
|
2012-10-08 00:34:29 +02:00
|
|
|
}
|
|
|
|
})()
|