/
AnimationFrameScheduler-spec.ts
241 lines (212 loc) · 7.79 KB
/
AnimationFrameScheduler-spec.ts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { expect } from 'chai';
import * as sinon from 'sinon';
import { animationFrameScheduler, Subscription, merge } from 'rxjs';
import { delay } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { observableMatcher } from '../helpers/observableMatcher';
import { animationFrameProvider } from 'rxjs/internal/scheduler/animationFrameProvider';
import { intervalProvider } from 'rxjs/internal/scheduler/intervalProvider';
const animationFrame = animationFrameScheduler;
/** @test {Scheduler} */
describe('Scheduler.animationFrame', () => {
let testScheduler: TestScheduler;
beforeEach(() => {
testScheduler = new TestScheduler(observableMatcher);
});
it('should exist', () => {
expect(animationFrame).exist;
});
it('should act like the async scheduler if delay > 0', () => {
testScheduler.run(({ animate, cold, expectObservable, time }) => {
animate(' ----------x--');
const a = cold(' a ');
const ta = time(' ----| ');
const b = cold(' b ');
const tb = time(' --------| ');
const expected = '----a---b----';
const result = merge(
a.pipe(delay(ta, animationFrame)),
b.pipe(delay(tb, animationFrame))
);
expectObservable(result).toBe(expected);
});
});
it('should cancel animationFrame actions when delay > 0', () => {
testScheduler.run(({ animate, cold, expectObservable, flush, time }) => {
const requestSpy = sinon.spy(animationFrameProvider, 'requestAnimationFrame');
const setSpy = sinon.spy(intervalProvider, 'setInterval');
const clearSpy = sinon.spy(intervalProvider, 'clearInterval');
animate(' ----------x--');
const a = cold(' a ');
const ta = time(' ----| ');
const subs = ' ^-! ';
const expected = '-------------';
const result = merge(
a.pipe(delay(ta, animationFrame))
);
expectObservable(result, subs).toBe(expected);
flush();
expect(requestSpy).to.have.not.been.called;
expect(setSpy).to.have.been.calledOnce;
expect(clearSpy).to.have.been.calledOnce;
requestSpy.restore();
setSpy.restore();
clearSpy.restore();
});
});
it('should schedule an action to happen later', (done) => {
let actionHappened = false;
animationFrame.schedule(() => {
actionHappened = true;
done();
});
if (actionHappened) {
done(new Error('Scheduled action happened synchronously'));
}
});
it('should execute recursively scheduled actions in separate asynchronous contexts', (done) => {
let syncExec1 = true;
let syncExec2 = true;
animationFrame.schedule(function (index) {
if (index === 0) {
this.schedule(1);
animationFrame.schedule(() => { syncExec1 = false; });
} else if (index === 1) {
this.schedule(2);
animationFrame.schedule(() => { syncExec2 = false; });
} else if (index === 2) {
this.schedule(3);
} else if (index === 3) {
if (!syncExec1 && !syncExec2) {
done();
} else {
done(new Error('Execution happened synchronously.'));
}
}
}, 0, 0);
});
it('should cancel the animation frame if all scheduled actions unsubscribe before it executes', (done) => {
let animationFrameExec1 = false;
let animationFrameExec2 = false;
const action1 = animationFrame.schedule(() => { animationFrameExec1 = true; });
const action2 = animationFrame.schedule(() => { animationFrameExec2 = true; });
expect(animationFrame._scheduled).to.exist;
expect(animationFrame.actions.length).to.equal(2);
action1.unsubscribe();
action2.unsubscribe();
expect(animationFrame.actions.length).to.equal(0);
expect(animationFrame._scheduled).to.equal(undefined);
animationFrame.schedule(() => {
expect(animationFrameExec1).to.equal(false);
expect(animationFrameExec2).to.equal(false);
done();
});
});
it('should execute the rest of the scheduled actions if the first action is canceled', (done) => {
let actionHappened = false;
let secondSubscription: Subscription | null = null;
const firstSubscription = animationFrame.schedule(() => {
actionHappened = true;
if (secondSubscription) {
secondSubscription.unsubscribe();
}
done(new Error('The first action should not have executed.'));
});
secondSubscription = animationFrame.schedule(() => {
if (!actionHappened) {
done();
}
});
if (actionHappened) {
done(new Error('Scheduled action happened synchronously'));
} else {
firstSubscription.unsubscribe();
}
});
it('should not execute rescheduled actions when flushing', (done) => {
let flushCount = 0;
let scheduledIndices: number[] = [];
let originalFlush = animationFrame.flush;
animationFrame.flush = (...args) => {
++flushCount;
originalFlush.apply(animationFrame, args);
if (flushCount === 2) {
animationFrame.flush = originalFlush;
try {
expect(scheduledIndices).to.deep.equal([0, 1]);
done();
} catch (error) {
done(error);
}
}
};
animationFrame.schedule(function (index) {
if (flushCount < 2) {
this.schedule(index! + 1);
scheduledIndices.push(index! + 1);
}
}, 0, 0);
scheduledIndices.push(0);
});
it('should execute actions scheduled when flushing in a subsequent flush', (done) => {
const sandbox = sinon.createSandbox();
const stubFlush = (sandbox.stub(animationFrameScheduler, 'flush')).callThrough();
let a: Subscription;
let b: Subscription;
let c: Subscription;
a = animationFrameScheduler.schedule(() => {
expect(stubFlush).to.have.callCount(1);
c = animationFrameScheduler.schedule(() => {
expect(stubFlush).to.have.callCount(2);
sandbox.restore();
done();
});
});
b = animationFrameScheduler.schedule(() => {
expect(stubFlush).to.have.callCount(1);
});
});
it('should execute actions scheduled when flushing in a subsequent flush when some actions are unsubscribed', (done) => {
const sandbox = sinon.createSandbox();
const stubFlush = (sandbox.stub(animationFrameScheduler, 'flush')).callThrough();
let a: Subscription;
let b: Subscription;
let c: Subscription;
a = animationFrameScheduler.schedule(() => {
expect(stubFlush).to.have.callCount(1);
c = animationFrameScheduler.schedule(() => {
expect(stubFlush).to.have.callCount(2);
sandbox.restore();
done();
});
b.unsubscribe();
});
b = animationFrameScheduler.schedule(() => {
done(new Error('Unexpected execution of b'));
});
});
it('should properly cancel an unnecessary flush', (done) => {
const sandbox = sinon.createSandbox();
const cancelAnimationFrameStub = sandbox.stub(animationFrameProvider, 'cancelAnimationFrame').callThrough();
let a: Subscription;
let b: Subscription;
let c: Subscription;
a = animationFrameScheduler.schedule(() => {
expect(animationFrameScheduler.actions).to.have.length(1);
c = animationFrameScheduler.schedule(() => {
done(new Error('Unexpected execution of c'));
});
expect(animationFrameScheduler.actions).to.have.length(2);
// What we're testing here is that the unsubscription of action c effects
// the cancellation of the animation frame in a scenario in which the
// actions queue is not empty - it contains action b.
c.unsubscribe();
expect(animationFrameScheduler.actions).to.have.length(1);
expect(cancelAnimationFrameStub).to.have.callCount(1);
});
b = animationFrameScheduler.schedule(() => {
sandbox.restore();
done();
});
});
});