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/test_browser_thread_bundle.h" | |
16 #include "media/audio/audio_manager.h" | |
17 #include "media/base/media_switches.h" | |
18 #include "media/mojo/interfaces/audio_output.mojom.h" | |
19 #include "testing/gmock/include/gmock/gmock.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 using ::testing::_; | |
23 using ::testing::Return; | |
24 | |
25 namespace content { | |
26 | |
27 namespace { | |
28 | |
29 const int kRenderFrameId1 = 5; | |
30 const int kRenderFrameId2 = 3; | |
31 const int kStreamId1 = 9; | |
32 const int kStreamId2 = 20; | |
33 | |
34 const int kStreamIds[] = {800, 2, 30, 22}; | |
35 const unsigned int kStreamIdsSize = 4; | |
36 const int kRenderProcessId = 1; | |
37 | |
38 std::string ReturnMockSalt() { | |
39 return std::string(); | |
40 } | |
41 | |
42 ResourceContext::SaltCallback GetMockSaltCallback() { | |
43 return base::Bind(&ReturnMockSalt); | |
44 } | |
45 } | |
Henrik Grunell
2016/05/18 10:29:06
Nit: empty line above and add " // namespace".
| |
46 | |
47 class MockAudioOutputStreamImpl : public AudioOutputStreamImpl { | |
48 public: | |
49 explicit MockAudioOutputStreamImpl( | |
50 media::mojom::AudioOutputStreamRequest request, | |
51 int stream_id, | |
52 int renderer_frame_id, | |
53 AudioRendererHost* audio_renderer_host) | |
54 : AudioOutputStreamImpl(std::move(request), | |
55 stream_id, | |
56 renderer_frame_id, | |
57 audio_renderer_host) {} | |
58 MOCK_METHOD0(Close, void()); | |
59 }; | |
60 | |
61 class MockAudioRendererHost : public AudioRendererHost { | |
62 public: | |
63 MockAudioRendererHost(int render_process_id, | |
64 media::AudioManager* audio_manager, | |
65 AudioMirroringManager* mirroring_manager, | |
66 MediaInternals* media_internals, | |
67 MediaStreamManager* media_stream_manager, | |
68 const ResourceContext::SaltCallback& salt_callback) | |
69 : AudioRendererHost(render_process_id, | |
70 audio_manager, | |
71 mirroring_manager, | |
72 media_internals, | |
73 media_stream_manager, | |
74 salt_callback) {} | |
75 | |
76 MOCK_METHOD1(CloseStream, void(int stream_id)); | |
77 MOCK_METHOD4( | |
78 CreateStream, | |
79 void(int stream_id, | |
80 int render_frame_id, | |
81 const media::AudioParameters& params, | |
82 const media::mojom::AudioOutput::CreateStreamCallback& callback)); | |
83 | |
84 MOCK_METHOD2( | |
85 DoCompleteCreation, | |
86 void(int stream_id, | |
87 const media::mojom::AudioOutput::CreateStreamCallback& callback)); | |
88 MOCK_METHOD2(set_audio_output_impl, | |
89 void(int render_frame_id, AudioOutputImpl* audio_output_impl)); | |
90 | |
91 protected: | |
92 FRIEND_TEST_ALL_PREFIXES(AudioOutputImplTest, CreateServiceOnIOThread); | |
Henrik Grunell
2016/05/18 10:29:06
This should not be needed.
| |
93 FRIEND_TEST_ALL_PREFIXES(AudioOutputImplTest, CreateStream); | |
94 friend class AudioOutputImplTest; | |
95 virtual ~MockAudioRendererHost() {} | |
96 | |
97 private: | |
98 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); | |
99 }; | |
100 | |
101 class AudioOutputImplTest : public ::testing::Test { | |
102 public: | |
103 AudioOutputImplTest() { | |
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 MockAudioRendererHost( | |
116 kRenderProcessId, audio_manager_.get(), &mirroring_manager_, | |
117 MediaInternals::GetInstance(), media_stream_manager_.get(), | |
118 GetMockSaltCallback()); | |
119 } | |
120 void CreateCallback(int stream_id, | |
121 media::mojom::AudioOutputStreamPtr stream, | |
122 mojo::ScopedSharedBufferHandle shared_buffer, | |
123 mojo::ScopedHandle socket_descriptor) {} | |
124 | |
125 ~AudioOutputImplTest() override {} | |
126 | |
127 private: | |
128 std::unique_ptr<MediaStreamManager> media_stream_manager_; | |
129 TestBrowserThreadBundle thread_bundle_; | |
130 media::ScopedAudioManagerPtr audio_manager_; | |
131 AudioMirroringManager mirroring_manager_; | |
132 | |
133 protected: | |
Henrik Grunell
2016/05/18 10:29:06
protected goes before private.
| |
134 MockAudioRendererHost* audio_renderer_host_; | |
135 std::unique_ptr<base::MessageLoop> message_loop_; | |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(AudioOutputImplTest); | |
138 }; | |
139 | |
140 TEST_F(AudioOutputImplTest, CreateStream) { | |
141 auto callback = | |
142 base::Bind(&AudioOutputImplTest::CreateCallback, base::Unretained(this)); | |
143 | |
144 media::AudioParameters params( | |
145 media::AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_STEREO, | |
146 media::AudioParameters::kAudioCDSampleRate, 16, | |
147 media::AudioParameters::kAudioCDSampleRate / 10); | |
148 | |
149 EXPECT_CALL(*audio_renderer_host_, | |
150 CreateStream(kStreamId1, kRenderFrameId1, _, _)); | |
151 | |
152 AudioOutputImpl audio_output_impl1(audio_renderer_host_, kRenderFrameId1, | |
153 media::mojom::AudioOutputRequest()); | |
154 audio_output_impl1.CreateStream(kStreamId1, params, callback); | |
155 | |
156 EXPECT_CALL(*audio_renderer_host_, | |
157 CreateStream(kStreamId2, kRenderFrameId2, _, _)); | |
158 | |
159 AudioOutputImpl audio_output_impl2(audio_renderer_host_, kRenderFrameId2, | |
160 media::mojom::AudioOutputRequest()); | |
161 audio_output_impl2.CreateStream(kStreamId2, params, callback); | |
162 } | |
163 | |
164 TEST_F(AudioOutputImplTest, StreamFactory) { | |
165 AudioOutputImpl audio_output_impl(audio_renderer_host_, kRenderFrameId1, | |
166 media::mojom::AudioOutputRequest()); | |
167 EXPECT_EQ(0u, audio_output_impl.stream_impls_.size()); | |
168 unsigned int i = 0; | |
169 for (i = 0; i < kStreamIdsSize; i++) { | |
170 auto stream = audio_output_impl.StreamFactory( | |
171 kStreamIds[i], kRenderFrameId1, audio_renderer_host_); | |
172 EXPECT_TRUE(stream.is_bound()); | |
173 EXPECT_NE(audio_output_impl.stream_impls_.end(), | |
174 audio_output_impl.stream_impls_.find(kStreamIds[i])); | |
175 EXPECT_EQ(kStreamIds[i], | |
176 audio_output_impl.stream_impls_[kStreamIds[i]]->get_stream_id()); | |
177 | |
178 EXPECT_EQ(i + 1, audio_output_impl.stream_impls_.size()); | |
179 } | |
180 } | |
181 | |
182 TEST_F(AudioOutputImplTest, RemoveStream) { | |
183 AudioOutputImpl audio_output_impl(audio_renderer_host_, kRenderFrameId1, | |
184 media::mojom::AudioOutputRequest()); | |
185 | |
186 EXPECT_EQ(0u, audio_output_impl.stream_impls_.size()); | |
187 // Remove a stream from an empty audio_output_impl should not be possible. | |
188 EXPECT_FALSE(audio_output_impl.RemoveStream(kStreamId1)); | |
189 EXPECT_EQ(0u, audio_output_impl.stream_impls_.size()); | |
190 unsigned int i = 0; | |
191 // Fill stream_impls_ with streams. | |
192 for (i = 0; i < kStreamIdsSize; i++) { | |
193 auto stream = audio_output_impl.StreamFactory( | |
194 kStreamIds[i], kRenderFrameId1, audio_renderer_host_); | |
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 EXPECT_EQ(i + 1, audio_output_impl.stream_impls_.size()); | |
200 } | |
201 // Remove streams. | |
202 for (i = 0; i < kStreamIdsSize; i++) { | |
203 EXPECT_TRUE(audio_output_impl.RemoveStream(kStreamIds[i])); | |
204 EXPECT_EQ(audio_output_impl.stream_impls_.end(), | |
205 audio_output_impl.stream_impls_.find(kStreamIds[i])); | |
206 EXPECT_EQ(kStreamIdsSize - i - 1, audio_output_impl.stream_impls_.size()); | |
207 } | |
208 } | |
209 | |
210 } // namespace content | |
OLD | NEW |