OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 "content/browser/renderer_host/media/audio_mirroring_manager.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/synchronization/waitable_event.h" | |
12 #include "base/message_loop.h" | |
13 #include "content/browser/browser_thread_impl.h" | |
14 #include "media/audio/audio_parameters.h" | |
15 #include "testing/gmock/include/gmock/gmock.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 using media::AudioOutputStream; | |
19 using media::AudioParameters; | |
20 using testing::_; | |
21 using testing::NotNull; | |
22 using testing::Ref; | |
23 using testing::Return; | |
24 using testing::ReturnRef; | |
25 | |
26 namespace content { | |
27 | |
28 class MockDiverter : public AudioMirroringManager::Diverter { | |
29 public: | |
30 MOCK_METHOD0(GetAudioParameters, const AudioParameters&()); | |
31 MOCK_METHOD1(StartDiverting, void(AudioOutputStream*)); | |
32 MOCK_METHOD0(StopDiverting, void()); | |
33 }; | |
34 | |
35 class MockMirroringDestination | |
36 : public AudioMirroringManager::MirroringDestination { | |
37 public: | |
38 MOCK_METHOD1(AddInput, | |
39 media::AudioOutputStream*(const media::AudioParameters& params)); | |
40 }; | |
41 | |
42 class AudioMirroringManagerTest : public testing::Test { | |
43 public: | |
44 AudioMirroringManagerTest() | |
45 : message_loop_(MessageLoop::TYPE_IO), | |
46 io_thread_(BrowserThread::IO, &message_loop_), | |
47 params_(AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_STEREO, | |
48 AudioParameters::kAudioCDSampleRate, 16, | |
49 AudioParameters::kAudioCDSampleRate / 10) {} | |
50 | |
51 MockDiverter* CreateStream( | |
52 int render_process_id, int render_view_id, int expected_times_diverted) { | |
53 MockDiverter* const diverter = new MockDiverter(); | |
54 if (expected_times_diverted > 0) { | |
55 EXPECT_CALL(*diverter, GetAudioParameters()) | |
56 .Times(expected_times_diverted) | |
57 .WillRepeatedly(ReturnRef(params_)); | |
58 EXPECT_CALL(*diverter, StartDiverting(NotNull())) | |
59 .Times(expected_times_diverted); | |
60 EXPECT_CALL(*diverter, StopDiverting()) | |
61 .Times(expected_times_diverted); | |
62 } | |
63 | |
64 AudioMirroringManager::GetInstance()-> | |
tommi (sloooow) - chröme
2012/12/19 12:27:54
instead of using GetInstance() suggest instead to
miu
2012/12/28 23:03:49
Done. Made it a member of AudioMirroringManagerTe
| |
65 AddDiverter(render_process_id, render_view_id, diverter); | |
66 | |
67 return diverter; | |
68 } | |
69 | |
70 void KillStream( | |
71 int render_process_id, int render_view_id, MockDiverter* diverter) { | |
72 AudioMirroringManager::GetInstance()-> | |
73 RemoveDiverter(render_process_id, render_view_id, diverter); | |
74 | |
75 delete diverter; | |
76 } | |
77 | |
78 MockMirroringDestination* StartMirroringTo( | |
79 int render_process_id, int render_view_id, int expected_inputs_added) { | |
80 MockMirroringDestination* const dest = new MockMirroringDestination(); | |
81 if (expected_inputs_added > 0) { | |
82 static AudioOutputStream* const kNonNullPointer = | |
83 reinterpret_cast<AudioOutputStream*>(0x11111110); | |
84 EXPECT_CALL(*dest, AddInput(Ref(params_))) | |
85 .Times(expected_inputs_added) | |
86 .WillRepeatedly(Return(kNonNullPointer)); | |
87 } | |
88 | |
89 AudioMirroringManager::GetInstance()-> | |
90 StartMirroring(render_process_id, render_view_id, dest); | |
91 | |
92 return dest; | |
93 } | |
94 | |
95 void StopMirroringTo(int render_process_id, int render_view_id, | |
96 MockMirroringDestination* dest) { | |
97 AudioMirroringManager::GetInstance()-> | |
98 StopMirroring(render_process_id, render_view_id, dest); | |
99 | |
100 delete dest; | |
101 } | |
102 | |
103 private: | |
104 MessageLoop message_loop_; | |
105 BrowserThreadImpl io_thread_; | |
106 AudioParameters params_; | |
107 | |
108 DISALLOW_COPY_AND_ASSIGN(AudioMirroringManagerTest); | |
109 }; | |
110 | |
111 namespace { | |
112 const int kRenderProcessId = 123; | |
113 const int kRenderViewId = 456; | |
114 const int kAnotherRenderProcessId = 789; | |
115 const int kAnotherRenderViewId = 1234; | |
116 const int kYetAnotherRenderProcessId = 4560; | |
117 const int kYetAnotherRenderViewId = 7890; | |
118 } | |
119 | |
120 TEST_F(AudioMirroringManagerTest, MirroringSessionOfNothing) { | |
121 MockMirroringDestination* const destination = | |
122 StartMirroringTo(kRenderProcessId, kRenderViewId, 0); | |
123 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
124 } | |
125 | |
126 TEST_F(AudioMirroringManagerTest, TwoMirroringSessionsOfNothing) { | |
127 MockMirroringDestination* const destination = | |
128 StartMirroringTo(kRenderProcessId, kRenderViewId, 0); | |
129 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
130 | |
131 MockMirroringDestination* const another_destination = | |
132 StartMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId, 0); | |
133 StopMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId, | |
134 another_destination); | |
135 } | |
136 | |
137 TEST_F(AudioMirroringManagerTest, SwitchMirroringDestinationNoStreams) { | |
138 MockMirroringDestination* const destination = | |
139 StartMirroringTo(kRenderProcessId, kRenderViewId, 0); | |
140 MockMirroringDestination* const new_destination = | |
141 StartMirroringTo(kRenderProcessId, kRenderViewId, 0); | |
142 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
143 StopMirroringTo(kRenderProcessId, kRenderViewId, new_destination); | |
144 } | |
145 | |
146 TEST_F(AudioMirroringManagerTest, StreamLifetimeAroundMirroringSession) { | |
147 MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 1); | |
148 MockMirroringDestination* const destination = | |
149 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | |
150 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
151 KillStream(kRenderProcessId, kRenderViewId, stream); | |
152 } | |
153 | |
154 TEST_F(AudioMirroringManagerTest, StreamLifetimeWithinMirroringSession) { | |
155 MockMirroringDestination* const destination = | |
156 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | |
157 MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 1); | |
158 KillStream(kRenderProcessId, kRenderViewId, stream); | |
159 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
160 } | |
161 | |
162 TEST_F(AudioMirroringManagerTest, StreamLifetimeAroundTwoMirroringSessions) { | |
163 MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 2); | |
164 MockMirroringDestination* const destination = | |
165 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | |
166 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
167 MockMirroringDestination* const new_destination = | |
168 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | |
169 StopMirroringTo(kRenderProcessId, kRenderViewId, new_destination); | |
170 KillStream(kRenderProcessId, kRenderViewId, stream); | |
171 } | |
172 | |
173 TEST_F(AudioMirroringManagerTest, StreamLifetimeWithinTwoMirroringSessions) { | |
174 MockMirroringDestination* const destination = | |
175 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | |
176 MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 2); | |
177 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
178 MockMirroringDestination* const new_destination = | |
179 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | |
180 KillStream(kRenderProcessId, kRenderViewId, stream); | |
181 StopMirroringTo(kRenderProcessId, kRenderViewId, new_destination); | |
182 } | |
183 | |
184 TEST_F(AudioMirroringManagerTest, MultipleStreamsInOneMirroringSession) { | |
185 MockDiverter* const stream1 = | |
186 CreateStream(kRenderProcessId, kRenderViewId, 1); | |
187 MockMirroringDestination* const destination = | |
188 StartMirroringTo(kRenderProcessId, kRenderViewId, 3); | |
189 MockDiverter* const stream2 = | |
190 CreateStream(kRenderProcessId, kRenderViewId, 1); | |
191 MockDiverter* const stream3 = | |
192 CreateStream(kRenderProcessId, kRenderViewId, 1); | |
193 KillStream(kRenderProcessId, kRenderViewId, stream2); | |
194 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
195 KillStream(kRenderProcessId, kRenderViewId, stream3); | |
196 KillStream(kRenderProcessId, kRenderViewId, stream1); | |
197 } | |
198 | |
199 // A random interleaving of operations for three separate targets, each of which | |
200 // has one stream mirrored to one destination. | |
201 TEST_F(AudioMirroringManagerTest, ThreeSeparateMirroringSessions) { | |
202 MockDiverter* const stream = | |
203 CreateStream(kRenderProcessId, kRenderViewId, 1); | |
204 MockMirroringDestination* const destination = | |
205 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | |
206 | |
207 MockMirroringDestination* const another_destination = | |
208 StartMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId, 1); | |
209 MockDiverter* const another_stream = | |
210 CreateStream(kAnotherRenderProcessId, kAnotherRenderViewId, 1); | |
211 | |
212 KillStream(kRenderProcessId, kRenderViewId, stream); | |
213 | |
214 MockDiverter* const yet_another_stream = | |
215 CreateStream(kYetAnotherRenderProcessId, kYetAnotherRenderViewId, 1); | |
216 MockMirroringDestination* const yet_another_destination = | |
217 StartMirroringTo(kYetAnotherRenderProcessId, kYetAnotherRenderViewId, 1); | |
218 | |
219 StopMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId, | |
220 another_destination); | |
221 | |
222 StopMirroringTo(kYetAnotherRenderProcessId, kYetAnotherRenderViewId, | |
223 yet_another_destination); | |
224 | |
225 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
226 | |
227 KillStream(kAnotherRenderProcessId, kAnotherRenderViewId, another_stream); | |
228 KillStream(kYetAnotherRenderProcessId, kYetAnotherRenderViewId, | |
229 yet_another_stream); | |
230 } | |
231 | |
232 } // namespace content | |
OLD | NEW |