Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 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/message_loop.h" | |
| 12 #include "base/synchronization/waitable_event.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 { | |
|
darin (slow to review)
2013/01/09 05:06:53
nit: shouldn't these classes also be in the anonym
miu
2013/01/09 07:21:49
Done. Good call, since all these tests get linked
| |
| 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 mirroring_manager_.AddDiverter(render_process_id, render_view_id, diverter); | |
| 65 | |
| 66 return diverter; | |
| 67 } | |
| 68 | |
| 69 void KillStream( | |
| 70 int render_process_id, int render_view_id, MockDiverter* diverter) { | |
| 71 mirroring_manager_.RemoveDiverter( | |
| 72 render_process_id, render_view_id, diverter); | |
| 73 | |
| 74 delete diverter; | |
| 75 } | |
| 76 | |
| 77 MockMirroringDestination* StartMirroringTo( | |
| 78 int render_process_id, int render_view_id, int expected_inputs_added) { | |
| 79 MockMirroringDestination* const dest = new MockMirroringDestination(); | |
| 80 if (expected_inputs_added > 0) { | |
| 81 static AudioOutputStream* const kNonNullPointer = | |
| 82 reinterpret_cast<AudioOutputStream*>(0x11111110); | |
| 83 EXPECT_CALL(*dest, AddInput(Ref(params_))) | |
| 84 .Times(expected_inputs_added) | |
| 85 .WillRepeatedly(Return(kNonNullPointer)); | |
| 86 } | |
| 87 | |
| 88 mirroring_manager_.StartMirroring(render_process_id, render_view_id, dest); | |
| 89 | |
| 90 return dest; | |
| 91 } | |
| 92 | |
| 93 void StopMirroringTo(int render_process_id, int render_view_id, | |
| 94 MockMirroringDestination* dest) { | |
| 95 mirroring_manager_.StopMirroring(render_process_id, render_view_id, dest); | |
| 96 | |
| 97 delete dest; | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 MessageLoop message_loop_; | |
| 102 BrowserThreadImpl io_thread_; | |
| 103 AudioParameters params_; | |
| 104 AudioMirroringManager mirroring_manager_; | |
| 105 | |
| 106 DISALLOW_COPY_AND_ASSIGN(AudioMirroringManagerTest); | |
| 107 }; | |
| 108 | |
| 109 namespace { | |
| 110 const int kRenderProcessId = 123; | |
| 111 const int kRenderViewId = 456; | |
| 112 const int kAnotherRenderProcessId = 789; | |
| 113 const int kAnotherRenderViewId = 1234; | |
| 114 const int kYetAnotherRenderProcessId = 4560; | |
| 115 const int kYetAnotherRenderViewId = 7890; | |
| 116 } | |
| 117 | |
| 118 TEST_F(AudioMirroringManagerTest, MirroringSessionOfNothing) { | |
| 119 MockMirroringDestination* const destination = | |
| 120 StartMirroringTo(kRenderProcessId, kRenderViewId, 0); | |
| 121 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
| 122 } | |
| 123 | |
| 124 TEST_F(AudioMirroringManagerTest, TwoMirroringSessionsOfNothing) { | |
| 125 MockMirroringDestination* const destination = | |
| 126 StartMirroringTo(kRenderProcessId, kRenderViewId, 0); | |
| 127 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
| 128 | |
| 129 MockMirroringDestination* const another_destination = | |
| 130 StartMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId, 0); | |
| 131 StopMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId, | |
| 132 another_destination); | |
| 133 } | |
| 134 | |
| 135 TEST_F(AudioMirroringManagerTest, SwitchMirroringDestinationNoStreams) { | |
| 136 MockMirroringDestination* const destination = | |
| 137 StartMirroringTo(kRenderProcessId, kRenderViewId, 0); | |
| 138 MockMirroringDestination* const new_destination = | |
| 139 StartMirroringTo(kRenderProcessId, kRenderViewId, 0); | |
| 140 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
| 141 StopMirroringTo(kRenderProcessId, kRenderViewId, new_destination); | |
| 142 } | |
| 143 | |
| 144 TEST_F(AudioMirroringManagerTest, StreamLifetimeAroundMirroringSession) { | |
| 145 MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 1); | |
| 146 MockMirroringDestination* const destination = | |
| 147 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | |
| 148 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
| 149 KillStream(kRenderProcessId, kRenderViewId, stream); | |
| 150 } | |
| 151 | |
| 152 TEST_F(AudioMirroringManagerTest, StreamLifetimeWithinMirroringSession) { | |
| 153 MockMirroringDestination* const destination = | |
| 154 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | |
| 155 MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 1); | |
| 156 KillStream(kRenderProcessId, kRenderViewId, stream); | |
| 157 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
| 158 } | |
| 159 | |
| 160 TEST_F(AudioMirroringManagerTest, StreamLifetimeAroundTwoMirroringSessions) { | |
| 161 MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 2); | |
| 162 MockMirroringDestination* const destination = | |
| 163 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | |
| 164 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
| 165 MockMirroringDestination* const new_destination = | |
| 166 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | |
| 167 StopMirroringTo(kRenderProcessId, kRenderViewId, new_destination); | |
| 168 KillStream(kRenderProcessId, kRenderViewId, stream); | |
| 169 } | |
| 170 | |
| 171 TEST_F(AudioMirroringManagerTest, StreamLifetimeWithinTwoMirroringSessions) { | |
| 172 MockMirroringDestination* const destination = | |
| 173 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | |
| 174 MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 2); | |
| 175 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
| 176 MockMirroringDestination* const new_destination = | |
| 177 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | |
| 178 KillStream(kRenderProcessId, kRenderViewId, stream); | |
| 179 StopMirroringTo(kRenderProcessId, kRenderViewId, new_destination); | |
| 180 } | |
| 181 | |
| 182 TEST_F(AudioMirroringManagerTest, MultipleStreamsInOneMirroringSession) { | |
| 183 MockDiverter* const stream1 = | |
| 184 CreateStream(kRenderProcessId, kRenderViewId, 1); | |
| 185 MockMirroringDestination* const destination = | |
| 186 StartMirroringTo(kRenderProcessId, kRenderViewId, 3); | |
| 187 MockDiverter* const stream2 = | |
| 188 CreateStream(kRenderProcessId, kRenderViewId, 1); | |
| 189 MockDiverter* const stream3 = | |
| 190 CreateStream(kRenderProcessId, kRenderViewId, 1); | |
| 191 KillStream(kRenderProcessId, kRenderViewId, stream2); | |
| 192 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
| 193 KillStream(kRenderProcessId, kRenderViewId, stream3); | |
| 194 KillStream(kRenderProcessId, kRenderViewId, stream1); | |
| 195 } | |
| 196 | |
| 197 // A random interleaving of operations for three separate targets, each of which | |
| 198 // has one stream mirrored to one destination. | |
| 199 TEST_F(AudioMirroringManagerTest, ThreeSeparateMirroringSessions) { | |
| 200 MockDiverter* const stream = | |
| 201 CreateStream(kRenderProcessId, kRenderViewId, 1); | |
| 202 MockMirroringDestination* const destination = | |
| 203 StartMirroringTo(kRenderProcessId, kRenderViewId, 1); | |
| 204 | |
| 205 MockMirroringDestination* const another_destination = | |
| 206 StartMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId, 1); | |
| 207 MockDiverter* const another_stream = | |
| 208 CreateStream(kAnotherRenderProcessId, kAnotherRenderViewId, 1); | |
| 209 | |
| 210 KillStream(kRenderProcessId, kRenderViewId, stream); | |
| 211 | |
| 212 MockDiverter* const yet_another_stream = | |
| 213 CreateStream(kYetAnotherRenderProcessId, kYetAnotherRenderViewId, 1); | |
| 214 MockMirroringDestination* const yet_another_destination = | |
| 215 StartMirroringTo(kYetAnotherRenderProcessId, kYetAnotherRenderViewId, 1); | |
| 216 | |
| 217 StopMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId, | |
| 218 another_destination); | |
| 219 | |
| 220 StopMirroringTo(kYetAnotherRenderProcessId, kYetAnotherRenderViewId, | |
| 221 yet_another_destination); | |
| 222 | |
| 223 StopMirroringTo(kRenderProcessId, kRenderViewId, destination); | |
| 224 | |
| 225 KillStream(kAnotherRenderProcessId, kAnotherRenderViewId, another_stream); | |
| 226 KillStream(kYetAnotherRenderProcessId, kYetAnotherRenderViewId, | |
| 227 yet_another_stream); | |
| 228 } | |
| 229 | |
| 230 } // namespace content | |
| OLD | NEW |