summaryrefslogtreecommitdiff
path: root/tests/frontend/helper.js
blob: 7131119ae30ced91af34e208c6dd2f17665be1d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var helper = {};

(function(){
  var $iframeContainer, $iframe, jsLibraries = {};

  helper.init = function(cb){
    $iframeContainer = $("#iframe-container");

    $.get('/static/js/jquery.js').done(function(code){ 
      // make sure we don't override existing jquery
      jsLibraries["jquery"] = "if(typeof $ === 'undefined') {\n" + code + "\n}";

      $.get('/tests/frontend/lib/sendkeys.js').done(function(code){ 
        jsLibraries["sendkeys"] = code;

        cb();
      });
    });
  }

  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;
  }

  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
    */

    var win = $iframe[0].contentWindow;
    var doc = win.document;

    //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");

    win.eval(jsLibraries["jquery"]);
    win.eval(jsLibraries["sendkeys"]);
    
    win.$.window = win;
    win.$.document = doc;

    return win.$;
  }

  helper.clearCookies = function(){
    window.document.cookie = "";
  }

  helper.newPad = function(cb, padName){
    //build opts object
    var opts = {clearCookies: true}
    if(typeof cb === 'function'){
      opts.cb = cb
    } else {
      opts = _.defaults(cb, opts);
    }

    //clear cookies
    if(opts.clearCookies){
      helper.clearCookies();
    }

    if(!padName)
      padName = "FRONTEND_TEST_" + helper.randomString(20);
    $iframe = $("<iframe src='/p/" + padName + "'></iframe>");
    
    //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");
        }, 50000).done(function(){
          helper.padChrome$ = getFrameJQuery(                $('#iframe-container iframe'));
          helper.padOuter$  = getFrameJQuery(helper.padChrome$('iframe[name="ace_outer"]'));
          helper.padInner$  = getFrameJQuery( helper.padOuter$('iframe[name="ace_inner"]'));

          //disable all animations, this makes tests faster and easier
          helper.padChrome$.fx.off = true;
          helper.padOuter$.fx.off = true;
          helper.padInner$.fx.off = true;
          
          opts.cb();
        }).fail(function(){
          throw new Error("Pad never loaded");
        });
      });
    }); 

    return padName;
  }

  helper.waitFor = function(conditionFunc, _timeoutTime, _intervalTime){
    var timeoutTime = _timeoutTime || 1000;
    var intervalTime = _intervalTime || 10;

    var deferred = $.Deferred();
    
    var _fail = deferred.fail;
    var listenForFail = false;
    deferred.fail = function(){
      listenForFail = true;
      _fail.apply(this, arguments);
    }

    var intervalCheck = setInterval(function(){
      var passed = false;

      passed = conditionFunc();

      if(passed){
        clearInterval(intervalCheck);
        clearTimeout(timeout);

        deferred.resolve();
      }
    }, intervalTime);

    var timeout = setTimeout(function(){
      clearInterval(intervalCheck);
      var error = new Error("wait for condition never became true " + conditionFunc.toString());
      deferred.reject(error);

      if(!listenForFail){
        throw error;
      }
    }, timeoutTime);

    return deferred;
  }

  /* 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(){}

  //force usage of callbacks in it
  var _it = it;
  it = function(name, func){
    if(func && func.length !== 1){
      func = function(){
        throw new Error("Please use always a callback with it() - " + func.toString());
      }
    }

    _it(name, func);
  }
})()