• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "base/basictypes.h"
6 #include "base/bind.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"
19 
20 namespace media {
21 
22 class AudioStreamHandlerTest : public testing::Test {
23  public:
AudioStreamHandlerTest()24   AudioStreamHandlerTest() {}
~AudioStreamHandlerTest()25   virtual ~AudioStreamHandlerTest() {}
26 
SetUp()27   virtual void SetUp() OVERRIDE {
28     audio_manager_.reset(AudioManager::CreateForTesting());
29 
30     base::StringPiece data(kTestAudioData, arraysize(kTestAudioData));
31     audio_stream_handler_.reset(new AudioStreamHandler(data));
32   }
33 
TearDown()34   virtual void TearDown() OVERRIDE {
35     audio_stream_handler_.reset();
36     audio_manager_.reset();
37   }
38 
audio_stream_handler()39   AudioStreamHandler* audio_stream_handler() {
40     return audio_stream_handler_.get();
41   }
42 
SetObserverForTesting(AudioStreamHandler::TestObserver * observer)43   void SetObserverForTesting(AudioStreamHandler::TestObserver* observer) {
44     AudioStreamHandler::SetObserverForTesting(observer);
45   }
46 
SetAudioSourceForTesting(AudioOutputStream::AudioSourceCallback * source)47   void SetAudioSourceForTesting(
48       AudioOutputStream::AudioSourceCallback* source) {
49     AudioStreamHandler::SetAudioSourceForTesting(source);
50   }
51 
52  private:
53   scoped_ptr<AudioManager> audio_manager_;
54   scoped_ptr<AudioStreamHandler> audio_stream_handler_;
55 
56   base::MessageLoop message_loop_;
57 };
58 
TEST_F(AudioStreamHandlerTest,Play)59 TEST_F(AudioStreamHandlerTest, Play) {
60   base::RunLoop run_loop;
61   TestObserver observer(run_loop.QuitClosure());
62 
63   SetObserverForTesting(&observer);
64 
65   ASSERT_TRUE(audio_stream_handler()->IsInitialized());
66   ASSERT_TRUE(audio_stream_handler()->Play());
67 
68   run_loop.Run();
69 
70   SetObserverForTesting(NULL);
71 
72   ASSERT_EQ(1, observer.num_play_requests());
73   ASSERT_EQ(1, observer.num_stop_requests());
74   ASSERT_EQ(4, observer.cursor());
75 }
76 
TEST_F(AudioStreamHandlerTest,ConsecutivePlayRequests)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);
81 
82   SetObserverForTesting(&observer);
83   SetAudioSourceForTesting(&source);
84 
85   ASSERT_TRUE(audio_stream_handler()->IsInitialized());
86 
87   ASSERT_TRUE(audio_stream_handler()->Play());
88   base::MessageLoop::current()->PostDelayedTask(
89       FROM_HERE,
90       base::Bind(base::IgnoreResult(&AudioStreamHandler::Play),
91                  base::Unretained(audio_stream_handler())),
92       base::TimeDelta::FromSeconds(1));
93   base::MessageLoop::current()->PostDelayedTask(
94       FROM_HERE,
95       base::Bind(&AudioStreamHandler::Stop,
96                  base::Unretained(audio_stream_handler())),
97       base::TimeDelta::FromSeconds(2));
98 
99   run_loop.Run();
100 
101   SetObserverForTesting(NULL);
102   SetAudioSourceForTesting(NULL);
103 
104   ASSERT_EQ(1, observer.num_play_requests());
105   ASSERT_EQ(1, observer.num_stop_requests());
106 }
107 
108 }  // namespace media
109