Index: third_party/WebKit/Source/modules/webaudio/AudioContextTest.cpp |
diff --git a/third_party/WebKit/Source/modules/webaudio/AudioContextTest.cpp b/third_party/WebKit/Source/modules/webaudio/AudioContextTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..907bb360fe75ac4f41ff8aadda234da7b276b9d2 |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/webaudio/AudioContextTest.cpp |
@@ -0,0 +1,96 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "modules/webaudio/AudioContext.h" |
+ |
+#include "core/dom/Document.h" |
+#include "core/testing/DummyPageHolder.h" |
+#include "platform/testing/TestingPlatformSupport.h" |
+#include "public/platform/WebAudioDevice.h" |
+#include "public/platform/WebAudioLatencyHint.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace blink { |
+ |
+namespace { |
+ |
+class MockWebAudioDevice : public WebAudioDevice { |
+ public: |
+ explicit MockWebAudioDevice(double sampleRate, int framesPerBuffer) |
+ : m_sampleRate(sampleRate), m_framesPerBuffer(framesPerBuffer) {} |
+ ~MockWebAudioDevice() override = default; |
+ |
+ void start() override {} |
+ void stop() override {} |
+ double sampleRate() override { return m_sampleRate; } |
+ int framesPerBuffer() override { return m_framesPerBuffer; } |
+ |
+ private: |
+ double m_sampleRate; |
+ int m_framesPerBuffer; |
+}; |
+ |
+class AudioContextTestPlatform : public TestingPlatformSupport { |
+ public: |
+ WebAudioDevice* createAudioDevice(unsigned numberOfInputChannels, |
+ unsigned numberOfChannels, |
+ const WebAudioLatencyHint& latencyHint, |
+ WebAudioDevice::RenderCallback*, |
+ const WebString& deviceId, |
+ const WebSecurityOrigin&) override { |
+ double bufferSize = 0; |
+ switch (latencyHint.category()) { |
+ case WebAudioLatencyHint::CategoryInteractive: |
+ bufferSize = audioHardwareBufferSize(); |
+ break; |
+ case WebAudioLatencyHint::CategoryBalanced: |
+ bufferSize = audioHardwareBufferSize() * 2; |
+ break; |
+ case WebAudioLatencyHint::CategoryPlayback: |
+ bufferSize = audioHardwareBufferSize() * 4; |
+ break; |
+ default: |
+ NOTREACHED(); |
+ break; |
+ } |
+ |
+ return new MockWebAudioDevice(audioHardwareSampleRate(), bufferSize); |
+ } |
+ |
+ double audioHardwareSampleRate() override { return 44100; } |
+ size_t audioHardwareBufferSize() override { return 128; } |
+}; |
+ |
+} // anonymous namespace |
+ |
+class AudioContextTest : public ::testing::Test { |
+ protected: |
+ void SetUp() override { m_dummyPageHolder = DummyPageHolder::create(); } |
+ |
+ void TearDown() override {} |
+ |
+ Document& document() { return m_dummyPageHolder->document(); } |
+ |
+ private: |
+ std::unique_ptr<DummyPageHolder> m_dummyPageHolder; |
+ AudioContextTestPlatform m_testPlatform; |
+}; |
+ |
+TEST_F(AudioContextTest, AudioContextOptions_WebAudioLatencyHint) { |
+ AudioContext* interactiveContext = AudioContext::create( |
+ document(), WebAudioLatencyHint(WebAudioLatencyHint::CategoryInteractive), |
+ ASSERT_NO_EXCEPTION); |
+ |
+ AudioContext* balancedContext = AudioContext::create( |
+ document(), WebAudioLatencyHint(WebAudioLatencyHint::CategoryBalanced), |
+ ASSERT_NO_EXCEPTION); |
+ EXPECT_GT(balancedContext->baseLatency(), interactiveContext->baseLatency()); |
+ |
+ AudioContext* playbackContext = AudioContext::create( |
+ document(), WebAudioLatencyHint(WebAudioLatencyHint::CategoryPlayback), |
+ ASSERT_NO_EXCEPTION); |
+ EXPECT_GT(playbackContext->baseLatency(), balancedContext->baseLatency()); |
+} |
+ |
+} // namespace blink |