Check that buffer exists before trying to get its duration.

Add a simple test for this as well.

BUG=

Review URL: https://codereview.chromium.org/50363005

git-svn-id: svn://svn.chromium.org/blink/trunk@161005 bbb929c8-8fbe-4397-9dbb-9b2b20218538
diff --git a/LayoutTests/webaudio/dom-exceptions-expected.txt b/LayoutTests/webaudio/dom-exceptions-expected.txt
index 6d2b172..604f309 100644
--- a/LayoutTests/webaudio/dom-exceptions-expected.txt
+++ b/LayoutTests/webaudio/dom-exceptions-expected.txt
@@ -46,6 +46,10 @@
 PASS new webkitOfflineAudioContext(1, 100, 1e6) threw exception SyntaxError: Failed to construct 'OfflineAudioContext': sample rate (1.00000e+6) must be in the range 44100-96000 Hz..
 PASS node.oversample = '9x' threw exception InvalidStateError: Failed to set the 'oversample' property on 'WaveShaperNode': invalid oversample '9x': must be 'none', '2x', or '4x'..
 PASS source = context.createBufferSource() did not throw exception.
+PASS source.buffer = buffer did not throw exception.
+PASS source.start() did not throw exception.
+PASS source.stop() did not throw exception.
+PASS source = context.createBufferSource() did not throw exception.
 PASS source.start() did not throw exception.
 PASS source.stop() did not throw exception.
 PASS source = context.createOscillator() did not throw exception.
diff --git a/LayoutTests/webaudio/dom-exceptions.html b/LayoutTests/webaudio/dom-exceptions.html
index 2725a70..f9b1e84 100644
--- a/LayoutTests/webaudio/dom-exceptions.html
+++ b/LayoutTests/webaudio/dom-exceptions.html
@@ -121,7 +121,12 @@
     // Start/stop for AudioBufferSourceNodes
     buffer = context.createBuffer(1,1, context.sampleRate);
     shouldNotThrow("source = context.createBufferSource()");
-    source.buffer = buffer;
+    shouldNotThrow("source.buffer = buffer");
+    shouldNotThrow("source.start()");
+    shouldNotThrow("source.stop()");
+
+    // It's valid to start a source that has no associated buffer.
+    shouldNotThrow("source = context.createBufferSource()");
     shouldNotThrow("source.start()");
     shouldNotThrow("source.stop()");
 
diff --git a/Source/modules/webaudio/AudioBufferSourceNode.cpp b/Source/modules/webaudio/AudioBufferSourceNode.cpp
index 331d300..8756212 100644
--- a/Source/modules/webaudio/AudioBufferSourceNode.cpp
+++ b/Source/modules/webaudio/AudioBufferSourceNode.cpp
@@ -383,12 +383,12 @@
 
 void AudioBufferSourceNode::start(double when)
 {
-    startPlaying(false, when, 0, buffer()->duration());
+    startPlaying(false, when, 0, buffer() ? buffer()->duration() : 0);
 }
 
 void AudioBufferSourceNode::start(double when, double grainOffset)
 {
-    startPlaying(true, when, grainOffset, buffer()->duration());
+    startPlaying(true, when, grainOffset, buffer() ? buffer()->duration() : 0);
 }
 
 void AudioBufferSourceNode::start(double when, double grainOffset, double grainDuration)