tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

audiocontextoptions.html (8126B)


      1 <!DOCTYPE html>
      2 <html>
      3  <head>
      4    <title>
      5      Test AudioContextOptions
      6    </title>
      7    <script src="/resources/testharness.js"></script>
      8    <script src="/resources/testharnessreport.js"></script>
      9    <script src="/webaudio/resources/audit.js"></script>
     10  </head>
     11  <body>
     12    <script id="layout-test-code">
     13      let context;
     14      let defaultLatency;
     15      let interactiveLatency;
     16      let balancedLatency;
     17      let playbackLatency;
     18 
     19      let audit = Audit.createTaskRunner();
     20 
     21      audit.define(
     22          {
     23            label: 'test-audiocontextoptions-latencyHint-basic',
     24            description: 'Test creating contexts with basic latencyHint types.'
     25          },
     26          function(task, should) {
     27            let closingPromises = [];
     28 
     29            // Verify that an AudioContext can be created with default options.
     30            should(function() {
     31              context = new AudioContext()
     32            }, 'context = new AudioContext()').notThrow();
     33 
     34            should(context.sampleRate,
     35              `context.sampleRate (${context.sampleRate} Hz)`).beGreaterThan(0);
     36 
     37            defaultLatency = context.baseLatency;
     38            should(defaultLatency, 'default baseLatency').beGreaterThanOrEqualTo(0);
     39 
     40            // Verify that an AudioContext can be created with the expected
     41            // latency types.
     42            should(
     43                function() {
     44                  context = new AudioContext({'latencyHint': 'interactive'})
     45                },
     46                'context = new AudioContext({\'latencyHint\': \'interactive\'})')
     47                .notThrow();
     48 
     49            interactiveLatency = context.baseLatency;
     50            should(interactiveLatency, 'interactive baseLatency')
     51                .beEqualTo(defaultLatency);
     52            closingPromises.push(context.close());
     53 
     54            should(
     55                function() {
     56                  context = new AudioContext({'latencyHint': 'balanced'})
     57                },
     58                'context = new AudioContext({\'latencyHint\': \'balanced\'})')
     59                .notThrow();
     60 
     61            balancedLatency = context.baseLatency;
     62            should(balancedLatency, 'balanced baseLatency')
     63                .beGreaterThanOrEqualTo(interactiveLatency);
     64            closingPromises.push(context.close());
     65 
     66            should(
     67                function() {
     68                  context = new AudioContext({'latencyHint': 'playback'})
     69                },
     70                'context = new AudioContext({\'latencyHint\': \'playback\'})')
     71                .notThrow();
     72 
     73            playbackLatency = context.baseLatency;
     74            should(playbackLatency, 'playback baseLatency')
     75                .beGreaterThanOrEqualTo(balancedLatency);
     76            closingPromises.push(context.close());
     77 
     78            Promise.all(closingPromises).then(function() {
     79              task.done();
     80            });
     81          });
     82 
     83      audit.define(
     84          {
     85            label: 'test-audiocontextoptions-latencyHint-double',
     86            description:
     87                'Test creating contexts with explicit latencyHint values.'
     88          },
     89          function(task, should) {
     90            let closingPromises = [];
     91 
     92            // Verify too small exact latency clamped to 'interactive'
     93            should(
     94                function() {
     95                  context =
     96                      new AudioContext({'latencyHint': interactiveLatency / 2})
     97                },
     98                'context = new AudioContext({\'latencyHint\': ' +
     99                    'interactiveLatency/2})')
    100                .notThrow();
    101            should(context.baseLatency, 'double-constructor baseLatency small')
    102                .beLessThanOrEqualTo(interactiveLatency);
    103            closingPromises.push(context.close());
    104 
    105            // Verify that exact latency in range works as expected
    106            let validLatency = (interactiveLatency + playbackLatency) / 2;
    107            should(
    108                function() {
    109                  context = new AudioContext({'latencyHint': validLatency})
    110                },
    111                'context = new AudioContext({\'latencyHint\': validLatency})')
    112                .notThrow();
    113            should(
    114                context.baseLatency, 'double-constructor baseLatency inrange 1')
    115                .beGreaterThanOrEqualTo(interactiveLatency);
    116            should(
    117                context.baseLatency, 'double-constructor baseLatency inrange 2')
    118                .beLessThanOrEqualTo(playbackLatency);
    119            closingPromises.push(context.close());
    120 
    121            // Verify too big exact latency clamped to some value
    122            let context1;
    123            let context2;
    124            should(function() {
    125              context1 =
    126                  new AudioContext({'latencyHint': playbackLatency * 10});
    127              context2 =
    128                  new AudioContext({'latencyHint': playbackLatency * 20});
    129            }, 'creating two high latency contexts').notThrow();
    130            should(context1.baseLatency, 'high latency context baseLatency')
    131                .beEqualTo(context2.baseLatency);
    132            should(context1.baseLatency, 'high latency context baseLatency')
    133                .beGreaterThanOrEqualTo(interactiveLatency);
    134            closingPromises.push(context1.close());
    135            closingPromises.push(context2.close());
    136 
    137            // Verify that invalid latencyHint values are rejected.
    138            should(
    139                function() {
    140                  context = new AudioContext({'latencyHint': 'foo'})
    141                },
    142                'context = new AudioContext({\'latencyHint\': \'foo\'})')
    143                .throw(TypeError);
    144 
    145            // Verify that no extra options can be passed into the
    146            // AudioContextOptions.
    147            should(
    148                function() {
    149                  context = new AudioContext('latencyHint')
    150                },
    151                'context = new AudioContext(\'latencyHint\')')
    152                .throw(TypeError);
    153 
    154            Promise.all(closingPromises).then(function() {
    155              task.done();
    156            });
    157          });
    158 
    159      audit.define(
    160          {
    161            label: 'test-audiocontextoptions-sampleRate',
    162            description:
    163                'Test creating contexts with non-default sampleRate values.'
    164          },
    165          function(task, should) {
    166            // A sampleRate of 1 is unlikely to be supported on any browser,
    167            // test that this rate is rejected.
    168            should(
    169                () => {
    170                  context = new AudioContext({sampleRate: 1})
    171                },
    172                'context = new AudioContext({sampleRate: 1})')
    173                .throw(DOMException, 'NotSupportedError');
    174 
    175            // A sampleRate of 1,000,000 is unlikely to be supported on any
    176            // browser, test that this rate is also rejected.
    177            should(
    178                () => {
    179                  context = new AudioContext({sampleRate: 1000000})
    180                },
    181                'context = new AudioContext({sampleRate: 1000000})')
    182                .throw(DOMException, 'NotSupportedError');
    183            // A negative sample rate should not be accepted
    184            should(
    185                () => {
    186                  context = new AudioContext({sampleRate: -1})
    187                },
    188                'context = new AudioContext({sampleRate: -1})')
    189                .throw(DOMException, 'NotSupportedError');
    190            // A null sample rate should not be accepted
    191            should(
    192                () => {
    193                  context = new AudioContext({sampleRate: 0})
    194                },
    195                'context = new AudioContext({sampleRate: 0})')
    196                .throw(DOMException, 'NotSupportedError');
    197 
    198            should(
    199                () => {
    200                  context = new AudioContext({sampleRate: 24000})
    201                },
    202                'context = new AudioContext({sampleRate: 24000})')
    203                .notThrow();
    204            should(
    205                context.sampleRate, 'sampleRate inrange')
    206                .beEqualTo(24000);
    207 
    208            context.close();
    209            task.done();
    210          });
    211 
    212      audit.run();
    213    </script>
    214  </body>
    215 </html>