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