OLD | NEW |
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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 #elif defined(OS_WIN) | 53 #elif defined(OS_WIN) |
54 typedef media::AudioManagerWin AudioManagerPlatform; | 54 typedef media::AudioManagerWin AudioManagerPlatform; |
55 #elif defined(OS_ANDROID) | 55 #elif defined(OS_ANDROID) |
56 typedef media::AudioManagerAndroid AudioManagerPlatform; | 56 typedef media::AudioManagerAndroid AudioManagerPlatform; |
57 #else | 57 #else |
58 typedef media::FakeAudioManager AudioManagerPlatform; | 58 typedef media::FakeAudioManager AudioManagerPlatform; |
59 #endif | 59 #endif |
60 | 60 |
61 namespace { | 61 namespace { |
62 | 62 |
63 std::string ReturnMockSalt() { | 63 const char kMockSalt[] = ""; |
64 return std::string(); | |
65 } | |
66 | |
67 ResourceContext::SaltCallback GetMockSaltCallback() { | |
68 return base::Bind(&ReturnMockSalt); | |
69 } | |
70 | 64 |
71 // This class mocks the audio manager and overrides some methods to ensure that | 65 // This class mocks the audio manager and overrides some methods to ensure that |
72 // we can run our tests on the buildbots. | 66 // we can run our tests on the buildbots. |
73 class MockAudioManager : public AudioManagerPlatform { | 67 class MockAudioManager : public AudioManagerPlatform { |
74 public: | 68 public: |
75 MockAudioManager() | 69 MockAudioManager() |
76 : AudioManagerPlatform(base::ThreadTaskRunnerHandle::Get(), | 70 : AudioManagerPlatform(base::ThreadTaskRunnerHandle::Get(), |
77 base::ThreadTaskRunnerHandle::Get(), | 71 base::ThreadTaskRunnerHandle::Get(), |
78 &fake_audio_log_factory_), | 72 &fake_audio_log_factory_), |
79 num_output_devices_(2), | 73 num_output_devices_(2), |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 // quit the test. | 303 // quit the test. |
310 EXPECT_CALL(*this, Response(1)); | 304 EXPECT_CALL(*this, Response(1)); |
311 run_loop_.Run(); | 305 run_loop_.Run(); |
312 } | 306 } |
313 | 307 |
314 TEST_F(MediaStreamManagerTest, DeviceID) { | 308 TEST_F(MediaStreamManagerTest, DeviceID) { |
315 url::Origin security_origin(GURL("http://localhost")); | 309 url::Origin security_origin(GURL("http://localhost")); |
316 const std::string unique_default_id( | 310 const std::string unique_default_id( |
317 media::AudioDeviceDescription::kDefaultDeviceId); | 311 media::AudioDeviceDescription::kDefaultDeviceId); |
318 const std::string hashed_default_id = | 312 const std::string hashed_default_id = |
319 MediaStreamManager::GetHMACForMediaDeviceID( | 313 MediaStreamManager::GetHMACForMediaDeviceID(kMockSalt, security_origin, |
320 GetMockSaltCallback(), security_origin, unique_default_id); | 314 unique_default_id); |
321 EXPECT_TRUE(MediaStreamManager::DoesMediaDeviceIDMatchHMAC( | 315 EXPECT_TRUE(MediaStreamManager::DoesMediaDeviceIDMatchHMAC( |
322 GetMockSaltCallback(), security_origin, hashed_default_id, | 316 kMockSalt, security_origin, hashed_default_id, unique_default_id)); |
323 unique_default_id)); | |
324 EXPECT_EQ(unique_default_id, hashed_default_id); | 317 EXPECT_EQ(unique_default_id, hashed_default_id); |
325 | 318 |
326 const std::string unique_communications_id( | 319 const std::string unique_communications_id( |
327 media::AudioDeviceDescription::kCommunicationsDeviceId); | 320 media::AudioDeviceDescription::kCommunicationsDeviceId); |
328 const std::string hashed_communications_id = | 321 const std::string hashed_communications_id = |
329 MediaStreamManager::GetHMACForMediaDeviceID( | 322 MediaStreamManager::GetHMACForMediaDeviceID(kMockSalt, security_origin, |
330 GetMockSaltCallback(), security_origin, unique_communications_id); | 323 unique_communications_id); |
331 EXPECT_TRUE(MediaStreamManager::DoesMediaDeviceIDMatchHMAC( | 324 EXPECT_TRUE(MediaStreamManager::DoesMediaDeviceIDMatchHMAC( |
332 GetMockSaltCallback(), security_origin, hashed_communications_id, | 325 kMockSalt, security_origin, hashed_communications_id, |
333 unique_communications_id)); | 326 unique_communications_id)); |
334 EXPECT_EQ(unique_communications_id, hashed_communications_id); | 327 EXPECT_EQ(unique_communications_id, hashed_communications_id); |
335 | 328 |
336 const std::string unique_other_id("other-unique-id"); | 329 const std::string unique_other_id("other-unique-id"); |
337 const std::string hashed_other_id = | 330 const std::string hashed_other_id = |
338 MediaStreamManager::GetHMACForMediaDeviceID( | 331 MediaStreamManager::GetHMACForMediaDeviceID(kMockSalt, security_origin, |
339 GetMockSaltCallback(), security_origin, unique_other_id); | 332 unique_other_id); |
340 EXPECT_TRUE(MediaStreamManager::DoesMediaDeviceIDMatchHMAC( | 333 EXPECT_TRUE(MediaStreamManager::DoesMediaDeviceIDMatchHMAC( |
341 GetMockSaltCallback(), security_origin, hashed_other_id, | 334 kMockSalt, security_origin, hashed_other_id, unique_other_id)); |
342 unique_other_id)); | |
343 EXPECT_NE(unique_other_id, hashed_other_id); | 335 EXPECT_NE(unique_other_id, hashed_other_id); |
344 EXPECT_EQ(hashed_other_id.size(), 64U); | 336 EXPECT_EQ(hashed_other_id.size(), 64U); |
345 for (const char& c : hashed_other_id) | 337 for (const char& c : hashed_other_id) |
346 EXPECT_TRUE(base::IsAsciiDigit(c) || (c >= 'a' && c <= 'f')); | 338 EXPECT_TRUE(base::IsAsciiDigit(c) || (c >= 'a' && c <= 'f')); |
347 } | 339 } |
348 | 340 |
349 TEST_F(MediaStreamManagerTest, EnumerationOutputDevices) { | 341 TEST_F(MediaStreamManagerTest, EnumerationOutputDevices) { |
350 for (size_t num_devices = 0; num_devices < 3; num_devices++) { | 342 for (size_t num_devices = 0; num_devices < 3; num_devices++) { |
351 audio_manager_->SetNumAudioOutputDevices(num_devices); | 343 audio_manager_->SetNumAudioOutputDevices(num_devices); |
352 base::RunLoop run_loop; | 344 base::RunLoop run_loop; |
353 MockMediaStreamRequester requester(media_stream_manager_.get(), &run_loop, | 345 MockMediaStreamRequester requester(media_stream_manager_.get(), &run_loop, |
354 num_devices == 0 ? 0 : num_devices + 1, | 346 num_devices == 0 ? 0 : num_devices + 1, |
355 nullptr); | 347 nullptr); |
356 const int render_process_id = 1; | 348 const int render_process_id = 1; |
357 const int render_frame_id = 1; | 349 const int render_frame_id = 1; |
358 const int page_request_id = 1; | 350 const int page_request_id = 1; |
359 const url::Origin security_origin(GURL("http://localhost")); | 351 const url::Origin security_origin(GURL("http://localhost")); |
360 EXPECT_CALL(requester, | 352 EXPECT_CALL(requester, |
361 MockDevicesEnumerated(render_frame_id, page_request_id, _, _)); | 353 MockDevicesEnumerated(render_frame_id, page_request_id, _, _)); |
362 std::string label = media_stream_manager_->EnumerateDevices( | 354 std::string label = media_stream_manager_->EnumerateDevices( |
363 &requester, render_process_id, render_frame_id, GetMockSaltCallback(), | 355 &requester, render_process_id, render_frame_id, kMockSalt, |
364 page_request_id, MEDIA_DEVICE_AUDIO_OUTPUT, security_origin); | 356 page_request_id, MEDIA_DEVICE_AUDIO_OUTPUT, security_origin); |
365 run_loop.Run(); | 357 run_loop.Run(); |
366 // CancelRequest is necessary for enumeration requests. | 358 // CancelRequest is necessary for enumeration requests. |
367 media_stream_manager_->CancelRequest(label); | 359 media_stream_manager_->CancelRequest(label); |
368 } | 360 } |
369 } | 361 } |
370 | 362 |
371 TEST_F(MediaStreamManagerTest, NotifyDeviceChanges) { | 363 TEST_F(MediaStreamManagerTest, NotifyDeviceChanges) { |
372 const int render_process_id = 1; | 364 const int render_process_id = 1; |
373 const int render_frame_id = 1; | 365 const int render_frame_id = 1; |
374 const int page_request_id = 1; | 366 const int page_request_id = 1; |
375 const url::Origin security_origin(GURL("http://localhost")); | 367 const url::Origin security_origin(GURL("http://localhost")); |
376 | 368 |
377 // Check that device change notifications are received | 369 // Check that device change notifications are received |
378 { | 370 { |
379 // First run an enumeration to warm up the cache | 371 // First run an enumeration to warm up the cache |
380 base::RunLoop run_loop_enumeration; | 372 base::RunLoop run_loop_enumeration; |
381 base::RunLoop run_loop_device_change; | 373 base::RunLoop run_loop_device_change; |
382 MockMediaStreamRequester requester(media_stream_manager_.get(), | 374 MockMediaStreamRequester requester(media_stream_manager_.get(), |
383 &run_loop_enumeration, 3, | 375 &run_loop_enumeration, 3, |
384 &run_loop_device_change); | 376 &run_loop_device_change); |
385 audio_manager_->SetNumAudioInputDevices(2); | 377 audio_manager_->SetNumAudioInputDevices(2); |
386 requester.SubscribeToDeviceChangeNotifications(); | 378 requester.SubscribeToDeviceChangeNotifications(); |
387 | 379 |
388 EXPECT_CALL(requester, | 380 EXPECT_CALL(requester, |
389 MockDevicesEnumerated(render_frame_id, page_request_id, _, _)); | 381 MockDevicesEnumerated(render_frame_id, page_request_id, _, _)); |
390 EXPECT_CALL(requester, MockDevicesChanged(_)).Times(0); | 382 EXPECT_CALL(requester, MockDevicesChanged(_)).Times(0); |
391 std::string label = media_stream_manager_->EnumerateDevices( | 383 std::string label = media_stream_manager_->EnumerateDevices( |
392 &requester, render_process_id, render_frame_id, GetMockSaltCallback(), | 384 &requester, render_process_id, render_frame_id, kMockSalt, |
393 page_request_id, MEDIA_DEVICE_AUDIO_CAPTURE, security_origin); | 385 page_request_id, MEDIA_DEVICE_AUDIO_CAPTURE, security_origin); |
394 run_loop_enumeration.Run(); | 386 run_loop_enumeration.Run(); |
395 media_stream_manager_->CancelRequest(label); | 387 media_stream_manager_->CancelRequest(label); |
396 | 388 |
397 // Simulate device change | 389 // Simulate device change |
398 EXPECT_CALL(requester, MockDevicesChanged(_)); | 390 EXPECT_CALL(requester, MockDevicesChanged(_)); |
399 audio_manager_->SetNumAudioInputDevices(3); | 391 audio_manager_->SetNumAudioInputDevices(3); |
400 media_stream_manager_->OnDevicesChanged( | 392 media_stream_manager_->OnDevicesChanged( |
401 base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE); | 393 base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE); |
402 run_loop_device_change.Run(); | 394 run_loop_device_change.Run(); |
(...skipping 10 matching lines...) Expand all Loading... |
413 // Bogus OnDeviceChange, as devices have not changed. Should not trigger | 405 // Bogus OnDeviceChange, as devices have not changed. Should not trigger |
414 // notification. | 406 // notification. |
415 EXPECT_CALL(requester, MockDevicesChanged(_)).Times(0); | 407 EXPECT_CALL(requester, MockDevicesChanged(_)).Times(0); |
416 media_stream_manager_->OnDevicesChanged( | 408 media_stream_manager_->OnDevicesChanged( |
417 base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE); | 409 base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE); |
418 | 410 |
419 // Do enumeration to be able to quit the RunLoop. | 411 // Do enumeration to be able to quit the RunLoop. |
420 EXPECT_CALL(requester, | 412 EXPECT_CALL(requester, |
421 MockDevicesEnumerated(render_frame_id, page_request_id, _, _)); | 413 MockDevicesEnumerated(render_frame_id, page_request_id, _, _)); |
422 std::string label = media_stream_manager_->EnumerateDevices( | 414 std::string label = media_stream_manager_->EnumerateDevices( |
423 &requester, render_process_id, render_frame_id, GetMockSaltCallback(), | 415 &requester, render_process_id, render_frame_id, kMockSalt, |
424 page_request_id, MEDIA_DEVICE_AUDIO_CAPTURE, security_origin); | 416 page_request_id, MEDIA_DEVICE_AUDIO_CAPTURE, security_origin); |
425 run_loop.Run(); | 417 run_loop.Run(); |
426 media_stream_manager_->CancelRequest(label); | 418 media_stream_manager_->CancelRequest(label); |
427 } | 419 } |
428 } | 420 } |
429 | 421 |
430 } // namespace content | 422 } // namespace content |
OLD | NEW |