Update V8 to version 4.7.56.
[chromium-blink-merge.git] / media / base / audio_renderer_mixer_input_unittest.cc
blobbe03867027f0963bc38fca13eb386827948527d7
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/bind_helpers.h"
7 #include "media/base/audio_renderer_mixer.h"
8 #include "media/base/audio_renderer_mixer_input.h"
9 #include "media/base/fake_audio_render_callback.h"
10 #include "media/base/mock_audio_renderer_sink.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace media {
16 static const int kBitsPerChannel = 16;
17 static const int kSampleRate = 48000;
18 static const int kBufferSize = 8192;
19 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
21 class AudioRendererMixerInputTest : public testing::Test {
22 public:
23 AudioRendererMixerInputTest() {
24 audio_parameters_ = AudioParameters(
25 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate,
26 kBitsPerChannel, kBufferSize);
28 CreateMixerInput();
29 fake_callback_.reset(new FakeAudioRenderCallback(0));
30 mixer_input_->Initialize(audio_parameters_, fake_callback_.get());
31 audio_bus_ = AudioBus::Create(audio_parameters_);
34 void CreateMixerInput() {
35 mixer_input_ = new AudioRendererMixerInput(
36 base::Bind(
37 &AudioRendererMixerInputTest::GetMixer, base::Unretained(this)),
38 base::Bind(
39 &AudioRendererMixerInputTest::RemoveMixer, base::Unretained(this)));
42 AudioRendererMixer* GetMixer(const AudioParameters& params) {
43 if (!mixer_) {
44 scoped_refptr<MockAudioRendererSink> sink = new MockAudioRendererSink();
45 EXPECT_CALL(*sink.get(), Start());
46 EXPECT_CALL(*sink.get(), Stop());
48 mixer_.reset(new AudioRendererMixer(
49 audio_parameters_, audio_parameters_, sink));
51 EXPECT_CALL(*this, RemoveMixer(testing::_));
52 return mixer_.get();
55 double ProvideInput() {
56 return mixer_input_->ProvideInput(audio_bus_.get(), base::TimeDelta());
59 MOCK_METHOD1(RemoveMixer, void(const AudioParameters&));
61 protected:
62 virtual ~AudioRendererMixerInputTest() {}
64 AudioParameters audio_parameters_;
65 scoped_ptr<AudioRendererMixer> mixer_;
66 scoped_refptr<AudioRendererMixerInput> mixer_input_;
67 scoped_ptr<FakeAudioRenderCallback> fake_callback_;
68 scoped_ptr<AudioBus> audio_bus_;
70 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerInputTest);
73 // Test that getting and setting the volume work as expected. The volume is
74 // returned from ProvideInput() only when playing.
75 TEST_F(AudioRendererMixerInputTest, GetSetVolume) {
76 mixer_input_->Start();
77 mixer_input_->Play();
79 // Starting volume should be 1.0.
80 EXPECT_DOUBLE_EQ(ProvideInput(), 1);
82 const double kVolume = 0.5;
83 EXPECT_TRUE(mixer_input_->SetVolume(kVolume));
84 EXPECT_DOUBLE_EQ(ProvideInput(), kVolume);
86 mixer_input_->Stop();
89 // Test Start()/Play()/Pause()/Stop()/playing() all work as expected. Also
90 // implicitly tests that AddMixerInput() and RemoveMixerInput() work without
91 // crashing; functional tests for these methods are in AudioRendererMixerTest.
92 TEST_F(AudioRendererMixerInputTest, StartPlayPauseStopPlaying) {
93 mixer_input_->Start();
94 mixer_input_->Play();
95 EXPECT_DOUBLE_EQ(ProvideInput(), 1);
96 mixer_input_->Pause();
97 mixer_input_->Play();
98 EXPECT_DOUBLE_EQ(ProvideInput(), 1);
99 mixer_input_->Stop();
102 // Test that Stop() can be called before Initialize() and Start().
103 TEST_F(AudioRendererMixerInputTest, StopBeforeInitializeOrStart) {
104 // |mixer_input_| was initialized during construction.
105 mixer_input_->Stop();
107 // Verify Stop() works without Initialize() or Start().
108 CreateMixerInput();
109 mixer_input_->Stop();
112 // Test that Start() can be called after Stop().
113 // TODO(dalecurtis): We shouldn't allow this. See http://crbug.com/151051
114 TEST_F(AudioRendererMixerInputTest, StartAfterStop) {
115 mixer_input_->Stop();
116 mixer_input_->Start();
117 mixer_input_->Stop();
120 } // namespace media