Index: LayoutTests/webaudio/resources/audio-testing.js |
diff --git a/LayoutTests/webaudio/resources/audio-testing.js b/LayoutTests/webaudio/resources/audio-testing.js |
index a1b86494b60d6040c115f70b209973a05212a060..e931793270966a2c27c8eb645018edbf40bc357e 100644 |
--- a/LayoutTests/webaudio/resources/audio-testing.js |
+++ b/LayoutTests/webaudio/resources/audio-testing.js |
@@ -175,22 +175,6 @@ function isValidNumber(x) { |
return !isNaN(x) && (x != Infinity) && (x != -Infinity); |
} |
-function shouldThrowTypeError(func, text) { |
- var ok = false; |
- try { |
- func(); |
- } catch (e) { |
- if (e instanceof TypeError) { |
- ok = true; |
- } |
- } |
- if (ok) { |
- testPassed(text + " threw TypeError."); |
- } else { |
- testFailed(text + " should throw TypeError."); |
- } |
-} |
- |
// |Audit| is a task runner for web audio test. It makes asynchronous web audio |
// testing simple and manageable. |
// |
@@ -255,4 +239,78 @@ function shouldThrowTypeError(func, text) { |
} |
}; |
-})(); |
+})(); |
+ |
+ |
+// Create an AudioBuffer for test verification. Fill an incremental index value |
+// into the each channel in the buffer. The channel index is between 1 and |
+// |numChannels|. For example, a 4-channel buffer created by this function will |
+// contain values 1, 2, 3 and 4 for each channel respectively. |
+function createTestingBuffer(context, numChannels, length) { |
+ var buffer = context.createBuffer(numChannels, length, context.sampleRate); |
+ for (var i = 1; i <= numChannels; i++) { |
+ var data = buffer.getChannelData(i-1); |
+ for (var j = 0; j < data.length; j++) { |
+ // Storing channel index into the channel buffer. |
+ data[j] = i; |
+ } |
+ } |
+ return buffer; |
+} |
+ |
+// Returns a truncated string out of a function. This is to generate a brief |
+// description of function when throwing an error/exception message. |
+function getTaskSummary(func) { |
+ var content = func.toString().replace(/(\s|\t|\r\n|\n|\r)/gm, ''); |
Raymond Toy
2015/02/12 19:45:53
Is this going to do something sensible if the func
hongchan
2015/02/13 01:16:57
It works well with the single quote and Google JS
Raymond Toy
2015/02/13 17:16:44
That's going to be hard for me to get used to (sin
|
+ return '"' + content.slice(11, -2) + '"'; |
Raymond Toy
2015/02/12 19:45:53
What if the function is many, many lines long? Do
hongchan
2015/02/13 01:16:57
I can't cover the multiline function. Would you ra
Raymond Toy
2015/02/13 17:16:44
Multiline is ok. I'm not sure I like squashing al
|
+} |
+ |
+// The name space for |Should| test utility. Dependencies: testPassed(), |
+// testFailed() from resources/js-test.js |
+var Should = {}; |
+ |
+// Expect an exception to be thrown with a certain type. |
+Should.throwWithType = function (type, func) { |
+ var summary = getTaskSummary(func); |
+ try { |
+ func(); |
+ testFailed(summary + ' should throw ' + type + '.'); |
+ } catch (e) { |
+ if (e.name === type) |
+ testPassed(summary + ' threw exception ' + e.name + '.'); |
+ else |
+ testFailed(summary + ' should throw ' + type + '. Threw exception ' + e.name + '.'); |
+ } |
+}; |
+ |
+// Expect not to throw an exception. |
+Should.notThrow = function (func) { |
+ var summary = getTaskSummary(func); |
+ try { |
+ func(); |
+ testPassed(summary + ' did not throw exception.'); |
+ } catch (e) { |
+ testFailed(summary + ' should not throw exception. Threw exception ' + e.name + '.'); |
+ } |
+}; |
+ |
+ |
+// Verify if the channelData array contains a single constant |value|. |
+Should.haveValueInChannel = function (value, channelData) { |
+ var mistmatch = {}; |
Raymond Toy
2015/02/12 19:45:53
Typo: mistmatch -> mismatch
hongchan
2015/02/13 01:16:57
Oops. Fixing it.
|
+ for (var i = 0; i < channelData.length; i++) { |
+ if (channelData[i] !== value) { |
+ mistmatch[i] = value; |
+ } |
+ } |
+ |
+ var numberOfmistmatches = Object.keys(mistmatch).length; |
+ if (numberOfmistmatches === 0) { |
+ testPassed('ChannelData has expected values (' + value + ').'); |
+ } else { |
+ testFailed(numberOfmistmatches + ' values in ChannelData are not equal to ' + value + ':'); |
+ for (var index in mistmatch) { |
+ console.log('[' + index + '] : ' + mistmatch[index]); |
+ } |
+ } |
+}; |