55ba80d92b75ddb247938b39f900174ae66251b4
[ccsdk/features.git] /
1 // find *Spec.js files in the src directory next to the corresponding source file
2
3 var test = window.test = {};
4
5 test.cb = (function( jasmine ) {
6         var callbacks = [];
7
8         return {
9                 use: function() {
10                         callbacks = [];
11                 },
12                 createSpy: function( name, arg, data, context ) {
13                         return jasmine.createSpy( name ).and.callFake( function() {
14                                 callbacks.push( { cb: arguments[ arg || 0 ], data: data, context: context } );
15                         });
16                 },
17                 execOne: function() {
18                         var exec = callbacks.shift();
19                         exec.cb.apply( exec.context, exec.data );
20                 },
21                 execAll: function() {
22                         while( callbacks.length ) {
23                                 this.execOne();
24                         }
25                 }
26         };
27 })( this.jasmine );
28
29
30 test.clock = ( function() {
31         var id = 0, timers, saved;
32         var names = [ "setTimeout", "setInterval", "clearTimeout", "clearInterval" ];
33         var byNext = function( a, b ) { return a.next - b.next; };
34         var mocks = {
35                 setTimeout: function( fn, t ) {
36                         timers.push( { id: id, fn: fn, next: t, t: t, type: "t" } );
37                         return id++;
38                 },
39                 clearTimeout: function( id ) {
40                         timers = timers.filter( function( timer ) { return timer.id !== id; } );
41                 },
42                 setInterval: function( fn, t ) {
43                         timers.push( { id: id, fn: fn, next: t, t: t, type: "i" } );
44                         return id++;
45                 },
46                 clearInterval: function( id ) {
47                         timers = timers.filter( function( timer ) { return timer.id !== id; } );
48                 }
49         };
50
51         return {
52                 steal: function() {
53                         timers = [];
54                         saved = {};
55                         names.forEach( function( n ) {
56                                 saved[n] = window[n];
57                                 window[n] = mocks[n];
58                         });
59                 },
60                 restore: function() {
61                         names.forEach( function( n ) {
62                                 window[n] = saved[n];
63                         });
64                         timers = null;
65                         saved = null;
66                 },
67                 tick: function() {
68                         if( timers.length ) {
69                                 timers.sort( byNext );
70                                 var t0 = timers[0];
71                                 if( t0.type === "t" ) {
72                                         timers.shift();
73                                 } else {
74                                         t0.next += t0.t;
75                                 }
76                                 t0.fn();
77                         }
78                 }
79         };
80
81 })();