OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 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 <stdint.h> |
| 6 #include <string> |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" |
| 10 #include "content/browser/media/audio_output_impl.h" |
| 11 #include "content/browser/media/audio_output_stream_impl.h" |
| 12 #include "content/browser/media/capture/audio_mirroring_manager.h" |
| 13 #include "content/browser/media/media_internals.h" |
| 14 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 15 #include "content/public/test/mock_render_process_host.h" |
| 16 #include "content/public/test/test_browser_context.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "media/audio/audio_manager.h" |
| 19 #include "media/base/media_switches.h" |
| 20 #include "media/mojo/interfaces/audio_output.mojom.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 |
| 24 using ::testing::_; |
| 25 using ::testing::Return; |
| 26 /* |
| 27 namespace content { |
| 28 |
| 29 namespace { |
| 30 |
| 31 const int kRenderFrameId1 = 5; |
| 32 const int kRenderFrameId2 = 3; |
| 33 const int kRenderProcessId = 1; |
| 34 const int kStreamId1 = 9; |
| 35 const int kStreamId2 = 20; |
| 36 const int kStreamIds[] = {800, 2, 30, 22}; |
| 37 const unsigned int kStreamIdsSize = 4; |
| 38 |
| 39 std::string ReturnMockSalt() { |
| 40 return std::string(); |
| 41 } |
| 42 |
| 43 ResourceContext::SaltCallback GetMockSaltCallback() { |
| 44 return base::Bind(&ReturnMockSalt); |
| 45 } |
| 46 } |
| 47 |
| 48 class MockAudioOutputStreamImpl : public AudioOutputStreamImpl { |
| 49 public: |
| 50 explicit MockAudioOutputStreamImpl( |
| 51 media::mojom::AudioOutputStreamRequest request, |
| 52 int stream_id, |
| 53 int renderer_frame_id, |
| 54 AudioRendererHost* audio_renderer_host) |
| 55 : AudioOutputStreamImpl(std::move(request), |
| 56 stream_id, |
| 57 renderer_frame_id, |
| 58 audio_renderer_host) {} |
| 59 MOCK_METHOD0(Close, void()); |
| 60 }; |
| 61 |
| 62 class MockAudioRendererHost : public AudioRendererHost { |
| 63 public: |
| 64 MockAudioRendererHost(int render_process_id, |
| 65 media::AudioManager* audio_manager, |
| 66 AudioMirroringManager* mirroring_manager, |
| 67 MediaInternals* media_internals, |
| 68 MediaStreamManager* media_stream_manager, |
| 69 const ResourceContext::SaltCallback& salt_callback) |
| 70 : AudioRendererHost(render_process_id, |
| 71 audio_manager, |
| 72 mirroring_manager, |
| 73 media_internals, |
| 74 media_stream_manager, |
| 75 salt_callback) {} |
| 76 |
| 77 MOCK_METHOD1(CloseStream, void(int stream_id)); |
| 78 MOCK_METHOD4( |
| 79 CreateStream, |
| 80 void(int stream_id, |
| 81 int render_frame_id, |
| 82 const media::AudioParameters& params, |
| 83 const media::mojom::AudioOutput::CreateStreamCallback& callback)); |
| 84 |
| 85 MOCK_METHOD2( |
| 86 DoCompleteCreation, |
| 87 void(int stream_id, |
| 88 const media::mojom::AudioOutput::CreateStreamCallback& callback)); |
| 89 MOCK_METHOD2(set_audio_output_impl, |
| 90 void(int render_frame_id, AudioOutputImpl* audio_output_impl)); |
| 91 |
| 92 protected: |
| 93 FRIEND_TEST_ALL_PREFIXES(AudioOutputImplTest, CreateServiceOnIOThread); |
| 94 FRIEND_TEST_ALL_PREFIXES(AudioOutputImplTest, CreateStream); |
| 95 friend class AudioOutputImplTest; |
| 96 virtual ~MockAudioRendererHost() {} |
| 97 |
| 98 private: |
| 99 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); |
| 100 }; |
| 101 |
| 102 class TestRenderProcessHost : public MockRenderProcessHost { |
| 103 public: |
| 104 TestRenderProcessHost(BrowserContext* context) |
| 105 : MockRenderProcessHost(context) {} |
| 106 scoped_refptr<AudioRendererHost> audio_renderer_host() const override { |
| 107 return audio_renderer_host_; |
| 108 } |
| 109 scoped_refptr<AudioRendererHost> audio_renderer_host_; |
| 110 }; |
| 111 |
| 112 class AudioOutputImplTest : public ::testing::Test { |
| 113 public: |
| 114 AudioOutputImplTest() { |
| 115 browser_context_.reset(new TestBrowserContext); |
| 116 render_process_host_.reset( |
| 117 new TestRenderProcessHost(browser_context_.get())); |
| 118 |
| 119 audio_manager_ = media::AudioManager::CreateForTesting( |
| 120 base::ThreadTaskRunnerHandle::Get()); |
| 121 |
| 122 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 123 switches::kUseFakeDeviceForMediaStream); |
| 124 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); |
| 125 |
| 126 // Enable caching to make enumerations run in a single thread |
| 127 media_stream_manager_->audio_output_device_enumerator()->SetCachePolicy( |
| 128 AudioOutputDeviceEnumerator::CACHE_POLICY_MANUAL_INVALIDATION); |
| 129 |
| 130 audio_renderer_host_ = new MockAudioRendererHost( |
| 131 kRenderProcessId, audio_manager_.get(), &mirroring_manager_, |
| 132 MediaInternals::GetInstance(), media_stream_manager_.get(), |
| 133 GetMockSaltCallback()); |
| 134 |
| 135 render_process_host_->audio_renderer_host_ = audio_renderer_host_; |
| 136 } |
| 137 void CreateCallback(int stream_id, |
| 138 media::mojom::AudioOutputStreamPtr stream, |
| 139 mojo::ScopedSharedBufferHandle shared_buffer, |
| 140 mojo::ScopedHandle socket_descriptor) {} |
| 141 |
| 142 ~AudioOutputImplTest() override {} |
| 143 |
| 144 private: |
| 145 media::ScopedAudioManagerPtr audio_manager_; |
| 146 std::unique_ptr<BrowserContext> browser_context_; |
| 147 std::unique_ptr<MediaStreamManager> media_stream_manager_; |
| 148 AudioMirroringManager mirroring_manager_; |
| 149 TestBrowserThreadBundle thread_bundle_; |
| 150 |
| 151 protected: |
| 152 std::unique_ptr<TestRenderProcessHost> render_process_host_; |
| 153 MockAudioRendererHost* audio_renderer_host_; |
| 154 std::unique_ptr<base::MessageLoop> message_loop_; |
| 155 DISALLOW_COPY_AND_ASSIGN(AudioOutputImplTest); |
| 156 }; |
| 157 |
| 158 TEST_F(AudioOutputImplTest, CreateStream) { |
| 159 auto callback = |
| 160 base::Bind(&AudioOutputImplTest::CreateCallback, base::Unretained(this)); |
| 161 |
| 162 media::AudioParameters params( |
| 163 media::AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_STEREO, |
| 164 media::AudioParameters::kAudioCDSampleRate, 16, |
| 165 media::AudioParameters::kAudioCDSampleRate / 10); |
| 166 |
| 167 EXPECT_CALL(*audio_renderer_host_, |
| 168 CreateStream(kStreamId1, kRenderFrameId1, _, _)) |
| 169 .Times(1); |
| 170 |
| 171 AudioOutputImpl audio_output_impl1(render_process_host_.get(), |
| 172 kRenderFrameId1, |
| 173 media::mojom::AudioOutputRequest()); |
| 174 audio_output_impl1.CreateStream(kStreamId1, params, callback); |
| 175 |
| 176 EXPECT_CALL(*audio_renderer_host_, |
| 177 CreateStream(kStreamId2, kRenderFrameId2, _, _)) |
| 178 .Times(1); |
| 179 |
| 180 AudioOutputImpl audio_output_impl2(render_process_host_.get(), |
| 181 kRenderFrameId2, |
| 182 media::mojom::AudioOutputRequest()); |
| 183 audio_output_impl2.CreateStream(kStreamId2, params, callback); |
| 184 } |
| 185 |
| 186 TEST_F(AudioOutputImplTest, StreamFactory) { |
| 187 AudioOutputImpl audio_output_impl(render_process_host_.get(), kRenderFrameId1, |
| 188 media::mojom::AudioOutputRequest()); |
| 189 EXPECT_EQ(0u, audio_output_impl.stream_impls_.size()); |
| 190 unsigned int i = 0; |
| 191 for (i = 0; i < kStreamIdsSize; i++) { |
| 192 auto stream = audio_output_impl.StreamFactory( |
| 193 kStreamIds[i], kRenderFrameId1, audio_renderer_host_); |
| 194 EXPECT_TRUE(stream.is_bound()); |
| 195 EXPECT_NE(audio_output_impl.stream_impls_.end(), |
| 196 audio_output_impl.stream_impls_.find(kStreamIds[i])); |
| 197 EXPECT_EQ(kStreamIds[i], |
| 198 audio_output_impl.stream_impls_[kStreamIds[i]]->get_stream_id()); |
| 199 |
| 200 EXPECT_EQ(i + 1, audio_output_impl.stream_impls_.size()); |
| 201 } |
| 202 } |
| 203 |
| 204 TEST_F(AudioOutputImplTest, RemoveStream) { |
| 205 AudioOutputImpl audio_output_impl(render_process_host_.get(), kRenderFrameId1, |
| 206 media::mojom::AudioOutputRequest()); |
| 207 |
| 208 EXPECT_EQ(0u, audio_output_impl.stream_impls_.size()); |
| 209 // Remove a stream from an empty audio_output_impl should not be possible. |
| 210 EXPECT_FALSE(audio_output_impl.RemoveStream(kStreamId1)); |
| 211 EXPECT_EQ(0u, audio_output_impl.stream_impls_.size()); |
| 212 unsigned int i = 0; |
| 213 // Fill stream_impls_ with streams. |
| 214 for (i = 0; i < kStreamIdsSize; i++) { |
| 215 auto stream = audio_output_impl.StreamFactory( |
| 216 kStreamIds[i], kRenderFrameId1, audio_renderer_host_); |
| 217 EXPECT_NE(audio_output_impl.stream_impls_.end(), |
| 218 audio_output_impl.stream_impls_.find(kStreamIds[i])); |
| 219 EXPECT_EQ(kStreamIds[i], |
| 220 audio_output_impl.stream_impls_[kStreamIds[i]]->get_stream_id()); |
| 221 EXPECT_EQ(i + 1, audio_output_impl.stream_impls_.size()); |
| 222 } |
| 223 // Remove streams. |
| 224 for (i = 0; i < kStreamIdsSize; i++) { |
| 225 EXPECT_TRUE(audio_output_impl.RemoveStream(kStreamIds[i])); |
| 226 EXPECT_EQ(audio_output_impl.stream_impls_.end(), |
| 227 audio_output_impl.stream_impls_.find(kStreamIds[i])); |
| 228 EXPECT_EQ(kStreamIdsSize - i - 1, audio_output_impl.stream_impls_.size()); |
| 229 } |
| 230 } |
| 231 |
| 232 } // namespace content |
| 233 */ |
OLD | NEW |