~davewalker/etherpad/ubuntu-unlimited-max-users-and-revisions

« back to all changes in this revision

Viewing changes to etherpad/src/plugins/debugJsBacktrace/static/js/javascript-stacktrace/test-stacktrace.js

  • Committer: James Page
  • Date: 2011-04-13 08:00:43 UTC
  • Revision ID: james.page@canonical.com-20110413080043-eee2nq7y1v7cv2mp
* Refactoring to use native Ubuntu Java libraries. 
* debian/control:
  - use openjdk instead of sun's java
  - update maintainer
* debian/etherpad.init.orig, debian/etherpad.upstart:
  - move the init script out of the way
  - create a basic upstart script
  - note that the open office document conversion daemon was dropped
    from the upstart configuration; if this behavior is desired, please
    create a separate upstart job for it
* debian/rules:
  - just use basic dh_installinit, as it will pick up the new upstart job
* New release
* Changed maintainer to Packaging
* Fixed installation scripts
* Initial Release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
//     Copyright (C) 2008 Loic Dachary <loic@dachary.org>
 
3
//     Copyright (C) 2008 Johan Euphrosine <proppy@aminche.com>
 
4
//     Copyright (C) 2010 Eric Wendelin <emwendelin@gmail.com>
 
5
//
 
6
//     This program is free software: you can redistribute it and/or modify
 
7
//     it under the terms of the GNU General Public License as published by
 
8
//     the Free Software Foundation, either version 3 of the License, or
 
9
//     (at your option) any later version.
 
10
//
 
11
//     This program is distributed in the hope that it will be useful,
 
12
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
//     GNU General Public License for more details.
 
15
//
 
16
//     You should have received a copy of the GNU General Public License
 
17
//     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
//
 
19
 
 
20
module("invocation");
 
21
 
 
22
test("printStackTrace", function() {
 
23
    expect(1);
 
24
    var r = printStackTrace();
 
25
    equals(r.constructor, Array, 'printStackTrace returns an array');
 
26
});
 
27
 
 
28
test("printStackTrace options", function() {
 
29
    expect(1);
 
30
    var guessFunctions = printStackTrace.implementation.prototype.guessFunctions;
 
31
    printStackTrace.implementation.prototype.guessFunctions = function() {
 
32
        printStackTrace.implementation.prototype.guessFunctions = guessFunctions;
 
33
        ok(true, 'guessFunctions called');
 
34
    };
 
35
    var r = printStackTrace({guess: true});
 
36
});
 
37
 
 
38
module("mode");
 
39
 
 
40
test("mode", function() {
 
41
    expect(1);
 
42
    equals("chrome firefox other opera opera10".indexOf(printStackTrace.implementation.prototype.mode()) >= 0,true);
 
43
});
 
44
 
 
45
test("run mode", function() {
 
46
    expect(1);
 
47
    var p = new printStackTrace.implementation();
 
48
    p.other = p.firefox = p.chrome = p.opera = p.opera10 = function() { equals(1,1,'called'); };
 
49
    p.run();
 
50
});
 
51
 
 
52
test("run firefox", function() {
 
53
    expect(1);
 
54
    var p = new printStackTrace.implementation();
 
55
    p._mode = 'firefox';
 
56
    p.other = p.opera = function() { equals(1,0,'must not be called'); };
 
57
    p.firefox = function() { equals(1,1,'called'); };
 
58
    p.run();
 
59
});
 
60
 
 
61
test("run opera", function() {
 
62
    expect(1);
 
63
    var p = new printStackTrace.implementation();
 
64
    p._mode = 'opera';
 
65
    p.opera10 = p.other = p.firefox = p.chrome = function() { equals(1,0,'must not be called'); };
 
66
    p.opera = function() { equals(1,1,'called'); };
 
67
    p.run();
 
68
});
 
69
 
 
70
test("run opera10", function() {
 
71
    expect(1);
 
72
    var p = new printStackTrace.implementation();
 
73
    p._mode = 'opera10';
 
74
    p.opera = p.other = p.firefox = p.chrome = function() { equals(1,0,'must not be called'); };
 
75
    p.opera10 = function() { equals(1,1,'called'); };
 
76
    p.run();
 
77
});
 
