| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 // Unit tests for AudioOutputAuthorizationHandler. | 5 // Unit tests for AudioOutputAuthorizationHandler. |
| 6 | 6 |
| 7 #include "content/browser/renderer_host/media/audio_output_authorization_handler
.h" | 7 #include "content/browser/renderer_host/media/audio_output_authorization_handler
.h" |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "content/browser/audio_device_thread.h" |
| 12 #include "content/browser/browser_thread_impl.h" | 13 #include "content/browser/browser_thread_impl.h" |
| 13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/test/mock_render_process_host.h" | 15 #include "content/public/test/mock_render_process_host.h" |
| 15 #include "content/public/test/test_browser_context.h" | 16 #include "content/public/test/test_browser_context.h" |
| 16 #include "content/public/test/test_browser_thread.h" | |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "media/audio/audio_device_description.h" | 18 #include "media/audio/audio_device_description.h" |
| 19 #include "media/audio/fake_audio_log_factory.h" | 19 #include "media/audio/fake_audio_log_factory.h" |
| 20 #include "media/audio/fake_audio_manager.h" | 20 #include "media/audio/fake_audio_manager.h" |
| 21 #include "media/base/media_switches.h" | 21 #include "media/base/media_switches.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 25 #include "url/origin.h" | 25 #include "url/origin.h" |
| 26 | 26 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 64 |
| 65 class AudioOutputAuthorizationHandlerTest : public testing::Test { | 65 class AudioOutputAuthorizationHandlerTest : public testing::Test { |
| 66 public: | 66 public: |
| 67 AudioOutputAuthorizationHandlerTest() { | 67 AudioOutputAuthorizationHandlerTest() { |
| 68 // Not threadsafe, thus set before threads are started: | 68 // Not threadsafe, thus set before threads are started: |
| 69 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 69 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 70 switches::kUseFakeDeviceForMediaStream); | 70 switches::kUseFakeDeviceForMediaStream); |
| 71 | 71 |
| 72 thread_bundle_ = base::MakeUnique<TestBrowserThreadBundle>( | 72 thread_bundle_ = base::MakeUnique<TestBrowserThreadBundle>( |
| 73 TestBrowserThreadBundle::Options::REAL_IO_THREAD); | 73 TestBrowserThreadBundle::Options::REAL_IO_THREAD); |
| 74 audio_thread_ = base::MakeUnique<base::Thread>("AudioThread"); | 74 audio_thread_ = base::MakeUnique<AudioDeviceThread>(); |
| 75 | |
| 76 // Audio manager creation stolen from content/browser/browser_main_loop.cc. | |
| 77 #if defined(OS_WIN) | |
| 78 audio_thread_->init_com_with_mta(true); | |
| 79 #endif // defined(OS_WIN) | |
| 80 CHECK(audio_thread_->Start()); | |
| 81 | |
| 82 #if defined(OS_MACOSX) | |
| 83 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner = | |
| 84 base::ThreadTaskRunnerHandle::Get(); | |
| 85 #else | |
| 86 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner = | |
| 87 audio_thread_->task_runner(); | |
| 88 #endif // defined(OS_MACOSX) | |
| 89 audio_manager_.reset(new media::FakeAudioManager( | 75 audio_manager_.reset(new media::FakeAudioManager( |
| 90 audio_task_runner, audio_thread_->task_runner(), &log_factory_)); | 76 audio_thread_->task_runner(), audio_thread_->worker_task_runner(), |
| 77 &log_factory_)); |
| 91 media_stream_manager_ = | 78 media_stream_manager_ = |
| 92 base::MakeUnique<MediaStreamManager>(audio_manager_.get()); | 79 base::MakeUnique<MediaStreamManager>(audio_manager_.get()); |
| 93 // Make sure everything is done initializing: | 80 // Make sure everything is done initializing: |
| 94 SyncWithAllThreads(); | 81 SyncWithAllThreads(); |
| 95 } | 82 } |
| 96 | 83 |
| 97 ~AudioOutputAuthorizationHandlerTest() override { SyncWithAllThreads(); } | 84 ~AudioOutputAuthorizationHandlerTest() override { SyncWithAllThreads(); } |
| 98 | 85 |
| 99 protected: | 86 protected: |
| 100 MediaStreamManager* GetMediaStreamManager() { | 87 MediaStreamManager* GetMediaStreamManager() { |
| 101 return media_stream_manager_.get(); | 88 return media_stream_manager_.get(); |
| 102 } | 89 } |
| 103 | 90 |
| 104 media::AudioManager* GetAudioManager() { return audio_manager_.get(); } | 91 media::AudioManager* GetAudioManager() { return audio_manager_.get(); } |
| 105 | 92 |
| 106 void SyncWithAllThreads() { | 93 void SyncWithAllThreads() { |
| 107 // New tasks might be posted while we are syncing, but in | 94 // New tasks might be posted while we are syncing, but in |
| 108 // every iteration at least one task will be run. 20 iterations should be | 95 // every iteration at least one task will be run. 20 iterations should be |
| 109 // enough for our code. | 96 // enough for our code. |
| 110 for (int i = 0; i < 20; ++i) { | 97 for (int i = 0; i < 20; ++i) { |
| 111 base::RunLoop().RunUntilIdle(); | 98 base::RunLoop().RunUntilIdle(); |
| 112 SyncWith(BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)); | 99 SyncWith(BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)); |
| 113 SyncWith(audio_thread_->task_runner()); | 100 SyncWith(audio_manager_->GetWorkerTaskRunner()); |
| 114 } | 101 } |
| 115 } | 102 } |
| 116 | 103 |
| 117 std::string GetRawNondefaultId() { | 104 std::string GetRawNondefaultId() { |
| 118 std::string id; | 105 std::string id; |
| 119 BrowserThread::PostTask( | 106 BrowserThread::PostTask( |
| 120 BrowserThread::IO, FROM_HERE, | 107 BrowserThread::IO, FROM_HERE, |
| 121 base::Bind( | 108 base::Bind( |
| 122 &AudioOutputAuthorizationHandlerTest::GetRawNondefaultIdOnIOThread, | 109 &AudioOutputAuthorizationHandlerTest::GetRawNondefaultIdOnIOThread, |
| 123 base::Unretained(this), base::Unretained(&id))); | 110 base::Unretained(this), base::Unretained(&id))); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 152 *out = result[MediaDeviceType::MEDIA_DEVICE_TYPE_AUDIO_OUTPUT][1] | 139 *out = result[MediaDeviceType::MEDIA_DEVICE_TYPE_AUDIO_OUTPUT][1] |
| 153 .device_id; | 140 .device_id; |
| 154 }, | 141 }, |
| 155 base::Unretained(out))); | 142 base::Unretained(out))); |
| 156 } | 143 } |
| 157 | 144 |
| 158 // media_stream_manager must die after threads since it's a | 145 // media_stream_manager must die after threads since it's a |
| 159 // DestructionObserver. | 146 // DestructionObserver. |
| 160 std::unique_ptr<MediaStreamManager> media_stream_manager_; | 147 std::unique_ptr<MediaStreamManager> media_stream_manager_; |
| 161 std::unique_ptr<TestBrowserThreadBundle> thread_bundle_; | 148 std::unique_ptr<TestBrowserThreadBundle> thread_bundle_; |
| 162 std::unique_ptr<base::Thread> audio_thread_; | 149 std::unique_ptr<AudioDeviceThread> audio_thread_; |
| 163 media::FakeAudioLogFactory log_factory_; | 150 media::FakeAudioLogFactory log_factory_; |
| 164 media::ScopedAudioManagerPtr audio_manager_; | 151 media::ScopedAudioManagerPtr audio_manager_; |
| 165 | 152 |
| 166 DISALLOW_COPY_AND_ASSIGN(AudioOutputAuthorizationHandlerTest); | 153 DISALLOW_COPY_AND_ASSIGN(AudioOutputAuthorizationHandlerTest); |
| 167 }; | 154 }; |
| 168 | 155 |
| 169 TEST_F(AudioOutputAuthorizationHandlerTest, AuthorizeDefaultDevice_Ok) { | 156 TEST_F(AudioOutputAuthorizationHandlerTest, AuthorizeDefaultDevice_Ok) { |
| 170 MockListener listener; | 157 MockListener listener; |
| 171 EXPECT_CALL(listener, | 158 EXPECT_CALL(listener, |
| 172 MockAuthorizationCallback(media::OUTPUT_DEVICE_STATUS_OK, false, | 159 MockAuthorizationCallback(media::OUTPUT_DEVICE_STATUS_OK, false, |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 (base::Bind(&AudioOutputAuthorizationHandler::RequestDeviceAuthorization, | 336 (base::Bind(&AudioOutputAuthorizationHandler::RequestDeviceAuthorization, |
| 350 base::Unretained(handler.get()), kRenderFrameId, kSessionId, | 337 base::Unretained(handler.get()), kRenderFrameId, kSessionId, |
| 351 std::string(), BadSecurityOrigin(), listener.GetCallback()))); | 338 std::string(), BadSecurityOrigin(), listener.GetCallback()))); |
| 352 | 339 |
| 353 SyncWithAllThreads(); | 340 SyncWithAllThreads(); |
| 354 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, handler.release()); | 341 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, handler.release()); |
| 355 SyncWithAllThreads(); | 342 SyncWithAllThreads(); |
| 356 } | 343 } |
| 357 | 344 |
| 358 } // namespace content | 345 } // namespace content |
| OLD | NEW |