summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/test/test_audioContextGC.html
blob: be9990cfaddbf59f430dad51daaf7513ef7fa0c7 (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
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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test inactive AudioContext is garbage collected</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script class="testbody" type="text/javascript">

SimpleTest.waitForExplicitFinish();

let ids;

const observer = (subject, topic, data) => {
  const id = parseInt(data);
  if (ids) {
    ok(ids.delete(id), "Collected AudioNode id " + id);
  }
}
SpecialPowers.addObserver(observer, "webaudio-node-demise");

SimpleTest.registerCleanupFunction(function() {
  if (observer) {
    SpecialPowers.removeObserver(observer, "webaudio-node-demise");
  }
});

function id(node) {
  return SpecialPowers.getPrivilegedProps(node, "id");
}

let tests = [{
  name: "Bare running AudioContext", setup: () => {
    const ac = new AudioContext();
    ids.add(id(ac.destination));
    // Await state change notification before collection.
    return new Promise((resolve) => {
      ac.onstatechange = () => {
        is(ac.state, "running", "ac.state");
        resolve();
      };
    });
  }
}, {
  name: "Stopped source", setup: () => {
    const ac = new AudioContext();
    ids.add(id(ac.destination));
    const source = new ConstantSourceNode(ac);
    ids.add(id(source));
    source.start();
    source.stop();
    // Await ended notification before collection.
    return new Promise((resolve) => {
      source.onended = () => {
        is(ac.state, "running", "ac.state");
        resolve();
      };
    });
  }
}, {
  name: "OfflineAudioContext not started", setup: () => {
    const ac = new OfflineAudioContext({
      numberOfChannels: 1, length: 1, sampleRate: 48000
    });
    ids.add(id(ac.destination));
    const source = new ConstantSourceNode(ac);
    ids.add(id(source));
    source.start();
  }
}, {
  name: "Completed OfflineAudioContext", setup: async () => {
    const ac = new OfflineAudioContext({
      numberOfChannels: 1, length: 1, sampleRate: 48000
    });
    ids.add(id(ac.destination));
    const sourceBeforeStart = new ConstantSourceNode(ac);
    ids.add(id(sourceBeforeStart));
    sourceBeforeStart.start();
    ac.startRendering();
    await new Promise((resolve) => {
      ac.oncomplete = () => {
        resolve();
      };
    });
    const sourceAfterComplete = new ConstantSourceNode(ac);
    ids.add(id(sourceAfterComplete));
    sourceAfterComplete.start();
  }
}, {
  name: "suspended AudioContext", setup: async () => {
    const ac = new AudioContext();
    ids.add(id(ac.destination));
    const sourceBeforeSuspend = new ConstantSourceNode(ac);
    ids.add(id(sourceBeforeSuspend));
    sourceBeforeSuspend.start();
    ac.suspend();
    const sourceAfterSuspend = new ConstantSourceNode(ac);
    ids.add(id(sourceAfterSuspend));
    sourceAfterSuspend.start();
    await new Promise((resolve) => {
      ac.onstatechange = () => {
        if (ac.state == "suspended") {
          resolve();
        }
      };
    });
    const sourceAfterSuspended = new ConstantSourceNode(ac);
    ids.add(id(sourceAfterSuspended));
    sourceAfterSuspended.start();
  }
}, {
  name: "closed AudioContext", setup: async () => {
    const ac = new AudioContext();
    ids.add(id(ac.destination));
    const sourceBeforeClose = new ConstantSourceNode(ac);
    ids.add(id(sourceBeforeClose));
    sourceBeforeClose.start();
    ac.close();
    const sourceAfterClose = new ConstantSourceNode(ac);
    ids.add(id(sourceAfterClose));
    sourceAfterClose.start();
    await new Promise((resolve) => {
      ac.onstatechange = () => {
        if (ac.state == "closed") {
          resolve();
        }
      };
    });
    const sourceAfterClosed = new ConstantSourceNode(ac);
    ids.add(id(sourceAfterClosed));
    sourceAfterClosed.start();
  }
}];

const start_next_test = async () => {
  const test = tests.shift();
  if (!test) {
    SimpleTest.finish();
    return;
  }
  // Collect all audio nodes from previous tests.
  if (!ids) {
    await new Promise(resolve => {
      SpecialPowers.exactGC(resolve);
    });
  }
  ids = new Set();
  await test.setup();
  SpecialPowers.exactGC(() => {
    is(ids.size, 0,
       `All expected nodes for "${test.name}" should be collected`);
    start_next_test();
  });
}

start_next_test();

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