Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / test / data / third_party / spaceport / js / tests / audioLatency.js
blob7962342c6c40451d9b806865a92ebd3ce260b612
1 define([ 'util/ensureCallback', 'util/cacheBust', 'util/timeout' ], function (ensureCallback, cacheBust, timeout) {
2     var MAX_LATENCY = 20;
4     function getPlayLatency(audio, callback) {
5         callback = ensureCallback(callback);
7         var startTime;
9         function update() {
10             var endTime = Date.now();
12             audio.removeEventListener('play', update, false);
13             audio.removeEventListener('timeupdate', update, false);
14             audio.pause();
16             callback(null, endTime - startTime);
17         }
19         audio.addEventListener('play', update, false);
20         audio.addEventListener('timeupdate', update, false);
22         startTime = Date.now();
23         audio.play();
24     }
26     function audioLatency(callback) {
27         callback = ensureCallback(callback);
29         if (!window.Audio) {
30             callback(new Error('Audio not supported'));
31             return;
32         }
34         var audio = new window.Audio();
36         function onCanPlayThrough() {
37             audio.removeEventListener('canplaythrough', onCanPlayThrough, false);
39             // Run the test twice: once for "cold" time, once for "warm" time.
40             getPlayLatency(audio, function (err, coldTime) {
41                 if (err) {
42                     callback(err);
43                     return;
44                 }
46                 getPlayLatency(audio, function (err, warmTime) {
47                     if (err) {
48                         callback(err);
49                         return;
50                     }
52                     callback(null, {
53                         pass: coldTime <= MAX_LATENCY && warmTime <= MAX_LATENCY,
54                         coldLatency: coldTime,
55                         warmLatency: warmTime
56                     });
57                 });
58             });
59         }
61         function onError() {
62             callback(new Error('Failed to load audio'));
63         }
65         var source = document.createElement('source');
66         source.src = cacheBust.url('assets/silence.wav');
67         source.addEventListener('error', onError, false);
69         audio.addEventListener('canplaythrough', onCanPlayThrough, false);
70         audio.addEventListener('error', onError, false);
71         audio.appendChild(source);
72         audio.play();
74         // Work around Webkit bug (present in Chrome <= 15, Safari <= 5, at
75         // time of writing) where the browser will decide it doesn't /need/
76         // to download all these pesky audio files.
77         window['audio__' + Math.random()] = audio;
78     }
80     return function (callback) {
81         callback = ensureCallback(callback);
83         timeout(5000, audioLatency, callback);
84     };
85 });