summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_audioNotificationSilent_webAudio.html
blob: de4165f8e438aa9c8abd0c4bfb7845acdf56e702 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Audio-playback should be inactive when web-audio is silent</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="application/javascript;version=1.7">

SimpleTest.waitForExplicitFinish();

var generator = runTest();
var expectedPlaybackActive = null;
var expectedPlaying = null;

var ac = new AudioContext();
var audibleDuration = 3;

var observerService = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
                                   .getService(SpecialPowers.Ci.nsIObserverService);

var observer = {
  observe: function(subject, topic, data) {
    is(topic, "audio-playback", "audio-playback received");
    is(data, expectedPlaybackActive, "Corrrect audible state");
    is(ac.state, expectedPlaying, "Corrrect playing state");
    continueTest();
  }
};

function continueTest() {
  try {
    generator.next();
  } catch (e if e instanceof StopIteration) {
    error("Stop test because of exception!");
  }
}

function playOscillatorNode() {
  var dest = ac.destination;
  var osc = ac.createOscillator();
  osc.connect(dest);
  osc.start(0);
  osc.stop(ac.currentTime + audibleDuration);
}

function audioPlayingStart() {
  observerService.addObserver(observer, "audio-playback", false);
  ok(true, "Observer set");

  expectedPlaybackActive = 'active';
  expectedPlaying = "running";

  info("Audio playing start");
  playOscillatorNode();
}

function audioBecomeSilentDuringPlaying() {
  info("Audio would become silent during playing");

  expectedPlaybackActive = 'inactive-pause';
  expectedPlaying = "running";
}

function finish() {
  observerService.removeObserver(observer, "audio-playback");
  ok(true, "Observer removed");

  SimpleTest.finish();
}

function startAudioContext() {
  if (ac.state != "running") {
    ac.resume();
    ac.onstatechange = function() {
      if (ac.state == "running") {
        ok(true, "AudioContext starts running!");
        ac.onstatechange = null;
        continueTest();
      }
    }
  } else {
    ok(true, "AudioContext is running!");
    continueTest();
  }
}

function runTest() {
  yield startAudioContext();

  yield audioPlayingStart();

  yield audioBecomeSilentDuringPlaying();

  yield finish();
}

continueTest();

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