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