OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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/logging.h" | |
8 #include "base/test/mock_log.h" | |
9 #include "chromecast/media/audio/cast_audio_manager.h" | |
10 #include "chromecast/media/audio/cast_audio_manager_factory.h" | |
11 #include "chromecast/public/cast_audio_output_device.h" | |
12 #include "chromecast/public/cast_audio_stream.h" | |
13 #include "media/audio/audio_manager.h" | |
14 #include "media/audio/fake_audio_log_factory.h" | |
15 #include "testing/gmock/include/gmock/gmock.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace chromecast { | |
19 namespace { | |
20 | |
21 // Makes reading the syntax prettier. | |
22 using testing::_; | |
23 | |
24 // Testing constants. | |
25 const int kTestBitDepth = 16; | |
26 const auto kTestChannelLayout = media::ChannelLayout::CHANNEL_LAYOUT_STEREO; | |
27 const char kTestDeviceId[] = "test_device_id"; | |
28 const int kTestFramesPerBuffer = 1024; | |
29 const int kTestSampleRate = 48000; | |
30 | |
31 // A stubbed-out output stream. | |
32 class FakeCastAudioOutputStream : public CastAudioOutputStream { | |
33 public: | |
34 FakeCastAudioOutputStream() {} | |
35 ~FakeCastAudioOutputStream() override {} | |
36 | |
37 // CastAudioOutputStream implementation: | |
38 bool Open() override { return true; } | |
39 void Start(AudioSourceCallback* callback) override {} | |
40 void Stop() override {} | |
41 void SetVolume(double volume) override {} | |
42 double GetVolume() override { return 0.0; } | |
43 void Close() override {} | |
44 }; | |
45 | |
46 // A fake audio output device. This takes the place of a platform specific | |
47 // implementation. | |
48 class FakeCastAudioOutputDevice : public CastAudioOutputDevice { | |
49 public: | |
50 FakeCastAudioOutputDevice() : null_stream_(false) {} | |
51 ~FakeCastAudioOutputDevice() override {} | |
52 | |
53 // Pass in true to return a nullptr when MakeOutputStream is called. | |
54 void set_null_stream(bool null_stream) { null_stream_ = null_stream; } | |
55 | |
56 // AudioOutputDevice implementation: | |
57 int GetMaximumOutputStreamsAllowed() const override { return 1; } | |
58 | |
59 AudioParameters GetPreferredOutputStreamParameters( | |
60 const AudioParameters& suggested) const override { | |
61 return suggested; | |
62 } | |
63 | |
64 CastAudioOutputStream* MakeOutputStream( | |
65 const AudioParameters& params) override { | |
66 if (null_stream_) | |
67 return nullptr; | |
68 | |
69 // Return a fake stream. | |
70 output_stream_.reset(new FakeCastAudioOutputStream()); | |
71 return output_stream_.get(); | |
72 } | |
73 | |
74 private: | |
75 bool null_stream_; | |
76 scoped_ptr<CastAudioOutputStream> output_stream_; | |
77 }; | |
78 | |
79 // The testbed for this suite of tests. | |
80 class CastAudioManagerTest : public testing::Test { | |
81 protected: | |
82 CastAudioManagerTest() {} | |
83 ~CastAudioManagerTest() override {} | |
84 | |
85 void SetUp() override { | |
86 audio_manager_.reset( | |
87 new CastAudioManager(&log_factory_, &audio_output_device_)); | |
88 } | |
89 | |
90 // Constructor parameters are fakes. | |
91 media::FakeAudioLogFactory log_factory_; | |
92 FakeCastAudioOutputDevice audio_output_device_; | |
93 | |
94 // Class being tested. | |
95 scoped_ptr<CastAudioManager> audio_manager_; | |
96 }; | |
97 | |
98 TEST_F(CastAudioManagerTest, HasAudioOutputDevices) { | |
99 EXPECT_TRUE(audio_manager_->HasAudioOutputDevices()); | |
100 } | |
101 | |
102 TEST_F(CastAudioManagerTest, HasAudioInputDevices) { | |
103 EXPECT_FALSE(audio_manager_->HasAudioInputDevices()); | |
104 } | |
105 | |
106 TEST_F(CastAudioManagerTest, ShowAudioInputSettings) { | |
107 base::test::MockLog log; | |
108 log.StartCapturingLogs(); | |
109 EXPECT_CALL(log, Log(logging::LOG_WARNING, _, _, _, _)); | |
110 audio_manager_->ShowAudioInputSettings(); | |
111 } | |
112 | |
113 TEST_F(CastAudioManagerTest, GetAudioInputDeviceNames) { | |
114 base::test::MockLog log; | |
115 log.StartCapturingLogs(); | |
116 EXPECT_CALL(log, Log(logging::LOG_WARNING, _, _, _, _)); | |
117 media::AudioDeviceNames device_names; | |
118 audio_manager_->GetAudioInputDeviceNames(&device_names); | |
119 } | |
120 | |
121 TEST_F(CastAudioManagerTest, GetInputStreamParameters) { | |
122 base::test::MockLog log; | |
123 log.StartCapturingLogs(); | |
124 EXPECT_CALL(log, Log(logging::LOG_WARNING, _, _, _, _)); | |
125 audio_manager_->GetInputStreamParameters(kTestDeviceId); | |
126 } | |
127 | |
128 TEST_F(CastAudioManagerTest, GetPreferredOutputStreamParameters) { | |
129 // Create the parameters. | |
130 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LINEAR, | |
131 kTestChannelLayout, kTestSampleRate, | |
132 kTestBitDepth, kTestFramesPerBuffer); | |
133 | |
134 media::AudioParameters params_out = | |
135 audio_manager_->GetPreferredOutputStreamParameters("", params); | |
136 ASSERT_TRUE(params.Equals(params_out)); | |
137 } | |
138 | |
139 TEST_F(CastAudioManagerTest, MakeLinearOutputStream) { | |
140 // Create the parameters. | |
141 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LINEAR, | |
142 kTestChannelLayout, kTestSampleRate, | |
143 kTestBitDepth, kTestFramesPerBuffer); | |
144 | |
145 // Test that a returned null stream trickles all the way up. | |
146 audio_output_device_.set_null_stream(true); | |
147 media::AudioOutputStream* stream = | |
148 audio_manager_->MakeLinearOutputStream(params); | |
149 EXPECT_EQ(nullptr, stream); | |
150 | |
151 // Test that a non-null stream returns successfully. | |
152 audio_output_device_.set_null_stream(false); | |
153 stream = audio_manager_->MakeLinearOutputStream(params); | |
154 EXPECT_NE(nullptr, stream); | |
155 } | |
156 | |
157 TEST_F(CastAudioManagerTest, MakeLowLatencyOutputStream) { | |
158 // Create the parameters. | |
159 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
160 kTestChannelLayout, kTestSampleRate, | |
161 kTestBitDepth, kTestFramesPerBuffer); | |
162 | |
163 // Test that a returned null stream trickles all the way up. | |
164 audio_output_device_.set_null_stream(true); | |
165 media::AudioOutputStream* stream = | |
166 audio_manager_->MakeLowLatencyOutputStream(params, kTestDeviceId); | |
167 EXPECT_EQ(nullptr, stream); | |
168 | |
169 // Test that a non-null stream returns successfully. | |
170 audio_output_device_.set_null_stream(false); | |
171 stream = audio_manager_->MakeLowLatencyOutputStream(params, kTestDeviceId); | |
172 EXPECT_NE(nullptr, stream); | |
173 } | |
174 | |
175 TEST_F(CastAudioManagerTest, MakeLinearInputStream) { | |
176 base::test::MockLog log; | |
177 log.StartCapturingLogs(); | |
178 EXPECT_CALL(log, Log(logging::LOG_WARNING, _, _, _, _)).Times(2); | |
179 | |
180 // Create the parameters. | |
181 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LINEAR, | |
182 kTestChannelLayout, kTestSampleRate, | |
183 kTestBitDepth, kTestFramesPerBuffer); | |
184 | |
185 // Test that the returned stream is always null. | |
186 audio_output_device_.set_null_stream(true); | |
187 media::AudioInputStream* stream = | |
188 audio_manager_->MakeLinearInputStream(params, kTestDeviceId); | |
189 EXPECT_EQ(nullptr, stream); | |
190 | |
191 audio_output_device_.set_null_stream(false); | |
192 stream = audio_manager_->MakeLinearInputStream(params, kTestDeviceId); | |
193 EXPECT_EQ(nullptr, stream); | |
194 } | |
195 | |
196 TEST_F(CastAudioManagerTest, MakeLowLatencyInputStream) { | |
197 base::test::MockLog log; | |
198 log.StartCapturingLogs(); | |
199 EXPECT_CALL(log, Log(logging::LOG_WARNING, _, _, _, _)).Times(2); | |
gunsch
2015/04/27 16:09:11
verifying number of WARNING statements logged seem
slan
2015/04/28 00:11:38
There are two calls, so two warnings should pop up
| |
200 | |
201 // Create the parameters. | |
202 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
203 kTestChannelLayout, kTestSampleRate, | |
204 kTestBitDepth, kTestFramesPerBuffer); | |
205 | |
206 // Test that a the returned stream is always null. | |
207 audio_output_device_.set_null_stream(true); | |
208 media::AudioInputStream* stream = | |
209 audio_manager_->MakeLowLatencyInputStream(params, kTestDeviceId); | |
210 EXPECT_EQ(nullptr, stream); | |
211 | |
212 audio_output_device_.set_null_stream(false); | |
213 stream = audio_manager_->MakeLowLatencyInputStream(params, kTestDeviceId); | |
214 EXPECT_EQ(nullptr, stream); | |
215 } | |
216 | |
217 } // namespace | |
218 } // namespace chromecast | |
OLD | NEW |