ctor-offlineaudiocontext.html (7477B)
1 <!doctype html> 2 <html> 3 <head> 4 <title>Test Constructor: OfflineAudioContext</title> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src="/webaudio/resources/audit.js"></script> 8 <script src="/webaudio/resources/audit-util.js"></script> 9 <script src="/webaudio/resources/audionodeoptions.js"></script> 10 </head> 11 12 <body> 13 <script> 14 let audit = Audit.createTaskRunner(); 15 16 // Just a simple test of the 3-arg constructor; This should be 17 // well-covered by other layout tests that use the 3-arg constructor. 18 audit.define( 19 {label: 'basic', description: 'Old-style constructor'}, 20 (task, should) => { 21 let context; 22 23 // First and only arg should be a dictionary. 24 should(() => { 25 new OfflineAudioContext(3); 26 }, 'new OfflineAudioContext(3)').throw(TypeError); 27 28 // Constructor needs 1 or 3 args, so 2 should throw. 29 should(() => { 30 new OfflineAudioContext(3, 42); 31 }, 'new OfflineAudioContext(3, 42)').throw(TypeError); 32 33 // Valid constructor 34 should(() => { 35 context = new OfflineAudioContext(3, 42, 12345); 36 }, 'context = new OfflineAudioContext(3, 42, 12345)').notThrow(); 37 38 // Verify that the context was constructed correctly. 39 should(context.length, 'context.length').beEqualTo(42); 40 should(context.sampleRate, 'context.sampleRate').beEqualTo(12345); 41 should( 42 context.destination.channelCount, 43 'context.destination.channelCount') 44 .beEqualTo(3); 45 should( 46 context.destination.channelCountMode, 47 'context.destination.channelCountMode') 48 .beEqualTo('explicit'); 49 should( 50 context.destination.channelInterpretation, 51 'context.destination.channelInterpretation') 52 .beEqualTo('speakers'); 53 task.done(); 54 }); 55 56 // Test constructor throws an error if the required members of the 57 // dictionary are not given. 58 audit.define( 59 {label: 'options-1', description: 'Required options'}, 60 (task, should) => { 61 let context2; 62 63 // No args should throw 64 should(() => { 65 new OfflineAudioContext(); 66 }, 'new OfflineAudioContext()').throw(TypeError); 67 68 // Empty OfflineAudioContextOptions should throw 69 should(() => { 70 new OfflineAudioContext({}); 71 }, 'new OfflineAudioContext({})').throw(TypeError); 72 73 let options = {length: 42}; 74 // sampleRate is required. 75 should( 76 () => { 77 new OfflineAudioContext(options); 78 }, 79 'new OfflineAudioContext(' + JSON.stringify(options) + ')') 80 .throw(TypeError); 81 82 options = {sampleRate: 12345}; 83 // length is required. 84 should( 85 () => { 86 new OfflineAudioContext(options); 87 }, 88 'new OfflineAudioContext(' + JSON.stringify(options) + ')') 89 .throw(TypeError); 90 91 // Valid constructor. Verify that the resulting context has the 92 // correct values. 93 options = {length: 42, sampleRate: 12345}; 94 should( 95 () => { 96 context2 = new OfflineAudioContext(options); 97 }, 98 'c2 = new OfflineAudioContext(' + JSON.stringify(options) + ')') 99 .notThrow(); 100 should( 101 context2.destination.channelCount, 102 'c2.destination.channelCount') 103 .beEqualTo(1); 104 should(context2.length, 'c2.length').beEqualTo(options.length); 105 should(context2.sampleRate, 'c2.sampleRate') 106 .beEqualTo(options.sampleRate); 107 should( 108 context2.destination.channelCountMode, 109 'c2.destination.channelCountMode') 110 .beEqualTo('explicit'); 111 should( 112 context2.destination.channelInterpretation, 113 'c2.destination.channelInterpretation') 114 .beEqualTo('speakers'); 115 116 task.done(); 117 }); 118 119 // Constructor should throw errors for invalid values specified by 120 // OfflineAudioContextOptions. 121 audit.define( 122 {label: 'options-2', description: 'Invalid options'}, 123 (task, should) => { 124 let options = {length: 42, sampleRate: 8000, numberOfChannels: 33}; 125 126 // channelCount too large. 127 should( 128 () => { 129 new OfflineAudioContext(options); 130 }, 131 'new OfflineAudioContext(' + JSON.stringify(options) + ')') 132 .throw(DOMException, 'NotSupportedError'); 133 134 // length cannot be 0 135 options = {length: 0, sampleRate: 8000}; 136 should( 137 () => { 138 new OfflineAudioContext(options); 139 }, 140 'new OfflineAudioContext(' + JSON.stringify(options) + ')') 141 .throw(DOMException, 'NotSupportedError'); 142 143 // sampleRate outside valid range 144 options = {length: 1, sampleRate: 1}; 145 should( 146 () => { 147 new OfflineAudioContext(options); 148 }, 149 'new OfflineAudioContext(' + JSON.stringify(options) + ')') 150 .throw(DOMException, 'NotSupportedError'); 151 152 task.done(); 153 }); 154 155 audit.define( 156 {label: 'options-3', description: 'Valid options'}, 157 (task, should) => { 158 let context; 159 let options = { 160 length: 1, 161 sampleRate: 8000, 162 }; 163 164 // Verify context with valid constructor has the correct values. 165 should( 166 () => { 167 context = new OfflineAudioContext(options); 168 }, 169 'c = new OfflineAudioContext' + JSON.stringify(options) + ')') 170 .notThrow(); 171 should(context.length, 'c.length').beEqualTo(options.length); 172 should(context.sampleRate, 'c.sampleRate') 173 .beEqualTo(options.sampleRate); 174 should( 175 context.destination.channelCount, 'c.destination.channelCount') 176 .beEqualTo(1); 177 should( 178 context.destination.channelCountMode, 179 'c.destination.channelCountMode') 180 .beEqualTo('explicit'); 181 should( 182 context.destination.channelInterpretation, 183 'c.destination.channelCountMode') 184 .beEqualTo('speakers'); 185 186 options.numberOfChannels = 7; 187 should( 188 () => { 189 context = new OfflineAudioContext(options); 190 }, 191 'c = new OfflineAudioContext' + JSON.stringify(options) + ')') 192 .notThrow(); 193 should( 194 context.destination.channelCount, 'c.destination.channelCount') 195 .beEqualTo(options.numberOfChannels); 196 197 task.done(); 198 }); 199 200 audit.run(); 201 </script> 202 </body> 203 </html>