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 kRenderProcessId = 1; |
| 32 const int kRenderFrameId = 5; |
| 33 const int kStreamId1 = 9; |
| 34 const int kStreamId2 = 20; |
| 35 |
| 36 std::string ReturnMockSalt() { |
| 37 return std::string(); |
| 38 } |
| 39 |
| 40 ResourceContext::SaltCallback GetMockSaltCallback() { |
| 41 return base::Bind(&ReturnMockSalt); |
| 42 } |
| 43 } |
| 44 |
| 45 |
| 46 |
| 47 class MockAudioOutputStreamAudioRendererHost : public AudioRendererHost { |
| 48 public: |
| 49 MockAudioOutputStreamAudioRendererHost( |
| 50 int render_process_id, |
| 51 media::AudioManager* audio_manager, |
| 52 AudioMirroringManager* mirroring_manager, |
| 53 MediaInternals* media_internals, |
| 54 MediaStreamManager* media_stream_manager, |
| 55 const ResourceContext::SaltCallback& salt_callback) |
| 56 : AudioRendererHost(render_process_id, |
| 57 audio_manager, |
| 58 mirroring_manager, |
| 59 media_internals, |
| 60 media_stream_manager, |
| 61 salt_callback) {} |
| 62 |
| 63 MOCK_METHOD1(CloseStream, void(int stream_id)); |
| 64 |
| 65 protected: |
| 66 FRIEND_TEST_ALL_PREFIXES(AudioOutputStreamImplTest, Close); |
| 67 friend class AudioOutputStreamImplTest; |
| 68 ~MockAudioOutputStreamAudioRendererHost() override {} |
| 69 |
| 70 private: |
| 71 DISALLOW_COPY_AND_ASSIGN(MockAudioOutputStreamAudioRendererHost); |
| 72 }; |
| 73 |
| 74 class MockAudioOutputImpl : public AudioOutputImpl { |
| 75 public: |
| 76 MockAudioOutputImpl(content::RenderProcessHost* process, |
| 77 int render_frame_id, |
| 78 media::mojom::AudioOutputRequest request) |
| 79 : AudioOutputImpl(process, |
| 80 render_frame_id, |
| 81 std::move(request)) {} |
| 82 ~MockAudioOutputImpl() {} |
| 83 MOCK_METHOD1(RemoveStream, bool(int stream_id)); |
| 84 DISALLOW_COPY_AND_ASSIGN(MockAudioOutputImpl); |
| 85 }; |
| 86 |
| 87 class TestRenderProcessHost : public MockRenderProcessHost { |
| 88 public: |
| 89 TestRenderProcessHost(BrowserContext* context) |
| 90 : MockRenderProcessHost(context) {} |
| 91 scoped_refptr<AudioRendererHost> audio_renderer_host() const override { |
| 92 return audio_renderer_host_; |
| 93 } |
| 94 scoped_refptr<AudioRendererHost> audio_renderer_host_; |
| 95 }; |
| 96 |
| 97 class AudioOutputStreamImplTest : public ::testing::Test { |
| 98 public: |
| 99 AudioOutputStreamImplTest() { |
| 100 browser_context_.reset(new TestBrowserContext); |
| 101 render_process_host_.reset( |
| 102 new TestRenderProcessHost(browser_context_.get())); |
| 103 |
| 104 audio_manager_ = media::AudioManager::CreateForTesting( |
| 105 base::ThreadTaskRunnerHandle::Get()); |
| 106 |
| 107 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 108 switches::kUseFakeDeviceForMediaStream); |
| 109 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); |
| 110 |
| 111 // Enable caching to make enumerations run in a single thread |
| 112 media_stream_manager_->audio_output_device_enumerator()->SetCachePolicy( |
| 113 AudioOutputDeviceEnumerator::CACHE_POLICY_MANUAL_INVALIDATION); |
| 114 |
| 115 audio_renderer_host_ = (new MockAudioOutputStreamAudioRendererHost( |
| 116 kRenderProcessId, audio_manager_.get(), &mirroring_manager_, |
| 117 MediaInternals::GetInstance(), media_stream_manager_.get(), |
| 118 GetMockSaltCallback())); |
| 119 render_process_host_->audio_renderer_host_ = audio_renderer_host_; |
| 120 audio_output_impl_.reset(new MockAudioOutputImpl( |
| 121 render_process_host_.get(), kRenderFrameId, media::mojom::AudioOutputReq
uest())); |
| 122 audio_renderer_host_->set_audio_output_impl(kRenderFrameId, |
| 123 audio_output_impl_.get()); |
| 124 } |
| 125 |
| 126 ~AudioOutputStreamImplTest() override { |
| 127 // Avoid leaking the |audio_renderer_host_|. |
| 128 audio_renderer_host_ = NULL; |
| 129 } |
| 130 |
| 131 protected: |
| 132 std::unique_ptr<MockAudioOutputImpl> audio_output_impl_; |
| 133 scoped_refptr<MockAudioOutputStreamAudioRendererHost> audio_renderer_host_; |
| 134 |
| 135 private: |
| 136 media::ScopedAudioManagerPtr audio_manager_; |
| 137 std::unique_ptr<BrowserContext> browser_context_; |
| 138 std::unique_ptr<MediaStreamManager> media_stream_manager_; |
| 139 AudioMirroringManager mirroring_manager_; |
| 140 std::unique_ptr<TestRenderProcessHost> render_process_host_; |
| 141 TestBrowserThreadBundle thread_bundle_; |
| 142 DISALLOW_COPY_AND_ASSIGN(AudioOutputStreamImplTest); |
| 143 }; |
| 144 |
| 145 TEST_F(AudioOutputStreamImplTest, Close) { |
| 146 |
| 147 media::mojom::AudioOutputStreamPtr stream1 = |
| 148 media::mojom::AudioOutputStreamPtr(); |
| 149 |
| 150 std::unique_ptr<AudioOutputStreamImpl> stream_ptr1( |
| 151 new AudioOutputStreamImpl(mojo::GetProxy(&stream1), kStreamId1, |
| 152 kRenderFrameId, audio_renderer_host_.get())); |
| 153 |
| 154 EXPECT_CALL(*audio_renderer_host_.get(), CloseStream(kStreamId1)).Times(1); |
| 155 ; |
| 156 EXPECT_CALL(*audio_output_impl_, RemoveStream(kStreamId1)).Times(1); |
| 157 |
| 158 stream_ptr1->Close(); |
| 159 |
| 160 media::mojom::AudioOutputStreamPtr stream2 = |
| 161 media::mojom::AudioOutputStreamPtr(); |
| 162 |
| 163 std::unique_ptr<AudioOutputStreamImpl> stream_ptr2( |
| 164 new AudioOutputStreamImpl(mojo::GetProxy(&stream2), kStreamId2, |
| 165 kRenderFrameId, audio_renderer_host_.get())); |
| 166 |
| 167 EXPECT_CALL(*audio_renderer_host_.get(), CloseStream(kStreamId2)).Times(1); |
| 168 ; |
| 169 EXPECT_CALL(*audio_output_impl_, RemoveStream(kStreamId2)).Times(1); |
| 170 |
| 171 stream_ptr2->Close(); |
| 172 } |
| 173 |
| 174 } // namespace content |
OLD | NEW |