blob: f8bc55874cd30cb9c839656d75f33f565ea57dc0 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!--
Create an oscillator of each type and verify that the type is set correctly.
-->
<html>
<head>
<link rel="stylesheet" href="../fast/js/resources/js-test-style.css"/>
<script type="text/javascript" src="resources/audio-testing.js"></script>
<script type="text/javascript" src="../fast/js/resources/js-test-pre.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script>
description("Basic test of setting Oscillator node types.");
var sampleRate = 44100;
var renderLengthSeconds = 0.25;
var oscTypes = [{type: "sine", integerType: 0, name: "SINE"},
{type: "square", integerType: 1, name: "SQUARE"},
{type: "sawtooth", integerType: 2, name: "SAWTOOTH"},
{type: "triangle", integerType: 3, name: "TRIANGLE"},
{type: "custom", integerType: 4, name: "CUSTOM"}];
function runTest()
{
if (window.testRunner) {
testRunner.dumpAsText();
testRunner.waitUntilDone();
}
window.jsTestIsAsync = true;
// Create offline audio context.
var context = new webkitOfflineAudioContext(2, sampleRate * renderLengthSeconds, sampleRate);
var osc = context.createOscillator();
// Set each possible oscillator type (except CUSTOM) and verify that the type is correct.
// Here we're setting the type using WebIDL enum values which are strings.
for (var k = 0; k < oscTypes.length - 1; ++k) {
osc.type = oscTypes[k].type;
if (osc.type == oscTypes[k].type)
testPassed("Oscillator correctly set to " + oscTypes[k].name + " type.");
else
testFailed("Oscillator set to " + oscTypes[k].name + " type, but returns " + oscTypes[osc.type].name + " type.");
}
// For legacy support, verify that we can set the type attribute as an integer value and
// verify that this translates correctly to the WebIDL enum value.
for (var k = 0; k < oscTypes.length - 1; ++k) {
osc.type = oscTypes[k].integerType;
if (osc.type == oscTypes[k].type)
testPassed("Oscillator correctly set to " + oscTypes[k].name + " type using legacy integer value.");
else
testFailed("Oscillator set to " + oscTypes[k].name + " type, but returns " + oscTypes[osc.type].name + " type using legacy integer value.");
}
// Now set a custom oscillator
var coeffA = new Float32Array([0, 1, 0.5]);
var coeffB = new Float32Array([0, 0, 0]);
var wave = context.createPeriodicWave(coeffA, coeffB);
osc.setPeriodicWave(wave);
if (osc.type == "custom")
testPassed("Oscillator correctly set to CUSTOM type using setPeriodicWave.");
else
testFailed("Oscillator set to CUSTOM type, but returns " + oscTypes[osc.type].name + " type.");
// Try setting some invalid types
try {
osc.type = "custom";
testFailed("Directly setting oscillator type to CUSTOM did not throw exception.");
} catch (e) {
testPassed("Directly setting oscillator type to CUSTOM correctly throws exception.");
}
var oscType = osc.CUSTOM + 1;
try {
osc.type = oscType;
testFailed("Setting oscillator to invalid type " + oscType + " did not throw exception.");
} catch (e) {
testPassed("Setting oscillator to invalid type " + oscType + " correctly throws exception.");
}
// Check specifically that we throw a TypeError.
shouldThrowTypeError(function() { osc.type = "xyz12349jfksd"; }, "Setting .type to illegal string value");
shouldThrowTypeError(function() { osc.type = new Float32Array(1); }, "Setting .type to illegal type of Float32Array");
finishJSTest();
}
runTest();
successfullyParsed = true;
</script>
<script src="../fast/js/resources/js-test-post.js"></script>
</body>
</html>