OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/media/capture/audio_mirroring_manager.h" | 5 #include "content/browser/media/capture/audio_mirroring_manager.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
12 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
13 #include "content/browser/browser_thread_impl.h" | 14 #include "content/browser/browser_thread_impl.h" |
14 #include "media/audio/audio_parameters.h" | 15 #include "media/audio/audio_parameters.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 using media::AudioOutputStream; | 19 using media::AudioOutputStream; |
19 using media::AudioParameters; | 20 using media::AudioParameters; |
20 using testing::_; | 21 using testing::_; |
| 22 using testing::Invoke; |
21 using testing::NotNull; | 23 using testing::NotNull; |
22 using testing::Ref; | 24 using testing::Ref; |
23 using testing::Return; | 25 using testing::Return; |
24 using testing::ReturnRef; | 26 using testing::ReturnRef; |
25 | 27 |
26 namespace content { | 28 namespace content { |
27 | 29 |
28 namespace { | 30 namespace { |
29 | 31 |
30 class MockDiverter : public AudioMirroringManager::Diverter { | 32 class MockDiverter : public AudioMirroringManager::Diverter { |
31 public: | 33 public: |
32 MOCK_METHOD0(GetAudioParameters, const AudioParameters&()); | 34 MOCK_METHOD0(GetAudioParameters, const AudioParameters&()); |
33 MOCK_METHOD1(StartDiverting, void(AudioOutputStream*)); | 35 MOCK_METHOD1(StartDiverting, void(AudioOutputStream*)); |
34 MOCK_METHOD0(StopDiverting, void()); | 36 MOCK_METHOD0(StopDiverting, void()); |
35 }; | 37 }; |
36 | 38 |
37 class MockMirroringDestination | 39 class MockMirroringDestination |
38 : public AudioMirroringManager::MirroringDestination { | 40 : public AudioMirroringManager::MirroringDestination { |
39 public: | 41 public: |
| 42 typedef AudioMirroringManager::SourceFrameRef SourceFrameRef; |
| 43 |
| 44 MockMirroringDestination(int render_process_id, int render_frame_id) |
| 45 : render_process_id_(render_process_id), |
| 46 render_frame_id_(render_frame_id), |
| 47 query_count_(0) {} |
| 48 |
| 49 MOCK_METHOD2(QueryForMatches, |
| 50 void(const std::set<SourceFrameRef>& candidates, |
| 51 const MatchesCallback& results_callback)); |
40 MOCK_METHOD1(AddInput, | 52 MOCK_METHOD1(AddInput, |
41 media::AudioOutputStream*(const media::AudioParameters& params)); | 53 media::AudioOutputStream*(const media::AudioParameters& params)); |
| 54 |
| 55 void SimulateQuery(const std::set<SourceFrameRef>& candidates, |
| 56 const MatchesCallback& results_callback) { |
| 57 ++query_count_; |
| 58 |
| 59 std::set<SourceFrameRef> result; |
| 60 if (candidates.find(SourceFrameRef(render_process_id_, render_frame_id_)) != |
| 61 candidates.end()) { |
| 62 result.insert(SourceFrameRef(render_process_id_, render_frame_id_)); |
| 63 } |
| 64 results_callback.Run(result); |
| 65 } |
| 66 |
| 67 media::AudioOutputStream* SimulateAddInput( |
| 68 const media::AudioParameters& params) { |
| 69 static AudioOutputStream* const kNonNullPointer = |
| 70 reinterpret_cast<AudioOutputStream*>(0x11111110); |
| 71 return kNonNullPointer; |
| 72 } |
| 73 |
| 74 int query_count() const { |
| 75 return query_count_; |
| 76 } |
| 77 |
| 78 private: |
| 79 const int render_process_id_; |
| 80 const int render_frame_id_; |
| 81 int query_count_; |
42 }; | 82 }; |
43 | 83 |
44 } // namespace | 84 } // namespace |
45 | 85 |
46 class AudioMirroringManagerTest : public testing::Test { | 86 class AudioMirroringManagerTest : public testing::Test { |
47 public: | 87 public: |
| 88 typedef AudioMirroringManager::Diverter Diverter; |
| 89 typedef AudioMirroringManager::MirroringDestination MirroringDestination; |
| 90 typedef AudioMirroringManager::StreamRoutes StreamRoutes; |
| 91 |
48 AudioMirroringManagerTest() | 92 AudioMirroringManagerTest() |
49 : io_thread_(BrowserThread::IO, &message_loop_), | 93 : io_thread_(BrowserThread::IO, &message_loop_), |
50 params_(AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_STEREO, | 94 params_(AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_STEREO, |
51 AudioParameters::kAudioCDSampleRate, 16, | 95 AudioParameters::kAudioCDSampleRate, 16, |
52 AudioParameters::kAudioCDSampleRate / 10) {} | 96 AudioParameters::kAudioCDSampleRate / 10) {} |
53 | 97 |
54 MockDiverter* CreateStream( | 98 MockDiverter* CreateStream( |
55 int render_process_id, int render_view_id, int expected_times_diverted) { | 99 int render_process_id, int render_frame_id, int expected_times_diverted) { |
56 MockDiverter* const diverter = new MockDiverter(); | 100 MockDiverter* const diverter = new MockDiverter(); |
57 if (expected_times_diverted > 0) { | 101 if (expected_times_diverted > 0) { |
58 EXPECT_CALL(*diverter, GetAudioParameters()) | 102 EXPECT_CALL(*diverter, GetAudioParameters()) |
59 .Times(expected_times_diverted) | 103 .Times(expected_times_diverted) |
60 .WillRepeatedly(ReturnRef(params_)); | 104 .WillRepeatedly(ReturnRef(params_)); |
61 EXPECT_CALL(*diverter, StartDiverting(NotNull())) | 105 EXPECT_CALL(*diverter, StartDiverting(NotNull())) |
62 .Times(expected_times_diverted); | 106 .Times(expected_times_diverted); |
63 EXPECT_CALL(*diverter, StopDiverting()) | 107 EXPECT_CALL(*diverter, StopDiverting()) |
64 .Times(expected_times_diverted); | 108 .Times(expected_times_diverted); |
65 } | 109 } |
66 | 110 |
67 mirroring_manager_.AddDiverter(render_process_id, render_view_id, diverter); | 111 mirroring_manager_.AddDiverter( |
| 112 render_process_id, render_frame_id, diverter); |
68 | 113 |
69 return diverter; | 114 return diverter; |
70 } | 115 } |
71 | 116 |
72 void KillStream( | 117 void KillStream(MockDiverter* diverter) { |
73 int render_process_id, int render_view_id, MockDiverter* diverter) { | 118 mirroring_manager_.RemoveDiverter(diverter); |
74 mirroring_manager_.RemoveDiverter( | |
75 render_process_id, render_view_id, diverter); | |
76 | |
77 delete diverter; | 119 delete diverter; |
78 } | 120 } |
79 | 121 |
80 MockMirroringDestination* StartMirroringTo( | 122 void StartMirroringTo(const scoped_ptr<MockMirroringDestination>& dest, |
81 int render_process_id, int render_view_id, int expected_inputs_added) { | 123 int expected_inputs_added) { |
82 MockMirroringDestination* const dest = new MockMirroringDestination(); | 124 EXPECT_CALL(*dest, QueryForMatches(_, _)) |
| 125 .WillRepeatedly(Invoke(dest.get(), |
| 126 &MockMirroringDestination::SimulateQuery)); |
83 if (expected_inputs_added > 0) { | 127 if (expected_inputs_added > 0) { |
84 static AudioOutputStream* const kNonNullPointer = | |
85 reinterpret_cast<AudioOutputStream*>(0x11111110); | |
86 EXPECT_CALL(*dest, AddInput(Ref(params_))) | 128 EXPECT_CALL(*dest, AddInput(Ref(params_))) |
87 .Times(expected_inputs_added) | 129 .Times(expected_inputs_added) |
88 .WillRepeatedly(Return(kNonNullPointer)); | 130 .WillRepeatedly(Invoke(dest.get(), |
| 131 &MockMirroringDestination::SimulateAddInput)) |
| 132 .RetiresOnSaturation(); |
89 } | 133 } |
90 | 134 |
91 mirroring_manager_.StartMirroring(render_process_id, render_view_id, dest); | 135 mirroring_manager_.StartMirroring(dest.get()); |
92 | |
93 return dest; | |
94 } | 136 } |
95 | 137 |
96 void StopMirroringTo(int render_process_id, int render_view_id, | 138 void StopMirroringTo(const scoped_ptr<MockMirroringDestination>& dest) { |
97 MockMirroringDestination* dest) { | 139 mirroring_manager_.StopMirroring(dest.get()); |
98 mirroring_manager_.StopMirroring(render_process_id, render_view_id, dest); | 140 } |
99 | 141 |
100 delete dest; | 142 int CountStreamsDivertedTo( |
101 } | 143 const scoped_ptr<MockMirroringDestination>& dest) const { |
| 144 int count = 0; |
| 145 for (StreamRoutes::const_iterator it = mirroring_manager_.routes_.begin(); |
| 146 it != mirroring_manager_.routes_.end(); ++it) { |
| 147 if (it->destination == dest.get()) |
| 148 ++count; |
| 149 } |
| 150 return count; |
| 151 } |
| 152 |
| 153 void ExpectNoLongerManagingAnything() const { |
| 154 EXPECT_TRUE(mirroring_manager_.routes_.empty()); |
| 155 EXPECT_TRUE(mirroring_manager_.sessions_.empty()); |
| 156 } |
102 | 157 |
103 private: | 158 private: |
104 base::MessageLoopForIO message_loop_; | 159 base::MessageLoopForIO message_loop_; |
105 BrowserThreadImpl io_thread_; | 160 BrowserThreadImpl io_thread_; |
106 AudioParameters params_; | 161 AudioParameters params_; |
107 AudioMirroringManager mirroring_manager_; | 162 AudioMirroringManager mirroring_manager_; |
108 | 163 |
109 DISALLOW_COPY_AND_ASSIGN(AudioMirroringManagerTest); | 164 DISALLOW_COPY_AND_ASSIGN(AudioMirroringManagerTest); |
110 }; | 165 }; |
111 | 166 |
112 namespace { | 167 namespace { |
113 const int kRenderProcessId = 123; | 168 const int kRenderProcessId = 123; |
114 const int kRenderViewId = 456; | 169 const int kRenderFrameId = 456; |
115 const int kAnotherRenderProcessId = 789; | 170 const int kAnotherRenderProcessId = 789; |
116 const int kAnotherRenderViewId = 1234; | 171 const int kAnotherRenderFrameId = 1234; |
117 const int kYetAnotherRenderProcessId = 4560; | 172 const int kYetAnotherRenderProcessId = 4560; |
118 const int kYetAnotherRenderViewId = 7890; | 173 const int kYetAnotherRenderFrameId = 7890; |
119 } | 174 } |
120 | 175 |
121 TEST_F(AudioMirroringManagerTest, MirroringSessionOfNothing) { | 176 TEST_F(AudioMirroringManagerTest, MirroringSessionOfNothing) { |
122 MockMirroringDestination* const destination = | 177 const scoped_ptr<MockMirroringDestination> destination( |
123 StartMirroringTo(kRenderProcessId, kRenderViewId, 0); | 178 new MockMirroringDestination(kRenderProcessId, kRenderFrameId)); |
124 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | 179 StartMirroringTo(destination, 0); |
| 180 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 181 |
| 182 StopMirroringTo(destination); |
| 183 EXPECT_EQ(0, destination->query_count()); |
| 184 |
| 185 ExpectNoLongerManagingAnything(); |
125 } | 186 } |
126 | 187 |
127 TEST_F(AudioMirroringManagerTest, TwoMirroringSessionsOfNothing) { | 188 TEST_F(AudioMirroringManagerTest, TwoMirroringSessionsOfNothing) { |
128 MockMirroringDestination* const destination = | 189 const scoped_ptr<MockMirroringDestination> destination( |
129 StartMirroringTo(kRenderProcessId, kRenderViewId, 0); | 190 new MockMirroringDestination(kRenderProcessId, kRenderFrameId)); |
130 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | 191 StartMirroringTo(destination, 0); |
131 | 192 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
132 MockMirroringDestination* const another_destination = | 193 |
133 StartMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId, 0); | 194 StopMirroringTo(destination); |
134 StopMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId, | 195 EXPECT_EQ(0, destination->query_count()); |
135 another_destination); | 196 |
136 } | 197 const scoped_ptr<MockMirroringDestination> another_destination( |
137 | 198 new MockMirroringDestination(kAnotherRenderProcessId, |
138 TEST_F(AudioMirroringManagerTest, SwitchMirroringDestinationNoStreams) { | 199 kAnotherRenderFrameId)); |
139 MockMirroringDestination* const destination = | 200 StartMirroringTo(another_destination, 0); |
140 StartMirroringTo(kRenderProcessId, kRenderViewId, 0); | 201 EXPECT_EQ(0, CountStreamsDivertedTo(another_destination)); |
141 MockMirroringDestination* const new_destination = | 202 |
142 StartMirroringTo(kRenderProcessId, kRenderViewId, 0); | 203 StopMirroringTo(another_destination); |
143 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | 204 EXPECT_EQ(0, another_destination->query_count()); |
144 StopMirroringTo(kRenderProcessId, kRenderViewId, new_destination); | 205 |
145 } | 206 ExpectNoLongerManagingAnything(); |
146 | 207 } |
| 208 |
| 209 // Tests that a mirroring session starts after, and ends before, a stream that |
| 210 // will be diverted to it. |
147 TEST_F(AudioMirroringManagerTest, StreamLifetimeAroundMirroringSession) { | 211 TEST_F(AudioMirroringManagerTest, StreamLifetimeAroundMirroringSession) { |
148 MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 1); | 212 MockDiverter* const stream = |
149 MockMirroringDestination* const destination = | 213 CreateStream(kRenderProcessId, kRenderFrameId, 1); |
150 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | 214 const scoped_ptr<MockMirroringDestination> destination( |
151 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | 215 new MockMirroringDestination(kRenderProcessId, kRenderFrameId)); |
152 KillStream(kRenderProcessId, kRenderViewId, stream); | 216 StartMirroringTo(destination, 1); |
153 } | 217 EXPECT_EQ(1, destination->query_count()); |
154 | 218 EXPECT_EQ(1, CountStreamsDivertedTo(destination)); |
| 219 |
| 220 StopMirroringTo(destination); |
| 221 EXPECT_EQ(1, destination->query_count()); |
| 222 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 223 |
| 224 KillStream(stream); |
| 225 EXPECT_EQ(1, destination->query_count()); |
| 226 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 227 |
| 228 ExpectNoLongerManagingAnything(); |
| 229 } |
| 230 |
| 231 // Tests that a mirroring session starts before, and ends after, a stream that |
| 232 // will be diverted to it. |
155 TEST_F(AudioMirroringManagerTest, StreamLifetimeWithinMirroringSession) { | 233 TEST_F(AudioMirroringManagerTest, StreamLifetimeWithinMirroringSession) { |
156 MockMirroringDestination* const destination = | 234 const scoped_ptr<MockMirroringDestination> destination( |
157 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | 235 new MockMirroringDestination(kRenderProcessId, kRenderFrameId)); |
158 MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 1); | 236 StartMirroringTo(destination, 1); |
159 KillStream(kRenderProcessId, kRenderViewId, stream); | 237 EXPECT_EQ(0, destination->query_count()); |
160 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | 238 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
161 } | 239 |
162 | 240 MockDiverter* const stream = |
163 TEST_F(AudioMirroringManagerTest, StreamLifetimeAroundTwoMirroringSessions) { | 241 CreateStream(kRenderProcessId, kRenderFrameId, 1); |
164 MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 2); | 242 EXPECT_EQ(1, destination->query_count()); |
165 MockMirroringDestination* const destination = | 243 EXPECT_EQ(1, CountStreamsDivertedTo(destination)); |
166 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | 244 |
167 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | 245 KillStream(stream); |
168 MockMirroringDestination* const new_destination = | 246 EXPECT_EQ(1, destination->query_count()); |
169 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | 247 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
170 StopMirroringTo(kRenderProcessId, kRenderViewId, new_destination); | 248 |
171 KillStream(kRenderProcessId, kRenderViewId, stream); | 249 StopMirroringTo(destination); |
172 } | 250 EXPECT_EQ(1, destination->query_count()); |
173 | 251 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
174 TEST_F(AudioMirroringManagerTest, StreamLifetimeWithinTwoMirroringSessions) { | 252 |
175 MockMirroringDestination* const destination = | 253 ExpectNoLongerManagingAnything(); |
176 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | 254 } |
177 MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 2); | 255 |
178 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | 256 // Tests that a stream is diverted correctly as two mirroring sessions come and |
179 MockMirroringDestination* const new_destination = | 257 // go. |
180 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | 258 TEST_F(AudioMirroringManagerTest, StreamLifetimeAcrossTwoMirroringSessions) { |
181 KillStream(kRenderProcessId, kRenderViewId, stream); | 259 MockDiverter* const stream = |
182 StopMirroringTo(kRenderProcessId, kRenderViewId, new_destination); | 260 CreateStream(kRenderProcessId, kRenderFrameId, 2); |
183 } | 261 |
184 | 262 const scoped_ptr<MockMirroringDestination> destination( |
| 263 new MockMirroringDestination(kRenderProcessId, kRenderFrameId)); |
| 264 StartMirroringTo(destination, 1); |
| 265 EXPECT_EQ(1, destination->query_count()); |
| 266 EXPECT_EQ(1, CountStreamsDivertedTo(destination)); |
| 267 |
| 268 StopMirroringTo(destination); |
| 269 EXPECT_EQ(1, destination->query_count()); |
| 270 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 271 |
| 272 const scoped_ptr<MockMirroringDestination> second_destination( |
| 273 new MockMirroringDestination(kRenderProcessId, kRenderFrameId)); |
| 274 StartMirroringTo(second_destination, 1); |
| 275 EXPECT_EQ(1, destination->query_count()); |
| 276 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 277 EXPECT_EQ(1, second_destination->query_count()); |
| 278 EXPECT_EQ(1, CountStreamsDivertedTo(second_destination)); |
| 279 |
| 280 StopMirroringTo(second_destination); |
| 281 EXPECT_EQ(1, destination->query_count()); |
| 282 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 283 EXPECT_EQ(1, second_destination->query_count()); |
| 284 EXPECT_EQ(0, CountStreamsDivertedTo(second_destination)); |
| 285 |
| 286 KillStream(stream); |
| 287 EXPECT_EQ(1, destination->query_count()); |
| 288 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 289 EXPECT_EQ(1, second_destination->query_count()); |
| 290 EXPECT_EQ(0, CountStreamsDivertedTo(second_destination)); |
| 291 |
| 292 ExpectNoLongerManagingAnything(); |
| 293 } |
| 294 |
| 295 // Tests that a stream does not flip-flop between two destinations that are a |
| 296 // match for it. |
| 297 TEST_F(AudioMirroringManagerTest, StreamDivertingStickyToOneDestination_1) { |
| 298 MockDiverter* const stream = |
| 299 CreateStream(kRenderProcessId, kRenderFrameId, 2); |
| 300 |
| 301 const scoped_ptr<MockMirroringDestination> destination( |
| 302 new MockMirroringDestination(kRenderProcessId, kRenderFrameId)); |
| 303 StartMirroringTo(destination, 1); |
| 304 EXPECT_EQ(1, destination->query_count()); |
| 305 EXPECT_EQ(1, CountStreamsDivertedTo(destination)); |
| 306 |
| 307 const scoped_ptr<MockMirroringDestination> replacement_destination( |
| 308 new MockMirroringDestination(kRenderProcessId, kRenderFrameId)); |
| 309 StartMirroringTo(replacement_destination, 1); |
| 310 EXPECT_EQ(1, destination->query_count()); |
| 311 EXPECT_EQ(1, CountStreamsDivertedTo(destination)); |
| 312 EXPECT_EQ(0, replacement_destination->query_count()); |
| 313 EXPECT_EQ(0, CountStreamsDivertedTo(replacement_destination)); |
| 314 |
| 315 StopMirroringTo(destination); |
| 316 EXPECT_EQ(1, destination->query_count()); |
| 317 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 318 EXPECT_EQ(1, replacement_destination->query_count()); |
| 319 EXPECT_EQ(1, CountStreamsDivertedTo(replacement_destination)); |
| 320 |
| 321 StopMirroringTo(replacement_destination); |
| 322 EXPECT_EQ(1, destination->query_count()); |
| 323 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 324 EXPECT_EQ(1, replacement_destination->query_count()); |
| 325 EXPECT_EQ(0, CountStreamsDivertedTo(replacement_destination)); |
| 326 |
| 327 KillStream(stream); |
| 328 EXPECT_EQ(1, destination->query_count()); |
| 329 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 330 EXPECT_EQ(1, replacement_destination->query_count()); |
| 331 EXPECT_EQ(0, CountStreamsDivertedTo(replacement_destination)); |
| 332 |
| 333 ExpectNoLongerManagingAnything(); |
| 334 } |
| 335 |
| 336 // Same as StreamDivertingStickyToOneDestination_1, with a different order of |
| 337 // operations that should have the same effects. |
| 338 TEST_F(AudioMirroringManagerTest, StreamDivertingStickyToOneDestination_2) { |
| 339 MockDiverter* const stream = |
| 340 CreateStream(kRenderProcessId, kRenderFrameId, 2); |
| 341 |
| 342 const scoped_ptr<MockMirroringDestination> destination( |
| 343 new MockMirroringDestination(kRenderProcessId, kRenderFrameId)); |
| 344 StartMirroringTo(destination, 1); |
| 345 EXPECT_EQ(1, destination->query_count()); |
| 346 EXPECT_EQ(1, CountStreamsDivertedTo(destination)); |
| 347 |
| 348 const scoped_ptr<MockMirroringDestination> replacement_destination( |
| 349 new MockMirroringDestination(kRenderProcessId, kRenderFrameId)); |
| 350 StartMirroringTo(replacement_destination, 1); |
| 351 EXPECT_EQ(1, destination->query_count()); |
| 352 EXPECT_EQ(1, CountStreamsDivertedTo(destination)); |
| 353 EXPECT_EQ(0, replacement_destination->query_count()); |
| 354 EXPECT_EQ(0, CountStreamsDivertedTo(replacement_destination)); |
| 355 |
| 356 StopMirroringTo(destination); |
| 357 EXPECT_EQ(1, destination->query_count()); |
| 358 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 359 EXPECT_EQ(1, replacement_destination->query_count()); |
| 360 EXPECT_EQ(1, CountStreamsDivertedTo(replacement_destination)); |
| 361 |
| 362 KillStream(stream); |
| 363 EXPECT_EQ(1, destination->query_count()); |
| 364 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 365 EXPECT_EQ(1, replacement_destination->query_count()); |
| 366 EXPECT_EQ(0, CountStreamsDivertedTo(replacement_destination)); |
| 367 |
| 368 StopMirroringTo(replacement_destination); |
| 369 EXPECT_EQ(1, destination->query_count()); |
| 370 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 371 EXPECT_EQ(1, replacement_destination->query_count()); |
| 372 EXPECT_EQ(0, CountStreamsDivertedTo(replacement_destination)); |
| 373 |
| 374 ExpectNoLongerManagingAnything(); |
| 375 } |
| 376 |
| 377 // Same as StreamDivertingStickyToOneDestination_1, except that the stream is |
| 378 // killed before the first destination is stopped. Therefore, the second |
| 379 // destination should never see the stream. |
| 380 TEST_F(AudioMirroringManagerTest, StreamDivertingStickyToOneDestination_3) { |
| 381 MockDiverter* const stream = |
| 382 CreateStream(kRenderProcessId, kRenderFrameId, 1); |
| 383 |
| 384 const scoped_ptr<MockMirroringDestination> destination( |
| 385 new MockMirroringDestination(kRenderProcessId, kRenderFrameId)); |
| 386 StartMirroringTo(destination, 1); |
| 387 EXPECT_EQ(1, destination->query_count()); |
| 388 EXPECT_EQ(1, CountStreamsDivertedTo(destination)); |
| 389 |
| 390 const scoped_ptr<MockMirroringDestination> replacement_destination( |
| 391 new MockMirroringDestination(kRenderProcessId, kRenderFrameId)); |
| 392 StartMirroringTo(replacement_destination, 0); |
| 393 EXPECT_EQ(1, destination->query_count()); |
| 394 EXPECT_EQ(1, CountStreamsDivertedTo(destination)); |
| 395 EXPECT_EQ(0, replacement_destination->query_count()); |
| 396 EXPECT_EQ(0, CountStreamsDivertedTo(replacement_destination)); |
| 397 |
| 398 KillStream(stream); |
| 399 EXPECT_EQ(1, destination->query_count()); |
| 400 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 401 EXPECT_EQ(0, replacement_destination->query_count()); |
| 402 EXPECT_EQ(0, CountStreamsDivertedTo(replacement_destination)); |
| 403 |
| 404 StopMirroringTo(destination); |
| 405 EXPECT_EQ(1, destination->query_count()); |
| 406 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 407 EXPECT_EQ(0, replacement_destination->query_count()); |
| 408 EXPECT_EQ(0, CountStreamsDivertedTo(replacement_destination)); |
| 409 |
| 410 StopMirroringTo(replacement_destination); |
| 411 EXPECT_EQ(1, destination->query_count()); |
| 412 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 413 EXPECT_EQ(0, replacement_destination->query_count()); |
| 414 EXPECT_EQ(0, CountStreamsDivertedTo(replacement_destination)); |
| 415 |
| 416 ExpectNoLongerManagingAnything(); |
| 417 } |
| 418 |
| 419 // Tests that multiple streams are diverted/mixed to one destination. |
185 TEST_F(AudioMirroringManagerTest, MultipleStreamsInOneMirroringSession) { | 420 TEST_F(AudioMirroringManagerTest, MultipleStreamsInOneMirroringSession) { |
186 MockDiverter* const stream1 = | 421 MockDiverter* const stream1 = |
187 CreateStream(kRenderProcessId, kRenderViewId, 1); | 422 CreateStream(kRenderProcessId, kRenderFrameId, 1); |
188 MockMirroringDestination* const destination = | 423 |
189 StartMirroringTo(kRenderProcessId, kRenderViewId, 3); | 424 const scoped_ptr<MockMirroringDestination> destination( |
| 425 new MockMirroringDestination(kRenderProcessId, kRenderFrameId)); |
| 426 StartMirroringTo(destination, 3); |
| 427 EXPECT_EQ(1, destination->query_count()); |
| 428 EXPECT_EQ(1, CountStreamsDivertedTo(destination)); |
| 429 |
190 MockDiverter* const stream2 = | 430 MockDiverter* const stream2 = |
191 CreateStream(kRenderProcessId, kRenderViewId, 1); | 431 CreateStream(kRenderProcessId, kRenderFrameId, 1); |
| 432 EXPECT_EQ(2, destination->query_count()); |
| 433 EXPECT_EQ(2, CountStreamsDivertedTo(destination)); |
| 434 |
192 MockDiverter* const stream3 = | 435 MockDiverter* const stream3 = |
193 CreateStream(kRenderProcessId, kRenderViewId, 1); | 436 CreateStream(kRenderProcessId, kRenderFrameId, 1); |
194 KillStream(kRenderProcessId, kRenderViewId, stream2); | 437 EXPECT_EQ(3, destination->query_count()); |
195 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | 438 EXPECT_EQ(3, CountStreamsDivertedTo(destination)); |
196 KillStream(kRenderProcessId, kRenderViewId, stream3); | 439 |
197 KillStream(kRenderProcessId, kRenderViewId, stream1); | 440 KillStream(stream2); |
| 441 EXPECT_EQ(3, destination->query_count()); |
| 442 EXPECT_EQ(2, CountStreamsDivertedTo(destination)); |
| 443 |
| 444 StopMirroringTo(destination); |
| 445 EXPECT_EQ(3, destination->query_count()); |
| 446 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 447 |
| 448 KillStream(stream3); |
| 449 EXPECT_EQ(3, destination->query_count()); |
| 450 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 451 |
| 452 KillStream(stream1); |
| 453 EXPECT_EQ(3, destination->query_count()); |
| 454 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 455 |
| 456 ExpectNoLongerManagingAnything(); |
198 } | 457 } |
199 | 458 |
200 // A random interleaving of operations for three separate targets, each of which | 459 // A random interleaving of operations for three separate targets, each of which |
201 // has one stream mirrored to one destination. | 460 // has one stream mirrored to one destination. |
202 TEST_F(AudioMirroringManagerTest, ThreeSeparateMirroringSessions) { | 461 TEST_F(AudioMirroringManagerTest, ThreeSeparateMirroringSessions) { |
203 MockDiverter* const stream = | 462 MockDiverter* const stream = |
204 CreateStream(kRenderProcessId, kRenderViewId, 1); | 463 CreateStream(kRenderProcessId, kRenderFrameId, 1); |
205 MockMirroringDestination* const destination = | 464 |
206 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | 465 const scoped_ptr<MockMirroringDestination> destination( |
207 | 466 new MockMirroringDestination(kRenderProcessId, kRenderFrameId)); |
208 MockMirroringDestination* const another_destination = | 467 StartMirroringTo(destination, 1); |
209 StartMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId, 1); | 468 EXPECT_EQ(1, destination->query_count()); |
| 469 EXPECT_EQ(1, CountStreamsDivertedTo(destination)); |
| 470 |
| 471 const scoped_ptr<MockMirroringDestination> another_destination( |
| 472 new MockMirroringDestination(kAnotherRenderProcessId, |
| 473 kAnotherRenderFrameId)); |
| 474 StartMirroringTo(another_destination, 1); |
| 475 EXPECT_EQ(1, destination->query_count()); |
| 476 EXPECT_EQ(1, CountStreamsDivertedTo(destination)); |
| 477 EXPECT_EQ(0, another_destination->query_count()); |
| 478 EXPECT_EQ(0, CountStreamsDivertedTo(another_destination)); |
| 479 |
210 MockDiverter* const another_stream = | 480 MockDiverter* const another_stream = |
211 CreateStream(kAnotherRenderProcessId, kAnotherRenderViewId, 1); | 481 CreateStream(kAnotherRenderProcessId, kAnotherRenderFrameId, 1); |
212 | 482 EXPECT_EQ(2, destination->query_count()); |
213 KillStream(kRenderProcessId, kRenderViewId, stream); | 483 EXPECT_EQ(1, CountStreamsDivertedTo(destination)); |
| 484 EXPECT_EQ(1, another_destination->query_count()); |
| 485 EXPECT_EQ(1, CountStreamsDivertedTo(another_destination)); |
| 486 |
| 487 KillStream(stream); |
| 488 EXPECT_EQ(2, destination->query_count()); |
| 489 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 490 EXPECT_EQ(1, another_destination->query_count()); |
| 491 EXPECT_EQ(1, CountStreamsDivertedTo(another_destination)); |
214 | 492 |
215 MockDiverter* const yet_another_stream = | 493 MockDiverter* const yet_another_stream = |
216 CreateStream(kYetAnotherRenderProcessId, kYetAnotherRenderViewId, 1); | 494 CreateStream(kYetAnotherRenderProcessId, kYetAnotherRenderFrameId, 1); |
217 MockMirroringDestination* const yet_another_destination = | 495 EXPECT_EQ(3, destination->query_count()); |
218 StartMirroringTo(kYetAnotherRenderProcessId, kYetAnotherRenderViewId, 1); | 496 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
219 | 497 EXPECT_EQ(2, another_destination->query_count()); |
220 StopMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId, | 498 EXPECT_EQ(1, CountStreamsDivertedTo(another_destination)); |
221 another_destination); | 499 |
222 | 500 const scoped_ptr<MockMirroringDestination> yet_another_destination( |
223 StopMirroringTo(kYetAnotherRenderProcessId, kYetAnotherRenderViewId, | 501 new MockMirroringDestination(kYetAnotherRenderProcessId, |
224 yet_another_destination); | 502 kYetAnotherRenderFrameId)); |
225 | 503 StartMirroringTo(yet_another_destination, 1); |
226 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | 504 EXPECT_EQ(3, destination->query_count()); |
227 | 505 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
228 KillStream(kAnotherRenderProcessId, kAnotherRenderViewId, another_stream); | 506 EXPECT_EQ(2, another_destination->query_count()); |
229 KillStream(kYetAnotherRenderProcessId, kYetAnotherRenderViewId, | 507 EXPECT_EQ(1, CountStreamsDivertedTo(another_destination)); |
230 yet_another_stream); | 508 EXPECT_EQ(1, yet_another_destination->query_count()); |
| 509 EXPECT_EQ(1, CountStreamsDivertedTo(yet_another_destination)); |
| 510 |
| 511 StopMirroringTo(another_destination); |
| 512 EXPECT_EQ(4, destination->query_count()); |
| 513 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 514 EXPECT_EQ(2, another_destination->query_count()); |
| 515 EXPECT_EQ(0, CountStreamsDivertedTo(another_destination)); |
| 516 EXPECT_EQ(2, yet_another_destination->query_count()); |
| 517 EXPECT_EQ(1, CountStreamsDivertedTo(yet_another_destination)); |
| 518 |
| 519 StopMirroringTo(yet_another_destination); |
| 520 EXPECT_EQ(5, destination->query_count()); |
| 521 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 522 EXPECT_EQ(2, another_destination->query_count()); |
| 523 EXPECT_EQ(0, CountStreamsDivertedTo(another_destination)); |
| 524 EXPECT_EQ(2, yet_another_destination->query_count()); |
| 525 EXPECT_EQ(0, CountStreamsDivertedTo(yet_another_destination)); |
| 526 |
| 527 StopMirroringTo(destination); |
| 528 EXPECT_EQ(5, destination->query_count()); |
| 529 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 530 EXPECT_EQ(2, another_destination->query_count()); |
| 531 EXPECT_EQ(0, CountStreamsDivertedTo(another_destination)); |
| 532 EXPECT_EQ(2, yet_another_destination->query_count()); |
| 533 EXPECT_EQ(0, CountStreamsDivertedTo(yet_another_destination)); |
| 534 |
| 535 KillStream(another_stream); |
| 536 EXPECT_EQ(5, destination->query_count()); |
| 537 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 538 EXPECT_EQ(2, another_destination->query_count()); |
| 539 EXPECT_EQ(0, CountStreamsDivertedTo(another_destination)); |
| 540 EXPECT_EQ(2, yet_another_destination->query_count()); |
| 541 EXPECT_EQ(0, CountStreamsDivertedTo(yet_another_destination)); |
| 542 |
| 543 KillStream(yet_another_stream); |
| 544 EXPECT_EQ(5, destination->query_count()); |
| 545 EXPECT_EQ(0, CountStreamsDivertedTo(destination)); |
| 546 EXPECT_EQ(2, another_destination->query_count()); |
| 547 EXPECT_EQ(0, CountStreamsDivertedTo(another_destination)); |
| 548 EXPECT_EQ(2, yet_another_destination->query_count()); |
| 549 EXPECT_EQ(0, CountStreamsDivertedTo(yet_another_destination)); |
| 550 |
| 551 ExpectNoLongerManagingAnything(); |
231 } | 552 } |
232 | 553 |
233 } // namespace content | 554 } // namespace content |
OLD | NEW |