Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / qunit / suites / resources / mediawiki / mediawiki.requestIdleCallback.test.js
blob3772097df40731ec1b9d4e15af180333b9b45dff
1 ( function ( mw ) {
2         QUnit.module( 'mediawiki.requestIdleCallback', QUnit.newMwEnvironment( {
3                 setup: function () {
4                         var time = mw.now(),
5                                 clock = this.clock = this.sandbox.useFakeTimers();
7                         this.tick = function ( forward ) {
8                                 time += forward;
9                                 clock.tick( forward );
10                         };
11                         this.sandbox.stub( mw, 'now', function () {
12                                 return time;
13                         } );
15                         // Don't test the native version (if available)
16                         this.mwRIC = mw.requestIdleCallback;
17                         mw.requestIdleCallback = mw.requestIdleCallbackInternal;
18                 },
19                 teardown: function () {
20                         mw.requestIdleCallback = this.mwRIC;
21                 }
22         } ) );
24         // Basic scheduling of callbacks
25         QUnit.test( 'callback', 3, function ( assert ) {
26                 var sequence,
27                         tick = this.tick;
29                 mw.requestIdleCallback( function () {
30                         sequence.push( 'x' );
31                         tick( 30 );
32                 } );
33                 mw.requestIdleCallback( function () {
34                         tick( 5 );
35                         sequence.push( 'y' );
36                         tick( 30 );
37                 } );
38                 // Task Z is not run in the first sequence because the
39                 // first two tasks consumed the available 50ms budget.
40                 mw.requestIdleCallback( function () {
41                         sequence.push( 'z' );
42                         tick( 30 );
43                 } );
45                 sequence = [];
46                 tick( 1000 );
47                 assert.deepEqual( sequence, [ 'x', 'y' ] );
49                 sequence = [];
50                 tick( 1000 );
51                 assert.deepEqual( sequence, [ 'z' ] );
53                 sequence = [];
54                 tick( 1000 );
55                 assert.deepEqual( sequence, [] );
56         } );
58         // Schedule new callbacks within a callback that tick
59         // the clock. If the budget is exceeded, the newly scheduled
60         // task is delayed until the next idle period.
61         QUnit.test( 'nest-tick', 3, function ( assert ) {
62                 var sequence,
63                         tick = this.tick;
65                 mw.requestIdleCallback( function () {
66                         sequence.push( 'x' );
67                         tick( 30 );
68                 } );
69                 // Task Y is a task that schedules another task.
70                 mw.requestIdleCallback( function () {
71                         function other() {
72                                 sequence.push( 'y' );
73                                 tick( 35 );
74                         }
75                         mw.requestIdleCallback( other );
76                 } );
77                 mw.requestIdleCallback( function () {
78                         sequence.push( 'z' );
79                         tick( 30 );
80                 } );
82                 sequence = [];
83                 tick( 1000 );
84                 assert.deepEqual( sequence, [ 'x', 'z' ] );
86                 sequence = [];
87                 tick( 1000 );
88                 assert.deepEqual( sequence, [ 'y' ] );
90                 sequence = [];
91                 tick( 1000 );
92                 assert.deepEqual( sequence, [] );
93         } );
95         // Schedule new callbacks within a callback that run quickly.
96         // Note how the newly scheduled task gets to run as part of the
97         // current idle period (budget allowing).
98         QUnit.test( 'nest-quick', 2, function ( assert ) {
99                 var sequence,
100                         tick = this.tick;
102                 mw.requestIdleCallback( function () {
103                         sequence.push( 'x' );
104                         mw.requestIdleCallback( function () {
105                                 sequence.push( 'x-expand' );
106                         } );
107                 } );
108                 mw.requestIdleCallback( function () {
109                         sequence.push( 'y' );
110                 } );
112                 sequence = [];
113                 tick( 1000 );
114                 assert.deepEqual( sequence, [ 'x', 'y', 'x-expand' ] );
116                 sequence = [];
117                 tick( 1000 );
118                 assert.deepEqual( sequence, [] );
119         } );
121 }( mediaWiki ) );