78
 
 
79
test("run other", function() {
 
80
    expect(1);
 
81
    var p = new printStackTrace.implementation();
 
82
    p._mode = 'other';
 
83
    p.opera = p.firefox = function() { equals(1,0,'must not be called'); };
 
84
    p.other = function() { equals(1,1,'called'); };
 
85
    p.run();
 
86
});
 
87
 
 
88
test("firefox", function() {
 
89
    var mode = printStackTrace.implementation.prototype.mode();
 
90
    var e = [];
 
91
    e.push({ stack: 'discarded()...\nf1(1,"abc")@file.js:40\n()@file.js:41\n@:0  \nf44()@file.js:494'});
 
92
    if(mode == 'firefox') {
 
93
        function discarded() {
 
94
            try {(0)();} catch (exception) {
 
95
                e.push(exception);
 
96
            }
 
97
        }
 
98
        function f1(arg1, arg2) {
 
99
            discarded();
 
100
        }
 
101
        var f2 = function() {
 
102
            f1(1, "abc");
 
103
        };
 
104
        f2();
 
105
    }
 
106
    expect(4 * e.length);
 
107
    for(var i = 0; i < e.length; i++) {
 
108
        var stack = printStackTrace.implementation.prototype.firefox(e[i]);
 
109
        var stack_string = stack.join("\n");
 
110
        //equals(message_string, '', 'debug');
 
111
        equals(stack_string.indexOf('discarded'), -1, 'discarded');
 
112
        equals(stack[0].indexOf('f1(1,"abc")') >= 0, true, 'f1');
 
113
        equals(stack[1].indexOf('{anonymous}()@') >= 0, true, 'f2 anonymous');
 
114
        equals(stack[2].indexOf('@:0'), -1, '@:0 discarded');
 
115
    }
 
116
});
 
117
 
 
118
test("chrome", function() {
 
119
    var mode = printStackTrace.implementation.prototype.mode();
 
120
    var e = [];
 
121
    e.push({ stack: 'ignored\nignored\n at discarded (file.js:132:3)\n at file.js:135:3\n at f1 (file.js:132:13)\n at file.js:135:23\n at Object.<anonymous> (file.js:137:9)\n at file.js:137:32 at process (file.js:594:22)'});
 
122
    if(mode == 'chrome') {
 
123
        function discarded() {
 
124
            try {(0)();} catch (exception) {
 
125
                e.push(exception);
 
126
            }
 
127
        }
 
128
        function f1(arg1, arg2) {
 
129
            discarded();
 
130
        }
 
131
        var f2 = function() {
 
132
            f1(1, "abc");
 
133
        };
 
134
        f2();
 
135
    }
 
136
    expect(4 * e.length);
 
137
    for(var i = 0; i < e.length; i++) {
 
138
        var message = printStackTrace.implementation.prototype.chrome(e[i]);
 
139
        var message_string = message.join("\n");
 
140
        //equals(message_string, '', 'debug');
 
141
        equals(message_string.indexOf('discarded'), -1, 'discarded');
 
142
        equals(message[0].indexOf('f1') >= 0, true, 'f1');
 
143
        equals(message[1].indexOf('anonymous') >= 0, true, 'f2 anonymous');
 
144
        equals(message[2].indexOf('unknown source'), -1, 'unknown source discarded');
 
145
    }
 
146
});
 
147
 
 
148
test("opera10", function() {
 
149
        var mode = printStackTrace.implementation.prototype.mode();
 
150
        var e = [];
 
151
        e.push({ stack: 'ignored\nf1([arguments not available])@http://site.com/main.js:2\n<anonymous function: f2>([arguments not available])@http://site.com/main.js:4\n@',
 
152
                stacktrace: 'ignored\nError thrown at line 1, column 55 in discarded():\n    (0)();\ncalled from line 1, column 333 in f1(arg1, arg2):\n   discarded();\ncalled from line 1, column 470 in <anonymous function>():\n   f1(1, "abc");\ncalled from line 1, column 278 in program code:\n   f2();' });
 
153
        if (mode == 'opera10') {
 
154
        function discarded() {
 
155
            try {(0)();} catch (exception) {
 
156
                e.push(exception);
 
157
            }
 
158
        }
 
159
        function f1(arg1, arg2) {
 
160
                        var blah = arg1;
 
161
            discarded();
 
162
        }
 
163
        var f2 = function() {
 
164
            f1(1, "abc");
 
165
        };
 
166
        f2();
 
167
        }
 
168
    expect(3 * e.length);
 
169
    for(var i = 0; i < e.length; i++) {
 
170
        var stack = printStackTrace.implementation.prototype.opera10(e[i]);
 
171
                var stack_string = stack.join('\n');
 
172
                // equals(stack[1], '', 'debug');
 
173
        equals(stack_string.indexOf('ignored'), -1, 'ignored');
 
174
        equals(stack[0].indexOf('f1(') >= 0, true, 'f1 function name');
 
175
        equals(stack[1].indexOf('{anonymous}()') >= 0, true, 'f2 is anonymous');
 
176
                //FIXME: Clean up stack[2], opera has some internal stack weirdness
 
177
    }
 
178
});
 
