Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: content/browser/renderer_host/media/audio_renderer_host_unittest.cc

Issue 1171953002: Add IPC interface for switching the audio output device for a given audio stream in the browser. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address reviewers' comments Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/sync_socket.h" 9 #include "base/sync_socket.h"
10 #include "content/browser/media/capture/audio_mirroring_manager.h" 10 #include "content/browser/media/capture/audio_mirroring_manager.h"
(...skipping 13 matching lines...) Expand all
24 24
25 using ::testing::_; 25 using ::testing::_;
26 using ::testing::Assign; 26 using ::testing::Assign;
27 using ::testing::DoAll; 27 using ::testing::DoAll;
28 using ::testing::NotNull; 28 using ::testing::NotNull;
29 29
30 namespace { 30 namespace {
31 const int kRenderProcessId = 1; 31 const int kRenderProcessId = 1;
32 const int kRenderFrameId = 5; 32 const int kRenderFrameId = 5;
33 const int kStreamId = 50; 33 const int kStreamId = 50;
34 const int kBadStreamId = 99;
35 const int kSwitchOutputDeviceRequestId = 1;
36 const GURL kSecurityOrigin("http://localhost");
37 const std::string kDefaultDeviceID = "";
38 const std::string kBadDeviceID = "bad-device-id";
34 } // namespace 39 } // namespace
35 40
36 namespace content { 41 namespace content {
37 42
38 class MockAudioMirroringManager : public AudioMirroringManager { 43 class MockAudioMirroringManager : public AudioMirroringManager {
39 public: 44 public:
40 MockAudioMirroringManager() {} 45 MockAudioMirroringManager() {}
41 virtual ~MockAudioMirroringManager() {} 46 virtual ~MockAudioMirroringManager() {}
42 47
43 MOCK_METHOD3(AddDiverter, 48 MOCK_METHOD3(AddDiverter,
44 void(int render_process_id, 49 void(int render_process_id,
45 int render_frame_id, 50 int render_frame_id,
46 Diverter* diverter)); 51 Diverter* diverter));
47 MOCK_METHOD1(RemoveDiverter, void(Diverter* diverter)); 52 MOCK_METHOD1(RemoveDiverter, void(Diverter* diverter));
48 53
49 private: 54 private:
50 DISALLOW_COPY_AND_ASSIGN(MockAudioMirroringManager); 55 DISALLOW_COPY_AND_ASSIGN(MockAudioMirroringManager);
51 }; 56 };
52 57
53 class MockAudioRendererHost : public AudioRendererHost { 58 class MockAudioRendererHost : public AudioRendererHost {
54 public: 59 public:
55 MockAudioRendererHost(media::AudioManager* audio_manager, 60 MockAudioRendererHost(media::AudioManager* audio_manager,
56 AudioMirroringManager* mirroring_manager, 61 AudioMirroringManager* mirroring_manager,
57 MediaInternals* media_internals, 62 MediaInternals* media_internals,
58 MediaStreamManager* media_stream_manager) 63 MediaStreamManager* media_stream_manager,
64 const ResourceContext::SaltCallback& salt_callback)
59 : AudioRendererHost(kRenderProcessId, 65 : AudioRendererHost(kRenderProcessId,
60 audio_manager, 66 audio_manager,
61 mirroring_manager, 67 mirroring_manager,
62 media_internals, 68 media_internals,
63 media_stream_manager), 69 media_stream_manager,
70 salt_callback),
64 shared_memory_length_(0) {} 71 shared_memory_length_(0) {}
65 72
66 // A list of mock methods. 73 // A list of mock methods.
67 MOCK_METHOD2(OnStreamCreated, void(int stream_id, int length)); 74 MOCK_METHOD2(OnStreamCreated, void(int stream_id, int length));
68 MOCK_METHOD1(OnStreamPlaying, void(int stream_id)); 75 MOCK_METHOD1(OnStreamPlaying, void(int stream_id));
69 MOCK_METHOD1(OnStreamPaused, void(int stream_id)); 76 MOCK_METHOD1(OnStreamPaused, void(int stream_id));
70 MOCK_METHOD1(OnStreamError, void(int stream_id)); 77 MOCK_METHOD1(OnStreamError, void(int stream_id));
78 MOCK_METHOD3(
79 OnOutputDeviceSwitched,
80 void(int stream_id,
81 int request_id,
82 media::AudioOutputIPCDelegate::SwitchOutputDeviceResult result));
71 83
72 private: 84 private:
73 virtual ~MockAudioRendererHost() { 85 virtual ~MockAudioRendererHost() {
74 // Make sure all audio streams have been deleted. 86 // Make sure all audio streams have been deleted.
75 EXPECT_TRUE(audio_entries_.empty()); 87 EXPECT_TRUE(audio_entries_.empty());
76 } 88 }
77 89
78 // This method is used to dispatch IPC messages to the renderer. We intercept 90 // This method is used to dispatch IPC messages to the renderer. We intercept
79 // these messages here and dispatch to our mock methods to verify the 91 // these messages here and dispatch to our mock methods to verify the
80 // conversation between this object and the renderer. 92 // conversation between this object and the renderer.
81 virtual bool Send(IPC::Message* message) { 93 virtual bool Send(IPC::Message* message) {
82 CHECK(message); 94 CHECK(message);
83 95
84 // In this method we dispatch the messages to the according handlers as if 96 // In this method we dispatch the messages to the according handlers as if
85 // we are the renderer. 97 // we are the renderer.
86 bool handled = true; 98 bool handled = true;
87 IPC_BEGIN_MESSAGE_MAP(MockAudioRendererHost, *message) 99 IPC_BEGIN_MESSAGE_MAP(MockAudioRendererHost, *message)
88 IPC_MESSAGE_HANDLER(AudioMsg_NotifyStreamCreated, 100 IPC_MESSAGE_HANDLER(AudioMsg_NotifyStreamCreated,
89 OnNotifyStreamCreated) 101 OnNotifyStreamCreated)
90 IPC_MESSAGE_HANDLER(AudioMsg_NotifyStreamStateChanged, 102 IPC_MESSAGE_HANDLER(AudioMsg_NotifyStreamStateChanged,
91 OnNotifyStreamStateChanged) 103 OnNotifyStreamStateChanged)
104 IPC_MESSAGE_HANDLER(AudioMsg_NotifyOutputDeviceSwitched,
105 OnNotifyOutputDeviceSwitched)
92 IPC_MESSAGE_UNHANDLED(handled = false) 106 IPC_MESSAGE_UNHANDLED(handled = false)
93 IPC_END_MESSAGE_MAP() 107 IPC_END_MESSAGE_MAP()
94 EXPECT_TRUE(handled); 108 EXPECT_TRUE(handled);
95 109
96 delete message; 110 delete message;
97 return true; 111 return true;
98 } 112 }
99 113
100 void OnNotifyStreamCreated( 114 void OnNotifyStreamCreated(
101 int stream_id, base::SharedMemoryHandle handle, 115 int stream_id, base::SharedMemoryHandle handle,
(...skipping 24 matching lines...) Expand all
126 break; 140 break;
127 case media::AudioOutputIPCDelegate::kError: 141 case media::AudioOutputIPCDelegate::kError:
128 OnStreamError(stream_id); 142 OnStreamError(stream_id);
129 break; 143 break;
130 default: 144 default:
131 FAIL() << "Unknown stream state"; 145 FAIL() << "Unknown stream state";
132 break; 146 break;
133 } 147 }
134 } 148 }
135 149
150 void OnNotifyOutputDeviceSwitched(
151 int stream_id,
152 int request_id,
153 media::AudioOutputIPCDelegate::SwitchOutputDeviceResult result) {
154 switch (result) {
155 case media::AudioOutputIPCDelegate::kSuccess:
156 case media::AudioOutputIPCDelegate::kNotFoundError:
157 case media::AudioOutputIPCDelegate::kSecurityError:
158 case media::AudioOutputIPCDelegate::kObsoleteError:
159 case media::AudioOutputIPCDelegate::kNotSupportedError:
160 OnOutputDeviceSwitched(stream_id, request_id, result);
161 break;
162 default:
163 FAIL() << "Unknown SwitchOutputDevice result";
164 break;
165 }
166 }
167
136 scoped_ptr<base::SharedMemory> shared_memory_; 168 scoped_ptr<base::SharedMemory> shared_memory_;
137 scoped_ptr<base::SyncSocket> sync_socket_; 169 scoped_ptr<base::SyncSocket> sync_socket_;
138 uint32 shared_memory_length_; 170 uint32 shared_memory_length_;
139 171
140 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); 172 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost);
141 }; 173 };
142 174
175 namespace {
176 std::string ReturnMockSalt() {
177 return std::string();
178 }
179
180 ResourceContext::SaltCallback GetMockSaltCallback() {
181 return base::Bind(&ReturnMockSalt);
182 }
183 }
184
143 class AudioRendererHostTest : public testing::Test { 185 class AudioRendererHostTest : public testing::Test {
144 public: 186 public:
145 AudioRendererHostTest() { 187 AudioRendererHostTest() {
146 audio_manager_.reset(media::AudioManager::CreateForTesting()); 188 audio_manager_.reset(media::AudioManager::CreateForTesting());
147 base::CommandLine::ForCurrentProcess()->AppendSwitch( 189 base::CommandLine::ForCurrentProcess()->AppendSwitch(
148 switches::kUseFakeDeviceForMediaStream); 190 switches::kUseFakeDeviceForMediaStream);
149 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); 191 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get()));
150 host_ = new MockAudioRendererHost(audio_manager_.get(), 192 host_ = new MockAudioRendererHost(audio_manager_.get(), &mirroring_manager_,
151 &mirroring_manager_,
152 MediaInternals::GetInstance(), 193 MediaInternals::GetInstance(),
153 media_stream_manager_.get()); 194 media_stream_manager_.get(),
195 GetMockSaltCallback());
154 196
155 // Simulate IPC channel connected. 197 // Simulate IPC channel connected.
156 host_->set_peer_process_for_testing(base::Process::Current()); 198 host_->set_peer_process_for_testing(base::Process::Current());
157 } 199 }
158 200
159 ~AudioRendererHostTest() override { 201 ~AudioRendererHostTest() override {
160 // Simulate closing the IPC channel and give the audio thread time to close 202 // Simulate closing the IPC channel and give the audio thread time to close
161 // the underlying streams. 203 // the underlying streams.
162 host_->OnChannelClosing(); 204 host_->OnChannelClosing();
163 SyncWithAudioThread(); 205 SyncWithAudioThread();
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 EXPECT_CALL(*host_.get(), OnStreamPaused(kStreamId)); 265 EXPECT_CALL(*host_.get(), OnStreamPaused(kStreamId));
224 host_->OnPauseStream(kStreamId); 266 host_->OnPauseStream(kStreamId);
225 SyncWithAudioThread(); 267 SyncWithAudioThread();
226 } 268 }
227 269
228 void SetVolume(double volume) { 270 void SetVolume(double volume) {
229 host_->OnSetVolume(kStreamId, volume); 271 host_->OnSetVolume(kStreamId, volume);
230 SyncWithAudioThread(); 272 SyncWithAudioThread();
231 } 273 }
232 274
275 void SwitchOutputDevice(
276 int stream_id,
277 std::string device_id,
278 media::AudioOutputIPCDelegate::SwitchOutputDeviceResult expected_result) {
279 EXPECT_CALL(*host_.get(),
280 OnOutputDeviceSwitched(stream_id, kSwitchOutputDeviceRequestId,
281 expected_result));
282 host_->OnSwitchOutputDevice(stream_id, kRenderFrameId, device_id,
283 kSecurityOrigin, kSwitchOutputDeviceRequestId);
284 SyncWithAudioThread();
285 }
286
233 void SimulateError() { 287 void SimulateError() {
234 EXPECT_EQ(1u, host_->audio_entries_.size()) 288 EXPECT_EQ(1u, host_->audio_entries_.size())
235 << "Calls Create() before calling this method"; 289 << "Calls Create() before calling this method";
236 290
237 // Expect an error signal sent through IPC. 291 // Expect an error signal sent through IPC.
238 EXPECT_CALL(*host_.get(), OnStreamError(kStreamId)); 292 EXPECT_CALL(*host_.get(), OnStreamError(kStreamId));
239 293
240 // Simulate an error sent from the audio device. 294 // Simulate an error sent from the audio device.
241 host_->ReportErrorAndClose(kStreamId); 295 host_->ReportErrorAndClose(kStreamId);
242 SyncWithAudioThread(); 296 SyncWithAudioThread();
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 } 348 }
295 349
296 TEST_F(AudioRendererHostTest, SetVolume) { 350 TEST_F(AudioRendererHostTest, SetVolume) {
297 Create(false); 351 Create(false);
298 SetVolume(0.5); 352 SetVolume(0.5);
299 Play(); 353 Play();
300 Pause(); 354 Pause();
301 Close(); 355 Close();
302 } 356 }
303 357
358 TEST_F(AudioRendererHostTest, SwitchOutputDevice) {
359 Create(false);
360 SwitchOutputDevice(kStreamId, kDefaultDeviceID,
361 media::AudioOutputIPCDelegate::kSuccess);
362 Close();
363 }
364
365 TEST_F(AudioRendererHostTest, SwitchOutputDeviceNotAuthorized) {
366 Create(false);
367 SwitchOutputDevice(kStreamId, kBadDeviceID,
368 media::AudioOutputIPCDelegate::kSecurityError);
369 Close();
370 }
371
372 TEST_F(AudioRendererHostTest, SwitchOutputDeviceNoStream) {
373 Create(false);
374 SwitchOutputDevice(kBadStreamId, kDefaultDeviceID,
375 media::AudioOutputIPCDelegate::kObsoleteError);
376 Close();
377 }
378
304 // Simulate the case where a stream is not properly closed. 379 // Simulate the case where a stream is not properly closed.
305 TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) { 380 TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) {
306 Create(false); 381 Create(false);
307 Play(); 382 Play();
308 } 383 }
309 384
310 // Simulate the case where a stream is not properly closed. 385 // Simulate the case where a stream is not properly closed.
311 TEST_F(AudioRendererHostTest, CreatePlayPauseAndShutdown) { 386 TEST_F(AudioRendererHostTest, CreatePlayPauseAndShutdown) {
312 Create(false); 387 Create(false);
313 Play(); 388 Play();
(...skipping 17 matching lines...) Expand all
331 } 406 }
332 407
333 TEST_F(AudioRendererHostTest, CreateUnifiedStreamAndClose) { 408 TEST_F(AudioRendererHostTest, CreateUnifiedStreamAndClose) {
334 Create(true); 409 Create(true);
335 Close(); 410 Close();
336 } 411 }
337 412
338 // TODO(hclam): Add tests for data conversation in low latency mode. 413 // TODO(hclam): Add tests for data conversation in low latency mode.
339 414
340 } // namespace content 415 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698