summaryrefslogtreecommitdiffstats
path: root/dom/browser-element/mochitest/browserElement_AudioChannel.js
blob: 20f7b4e47d3a8b07985c22b8b807bec6b61db4b7 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* Any copyright is dedicated to the public domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Bug 1113086 - tests for AudioChannel API into BrowserElement

"use strict";

SimpleTest.waitForExplicitFinish();
browserElementTestHelpers.setEnabledPref(true);

function noaudio() {
  info("Test : no-audio");
  var iframe = document.createElement('iframe');
  iframe.setAttribute('mozbrowser', 'true');
  iframe.src = 'chrome://mochitests/content/chrome/dom/browser-element/mochitest/file_empty.html';

  function noaudio_loadend() {
    ok("mute" in iframe, "iframe.mute exists");
    ok("unmute" in iframe, "iframe.unmute exists");
    ok("getMuted" in iframe, "iframe.getMuted exists");
    ok("getVolume" in iframe, "iframe.getVolume exists");
    ok("setVolume" in iframe, "iframe.setVolume exists");

    ok("allowedAudioChannels" in iframe, "allowedAudioChannels exist");
    var channels = iframe.allowedAudioChannels;
    is(channels.length, 9, "9 audio channel by default");

    var ac = channels[0];

    ok(ac instanceof BrowserElementAudioChannel, "Correct class");
    ok("getVolume" in ac, "ac.getVolume exists");
    ok("setVolume" in ac, "ac.setVolume exists");
    ok("getMuted" in ac, "ac.getMuted exists");
    ok("setMuted" in ac, "ac.setMuted exists");
    ok("isActive" in ac, "ac.isActive exists");

    new Promise(function(r, rr) {
      var req = ac.getVolume();
      ok(req instanceof DOMRequest, "This is a domRequest.");
      req.onsuccess = function(e) {
        is(e.target.result, 1.0, "The default volume should be 1.0");
        r();
      }
    })

    .then(function() {
      return new Promise(function(resolve) {
        iframe.mute();
        iframe.getMuted()
          .then(result => is(result, true, "iframe.getMuted should be true."))
          .then(resolve);
      });
    })

    .then(function() {
      return new Promise(function(resolve) {
        iframe.unmute();
        iframe.getMuted()
          .then(result => is(result, false, "iframe.getMuted should be false."))
          .then(resolve);
      });
    })

    .then(function() {
      return new Promise(function(resolve) {
        iframe.setVolume(0);
        iframe.getVolume()
          .then(result => is(result, 0, "iframe.getVolume should be 0."))
          .then(resolve);
      });
    })

    .then(function() {
      return new Promise(function(resolve) {
        iframe.setVolume(1);
        iframe.getVolume()
          .then(result => is(result, 1, "iframe.getVolume should be 1."))
          .then(resolve);
      });
    })

    .then(function() {
      return new Promise(function(r, rr) {
        ac.getMuted().onsuccess = function(e) {
          is(e.target.result, false, "The default muted value should be false");
          r();
        }
      });
    })

    .then(function() {
      return new Promise(function(r, rr) {
        ac.setVolume(0.8).onsuccess = function() { r(); }
      });
    })

    .then(function() {
      return new Promise(function(r, rr) {
        ac.getVolume().onsuccess = function(e) {
          // the actual value is 0.800000011920929..
          ok(Math.abs(0.8 - e.target.result) < 0.01, "The new volume should be 0.8: " + e.target.result);
          r();
        }
      });
    })

    .then(function() {
      return new Promise(function(r, rr) {
        ac.setVolume(1.0).onsuccess = function() { r(); }
      });
    })

    .then(function() {
      return new Promise(function(r, rr) {
        ac.setMuted(true).onsuccess = function() { r(); }
      });
    })

    .then(function() {
      return new Promise(function(r, rr) {
        ac.getMuted().onsuccess = function(e) {
          is(e.target.result, true, "The new muted value should be true");
          r();
        }
      });
    })

    .then(function() {
      return new Promise(function(r, rr) {
        ac.isActive().onsuccess = function(e) {
          is(e.target.result, false, "ac.isActive is false: no audio element active.");
          r();
        }
      });
    })

    .then(runTests);
  }

  iframe.addEventListener('mozbrowserloadend', noaudio_loadend);
  document.body.appendChild(iframe);
}

function audio() {
  info("Test : audio");
  var iframe = document.createElement('iframe');
  iframe.setAttribute('mozbrowser', 'true');
  iframe.src = 'chrome://mochitests/content/chrome/dom/browser-element/mochitest/iframe_file_audio.html';

  function audio_loadend() {
    ok("mute" in iframe, "iframe.mute exists");
    ok("unmute" in iframe, "iframe.unmute exists");
    ok("getMuted" in iframe, "iframe.getMuted exists");
    ok("getVolume" in iframe, "iframe.getVolume exists");
    ok("setVolume" in iframe, "iframe.setVolume exists");

    ok("allowedAudioChannels" in iframe, "allowedAudioChannels exist");
    var channels = iframe.allowedAudioChannels;
    is(channels.length, 9, "9 audio channel by default");

    var ac = channels[0];

    ok(ac instanceof BrowserElementAudioChannel, "Correct class");
    ok("getVolume" in ac, "ac.getVolume exists");
    ok("setVolume" in ac, "ac.setVolume exists");
    ok("getMuted" in ac, "ac.getMuted exists");
    ok("setMuted" in ac, "ac.setMuted exists");
    ok("isActive" in ac, "ac.isActive exists");

    ac.onactivestatechanged = function() {
      ok(true, "activestatechanged event received.");
      ac.onactivestatechanged = null;
      document.body.removeChild(iframe);
      runTests();
    }
  }

  iframe.addEventListener('mozbrowserloadend', audio_loadend);
  document.body.appendChild(iframe);
}

var tests = [ noaudio, audio ];

function runTests() {
  if (tests.length == 0) {
    SimpleTest.finish();
    return;
  }

  var test = tests.shift();
  test();
}

addEventListener('testready', function() {
  SpecialPowers.pushPrefEnv({'set': [["b2g.system_startup_url", window.location.href]]},
                            function() {
    SimpleTest.executeSoon(runTests);
  });
});