179
 
 
180
test("opera", function() {
 
181
    var mode = printStackTrace.implementation.prototype.mode();
 
182
    var e = [];
 
183
    e.push({ message: 'ignored\nignored\nignored\nignored\nLine 40 of linked script http://site.com: in function f1\n      discarded()\nLine 44 of linked script http://site.com\n     f1(1, "abc")\nignored\nignored'});
 
184
    if(mode == 'opera') {
 
185
        function discarded() {
 
186
            try {(0)();} catch (exception) {
 
187
                e.push(exception);
 
188
            }
 
189
        }
 
190
        function f1(arg1, arg2) {
 
191
            discarded();
 
192
        }
 
193
        var f2 = function() {
 
194
            f1(1, "abc");
 
195
        };
 
196
        f2();
 
197
    }
 
198
    expect(5 * e.length);
 
199
    for(var i = 0; i < e.length; i++) {
 
200
        var message = printStackTrace.implementation.prototype.opera(e[i]);
 
201
        var message_string = message.join("\n");
 
202
        equals(message_string.indexOf('ignored'), -1, 'ignored');
 
203
        equals(message[0].indexOf('f1()') >= 0, true, 'f1 function name');
 
204
        equals(message[0].indexOf('discarded()') >= 0, true, 'f1 statement');
 
205
        equals(message[1].indexOf('{anonymous}()@') >= 0, true, 'f2 is anonymous');
 
206
        equals(message[1].indexOf('f1(1, "abc")') >= 0, true, 'f2 statement');
 
207
    }
 
208
});
 
209
 
 
210
test("other", function() {
 
211
    var mode = printStackTrace.implementation.prototype.mode();
 
212
    var frame = function(args, fun, caller) {
 
213
        this['arguments'] = args;
 
214
        this.caller = caller;
 
215
        this.fun = fun;
 
216
    };
 
217
    frame.prototype.toString = function() { return 'function '+this.fun+'() {}'; };
 
218
    function f10() {}
 
219
    var frame_f2 = new frame([], '', undefined);
 
220
    var frame_f1 = new frame([1, 'abc', f10, {1: {2: {3: 4} } }], 'FUNCTION f1  (a,b,c)', frame_f2);
 
221
    expect(mode == 'other' ? 4 : 2);
 
222
    var message = printStackTrace.implementation.prototype.other(frame_f1);
 
223
    var message_string = message.join("\n");
 
224
    equals(message[0].indexOf('f1(1,"abc",#function,#object)') >= 0, true, 'f1');
 
225
    equals(message[1].indexOf('{anonymous}()') >= 0, true, 'f2 anonymous');
 
226
    if(mode == 'other') {
 
227
        function f1(arg1, arg2) {
 
228
            var message = printStackTrace.implementation.prototype.other(arguments.callee);
 
229
            var message_string = message.join("\n");
 
230
            //equals(message_string, '', 'debug');
 
231
            equals(message[0].indexOf('f1(1,"abc",#function,#object)') >= 0, true, 'f1');
 
232
            equals(message[1].indexOf('{anonymous}()') >= 0, true, 'f2 anonymous');
 
233
        }
 
234
        var f2 = function() {
 
235
            f1(1, 'abc', f10, {1: {2: {3: 4} } });
 
236
        };
 
237
        f2();
 
238
    }
 
239
});
 
240
 
 
241
module("util");
 
