summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/tests/mochitests/test_setSinkId.html
blob: 0d85114a0e550c4753b96f0d2ea428d1fb169e5b (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
<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="mediaStreamPlayback.js"></script>
</head>
<body>
<pre id="test">

<script>
  createHTML({
    title: "SetSinkId in HTMLMediaElement",
    bug: "934425",
  });

  const memoryReportPath = 'explicit/media/media-manager-aggregates';

  /**
   * Run a test to verify set sink id in audio element.
   */
  runTest(async () => {
    await pushPrefs(["media.setsinkid.enabled", true]);

    if (!SpecialPowers.getCharPref("media.audio_loopback_dev", "")) {
      ok(false, "No loopback device set by framework. Try --use-test-media-devices");
      return;
    }

    // Expose an audio output device.
    SpecialPowers.wrap(document).notifyUserGestureActivation();
    await navigator.mediaDevices.selectAudioOutput();

    const allDevices = await navigator.mediaDevices.enumerateDevices();
    const audioDevices = allDevices.filter(({kind}) => kind == 'audiooutput');
    is(audioDevices.length, 1, "Number of output devices found");

    const audio = createMediaElement("audio", "audio");
    document.body.appendChild(audio);

    is(audio.sinkId, "", "Initial value is empty string");

    const p = audio.setSinkId(audioDevices[0].deviceId);
    is(audio.sinkId, "", "Value is unchanged upon function return");
    is(await p, undefined, "promise resolves with undefined");
    is(audio.sinkId, audioDevices[0].deviceId, `Sink device is set, id: ${audio.sinkId}`);

    await audio.setSinkId(audioDevices[0].deviceId);
    ok(true, `Sink device is set for 2nd time for the same id: ${audio.sinkId}`);

    try {
      await audio.setSinkId("dummy sink id");
      ok(false, "Never enter here, this must fail");
    } catch (error) {
      ok(true, `Set sink id expected to fail: ${error}`);
      is(error.name, "NotFoundError", "Verify correct error");
    }

    const {usage: usage1} =
          await collectMemoryUsage(memoryReportPath); // Provided by head.js

    ok(usage1 > 0, "MediaManager memory usage should be non-zero to store \
device ids after enumerateDevices");

    const p2 = audio.setSinkId("");
    is(audio.sinkId, audioDevices[0].deviceId,
       'sinkId after setSinkId("") return');
    is(await p2, undefined,
       "promise resolution value when sinkId parameter is empty");
    is(audio.sinkId, "", 'sinkId after setSinkId("") resolution');

    await audio.setSinkId(audioDevices[0].deviceId);

    const {usage: usage2, reportCount} =
          await collectMemoryUsage(memoryReportPath);
    is(reportCount, 1,
       'Expect only one MediaManager to report in content processes.');
    is(usage2, usage1, "MediaManager memory usage should return to previous \
value after promise resolution");
  });

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