summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/test/test_OfflineAudioContext.html
blob: 81d3e2313cbd16a9ab482425939d287acfbfbde5 (plain)
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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test OfflineAudioContext</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="webaudio.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">

var renderedBuffer = null;
var finished = 0;

function finish() {
  finished++;
  if (finished == 2) {
    SimpleTest.finish();
  }
}

function setOrCompareRenderedBuffer(aRenderedBuffer) {
  if (renderedBuffer) {
    is(renderedBuffer, aRenderedBuffer, "Rendered buffers from the event and the promise should be the same");
    finish();
  } else {
    renderedBuffer = aRenderedBuffer;
  }
}

SimpleTest.waitForExplicitFinish();
addLoadEvent(function() {
  var ctx = new OfflineAudioContext(2, 100, 22050);
  ok(ctx instanceof EventTarget, "OfflineAudioContexts must be EventTargets");
  is(ctx.length, 100, "OfflineAudioContext.length is equal to the value passed to the ctor.");

  var buf = ctx.createBuffer(2, 100, ctx.sampleRate);
  for (var i = 0; i < 2; ++i) {
    for (var j = 0; j < 100; ++j) {
      buf.getChannelData(i)[j] = Math.sin(2 * Math.PI * 200 * j / ctx.sampleRate);
    }
  }

  expectException(function() {
    ctx.createMediaStreamDestination();
  }, DOMException.NOT_SUPPORTED_ERR);

  expectException(function() {
    new OfflineAudioContext(2, 100, 0);
  }, DOMException.NOT_SUPPORTED_ERR);
  expectException(function() {
    new OfflineAudioContext(2, 100, -1);
  }, DOMException.NOT_SUPPORTED_ERR);
  expectException(function() {
    new OfflineAudioContext(0, 100, 44100);
  }, DOMException.NOT_SUPPORTED_ERR);
  new OfflineAudioContext(32, 100, 44100);
  expectException(function() {
    new OfflineAudioContext(33, 100, 44100);
  }, DOMException.NOT_SUPPORTED_ERR);
  expectException(function() {
    new OfflineAudioContext(2, 0, 44100);
  }, DOMException.NOT_SUPPORTED_ERR);

  var src = ctx.createBufferSource();
  src.buffer = buf;
  src.start(0);
  src.connect(ctx.destination);

  ctx.addEventListener("complete", function(e) {
    ok(e instanceof OfflineAudioCompletionEvent, "Correct event received");
    is(e.renderedBuffer.numberOfChannels, 2, "Correct expected number of buffers");
    ok(renderedBuffer != null, "The event should be fired after the promise callback.");
    expectNoException(function() {
      ctx.startRendering().then(function() {
        ok(false, "Promise should not resolve when startRendering is called a second time on an OfflineAudioContext")
        finish();
      }).catch(function(err) {
        ok(true, "Promise should reject when startRendering is called a second time on an OfflineAudioContext")
        finish();
      });
    });
    compareBuffers(e.renderedBuffer, buf);
    setOrCompareRenderedBuffer(e.renderedBuffer);

  }, false);

  expectNoException(function() {
    ctx.startRendering().then(function(b) {
      is(renderedBuffer, null, "The promise callback should be called first.");
      setOrCompareRenderedBuffer(b);
    }).catch(function (error) {
       ok(false, "The promise from OfflineAudioContext.startRendering should never be rejected");
    });
  });
});

</script>
</pre>
</body>
</html>