OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "media/audio/audio_io.h" | 12 #include "media/audio/audio_io.h" |
13 #include "media/audio/audio_manager.h" | 13 #include "media/audio/audio_manager.h" |
14 #include "media/audio/simple_sources.h" | 14 #include "media/audio/simple_sources.h" |
15 #include "media/audio/sounds/audio_stream_handler.h" | 15 #include "media/audio/sounds/audio_stream_handler.h" |
16 #include "media/audio/sounds/test_data.h" | 16 #include "media/audio/sounds/test_data.h" |
17 #include "media/audio/sounds/wav_reader.h" | |
17 #include "media/base/channel_layout.h" | 18 #include "media/base/channel_layout.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace media { | 21 namespace media { |
21 | 22 |
22 class AudioStreamHandlerTest : public testing::Test { | 23 class AudioStreamHandlerTest : public testing::Test { |
23 public: | 24 public: |
24 AudioStreamHandlerTest() {} | 25 AudioStreamHandlerTest() {} |
25 virtual ~AudioStreamHandlerTest() {} | 26 virtual ~AudioStreamHandlerTest() {} |
26 | 27 |
(...skipping 15 matching lines...) Expand all Loading... | |
42 | 43 |
43 void SetObserverForTesting(AudioStreamHandler::TestObserver* observer) { | 44 void SetObserverForTesting(AudioStreamHandler::TestObserver* observer) { |
44 AudioStreamHandler::SetObserverForTesting(observer); | 45 AudioStreamHandler::SetObserverForTesting(observer); |
45 } | 46 } |
46 | 47 |
47 void SetAudioSourceForTesting( | 48 void SetAudioSourceForTesting( |
48 AudioOutputStream::AudioSourceCallback* source) { | 49 AudioOutputStream::AudioSourceCallback* source) { |
49 AudioStreamHandler::SetAudioSourceForTesting(source); | 50 AudioStreamHandler::SetAudioSourceForTesting(source); |
50 } | 51 } |
51 | 52 |
53 void SetWavReaderForTesting(WavReader* reader) { | |
54 audio_stream_handler_->SetWavReaderForTesting(reader); | |
55 } | |
56 | |
57 void AllowReplayOnceForTesting() { | |
58 audio_stream_handler_->AllowReplayOnceForTesting(); | |
59 } | |
60 | |
52 private: | 61 private: |
53 scoped_ptr<AudioManager> audio_manager_; | 62 scoped_ptr<AudioManager> audio_manager_; |
54 scoped_ptr<AudioStreamHandler> audio_stream_handler_; | 63 scoped_ptr<AudioStreamHandler> audio_stream_handler_; |
55 | 64 |
56 base::MessageLoop message_loop_; | 65 base::MessageLoop message_loop_; |
57 }; | 66 }; |
58 | 67 |
59 TEST_F(AudioStreamHandlerTest, Play) { | 68 TEST_F(AudioStreamHandlerTest, Play) { |
60 base::RunLoop run_loop; | 69 base::RunLoop run_loop; |
61 TestObserver observer(run_loop.QuitClosure()); | 70 TestObserver observer(run_loop.QuitClosure()); |
62 | 71 |
63 SetObserverForTesting(&observer); | 72 SetObserverForTesting(&observer); |
64 | 73 |
65 ASSERT_TRUE(audio_stream_handler()->IsInitialized()); | 74 ASSERT_TRUE(audio_stream_handler()->IsInitialized()); |
66 ASSERT_TRUE(audio_stream_handler()->Play()); | 75 ASSERT_TRUE(audio_stream_handler()->Play()); |
67 | 76 |
68 run_loop.Run(); | 77 run_loop.Run(); |
69 | 78 |
70 SetObserverForTesting(NULL); | 79 SetObserverForTesting(NULL); |
71 | 80 |
72 ASSERT_EQ(1, observer.num_play_requests()); | 81 ASSERT_EQ(1, observer.num_play_requests()); |
73 ASSERT_EQ(1, observer.num_stop_requests()); | 82 ASSERT_EQ(1, observer.num_stop_requests()); |
74 ASSERT_EQ(4, observer.cursor()); | 83 ASSERT_EQ(4, observer.cursor()); |
75 } | 84 } |
76 | 85 |
77 TEST_F(AudioStreamHandlerTest, Rewind) { | 86 TEST_F(AudioStreamHandlerTest, Replay) { |
87 // As we're trying to test replay logic, "infinite" sound is played. | |
88 // "Infinite" sound is simulated by TestWavReader and, after 1 | |
89 // second, end of this sound (which is necessary for replay) is | |
90 // simulated. | |
91 | |
78 base::RunLoop run_loop; | 92 base::RunLoop run_loop; |
79 TestObserver observer(run_loop.QuitClosure()); | 93 TestObserver observer(run_loop.QuitClosure()); |
80 SineWaveAudioSource source(CHANNEL_LAYOUT_STEREO, 200.0, 8000); | 94 TestWavReader* reader = |
81 | 95 new TestWavReader(audio_stream_handler()->wav_parser()); |
DaleCurtis
2013/12/17 19:28:52
scoped_ptr?
ygorshenin1
2013/12/18 14:33:12
I'm not sure that smart pointers are helpful in th
| |
82 SetObserverForTesting(&observer); | 96 SetObserverForTesting(&observer); |
83 SetAudioSourceForTesting(&source); | 97 SetWavReaderForTesting(reader); |
84 | 98 |
85 ASSERT_TRUE(audio_stream_handler()->IsInitialized()); | 99 ASSERT_TRUE(audio_stream_handler()->IsInitialized()); |
86 | 100 |
87 ASSERT_TRUE(audio_stream_handler()->Play()); | 101 ASSERT_TRUE(audio_stream_handler()->Play()); |
102 | |
103 AllowReplayOnceForTesting(); | |
104 ASSERT_TRUE(audio_stream_handler()->Play()); | |
105 | |
88 base::MessageLoop::current()->PostDelayedTask( | 106 base::MessageLoop::current()->PostDelayedTask( |
89 FROM_HERE, | 107 FROM_HERE, |
90 base::Bind(base::IgnoreResult(&AudioStreamHandler::Play), | 108 base::Bind(&TestWavReader::SimulateAtEndOnce, |
91 base::Unretained(audio_stream_handler())), | 109 base::Unretained(reader)), |
92 base::TimeDelta::FromSeconds(3)); | 110 base::TimeDelta::FromSeconds(1)); |
93 base::MessageLoop::current()->PostDelayedTask( | 111 base::MessageLoop::current()->PostDelayedTask( |
94 FROM_HERE, | 112 FROM_HERE, |
95 base::Bind(&AudioStreamHandler::Stop, | 113 base::Bind(&AudioStreamHandler::Stop, |
96 base::Unretained(audio_stream_handler())), | 114 base::Unretained(audio_stream_handler())), |
97 base::TimeDelta::FromSeconds(6)); | 115 base::TimeDelta::FromSeconds(2)); |
98 | 116 |
99 run_loop.Run(); | 117 run_loop.Run(); |
100 | 118 |
101 SetObserverForTesting(NULL); | 119 SetObserverForTesting(NULL); |
102 SetAudioSourceForTesting(NULL); | 120 SetAudioSourceForTesting(NULL); |
103 | 121 |
104 ASSERT_EQ(2, observer.num_play_requests()); | 122 ASSERT_EQ(1, observer.num_play_requests()); |
123 ASSERT_EQ(1, observer.num_replays()); | |
105 ASSERT_EQ(1, observer.num_stop_requests()); | 124 ASSERT_EQ(1, observer.num_stop_requests()); |
106 } | 125 } |
107 | 126 |
108 } // namespace media | 127 } // namespace media |
OLD | NEW |