Skip to content

Commit

Permalink
Fix the error catching logic of should().throw() in audit.js
Browse files Browse the repository at this point in the history
Context: #12606

Change the logic of should.throw() so we can handle 3 cases -

should(someExpression).throw();
should(someExpression).throw(TypeError);
should(someExpression).throw(DOMException, 'NotSupportedError');

The generic error (except for DOMException) can be passed without
the second argument, but this change will enforce the second arg
when the expected error is a DOMException type.

This touches many test files, so the work will be done in several
steps:

1. Change audit.js, audionodeoptions.js on both locations.
  (wpt, non-wpt)
2. Update affected test files with the script.
3. Update the rest of test files which can't be updated
  programmatically.

Bug: 865614
Test: All layout tests pass.
Change-Id: I16acacb26c194a0ff950aca05e931195bf55167f
Reviewed-on: https://chromium-review.googlesource.com/1184146
Commit-Queue: Hongchan Choi <[email protected]>
Reviewed-by: Raymond Toy <[email protected]>
Cr-Commit-Position: refs/heads/master@{#587660}
  • Loading branch information
hoch authored and Chrome-bot committed Aug 30, 2018
1 parent 9b90762 commit 91a7ab3
Show file tree
Hide file tree
Showing 32 changed files with 164 additions and 137 deletions.
14 changes: 8 additions & 6 deletions webaudio/resources/audionodeoptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ function testAudioNodeOptions(should, context, nodeName, expectedNodeOptions) {
{channelCount: testChannelCount}));
},
'new ' + nodeName + '(c, {channelCount: ' + testChannelCount + '}}')
.throw(expectedNodeOptions.channelCount.errorType || TypeError);
.throw(DOMException,
expectedNodeOptions.channelCount.exceptionType);
} else {
// The channel count is not fixed. Try to set the count to invalid
// values and make sure an error is thrown.
let errorType = 'NotSupportedError';

[0, 99].forEach(testValue => {
should(() => {
node = new window[nodeName](
context, Object.assign({}, expectedNodeOptions.additionalOptions, {
channelCount: testValue
}));
}, `new ${nodeName}(c, {channelCount: ${testValue}})`).throw(errorType);
}, `new ${nodeName}(c, {channelCount: ${testValue}})`)
.throw(DOMException, 'NotSupportedError');
});
}