242
 
 
243
test("recursion other", function() {
 
244
    var mode = printStackTrace.implementation.prototype.mode();
 
245
    expect(mode == 'other' ? 2 : 0);
 
246
    if (mode == 'other') {
 
247
            function recurse(b) {
 
248
                    if (!b) {
 
249
                    var message = printStackTrace.implementation.prototype.other(arguments.callee);
 
250
                    var message_string = message.join("\n");
 
251
                    //equals(message_string, '', 'debug');
 
252
                            equals(message[0].indexOf('recurse(false)') >= 0, true, 'first recurse false');
 
253
                            equals(message[1].indexOf('recurse(true)') >= 0, true, 'second recurse true');
 
254
                    } else {
 
255
                            recurse(true);
 
256
                    }
 
257
            }
 
258
            recurse(false);
 
259
    }
 
260
});
 
261
 
 
262
test("stringify", function() {
 
263
    expect(5);
 
264
    equals(printStackTrace.implementation.prototype.stringifyArguments(["a", 1, {}, function() {}, undefined]), '"a",1,#object,#function,undefined');
 
265
    equals(printStackTrace.implementation.prototype.stringifyArguments([0, 1, 2, 3]), '0,1,2,3');
 
266
    equals(printStackTrace.implementation.prototype.stringifyArguments([['a', null]]), '["a",null]');
 
267
    equals(printStackTrace.implementation.prototype.stringifyArguments([[2, 4, 6, 8, 10, 12, 14]]), '[2...14]');
 
268
    equals(printStackTrace.implementation.prototype.stringifyArguments([]), '');
 
269
});
 
270
 
 
271
test("isSameDomain", function() {
 
272
        expect(1);
 
273
        ok(printStackTrace.implementation.prototype.isSameDomain(location.href));
 
274
        
 
275
});
 
276
 
 
277
test("guessFunctionNameFromLines", function() {
 
278
    expect(3);
 
279
    equals(printStackTrace.implementation.prototype.guessFunctionNameFromLines(2, ['var a = function() {', 'var b = 2;', '};']), 'a');
 
280
    equals(printStackTrace.implementation.prototype.guessFunctionNameFromLines(2, ['function a() {', 'var b = 2;', '};']), 'a');
 
281
    equals(printStackTrace.implementation.prototype.guessFunctionNameFromLines(2, ['var a = 1;', 'var b = 2;', 'var c = 3;']), '(?)');
 
282
});
 
283
 
 
284
test("getSource cache miss", function() {
 
285
    expect(3);
 
286
    var p = new printStackTrace.implementation();
 
287
    var file = 'file:///test';
 
288
    p.ajax = function(fileArg, callback) {
 
289
        equals(fileArg, file, 'cache miss');
 
290
        return 'line0\nline1\n';
 
291
    };
 
292
    var lines = p.getSource(file);
 
293
    equals(lines[0], 'line0');
 
294
    equals(lines[1], 'line1');
 
295
});
 
296
 
 
297
test("getSource cache hit", function() {
 
298
    expect(2);
 
299
    var p = new printStackTrace.implementation();
 
300
    var file = 'file:///test';
 
301
    p.ajax = function(fileArg, callback) {
 
302
        ok(false, 'not called');
 
303
    };
 
304
    p.sourceCache[file] = ['line0', 'line1'];
 
305
    var lines = p.getSource(file);
 
306
    equals(lines[0], 'line0');
 
307
    equals(lines[1], 'line1');
 
308
});
 
309
 
 
310
test("sync ajax", function() {
 
311
    expect(1);
 
312
    var p = new printStackTrace.implementation();
 
313
    var data = p.ajax(document.location.href);
 
314
    ok(data.indexOf('stacktrace') >= 0, 'synchronous get');
 
315
});
 
316
 
 
317
test("guessFunctionName", function() {
 
318
    expect(1);
 
319
    var p = new printStackTrace.implementation();
 
320
    var file = 'http://' + window.location.hostname + '/file.js';
 
321
    p.sourceCache[file] = ['var a = function() {', 'var b = 2;', '};'];
 
322
    equals(p.guessFunctionName(file, 2), 'a');
 
323
});
 
324
 
 
325
test("guessFunctionName exception", function() {
 
326
    expect(1);
 
327
    var p = new printStackTrace.implementation();
 
328
    p.getSource = function() {
 
329
        throw 'permission denied';
 
330
    };
 
331
    var file = 'file:///test';
 
332
    equals(p.guessFunctionName(file, 2), 'getSource failed with url: file:///test, exception: permission denied');
 
333
});
 
