d6f2083a41d6d46f1a501f172c29968d5f94ac1e
[ccsdk/features.git] /
1 /**
2  * Copyright 2010-2013 Ben Birch
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this software except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 // find *Spec.js files in the src directory next to the corresponding source file
17
18 var test = window.test = {};
19
20 test.cb = (function( jasmine ) {
21         var callbacks = [];
22
23         return {
24                 use: function() {
25                         callbacks = [];
26                 },
27                 createSpy: function( name, arg, data, context ) {
28                         return jasmine.createSpy( name ).and.callFake( function() {
29                                 callbacks.push( { cb: arguments[ arg || 0 ], data: data, context: context } );
30                         });
31                 },
32                 execOne: function() {
33                         var exec = callbacks.shift();
34                         exec.cb.apply( exec.context, exec.data );
35                 },
36                 execAll: function() {
37                         while( callbacks.length ) {
38                                 this.execOne();
39                         }
40                 }
41         };
42 })( this.jasmine );
43
44
45 test.clock = ( function() {
46         var id = 0, timers, saved;
47         var names = [ "setTimeout", "setInterval", "clearTimeout", "clearInterval" ];
48         var byNext = function( a, b ) { return a.next - b.next; };
49         var mocks = {
50                 setTimeout: function( fn, t ) {
51                         timers.push( { id: id, fn: fn, next: t, t: t, type: "t" } );
52                         return id++;
53                 },
54                 clearTimeout: function( id ) {
55                         timers = timers.filter( function( timer ) { return timer.id !== id; } );
56                 },
57                 setInterval: function( fn, t ) {
58                         timers.push( { id: id, fn: fn, next: t, t: t, type: "i" } );
59                         return id++;
60                 },
61                 clearInterval: function( id ) {
62                         timers = timers.filter( function( timer ) { return timer.id !== id; } );
63                 }
64         };
65
66         return {
67                 steal: function() {
68                         timers = [];
69                         saved = {};
70                         names.forEach( function( n ) {
71                                 saved[n] = window[n];
72                                 window[n] = mocks[n];
73                         });
74                 },
75                 restore: function() {
76                         names.forEach( function( n ) {
77                                 window[n] = saved[n];
78                         });
79                         timers = null;
80                         saved = null;
81                 },
82                 tick: function() {
83                         if( timers.length ) {
84                                 timers.sort( byNext );
85                                 var t0 = timers[0];
86                                 if( t0.type === "t" ) {
87                                         timers.shift();
88                                 } else {
89                                         t0.next += t0.t;
90                                 }
91                                 t0.fn();
92                         }
93                 }
94         };
95
96 })();