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.
7 #include "base/compiler_specific.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_piece.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/sounds_manager.h"
17 #include "media/audio/sounds/test_data.h"
18 #include "testing/gtest/include/gtest/gtest.h"
22 class SoundsManagerTest
: public testing::Test
{
24 SoundsManagerTest() {}
25 ~SoundsManagerTest() override
{}
27 void SetUp() override
{
28 audio_manager_
.reset(AudioManager::CreateForTesting());
29 SoundsManager::Create();
32 void TearDown() override
{
33 SoundsManager::Shutdown();
34 audio_manager_
.reset();
37 void SetObserverForTesting(AudioStreamHandler::TestObserver
* observer
) {
38 AudioStreamHandler::SetObserverForTesting(observer
);
41 void SetAudioSourceForTesting(
42 AudioOutputStream::AudioSourceCallback
* source
) {
43 AudioStreamHandler::SetAudioSourceForTesting(source
);
47 scoped_ptr
<AudioManager
> audio_manager_
;
49 base::MessageLoop message_loop_
;
52 TEST_F(SoundsManagerTest
, Play
) {
53 ASSERT_TRUE(SoundsManager::Get());
55 base::RunLoop run_loop
;
56 TestObserver
observer(run_loop
.QuitClosure());
58 SetObserverForTesting(&observer
);
60 ASSERT_TRUE(SoundsManager::Get()->Initialize(
62 base::StringPiece(kTestAudioData
, arraysize(kTestAudioData
))));
64 SoundsManager::Get()->GetDuration(kTestAudioKey
).InMicroseconds());
65 ASSERT_TRUE(SoundsManager::Get()->Play(kTestAudioKey
));
68 ASSERT_EQ(1, observer
.num_play_requests());
69 ASSERT_EQ(1, observer
.num_stop_requests());
70 ASSERT_EQ(4, observer
.cursor());
72 SetObserverForTesting(NULL
);
75 TEST_F(SoundsManagerTest
, Stop
) {
76 ASSERT_TRUE(SoundsManager::Get());
78 base::RunLoop run_loop
;
79 TestObserver
observer(run_loop
.QuitClosure());
81 SetObserverForTesting(&observer
);
83 ASSERT_TRUE(SoundsManager::Get()->Initialize(
85 base::StringPiece(kTestAudioData
, arraysize(kTestAudioData
))));
87 // This overrides the wav data set by kTestAudioData and results in
88 // a never-ending sine wave being played.
89 const int kChannels
= 1;
90 const double kFreq
= 200;
91 const double kSampleFreq
= 44100;
92 SineWaveAudioSource
sine_source(kChannels
, kFreq
, kSampleFreq
);
93 SetAudioSourceForTesting(&sine_source
);
95 ASSERT_EQ(0, observer
.num_play_requests());
96 ASSERT_EQ(0, observer
.num_stop_requests());
98 ASSERT_TRUE(SoundsManager::Get()->Play(kTestAudioKey
));
99 ASSERT_TRUE(SoundsManager::Get()->Stop(kTestAudioKey
));
102 ASSERT_EQ(1, observer
.num_play_requests());
103 ASSERT_EQ(1, observer
.num_stop_requests());
105 SetObserverForTesting(NULL
);
108 TEST_F(SoundsManagerTest
, Uninitialized
) {
109 ASSERT_TRUE(SoundsManager::Get());
110 ASSERT_FALSE(SoundsManager::Get()->Play(kTestAudioKey
));
111 ASSERT_FALSE(SoundsManager::Get()->Stop(kTestAudioKey
));