Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: media/audio/linux/cras_unified_unittest.cc

Issue 11959018: Add a unified audio I/O backend for ChromeOS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include <string>
6
7 #include "base/synchronization/waitable_event.h"
8 #include "base/test/test_timeouts.h"
9 #include "base/time.h"
10 #include "media/audio/linux/audio_manager_linux.h"
11 #include "media/audio/linux/cras_unified.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 using testing::_;
16 using testing::DoAll;
17 using testing::InvokeWithoutArgs;
18 using testing::Return;
19 using testing::SetArgumentPointee;
20 using testing::StrictMock;
21
22 namespace media {
23
24 class MockAudioSourceCallback : public AudioOutputStream::AudioSourceCallback {
25 public:
26 MOCK_METHOD2(OnMoreData, int(AudioBus* audio_bus,
27 AudioBuffersState buffers_state));
28 MOCK_METHOD3(OnMoreIOData, int(AudioBus* source,
29 AudioBus* dest,
30 AudioBuffersState buffers_state));
31 MOCK_METHOD2(OnError, void(AudioOutputStream* stream, int code));
32 };
33
34 class MockAudioManagerLinux : public AudioManagerLinux {
35 public:
36 MOCK_METHOD0(Init, void());
37 MOCK_METHOD0(HasAudioOutputDevices, bool());
38 MOCK_METHOD0(HasAudioInputDevices, bool());
39 MOCK_METHOD1(MakeLinearOutputStream, AudioOutputStream*(
40 const AudioParameters& params));
41 MOCK_METHOD1(MakeLowLatencyOutputStream, AudioOutputStream*(
42 const AudioParameters& params));
43 MOCK_METHOD2(MakeLinearOutputStream, AudioInputStream*(
44 const AudioParameters& params, const std::string& device_id));
45 MOCK_METHOD2(MakeLowLatencyInputStream, AudioInputStream*(
46 const AudioParameters& params, const std::string& device_id));
47
48 // We need to override this function in order to skip the checking the number
49 // of active output streams. It is because the number of active streams
50 // is managed inside MakeAudioOutputStream, and we don't use
51 // MakeAudioOutputStream to create the stream in the tests.
52 virtual void ReleaseOutputStream(AudioOutputStream* stream) OVERRIDE {
53 DCHECK(stream);
54 delete stream;
55 }
56
57 // We don't mock this method since all tests will do the same thing
58 // and use the current message loop.
59 virtual scoped_refptr<base::MessageLoopProxy> GetMessageLoop() OVERRIDE {
60 return MessageLoop::current()->message_loop_proxy();
61 }
62 };
63
64 class CrasUnifiedStreamTest : public testing::Test {
65 protected:
66 CrasUnifiedStreamTest() {
67 mock_manager_.reset(new StrictMock<MockAudioManagerLinux>());
68 }
69
70 virtual ~CrasUnifiedStreamTest() {
71 }
72
73 CrasUnifiedStream* CreateStream(ChannelLayout layout) {
74 return CreateStream(layout, kTestFramesPerPacket);
75 }
76
77 CrasUnifiedStream* CreateStream(ChannelLayout layout,
78 int32 samples_per_packet) {
79 AudioParameters params(kTestFormat, layout, kTestSampleRate,
80 kTestBitsPerSample, samples_per_packet);
81 return new CrasUnifiedStream(params,
82 mock_manager_.get());
83 }
84
85 MockAudioManagerLinux& mock_manager() {
86 return *(mock_manager_.get());
87 }
88
89 static const ChannelLayout kTestChannelLayout;
90 static const int kTestSampleRate;
91 static const int kTestBitsPerSample;
92 static const AudioParameters::Format kTestFormat;
93 static const uint32 kTestFramesPerPacket;
94
95 scoped_ptr<StrictMock<MockAudioManagerLinux> > mock_manager_;
96
97 private:
98 DISALLOW_COPY_AND_ASSIGN(CrasUnifiedStreamTest);
99 };
100
101 const ChannelLayout CrasUnifiedStreamTest::kTestChannelLayout =
102 CHANNEL_LAYOUT_STEREO;
103 const int CrasUnifiedStreamTest::kTestSampleRate =
104 AudioParameters::kAudioCDSampleRate;
105 const int CrasUnifiedStreamTest::kTestBitsPerSample = 16;
106 const AudioParameters::Format CrasUnifiedStreamTest::kTestFormat =
107 AudioParameters::AUDIO_PCM_LINEAR;
108 const uint32 CrasUnifiedStreamTest::kTestFramesPerPacket = 1000;
109
110 TEST_F(CrasUnifiedStreamTest, ConstructedState) {
111 // Should support mono.
112 CrasUnifiedStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO);
113 test_stream->Close();
114
115 // Should support stereo.
116 test_stream = CreateStream(CHANNEL_LAYOUT_SURROUND);
117 EXPECT_TRUE(test_stream->Open());
118 test_stream->Close();
119
120 // Bad bits per sample.
121 AudioParameters bad_bps_params(kTestFormat, kTestChannelLayout,
122 kTestSampleRate, kTestBitsPerSample - 1,
123 kTestFramesPerPacket);
124 test_stream = new CrasUnifiedStream(bad_bps_params, mock_manager_.get());
125 EXPECT_FALSE(test_stream->Open());
126 test_stream->Close();
127
128 // Bad sample rate.
129 AudioParameters bad_rate_params(kTestFormat, kTestChannelLayout,
130 0, kTestBitsPerSample, kTestFramesPerPacket);
131 test_stream = new CrasUnifiedStream(bad_rate_params, mock_manager_.get());
132 EXPECT_FALSE(test_stream->Open());
133 test_stream->Close();
134
135 // Check that Mono works too.
136 test_stream = CreateStream(CHANNEL_LAYOUT_MONO);
137 ASSERT_TRUE(test_stream->Open());
138 test_stream->Close();
139 }
140
141 TEST_F(CrasUnifiedStreamTest, RenderFrames) {
142 CrasUnifiedStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO);
143 MockAudioSourceCallback mock_callback;
144
145 ASSERT_TRUE(test_stream->Open());
146
147 base::WaitableEvent event(false, false);
148
149 EXPECT_CALL(mock_callback, OnMoreIOData(_, _, _))
150 .WillOnce(DoAll(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal),
151 Return(kTestFramesPerPacket)));
152
153 test_stream->Start(&mock_callback);
154
155 // Wait for samples to be captured.
156 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout()));
157
158 test_stream->Stop();
159
160 test_stream->Close();
161 }
162
163 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698