-
Notifications
You must be signed in to change notification settings - Fork 71
/
playback.js
246 lines (243 loc) · 10.4 KB
/
playback.js
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
242
243
244
245
246
/**
* Those tests should pass with a browser that handles well all the
* Media Events: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events
*
* It relies *heavily* on those.
* Moreover an active internet connection is needed, and better be a fast one, since it
* preloads 40~ seconds of ogg files. (512Kb?)
*/
describe('browser tests: playback', function () {
// 2 functions, useful to debug events
function consolesomething(listener_name) {
return function (evt) {
console.log(listener_name, 'called');
};
}
function debugMedia(element) {
[
'abort',
'canplay',
'canplaythrough',
'durationchange',
'emptied',
'ended',
'error',
'loadeddata',
'loadedmetadata',
'loadstart',
'mozaudioavailable',
'pause',
'play',
'playing',
'progress',
'ratechange',
'seeked',
'seeking',
'stalled',
'suspend',
'timeupdate',
'volumechange',
'waiting'
].forEach(function (listener_name) {
element.addEventListener(listener_name, consolesomething(listener_name));
});
}
// in case of need use debugMedia(DOMElement)
beforeEach(module('mediaPlayer'));
afterEach(function () {
var audioTags = document.querySelectorAll('audio');
Array.prototype.forEach.call(audioTags, function (audioTag) {
angular.element(audioTag).remove();
});
});
it('should load a single file when calling load()', function (done) {
inject(function ($compile, $rootScope) {
var element = $compile('<audio media-player="testplayer"></audio>')($rootScope);
angular.element(document.body).append(element);
expect($rootScope.testplayer).to.be.an('object');
$rootScope.testplayer.load({ src: 'http://upload.wikimedia.org/wikipedia/commons/0/07/Silence.ogg', type: 'audio/ogg' });
$rootScope.testplayer.one('durationchange', function () {
setTimeout(function () {
expect($rootScope.testplayer.duration).to.be.above(1);
done();
}, 10);
});
});
});
it('should start playing when calling play()', function (done) {
inject(function ($compile, $rootScope) {
var element = $compile('<audio media-player="testplayer"></audio>')($rootScope);
angular.element(document.body).append(element);
expect($rootScope.testplayer).to.be.an('object');
$rootScope.testplayer.load({ src: 'http://upload.wikimedia.org/wikipedia/commons/0/07/Silence.ogg', type: 'audio/ogg' });
$rootScope.testplayer.one('loadstart', function () {
setTimeout(function () {
$rootScope.testplayer.play();
}, 10);
});
$rootScope.testplayer.one('playing', function () {
setTimeout(function () {
expect($rootScope.testplayer.duration).to.be.above(1);
expect($rootScope.testplayer.playing).to.equal(true);
done();
}, 10);
});
});
});
it('should stop playing when calling stop()', function (done) {
inject(function ($compile, $rootScope) {
var element = $compile('<audio media-player="testplayer"></audio>')($rootScope);
angular.element(document.body).append(element);
expect($rootScope.testplayer).to.be.an('object');
$rootScope.testplayer.load({ src: 'http://upload.wikimedia.org/wikipedia/commons/0/07/Silence.ogg', type: 'audio/ogg' }, true);
$rootScope.testplayer.one('playing', function () {
setTimeout(function () { $rootScope.testplayer.stop(); }, 10);
});
$rootScope.testplayer.one('abort', function () {
setTimeout(function () {
expect($rootScope.testplayer.playing).to.equal(false);
done();
}, 10);
});
});
});
it('should pause playing when calling pause()', function (done) {
inject(function ($compile, $rootScope) {
var element = $compile('<audio media-player="testplayer"></audio>')($rootScope);
angular.element(document.body).append(element);
expect($rootScope.testplayer).to.be.an('object');
$rootScope.testplayer.load({ src: 'http://upload.wikimedia.org/wikipedia/commons/0/07/Silence.ogg', type: 'audio/ogg' }, true);
$rootScope.testplayer.one('playing', function () {
setTimeout(function () { $rootScope.testplayer.pause(); }, 10);
});
$rootScope.testplayer.one('pause', function () {
setTimeout(function () {
expect($rootScope.testplayer.playing).to.equal(false);
done();
}, 10);
});
});
});
it('should start or stop playing when calling playPause()', function (done) {
inject(function ($compile, $rootScope) {
var element = $compile('<audio media-player="testplayer"></audio>')($rootScope);
angular.element(document.body).append(element);
expect($rootScope.testplayer).to.be.an('object');
$rootScope.testplayer.load({ src: 'http://upload.wikimedia.org/wikipedia/commons/0/07/Silence.ogg', type: 'audio/ogg' });
// async tests are not easy to read:
// 1) play after 10ms,
// 2) waiting for a play event
// 3) expect true, then stopping after 10ms
// 3) expect false, after 50ms
setTimeout(function () { $rootScope.testplayer.playPause(); }, 10);
$rootScope.testplayer.$domEl.addEventListener('playing', function () {
setTimeout(function () {
expect($rootScope.testplayer.playing).to.equal(true);
setTimeout(function () { $rootScope.testplayer.playPause(); }, 10);
setTimeout(function () {
expect($rootScope.testplayer.playing).to.equal(false);
done();
}, 50);
}, 10);
});
});
});
it('should preload the first AudioElement put in the playlist', function (done) {
inject(function ($compile, $rootScope) {
$rootScope.testplaylist = [
{ src: 'http://upload.wikimedia.org/wikipedia/commons/0/07/Silence.ogg', type: 'audio/ogg' },
{ src: 'http://upload.wikimedia.org/wikipedia/en/c/cb/Stairway_to_Heaven_3_sections.ogg', type: 'audio/ogg' },
{ src: 'http://upload.wikimedia.org/wikipedia/en/d/d0/Beatles_cometogether.ogg', type: 'audio/ogg' }
];
var element = $compile('<audio media-player="testplayer" playlist="testplaylist"></audio>')($rootScope);
angular.element(document.body).append(element);
expect($rootScope.testplayer).to.be.an('object');
// currentTrack should gets updated after a loadedmetadata
$rootScope.testplayer.one('loadedmetadata', function () {
setTimeout(function () {
expect($rootScope.testplayer.currentTrack).to.be.above(0);
done();
}, 10);
});
});
});
it('should support playing a specific index calling play(index)', function (done) {
inject(function ($compile, $rootScope) {
$rootScope.testplaylist = [
{ src: 'http://upload.wikimedia.org/wikipedia/commons/0/07/Silence.ogg', type: 'audio/ogg' },
{ src: 'http://upload.wikimedia.org/wikipedia/en/c/cb/Stairway_to_Heaven_3_sections.ogg', type: 'audio/ogg' },
{ src: 'http://upload.wikimedia.org/wikipedia/en/d/d0/Beatles_cometogether.ogg', type: 'audio/ogg' }
];
var element = $compile('<audio media-player="testplayer" playlist="testplaylist"></audio>')($rootScope);
angular.element(document.body).append(element);
expect($rootScope.testplayer).to.be.an('object');
$rootScope.testplayer.one('canplaythrough', function () {
setTimeout(function () {
$rootScope.testplayer.play(1);
}, 10);
});
$rootScope.testplayer.one('playing', function () {
setTimeout(function () {
expect($rootScope.testplayer.currentTrack).to.be.equal(2);
done();
}, 10);
});
});
});
it('should autoplay the next file when calling next(), during playback', function (done) {
this.timeout(5000);
inject(function ($compile, $rootScope, $timeout) {
$rootScope.testplaylist = [
{ src: 'http://upload.wikimedia.org/wikipedia/commons/0/07/Silence.ogg', type: 'audio/ogg' },
{ src: 'http://upload.wikimedia.org/wikipedia/en/c/cb/Stairway_to_Heaven_3_sections.ogg', type: 'audio/ogg' },
{ src: 'http://upload.wikimedia.org/wikipedia/en/d/d0/Beatles_cometogether.ogg', type: 'audio/ogg' }
];
var element = $compile('<audio media-player="testplayer" playlist="testplaylist"></audio>')($rootScope);
angular.element(document.body).append(element);
expect($rootScope.testplayer).to.be.an('object');
var songsPlayed = 0;
$rootScope.testplayer.one('canplaythrough', function () {
setTimeout(function () { $rootScope.testplayer.play(); }, 10);
});
// should play 2 times, first song, then second song!
$rootScope.testplayer.on('playing', function () {
songsPlayed++;
setTimeout(function () {
if (songsPlayed === 1) {
$rootScope.testplayer.next();
$timeout.flush();
} else if (songsPlayed === 2) {
expect($rootScope.testplayer.playing).to.equal(true);
expect($rootScope.testplayer.currentTrack).to.equal(2);
done();
}
}, 10);
});
});
});
it('should not autoplay the next file when calling prev(false), during playback', function (done) {
inject(function ($compile, $rootScope, $timeout) {
$rootScope.testplaylist = [
{ src: 'http://upload.wikimedia.org/wikipedia/commons/0/07/Silence.ogg', type: 'audio/ogg' },
{ src: 'http://upload.wikimedia.org/wikipedia/en/c/cb/Stairway_to_Heaven_3_sections.ogg', type: 'audio/ogg' },
{ src: 'http://upload.wikimedia.org/wikipedia/en/d/d0/Beatles_cometogether.ogg', type: 'audio/ogg' }
];
var element = $compile('<audio media-player="testplayer" playlist="testplaylist"></audio>')($rootScope);
angular.element(document.body).append(element);
expect($rootScope.testplayer).to.be.an('object');
setTimeout(function () { $rootScope.testplayer.play(1); }, 100);
// should play 2 times, first song, then second song!
$rootScope.testplayer.one('playing', function () {
setTimeout(function () {
expect($rootScope.testplayer.currentTrack).to.be.equal(2);
$rootScope.testplayer.prev(false);
$timeout.flush();
setTimeout(function () {
expect($rootScope.testplayer.currentTrack).to.be.equal(1);
done();
}, 10);
}, 10);
});
});
});
});