1 // Copyright 2013 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/basictypes.h"
7 #include "base/bind_helpers.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "media/audio/audio_io.h"
13 #include "media/audio/audio_manager.h"
14 #include "media/audio/simple_sources.h"
15 #include "media/audio/sounds/audio_stream_handler.h"
16 #include "media/audio/sounds/test_data.h"
17 #include "media/base/channel_layout.h"
18 #include "testing/gtest/include/gtest/gtest.h"
22 class AudioStreamHandlerTest
: public testing::Test
{
24 AudioStreamHandlerTest() {}
25 ~AudioStreamHandlerTest() override
{}
27 void SetUp() override
{
28 audio_manager_
.reset(AudioManager::CreateForTesting());
30 base::StringPiece
data(kTestAudioData
, arraysize(kTestAudioData
));
31 audio_stream_handler_
.reset(new AudioStreamHandler(data
));
34 void TearDown() override
{
35 audio_stream_handler_
.reset();
36 audio_manager_
.reset();
39 AudioStreamHandler
* audio_stream_handler() {
40 return audio_stream_handler_
.get();
43 void SetObserverForTesting(AudioStreamHandler::TestObserver
* observer
) {
44 AudioStreamHandler::SetObserverForTesting(observer
);
47 void SetAudioSourceForTesting(
48 AudioOutputStream::AudioSourceCallback
* source
) {
49 AudioStreamHandler::SetAudioSourceForTesting(source
);
53 scoped_ptr
<AudioManager
> audio_manager_
;
54 scoped_ptr
<AudioStreamHandler
> audio_stream_handler_
;
56 base::MessageLoop message_loop_
;
59 TEST_F(AudioStreamHandlerTest
, Play
) {
60 base::RunLoop run_loop
;
61 TestObserver
observer(run_loop
.QuitClosure());
63 SetObserverForTesting(&observer
);
65 ASSERT_TRUE(audio_stream_handler()->IsInitialized());
66 ASSERT_TRUE(audio_stream_handler()->Play());
70 SetObserverForTesting(NULL
);
72 ASSERT_EQ(1, observer
.num_play_requests());
73 ASSERT_EQ(1, observer
.num_stop_requests());
74 ASSERT_EQ(4, observer
.cursor());
77 TEST_F(AudioStreamHandlerTest
, ConsecutivePlayRequests
) {
78 base::RunLoop run_loop
;
79 TestObserver
observer(run_loop
.QuitClosure());
80 SineWaveAudioSource
source(CHANNEL_LAYOUT_STEREO
, 200.0, 8000);
82 SetObserverForTesting(&observer
);
83 SetAudioSourceForTesting(&source
);
85 ASSERT_TRUE(audio_stream_handler()->IsInitialized());
87 ASSERT_TRUE(audio_stream_handler()->Play());
88 base::MessageLoop::current()->PostDelayedTask(
90 base::Bind(base::IgnoreResult(&AudioStreamHandler::Play
),
91 base::Unretained(audio_stream_handler())),
92 base::TimeDelta::FromSeconds(1));
93 base::MessageLoop::current()->PostDelayedTask(
95 base::Bind(&AudioStreamHandler::Stop
,
96 base::Unretained(audio_stream_handler())),
97 base::TimeDelta::FromSeconds(2));
101 SetObserverForTesting(NULL
);
102 SetAudioSourceForTesting(NULL
);
104 ASSERT_EQ(1, observer
.num_play_requests());
105 ASSERT_EQ(1, observer
.num_stop_requests());