Chromium Code Reviews| 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_output_delegate.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/command_line.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "content/browser/media/capture/audio_mirroring_manager.h" | |
| 15 #include "content/browser/renderer_host/media/media_stream_manager.h" | |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | |
| 17 #include "media/audio/audio_manager.h" | |
| 18 #include "media/audio/fake_audio_log_factory.h" | |
| 19 #include "media/base/media_switches.h" | |
| 20 #include "testing/gmock/include/gmock/gmock.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 using ::testing::_; | |
| 24 using ::testing::InSequence; | |
| 25 using ::testing::NiceMock; | |
| 26 using ::testing::NotNull; | |
| 27 | |
| 28 namespace content { | |
|
o1ka
2016/11/03 13:39:12
nit: empty line after each namespace {
Max Morin
2016/11/18 16:03:42
Done.
| |
| 29 namespace { | |
| 30 const int kRenderProcessId = 1; | |
| 31 const int kRenderFrameId = 5; | |
| 32 const int kStreamId = 50; | |
| 33 const char kDefaultDeviceId[] = ""; | |
| 34 | |
| 35 struct MockAudioMirroringManager : public AudioMirroringManager { | |
| 36 MockAudioMirroringManager() : AudioMirroringManager() {} | |
| 37 virtual ~MockAudioMirroringManager() {} | |
| 38 | |
| 39 MOCK_METHOD3(AddDiverter, | |
| 40 void(int render_process_id, | |
| 41 int render_frame_id, | |
| 42 Diverter* diverter)); | |
| 43 MOCK_METHOD1(RemoveDiverter, void(Diverter* diverter)); | |
| 44 }; | |
| 45 | |
| 46 struct MockObserver : public content::MediaObserver { | |
| 47 void OnAudioCaptureDevicesChanged() override {} | |
| 48 void OnVideoCaptureDevicesChanged() override {} | |
| 49 void OnMediaRequestStateChanged(int render_process_id, | |
| 50 int render_frame_id, | |
| 51 int page_request_id, | |
| 52 const GURL& security_origin, | |
| 53 MediaStreamType stream_type, | |
| 54 MediaRequestState state) override {} | |
| 55 void OnSetCapturingLinkSecured(int render_process_id, | |
| 56 int render_frame_id, | |
| 57 int page_request_id, | |
| 58 MediaStreamType stream_type, | |
| 59 bool is_secure) override {} | |
| 60 | |
| 61 MOCK_METHOD2(OnCreatingAudioStream, | |
| 62 void(int render_process_id, int render_frame_id)); | |
| 63 }; | |
| 64 | |
| 65 struct MockEventHandler : public AudioOutputDelegate::EventHandler { | |
| 66 MOCK_METHOD1(OnStreamStateChanged, void(bool playing)); | |
| 67 MOCK_METHOD3(OnStreamCreated, | |
| 68 void(int stream_id, | |
| 69 base::SharedMemory* shared_memory, | |
| 70 base::CancelableSyncSocket* socket)); | |
| 71 MOCK_METHOD1(OnStreamError, void(int stream_id)); | |
| 72 }; | |
| 73 | |
| 74 } // namespace | |
| 75 | |
| 76 class AudioOutputDelegateTest : public testing::Test { | |
| 77 public: | |
| 78 AudioOutputDelegateTest() { | |
| 79 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 80 switches::kUseFakeDeviceForMediaStream); | |
| 81 | |
| 82 audio_manager_ = media::AudioManager::CreateForTesting( | |
| 83 base::ThreadTaskRunnerHandle::Get()); | |
| 84 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); | |
| 85 } | |
| 86 | |
| 87 ~AudioOutputDelegateTest() override {} | |
| 88 | |
| 89 AudioOutputDelegate::UniquePtr Create() { | |
| 90 return AudioOutputDelegate::Create( | |
| 91 &event_handler_, audio_manager_.get(), | |
| 92 log_factory_.CreateAudioLog( | |
| 93 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER), | |
| 94 &mirroring_manager_, &media_observer_, kStreamId, kRenderFrameId, | |
| 95 kRenderProcessId, audio_manager_->GetDefaultOutputStreamParameters(), | |
| 96 kDefaultDeviceId); | |
| 97 } | |
| 98 | |
| 99 protected: | |
| 100 // MediaStreamManager uses a DestructionObserver, so it must outlive the | |
| 101 // TestBrowserThreadBundle. | |
| 102 std::unique_ptr<MediaStreamManager> media_stream_manager_; | |
| 103 TestBrowserThreadBundle thread_bundle_; | |
|
o1ka
2016/11/03 13:39:12
Do we want to have some real threads? (I think we
Max Morin
2016/11/18 16:03:42
Done. I learned from the authorization CL that a s
| |
| 104 media::ScopedAudioManagerPtr audio_manager_; | |
| 105 NiceMock<MockAudioMirroringManager> mirroring_manager_; | |
| 106 NiceMock<MockEventHandler> event_handler_; | |
| 107 NiceMock<MockObserver> media_observer_; | |
| 108 media::FakeAudioLogFactory log_factory_; | |
| 109 | |
| 110 private: | |
| 111 DISALLOW_COPY_AND_ASSIGN(AudioOutputDelegateTest); | |
| 112 }; | |
| 113 | |
| 114 TEST_F(AudioOutputDelegateTest, Create_AlertsObserver) { | |
| 115 EXPECT_CALL(media_observer_, | |
| 116 OnCreatingAudioStream(kRenderProcessId, kRenderFrameId)) | |
| 117 .Times(1); | |
| 118 | |
| 119 AudioOutputDelegate::UniquePtr delegate = Create(); | |
| 120 // Due to complex destruction of AudioOutputDelegate, we first flush all | |
| 121 // operations, then destroy, then make sure the destruction is done by | |
| 122 // calling RunUntilIdle again. | |
| 123 base::RunLoop().RunUntilIdle(); | |
| 124 delegate.reset(); | |
| 125 base::RunLoop().RunUntilIdle(); | |
| 126 } | |
| 127 | |
| 128 TEST_F(AudioOutputDelegateTest, Create_CallsHandler) { | |
| 129 EXPECT_CALL(event_handler_, OnStreamCreated(kStreamId, NotNull(), NotNull())); | |
| 130 | |
| 131 AudioOutputDelegate::UniquePtr delegate = Create(); | |
| 132 base::RunLoop().RunUntilIdle(); | |
| 133 delegate.reset(); | |
| 134 base::RunLoop().RunUntilIdle(); | |
| 135 } | |
| 136 | |
| 137 TEST_F(AudioOutputDelegateTest, CreateAndDestroy_DoesNotCallHandler) { | |
| 138 EXPECT_CALL(event_handler_, OnStreamCreated(_, _, _)).Times(0); | |
| 139 | |
| 140 AudioOutputDelegate::UniquePtr delegate = Create(); | |
| 141 delegate.reset(); | |
| 142 base::RunLoop().RunUntilIdle(); | |
| 143 } | |
| 144 | |
| 145 TEST_F(AudioOutputDelegateTest, | |
| 146 Create_RegistersAndUnregistersControllerWithMirroringManager) { | |
| 147 { | |
| 148 InSequence s; | |
| 149 EXPECT_CALL(mirroring_manager_, | |
| 150 AddDiverter(kRenderProcessId, kRenderFrameId, NotNull())) | |
| 151 .Times(1); | |
| 152 EXPECT_CALL(mirroring_manager_, RemoveDiverter(NotNull())).Times(1); | |
| 153 } | |
| 154 | |
| 155 AudioOutputDelegate::UniquePtr delegate = Create(); | |
| 156 base::RunLoop().RunUntilIdle(); | |
| 157 delegate.reset(); | |
| 158 base::RunLoop().RunUntilIdle(); | |
| 159 } | |
| 160 | |
| 161 TEST_F(AudioOutputDelegateTest, Play_CallsHandler) { | |
| 162 { | |
| 163 InSequence s; | |
| 164 EXPECT_CALL(event_handler_, OnStreamStateChanged(true)).Times(1); | |
| 165 EXPECT_CALL(event_handler_, OnStreamStateChanged(false)).Times(1); | |
| 166 } | |
| 167 | |
| 168 AudioOutputDelegate::UniquePtr delegate = Create(); | |
| 169 delegate->OnPlayStream(); | |
| 170 base::RunLoop().RunUntilIdle(); | |
| 171 delegate.reset(); | |
| 172 base::RunLoop().RunUntilIdle(); | |
| 173 } | |
| 174 | |
| 175 TEST_F(AudioOutputDelegateTest, PlayAndDestroy_DoesNotCallHandler) { | |
| 176 EXPECT_CALL(event_handler_, OnStreamStateChanged(_)).Times(0); | |
| 177 | |
| 178 AudioOutputDelegate::UniquePtr delegate = Create(); | |
| 179 delegate->OnPlayStream(); | |
| 180 delegate.reset(); | |
| 181 base::RunLoop().RunUntilIdle(); | |
| 182 } | |
| 183 | |
| 184 TEST_F(AudioOutputDelegateTest, Error_CallsHandler) { | |
| 185 EXPECT_CALL(event_handler_, OnStreamError(kStreamId)).Times(1); | |
| 186 | |
| 187 AudioOutputDelegate::UniquePtr delegate = Create(); | |
| 188 delegate->controller()->OnError(nullptr); | |
| 189 base::RunLoop().RunUntilIdle(); | |
| 190 delegate.reset(); | |
| 191 base::RunLoop().RunUntilIdle(); | |
| 192 } | |
| 193 | |
| 194 TEST_F(AudioOutputDelegateTest, ErrorAndDestroy_DoesNotCallHandler) { | |
| 195 EXPECT_CALL(event_handler_, OnStreamError(kStreamId)).Times(0); | |
| 196 | |
| 197 AudioOutputDelegate::UniquePtr delegate = Create(); | |
| 198 delegate->controller()->OnError(nullptr); | |
| 199 delegate.reset(); | |
| 200 base::RunLoop().RunUntilIdle(); | |
| 201 } | |
| 202 | |
| 203 TEST_F(AudioOutputDelegateTest, PlayPause_CallsHandlerCorrectNumberOfTimes) { | |
| 204 { | |
| 205 InSequence s; | |
| 206 EXPECT_CALL(event_handler_, OnStreamStateChanged(true)).Times(1); | |
| 207 EXPECT_CALL(event_handler_, OnStreamStateChanged(false)).Times(1); | |
| 208 } | |
| 209 | |
| 210 AudioOutputDelegate::UniquePtr delegate = Create(); | |
| 211 delegate->OnPlayStream(); | |
| 212 delegate->OnPauseStream(); | |
| 213 base::RunLoop().RunUntilIdle(); | |
| 214 delegate.reset(); | |
| 215 base::RunLoop().RunUntilIdle(); | |
| 216 } | |
| 217 | |
| 218 TEST_F(AudioOutputDelegateTest, | |
| 219 PlayPausePlay_CallsHandlerCorrectNumberOfTimes) { | |
| 220 { | |
| 221 InSequence s; | |
| 222 EXPECT_CALL(event_handler_, OnStreamStateChanged(true)).Times(1); | |
| 223 EXPECT_CALL(event_handler_, OnStreamStateChanged(false)).Times(1); | |
| 224 EXPECT_CALL(event_handler_, OnStreamStateChanged(true)).Times(1); | |
| 225 EXPECT_CALL(event_handler_, OnStreamStateChanged(false)).Times(1); | |
| 226 } | |
| 227 | |
| 228 AudioOutputDelegate::UniquePtr delegate = Create(); | |
| 229 delegate->OnPlayStream(); | |
| 230 delegate->OnPauseStream(); | |
| 231 delegate->OnPlayStream(); | |
| 232 base::RunLoop().RunUntilIdle(); | |
| 233 delegate.reset(); | |
| 234 base::RunLoop().RunUntilIdle(); | |
| 235 } | |
| 236 | |
| 237 TEST_F(AudioOutputDelegateTest, | |
| 238 PlayPausePlayPause_CallsHandlerCorrectNumberOfTimes) { | |
| 239 { | |
| 240 InSequence s; | |
| 241 EXPECT_CALL(event_handler_, OnStreamStateChanged(true)).Times(1); | |
| 242 EXPECT_CALL(event_handler_, OnStreamStateChanged(false)).Times(1); | |
| 243 EXPECT_CALL(event_handler_, OnStreamStateChanged(true)).Times(1); | |
| 244 EXPECT_CALL(event_handler_, OnStreamStateChanged(false)).Times(1); | |
| 245 } | |
| 246 | |
| 247 AudioOutputDelegate::UniquePtr delegate = Create(); | |
| 248 delegate->OnPlayStream(); | |
| 249 delegate->OnPauseStream(); | |
| 250 delegate->OnPlayStream(); | |
| 251 delegate->OnPauseStream(); | |
| 252 base::RunLoop().RunUntilIdle(); | |
| 253 delegate.reset(); | |
| 254 base::RunLoop().RunUntilIdle(); | |
| 255 } | |
| 256 | |
| 257 TEST_F(AudioOutputDelegateTest, PlayPlay_CallsHandlerCorrectNumberOfTimes) { | |
| 258 { | |
| 259 InSequence s; | |
| 260 EXPECT_CALL(event_handler_, OnStreamStateChanged(true)).Times(1); | |
| 261 EXPECT_CALL(event_handler_, OnStreamStateChanged(false)).Times(1); | |
| 262 } | |
| 263 | |
| 264 AudioOutputDelegate::UniquePtr delegate = Create(); | |
| 265 delegate->OnPlayStream(); | |
| 266 delegate->OnPlayStream(); | |
| 267 base::RunLoop().RunUntilIdle(); | |
| 268 delegate.reset(); | |
| 269 base::RunLoop().RunUntilIdle(); | |
| 270 } | |
| 271 | |
| 272 } // namespace content | |
| OLD | NEW |