Expand Down Expand Up @@ -88,7 +88,8 @@ function testAudioNodeOptions(should, context, nodeName, expectedNodeOptions) {
{channelCountMode: testValue}));
},
`new ${nodeName}(c, {channelCountMode: "${testValue}"})`)
.throw(expectedNodeOptions.channelCountMode.errorType);
.throw(DOMException,
expectedNodeOptions.channelCountMode.exceptionType);
}
});
} else {
Expand Down Expand Up @@ -140,7 +141,8 @@ function testAudioNodeOptions(should, context, nodeName, expectedNodeOptions) {
{channelInterpretation: testValue}));
},
`new ${nodeName}(c, {channelInterpretation: "${testValue}"})`)
.throw(expectedNodeOptions.channelInterpretation.errorType);
.throw(DOMException,
expectedNodeOptions.channelCountMode.exceptionType);
}
});
} else {
Expand Down
35 changes: 19 additions & 16 deletions webaudio/resources/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,21 +274,25 @@ window.Audit = (function() {

/**
* Check if |actual| operation wrapped in a function throws an exception
* with a expected error type correctly. |expected| is optional. If it is a
* String, then it is considered to be the name of a DOMException. It can
* also be an instance of either an Error or a DOMException, to be more
* strict about the actual error type.
* with a expected error type correctly. |expected| is optional. If it is an
* instance of DOMException, then the description (second argument) can be
* provided to be more strict about the expected exception type. |expected|
* also can be other generic error types such as TypeError, RangeError or
* etc.
*
* @example
* should(() => { let a = b; }, 'A bad code').throw();
* should(() => { let c = d; }, 'Assigning d to c.')
* .throw('ReferenceError');
* should(() => { let e = f; }, 'Assigning e to f.')
* .throw('ReferenceError', { omitErrorMessage: true });
* should(() => { new SomeConstructor(); }, 'A bad construction')
* .throw(DOMException, 'NotSupportedError');
* should(() => { let c = d; }, 'Assigning d to c')
* .throw(ReferenceError);
* should(() => { let e = f; }, 'Assigning e to f')
* .throw(ReferenceError, { omitErrorMessage: true });
*
* @result
* "PASS A bad code threw an exception of ReferenceError: b is not
* defined."
* "PASS A bad construction threw DOMException:NotSupportedError."
* "PASS Assigning d to c threw ReferenceError: d is not defined."
* "PASS Assigning e to f threw ReferenceError: [error message
* omitted]."
Expand All @@ -313,17 +317,16 @@ window.Audit = (function() {
// The expected error type was not given.
didThrowCorrectly = true;
passDetail = '${actual} threw ' + error.name + errorMessage + '.';
} else if (typeof(this._expected) == "string" &&
error instanceof DOMException &&
error.name === this._expected) {
// A DOMException was thrown and expected, and the names match
} else if (this._expected === DOMException &&
(this._expectedDescription === undefined ||
this._expectedDescription === error.name)) {
// Handles DOMException with the associated name.
didThrowCorrectly = true;
passDetail = '${actual} threw ${expected}' + errorMessage + '.';
} else if (this._expected == error.constructor &&
this._expected.name == error.name) {
// The expected error type and names match the actual one.
} else if (this._expected == error.constructor) {
// Handler other error types.
didThrowCorrectly = true;
passDetail = '${actual} threw ${expected}' + errorMessage + '.';
passDetail = '${actual} threw ' + error.name + errorMessage + '.';
} else {
didThrowCorrectly = false;
failDetail =
Expand Down
4 changes: 2 additions & 2 deletions webaudio/resources/start-stop-exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function testStartStop(should, node, options) {

should(() => {
node.stop();
}, 'Calling stop() before start()').throw('InvalidStateError');
}, 'Calling stop() before start()').throw(DOMException, 'InvalidStateError');

should(() => {
node.start(-1);
Expand All @@ -29,7 +29,7 @@ function testStartStop(should, node, options) {
node.start();
should(() => {
node.start();
}, 'Calling start() twice').throw('InvalidStateError');
}, 'Calling start() twice').throw(DOMException, 'InvalidStateError');
should(() => {
node.stop(-1);
}, 'stop(-1)').throw(RangeError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,25 @@
node = new AnalyserNode(context, {fftSize: 33});
},
'node = new AnalyserNode(c, { fftSize: 33 })')
.throw('IndexSizeError');
.throw(DOMException, 'IndexSizeError');
should(
() => {
node = new AnalyserNode(context, {maxDecibels: -500});
},
'node = new AnalyserNode(c, { maxDecibels: -500 })')
.throw('IndexSizeError');
.throw(DOMException, 'IndexSizeError');
should(
() => {
node = new AnalyserNode(context, {minDecibels: -10});
},
'node = new AnalyserNode(c, { minDecibels: -10 })')
.throw('IndexSizeError');
.throw(DOMException, 'IndexSizeError');
should(
() => {
node = new AnalyserNode(context, {smoothingTimeConstant: 2});
},
'node = new AnalyserNode(c, { smoothingTimeConstant: 2 })')
.throw('IndexSizeError');
.throw(DOMException, 'IndexSizeError');
should(function() {
node = new AnalyserNode(context, {frequencyBinCount: 33});
}, 'node = new AnalyserNode(c, { frequencyBinCount: 33 })').notThrow();
Expand Down Expand Up @@ -164,15 +164,15 @@
node = new AnalyserNode(context, options);
},
'node = new AnalyserNode(c, ' + JSON.stringify(options) + ')')
.throw('IndexSizeError');
.throw(DOMException, 'IndexSizeError');

options = {minDecibels: -10, maxDecibels: -150};
should(
() => {
node = new AnalyserNode(context, options);
},
'node = new AnalyserNode(c, ' + JSON.stringify(options) + ')')
.throw('IndexSizeError');
.throw(DOMException, 'IndexSizeError');

task.done();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
};

if (illegal) {
should(tester, message).throw('IndexSizeError');
should(tester, message).throw(DOMException, 'IndexSizeError');
} else {
should(tester, message).notThrow();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,27 @@
}, '2: buffer.copyFromChannel(context, 0)').throw(TypeError);
should(() => {
buffer.copyFromChannel(x, -1);
}, '3: buffer.copyFromChannel(x, -1)').throw('IndexSizeError');
}, '3: buffer.copyFromChannel(x, -1)').throw(DOMException, 'IndexSizeError');
should(
() => {
buffer.copyFromChannel(x, numberOfChannels);
},
'4: buffer.copyFromChannel(x, ' + numberOfChannels + ')')
.throw('IndexSizeError');
.throw(DOMException, 'IndexSizeError');
;
should(() => {
buffer.copyFromChannel(x, 0, -1);
}, '5: buffer.copyFromChannel(x, 0, -1)').throw('IndexSizeError');
}, '5: buffer.copyFromChannel(x, 0, -1)').throw(DOMException, 'IndexSizeError');
should(
() => {
buffer.copyFromChannel(x, 0, bufferLength);
},
'6: buffer.copyFromChannel(x, 0, ' + bufferLength + ')')
.throw('IndexSizeError');
.throw(DOMException, 'IndexSizeError');

should(() => {
buffer.copyFromChannel(x, 3);
}, '7: buffer.copyFromChannel(x, 3)').throw('IndexSizeError');
}, '7: buffer.copyFromChannel(x, 3)').throw(DOMException, 'IndexSizeError');

if (window.SharedArrayBuffer) {
let shared_buffer = new Float32Array(new SharedArrayBuffer(32));
Expand Down Expand Up @@ -183,26 +183,26 @@
}, '1: buffer.copyToChannel(context, 0)').throw(TypeError);
should(() => {
buffer.copyToChannel(x, -1);
}, '2: buffer.copyToChannel(x, -1)').throw('IndexSizeError');
}, '2: buffer.copyToChannel(x, -1)').throw(DOMException, 'IndexSizeError');
should(
() => {
buffer.copyToChannel(x, numberOfChannels);
},
'3: buffer.copyToChannel(x, ' + numberOfChannels + ')')
.throw('IndexSizeError');
.throw(DOMException, 'IndexSizeError');
should(() => {
buffer.copyToChannel(x, 0, -1);
}, '4: buffer.copyToChannel(x, 0, -1)').throw('IndexSizeError');
}, '4: buffer.copyToChannel(x, 0, -1)').throw(DOMException, 'IndexSizeError');
should(
() => {
buffer.copyToChannel(x, 0, bufferLength);
},
'5: buffer.copyToChannel(x, 0, ' + bufferLength + ')')
.throw('IndexSizeError');
.throw(DOMException, 'IndexSizeError');

should(() => {
buffer.copyToChannel(x, 3);
}, '6: buffer.copyToChannel(x, 3)').throw('IndexSizeError');
}, '6: buffer.copyToChannel(x, 3)').throw(DOMException, 'IndexSizeError');

if (window.SharedArrayBuffer) {
let shared_buffer = new Float32Array(new SharedArrayBuffer(32));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
buffer.getChannelData(buffer.numberOfChannels);
},
'buffer.getChannelData(' + buffer.numberOfChannels + ')')
.throw('IndexSizeError');
.throw(DOMException, 'IndexSizeError');

let buffer2 = context.createBuffer(1, 1000, 24576);
let expectedDuration = 1000 / 24576;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,31 @@
let buffer = new AudioBuffer(options);
},
'new AudioBuffer(' + JSON.stringify(options) + ')')
.throw('NotSupportedError');
.throw(DOMException, 'NotSupportedError');

options = {numberOfChannels: 99, length: 0, sampleRate: 16000};
should(
() => {
let buffer = new AudioBuffer(options);
},
'new AudioBuffer(' + JSON.stringify(options) + ')')
.throw('NotSupportedError');
.throw(DOMException, 'NotSupportedError');

options = {numberOfChannels: 1, length: 0, sampleRate: 16000};
should(
() => {
let buffer = new AudioBuffer(options);
},
'new AudioBuffer(' + JSON.stringify(options) + ')')
.throw('NotSupportedError');
.throw(DOMException, 'NotSupportedError');

options = {numberOfChannels: 1, length: 1, sampleRate: 100};
should(
() => {
let buffer = new AudioBuffer(options);
},
'new AudioBuffer(' + JSON.stringify(options) + ')')
.throw('NotSupportedError');
.throw(DOMException, 'NotSupportedError');

task.done();
});
Expand Down Expand Up @@ -178,7 +178,7 @@
buffer.getChannelData(options.numberOfChannels);
},
'buffer.getChannelData(' + options.numberOfChannels + ')')
.throw('IndexSizeError');
.throw(DOMException, 'IndexSizeError');

task.done();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
should(function() {
source.buffer =
new AudioBuffer({length: 128, sampleRate: context.sampleRate})
}, 'source.buffer = new buffer').throw('InvalidStateError');
}, 'source.buffer = new buffer').throw(DOMException, 'InvalidStateError');

// The buffer has been set; it's ok to set it to null.
should(function() {
Expand All @@ -58,7 +58,7 @@
// again.
should(function() {
source.buffer = buffer;
}, 'source.buffer = buffer again').throw('InvalidStateError');
}, 'source.buffer = buffer again').throw(DOMException, 'InvalidStateError');

// But setting to null is ok.
should(function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
// does not have the second output, so it should throw.
should(function() {
gain1.connect(gain2, 1).connect(contextA.destination);
}, 'Connecting with an invalid output').throw('IndexSizeError');
}, 'Connecting with an invalid output').throw(DOMException, 'IndexSizeError');

// Test if the second connection throws correctly. The contextB's
// destination is not compatible with the nodes from contextA, thus the
Expand All @@ -124,7 +124,7 @@
gain1.connect(gain2).connect(contextB.destination);
},
'Connecting to a node from the different context')
.throw('InvalidAccessError');
.throw(DOMException, 'InvalidAccessError');

task.done();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,17 @@
// gain1 is not connected to gain3.gain. Exception should be thrown.
should(function() {
gain1.disconnect(gain3.gain);
}, 'gain1.disconnect(gain3.gain)').throw('InvalidAccessError');
}, 'gain1.disconnect(gain3.gain)').throw(DOMException, 'InvalidAccessError');

// When the output index is good but the destination is invalid.
should(function() {
splitter.disconnect(gain1.gain, 1);
}, 'splitter.disconnect(gain1.gain, 1)').throw('InvalidAccessError');
}, 'splitter.disconnect(gain1.gain, 1)').throw(DOMException, 'InvalidAccessError');

// When both arguments are wrong, throw IndexSizeError first.
should(function() {
splitter.disconnect(gain1.gain, 2);
}, 'splitter.disconnect(gain1.gain, 2)').throw('IndexSizeError');
}, 'splitter.disconnect(gain1.gain, 2)').throw(DOMException, 'IndexSizeError');

task.done();
});
Expand Down
Loading

0 comments on commit 91a7ab3

Please sign in to comment.