334
 
 
335
test("guessFunctions firefox", function() {
 
336
    var results = [];
 
337
    var mode = printStackTrace.implementation.prototype.mode();
 
338
    var p = new printStackTrace.implementation();
 
339
    p._mode = 'firefox';
 
340
    var file = 'http://' + window.location.hostname + '/file.js';
 
341
    p.sourceCache[file] = ['var f2 = function() {', 'var b = 2;', '};'];
 
342
    results.push(['run() ('+file+':1)', 'f2()@'+file+':1']);
 
343
        
 
344
    if (mode == 'firefox') {
 
345
        var f2 = function() {
 
346
            try {
 
347
                (0)();
 
348
            } catch(e) {
 
349
                results.push(p.run());
 
350
            }
 
351
        };
 
352
        f2();
 
353
    }
 
354
    
 
355
    expect(results.length * 1);
 
356
    for (var i = 0; i < results.length; ++i) {
 
357
        //equals(p.guessFunctions(results[i]), '', 'debug');
 
358
        equals(p.guessFunctions(results[i])[1].indexOf('f2'), 0, 'f2');
 
359
    }
 
360
});
 
361
 
 
362
test("guessFunctions chrome", function() {
 
363
    var results = [];
 
364
    var mode = printStackTrace.implementation.prototype.mode();
 
365
    var p = new printStackTrace.implementation();
 
366
    p._mode = 'chrome';
 
367
    var file = 'http://' + window.location.hostname + '/file.js';
 
368
    p.sourceCache[file] = ['var f2 = function() {', 'var b = 2;', '};'];
 
369
    results.push(['run() ('+file+':1:1)', 'f2() ('+file+':1:1)']);
 
370
        
 
371
    if (mode == 'chrome') {
 
372
        var f2 = function() {
 
373
            try {
 
374
                (0)();
 
375
            } catch(e) {
 
376
                results.push(p.run());
 
377
            }
 
378
        };
 
379
        f2();
 
380
    }
 
381
    
 
382
    expect(results.length);
 
383
    for (var i = 0; i < results.length; ++i) {
 
384
        //equals((results[i]), '', 'debug');
 
385
        equals(p.guessFunctions(results[i])[1].indexOf('f2()'), 0, 'f2');
 
386
    }
 
387
});
 
388
 
 
389
test("guessFunctions opera", function() {
 
390
        var results = [];
 
391
    var mode = printStackTrace.implementation.prototype.mode();
 
392
        var p = new printStackTrace.implementation();
 
393
        p._mode = 'opera';
 
394
        var file = 'http://' + window.location.hostname + '/file.js';
 
395
        p.sourceCache[file] = ['var f2 = function() {', 'var b = 2;', '};'];
 
396
        results.push(['f2()@'+file+':2 -- code']);
 
397
        
 
398
        if (mode == 'opera') {
 
399
            var f2 = function() {
 
400
                try {
 
401
                    (0)();
 
402
                } catch(e) {
 
403
                    results.push(p.run());
 
404
                }
 
405
            };
 
406
            f2();
 
407
        }
 
408
        
 
409
        expect(results.length * 1);
 
410
        for (var i = 0; i < results.length; ++i) {
 
411
                // equals(p.guessFunctions(results[i]), '', 'debug');
 
412
            equals(p.guessFunctions(results[i])[0].indexOf('f2'), 0, 'f2');
 
413
        }
 
414
});
 
415
 
 
416
test("guessFunctions other", function() {
 
417
    var results = [];
 
418
    var mode = printStackTrace.implementation.prototype.mode();
 
419
    var p = new printStackTrace.implementation();
 
420
    p._mode = 'other';
 
421
    var file = 'http://' + window.location.hostname + '/file.js';
 
422
    p.sourceCache[file] = ['var f2 = function() {', 'var b = 2;', '};'];
 
423
    results.push(['{anonymous}()']);
 
424
       
 
425
    if (mode == 'other') {
 
426
        var f2 = function() {
 
427
            try { (0)(); } catch(e) {
 
428
                results.push(p.run());
 
429
            }
 
430
        };
 
431
        f2();
 
432
    }
 
433
    
 
434
    expect(1 * results.length);
 
435
    for (var i = 0; i < results.length; ++i) {
 
436
        //equals((results[i]), '', 'debug');
 
437
        equals(p.guessFunctions(results[i])[0].indexOf('{anonymous}'), 0, 'no file and line number on other');
 
438
    }
 
439
});