pad.pub0.org/tests/frontend/helper.js

140 lines
3.4 KiB
JavaScript
Raw Normal View History

2012-10-06 19:29:37 +00:00
var helper = {};
(function(){
2012-10-06 19:29:37 +00:00
var $iframeContainer, $iframe, $padChrome, $padOuter, $padInner;
2012-10-06 19:29:37 +00:00
helper.init = function(){
$iframeContainer = $("#iframe-container");
}
2012-10-06 19:29:37 +00:00
helper.randomString = function randomString(len)
{
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-06 19:29:37 +00:00
var getFrameJQuery = function($, selector, callback){
//find the iframe and get its window and document
var $iframe = $(selector);
var $content = $iframe.contents();
var win = $iframe[0].contentWindow;
var doc = win.document;
//inject jquery if not already existing
if(win.$ === undefined){
helper.injectJS(doc, "/static/js/jquery.js");
}
helper.waitFor(function(){
return win.$
}).then(function(){
if(!(win.$ && win.$.fn && win.$.fn.sendkeys)){
helper.injectJS(doc, "/tests/frontend/sendkeys.js");
}
helper.waitFor(function(){
return (win.$ && win.$.fn && win.$.fn.sendkeys);
}).then(function(){
win.$.window = win;
win.$.document = doc;
callback(win.$);
});
});
}
helper.newPad = function(cb){
var padName = "FRONTEND_TEST_" + helper.randomString(20);
$iframe = $("<iframe src='/p/" + padName + "'></iframe>");
$iframeContainer.empty().append($iframe);
var checkInterval;
$iframe.load(function(){
2012-10-06 19:29:37 +00:00
helper.waitFor(function(){
return !$iframe.contents().find("#editorloadingbox").is(":visible");
}).then(function(){
//INCEPTION!!!
getFrameJQuery($, '#iframe-container iframe', function(_$padChrome){
$padChrome = _$padChrome;
getFrameJQuery($padChrome, 'iframe.[name="ace_outer"]', function(_$padOuter){
$padOuter = _$padOuter;
getFrameJQuery($padOuter, 'iframe.[name="ace_inner"]', function(_$padInner){
$padInner = _$padInner;
cb();
});
});
})
});
});
return padName;
}
2012-10-06 19:29:37 +00:00
//helper to inject javascript
helper.injectJS = function(doc, url){
var script = doc.createElement( 'script' );
script.type = 'text/javascript';
script.src = url;
doc.body.appendChild(script);
}
2012-10-06 19:29:37 +00:00
helper.waitFor = function(conditionFunc, _timeoutTime, _intervalTime){
var timeoutTime = _timeoutTime || 1000;
var intervalTime = _intervalTime || 10;
var callback = function(){}
var returnObj = { then: function(_callback){
callback = _callback;
}}
2012-10-06 19:29:37 +00:00
var intervalCheck = setInterval(function(){
var passed = conditionFunc();
2012-10-06 19:29:37 +00:00
if(passed){
clearInterval(intervalCheck);
clearTimeout(timeout);
callback(passed);
}
}, intervalTime);
var timeout = setTimeout(function(){
clearInterval(intervalCheck);
throw Error("wait for condition never became true");
}, timeoutTime);
2012-10-06 19:29:37 +00:00
return returnObj;
}
2012-10-06 19:29:37 +00:00
helper.log = function(){
if(console && console.log){
console.log.apply(console, arguments);
}
}
2012-10-06 19:29:37 +00:00
helper.jQueryOf = function(name){
switch(name){
case "chrome":
return $padChrome;
break;
case "outer":
return $padOuter;
break;
case "inner":
return $padInner;
break;
}
}
})()